Change group-by to accept cell paths (#9020)

Closes #9003.

This PR changes `group-by` so that its optional argument is interpreted
as a cell path. In turn, this lets users use `?` to ignore rows that are
missing the column they wish to group on. For example:

```
> [{foo: 123}, {foo: 234}, {bar: 345}] | group-by foo
Error: nu:🐚:column_not_found

  × Cannot find column
   ╭─[entry #3:1:1]
 1 │ [{foo: 123}, {foo: 234}, {bar: 345}] | group-by foo
   ·                          ─────┬────             ─┬─
   ·                               │                  ╰── cannot find column 'foo'
   ·                               ╰── value originates here
   ╰────

> [{foo: 123}, {foo: 234}, {bar: 345}] | group-by foo?
╭─────┬───────────────╮
│ 123 │ [table 1 row] │
│ 234 │ [table 1 row] │
╰─────┴───────────────╯
```

~~This removes the ability to pass `group-by` a closure or block (I
wasn't able to figure out how to make the 2 features coexist), and so it
is a breaking change. I think this is OK; I didn't even know `group-by`
could accept a closure or block because there was no example for that
functionality.~~
This commit is contained in:
Reilly Wood
2023-05-17 16:34:44 -07:00
committed by GitHub
parent b912d4c1ea
commit 9ce61dc677
4 changed files with 212 additions and 157 deletions

View File

@ -72,7 +72,7 @@ fn errors_if_given_unknown_column_name() {
}
#[test]
fn errors_if_block_given_evaluates_more_than_one_row() {
fn errors_if_column_not_found() {
Playground::setup("group_by_test_3", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"los_tres_caballeros.csv",
@ -92,21 +92,19 @@ fn errors_if_block_given_evaluates_more_than_one_row() {
"#
));
assert!(actual.err.contains("value originates here"),);
assert!(actual.err.contains("cannot find column"),);
assert!(actual.err.contains("did you mean 'type'"),);
})
}
#[test]
fn errors_if_input_empty() {
Playground::setup("group_by_empty_test", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
group-by date
"#
));
assert!(actual.err.contains("expected table from pipeline"));
});
let actual = nu!("group-by date");
assert!(actual.err.contains("expected table from pipeline"));
}
#[test]
fn optional_cell_path_works() {
let actual = nu!("[{foo: 123}, {foo: 234}, {bar: 345}] | group-by foo? | to nuon");
let expected = r#"{"123": [[foo]; [123]], "234": [[foo]; [234]]}"#;
assert_eq!(actual.out, expected)
}