Skip to content

compact: accept cell paths for column arguments - #19624

Merged
fdncred merged 6 commits into
nushell:mainfrom
cemaliplt:compact-cell-paths
Nov 21, 2026
Merged

fdncred merged 6 commits into
nushell:mainfrom
cemaliplt:compact-cell-paths

Conversation

@cemaliplt

@cemaliplt cemaliplt commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

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:

[{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.

Cem Ali Polat added 4 commits November 20, 2026 22:31
compact read its column arguments as literal top-level names, so a nested path like labels.c looked up a key named "labels.c" and dropped every row. The arguments are now cell paths, the same change made to default. The example for a nested null is added.
Covers a nested path that keeps and drops rows, --empty on a nested value, a missing nested path, several paths at once, a top-level column as a regression guard, and a quoted dotted name that still matches the literal key.
A plain column name is one record lookup. Only multi-member and nested paths still walk follow_cell_path. Missing columns and optional misses still drop the row, as before. Timed debug builds on 200k rows: compact a 211 -> 133 ms, compact a id s 337 -> 181 ms, same as main.
Pins that an optional nested member drops rows where it is missing or null, and that an int cell path leaves list rows unchanged.

@fdncred fdncred left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review overview

🟡 Changes recommended

compact labels.c now follows the nested path. Plain names, quoted dotted names, typed string params and spread string lists all behave as they do on main. What's left: a bare number now empties a table of records with no error, plus two small notes.

Findings: 1 High severity · 0 Medium severity · 2 Low severity

Open (3)

Notes from my clanker, checked on b4611fb. Built the branch, ran cargo test -p nu-command --test tests -- commands::compact (15 passed), and ran each case on the branch binary and on main (ca70639). CI's macOS and Windows lint jobs passed on this head, so I didn't rerun clippy.

Cem Ali Polat added 2 commits November 21, 2026 01:12
A bare number such as `compact 2024` parsed as a row index, so on record rows the lookup failed and every row was dropped with no error. Main errored here with "can't convert int to string". It now errors with a hint to quote the name. A number later in the path (`compact a.0`) still works. Replaces the list-row test with rejection tests and adds quoted-number and nested-index cases. Without the check, int_cell_path_is_rejected and number_column_is_rejected fail (16/18); with it, 18/18.
The do_keep closure had one caller left.
@fdncred fdncred added notes:ready Indicates Ready for Release notes notes:additions Noted in "Additions" section labels Nov 21, 2026
@fdncred
fdncred merged commit a661e23 into nushell:main Nov 21, 2026
14 of 15 checks passed
@fdncred

fdncred commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Thanks

@github-actions github-actions Bot added this to the v0.118.0 milestone Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:additions Noted in "Additions" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

compact does not support nested cell paths

2 participants