Skip to content

feat(lower): arbitrary PromQL function nesting in aggregates (#27) - #28

Merged
milindsrivastava1997 merged 1 commit into
mainfrom
feat/promql-nested-query-functions
Jun 21, 2026
Merged

milindsrivastava1997 merged 1 commit into
mainfrom
feat/promql-nested-query-functions

Conversation

@zzylol

@zzylol zzylol commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

What & why

Supports nested query functions in PromQL — an aggregate over a composite argument — closing the core PromQL part of #27.

The L3 intent algebra, the L2→L3 converter, and the Binder are already fully recursive; the only ceiling was the PromQL front end. walk_aggregate (crates/lower/src/promql.rs) lowered an aggregate's argument through a flat two-level template (Inner/Outer): an outer aggregator over exactly one inner selector or range-vector function. That rejected the common cases:

  • max(sum by (job) (rate(m[5m]))) — outer aggregate over a nested aggregate
  • topk(3, sum by (instance) (rate(m[5m]))) — was pinned as topk_over_aggregate_arg_is_rejected__GAP
  • sum(rate(a[5m]) + rate(b[5m])) — aggregate over a binary op
  • sum(histogram_quantile(0.9, sum by (le) (rate(h_bucket[5m]))))

How

  • walk_aggregate tries the flat fast path first (lower_inner), preserving heavy-hitter topk(k, count_over_time(...)) recognition. When the argument is a composite expression it recurses via the same walk used at the top level and wraps the result in the outer op (outer_kind + build_over_subtree). Unsupported inner expressions (e.g. unary negation) still surface their error — nothing is silently mislowered.
  • ColumnRef::SampleValue resolution (column_resolution.rs) now also resolves to the sole numeric non-timestamp column, so an outer ranking over a cross-series aggregate (value column renamed sum) finds its sort key.

No changes to the L3 IR, the converter, or the Binder were needed — only the front end and one resolver fallback.

Tests

  • Flips topk_over_aggregate_arg_is_rejected__GAPtopk_over_nested_aggregate_is_generic_sort_limit (passing).
  • Adds outer_aggregate_over_nested_aggregate_nests, aggregate_over_binary_op_nests (conformance), q27_max_over_sum_by_job_over_rate (exact-tree e2e), and two SampleValue resolution unit tests.
  • Full workspace test suite green; cargo clippy --all-targets clean.

Scope / follow-ups

This PR covers PromQL aggregate nesting. Remaining tasks (range-vector-function-over-subquery, topk(k, <bare selector>) by (...), SQL derived tables/inline views) are enumerated in the updated #27.

Closes part of #27.

🤖 Generated with Claude Code

…27)

The PromQL front end lowered an aggregate argument through a flat two-level
template (`Inner`/`Outer`): an outer aggregator over exactly one inner
selector or range-vector function. That rejected the very common case of an
aggregate over a *composite* expression — a nested aggregate
(`max(sum by (job) (rate(m[5m])))`), a binary op
(`sum(rate(a[5m]) + rate(b[5m]))`), a sub-query, or a function lowered
elsewhere (`sum(histogram_quantile(0.9, …))`).

The L3 intent algebra, the L2→L3 converter, and the Binder are already fully
recursive — the only ceiling was this front-end template. `walk_aggregate`
now tries the flat fast path first (preserving the heavy-hitter
`topk(k, count_over_time(...))` recognition) and, when the argument is a
composite expression, recurses via the same `walk` used at the top level and
wraps the result in the outer aggregation. Genuinely unsupported inner
expressions (e.g. unary negation) still surface their error, so nothing is
silently mislowered.

`ColumnRef::SampleValue` now also resolves to the sole *numeric* non-timestamp
column, so an outer ranking over a cross-series aggregate
(`topk(k, sum by (job) (…))`, whose value column is renamed `sum`) finds its
sort key.

Tests: flips the `topk_over_aggregate_arg_is_rejected__GAP` conformance gap to
a passing test; adds nested-aggregate, aggregate-over-binary-op, and outer-over-
nested-aggregate conformance tests, an exact-tree e2e test, and SampleValue
resolution unit tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@milindsrivastava1997
milindsrivastava1997 merged commit fb11ce6 into main Jun 21, 2026
1 check passed
@milindsrivastava1997
milindsrivastava1997 deleted the feat/promql-nested-query-functions branch June 21, 2026 21:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants