Skip to content

Review #2398 for code improvements: GROUP BY prefers a SELECT alias over a table column #2709

Backlog

Description

@mathiasrw

This is not a bug report. It is an invitation to look at #2398 again, because it decides between an alias and a table column differently from SQLite and from HAVING. The goal is to understand the change and decide whether anything should be improved. The notes below come from a quick read of the diff and a few runs, not from a full analysis.

The concern:

  • GROUP BY b takes the SELECT alias b even when the table has a column b. compileSelectGroup0 (src/424select.js) replaces a GROUP BY name with a copy of the aliased SELECT expression when the name is not a column in the SELECT list. It does not look at the source tables. SQLite and PostgreSQL group by the table column in that case:

    CREATE TABLE gt (a INT, b STRING); INSERT INTO gt VALUES (1, 'p'), (2, 'p'), (3, 'q');
    SELECT CASE WHEN a > 1 THEN 'big' ELSE 'small' END AS b, COUNT(*) AS n FROM gt GROUP BY b
    -- AlaSQL: small 1, big 2 (grouped by the CASE). SQLite: groups by gt.b, 2 and 1 rows.
  • HAVING follows the SQLite rule since Let HAVING use a SELECT alias to close #887 #2672 (a source column wins over an alias, using query.defcols). One rule for both, in one place, would keep GROUP BY and HAVING consistent. Changing GROUP BY changes results that work consistently today, so it is a decision for the breaking list.

Related: #1146, #2672.

Activity

  1. mathiasrw commented on Oct 20, 2026

    @mathiasrw
    MemberAuthor

    Decided 2026-10-21: 4.x keeps the current GROUP BY behaviour; 5.0 follows SQLite with the v4 rule available through a dialect. Tracked in #2710.

  2. added this to the v5.0.0 milestone on Nov 9, 2026
  3. mathiasrw commented on Nov 26, 2026

    @mathiasrw
    MemberAuthor

    Will be covered by #2710.

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