Skip to content

Read a bare column of a later source in a grouped select list (#3110) - #3111

Merged
mathiasrw merged 1 commit into
mainfrom
fix/3110
Nov 22, 2026
Merged

mathiasrw merged 1 commit into
mainfrom
fix/3110

Conversation

@mathiasrw

@mathiasrw mathiasrw commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Closes the select list part of #3110. The HAVING part of the issue is not touched, so the issue should stay open for it (this PR does not use a closing keyword for that reason).

The bug

In a grouped query, a bare column that only a later source of FROM defines was missing from the result.

CREATE TABLE test (a INT, b INT);
CREATE TABLE test1 (a INT, c INT);
INSERT INTO test VALUES (1,10), (2,20), (3,30);
INSERT INTO test1 VALUES (1,7), (2,8), (3,7);

SELECT c, SUM(b) AS s FROM test JOIN test1 ON test.a = test1.a GROUP BY test1.a;
-- before [{s:10}, {s:20}, {s:30}]
-- now    [{c:7, s:10}, {c:8, s:20}, {c:7, s:30}]

The change

compileSelectGroup in src/select/columns.js called col.toJS for a column that stands alone without the default columns, so the lookup wrote a read of the first table of FROM. It now passes query.defcols, and the lookup finds the source that has the name, as it does everywhere else.

A name that two tables define is the exception. The default columns mark it as ambiguous and the lookup would throw. For such a name the call is made as before, without the default columns, so it keeps reading the first table and there is no new error. Making that an error is #2928.

src/select/having.js is not changed. A bare column of a later source in HAVING still finds nothing, and waits for #3095.

Tests

test/test3110.js, in the named database test3110.

  • A, B and C are the three queries of the issue: GROUP BY on another column, an aggregate without GROUP BY, and the other order of FROM.
  • D has a subquery as the later source.
  • E groups by an expression of the column (GROUP BY c+0).
  • F checks that a column of the first table and a GROUP BY key are as before.
  • G checks that a name two tables define still reads the first table in both orders of FROM, with no error.

A, B, D and E fail on main. C, F and G pass on main and here.

Suite: 2745 pass, 213 skip, 0 fail. The run-state check gives an empty list.

The binding check

This bug was case 3 of the binding check of #3108. With the fix today's lookup and bind.column agree there, so the rule for case 3 is removed from knownCase in test/bind/record.js and from the README, which now lists five cases. A return of the old reading would fail the check. Case 2c (a name two tables define, in a grouped select list) is still there and counts 6 references, 2 more than before, from test G.

Parity differences

From the parity job of this PR, against the base (1169193) with the test files of this PR. 78,594 statements aligned in 588 files.

Kind Count
result 4
error 0
params 0
generated code 7
only in the base, only in this PR, diverged 0
API paths (1,720) 0 changed, 0 allowed

The 4 result differences are all in the new test/test3110.js, and each is the fix. The base gives c as undefined.

Statement Base This PR
SELECT c, SUM(b) AS s FROM test JOIN test1 ON test.a = test1.a GROUP BY test1.a c undefined in 3 rows c is 7, 8, 7
SELECT c, COUNT(*) AS n FROM test JOIN test1 ON test.a = test1.a [{c: undefined, n: 3}] [{c: 7, n: 3}]
SELECT c, SUM(b) AS s FROM test JOIN (SELECT a, c FROM test1) t1 ON test.a = t1.a GROUP BY t1.a c undefined in 3 rows c is 7, 8, 7
SELECT c, SUM(b) AS s FROM test JOIN test1 ON test.a = test1.a GROUP BY c+0 c undefined in 2 rows c is 7, 8

The 7 differences in generated code are the same one line each time. The read of the bare column in the grouped select list names the source that has it.

  • The four statements above (4). For a table the code goes from (this.groupSources.get(g) || g)['test']['c'] to ...['test1']['c']. For the subquery it becomes the per-row read ('c' in Object(...['t1']) ? ...['t1']['c'] : undefined).
  • SELECT c, SUM(b) AS s FROM test JOIN test1 ON test.a = test1.a GROUP BY c in test3110.js (1), with the same result.
  • SELECT SUM(b) AS sb,a,c FROM test LEFT JOIN test1 ON test.a = test1.a GROUP BY c,test.a in test009.js (1), with the same result.
  • SELECT c, SUM(b) AS s FROM one t0 JOIN (SELECT a,c FROM two) t1 ON t0.a=t1.a GROUP BY c in test832.js (1), with the same result.

In the last three the column is also a GROUP BY key, so the value was and is copied from the group afterwards. No statement of the existing suite gives another result or error.

Sizes: alasql.fs.js 953,862 to 954,074 bytes (+0.02%), alasql.min.js 543,459 to 543,544 bytes (+0.02%), gzip +18 bytes on the minified file.

Supply chain

No change to package.json, the lockfiles, the workflows, the grammar or the generated parser.

@mathiasrw
mathiasrw merged commit f4a75e7 into main Nov 22, 2026
28 checks passed
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.

1 participant