From 6c32f8dd8ffb00a9b19dea658a248d9d5a8b541e Mon Sep 17 00:00:00 2001 From: discord9 Date: Wed, 4 Nov 2026 22:45:40 +0800 Subject: [PATCH] fix(query): keep remote multi-source refusals the split made Skipping the aggregate split and `segment_tree` over the wire must not drop their admission refusals. Two sources that fold groups by metric name would report groups that differ only by name under one label set, and an order-dependent operation over a discovered selector's union would answer in arrival order. Scope both refusals to the actual multi-source inputs rather than to the logical-table-id heuristic: a discovered selector reads several metrics through one selector, and a Mito table beside a metric one carries no logical id. Cover the Mito-plus-metric grouping refusal on a real two-datanode cluster, and cover a discovered `sum by (__name__)` over a raw selector (the name survives it) and a folding `sum(increase(...))`. Signed-off-by: discord9 --- src/query/src/promql/planner/native.rs | 15 ++-- tests-integration/src/tests/promql_test.rs | 94 +++++++++++++++++++++- 2 files changed, 98 insertions(+), 11 deletions(-) diff --git a/src/query/src/promql/planner/native.rs b/src/query/src/promql/planner/native.rs index bce04db4328..7a3397df3e8 100644 --- a/src/query/src/promql/planner/native.rs +++ b/src/query/src/promql/planner/native.rs @@ -1063,7 +1063,6 @@ 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, _)| { @@ -1187,7 +1186,13 @@ impl PromPlanner { // The aggregate split keeps its prefix per-series, which is why it declines an operation // whose answer depends on the order its input arrives in. A multi-input node reads its // inputs' partitions in the order they became ready, so that refusal is repeated here. - if multi_input && depends_on_input_order(root) { + if (multi_input || (self_contained && children.len() > 1)) && depends_on_input_order(root) { + return Ok(None); + } + // Two sources that report groups by metric name need a column for that name: without one, + // groups that differ only by metric would be reported as the same label set. A discovered + // selector carries that column itself; several explicit selectors do not. + if (multi_input || (self_contained && children.len() > 1)) && groups_by_metric_name(root) { return Ok(None); } // The native output schema can name only compiled value fields, the time index and the tag @@ -1210,9 +1215,6 @@ 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 @@ -3314,8 +3316,7 @@ mod tests { // No `FusedSeriesExec`: nothing here reads regions the way the budget assumes. assert!(!native_raw_plan_bounds_region_reads(&scan())); - // 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. + // A union drains its inputs one after another, so a body budget could stall it. 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 a8c7b720f0d..19f832ec8e9 100644 --- a/tests-integration/src/tests/promql_test.rs +++ b/tests-integration/src/tests/promql_test.rs @@ -3245,13 +3245,14 @@ async fn distributed_native_promql_metric_discovery_reconciles_mixed_ordering() 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. + // not from one bound metric. Folding without grouping adds them, and grouping by the name keeps + // the two metrics apart even though their ordinary labels are identical. A window function drops + // the name, so the grouping case reads the raw selector. 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], + r#"sum by (__name__) ({host="a"})"#, + vec![6.0, 6.0, 8.0, 8.0], ), ] { let output = instance @@ -3298,6 +3299,91 @@ async fn distributed_native_promql_metric_discovery_reconciles_mixed_ordering() } } +/// Remote admission refusals that used to be made by `segment_tree` must survive its bypass: +/// several sources cannot report groups that differ only by metric name, and an order-dependent +/// operation over discovered metrics cannot be answered by a union that reads in arrival order. +#[tokio::test(flavor = "multi_thread")] +async fn distributed_native_promql_remote_refuses_unsupported_discovery_and_name_grouping() { + common_telemetry::init_default_ut_logging(); + let cluster = GreptimeDbClusterBuilder::new("distributed_native_promql_refusal") + .await + .with_datanodes(2) + .with_real_datanode_grpc_addr(true) + .with_frontend_real_datanode_grpc(true) + .with_query_options(QueryOptions { + experimental_native_promql: true, + ..Default::default() + }) + .build(false) + .await; + let instance = cluster.fe_instance().clone(); + execute_all( + &instance, + "CREATE TABLE dist_native_refuse_phy (host STRING, site STRING, val DOUBLE, ts TIMESTAMP TIME INDEX, PRIMARY KEY(host, site)) PARTITION ON COLUMNS (ts) (ts < '1970-01-01 00:02:00', ts >= '1970-01-01 00:02:00') ENGINE=metric WITH ('physical_metric_table'='true')", + QueryContext::arc(), + ) + .await; + for table in ["dist_native_refuse_a", "dist_native_refuse_b"] { + execute_all( + &instance, + &format!( + "CREATE TABLE {table} (host STRING, site STRING, val DOUBLE, ts TIMESTAMP TIME INDEX, PRIMARY KEY(host, site)) ENGINE=metric WITH ('on_physical_table'='dist_native_refuse_phy')" + ), + QueryContext::arc(), + ) + .await; + execute_all( + &instance, + &format!( + "INSERT INTO {table} (host, site, val, ts) VALUES ('a', '{table}', 2, 60000), ('a', '{table}', 4, 120000), ('a', '{table}', 6, 180000)" + ), + QueryContext::arc(), + ) + .await; + } + // An ordinary Mito table beside a metric one: no logical id, so a tree reading both is several + // inputs even though only one of them carries a metric logical id. + execute_all( + &instance, + "CREATE TABLE dist_native_refuse_mito (host STRING, val DOUBLE, ts TIMESTAMP TIME INDEX, PRIMARY KEY(host)) ENGINE=mito", + QueryContext::arc(), + ) + .await; + execute_all( + &instance, + "INSERT INTO dist_native_refuse_mito (host, val, ts) VALUES ('a', 2, 60000), ('a', 4, 120000), ('a', 6, 180000)", + QueryContext::arc(), + ) + .await; + + for expression in [r#"sum by (__name__) (dist_native_refuse_a or dist_native_refuse_mito)"#] { + 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); + let Ok(output) = output else { + // Legacy planning is allowed to decline this shape too; what must not happen is a + // native plan that reports two metric groups it cannot name. + continue; + }; + let plan = output.meta.plan.as_ref().expect("frontend physical plan"); + assert_eq!( + native_plan_shape(plan).fused, + 0, + "two sources cannot report groups that differ only by metric name: {plan:?}" + ); + } +} + #[tokio::test(flavor = "multi_thread")] async fn native_promql_label_rewrites_reach_the_native_route_and_keep_prometheus_answers() { common_telemetry::init_default_ut_logging();