Repository navigation
Open
0 / 40 of 4 issues completedOpen
0 / 40 of 4 issues completed
Copy link
Milestone
Description
What happens today
compileGroup(src/select/groupby.js) anddecartes(src/select/rollup.js) store a grouped column in the group objectgunder its bare name, a grouped expression under itstoString()text (expressionKey), and each aggregate undertoString():index, with_SUM_,_COUNT_and$$_VALUES_helpers in the same object.compileSelectGroup0,1and2(src/select/columns.js),compileHaving(src/select/having.js) and the group branch ofColumn.lookupJS(src/expression/expression.js, lines 788-794) each find items ingby 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
- 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 whenbind.column(src/engine/bind.js) gives the same source and column, other nodes whenequals()(src/expression/node.js) is true. The GROUP BY alias replacement incompileSelectGroup0runs first, unchanged. - Each group is
{keys: [], aggs: [], help: [], row}.keys[i]is sloti(NULL outside the current grouping set),aggs[j]aggregatej(shared ascollectAggregatedecides),help[j]the AVG and DISTINCT helpers,rowthe first row. Generated aggregate code, the empty group and the user aggregate calls inqueryfn3(lines 174-210) useg.aggs[j]. The text key inthis.xgroupsstays. - Add
rewriteForGroup(node, query)insrc/select/groupby.js, run once per SELECT, HAVING and ORDER BY item at compile time. It replaces a part equal to slotiwithg.keys[i], an aggregate withg.aggs[j](registering new ones), and any other column with a read ofg.row. A HAVING alias fromresolveHavingAliasesbecomes 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 totoJS('g'). - Build the grouped output row only from the rewritten SELECT list.
SELECT *copies the columns ofg.row. Remove the group branch ofColumn.lookupJS, the SELECT maps ofcompileSelectGroup2,query.groupKeys, the lookup uses ofquery.groupColumns, and the delete loop inqueryfn3.
What stays the same
- Column names, row order, which rows share a group, NULL in the keys of CUBE, ROLLUP and GROUPING SETS, and every aggregate value.
- The GROUP BY and HAVING alias rules,
GROUP BY 1and the no-row results. - Results change for GROUP BY a.id, b.id: a.id inside an expression reads b.id #2637, ORDER BY LOWER(c) in a query grouped by LOWER(c) does not sort #2817, HAVING with a grouped expression finds no rows #3148, ORDER BY an aggregate that is not in the SELECT list does not sort #3149, A column that is neither grouped nor aggregated reads undefined in an expression, HAVING or ORDER BY of a grouped query #3150 and SELECT * in a grouped query shows internal keys such as a + 1 and $$_VALUES_COUNT(DISTINCT b) #3151.
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-byshows no slowdown beyond the noise described intest/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
Collapse Sub-issuesSub-issues
Loading 4 sub-issues
Activity
mathiasrw commentedon Oct 20, 2026
Part of the GROUP BY design in #2722.
[-]Review #2307 for code improvements: grouped columns keyed by bare name[/-][+]Match GROUP BY keys by structure and keep each group's state in slots[/+]