Skip to content

GROUP BY / SELECT alias colliding with subquery column name causes false Code: 215 (NOT_AN_AGGREGATE) #114200

Open

Description

@rusackas

Company or project name

Apache Superset (apache/superset) — hit this generating queries against virtual (SQL-defined) datasets. See apache/superset#40289 and the fix workaround at apache/superset#42793.

Describe the unexpected behaviour

When a query wraps a subquery, and the outer SELECT list aliases an expression to the same name as a column inside that subquery, referencing the subquery's column unqualified (no table/alias prefix) in both the SELECT list and GROUP BY causes Code: 215. NOT_AN_AGGREGATE, even though the SELECT and GROUP BY expressions are lexically identical.

Qualifying the inner column reference with the subquery's alias (e.g. t.create_time instead of create_time) makes the exact same query succeed. The only difference between the failing and passing query is that one qualifier.

Which ClickHouse versions are affected?

Confirmed on 26.7.3.19 (latest clickhouse/clickhouse-server:latest Docker image as of this report). Reported independently by three Superset users against 25.3.1.2703 as a regression from 4.1.1-era behavior, so this has been present across the 25.3–26.7 range at least. enable_analyzer=1 (the current default).

How to reproduce

CREATE TABLE raw (create_time DateTime, value Int32) ENGINE = Memory;
INSERT INTO raw VALUES ('2026-01-15 10:00:00', 5), ('2026-02-20 11:00:00', 7);

-- Fails with Code: 215
SELECT toStartOfMonth(create_time) AS create_time, COUNT(*) AS count
FROM (SELECT create_time FROM raw) AS t
GROUP BY toStartOfMonth(create_time);

-- Succeeds — only difference is the `t.` qualifier on the inner reference
SELECT toStartOfMonth(t.create_time) AS create_time, COUNT(*) AS count
FROM (SELECT create_time FROM raw) AS t
GROUP BY toStartOfMonth(t.create_time);

Default settings (prefer_column_name_to_alias=0, enable_analyzer=1) in both cases.

Expected behavior

Both queries should succeed and return identical results. The SELECT-list expression and the GROUP BY expression are lexically identical in the failing query — the analyzer should resolve the unqualified create_time inside both to the same entity (whichever it prefers, alias or subquery column), consistently. Instead it appears to resolve them inconsistently between the two clause positions, so the aggregate checker can no longer correlate them.

Error message and/or stacktrace

Code: 215. DB::Exception: Received from localhost:9000. DB::Exception: Column 't.create_time' is not under aggregate function and not in GROUP BY keys. In query SELECT toStartOfMonth(create_time) AS create_time, COUNT(*) AS count FROM (SELECT create_time FROM raw) AS t GROUP BY toStartOfMonth(create_time). (NOT_AN_AGGREGATE)

Note the error names t.create_time — i.e. it did resolve the unqualified reference back to the subquery column internally, in at least one of the two clause positions, but apparently not consistently in both.

Related issues and pull requests

Related: apache/superset#40289
Related: apache/superset#42793
Related: #47552

Additional context

One Superset PR author additionally reported (not independently verified by me) that in a related shape of this same alias/column collision, instead of erroring outright, a WHERE clause filtering on the alias name silently gets the aliased/transformed expression substituted in rather than the raw subquery column, narrowing results incorrectly with no error at all. If real, that would be a more serious silent-wrong-results variant of the same underlying name-resolution ambiguity described above. Flagging it here in case it's the same root cause, but I have not reproduced that variant myself, only the Code: 215 case above.

Activity

  1. PedroTadim commented on Aug 11, 2026

    @PedroTadim
    Member

    The same error appears against a plain table:

    SELECT toStartOfMonth(create_time) AS create_time, COUNT(*) FROM raw
    GROUP BY toStartOfMonth(create_time);
    -- Code: 215. Column 'default.raw.create_time' is not under aggregate function and not in GROUP BY keys.

    and with the old analyzer. Setting prefer_column_name_to_alias = 1 should fix your limitation.

  2. added
    comp-query-analyzer
    on Sep 4, 2026
  3. added
    external
    on Sep 11, 2026
  4. added
    bug
    question
    and removed
    question
    bug
    on Sep 15, 2026
  5. KochetovNicolai commented on Nov 20, 2026

    @KochetovNicolai
    Member

    Potentially won't fix.The query is invalid.

  6. added
    not planned
    close in a month if not active
    on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    close in a month if not activecomp-query-analyzerexternalnot plannedquestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions