Skip to content

SELECT (a+1) ... GROUP BY a+1 drops the column #2721

Closed

Description

@mathiasrw

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.

Related: #2675.

Activity

  1. added this to the v4.x Focus milestone on Oct 20, 2026
  2. mathiasrw commented on Oct 21, 2026

    @mathiasrw
    MemberAuthor

    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 missing
    alasql('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.

  3. added a commit that references this issue on Nov 22, 2026
    65f6f9f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions