Skip to content

sql: two-layer nested aggregation (subquery in FROM) is rejected #819

Closed

Description

@milindsrivastava1997

Problem

A two-layer SQL query (an aggregation over a subquery that aggregates the table) is rejected. The pattern parser accepts it, but SQLPatternMatcher::query_info_to_pattern returns QueryError::NestedQueryUnsupported for any query with more than one layer, and the planner rejects it with nested SQL queries are not supported (n=2).

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
) GROUP BY job

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

The PromQL path executes these through the native aggregation DAG; the SQL path has no equivalent.

More examples:

SQL outer layer over the subquery above PromQL
SELECT job, QUANTILE(0.9, v) FROM (...) GROUP BY job quantile by (job) (0.9, sum by (job, instance) (data))
SELECT job, AVG(v) FROM (...) GROUP BY job avg by (job) (sum by (job, instance) (data))
SELECT L1, AVG(result) FROM (SELECT L1, L2, QUANTILE(0.95, value) AS result ... GROUP BY L1, L2) GROUP BY L1 avg by (L1) (quantile_over_time(0.95, cpu_usage[10s]))

Reproduce

cargo test -p query_engine_rust --test sql_compliance -- --nocapture; see one-stage-* in nested-aggregations.yaml.

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions