group by to table empty (#16219)

- fixes #16217

# Description

> `group-by --to-table` does not always return a table (a list)
> ```nushell
> [] | group-by --to-table something
> #=> ╭──────────────╮
> #=> │ empty record │
> #=> ╰──────────────╯
> ```

Fixed:
```nushell
[] | group-by --to-table something
#=> ╭────────────╮
#=> │ empty list │
#=> ╰────────────╯
```

# Tests + Formatting
+1 test

---------

Co-authored-by: Bahex <17417311+Bahex@users.noreply.github.com>
This commit is contained in:
Bahex
2025-07-22 15:07:55 +03:00
committed by GitHub
parent 324aaef0af
commit b0687606f7
2 changed files with 17 additions and 3 deletions

View File

@ -231,7 +231,12 @@ 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 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[..] { let grouped = match &groupers[..] {

View File

@ -68,9 +68,18 @@ fn errors_if_column_not_found() {
#[test] #[test]
fn group_by_on_empty_list_returns_empty_record() { 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.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] #[test]