Files
greptimedb/src
discord9 63dfaf8915 feat(promql): run subqueries through the native prefix (#10085)
* feat(promql): run subqueries through the native prefix

A subquery evaluates its inner expression on its own grid, so the native route now plans it
instead of sending every statement containing one to the existing planner.

- `collect_selectors` recurses into a subquery's expression, so its selectors are prepared
  like any other and each gets its scan bounds from `raw_scan_bounds`.
- `calls_rate_or_increase` recurses too, so a delta-temporality table still keeps a rate or
  an increase inside a subquery on the existing planner.
- A window whose input is already evaluated - a subquery - is admitted above the split
  aggregate when its declared grid equals its direct input's grid, and the suffix is rebuilt
  with it. A raw-sample window still cannot sit above the cut.
- A `FusedSeries` whose root is such a window publishes on that window's output grid, which
  is not necessarily the statement's; the binding follows it at every construction site, and
  the split's partial state keeps the cut aggregate's own grid. An evaluated-input window
  does not need an owner router for its sources, and a segment whose root is one is cut on
  its own grid.

Verified with `-p promql`, `-p query`, `tests-integration --lib` (native_promql) and clippy.

Signed-off-by: discord9 <discord9@163.com>

* fix(promql): keep a subquery's aggregate operand and gate an evaluated cut

Two shapes reached the native route and failed while planning instead of keeping the plan they had.

A hybrid-binary operand folds a group one subquery down
(`avg_over_time((sum by (job, host) (pm))[5m:1m]) + on(job, host) pn`): `single_selector_operand`
counts the subquery's selector, so the operand looked admissible, while
`contains_aggregate_expression` stopped at `PromExpr::Subquery` and never saw the aggregate. The
admission then built a `FusedSeries` the physical planner could not convert
(`No installed planner was able to convert the custom node to an execution plan ...`). The walk
descends into a subquery again, and both statements keep the existing plan with the same rows.

A window over an evaluated input was cut as a segment regardless of locality. For any such window
whose input is not a bare `Input` - a raw window, a `label_replace` - `reads_across_series` is true
and `pointwise_operand` false, so the `FusedSeries` arm of the dist analyzer answers
`NonCommutative`: the segment stays above the `MergeScan` and nothing converts it
(`sum by (job) (max_over_time(rate(pm[2m])[5m:1m]))` and two neighbours failed that way at instant
and range evaluation). `region_local_segment` mirrors that arm's decision from the tree, and
`cut_as_segment` declines an evaluated-input window it refuses, cutting the per-series prefix inside
the window instead; where there is none, the statement keeps the plan it had. The analyzer is not
extended: every range function but `last_over_time`/`first_over_time` drops the metric name, so such
a window is genuinely cross-series and a region-local segment would publish two series under one
label set.

`build_evaluated_segment` stays, and so does its caller. The canonical pass inserts a merge under a
label rewrite, and that boundary is only ever built from the frames the loop has published, so the
cut needs the fallback; deleting it took every rewritten fold off the route, which is what
`sum(label_replace(...))` and the histogram-over-rewrite boundary caught. The frames-bound segment
itself is not the defect - the gate is: the evaluated-input window is declined before any cut is
taken, so the fallback only sees cuts the analyzer places.

Tests that asserted the older rejection are decided on evidence: `sum_over_time(rate(m[5m])[1h:])`
is admitted now and its rows match the existing plan, so the plan-level test asserts the route and the
integration case proves the rows; the aggregate-subquery case in
`native_aggregate_complete_over_four_nonempty_mito_partitions` is answered by the split (one `Partial`
producer, one frontend merge) and returns the same 15.0 on both routes, so it moved to the split cases
rather than being dropped. Two fold-over-subquery shapes in `native_promql_subqueries_match_legacy`
(`sum by (job) (max_over_time(rate(...)[5m:1m]))` and the fold above it) are answered natively for the
same reason - the fallback builds their cut - and their rows equal the existing plan's, so they assert
the route instead of the fallback; the rewrite shape still declines, because its window reads across
series and there is no per-series prefix inside it to take instead.

Signed-off-by: discord9 <discord9@163.com>

* test(promql): assert anchored subqueries do not re-fold across steps

Signed-off-by: discord9 <discord9@163.com>

* test(promql): drop the unused fallback-case helper

Signed-off-by: discord9 <discord9@163.com>

* test(promql): update subquery expectations for the native route

Signed-off-by: discord9 <discord9@163.com>

* fix(promql): publish subquery segments on their declared grid

Signed-off-by: discord9 <discord9@163.com>

* refactor(promql): drop unreachable segment arms and pin subquery routes

Signed-off-by: discord9 <discord9@163.com>

---------

Signed-off-by: discord9 <discord9@163.com>
2026-11-09 21:46:03 +08:00
..