From b35967ca0bf0f0e8089ad11f31acf01a686573ba Mon Sep 17 00:00:00 2001 From: discord9 Date: Mon, 9 Nov 2026 22:37:23 +0800 Subject: [PATCH] feat(promql): answer subquery matrix roots natively (#10095) * feat(promql): answer a subquery matrix root on the native route A matrix-valued instant query whose root is a subquery (`m[5m:1m]`, with `offset` and `@`) now runs natively: the planner recognises the root, derives the subquery's child grid and compiles the inner expression on it, while the statement's own bounds stay where `@ start()` and `@ end()` resolve against them. No window node is added - the tree is the inner expression, and its rows carry the evaluation timestamps, which is what the HTTP encoder reports as `resultType: matrix`. A raw range selector still declines (raw samples have no evaluation grid to bind), and a range query with a matrix root declines so the existing planner's "range vector" rejection is what the caller sees. A pre-epoch statement (`TQL EVAL ('-60', '-60', '1m')`) rebuilds its bounds on the side of the epoch their sign names, and a statement whose grid cannot be built keeps the existing plan. `compile_statement_with_grid` / `compile_selectors_with_grid` take the grid explicitly; the grid is still derived lazily inside the compiler so an unsupported statement reports the reason it has no tree, not a grid complaint. Verified with `-p query`, `-p promql`, `tests-integration --lib` (the matrix-root cases) and the file-backed HTTP route test. Signed-off-by: discord9 * fix(promql): bind the subquery's child grid on every lowering path `compile_statement_with_grid` compiled a subquery root's tree on the child grid and the plain `FusedSeries::try_new` path bound that grid, but the split-aggregate, canonical-boundary and segment paths still received the statement grid. A matrix root whose inner expression folds as an aggregate therefore built a final node publishing on the statement grid over a tree compiled on the child grid, and `plan_split_aggregate` refused it with `FusedSeries output is not on the statement grid`: `sum(pm)[5m:1m]`, `(sum by (job) (rate(pm[2m])))[5m:1m]`, its `@ 600` form and its `* 2` form all failed instead of answering the five child-grid points. The effective grid is now chosen once, right after the statement grid is known - the child grid for a subquery root, the statement grid otherwise - and every lowering path binds that one. The per-site `unwrap_or_else` workarounds and `fused_publish_grid` go away with it: they were compensating for two halves that disagreed, and the binding is now the grid the tree was compiled on. The child-grid derivation itself is back in one function: `subquery_child_grid` in `native_compile.rs` feeds both the compiler (which binds it into the window node) and the planner (which binds the rows to it), using the checked `range_millis`/`millis` conversions and the compiler's zero-step refusal. The planner's inline copy used unchecked `as i64` and its own default step, so an overflowing range wrapped there while the compiler reported it. `native_promql_matrix_root_matches_legacy` covers the four shapes, asserting both the route and the child-grid timestamps for the split case. Signed-off-by: discord9 * test(promql): cover pre-epoch non-matrix routing Signed-off-by: discord9 * fix(promql): publish recursive segments on their declared grid Signed-off-by: discord9 --------- Signed-off-by: discord9 --- src/query/src/promql/native_compile.rs | 172 +++++++--- src/query/src/promql/planner/native.rs | 105 ++++-- tests-integration/src/tests/promql_test.rs | 376 ++++++++++++++++++++- tests-integration/tests/http.rs | 32 ++ 4 files changed, 620 insertions(+), 65 deletions(-) diff --git a/src/query/src/promql/native_compile.rs b/src/query/src/promql/native_compile.rs index 32dc93c7a91..92d40507f7e 100644 --- a/src/query/src/promql/native_compile.rs +++ b/src/query/src/promql/native_compile.rs @@ -389,6 +389,30 @@ pub fn compile_statement(stmt: &EvalStmt) -> Result { Ok(compiled.tree) } +/// Compiles `stmt` with an explicit evaluation grid, instead of the one its bounds derive. +/// +/// A subquery root's inner expression is evaluated on the subquery's child grid, while the +/// statement's own bounds stay where `@ start()` and `@ end()` resolve against them. +pub(crate) fn compile_statement_with_grid( + stmt: &EvalStmt, + grid: Arc, +) -> Result { + let output_field = data_free_value_field(stmt); + let compiled = compile_selectors_with_grid_and_value_field( + stmt, + &|_| { + Err(not_implemented(&format!( + "a selector in `{}`, which is compiled as a statement that reads no data", + stmt.expr + ))) + }, + SeriesOrdering::Contiguous, + output_field, + Some(grid), + )?; + Ok(compiled.tree) +} + /// Compiles `stmt` into a tree and returns a binding record for each selector occurrence. /// /// `bindings` is called once per selector with its requirements. The resulting source indices are @@ -401,11 +425,37 @@ pub fn compile_selectors( compile_selectors_with_value_field(stmt, bindings, ordering, Arc::from(VECTOR_FIELD)) } +/// Compiles `stmt` with an explicit evaluation grid. See [`compile_statement_with_grid`]. +pub(crate) fn compile_selectors_with_grid( + stmt: &EvalStmt, + bindings: &dyn Fn(&SelectorRequirement) -> Result>, + ordering: SeriesOrdering, + grid: Arc, +) -> Result { + compile_selectors_with_grid_and_value_field( + stmt, + bindings, + ordering, + Arc::from(VECTOR_FIELD), + Some(grid), + ) +} + fn compile_selectors_with_value_field( stmt: &EvalStmt, bindings: &dyn Fn(&SelectorRequirement) -> Result>, ordering: SeriesOrdering, value_field: Arc, +) -> Result { + compile_selectors_with_grid_and_value_field(stmt, bindings, ordering, value_field, None) +} + +fn compile_selectors_with_grid_and_value_field( + stmt: &EvalStmt, + bindings: &dyn Fn(&SelectorRequirement) -> Result>, + ordering: SeriesOrdering, + value_field: Arc, + grid: Option>, ) -> Result { if ordering == SeriesOrdering::Interleaved { return Err(not_implemented( @@ -433,7 +483,12 @@ fn compile_selectors_with_value_field( bindings, ordering, inputs: &inputs, - grid: statement_grid(stmt)?, + // The grid is derived only once the statement is a shape this compiler answers, so an + // unsupported statement reports the reason it has no tree rather than a grid complaint. + grid: match grid { + Some(grid) => grid, + None => statement_grid(stmt)?, + }, value_field, }; let tree = NativeTree::try_new(compile_expr(&context, &stmt.expr)?)?; @@ -1823,6 +1878,71 @@ fn window_selection( /// than a number either caller may choose. const DEFAULT_SUBQUERY_STEP_MS: i64 = 60_000; +/// The grid a subquery's inner expression is evaluated on, and the window that reads it. +/// +/// Both halves of the native route need the same answer: the compiler binds the grid into the node, +/// where it tells the window which points its input was evaluated at, and the planner binds the tree +/// it compiles to the grid its rows carry. A second derivation would let the two disagree about the +/// default step or the conversion of the range, and a tree bound to a grid it was not compiled on +/// cannot be planned, so the numbers are derived here once. +pub(crate) struct SubqueryGrid { + /// The interval the range function folds, and how far back from the anchor it reads. + pub(crate) window: RangeWindow, + /// The grid `subquery.expr` is evaluated on. + pub(crate) grid: Arc, +} + +/// Derives the [`SubqueryGrid`] of `subquery`, whose outer statement is evaluated on `outer`. +/// +/// The child grid is one point per `step` over the interval the subquery's window reads, which its +/// `@` anchor, offset and range decide; without an anchor it is the statement's own range. Every +/// number is converted checked, so a range or a step too large to name a millisecond is reported as +/// itself rather than wrapped into a different interval. +/// +/// `None` when the anchored interval contains no step boundary, which names no evaluation point at +/// all. Declining is the caller's decision: the compiler reports the shape it cannot fold, while the +/// planner keeps the plan the existing path builds. +pub(crate) fn subquery_child_grid( + stmt: &EvalStmt, + outer: &EvalGrid, + subquery: &SubqueryExpr, +) -> Result> { + // The anchor is resolved by the same function a range selector's is, so `start()`, `end()` and a + // literal — including a negative one — mean the same thing in both shapes. + let anchor_ms = selector_anchor_ms(&subquery.at, stmt)?; + let range_ms = range_millis(subquery.range.as_millis())?; + // A subquery step of zero names no evaluation point at all, so it is reported as its own value + // rather than as an empty grid. + let step_ms = match subquery.step { + Some(step) => millis(step.as_millis()).ok_or_else(|| { + DataFusionError::Plan(format!( + "the subquery's step of {}ms is out of range", + step.as_millis() + )) + })?, + None => DEFAULT_SUBQUERY_STEP_MS, + }; + if step_ms <= 0 { + return Err(DataFusionError::Plan(format!( + "the subquery's step is {step_ms}ms, and a step has to be longer than zero" + ))); + } + let offset_ms = selector_offset_ms(&subquery.offset)?; + let window = RangeWindow { + range_ms, + offset_ms, + anchor_ms, + modifier: None, + }; + let Some(spec) = subquery_grid(outer.spec(), window, step_ms)? else { + return Ok(None); + }; + // Keep subquery grids deferred: selector binding needs only their spec endpoints, not a + // materialized timestamp vector proportional to the range. + let grid = Arc::new(EvalGrid::try_deferred(spec).map_err(grid_error)?); + Ok(Some(SubqueryGrid { window, grid })) +} + /// Binds `function` over `subquery`, whose inner expression is evaluated on a grid of its own. /// /// A range function folds a matrix, and PromQL writes a matrix two ways. A range selector reads raw @@ -1842,16 +1962,7 @@ fn subquery_window( function: RangeFunction, subquery: &SubqueryExpr, ) -> Result { - let SubqueryExpr { - expr, - range, - step, - offset, - at, - } = subquery; - // The anchor is resolved by the same function a range selector's is, so `start()`, `end()` and a - // literal — including a negative one — mean the same thing in both shapes. - let anchor_ms = selector_anchor_ms(at, context.stmt)?; + let SubqueryExpr { expr, .. } = subquery; // A subquery evaluates an expression and hands the window one value per grid point per field, so // the expression inside it has to be a vector: a scalar has no fields for the window to fold. if expr.value_type() != ValueType::Vector { @@ -1861,39 +1972,16 @@ fn subquery_window( expr.value_type() ))); } - let range_ms = range_millis(range.as_millis())?; - // A subquery step of zero names no evaluation point at all, so it is reported as its own value - // rather than as an empty grid. - let step_ms = match step { - Some(step) => millis(step.as_millis()).ok_or_else(|| { - DataFusionError::Plan(format!( - "the subquery's step of {}ms is out of range", - step.as_millis() - )) - })?, - None => DEFAULT_SUBQUERY_STEP_MS, - }; - if step_ms <= 0 { - return Err(DataFusionError::Plan(format!( - "the subquery's step is {step_ms}ms, and a step has to be longer than zero" + let Some(SubqueryGrid { + window, + grid: child_grid, + }) = subquery_child_grid(context.stmt, &context.grid, subquery)? + else { + return Err(not_implemented(&format!( + "the subquery `{subquery}`, which names no evaluation point in its anchored interval or \ + the statement's range" ))); - } - let offset_ms = selector_offset_ms(offset)?; - let window = RangeWindow { - range_ms, - offset_ms, - anchor_ms, - modifier: None, }; - let child_spec = subquery_grid(context.grid.spec(), window, step_ms)?.ok_or_else(|| { - not_implemented(&format!( - "the subquery over {range_ms}ms at {step_ms}ms per step, which names no evaluation point \ - in its anchored interval or the statement's range" - )) - })?; - // Keep subquery grids deferred: selector binding needs only their spec endpoints, not a - // materialized timestamp vector proportional to the range. - let child_grid = Arc::new(EvalGrid::try_deferred(child_spec).map_err(grid_error)?); let inner = compile_expr( &Context { stmt: context.stmt, diff --git a/src/query/src/promql/planner/native.rs b/src/query/src/promql/planner/native.rs index 1c97888b050..68697b42d5a 100644 --- a/src/query/src/promql/planner/native.rs +++ b/src/query/src/promql/planner/native.rs @@ -35,7 +35,7 @@ use std::borrow::Cow; use std::sync::Arc; -use std::time::{Duration, UNIX_EPOCH}; +use std::time::{Duration, SystemTime, UNIX_EPOCH}; use common_catalog::consts::{METRIC_ENGINE, MITO_ENGINE, MITO2_ENGINE}; use common_query::native_histogram::native_histogram_value_type; @@ -70,7 +70,7 @@ use promql_parser::label::{METRIC_NAME, MatchOp, Matchers}; use promql_parser::parser::value::ValueType; use promql_parser::parser::{ BinModifier, EvalStmt, Expr as PromExpr, LabelModifier, MatrixSelector, ParenExpr, - VectorMatchCardinality, VectorSelector, token, + SubqueryExpr, VectorMatchCardinality, VectorSelector, token, }; use session::context::{MetricDiscoveryScope, QueryContextRef}; use snafu::ResultExt; @@ -79,7 +79,8 @@ use store_api::region_engine::RawLabelSource; use crate::promql::error::{DataFusionPlanningSnafu, Result}; use crate::promql::native_compile::{ - InfoInput, compile_selectors, compile_statement, info_input, statement_grid, + InfoInput, compile_selectors_with_grid, compile_statement_with_grid, info_input, + statement_grid, subquery_child_grid, }; use crate::promql::planner::{DEFAULT_FIELD_COLUMN, DEFAULT_TIME_INDEX_COLUMN, PromPlanner}; use crate::query_engine::QueryEngineState; @@ -487,35 +488,61 @@ impl PromPlanner { if !self.native_fused { return Ok(None); } - // Over the wire this route plans the whole statement as one fused node over raw sources: - // each region answers the series it holds, and the node reconciles them before evaluating. - // In process a region can also run a split or a segmented tree over its own scan, so those - // shapes stay there. This is a planning choice, not a limit of what the node can encode. let self_contained = plans_over_remote_regions(query_engine_state); - // Only the statement root is offered: the tree is compiled against the statement's grid, - // and a subquery's expression is evaluated on its own, smaller one. A range-vector root is - // the instant query's raw sample result, which the existing path reports as a matrix rather - // than as one point per grid position. - if prom_expr.value_type() == ValueType::Matrix { + // A range selector returns raw samples, which have no evaluation grid to bind. A subquery + // root instead returns its inner expression on the subquery's child grid, and a range + // query has no grid to bind a matrix root to at all. + let subquery_root = root_subquery(prom_expr); + if prom_expr.value_type() == ValueType::Matrix + && (subquery_root.is_none() || self.ctx.start != self.ctx.end) + { return Ok(None); } let Some(stmt) = self.native_statement(prom_expr) else { return Ok(None); }; + // The subquery root's inner expression is compiled on the child grid, with the statement + // left untouched: `@ start()` and `@ end()` inside it resolve against the statement's own + // bounds, not against the child's. A statement whose grid cannot be built is not one this + // route can evaluate, so it keeps the existing plan. The child grid is the compiler's own + // derivation, so both halves of the route agree on the step, the range and the anchor. + let child_grid = if let Some(subquery) = subquery_root { + let Ok(outer) = statement_grid(&stmt) else { + return Ok(None); + }; + let Some(child) = + subquery_child_grid(&stmt, &outer, subquery).context(DataFusionPlanningSnafu)? + else { + return Ok(None); + }; + Some(child.grid) + } else { + None + }; + let effective_expr = match subquery_root { + Some(subquery) => subquery.expr.as_ref(), + None => &stmt.expr, + }; let mut selectors = Vec::new(); - if !collect_selectors(&stmt, prom_expr, &mut selectors) { + if !collect_selectors(&stmt, effective_expr, &mut selectors) { return Ok(None); } // The compiler decides which calls and shapes this route answers: a name it has no kernel // for comes back as `NotImplemented`, which is a decline below. A second list of names here // refused expressions the compiler does fold - a call under an aggregate was admitted while // the same call on its own was not - so there is one place that decides. - let Ok(grid) = statement_grid(&stmt) else { + let Ok(statement) = statement_grid(&stmt) else { return Ok(None); }; + // The one grid every lowering path below binds: a subquery root is compiled on its child + // grid, and the rows it publishes carry that grid, so the split, the boundary and the + // segment nodes have to publish on it too rather than on the statement's. + let grid = child_grid.unwrap_or(statement); if selectors.is_empty() { - let tree = match compile_statement(&stmt) { + let mut compile_stmt = stmt.clone(); + compile_stmt.expr = effective_expr.clone(); + let tree = match compile_statement_with_grid(&compile_stmt, Arc::clone(&grid)) { Ok(tree) => tree, Err(DataFusionError::NotImplemented(_)) => return Ok(None), Err(error) => return Err(error).context(DataFusionPlanningSnafu), @@ -552,7 +579,7 @@ impl PromPlanner { Arc::from(prom_expr.to_string()), tree, FusedSeriesBinding { - grid, + grid: Arc::clone(&grid), time_index, tags: Vec::new(), metric_name: None, @@ -888,8 +915,10 @@ impl PromPlanner { }) .collect::>>()?; let bind_index = std::cell::Cell::new(0usize); - let compiled = match compile_selectors( - &stmt, + let mut compile_stmt = stmt.clone(); + compile_stmt.expr = effective_expr.clone(); + let compiled = match compile_selectors_with_grid( + &compile_stmt, &|req| { let i = bind_index.get(); bind_index.set(i + 1); @@ -914,6 +943,7 @@ impl PromPlanner { Ok(Arc::clone(schema)) }, NATIVE_ORDERING, + Arc::clone(&grid), ) { Ok(v) => v, // The compiler refusing a shape is this route declining it. @@ -1279,7 +1309,7 @@ impl PromPlanner { let Some(rawinput) = children.into_iter().next() else { return Ok(None); }; - let publish_grid = fused_publish_grid(final_tree.root(), &grid); + let publish_grid = Arc::clone(&grid); let node = plan_split_aggregate( Arc::from(prom_expr.to_string()), partial, @@ -1375,7 +1405,9 @@ impl PromPlanner { } else { (tags, metric) }; - let publish_grid = fused_publish_grid(tree.root(), &grid); + // Every lowering path above publishes on the same grid the tree was compiled on, so the + // binding is that grid rather than a per-shape choice. + let publish_grid = Arc::clone(&grid); let node = FusedSeries::try_new( Arc::from(prom_expr.to_string()), tree, @@ -1442,8 +1474,8 @@ impl PromPlanner { /// `None` means the bounds are not representable as a [`SystemTime`], which is not a grid a /// tree can be evaluated on either. fn native_statement(&self, prom_expr: &PromExpr) -> Option { - let start = UNIX_EPOCH.checked_add(Duration::from_millis(self.ctx.stmt_start as u64))?; - let end = UNIX_EPOCH.checked_add(Duration::from_millis(self.ctx.stmt_end as u64))?; + let start = epoch_millis(self.ctx.stmt_start)?; + let end = epoch_millis(self.ctx.stmt_end)?; Some(EvalStmt { expr: prom_expr.clone(), start, @@ -1918,6 +1950,8 @@ fn cut_segments(node: &NativeNode, canonical_segmentation: bool) -> Segment { /// expression of a subquery compiles to, produces on the subquery's grid rather than on the /// statement's. A statement root declares the statement's grid, so the two agree there. A root with /// no declared grid - raw samples - is read where it was scanned and keeps the caller's grid. +/// Every lowering path binds the effective grid (the subquery's child grid for a matrix root, the +/// statement's grid otherwise), so a window root answers that same grid here. fn fused_publish_grid(root: &NativeNode, statement_grid: &Arc) -> Arc { root.output .grid() @@ -2550,6 +2584,11 @@ fn segment_tree( ) }; let (segment_metric, segment_time_index) = segment_binding(¤t_source_ids); + // A segment publishes on the grid its root declares, which is the grid its consumer reads it + // on. Segmentation recurses into a cut subtree with the caller's grid, and that subtree can sit + // inside a subquery window: the canonical segmentation of a collapsing rewrite cuts the window's + // input while its caller still holds the statement's grid. Binding the caller's grid here + // rejects such a segment, and the rejection aborts the whole route. let publish_grid = fused_publish_grid(parent.root(), grid); let Ok(plan) = FusedSeries::try_new_with_format( Arc::from(prom_expr.to_string()), @@ -2571,6 +2610,28 @@ fn segment_tree( }))) } +/// The moment `millis` milliseconds from the Unix epoch, on whichever side of it that lands. +/// +/// A pre-epoch statement (`TQL EVAL ('-60', '-60', '1m')`) reaches the planner as a negative +/// millisecond, which `PromPlannerContext::from_eval_stmt` keeps on purpose. `None` means the bound +/// is outside what a [`SystemTime`] represents. +fn epoch_millis(millis: i64) -> Option { + if millis.is_negative() { + UNIX_EPOCH.checked_sub(Duration::from_millis(millis.unsigned_abs())) + } else { + UNIX_EPOCH.checked_add(Duration::from_millis(millis as u64)) + } +} + +/// Finds a subquery at the statement root, treating parentheses as transparent. +fn root_subquery(expr: &PromExpr) -> Option<&SubqueryExpr> { + match expr { + PromExpr::Subquery(subquery) => Some(subquery), + PromExpr::Paren(paren) => root_subquery(&paren.expr), + _ => None, + } +} + /// The native root's value columns, named from the compiled tree rather than legacy expressions. fn native_root_fields(node: &NativeNode) -> Option> { match &node.output { diff --git a/tests-integration/src/tests/promql_test.rs b/tests-integration/src/tests/promql_test.rs index a917df2757f..77e56a37c86 100644 --- a/tests-integration/src/tests/promql_test.rs +++ b/tests-integration/src/tests/promql_test.rs @@ -31,7 +31,7 @@ use datatypes::arrow::array::{ Array, Float64Array, Int64Array, StringArray, TimestampMillisecondArray, }; use datatypes::arrow::compute::cast; -use datatypes::arrow::datatypes::DataType; +use datatypes::arrow::datatypes::{DataType, TimeUnit}; use frontend::instance::Instance; use query::datafusion::{QUERY_FALLBACK_HINT, QUERY_PARALLELISM_HINT}; use query::dist_plan::MergeScanExec; @@ -2198,6 +2198,55 @@ async fn native_promql_clamp_aggregates_execute_on_native_segments() { } } +/// [`native_result_rows`] for a statement whose output carries no series labels, such as the +/// data-free `vector(time())`: the evaluation timestamp and the one value, sorted by time. +/// +/// The timestamp is found by type rather than by name: the native route names the time index of a +/// statement that reads nothing `time`, while the existing planner names it `ts`. +async fn native_timestamp_rows(output: Output) -> Vec<(i64, f64)> { + let batches = match output.data { + OutputData::Stream(stream) => collect_batches(stream).await.unwrap(), + OutputData::RecordBatches(batches) => batches, + _ => unreachable!("TQL EVAL returns record batches"), + }; + let mut rows = batches + .iter() + .flat_map(|batch| { + let timestamps = batch + .columns() + .iter() + .find(|column| { + matches!( + column.data_type(), + DataType::Timestamp(TimeUnit::Millisecond, _) + ) + }) + .expect("a timestamp column"); + let timestamps = timestamps + .as_any() + .downcast_ref::() + .unwrap(); + let value_columns = batch + .columns() + .iter() + .filter(|column| column.data_type() == &DataType::Float64) + .collect::>(); + assert_eq!(value_columns.len(), 1, "expected one Float64 value column"); + let values = value_columns[0] + .as_any() + .downcast_ref::() + .unwrap(); + assert_eq!(timestamps.null_count(), 0); + assert_eq!(values.null_count(), 0); + (0..batch.num_rows()) + .map(|row| (timestamps.value(row), values.value(row))) + .collect::>() + }) + .collect::>(); + rows.sort_by_key(|row| row.0); + rows +} + /// [`native_result_rows`] plus each row's OTLP temporality marker, sorted by label then time. async fn native_marker_rows(output: Output) -> Vec<(i64, String, String, f64)> { let batches = match output.data { @@ -12495,6 +12544,42 @@ async fn native_promql_subqueries_match_legacy() { legacy.sort_by(|a, b| (a.0, &a.1).cmp(&(b.0, &b.1)).then(a.2.total_cmp(&b.2))); assert_eq!(native, legacy, "{sql}"); } + + // A rewrite that writes a label the series already carry collapses them: the canonicalizer puts + // a merge at the statement root, and the segmentation below runs with canonical segmentation on. + // The fold inside the subquery is a global consumer, so it is cut recursively, and that nested + // segment answers the subquery's own grid - the two points at 120000 and 180000 - while the + // statement's grid holds the single point at 180000. Publishing the nested segment on the + // caller's grid rejects its binding, and the rejection aborts the whole route, so the statement + // has to stay native here and answer what the existing plan answers. + // + // `host="h0"` keeps one series: the rewrite makes every series' label set `host="all"`, and two + // of them meeting at one grid point is the overlap both routes report as an error. + let collapse = "TQL EVAL (180, 180, '1m') label_replace(avg_over_time((sum by (host) \ + (native_subq{host=\"h0\"}))[2m:1m]), \"host\", \"all\", \"\", \"\")"; + observer.clear(); + let collapse_native = + native_result_rows(native_tql_output(&instance, collapse, true).await).await; + assert!( + observer.fused().is_some(), + "{collapse} must run on the native route: {:#?}", + observer.root_plan() + ); + observer.clear(); + let collapse_legacy = + native_result_rows(native_tql_output(&instance, collapse, false).await).await; + assert!( + observer.fused().is_none(), + "{collapse} must keep the existing plan when the native route is off" + ); + // `h0` sums to 10 + 40 at 120000 and 20 + 80 at 180000, so the fold over the subquery's two + // points averages to 75 at the statement's one step. + assert_eq!( + collapse_native, + vec![(180_000, "all".to_string(), 75.0)], + "{collapse}" + ); + assert_eq!(collapse_native, collapse_legacy, "{collapse}"); } /// An aggregate inside a subquery is still an aggregate, so it keeps that operand out of the @@ -12559,3 +12644,292 @@ async fn native_promql_aggregate_subquery_operands_run_natively() { assert_eq!(native, legacy, "{sql}"); } } +#[tokio::test(flavor = "multi_thread")] +async fn native_promql_matrix_root_matches_legacy() { + common_telemetry::init_default_ut_logging(); + let mito_config = mito2::config::MitoConfig { + experimental_series_scan_v2: true, + ..Default::default() + }; + let (standalone, observer) = native_test_instance( + GreptimeDbStandaloneBuilder::new("native_promql_matrix_root").with_mito_config(mito_config), + ) + .await; + let instance = standalone.fe_instance().clone(); + execute_all( + &instance, + "CREATE TABLE native_matrix_root (host STRING, ts TIMESTAMP TIME INDEX, val DOUBLE, PRIMARY KEY(host))", + QueryContext::arc(), + ) + .await; + let mut values = Vec::new(); + for host in 0..4 { + for minute in 0..=10 { + values.push(format!( + "('h{host}', {}, {})", + minute * 60_000, + host * 1000 + minute + )); + } + } + execute_all( + &instance, + &format!( + "INSERT INTO native_matrix_root (host, ts, val) VALUES {}", + values.join(",") + ), + QueryContext::arc(), + ) + .await; + + async fn run( + instance: &Arc, + observer: &NativePlanObserver, + sql: &str, + native: bool, + ) -> Vec<(i64, String, f64)> { + observer.clear(); + native_result_rows(native_tql_output(instance, sql, native).await).await + } + + let sql = "TQL EVAL (600, 600, '1m') native_matrix_root[5m:1m]"; + let native = run(&instance, &observer, sql, true).await; + assert!( + observer.fused().is_some(), + "subquery root did not run natively: {sql}" + ); + let legacy = run(&instance, &observer, sql, false).await; + assert_eq!(native, legacy); + assert_eq!( + native + .iter() + .map(|row| row.0) + .collect::>(), + [360_000, 420_000, 480_000, 540_000, 600_000].into() + ); + + let sql = "TQL EVAL (600, 600, '1m') native_matrix_root[5m:1m] offset 1m"; + let native = run(&instance, &observer, sql, true).await; + assert!( + observer.fused().is_some(), + "offset subquery root did not run natively: {sql}" + ); + let legacy = run(&instance, &observer, sql, false).await; + assert_eq!(native, legacy); + assert_eq!( + native + .iter() + .map(|row| row.0) + .collect::>(), + [300_000, 360_000, 420_000, 480_000, 540_000].into() + ); + + // PromQL's `@ 420` is seconds, so this anchor is 420,000ms on the stored timestamp grid. + let anchor_sql = "TQL EVAL (600, 600, '1m') native_matrix_root[5m:1m] @ 420"; + let anchored = run(&instance, &observer, anchor_sql, true).await; + let other_instant = "TQL EVAL (540, 540, '1m') native_matrix_root[5m:1m] @ 420"; + let anchored_other = run(&instance, &observer, other_instant, true).await; + assert_eq!( + anchored, anchored_other, + "@ anchor must ignore query instant" + ); + assert_eq!( + anchored + .iter() + .map(|row| row.0) + .collect::>(), + [180_000, 240_000, 300_000, 360_000, 420_000].into() + ); + let mut expected_values = (0..4) + .flat_map(|host| { + (3..=7).map(move |minute| { + ( + minute * 60_000, + format!("h{host}"), + (host * 1000 + minute) as f64, + ) + }) + }) + .collect::>(); + expected_values.sort_by(|left, right| (left.0, &left.1).cmp(&(right.0, &right.1))); + assert_eq!(anchored, expected_values); + + for (expr, grid) in [ + ( + "(native_matrix_root @ start())[5m:1m]", + [360_000, 420_000, 480_000, 540_000, 600_000], + ), + ( + "(native_matrix_root @ end())[5m:1m]", + [360_000, 420_000, 480_000, 540_000, 600_000], + ), + ] { + let sql = format!("TQL EVAL (600, 600, '1m') {expr}"); + let native = run(&instance, &observer, &sql, true).await; + assert!(observer.fused().is_some(), "{expr} did not run natively"); + let legacy = run(&instance, &observer, &sql, false).await; + assert_eq!(native, legacy, "{expr}"); + assert_eq!( + native + .iter() + .map(|row| row.0) + .collect::>(), + grid.into() + ); + } + + let sql = "TQL EVAL (60, 60, '1m') native_matrix_root[5m:1m]"; + let native = run(&instance, &observer, sql, true).await; + assert!( + observer.fused().is_some(), + "negative child grid did not run natively" + ); + let legacy = run(&instance, &observer, sql, false).await; + assert_eq!(native, legacy, "negative child grid"); + + // A statement before the Unix epoch is valid PromQL, and the subquery root rebuilds the + // statement from the planner's own bounds. -60s has to stay 60 seconds *before* the epoch: read + // as a magnitude it lands hundreds of millions of years in the future, which is not a grid this + // route can build, and an error there would replace the answer the existing planner gives. + // The bounds are quoted because `TQL EVAL` only accepts a pre-epoch time as a string: an + // unquoted `-60` is a negation, which the TQL parser rejects before planning. + async fn run_timestamps( + instance: &Arc, + observer: &NativePlanObserver, + sql: &str, + native: bool, + ) -> Vec<(i64, f64)> { + observer.clear(); + native_timestamp_rows(native_tql_output(instance, sql, native).await).await + } + // `vector(time())` is the shape the pre-epoch grid is read on: it reads no data, so its rows are + // the subquery's child grid, and every one of them is before the epoch. Only the native route + // can be asked for it — the existing planner's `vector()` takes a literal only — so this case + // asserts the answer itself, five negative timestamps 60s apart ending at the instant. + let pre_epoch_time = "TQL EVAL ('-60', '-60', '1m') vector(time())[5m:1m]"; + let native = run_timestamps(&instance, &observer, pre_epoch_time, true).await; + assert!( + observer.fused().is_some(), + "pre-epoch subquery root did not run natively: {pre_epoch_time}" + ); + assert_eq!( + native, + [ + (-300_000, -300.0), + (-240_000, -240.0), + (-180_000, -180.0), + (-120_000, -120.0), + (-60_000, -60.0), + ], + "the pre-epoch grid keeps its negative milliseconds" + ); + + // A constant is the same pre-epoch statement on a shape both routes plan, so the answer can be + // compared: the native grid has to be the one the existing planner works on. + let pre_epoch_constant = "TQL EVAL ('-60', '-60', '1m') vector(1)[5m:1m]"; + let native = run_timestamps(&instance, &observer, pre_epoch_constant, true).await; + assert!( + observer.fused().is_some(), + "pre-epoch constant subquery root did not run natively: {pre_epoch_constant}" + ); + assert_eq!( + native, + [ + (-300_000, 1.0), + (-240_000, 1.0), + (-180_000, 1.0), + (-120_000, 1.0), + (-60_000, 1.0), + ], + "the pre-epoch constant grid keeps its negative milliseconds" + ); + let legacy = run_timestamps(&instance, &observer, pre_epoch_constant, false).await; + assert_eq!(native, legacy, "pre-epoch subquery root"); + + // The same pre-epoch statement anchored at 420s: the anchor decides the child grid. + let pre_epoch_anchored = "TQL EVAL ('-60', '-60', '1m') vector(1)[5m:1m] @ 420"; + let native = run_timestamps(&instance, &observer, pre_epoch_anchored, true).await; + assert!( + observer.fused().is_some(), + "anchored pre-epoch subquery root did not run natively: {pre_epoch_anchored}" + ); + let legacy = run_timestamps(&instance, &observer, pre_epoch_anchored, false).await; + assert_eq!(native, legacy, "anchored pre-epoch subquery root"); + + // The bounds route the statement, not the root shape: this one has no subquery and no matrix + // root at all, a plain aggregate over a selector, and its pre-epoch start still takes the + // native route. It is kept apart from the matrix-root cases because the bound alone is what + // puts it there. The fixture starts at the epoch, so of the five 30s steps over the statement's + // own range only the three from the epoch on have anything inside the 5m lookback: the four + // hosts carry `host * 1000 + minute`, so the epoch step and the one 30s later both read minute + // 0 (0 + 1000 + 2000 + 3000) and the step at 60s reads minute 1 (6004). + let pre_epoch_aggregate = "TQL EVAL ('-60', '60', '30s') sum(native_matrix_root)"; + let native = run_timestamps(&instance, &observer, pre_epoch_aggregate, true).await; + assert!( + observer.fused().is_some(), + "pre-epoch statement without a matrix root did not run natively: {pre_epoch_aggregate}" + ); + let legacy = run_timestamps(&instance, &observer, pre_epoch_aggregate, false).await; + assert_eq!(native, legacy, "pre-epoch aggregate over a selector"); + assert_eq!( + native, + [(0, 6000.0), (30_000, 6000.0), (60_000, 6004.0)], + "the pre-epoch aggregate did not answer on its own 30s grid" + ); + + // A matrix root whose inner expression is folded rather than merely selected reaches the + // split-aggregate lowering, which builds its own final node: that node has to publish on the + // grid the tree was compiled on, which is the subquery's child grid. `sum(...)` answers without + // the tag, so it is compared on its timestamps alone. + let sum_expr = "sum(native_matrix_root)[5m:1m]"; + let sql = format!("TQL EVAL (600, 600, '1m') {sum_expr}"); + let native = run_timestamps(&instance, &observer, &sql, true).await; + assert!( + observer.fused().is_some(), + "matrix root over an aggregate did not run natively: {sum_expr}" + ); + let legacy = run_timestamps(&instance, &observer, &sql, false).await; + assert_eq!(native, legacy, "{sum_expr}"); + assert_eq!( + native.iter().map(|row| row.0).collect::>(), + [360_000, 420_000, 480_000, 540_000, 600_000], + "{sum_expr} did not answer on the subquery's child grid" + ); + + for expr in [ + "(sum by (host) (rate(native_matrix_root[2m])))[5m:1m]", + "(sum by (host) (rate(native_matrix_root[2m])))[5m:1m] @ 600", + "(sum by (host) (rate(native_matrix_root[2m])) * 2)[5m:1m]", + ] { + let sql = format!("TQL EVAL (600, 600, '1m') {expr}"); + let native = run(&instance, &observer, &sql, true).await; + assert!( + observer.fused().is_some(), + "matrix root over a folded inner expression did not run natively: {expr}" + ); + let legacy = run(&instance, &observer, &sql, false).await; + assert_eq!(native, legacy, "{expr}"); + assert!(!native.is_empty(), "{expr} produced no rows"); + } + + let range_query = "TQL EVAL (600, 1200, '1m') native_matrix_root[5m:1m]"; + let err = instance + .do_query(range_query, native_query_context(true, 4)) + .await + .remove(0) + .unwrap_err(); + assert!( + format!("{err:?}").contains("invalid expression type \"range vector\" for range query"), + "{err:?}" + ); + + // This remains on the existing planner: a raw-sample matrix has no evaluation grid to bind. + let sql = "TQL EVAL (600, 600, '1m') native_matrix_root[5m]"; + let native = run(&instance, &observer, sql, true).await; + assert!( + observer.fused().is_none(), + "raw matrix roots must use existing planning" + ); + let legacy = run(&instance, &observer, sql, false).await; + assert_eq!(native, legacy); +} diff --git a/tests-integration/tests/http.rs b/tests-integration/tests/http.rs index b968f418024..6bdfea38926 100644 --- a/tests-integration/tests/http.rs +++ b/tests-integration/tests/http.rs @@ -2294,6 +2294,38 @@ pub async fn test_prometheus_native_promql_route(store_type: StorageType) { } } + // A subquery root is a matrix natively, while query_range rejects a range vector. + let res = client + .get("/v1/prometheus/api/v1/query?query=native_route[5m:1m]&time=300") + .header(native_hint.0, native_hint.1) + .send() + .await; + assert_eq!(res.status(), StatusCode::OK); + let body = serde_json::from_str::(&res.text().await).unwrap(); + assert_eq!( + serde_json::to_value(&body.data).unwrap()["resultType"], + "matrix" + ); + assert_eq!( + serde_json::to_value(&body.data).unwrap()["result"][0]["values"], + json!([ + [60.0, "2.0"], + [120.0, "2.0"], + [180.0, "2.0"], + [240.0, "2.0"], + [300.0, "2.0"] + ]) + ); + let res = client + .get( + "/v1/prometheus/api/v1/query_range?query=native_route[5m:1m]&start=300&end=300&step=1s", + ) + .header(native_hint.0, native_hint.1) + .send() + .await; + assert_eq!(res.status(), StatusCode::BAD_REQUEST); + assert!(res.text().await.contains("range vector")); + // The same expression over a range: a matrix carries every grid point of every series, with the // last sample carried forward, and both routes answer it with the same one. let query = encode("native_route * 100");