From 1f0a601abd263b70dbc478b48fbff525ba2c7458 Mon Sep 17 00:00:00 2001 From: discord9 Date: Wed, 4 Nov 2026 21:12:05 +0800 Subject: [PATCH] test(promql): cover aggregates over discovered metrics A nameless selector's metric comes from its own frames, so a folding aggregate is a multi-input tree even with one selector: apply the same preflight and order-dependent refusals on the remote route. Cover `sum(increase(...))` and grouping by the metric name over the mixed discovery fixture. A grouping that names only `__name__` stays declined, since the output schema cannot name that group: here the metrics also differ by `site`, so grouping by the name and `site` exercises the two groups. Signed-off-by: discord9 --- src/query/src/promql/planner/native.rs | 7 ++- tests-integration/src/tests/promql_test.rs | 53 ++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/query/src/promql/planner/native.rs b/src/query/src/promql/planner/native.rs index b5c3be0d19d..bce04db4328 100644 --- a/src/query/src/promql/planner/native.rs +++ b/src/query/src/promql/planner/native.rs @@ -1063,6 +1063,7 @@ impl PromPlanner { (metric.clone(), time_index.clone(), tags.clone()) }) .collect::>(); + let discovers_metrics = prepared.iter().any(|input| input.2.is_none()); let children = prepared .into_iter() .map(|(scan, schema, metric, _, tags, _)| { @@ -1209,6 +1210,9 @@ impl PromPlanner { { return Ok(None); } + // A discovered selector already reads every metric, so one selector is several inputs: the + // multi-input refusals above apply to its tree too, including the order-dependent refusal. + let multi_input = multi_input || (self_contained && discovers_metrics); // A tree that ends in a canonical merge answers one series per label set, and the pieces of // one set were spread over the scan's partitions by the rewrite that created them. Lowering // that boundary keeps the scan width: each partition merges the pieces it holds and exports @@ -3310,7 +3314,8 @@ mod tests { // No `FusedSeriesExec`: nothing here reads regions the way the budget assumes. assert!(!native_raw_plan_bounds_region_reads(&scan())); - // A union drains its inputs one after another, so a body budget could stall it. + // A discovered selector's union is not the admitted topology: only the single fused node + // takes the query-wide budget, so this plan keeps its per-scan bounds. let union = datafusion::physical_plan::union::UnionExec::try_new(vec![scan(), scan()]) .expect("two inputs"); assert!(!native_raw_plan_bounds_region_reads(&union)); diff --git a/tests-integration/src/tests/promql_test.rs b/tests-integration/src/tests/promql_test.rs index 60de0ce9684..a8c7b720f0d 100644 --- a/tests-integration/src/tests/promql_test.rs +++ b/tests-integration/src/tests/promql_test.rs @@ -3243,6 +3243,59 @@ async fn distributed_native_promql_metric_discovery_reconciles_mixed_ordering() // not counted against it. let peak = cluster.network_do_get_peak_active(); assert!((1..=3).contains(&peak), "region reads stay bounded: {peak}"); + + // An aggregate over a discovered selector reads the metric name from each source's own frames, + // not from one bound metric. Folding without grouping adds them; grouping by the name must keep + // the two metrics apart even though their ordinary labels are identical. + for (expression, expected) in [ + (r#"sum(increase({host="a"}[5m]))"#, vec![10.0, 14.0]), + ( + r#"sum by (__name__, site) (increase({host="a"}[5m]))"#, + vec![5.0, 5.0, 7.0, 7.0], + ), + ] { + let output = instance + .do_promql_query( + &PromQuery { + query: expression.to_string(), + start: "180".to_string(), + end: "240".to_string(), + step: "60s".to_string(), + ..PromQuery::default() + }, + native_query_context(true, 2), + ) + .await + .remove(0) + .unwrap(); + let plan = output.meta.plan.as_ref().expect("frontend physical plan"); + assert_eq!( + native_plan_shape(plan).fused, + 1, + "a discovery aggregate stays native: {plan:?}" + ); + let batches = match output.data { + OutputData::Stream(stream) => collect_batches(stream).await.unwrap(), + OutputData::RecordBatches(batches) => batches, + _ => unreachable!("PromQL returns record batches"), + }; + let mut rows = Vec::new(); + for batch in batches { + let values = batch + .columns() + .iter() + .find(|column| column.data_type() == &DataType::Float64) + .expect("one value column") + .as_any() + .downcast_ref::() + .unwrap(); + for row in 0..batch.num_rows() { + rows.push(values.value(row)); + } + } + rows.sort_by(f64::total_cmp); + assert_eq!(expected, rows, "{expression}"); + } } #[tokio::test(flavor = "multi_thread")]