Repository navigation
fix(default): accept cell paths for column arguments - #19058
Merged
Merged
Conversation
`default` typed its column arguments as plain strings, so `default 5 a.b` added a literal "a.b" key instead of filling the nested field. The rest arguments are now cell paths. Each path is followed to decide whether the value is missing, null, or (with --empty) empty, and then filled with `upsert_data_at_cell_path`, which creates intermediate records on demand the same way `upsert` does. Indexing into a scalar now reports the usual cannot-find-column error instead of inserting a bogus key.
fdncred
pushed a commit
that referenced
this pull request
Nov 21, 2026
## Description Fixes #4274. `compact` read its column arguments as plain strings, so a nested path was looked up as one literal key and every row was dropped: ```nu [{labels: {c: 1}} {labels: {c: null}}] | compact labels.c # before: [] # after: [{labels: {c: 1}}] ``` The column arguments are now cell paths, the same change `default` got in #19058, so `compact` and `default` read columns the same way: - `compact a.b` follows the nested path; `--empty` applies to the nested value too (`[{a: {b: ""}} {a: {b: x}}] | compact -e a.b` keeps only the second row; main dropped both). - Optional members work: `compact a?.b` drops rows where `a` or `b` is missing or null. - A missing column or path still drops the row, as before. Record input and lists of non-records behave as before. - A path that starts with a number, like `compact 2024` or `compact 0`, is an error that asks for a quoted name (`compact "2024"`). Main also errored on these, with "can't convert int to string"; without the check a number would parse as a row index and silently drop every record row. A number later in the path (`compact a.0`) works. - A plain column name still takes a single record lookup, so there is no slowdown for the common case (see below). ## User-facing changes (Release notes) - `compact` accepts cell paths: `compact labels.c` checks the nested field `c` inside `labels` instead of a column literally named `labels.c`. To match a column whose name contains a `.` or ends in `?` or `!`, quote it: `compact "a.b"`, `compact "a?"` (a quoted name or a string variable still matches the literal key). Unquoted, `a?` is an optional member and `a!` a case-insensitive one. - Passing a non-path argument such as a list or record now gives a parse/type error ("expected cell-path") instead of "Can't convert to string." A bare number gives "compact needs a column name; quote a number to match a column, like "2024"". ## Additional notes - Tests: 11 new tests in `crates/nu-command/tests/commands/compact.rs` (nested path, nested with `--empty`, missing nested path, several paths, plain top-level column, quoted dotted name, optional path, a number as the first member rejected on list rows and on record rows, quoted number column, number later in the path) and an `examples()` entry. The nested ones fail on main; the plain-column and quoted-name ones pin behaviour that did not change. The two rejection tests fail with the new check commented out. - Run on my machine: `cargo test -p nu-command --test tests -- commands::compact` (18 passed), `cargo test -p nu-command --lib -- compact` (5 passed), `cargo fmt --all --check` and `cargo clippy -p nu-command --all-targets -- -D warnings -D clippy::unwrap_used` are clean. - Performance (debug builds, 200k rows, median of 7 `timeit` runs): `compact a` 131 ms on main, 133 ms here; `compact a id s` 178 ms on main, 181 ms here. The first version of this change walked every column with `follow_cell_path` and was about 1.5-2x slower, so a single plain name now goes straight to `record.get` and only real paths walk. - I used AI assistance (Claude Code) while writing the code, the tests and this description. I read the diff, understand how it works, and checked the before/after outputs above myself; the tests listed above ran on my machine. --------- Co-authored-by: Cem Ali Polat <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
defaultdeclared its column arguments asSyntaxShape::String, so a dotted argument was treated as a literal key rather than a path. The signature even carried a TODO asking whether it should declare cell paths, since users kept assuming it did.In this PR the rest arguments become
SyntaxShape::CellPath.fill_recordnow works on aValueinstead of aRecord.User-facing changes (Release notes)
defaultnow accepts cell paths for its column arguments, sodefault 5 a.bfills the nested fieldbinsidea(creatingaif needed) instead of adding a literal"a.b"key. Quote the argument, as indefault 5 "a.b", to target a key that actually contains a dot.Additional notes