Allow empty list inputs in group-by and return empty record (#10730)

# Description

Changed `group-by` behavior to accept empty list as input and return an
empty record instead of throwing an error. I also replaced
`errors_if_input_empty()` test to reflect the new expected behavior.

See #10713 

# User-Facing Changes
`[] | group-by` or `[] | group-by a` now returns empty record


# Tests + Formatting
1 test for emptied table i.e. list

---------

Signed-off-by: Oscar <71343264+0scvr@users.noreply.github.com>
This commit is contained in:
Oscar 2023-10-19 12:20:52 +02:00 committed by GitHub
parent 1662e61ecb
commit 0a8f27f6f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 8 deletions

View file

@ -128,12 +128,9 @@ pub fn group_by(
let values: Vec<Value> = input.into_iter().collect(); let values: Vec<Value> = input.into_iter().collect();
if values.is_empty() { if values.is_empty() {
return Err(ShellError::GenericError( return Ok(PipelineData::Value(
"expected table from pipeline".into(), Value::record(Record::new(), Span::unknown()),
"requires a table input".into(),
Some(span),
None, None,
Vec::new(),
)); ));
} }

View file

@ -97,9 +97,10 @@ fn errors_if_column_not_found() {
} }
#[test] #[test]
fn errors_if_input_empty() { fn group_by_on_empty_list_returns_empty_record() {
let actual = nu!("group-by date"); let actual = nu!("[[a b]; [1 2]] | where false | group-by a");
assert!(actual.err.contains("expected table from pipeline")); assert!(actual.err.is_empty());
assert!(actual.out.contains("empty record"));
} }
#[test] #[test]