Skip to content

fix(default): accept cell paths for column arguments - #19058

Merged
fdncred merged 1 commit into
nushell:mainfrom
fdncred:default-cell-path
Sep 21, 2026
Merged

fdncred merged 1 commit into
nushell:mainfrom
fdncred:default-cell-path

Conversation

@fdncred

@fdncred fdncred commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Description

default declared its column arguments as SyntaxShape::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.

# before
{a: {}} | default 5 a.b
# => {a: {}, "a.b": 5}

In this PR the rest arguments become SyntaxShape::CellPath. fill_record now works on a Value instead of a Record.

# after
{a: {}} | default 5 a.b        # => {a: {b: 5}}
{} | default 5 a.b             # => {a: {b: 5}}
{a: {b: null}} | default 5 a.b # => {a: {b: 5}}
{a: [1 null]} | default 5 a.1  # => {a: [1, 5]}
{"a.b": null} | default 5 "a.b" # => {"a.b": 5}   (quoted segment still targets the literal key)
{a: 1} | default 5 a.b         # => error: Cannot find column 'b'  (was silently {a: 1, "a.b": 5})
[{a: {}} {a: {c: 1}}] | default 5 a.c  # => [{a: {c: 5}}, {a: {c: 1}}]

User-facing changes (Release notes)

default now accepts cell paths for its column arguments, so default 5 a.b fills the nested field b inside a (creating a if needed) instead of adding a literal "a.b" key. Quote the argument, as in default 5 "a.b", to target a key that actually contains a dot.

Additional notes

`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 fdncred added notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes labels Sep 21, 2026
@fdncred
fdncred merged commit 6dceb6e into nushell:main Sep 21, 2026
15 checks passed
@fdncred
fdncred deleted the default-cell-path branch September 21, 2026 15:55
@github-actions github-actions Bot added this to the v0.116.0 milestone Sep 21, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant