diff --git a/crates/nu-command/src/filters/group_by.rs b/crates/nu-command/src/filters/group_by.rs index 0f666d7a19..aab91ecb47 100644 --- a/crates/nu-command/src/filters/group_by.rs +++ b/crates/nu-command/src/filters/group_by.rs @@ -231,7 +231,12 @@ pub fn group_by( let values: Vec = input.into_iter().collect(); if values.is_empty() { - return Ok(Value::record(Record::new(), head).into_pipeline_data()); + let val = if to_table { + Value::list(Vec::new(), head) + } else { + Value::record(Record::new(), head) + }; + return Ok(val.into_pipeline_data()); } let grouped = match &groupers[..] { diff --git a/crates/nu-command/tests/commands/group_by.rs b/crates/nu-command/tests/commands/group_by.rs index a5b6328128..7eaf021541 100644 --- a/crates/nu-command/tests/commands/group_by.rs +++ b/crates/nu-command/tests/commands/group_by.rs @@ -68,9 +68,18 @@ fn errors_if_column_not_found() { #[test] fn group_by_on_empty_list_returns_empty_record() { - let actual = nu!("[[a b]; [1 2]] | where false | group-by a"); + let actual = nu!("[[a b]; [1 2]] | where false | group-by a | to nuon --raw"); + let expected = r#"{}"#; assert!(actual.err.is_empty()); - assert!(actual.out.contains("empty record")); + assert_eq!(actual.out, expected); +} + +#[test] +fn group_by_to_table_on_empty_list_returns_empty_list() { + let actual = nu!("[[a b]; [1 2]] | where false | group-by --to-table a | to nuon --raw"); + let expected = r#"[]"#; + assert!(actual.err.is_empty()); + assert_eq!(actual.out, expected); } #[test]