Skip to content

sql: ORDER BY / LIMIT inside a FROM subquery is silently dropped #821

Open

Description

@milindsrivastava1997

Problem

When SQLPatternParser parses a subquery in FROM (...), it reads only the subquery's SELECT body and drops the subquery's ORDER BY and LIMIT without rejecting the query. The resulting SQLQueryData for the inner layer has order_by: [] and limit: None.

SELECT job, SUM(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
) GROUP BY job

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

This parses to the same structure as the query without the inner ORDER BY ... LIMIT ..., i.e. sum by (job) (sum by (job, instance) (data)). Today it is still rejected later because two-layer queries are unsupported (nested SQL queries are not supported (n=2)), so no wrong result is returned yet. Once two-layer queries are supported, it would return the sum over all instances instead of the top 3. The same happens with a plain inner LIMIT 3 (without BY), which the top-level parser would otherwise accept.

Reproduce

cargo test -p query_engine_rust --test sql_compliance -- --nocapture; bare-topk-by-job-then-sum and two-stage-topk-3-then-* in nested-aggregations.yaml fail with n=2, not with the LIMIT BY parse error that the same clause produces at the top level.

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

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions