From 94ab98123588155b67734f406ad2636fc5190627 Mon Sep 17 00:00:00 2001 From: Ray Henry Date: Wed, 9 Feb 2022 09:47:47 -0500 Subject: [PATCH] Fix "Index out of bounds" when input to the group-by filter is empty. #4369 (#4382) * Fix "index out of bounds" when input to group-by is empty #4369 * Fix formatting #4369 * Adds test for empty input Co-authored-by: Ray Henry --- crates/nu-command/src/filters/group_by.rs | 4 ++-- crates/nu-command/tests/commands/group_by.rs | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/crates/nu-command/src/filters/group_by.rs b/crates/nu-command/src/filters/group_by.rs index 5b36539f70..906590458c 100644 --- a/crates/nu-command/src/filters/group_by.rs +++ b/crates/nu-command/src/filters/group_by.rs @@ -95,8 +95,6 @@ pub fn group_by( let mut keys: Vec> = vec![]; let mut group_strategy = Grouper::ByColumn(None); - let first = values[0].clone(); - if values.is_empty() { return Err(ShellError::SpannedLabeledError( "expected table from pipeline".into(), @@ -105,6 +103,8 @@ pub fn group_by( )); } + let first = values[0].clone(); + let value_list = Value::List { vals: values.clone(), span: name, diff --git a/crates/nu-command/tests/commands/group_by.rs b/crates/nu-command/tests/commands/group_by.rs index 05f8fe9902..f64dba0391 100644 --- a/crates/nu-command/tests/commands/group_by.rs +++ b/crates/nu-command/tests/commands/group_by.rs @@ -97,3 +97,17 @@ fn errors_if_block_given_evaluates_more_than_one_row() { assert!(actual.err.contains("Unknown column")); }) } + +#[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")); + }); +}