Skip to content

Fix CUBE and GROUPING SETS merging groups from different sets to close #644 - #2577

Open
joshuablac wants to merge 1 commit into
AlaSQL:developfrom
joshuablac:fix/cube-644
Open

joshuablac wants to merge 1 commit into
AlaSQL:developfrom
joshuablac:fix/cube-644

Conversation

@joshuablac

Copy link
Copy Markdown

Closes #644

Problem

With CUBE or GROUPING SETS, a group from one grouping set can be merged with a group from another set when both have the same values. That double-counts one group and drops the other:

alasql('SELECT a, b, SUM(v) AS s FROM ? GROUP BY CUBE(a, b)', [[{a: 2, b: 2, v: 15}, {a: 2, b: 4, v: 20}]]);
Group develop With this PR
(null, null) 35 35
(2, null) 50 35
(null, 2) missing 15
(2, 2) 15 15
(null, 4) 20 20
(2, 4) 20 20

GROUP BY GROUPING SETS(a, b) on the same data gives [(2,null,50), (null,4,20)] on develop. The correct result is [(2,null,35), (null,2,15), (null,4,20)].

Cause

compileGroup keys each group in xgroups by its grouping column values only (a + "`" + b). The (a) group for a = 2 and the (b) group for b = 2 therefore both get the key "2", and the second set's rows are added to the first set's group.

Fix

When the query has more than one grouping set, the key is prefixed with the grouping set's index. A query with a single grouping set (plain GROUP BY) keeps exactly the same keys and code as before. xgroups is only used inside the compiled group function.

Tests

  • Added test/test644.js: the issue's CUBE, GROUPING SETS(a, b), a CUBE with COUNT(*) over several rows, and a check that ROLLUP and plain GROUP BY are unchanged.
  • Updated test/test015.js. It asserted the merged totals for GROUPING SETS(a,b), c. For example, it expected (a=0, c=0) to be 1000, which is the (a=0,c=0) group (400) plus the (b=0,c=0) group (600). It now expects the eight correct groups and checks all of them, not just the first four.
  • The new tests and the updated test015 fail on develop and pass with this change.
  • Full suite (bun test, same command as yarn test-only): 2253 pass, 213 skip. The only failures are the 2 CLI tests in test2149.js, which fail the same way on unmodified develop in my local setup (a space in my checkout path, and the version-injection step of build.sh not running).

This fix was written with AI assistance (Claude Code) and verified locally as described above.

🤖 Generated with Claude Code

…AlaSQL#644

Groups were keyed only by the values of their grouping columns, so with
several grouping sets a group from one set could collide with a group from
another set that had the same values, e.g. (a=2) from CUBE(a, b) and (b=2).
The rows were then summed into one group and the other group was missing.

The group key now includes the grouping set index when the query has more
than one grouping set. Plain GROUP BY keys are unchanged.

test015 asserted the merged totals; it now expects the correct eight groups.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 12, 2026 16:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug with Group by with Cube operation

2 participants