From 0a8f27f6f2869aed9cb8391a185ddea5035f39bb Mon Sep 17 00:00:00 2001 From: Oscar <71343264+0scvr@users.noreply.github.com> Date: Thu, 19 Oct 2023 12:20:52 +0200 Subject: [PATCH] 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> --- crates/nu-command/src/filters/group_by.rs | 7 ++----- crates/nu-command/tests/commands/group_by.rs | 7 ++++--- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/crates/nu-command/src/filters/group_by.rs b/crates/nu-command/src/filters/group_by.rs index 3f4c8417f..7fd9774d2 100644 --- a/crates/nu-command/src/filters/group_by.rs +++ b/crates/nu-command/src/filters/group_by.rs @@ -128,12 +128,9 @@ pub fn group_by( let values: Vec = input.into_iter().collect(); if values.is_empty() { - return Err(ShellError::GenericError( - "expected table from pipeline".into(), - "requires a table input".into(), - Some(span), + return Ok(PipelineData::Value( + Value::record(Record::new(), Span::unknown()), None, - Vec::new(), )); } diff --git a/crates/nu-command/tests/commands/group_by.rs b/crates/nu-command/tests/commands/group_by.rs index 245bcd3b4..caded3291 100644 --- a/crates/nu-command/tests/commands/group_by.rs +++ b/crates/nu-command/tests/commands/group_by.rs @@ -97,9 +97,10 @@ fn errors_if_column_not_found() { } #[test] -fn errors_if_input_empty() { - let actual = nu!("group-by date"); - assert!(actual.err.contains("expected table from pipeline")); +fn group_by_on_empty_list_returns_empty_record() { + let actual = nu!("[[a b]; [1 2]] | where false | group-by a"); + assert!(actual.err.is_empty()); + assert!(actual.out.contains("empty record")); } #[test]