diff --git a/src/promql/src/extension_plan/aggregate_finalize.rs b/src/promql/src/extension_plan/aggregate_finalize.rs index 1535119015a..ce6a079928a 100644 --- a/src/promql/src/extension_plan/aggregate_finalize.rs +++ b/src/promql/src/extension_plan/aggregate_finalize.rs @@ -187,7 +187,8 @@ impl AggregateFinalize { /// its binding: its `Utf8` columns are the labels it writes, and its `Float64` columns name the /// merged fields by position, since the merge folds the cut aggregate's fields in the order the /// statement declares them. Building rejects an input that is not state, a suffix the runtime - /// does not execute and a schema [`classify_output`] cannot convert. + /// does not execute (the supported suffix operations are maps, aggregates, TopK and + /// constant-sided binary operations) and a schema [`classify_output`] cannot convert. pub fn to_execution_plan( &self, input: Arc, @@ -346,13 +347,18 @@ struct Split { /// Splits the innermost aggregate out of `node`. /// -/// `Ok(None)` is a subtree holding no aggregate. Only a map, an aggregate, topk/bottomk and a binary -/// operation with a constant on one side may sit above the cut. TopK/BottomK is safe because it runs -/// once in the suffix over the fully merged aggregate series, per evaluation timestamp, and never in -/// the partition-local producer. A map is point-wise and an aggregate runs over the complete merge, -/// while an instant selection, a window, other vector-wide operations or a binary operation between -/// two vectors would read evaluated values, the whole vector or a matched pair, shapes the merge does -/// not produce. +/// `Ok(None)` is a subtree holding no aggregate. The currently verified suffix operations are maps, +/// aggregates, TopK/BottomK and binary operations with a constant on one side. TopK/BottomK run once +/// in the suffix over the fully merged aggregate series, per evaluation timestamp, and never in the +/// partition-local producer. A map is point-wise and an aggregate runs over the complete merge. +/// LimitK is declined because it selects the first `k` series in input order +/// ([`crate::select::limitk`]), while this merge does not preserve the storage input order. Other +/// vector operations have not yet been verified/supported by this split. +/// +/// An instant selection or window above the cut cannot be applied to the merged values under the +/// tree's current binding: the cut output is evaluated, whereas these nodes consume raw samples. +/// A binary operation between two vectors would require matching the cut's merged output against a +/// vector sibling, but this merge only gathers the cut's state. fn split(node: &NativeNode, fields: &[FieldSpec], time_index: &str) -> DfResult> { match &node.expr { NativeExpr::Input(_) | NativeExpr::Scalar(_) => Ok(None), @@ -626,9 +632,9 @@ const STRING_CAPACITY: usize = 1024; /// first item rather than a row the error invalidates after it. What the answer holds is charged to /// that stream, which is the only owner it has: the plan answers once. /// -/// The cut is the innermost aggregate of a tree of inputs, maps and aggregates, so the suffix the -/// runtime is asked to build is one of those; [`ExecutionPlan::execute`] builds its state and is -/// where a shape the runtime cannot run is refused. +/// The suffix above the cut is built from the aggregate's output and may contain maps, aggregates, +/// TopK/BottomK or a binary operation with a constant on one side. [`ExecutionPlan::execute`] +/// builds its state and is where a shape the runtime cannot run is refused. /// /// The plain state-batch input is temporary. The final design consumes series frames directly /// from FusedSeries instead of decoding and rebuilding this intermediate state-row layout. diff --git a/src/promql/src/extension_plan/aggregate_finalize/tests.rs b/src/promql/src/extension_plan/aggregate_finalize/tests.rs index c269c9cac66..d7136ee2886 100644 --- a/src/promql/src/extension_plan/aggregate_finalize/tests.rs +++ b/src/promql/src/extension_plan/aggregate_finalize/tests.rs @@ -338,11 +338,7 @@ fn a_constant_operand_keeps_the_cut_below_it() { "the merged values enter where the aggregate was: {}", node.suffix ); - assert!( - !node.suffix.to_string().contains("Window"), - "{}", - node.suffix - ); + assert!(!node.suffix.to_string().contains("Window")); } } @@ -385,30 +381,26 @@ fn topk_and_bottomk_run_after_the_aggregate_cut() { assert!( matches!(&input.expr, NativeExpr::Input(bound) if bound.source_index == 0 && bound.output.samples().is_some_and(|schema| schema.fields.len() == 1 && schema.fields[0].name.as_ref() == "v0" && schema.eval_grid.as_ref().is_some_and(|actual| actual.spec() == grid().spec()))) ); - assert!(!node.suffix.to_string().contains("Window")); } } #[test] -fn other_vector_operations_above_the_cut_are_declined() { - let aggregate = || complete(AggregateKind::Sum, by(&["k0"]))(window()); - for op in [ - BoundVectorOp::Sort(crate::select::SortOrder::Asc), - BoundVectorOp::LimitK { - k: 1.0, - group: by(&[]), - }, - ] { - let error = finalize( - vector_over(aggregate(), op), - output_schema(&["sum(v0)"], &["k0"]), - ) - .unwrap_err(); - assert!( - matches!(error, DataFusionError::NotImplemented(_)), - "{error}" - ); - } +fn limitk_above_the_cut_is_declined_because_merge_does_not_preserve_input_order() { + let error = finalize( + vector_over( + complete(AggregateKind::Sum, by(&["k0"]))(window()), + BoundVectorOp::LimitK { + k: 1.0, + group: by(&[]), + }, + ), + output_schema(&["sum(v0)"], &["k0"]), + ) + .unwrap_err(); + assert!( + matches!(error, DataFusionError::NotImplemented(_)), + "{error}" + ); } #[test] diff --git a/tests-integration/src/tests/native_aggregate_test.rs b/tests-integration/src/tests/native_aggregate_test.rs index 2aa6f1df682..ce5769ff9c3 100644 --- a/tests-integration/src/tests/native_aggregate_test.rs +++ b/tests-integration/src/tests/native_aggregate_test.rs @@ -1186,29 +1186,26 @@ async fn native_aggregate_topk_over_global_partitions() { .all(|count| count.is_some_and(|count| count > 0)), "{per_partition:?}" ); - assert_eq!( - 56, - per_partition.iter().flatten().sum::(), - "{per_partition:?}" - ); - // Prove the partition-local states overlap without decoding or summing their values: at each // evaluation point, at least one selected hostname/point/field was emitted by distinct original // producer partitions. let mut owners = BTreeMap::<(String, u64, u32), BTreeSet>::new(); for (partition, batch) in &partial_batches { + let schema = batch.schema(); + // `state_schema` names its fixed columns and labels `metric_name`, `tag_N`, `point` and + // `field`; use those names rather than baking their positions into this assertion. let hostname = batch - .column(1) + .column(schema.index_of("tag_0").unwrap()) .as_any() .downcast_ref::() .unwrap(); let batch_point = batch - .column(4) + .column(schema.index_of("point").unwrap()) .as_any() .downcast_ref::() .unwrap(); let field = batch - .column(5) + .column(schema.index_of("field").unwrap()) .as_any() .downcast_ref::() .unwrap();