-
Notifications
You must be signed in to change notification settings - Fork 2.1k
group-by should support multiple grouper arguments #14330
Copy link
Copy link
Closed
Labels
category:enhancementNew feature or requestNew feature or requeststatus:needs-triageAn issue that hasn't had any proper lookAn issue that hasn't had any proper look
Milestone
Description
Related problem
Related:
- A more sql-like group-by #2607
- Deprecate
split-bycommand #14019 - add
agg-byas a command that helps aggregate data with sum, avg, and count #14316
Describe the solution you'd like
Similar to sort-by, group-by should support multiple grouper arguments, as the rest parameter.
group-by ...(grouper)Examples will be demonstrated on this sample data, taken from split-by's examples
let data = [
[name, lang, year];
[andres, rb, "2019"],
[jt, rs, "2019"],
[storm, rs, "2021"]
]Without --to-table, the output would be in the form of nested records, which would also make split-by obsolete.
Output of the split-by command, and the suggested group-by should be the same.
> $data | group-by year | split-by lang
> $data | group-by lang year
╭────┬─────────────────────────────────────────╮
│ │ ╭──────┬──────────────────────────────╮ │
│ rb │ │ │ ╭─#─┬──name──┬─lang─┬─year─╮ │ │
│ │ │ 2019 │ │ 0 │ andres │ rb │ 2019 │ │ │
│ │ │ │ ╰─#─┴──name──┴─lang─┴─year─╯ │ │
│ │ ╰──────┴──────────────────────────────╯ │
│ │ ╭──────┬─────────────────────────────╮ │
│ rs │ │ │ ╭─#─┬─name─┬─lang─┬─year─╮ │ │
│ │ │ 2019 │ │ 0 │ jt │ rs │ 2019 │ │ │
│ │ │ │ ╰─#─┴─name─┴─lang─┴─year─╯ │ │
│ │ │ │ ╭─#─┬─name──┬─lang─┬─year─╮ │ │
│ │ │ 2021 │ │ 0 │ storm │ rs │ 2021 │ │ │
│ │ │ │ ╰─#─┴─name──┴─lang─┴─year─╯ │ │
│ │ ╰──────┴─────────────────────────────╯ │
╰────┴─────────────────────────────────────────╯With --to-table, the output should be a flat table (excluding items column) regardless of how many grouper arguments are used.
> $data | group-by lang year --to-table
╭─#─┬─group_1─┬─group_2─┬────────────items─────────────╮
│ 0 │ rb │ 2019 │ ╭─#─┬──name──┬─lang─┬─year─╮ │
│ │ │ │ │ 0 │ andres │ rb │ 2019 │ │
│ │ │ │ ╰─#─┴──name──┴─lang─┴─year─╯ │
│ 1 │ rs │ 2019 │ ╭─#─┬─name─┬─lang─┬─year─╮ │
│ │ │ │ │ 0 │ jt │ rs │ 2019 │ │
│ │ │ │ ╰─#─┴─name─┴─lang─┴─year─╯ │
│ 2 │ rs │ 2021 │ ╭─#─┬─name──┬─lang─┬─year─╮ │
│ │ │ │ │ 0 │ storm │ rs │ 2021 │ │
│ │ │ │ ╰─#─┴─name──┴─lang─┴─year─╯ │
╰─#─┴──lang───┴──year───┴────────────items─────────────╯Note: The columns will be named after the groupers' string form, and group{i} will be used for groupers that can't be stringified (closures).
Describe alternatives you've considered
No response
Additional context and details
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
category:enhancementNew feature or requestNew feature or requeststatus:needs-triageAn issue that hasn't had any proper lookAn issue that hasn't had any proper look