You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SELECT (a+1) ... GROUP BY a+1 drops the column #2721
A SELECT expression that matches a GROUP BY expression is dropped from the result when one of them is written with parentheses. There is no error.
Measured on develop (94ccf84), data a: 1, 1, 2, next to SQLite 3.43:
Query
AlaSQL
SQLite
SELECT a+1 AS x, COUNT(*) AS n FROM ? GROUP BY a + 1
{x: 2, n: 2}, {x: 3, n: 1}
same
SELECT (a+1) AS x, COUNT(*) AS n FROM ? GROUP BY a+1
{n: 2}, {n: 1}: x is missing
x = 2, 3
Cause
A grouped expression is matched to its GROUP BY key by its text: query.ingroup holds toString() of each GROUP BY item (src/423groupby.js) and compileSelectGroup2 (src/424select.js) looks the SELECT item's text up in it. (a+1) is a UniOp whose text is (a + 1), which is not a + 1.
Direction
Match expressions by structure, ignoring parentheses, not by text (#2675). The GROUP BY design in #2722 resolves every SELECT, HAVING and ORDER BY expression to its group key once. A dropped column is a wrong result, so this can go into 4.x.
The same happens when one side names the table and the other does not, seen in the PostgreSQL regression tests (select_implicit.sql: SELECT a%2, count(b) FROM test_missing_target GROUP BY test_missing_target.a%2). Reproduced on develop (38bf3c3):
alasql('CREATE TABLE tm (a INT, b INT)');alasql('INSERT INTO tm VALUES (1, 1), (2, 2), (3, 3)');alasql('SELECT a % 2 AS k, COUNT(b) AS n FROM tm GROUP BY a % 2');// [{k: 1, n: 2}, {k: 0, n: 1}]alasql('SELECT a % 2 AS k, COUNT(b) AS n FROM tm GROUP BY tm.a % 2');// [{n: 2}, {n: 1}] k is missingalasql('SELECT tm.a % 2 AS k, COUNT(b) AS n FROM tm GROUP BY a % 2');// [{n: 2}, {n: 1}] k is missing
GROUP BY tm.a with SELECT a works, so only expressions are affected. Matching by structure should treat tm.a and a as the same column when tm is the only source that has it.
Activity
mathiasrw commentedon Oct 21, 2026
The same happens when one side names the table and the other does not, seen in the PostgreSQL regression tests (
select_implicit.sql:SELECT a%2, count(b) FROM test_missing_target GROUP BY test_missing_target.a%2). Reproduced ondevelop(38bf3c3):GROUP BY tm.awithSELECT aworks, so only expressions are affected. Matching by structure should treattm.aandaas the same column whentmis the only source that has it.Give expression nodes one base class with walk() and equals() to close …