You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
sql: ORDER BY / LIMIT inside a FROM subquery is silently dropped #821
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
WHEREtime BETWEEN DATEADD(s, -1, NOW()) AND NOW()
GROUP BY job, instance
ORDER BY v DESCLIMIT3 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.
Activity