Repository navigation
Description
Describe the bug
sort-by and uniq-by both take a column name and perform some error checking that the column exists. Currently, the only check is that the first element of the list is a record with the specified column. When passed a heterogenous list (aka table with missing values), these commands have inconsistent behavior depending on whether or not the first row contains a missing value.
How to reproduce
[{a: 1} {}] | sort-by a # case 1
[{} {a: 1}] | sort-by b # case 2
[{a: 1} {}] | uniq-by a # case 3
[{} {a: 1}] | uniq-by b # case 4results:
Case 1:
╭───┬────╮
│ # │ a │
├───┼────┤
│ 0 │ 1 │
│ 1 │ ❎ │
╰───┴────╯
Case 2:
Error: nu::shell::column_not_found
× Cannot find column
╭─[entry #12:1:1]
1 │ [{} {a: 1}] | sort-by b # case 2
· ─┬ ───┬───
· │ ╰── cannot find column 'b'
· ╰── value originates here
╰────
Case 3:
╭───┬────╮
│ # │ a │
├───┼────┤
│ 0 │ 1 │
│ 1 │ ❎ │
╰───┴────╯
Case 4:
Error: nu::shell::column_not_found
× Cannot find column
╭─[entry #15:1:1]
1 │ [{} {a: 1}] | uniq-by b # case 4
· ─┬ ───┬───
· │ ╰── cannot find column 'b'
· ╰── value originates here
╰────
Expected behavior
Case 1 and 2 should produce the same output (or both should produce an error). Same for cases 3 and 4.
I'm not sure which choice makes more sense, but would lean towards both producing output. I think it's a little weird that we're giving comparison semantics to ❎, which isn't a real Value, but these semantics already exist in the current implementation.
Screenshots
No response
Configuration
| key | value |
|---|---|
| version | 0.77.0 |
| branch | |
| commit_hash | |
| build_os | macos-aarch64 |
| build_target | aarch64-apple-darwin |
| rust_version | rustc 1.67.1 (d5a82bbd2 2023-02-07) (built from a source tarball) |
| cargo_version | cargo 1.67.0 |
| build_time | 1980-01-01 00:00:00 +00:00 |
| build_rust_channel | release |
| features | default, zip |
| installed_plugins |
Additional context
The current comparison semantics for ❎ are also quite surprising.
[{a: 1} {} {a: 2} {a: null}] | sort-by a╭───┬────╮
│ # │ a │
├───┼────┤
│ 0 │ 1 │
│ 1 │ 2 │
│ 2 │ ❎ │
│ 3 │ │
╰───┴────╯
Looks like $x < ❎ < null for all $x != null. I would expect $x < ❎ for all $x or $x > ❎ for all $x.
It's possible that the change to fix this issue will eliminate these comparisons entirely, so I haven't created a separate issue for it yet.
Activity
[-]sort-by and similar handle missing values inconsistently[/-][+]sort-by and uniq-by handle missing values inconsistently[/+]Support passing an empty list to sort, uniq, sort-by, and uniq-by (is…
sholderbach commentedon Mar 30, 2023
Is this closed by #8669 or only related?
olivia-fl commentedon Apr 13, 2023
Only related. #8669 changed the behavior of
sort-byanduniq-byso that they don't perform column checking on an empty input, but the behavior for nonempty input is still inconsistent because it only checks the first row.Rework sorting and add cell path and closure comparators to `sort-by` (…
weirdan commentedon Feb 8, 2026
Cases 1, 2 and 4 now throw errors on 0.110.0, case 3 still produces a table.