Repository navigation
Description
Many merged pull requests fixed one GROUP BY case each. Read together, they trace back to five design choices. This issue proposes removing those causes instead of patching each case. It goes with #2717 (subqueries), #2718 (set operations) and #2720 (ORDER BY).
Why GROUP BY breaks
- Group keys and values are stored and found by name or text. The group object
gkeeps each grouped column under its bare name (nickorcolumnid) and each grouped expression and aggregate under itstoString()text. Two grouped columns with the same name from different tables collide (Fix incorrect result when grouping by two columns with the same name #2307, Match GROUP BY keys by structure and keep each group's state in slots #2647); a table-qualified column inside an expression falls back to its bare name (Fix GROUP BY with table-prefixed columns in arithmetic expressions #2297, Review #2297 for code improvements: grouped columns looked up by bare name #2640);(a+1)does not matchGROUP BY a+1and the column is dropped (SELECT (a+1) ... GROUP BY a+1 drops the column #2721). - SELECT, HAVING and ORDER BY items are matched to group keys in four places.
compileSelectGroup0assigns nicks and swaps in aliased expressions (Fix GROUP BY with aliased expressions (CASE, functions) #2398),compileSelectGroup1andcompileSelectGroup2(src/424select.js) look items up again with their own maps (Fix incorrect result when grouping by two columns with the same name #2307, Fix ORDER BY on grouped source columns when SELECT uses aliases #2525), andcompileHavingresolves aliases separately (Let HAVING use a SELECT alias to close #887 #2672). Each place applies its own rule for alias against column (Review #2398 for code improvements: GROUP BY prefers a SELECT alias over a table column #2709, GROUP BY name prefers a source column over a SELECT alias, as SQLite #2710). - Each aggregate is generated as code with its own rules.
SUM,MIN,MAX,AVG,COUNTeach have a type list (MAXof text drops the column, Fix for MAX of dates #2156, MIN and MAX compare through the comparison module, and SUM over no rows is NULL, with a dialect keeping the old rules #2696);GROUP_CONCATis special-cased inside the generic user-aggregate path (Add GROUP_CONCAT support for ORDER BY and SEPARATOR clauses #2418, Give every aggregate the same init, step and result interface #2660); user aggregates take their accumulator at a position that depends on the number of arguments (Enable multi-column user-defined aggregate functions #2396); window aggregates are a second implementation (Fix window aggregate functions not implementing semantics #2411, Review #2411 for code improvements: window aggregates are a second implementation on the output rows #2690). - A column that is neither grouped nor aggregated has no defined value. It reads the first row through a copy made per group (Fix correlated subqueries in grouped selects #2514, Review #2514 for code improvements: row scope copied for every group #2668) or from whatever the group object holds.
- Partitioning is implemented several times.
GROUP BY,CUBE/ROLLUP/GROUPING SETS(Fix CUBE and GROUPING SETS merging groups from different sets to close #644 #2577),DISTINCT,ROW_NUMBER() OVER (PARTITION BY …)(Implement SQL-99 ROW_NUMBER() OVER (PARTITION BY) syntax for per-group row numbering #2208, Compute window functions per partition over the source rows, sorted by the window's ORDER BY #2702) and window aggregates each group rows their own way. The no-GROUP BYcase (aggregates over all rows, including no rows, Add regression test for GROUP BY with no matching rows #2300) is a special case (self.group = ['']).
Proposal
- Group keys as slots, matched by structure. At binding time (the binding step of Subqueries: give each SELECT its own subqueries, one compiled form and a run context #2717) each
GROUP BYitem becomes key sloti, compared by structure, not text (Write the text of every expression from all of its parts, so unaliased column names are complete #2675). Every SELECT, HAVING and ORDER BY expression is rewritten once: a part equal to a group key becomeskey[i], an aggregate becomesagg[j](identical aggregates share a slot), and any other column reference becomes an explicit "representative row" read (today's v4 behaviour) or, in a strict dialect, an error. One rewrite replaces the four lookup places, and the alias-or-column rule lives in that one step (GROUP BY name prefers a source column over a SELECT alias, as SQLite #2710). - Group state as arrays.
{keys: [...], aggs: [...], row}instead of names on an object: no collisions, noremoveKeys, no bare-name fallback. - One aggregate interface. Every aggregate (built-in and
alasql.aggr) is{init, step(args…), result}.DISTINCTandORDER BYinside an aggregate are generic wrappers (the sort specification of ORDER BY: one sort specification, keys beside the rows and one comparator #2720);MIN/MAXcompare with the comparison module (One comparison module with levels (dates compared by value, MAX/MIN on strings, one NULL order) #2595). Window aggregates use the same objects over each partition (Review #2411 for code improvements: window aggregates are a second implementation on the output rows #2690). - One partitioning step. Rows are grouped by a key tuple using the comparison module's key function.
GROUP BYuses one key set,GROUPING SETS/CUBE/ROLLUPseveral,DISTINCTall output columns, and window functions theirPARTITION BY(Compute window functions per partition over the source rows, sorted by the window's ORDER BY #2702). Aggregates withoutGROUP BYare one group, always present, also for no rows.
| Pull request / issue | Removed by |
|---|---|
| #2297, #2307, #2721 | 1 and 2 (slots matched by structure) |
| #2398, #2525, #2709 | 1 (one rewrite step, one alias rule) |
| #2156, #2396, #2418, #2660, #2696 | 3 (one aggregate interface) |
| #2514, #2668 | 1 (explicit representative row) |
| #2208, #2300, #2411, #2577, #2690, #2702 | 4 (one partitioning step) |
Test matrix
GROUP BY item (column, qualified column, same name from two tables, alias, expression, parenthesised expression, position) × use in SELECT / HAVING / ORDER BY × aggregate (each built-in, DISTINCT, user aggregate with one and two arguments, GROUP_CONCAT … ORDER BY) × values (numbers, text, dates, NULL, undefined) × input (no rows, one group, many groups) × GROUPING SETS / CUBE / ROLLUP, each compared with SQLite (#2593).
Order
- Slots and the one rewrite step, keeping today's v4 resolution rules and the representative row. Fixes SELECT (a+1) ... GROUP BY a+1 drops the column #2721 and the collisions. 4.x.
- The aggregate interface, keeping each aggregate's current results. 4.x.
- One partitioning step for
GROUP BY,GROUPING SETSandDISTINCT. 4.x. - SQLite's alias rule (GROUP BY name prefers a source column over a SELECT alias, as SQLite #2710), aggregates over text and dates (One comparison module with levels (dates compared by value, MAX/MIN on strings, one NULL order) #2595), and
SUMof no rows: 5.0 as listed inbreaking.md, with a dialect keeping the v4 rules. - Window partitions and ordering: 5.0 (ROW_NUMBER() OVER (ORDER BY …) ignores the window's ORDER BY; PARTITION BY works only on already-sorted output #2591, Compute window functions per partition over the source rows, sorted by the window's ORDER BY #2702).
Related: #2156, #2208, #2297, #2300, #2307, #2396, #2398, #2411, #2418, #2514, #2525, #2577, #2591, #2593, #2595, #2640, #2647, #2660, #2668, #2672, #2675, #2690, #2696, #2702, #2709, #2710, #2717, #2718, #2720, #2721.
Activity