Skip to content

GROUP BY: group keys as slots matched by structure, one aggregate interface and one partitioning step #2722

Open
0 of 5 issues completed

Description

@mathiasrw

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

  1. Group keys and values are stored and found by name or text. The group object g keeps each grouped column under its bare name (nick or columnid) and each grouped expression and aggregate under its toString() 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 match GROUP BY a+1 and the column is dropped (SELECT (a+1) ... GROUP BY a+1 drops the column #2721).
  2. SELECT, HAVING and ORDER BY items are matched to group keys in four places. compileSelectGroup0 assigns nicks and swaps in aliased expressions (Fix GROUP BY with aliased expressions (CASE, functions) #2398), compileSelectGroup1 and compileSelectGroup2 (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), and compileHaving resolves 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).
  3. Each aggregate is generated as code with its own rules. SUM, MIN, MAX, AVG, COUNT each have a type list (MAX of 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_CONCAT is 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).
  4. 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.
  5. 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 BY case (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

  1. 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 BY item becomes key slot i, 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 becomes key[i], an aggregate becomes agg[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).
  2. Group state as arrays. {keys: [...], aggs: [...], row} instead of names on an object: no collisions, no removeKeys, no bare-name fallback.
  3. One aggregate interface. Every aggregate (built-in and alasql.aggr) is {init, step(args…), result}. DISTINCT and ORDER BY inside an aggregate are generic wrappers (the sort specification of ORDER BY: one sort specification, keys beside the rows and one comparator #2720); MIN / MAX compare 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).
  4. One partitioning step. Rows are grouped by a key tuple using the comparison module's key function. GROUP BY uses one key set, GROUPING SETS / CUBE / ROLLUP several, DISTINCT all output columns, and window functions their PARTITION BY (Compute window functions per partition over the source rows, sorted by the window's ORDER BY #2702). Aggregates without GROUP BY are 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

  1. 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.
  2. The aggregate interface, keeping each aggregate's current results. 4.x.
  3. One partitioning step for GROUP BY, GROUPING SETS and DISTINCT. 4.x.
  4. 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 SUM of no rows: 5.0 as listed in breaking.md, with a dialect keeping the v4 rules.
  5. 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.

Sub-issues

Sub-issues

0 of 5 Issues completed
Loading 5 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