Skip to content

Match GROUP BY keys by structure and keep each group's state in slots #2647

Open
0 of 4 issues completed
Open

Description

@mathiasrw

What happens today

  • compileGroup (src/select/groupby.js) and decartes (src/select/rollup.js) store a grouped column in the group object g under its bare name, a grouped expression under its toString() text (expressionKey), and each aggregate under toString():index, with _SUM_, _COUNT_ and $$_VALUES_ helpers in the same object.
  • compileSelectGroup0, 1 and 2 (src/select/columns.js), compileHaving (src/select/having.js) and the group branch of Column.lookupJS (src/expression/expression.js, lines 788-794) each find items in g by their own rules. A qualified column falls back to its bare name (GROUP BY a.id, b.id: a.id inside an expression reads b.id #2637).
  • queryfn3 (src/select/queryfn.js, lines 220-241) deletes alias duplicates from each output row.

This also fixes #3148, #3149, #3150 and #3151.

What to change

  1. In compileGroup, after the grouping sets are expanded, number every distinct GROUP BY item as a slot in order of first appearance. Columns share a slot when bind.column (src/engine/bind.js) gives the same source and column, other nodes when equals() (src/expression/node.js) is true. The GROUP BY alias replacement in compileSelectGroup0 runs first, unchanged.
  2. Each group is {keys: [], aggs: [], help: [], row}. keys[i] is slot i (NULL outside the current grouping set), aggs[j] aggregate j (shared as collectAggregate decides), help[j] the AVG and DISTINCT helpers, row the first row. Generated aggregate code, the empty group and the user aggregate calls in queryfn3 (lines 174-210) use g.aggs[j]. The text key in this.xgroups stays.
  3. Add rewriteForGroup(node, query) in src/select/groupby.js, run once per SELECT, HAVING and ORDER BY item at compile time. It replaces a part equal to slot i with g.keys[i], an aggregate with g.aggs[j] (registering new ones), and any other column with a read of g.row. A HAVING alias from resolveHavingAliases becomes the rewritten SELECT expression. ORDER BY items that resolve to an output column stay output columns. Until Resolve ORDER BY items in one step for plain, grouped and set-operation queries #2693 is merged, rewrite only the ORDER BY item that falls through to toJS('g').
  4. Build the grouped output row only from the rewritten SELECT list. SELECT * copies the columns of g.row. Remove the group branch of Column.lookupJS, the SELECT maps of compileSelectGroup2, query.groupKeys, the lookup uses of query.groupColumns, and the delete loop in queryfn3.

What stays the same

Done when

  • A new test file named after this issue covers each GROUP BY item (column, qualified column, the same name from two tables, alias, expression, parenthesised expression) used in SELECT, HAVING and ORDER BY, with no rows, one group and many groups, ROLLUP, CUBE, GROUPING SETS, SELECT * with GROUP BY, and the fixed issues.
  • bun test/performance/bench.js --group=group-by shows no slowdown beyond the noise described in test/performance/README.md.
  • The full test suite and the CI checks (dialect test, parity check, run-state check, binding check) pass, and the pull request lists every parity difference.

Fixes

Fixes #2637, #2817, #3148, #3149, #3150, #3151.

Depends on

Nothing, can start now.

Sub-issues

Sub-issues

0 of 4 Issues completed
Loading 4 sub-issues

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions