Skip to content

sort-by and uniq-by handle missing values inconsistently #8667

Closed

Description

@olivia-fl

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 4

results:

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

  1. changed the title [-]sort-by and similar handle missing values inconsistently[/-] [+]sort-by and uniq-by handle missing values inconsistently[/+] on Mar 29, 2023
  2. sholderbach commented on Mar 30, 2023

    @sholderbach
    Member

    Is this closed by #8669 or only related?

  3. added
    category:inconsistent-behavior
    semantics
    on Mar 30, 2023
  4. olivia-fl commented on Apr 13, 2023

    @olivia-fl
    ContributorAuthor

    Only related. #8669 changed the behavior of sort-by and uniq-by so 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.

  5. weirdan commented on Feb 8, 2026

    @weirdan
    Contributor

    Cases 1, 2 and 4 now throw errors on 0.110.0, case 3 still produces a table.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    category:inconsistent-behaviorsemantics

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions