Skip to content

sql: outer SELECT without an aggregate over a subquery is rejected #820

Open

Description

@milindsrivastava1997

Problem

A query whose outer SELECT has no aggregate, only selecting and ranking rows from a subquery, is rejected. SQLPatternParser::get_aggregation requires exactly one aggregate function in every SELECT layer, so the parser returns None and the planner fails with SQL parse error: Failed to parse SQL query: ....

SELECT job, instance, v FROM (
  SELECT job, instance, SUM(value) AS v FROM data
  WHERE time BETWEEN DATEADD(s, -1, NOW()) AND NOW()
  GROUP BY job, instance
) ORDER BY v DESC LIMIT 3 BY job

Equivalent PromQL: topk by (job) (3, sum by (job, instance) (data))

This is the shape top-k takes when applied to an already-aggregated result. It also uses LIMIT ... BY (filed separately), but it would still be rejected with a plain LIMIT.

More examples:

SQL PromQL
SELECT job, m FROM (SELECT job, SUM(v) AS m FROM (...) GROUP BY job) ORDER BY m DESC LIMIT 3 BY job topk by (job) (3, sum by (job) (sum by (job, instance) (data)))
SELECT job, instance, v FROM (... ORDER BY v DESC LIMIT 3 BY job) ORDER BY v DESC LIMIT 3 BY job topk by (job) (3, topk by (job) (3, sum by (job, instance) (data)))

Reproduce

cargo test -p query_engine_rust --test sql_compliance -- --nocapture; see one-stage-topk-3 and two-stage-*-then-topk-3 in nested-aggregations.yaml.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

asap-commonbug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions