feat(lower): arbitrary PromQL function nesting in aggregates (#27) - #28
Merged
Merged
Conversation
…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
approved these changes
Jun 21, 2026
This was referenced Jul 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 aggregatetopk(3, sum by (instance) (rate(m[5m])))— was pinned astopk_over_aggregate_arg_is_rejected__GAPsum(rate(a[5m]) + rate(b[5m]))— aggregate over a binary opsum(histogram_quantile(0.9, sum by (le) (rate(h_bucket[5m]))))How
walk_aggregatetries the flat fast path first (lower_inner), preserving heavy-hittertopk(k, count_over_time(...))recognition. When the argument is a composite expression it recurses via the samewalkused 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::SampleValueresolution (column_resolution.rs) now also resolves to the sole numeric non-timestamp column, so an outer ranking over a cross-series aggregate (value column renamedsum) 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
topk_over_aggregate_arg_is_rejected__GAP→topk_over_nested_aggregate_is_generic_sort_limit(passing).outer_aggregate_over_nested_aggregate_nests,aggregate_over_binary_op_nests(conformance),q27_max_over_sum_by_job_over_rate(exact-tree e2e), and twoSampleValueresolution unit tests.cargo clippy --all-targetsclean.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