Skip to content

GROUP BY on a SELECT alias groups by a non-existent field and returns zero rows with HTTP 200 #296

Closed

Description

@fupelaqu

Grouping by a column's SELECT alias produces an Elasticsearch terms aggregation on a field that does not exist. Validation passes, Elasticsearch answers HTTP 200 with zero buckets, and the caller sees an empty result for a query that should return every group.

Reproduction

SELECT country AS pays FROM bi_events GROUP BY pays    -- 0 rows, HTTP 200
SELECT country AS pays FROM bi_events GROUP BY country -- correct

Mechanism

Select.fieldAliases is keyed identifierName -> alias, so an alias written in a GROUP BY never reverse-resolves. The bucket is built from the bare name, emitting terms { field: "pays" }. SingleSearch.validate() passes because the bucket's display name coincidentally equals the field's alias.

Measured bucket triples (name, path, sourceBucket):

statement bucket
SELECT country AS pays FROM t GROUP BY country (pays, country, country) — correct
SELECT country AS pays FROM t GROUP BY pays (pays, pays, pays) — wrong
SELECT country AS pays, COUNT(*) AS cnt FROM t GROUP BY pays (pays, pays, pays) — the aggregate-bearing twin, wrong too
SELECT UPPER(country) AS c FROM t GROUP BY c (c, c, c) — wrong for an expression alias

Impact

Three statements in the captured BI corpus (Tableau and Superset) use this spelling. It is a silent wrong answer in the #205 / #207 / #209 / #224 / #253 family — the worst variety, since an empty success looks like "no matching data".

Resolution

A bare-name bucket resolves against the SELECT list's aliases at the AST level before the terms aggregation is built, so GROUP BY pays produces byte-identically the bucket GROUP BY country produces. A projected column always wins over an alias spelled the same. Expression aliases resolve to the aliased expression.

Fixing the resolution also required repairing two things it desynchronised: the terms order for ORDER BY <alias>, and the terms include/exclude for HAVING <alias> — both of which had been silently dropped.

Activity

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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions