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 <discord9@163.com>
This commit is contained in:
discord9
2026-11-05 22:16:57 +08:00
parent 6f43eff1f0
commit 1f0a601abd
2 changed files with 59 additions and 1 deletions
+6 -1
View File
@@ -1063,6 +1063,7 @@ impl PromPlanner {
(metric.clone(), time_index.clone(), tags.clone())
})
.collect::<Vec<_>>();
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));
@@ -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::<Float64Array>()
.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")]