diff --git a/Cargo.lock b/Cargo.lock index 13c70d54a64..34d2e06170c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11413,6 +11413,7 @@ dependencies = [ "regex", "serde", "snafu 0.8.6", + "stacker", "store-api", "table", "tokio", diff --git a/Cargo.toml b/Cargo.toml index cd47c13f779..f32247088ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -258,6 +258,7 @@ sqlx = { version = "0.8", default-features = false, features = [ "tls-rustls-aws-lc-rs", "rust_decimal", ] } +stacker = "0.1.25" stringprep = "0.1" strum = { version = "0.27", features = ["derive"] } sysinfo = "0.33" diff --git a/src/promql/Cargo.toml b/src/promql/Cargo.toml index b1ca57de20d..21870efb8fa 100644 --- a/src/promql/Cargo.toml +++ b/src/promql/Cargo.toml @@ -34,6 +34,7 @@ prost.workspace = true regex.workspace = true serde = { workspace = true, features = ["rc"] } snafu.workspace = true +stacker.workspace = true store-api.workspace = true table.workspace = true tokio.workspace = true diff --git a/src/promql/src/functions/native_histogram.rs b/src/promql/src/functions/native_histogram.rs index 1fcb9c553ad..f53a1c3b741 100644 --- a/src/promql/src/functions/native_histogram.rs +++ b/src/promql/src/functions/native_histogram.rs @@ -405,18 +405,27 @@ fn histogram_pair_udf_with_collector( result.push( match (read_histogram(lhs, row)?, read_histogram(rhs, row)?) { (Some(lhs), Some(rhs)) => { - record_custom_reconciliation(&collector, name, &lhs, &rhs); - record_counter_reset_contradiction(&collector, name, &lhs, &rhs); - let result = op(&lhs, &rhs); - if result.is_none() { - record_warning( - &collector, - format!( - "{name}: dropped native histogram sample with incompatible schemas" - ), - ); + // The layouts decide whether there is a pair at all: Prometheus reports + // a pair whose bucket layouts cannot be combined as that one error, + // before it looks at the reset hints, so a pair that fails both carries + // only the schema warning. This is the order the native matcher in + // `vector_match` reports it in too. + match op(&lhs, &rhs) { + Some(result) => { + record_custom_reconciliation(&collector, name, &lhs, &rhs); + record_counter_reset_contradiction(&collector, name, &lhs, &rhs); + Some(result) + } + None => { + record_warning( + &collector, + format!( + "{name}: dropped native histogram sample with incompatible schemas" + ), + ); + None + } } - result } _ => None, }, @@ -3612,7 +3621,9 @@ mod tests { } #[test] - fn subtraction_records_reset_hint_contradictions_for_incompatible_histograms() { + fn subtraction_reports_a_reset_hint_contradiction_only_between_compatible_histograms() { + // Prometheus reports a pair whose bucket layouts cannot be combined before it looks at the + // reset hints, so the collision warning belongs to the pairs the operation can answer. for incompatible in [false, true] { let mut left = sample_histogram(2.0, 2.0, vec![2.0]); left.reset_hint = COUNTER_RESET_HINT; @@ -3633,10 +3644,23 @@ mod tests { native_histogram_arrow_type(), ); - assert!(collected_warnings(&collector).contains(&format!( - "{}: native histogram counter reset hints contradict", - NativeHistogramSub::name() - ))); + let warnings = collected_warnings(&collector); + let name = NativeHistogramSub::name(); + assert_eq!( + warnings.contains(&format!( + "{name}: native histogram counter reset hints contradict" + )), + !incompatible, + "{incompatible}: {warnings:?}" + ); + if incompatible { + assert!( + warnings.contains(&format!( + "{name}: dropped native histogram sample with incompatible schemas" + )), + "{warnings:?}" + ); + } } } diff --git a/src/promql/src/native/execute.rs b/src/promql/src/native/execute.rs index 4945dfa9254..6e79ece9ee8 100644 --- a/src/promql/src/native/execute.rs +++ b/src/promql/src/native/execute.rs @@ -1458,6 +1458,8 @@ struct BinaryRuntime { node_index: usize, /// Evidence shared with the other output partitions of this fused-series execution. evidence: Option>, + /// The query's collector for dropped histogram sample pairs, when requested. + annotations: Option, /// The kernel and the modifiers the compiler bound it with. kernel: PairKernel, /// The layout each operand is bound to, what a chunk of it is checked against: `0` is the left @@ -1539,6 +1541,7 @@ impl BinaryRuntime { Self { node_index, evidence, + annotations: None, kernel, schemas, operands: [RetainedInput::default(), RetainedInput::default()], @@ -1699,6 +1702,7 @@ impl BinaryRuntime { node: self.slot(0).0, evidence, }), + self.annotations.as_ref(), &mut reserve, ) } @@ -2983,7 +2987,25 @@ impl NodeState { } } +/// Stack one [`RuntimeNode::build`] or [`RuntimeNode::pull`] level needs below it before the +/// recursion grows its stack. +/// +/// Both walks recurse once per tree level, and a debug build's frame for one level of either is +/// large: measured on a 100-term chain, a `build` level used about 140 KiB and a `pull` level about +/// 80 KiB, which is what aborted the shared 8 MiB runtime worker on a tree the compiler admits. +/// There is no depth limit to keep that from happening, so the recursion grows its stack instead; +/// the reserve is several levels' worth rather than a bare guard page, because it has to hold the +/// level that is about to be entered, not only the one that asked. +const NATIVE_STACK_RED_ZONE: usize = 512 * 1024; + +/// Stack a growth adds when the reserve above is spent. It holds several levels of the same walk. +const NATIVE_STACK_GROWTH: usize = 4 * 1024 * 1024; + impl RuntimeNode { + /// Builds the runtime node for `node` and, recursively, for its inputs. + /// + /// A deep tree is executed rather than refused: there is no depth limit, so what has to fit is + /// the recursion, and it grows its stack on demand instead of relying on the caller's. fn build( node: &NativeNode, pool: &Arc, @@ -2991,6 +3013,26 @@ impl RuntimeNode { binary_before: &mut usize, location: String, duplicate_evidence: Option>, + ) -> Result<(Self, bool)> { + stacker::maybe_grow(NATIVE_STACK_RED_ZONE, NATIVE_STACK_GROWTH, || { + Self::build_within_stack( + node, + pool, + inputs, + binary_before, + location, + duplicate_evidence, + ) + }) + } + + fn build_within_stack( + node: &NativeNode, + pool: &Arc, + inputs: &mut Vec, + binary_before: &mut usize, + location: String, + duplicate_evidence: Option>, ) -> Result<(Self, bool)> { let metrics = Arc::new(NodeMetrics::default()); let (op, shared) = match &node.expr { @@ -3867,7 +3909,13 @@ impl RuntimeNode { RuntimeOp::VectorOf { input, .. } | RuntimeOp::Instant { input, .. } | RuntimeOp::Merge { input, .. } => input.set_annotations(collector), - RuntimeOp::Binary { left, right, .. } => { + RuntimeOp::Binary { + left, + right, + binary, + .. + } => { + binary.annotations = Some(collector.clone()); left.set_annotations(collector.clone()); right.set_annotations(collector); } @@ -3926,10 +3974,24 @@ impl RuntimeNode { /// `inputs_finished` is the caller's word that no more source chunks will arrive. A node that /// decides nothing until its whole input is there reads it to tell the last pull that found /// nothing -- "nothing more" -- from the ones before it, which meant "nothing yet". + /// + /// The pull recurses once per tree level, so a deep tree grows the stack it runs on rather than + /// being refused ([`NATIVE_STACK_RED_ZONE`]). The stack is the driver's, whichever thread polls + /// the result stream: a shared runtime worker has 8 MiB of it in a debug build. fn pull( &mut self, inputs: &mut [InputSlot], inputs_finished: bool, + ) -> Result> { + stacker::maybe_grow(NATIVE_STACK_RED_ZONE, NATIVE_STACK_GROWTH, || { + self.pull_within_stack(inputs, inputs_finished) + }) + } + + fn pull_within_stack( + &mut self, + inputs: &mut [InputSlot], + inputs_finished: bool, ) -> Result> { match &mut self.op { // A source hands over what the caller queued at it. The id was assigned when the tree diff --git a/src/promql/src/native/execute/tests.rs b/src/promql/src/native/execute/tests.rs index fdcf949976b..51da165a088 100644 --- a/src/promql/src/native/execute/tests.rs +++ b/src/promql/src/native/execute/tests.rs @@ -934,6 +934,98 @@ fn run(executor: &mut NativeExecutor) -> Vec { out } +/// A deep native tree executes after the real FusedSeries envelope is decoded. +#[test] +fn deep_native_tree_executes_after_fused_series_round_trip() { + // A representative deep codec/runtime test owns its worker stack; this is not an + // arbitrary-depth guarantee or a product-level stack setting. + std::thread::Builder::new() + .name("deep-native-codec".into()) + .stack_size(32 * 1024 * 1024) + .spawn(|| { + use crate::extension_plan::{FusedOutputFormat, FusedSeries, FusedSeriesBinding}; + use datafusion::common::DFSchema; + use datafusion::logical_expr::{EmptyRelation, LogicalPlan}; + + let grid = grid(0, 60_000, 3); + let schema = evaluated_schema(Arc::clone(&grid)); + let mut root = evaluated_input(Arc::clone(&grid)); + for _ in 0..80 { + root = scalar_binary( + root, + scalar(1.0, Arc::clone(&grid)), + binary_matching(BinaryOp::Add), + ); + } + root = map( + root, + vec![MathOp::Clamp { + min: 0.0, + max: 1000.0, + }], + ); + let tree = NativeTree::try_new(root).unwrap(); + let output_schema = FusedSeries::series_frame_schema(&tree).unwrap(); + let output_schema = + Arc::new(DFSchema::try_from(output_schema.as_ref().clone()).unwrap()); + let node = FusedSeries::try_new_with_format( + Arc::from("deep"), + tree, + FusedSeriesBinding { + grid: Arc::clone(&grid), + time_index: "ts".to_string(), + tags: Vec::new(), + metric_name: None, + }, + vec![LogicalPlan::EmptyRelation(EmptyRelation { + produce_one_row: false, + schema: Arc::new(DFSchema::empty()), + })], + output_schema, + FusedOutputFormat::SeriesFrame, + ) + .unwrap(); + let bytes = node.serialize().unwrap(); + let decoded = FusedSeries::deserialize(&bytes).unwrap(); + fn depth(node: &NativeNode) -> usize { + 1 + match &node.expr { + NativeExpr::Input(_) | NativeExpr::Scalar(_) => 0, + NativeExpr::Map { input, .. } => depth(input), + NativeExpr::Binary { left, right, .. } => depth(left).max(depth(right)), + other => panic!("unexpected node: {other:?}"), + } + } + assert!(depth(decoded.tree().root()) > 64); + let pool = pool(); + let mut execution = executor(decoded.tree(), Arc::clone(&pool)); + execution + .push_evaluated( + InputId::new(0), + evaluated_chunk_of( + &schema, + named_metric("x", "a"), + 0, + vec![Some(-1.0), None, Some(3.0)], + true, + ), + ) + .unwrap(); + let chunks = run(&mut execution); + assert_eq!(chunks.len(), 1); + assert_eq!( + sample_values(&chunks[0]), + vec![Some(79.0), None, Some(83.0)] + ); + assert_eq!(chunks[0].series.time.grid().unwrap().spec(), grid.spec()); + assert_eq!(chunks[0].series.metric.get("job"), Some("a")); + assert_eq!(chunks[0].series.metric.name(), None); + assert_eq!(pool.reserved(), 0); + }) + .unwrap() + .join() + .unwrap(); +} + /// A scalar root answers its own value, once, on the grid it was bound to. /// /// It reads nothing, so what it reports is not a series it was handed: one unlabelled chunk holding @@ -7208,6 +7300,633 @@ fn a_binary_adds_the_native_histograms_of_both_sides() { assert_eq!(histogram_counts(&out[0]), vec![8.0, 8.0]); } +/// The native vector matcher annotates only unsupported, present histogram pairs. +#[test] +fn binary_histogram_drop_info_respects_presence_and_duplicates() { + use common_query::promql_annotations::PromqlAnnotationCollector; + + let infos = |collector: &PromqlAnnotationCollector| { + let mut warnings = Vec::new(); + let mut infos = Vec::new(); + collector.append_to(&mut warnings, &mut infos); + assert!(warnings.is_empty()); + infos + }; + for (op, histogram_left, histogram_right, symbol) in [ + (BinaryOp::Add, true, false, "+"), + (BinaryOp::Add, false, true, "+"), + (BinaryOp::Mul, true, true, "*"), + ] { + let (tree, grid, left, right) = binary_tree(op, 2); + let schema = evaluated_schema(grid); + let collector = PromqlAnnotationCollector::default(); + let pool = pool(); + let mut state = NativeExecutionState::try_new(&tree, Arc::clone(&pool), None).unwrap(); + state.set_annotations(Some(collector.clone())); + let mut execution = NativeExecutor::new(state); + let chunk = |histogram, name| { + if histogram { + evaluated_histograms(&schema, named_metric(name, "a"), 0, 4, 2, true) + } else { + evaluated_chunk_of( + &schema, + named_metric(name, "a"), + 0, + vec![Some(2.0), Some(2.0)], + true, + ) + } + }; + execution + .push_evaluated(left, chunk(histogram_left, "left")) + .unwrap(); + execution + .push_evaluated(right, chunk(histogram_right, "right")) + .unwrap(); + assert!(run(&mut execution).is_empty()); + assert_eq!( + infos(&collector), + vec![format!( + "{symbol}: dropped native histogram samples because this binary operation is not supported for native histograms" + )] + ); + assert_eq!(pool.reserved(), 0); + } + + // No label match, or a missing sample at every shared point, cannot produce a drop Info. + for unmatched in [false, true] { + let (tree, grid, left, right) = binary_tree(BinaryOp::Add, 2); + let schema = evaluated_schema(grid); + let collector = PromqlAnnotationCollector::default(); + let mut state = NativeExecutionState::try_new(&tree, pool(), None).unwrap(); + state.set_annotations(Some(collector.clone())); + let mut execution = NativeExecutor::new(state); + execution + .push_evaluated( + left, + evaluated_histograms(&schema, named_metric("hist", "a"), 0, 4, 2, true), + ) + .unwrap(); + let values = if unmatched { + vec![Some(2.0); 2] + } else { + vec![None; 2] + }; + let job = if unmatched { "b" } else { "a" }; + execution + .push_evaluated( + right, + evaluated_chunk_of(&schema, named_metric("float", job), 0, values, true), + ) + .unwrap(); + assert!(run(&mut execution).is_empty()); + assert!(infos(&collector).is_empty()); + } + + // Even when the operation would drop the pair, duplicate one-side series retain their error. + let grid = grid(0, 60_000, 1); + let mut matching = binary_matching(BinaryOp::Add); + matching.labels = MatchingLabels::On(vec![Arc::from("job")]); + let tree = NativeTree::try_new(binary_of( + evaluated_input_with_source_index(Arc::clone(&grid), 0), + evaluated_input_with_source_index(Arc::clone(&grid), 1), + matching, + )) + .unwrap(); + let left = InputId::new(0); + let right = InputId::new(1); + let schema = evaluated_schema(grid); + let collector = PromqlAnnotationCollector::default(); + let pool = pool(); + let mut state = NativeExecutionState::try_new(&tree, Arc::clone(&pool), None).unwrap(); + state.set_annotations(Some(collector)); + let mut execution = NativeExecutor::new(state); + execution + .push_evaluated( + left, + evaluated_histograms(&schema, pod_metric("hist", "a", "h"), 0, 4, 1, true), + ) + .unwrap(); + for pod in ["one", "two"] { + execution + .push_evaluated( + right, + evaluated_chunk_of( + &schema, + pod_metric("float", "a", pod), + 0, + vec![Some(2.0)], + true, + ), + ) + .unwrap(); + } + let error = execution.finish().unwrap_err().to_string(); + assert!( + error.contains("duplicate series for the match group"), + "{error}" + ); + assert_eq!(pool.reserved(), 0); +} + +/// A filled pair is a pair the operation is run on, so the native executor annotates the +/// unsupported histogram pairing a fill value made. +/// +/// `hist{job="a"}` has no `job="b"` counterpart, so `hist + on(job) fill_right(0) float` answers +/// every point of it with `histogram + 0`, the pairing Prometheus runs `doBinOp` on after it fills +/// a step. The Info it records is the wording the legacy planner states the same rule with, and a +/// pair the fill never forms at all carries nothing. +#[test] +fn binary_histogram_fill_reports_the_dropped_info() { + use common_query::promql_annotations::PromqlAnnotationCollector; + + let dropped = |symbol: &str| { + vec![format!( + "{symbol}: dropped native histogram samples because this binary operation is not supported for native histograms" + )] + }; + // `fill_right` answers a missing right-hand sample, so the histogram is the left operand and + // every point of it is the pair `histogram + 0`; `fill_left` answers a missing left-hand one, + // so the histogram is the right operand and the pair is `0 + histogram`. + for (fill, histogram_left) in [ + ( + FillValues { + lhs: None, + rhs: Some(0.0), + }, + true, + ), + ( + FillValues { + lhs: Some(0.0), + rhs: None, + }, + false, + ), + ] { + let grid = grid(0, 60_000, 2); + let mut matching = binary_matching(BinaryOp::Add); + matching.labels = MatchingLabels::On(vec![Arc::from("job")]); + matching.fill = fill; + let tree = NativeTree::try_new(binary_of( + evaluated_input_with_source_index(Arc::clone(&grid), 0), + evaluated_input_with_source_index(Arc::clone(&grid), 1), + matching, + )) + .unwrap(); + let schema = evaluated_schema(grid); + // The two sides carry different `job` labels, so nothing matches and the fill value is what + // every point of the histogram series pairs with. + let histogram = || evaluated_histograms(&schema, named_metric("hist", "a"), 0, 4, 2, true); + let float = || { + evaluated_chunk_of( + &schema, + named_metric("float", "b"), + 0, + vec![Some(2.0), Some(2.0)], + true, + ) + }; + let (left, right) = if histogram_left { + (histogram(), float()) + } else { + (float(), histogram()) + }; + let collector = PromqlAnnotationCollector::default(); + let pool = pool(); + let mut state = NativeExecutionState::try_new(&tree, Arc::clone(&pool), None).unwrap(); + state.set_annotations(Some(collector.clone())); + let mut execution = NativeExecutor::new(state); + execution.push_evaluated(InputId::new(0), left).unwrap(); + execution.push_evaluated(InputId::new(1), right).unwrap(); + let chunks = run(&mut execution); + assert!( + chunks.is_empty(), + "a dropped fill pair filed a row: {:?}", + chunks + .iter() + .map(|chunk| chunk.series.metric.clone()) + .collect::>() + ); + let mut warnings = Vec::new(); + let mut infos = Vec::new(); + collector.append_to(&mut warnings, &mut infos); + assert!(warnings.is_empty(), "{warnings:?}"); + assert_eq!(infos, dropped("+"), "{fill:?}"); + assert_eq!(pool.reserved(), 0); + } + + // Without a fill modifier the two sides never pair, and a pair that was never formed produces + // no sample to drop. + let grid = grid(0, 60_000, 2); + let mut matching = binary_matching(BinaryOp::Add); + matching.labels = MatchingLabels::On(vec![Arc::from("job")]); + let tree = NativeTree::try_new(binary_of( + evaluated_input_with_source_index(Arc::clone(&grid), 0), + evaluated_input_with_source_index(Arc::clone(&grid), 1), + matching, + )) + .unwrap(); + let schema = evaluated_schema(grid); + let collector = PromqlAnnotationCollector::default(); + let pool = pool(); + let mut state = NativeExecutionState::try_new(&tree, Arc::clone(&pool), None).unwrap(); + state.set_annotations(Some(collector.clone())); + let mut execution = NativeExecutor::new(state); + execution + .push_evaluated( + InputId::new(0), + evaluated_histograms(&schema, named_metric("hist", "a"), 0, 4, 2, true), + ) + .unwrap(); + execution + .push_evaluated( + InputId::new(1), + evaluated_chunk_of( + &schema, + named_metric("float", "b"), + 0, + vec![Some(2.0), Some(2.0)], + true, + ), + ) + .unwrap(); + assert!(run(&mut execution).is_empty()); + let mut warnings = Vec::new(); + let mut infos = Vec::new(); + collector.append_to(&mut warnings, &mut infos); + assert!(warnings.is_empty(), "{warnings:?}"); + assert!(infos.is_empty(), "{infos:?}"); + assert_eq!(pool.reserved(), 0); +} + +/// Matched histogram arithmetic preserves the existing reset/schema diagnostics and values. +#[test] +fn binary_histogram_add_sub_preserves_payload_and_diagnostics() { + use common_query::native_histogram::CUSTOM_BUCKETS_SCHEMA; + use common_query::promql_annotations::PromqlAnnotationCollector; + + // A single positive bucket makes its post-reconciliation count unambiguous, including when + // all custom bounds differ and both source buckets fold into the result's +Inf bucket. + let hist = |custom: bool, bounds: &[f64], hint, count, sum| { + let offsets = [0]; + let lengths = [1]; + let buckets = [count]; + let mut builder = HistogramBatchBuilder::new(); + let index = builder + .push(HistogramView { + schema: if custom { CUSTOM_BUCKETS_SCHEMA } else { 0 }, + zero_threshold: 0.0, + sum, + count, + zero_count: 0.0, + reset_hint: hint, + start_timestamp: None, + custom_values: bounds, + positive: BucketSideView { + span_offsets: &offsets, + span_lengths: &lengths, + buckets: &buckets, + }, + negative: BucketSideView { + span_offsets: &[], + span_lengths: &[], + buckets: &[], + }, + }) + .unwrap(); + (builder.finish().unwrap(), index) + }; + for op in [BinaryOp::Add, BinaryOp::Sub] { + let name = if op == BinaryOp::Add { + "prom_native_histogram_add" + } else { + "prom_native_histogram_sub" + }; + let factor = if op == BinaryOp::Add { 1.0 } else { -1.0 }; + for reverse in [false, true] { + // Same exponential buckets; both orientations of the reset contradiction are tested. + let hints = if reverse { + ( + CounterResetHint::NotCounterReset, + CounterResetHint::CounterReset, + ) + } else { + ( + CounterResetHint::CounterReset, + CounterResetHint::NotCounterReset, + ) + }; + let left_hist = hist(false, &[], hints.0, 5.0, 10.0); + let right_hist = hist(false, &[], hints.1, 2.0, 4.0); + let (tree, grid, left, right) = binary_tree(op, 1); + let schema = evaluated_schema(grid); + let collector = PromqlAnnotationCollector::default(); + let pool = pool(); + let mut state = NativeExecutionState::try_new(&tree, Arc::clone(&pool), None).unwrap(); + state.set_annotations(Some(collector.clone())); + let mut execution = NativeExecutor::new(state); + for (input, (table, index)) in [(left, left_hist), (right, right_hist)] { + execution + .push_evaluated( + input, + evaluated_histogram_chunk_of( + &schema, + named_metric("hist", "a"), + 0, + vec![Some(index)], + &table, + true, + ), + ) + .unwrap(); + } + let chunks = run(&mut execution); + assert_eq!(chunks.len(), 1); + let mut cursor = chunks[0].series.fields.as_slice()[0].cursor(); + let SampleRef::Histogram(h) = cursor.at(0).unwrap() else { + panic!("expected histogram") + }; + assert_eq!( + (h.count, h.sum, h.zero_count), + (5.0 + factor * 2.0, 10.0 + factor * 4.0, 0.0) + ); + assert_eq!(h.positive.buckets, &[5.0 + factor * 2.0]); + assert_eq!( + h.reset_hint, + if op == BinaryOp::Add { + CounterResetHint::Unknown + } else { + CounterResetHint::Gauge + } + ); + assert_eq!(chunks[0].series.metric.get("job"), Some("a")); + assert_eq!(chunks[0].series.metric.name(), None); + let mut warnings = Vec::new(); + let mut infos = Vec::new(); + collector.append_to(&mut warnings, &mut infos); + assert_eq!( + warnings, + vec![format!( + "{name}: native histogram counter reset hints contradict" + )] + ); + assert!(infos.is_empty()); + assert_eq!(pool.reserved(), 0); + } + + // Differing custom bounds reconcile; exercise partially overlapping and disjoint bounds, + // with and without contradictory reset hints, so each existing diagnostic is independent. + for (left_bounds, right_bounds) in + [(&[1.0, 2.0][..], &[2.0, 3.0][..]), (&[1.0][..], &[2.0][..])] + { + for collision in [false, true] { + let hints = if collision { + ( + CounterResetHint::CounterReset, + CounterResetHint::NotCounterReset, + ) + } else { + (CounterResetHint::Unknown, CounterResetHint::Unknown) + }; + let left_hist = hist(true, left_bounds, hints.0, 5.0, 10.0); + let right_hist = hist(true, right_bounds, hints.1, 2.0, 4.0); + let (tree, grid, left, right) = binary_tree(op, 1); + let schema = evaluated_schema(grid); + let collector = PromqlAnnotationCollector::default(); + let pool = pool(); + let mut state = + NativeExecutionState::try_new(&tree, Arc::clone(&pool), None).unwrap(); + state.set_annotations(Some(collector.clone())); + let mut execution = NativeExecutor::new(state); + for (input, (table, index)) in [(left, left_hist), (right, right_hist)] { + execution + .push_evaluated( + input, + evaluated_histogram_chunk_of( + &schema, + named_metric("hist", "a"), + 0, + vec![Some(index)], + &table, + true, + ), + ) + .unwrap(); + } + let chunks = run(&mut execution); + assert_eq!(chunks.len(), 1); + let mut cursor = chunks[0].series.fields.as_slice()[0].cursor(); + let SampleRef::Histogram(h) = cursor.at(0).unwrap() else { + panic!("expected histogram") + }; + assert_eq!((h.count, h.sum), (5.0 + factor * 2.0, 10.0 + factor * 4.0)); + assert_eq!(h.positive.buckets, &[5.0 + factor * 2.0]); + assert_eq!( + h.custom_values, + if left_bounds.len() == 1 { + &[][..] + } else { + &[2.0][..] + } + ); + assert_eq!( + h.reset_hint, + if op == BinaryOp::Add { + CounterResetHint::Unknown + } else { + CounterResetHint::Gauge + } + ); + let mut warnings = Vec::new(); + let mut infos = Vec::new(); + collector.append_to(&mut warnings, &mut infos); + let expected_warnings = if collision { + vec![format!( + "{name}: native histogram counter reset hints contradict" + )] + } else { + vec![] + }; + assert_eq!(warnings, expected_warnings); + assert_eq!( + infos, + vec![format!( + "{name}: reconciled native histograms with different custom buckets" + )] + ); + assert_eq!(pool.reserved(), 0); + } + } + + // A conflicting pair is not a sample pair at an absent point or when labels do not match. + for unmatched in [false, true] { + let left_hist = hist(true, &[1.0], CounterResetHint::CounterReset, 5.0, 10.0); + let right_hist = hist(false, &[], CounterResetHint::NotCounterReset, 2.0, 4.0); + let (tree, grid, left, right) = binary_tree(op, 1); + let schema = evaluated_schema(grid); + let collector = PromqlAnnotationCollector::default(); + let pool = pool(); + let mut state = NativeExecutionState::try_new(&tree, Arc::clone(&pool), None).unwrap(); + state.set_annotations(Some(collector.clone())); + let mut execution = NativeExecutor::new(state); + execution + .push_evaluated( + left, + evaluated_histogram_chunk_of( + &schema, + named_metric("left", "a"), + 0, + vec![Some(left_hist.1)], + &left_hist.0, + true, + ), + ) + .unwrap(); + let right_index = if unmatched { Some(right_hist.1) } else { None }; + let right_job = if unmatched { "b" } else { "a" }; + execution + .push_evaluated( + right, + evaluated_histogram_chunk_of( + &schema, + named_metric("right", right_job), + 0, + vec![right_index], + &right_hist.0, + true, + ), + ) + .unwrap(); + assert!(run(&mut execution).is_empty()); + let mut warnings = Vec::new(); + let mut infos = Vec::new(); + collector.append_to(&mut warnings, &mut infos); + assert!(warnings.is_empty()); + assert!(infos.is_empty()); + assert_eq!(pool.reserved(), 0); + } + + // Incompatible custom/exponential pairs are dropped, in either direction. Prometheus + // reports the layouts before it reaches the reset hints, so a pair that fails both carries + // the schema warning alone. + for reverse in [false, true] { + for collision in [false, true] { + let hints = if collision { + ( + CounterResetHint::CounterReset, + CounterResetHint::NotCounterReset, + ) + } else { + (CounterResetHint::Unknown, CounterResetHint::Unknown) + }; + let left_hist = hist( + reverse, + if reverse { &[1.0] } else { &[] }, + hints.0, + 5.0, + 10.0, + ); + let right_hist = hist( + !reverse, + if reverse { &[] } else { &[1.0] }, + hints.1, + 2.0, + 4.0, + ); + let (tree, grid, left, right) = binary_tree(op, 1); + let schema = evaluated_schema(grid); + let collector = PromqlAnnotationCollector::default(); + let pool = pool(); + let mut state = + NativeExecutionState::try_new(&tree, Arc::clone(&pool), None).unwrap(); + state.set_annotations(Some(collector.clone())); + let mut execution = NativeExecutor::new(state); + for (input, (table, index)) in [(left, left_hist), (right, right_hist)] { + execution + .push_evaluated( + input, + evaluated_histogram_chunk_of( + &schema, + named_metric("hist", "a"), + 0, + vec![Some(index)], + &table, + true, + ), + ) + .unwrap(); + } + assert!(run(&mut execution).is_empty()); + let mut warnings = Vec::new(); + let mut infos = Vec::new(); + collector.append_to(&mut warnings, &mut infos); + // The hint collision changes nothing here: the layouts already decided the pair, + // and that one warning is all Prometheus reports for it. + assert_eq!( + warnings, + vec![format!( + "{name}: dropped native histogram sample with incompatible schemas" + )], + "collision {collision}" + ); + assert!(infos.is_empty()); + assert_eq!(pool.reserved(), 0); + } + } + } +} + +/// Supported histogram scaling keeps the values, labels and grid in both operand orders. +#[test] +fn binary_histogram_scaling_preserves_payload_and_labels() { + for (op, histogram_left, expected) in [ + (BinaryOp::Mul, true, 8.0), + (BinaryOp::Mul, false, 8.0), + (BinaryOp::Div, true, 2.0), + ] { + let (tree, grid, left, right) = binary_tree(op, 2); + let schema = evaluated_schema(Arc::clone(&grid)); + let pool = pool(); + let mut execution = executor(&tree, Arc::clone(&pool)); + let histogram = evaluated_histograms(&schema, named_metric("hist", "a"), 0, 4, 2, true); + let float = evaluated_chunk_of( + &schema, + named_metric("float", "a"), + 0, + vec![Some(2.0); 2], + true, + ); + let (lhs, rhs) = if histogram_left { + (histogram, float) + } else { + (float, histogram) + }; + execution.push_evaluated(left, lhs).unwrap(); + execution.push_evaluated(right, rhs).unwrap(); + let chunks = run(&mut execution); + assert_eq!(chunks.len(), 1); + assert_eq!(histogram_counts(&chunks[0]), vec![expected; 2]); + let mut cursor = chunks[0].series.fields.as_slice()[0].cursor(); + for row in 0..2 { + let SampleRef::Histogram(histogram) = cursor.at(row).unwrap() else { + panic!("scaled sample is not a histogram"); + }; + assert_eq!(histogram.sum, expected); + let bucket = if op == BinaryOp::Mul { 2.0 } else { 0.5 }; + assert_eq!(histogram.positive.buckets, &[bucket; 4]); + } + assert_eq!( + chunks[0].series.time.timestamps().unwrap().as_ref(), + &grid.timestamps().unwrap()[..] + ); + assert_eq!(chunks[0].series.metric.get("job"), Some("a")); + assert_eq!(chunks[0].series.metric.name(), None); + assert_eq!(chunks[0].series.time.grid().unwrap().spec(), grid.spec()); + assert_eq!(pool.reserved(), 0); + } +} + /// Nothing is decided before the input ends; `finish` is what hands the result over. #[test] fn a_binary_yields_nothing_until_the_input_finishes() { diff --git a/src/promql/src/native/plan.rs b/src/promql/src/native/plan.rs index 56b2668e558..4392c5950b6 100644 --- a/src/promql/src/native/plan.rs +++ b/src/promql/src/native/plan.rs @@ -27,36 +27,9 @@ //! constant, so `time()` and `scalar(v)` have no node. The legality below is therefore about the //! operations that exist, not about PromQL's whole surface. -use std::cell::Cell; use std::hash::Hash; use std::sync::Arc; -const MAX_NATIVE_NODE_DEPTH: usize = 64; -thread_local! { - static NATIVE_NODE_DEPTH: Cell = const { Cell::new(0) }; -} - -struct NativeNodeDepthGuard; -impl NativeNodeDepthGuard { - fn enter() -> std::result::Result { - NATIVE_NODE_DEPTH.with(|depth| { - let current = depth.get(); - if current >= MAX_NATIVE_NODE_DEPTH { - return Err(E::custom(format!( - "native tree nesting exceeds {MAX_NATIVE_NODE_DEPTH}" - ))); - } - depth.set(current + 1); - Ok(Self) - }) - } -} -impl Drop for NativeNodeDepthGuard { - fn drop(&mut self) { - NATIVE_NODE_DEPTH.with(|depth| depth.set(depth.get() - 1)); - } -} - use datafusion::arrow::datatypes::DataType; use datafusion::error::{DataFusionError, Result}; use datatypes::series::aggregate_state::{AggregateStateKind, StateComponent}; @@ -74,7 +47,9 @@ use crate::map_label::LabelRewrite; use crate::map_time::DateFunction; use crate::range_eval::{RangeFunction, RangeWindow}; use crate::select::SortOrder; -use crate::vector_match::{BinaryMatching, BinaryOp, Cardinality, MatchingLabels, SetOp}; +use crate::vector_match::{ + BinaryMatching, BinaryOp, Cardinality, FillValues, MatchingLabels, SetOp, +}; /// The bucket-bound label a classic histogram's series carry, which the histogram function folds and /// its result does not report. @@ -90,12 +65,14 @@ const PROMQL_METADATA_LABELS: [&str; 3] = ["__name__", "__type__", "__unit__"]; /// by `on` or `ignoring`; `and` and `unless` keep the left side's series and `or` adds the right /// side's; `info` adds the data labels of the info table; an operation with a scalar or a function /// of each value keeps its operand's; a scalar and `vector(s)` have none; `count_values` keeps its -/// grouping and the label it writes. `None` for a node whose labels this walk does not derive - a -/// label rewrite of the metric name, an `absent` naming a label no input column carries, and a -/// `count_values` whose destination is the metric name or no label at all, none of which is a column -/// here - so a tree holding one reports no label set here. `None` is not the query's answer: the -/// caller asks the existing planner for the columns the node is named after instead of declining the -/// query. +/// grouping and the label it writes; a fill that answers a missing many-side sample files the one +/// side's matching labels, so they join the set, while a grouped `include` label joins them only when +/// the one side carries it or the matching keeps it in the key a fill reads. `None` for a node whose +/// labels this walk does not derive - a label rewrite of the metric name, an `absent` naming a label +/// no input column carries, and a `count_values` whose destination is the metric name or no label at +/// all, none of which is a column here - so a tree holding one reports no label set here. `None` is +/// not the query's answer: the caller asks the existing planner for the columns the node is named +/// after instead of declining the query. pub fn fused_output_tags(node: &NativeNode, source_tags: &[Vec]) -> Option> { match &node.expr { NativeExpr::Input(input) => source_tags.get(input.source_index).cloned(), @@ -286,10 +263,57 @@ pub fn fused_output_tags(node: &NativeNode, source_tags: &[Vec]) -> Opti tags.push(tag.clone()); } } - // A filled left side takes an unmatched right series' matching labels, so those - // results carry the right side's tags. - if matching.fill.lhs.is_some() { - for tag in kept(fused_output_tags(right, source_tags)?) { + // A grouped result carries an include label only when a row can give it one. + // `result_metric` removes the label from the many side's labels unless the one side + // has it, so a paired row and a row filed from a one-side series take it from the + // one series, which is what the loop above declares. A row filed from a many-side + // series takes it from that series' match key instead, so it carries the label only + // when the matching keeps the label in the key. Everything else the many side + // carries is the base above, so a label neither of those can provide has to leave + // the set: nothing would ever fill its column. + if !includes.is_empty() { + let many_rows_filled = match &matching.cardinality { + // `group_right` puts the one side on the left, so the fills trade places: + // `fill_left` is the one that answers a missing one-side sample there. + Cardinality::OneToMany(_) => matching.fill.lhs, + Cardinality::ManyToOne(_) | Cardinality::OneToOne => matching.fill.rhs, + } + .is_some(); + let in_match_key = |label: &str| match &matching.labels { + MatchingLabels::On(names) => { + names.iter().any(|name| name.as_ref() == label) + } + // A match key is built from a series' tags, which never hold the metric name. + MatchingLabels::Ignoring(names) => { + label != "__name__" && !names.iter().any(|name| name.as_ref() == label) + } + }; + tags.retain(|tag| { + // The name is not an ordinary label here: this walk reports it by + // `__name__` in the set, which the nodes above decide. + tag == "__name__" + || !includes.iter().any(|label| label.as_ref() == tag.as_str()) + || include_tags.contains(tag) + || (many_rows_filled && in_match_key(tag)) + }); + } + // A fill answers a missing sample from the series of the other side, and the kernel + // files that row at the label set the series it has gives it + // (`vector_match::binary_field`): a many-side series keeps all of its labels, which + // `tags` already holds, while a one-side series keeps only its matching labels plus + // the group labels the modifier names, which the loop above adds. So what a fill can + // add here is the one side's matching labels, and only a fill that can file a + // one-side row has any to add: one where the many side is the missing side. Which + // fill that is follows the kernel, which trades the two for `group_right`, whose one + // side is the left - there it is `fill_right` that leaves the many side missing. + let (one_tags, fills_many_side) = match &matching.cardinality { + Cardinality::OneToMany(_) => (&left_tags, matching.fill.rhs), + Cardinality::ManyToOne(_) | Cardinality::OneToOne => { + (&right_tags, matching.fill.lhs) + } + }; + if fills_many_side.is_some() { + for tag in kept(one_tags.clone()) { if !tags.contains(&tag) { tags.push(tag); } @@ -421,6 +445,57 @@ pub fn uniform_field_kind<'a>( answer } +/// The output kind of a binary operation over two evaluated vectors, including fill pairs. +pub fn binary_output_field_kind( + left: FieldKind, + right: FieldKind, + op: BinaryOp, + return_bool: bool, + fill: FillValues, +) -> Option { + if return_bool { + return Some(FieldKind::Float); + } + match op { + BinaryOp::Add + | BinaryOp::Sub + | BinaryOp::Mul + | BinaryOp::Div + | BinaryOp::Mod + | BinaryOp::Pow + | BinaryOp::Atan2 => { + // An unsupported pair emits no sample, not a float. Fill values are floats, so one + // field can be produced by the real pair and by either enabled synthetic pair. + let payload_kind = |left, right| match (left, op, right) { + (FieldKind::Float, _, FieldKind::Float) => Some(FieldKind::Float), + (FieldKind::Histogram, BinaryOp::Add | BinaryOp::Sub, FieldKind::Histogram) + | (FieldKind::Histogram, BinaryOp::Mul | BinaryOp::Div, FieldKind::Float) + | (FieldKind::Float, BinaryOp::Mul, FieldKind::Histogram) => { + Some(FieldKind::Histogram) + } + _ => None, + }; + let candidates = [ + payload_kind(left, right), + fill.rhs.and_then(|_| payload_kind(left, FieldKind::Float)), + fill.lhs.and_then(|_| payload_kind(FieldKind::Float, right)), + ]; + let mut kind = None; + for candidate in candidates.into_iter().flatten() { + if kind.is_some_and(|existing| existing != candidate) { + return None; + } + kind = Some(candidate); + } + // With no possible samples, retain the legacy float layout for an empty result. + Some(kind.unwrap_or(FieldKind::Float)) + } + // Comparison and bool behavior is separate from arithmetic's empty-field layout. + _ if left == right => Some(left), + _ => None, + } +} + impl NativeNode { /// The kind this node's output field carries, given the kind its input carries. /// @@ -466,9 +541,9 @@ impl NativeNode { Some(kind) } // A constant beside a vector is broadcast over it, so the operation answers with the - // vector's own samples: a histogram times a float is still a histogram. Two vectors are - // only one column if both sides carry the same kind; a mixed result needs two columns - // per field, which this route does not declare. + // vector's own samples: a histogram times a float is still a histogram. Two vectors + // use the arithmetic result's kind, including any samples emitted by fill, and an + // empty float field only when no possible pairing can emit a sample. NativeExpr::Binary { left, right, @@ -485,7 +560,14 @@ impl NativeNode { (_, NativeExpr::Scalar(_)) => kind_of(left), (_, _) => { let left_kind = kind_of(left)?; - (left_kind == kind_of(right)?).then_some(left_kind) + let right_kind = kind_of(right)?; + binary_output_field_kind( + left_kind, + right_kind, + matching.op, + false, + matching.fill, + ) } } } @@ -1136,10 +1218,8 @@ pub struct NativeTree { impl NativeTree { /// Creates a tree over `root`, checking that every node's contract holds. /// - /// A structurally inconsistent tree is a compiler defect; a tree beyond the native depth - /// limit is unsupported by this route and can fall back to the existing planner. + /// A structurally inconsistent tree is a compiler defect. pub fn try_new(root: NativeNode) -> Result { - ensure_native_node_depth(&root)?; root.validate()?; Ok(Self { root }) } @@ -1154,7 +1234,6 @@ impl<'de> serde::Deserialize<'de> for NativeNode { fn deserialize>( deserializer: D, ) -> std::result::Result { - let _guard = NativeNodeDepthGuard::enter::()?; #[derive(serde::Deserialize)] struct Wire { expr: NativeExpr, @@ -1177,64 +1256,31 @@ impl<'de> serde::Deserialize<'de> for NativeTree { root: NativeNode, } let wire = Wire::deserialize(deserializer)?; - ensure_native_node_depth(&wire.root).map_err(serde::de::Error::custom)?; wire.root.validate().map_err(serde::de::Error::custom)?; Ok(Self { root: wire.root }) } } -fn ensure_native_node_depth(root: &NativeNode) -> Result<()> { - fn visit(node: &NativeNode, depth: usize) -> Result<()> { - if depth > MAX_NATIVE_NODE_DEPTH { - return Err(DataFusionError::NotImplemented(format!( - "native tree nesting exceeds {MAX_NATIVE_NODE_DEPTH}" - ))); - } - match &node.expr { - NativeExpr::VectorOf { input } - | NativeExpr::ScalarOf { input } - | NativeExpr::Instant { input, .. } - | NativeExpr::Merge { input } - | NativeExpr::Window { input, .. } - | NativeExpr::Map { input, .. } - | NativeExpr::Timestamp { input } - | NativeExpr::Aggregate { input, .. } - | NativeExpr::Vector { input, .. } => visit(input, depth + 1), - NativeExpr::ParameterizedTopK { - input, parameter, .. - } => { - visit(input, depth + 1)?; - visit(parameter, depth + 1) - } - NativeExpr::ParameterizedMap { - input, parameters, .. - } - | NativeExpr::ParameterizedHistogram { - input, parameters, .. - } => { - visit(input, depth + 1)?; - for parameter in parameters { - visit(parameter, depth + 1)?; - } - Ok(()) - } - NativeExpr::Binary { left, right, .. } | NativeExpr::Set { left, right, .. } => { - visit(left, depth + 1)?; - visit(right, depth + 1) - } - NativeExpr::Info { base, info, .. } => { - visit(base, depth + 1)?; - visit(info, depth + 1) - } - NativeExpr::Input(_) | NativeExpr::Scalar(_) | NativeExpr::EvalTime { .. } => Ok(()), - } - } - visit(root, 1) -} +/// Stack one [`NativeNode::validate`] level needs below it before the check grows its stack. +/// +/// The check recurses once per tree level, and a debug build's frame for one level is large: +/// measured on an 80-term chain, the whole check needed about 1.9 MiB, which overflows the 2 MiB +/// stack a test thread has. There is no depth limit on the trees this route compiles, so the check +/// grows its stack on demand rather than relying on the caller's. +const VALIDATE_STACK_RED_ZONE: usize = 256 * 1024; + +/// Stack a growth adds when the reserve above is spent. It holds several levels of the same check. +const VALIDATE_STACK_GROWTH: usize = 4 * 1024 * 1024; impl NativeNode { /// Checks this node and its inputs against the contract of its operation. pub fn validate(&self) -> Result<()> { + stacker::maybe_grow(VALIDATE_STACK_RED_ZONE, VALIDATE_STACK_GROWTH, || { + self.validate_within_stack() + }) + } + + fn validate_within_stack(&self) -> Result<()> { match &self.expr { NativeExpr::Input(input) => { // An input node's output is exactly the binding it was bound to: a scan hands over diff --git a/src/promql/src/native/plan/tests.rs b/src/promql/src/native/plan/tests.rs index a7f5ae49f19..18e6e71b773 100644 --- a/src/promql/src/native/plan/tests.rs +++ b/src/promql/src/native/plan/tests.rs @@ -14,7 +14,7 @@ use std::sync::Arc; -use datafusion::common::{DFSchema, DataFusionError}; +use datafusion::common::DFSchema; use datatypes::series::SeriesOrdering; use datatypes::series::aggregate_state::{AggregateStateKind, StateComponent}; use datatypes::series::samples::{FieldSpec, SeriesSchema}; @@ -293,6 +293,214 @@ fn parameterized_topk_validates_scalar_and_grid() { ); } +#[test] +fn binary_histogram_float_kind_matches_supported_operations() { + use crate::extension_plan::FieldKind; + + let histogram = input_on(grid(0, 60_000, 1)); + let float = NativeNode { + expr: NativeExpr::Timestamp { + input: Box::new(histogram.clone()), + }, + output: histogram.output.clone(), + }; + for (left, op, right, expected) in [ + ( + &histogram, + BinaryOp::Mul, + &float, + Some(FieldKind::Histogram), + ), + ( + &float, + BinaryOp::Mul, + &histogram, + Some(FieldKind::Histogram), + ), + ( + &histogram, + BinaryOp::Div, + &float, + Some(FieldKind::Histogram), + ), + (&float, BinaryOp::Div, &histogram, Some(FieldKind::Float)), + (&histogram, BinaryOp::Add, &float, Some(FieldKind::Float)), + (&histogram, BinaryOp::Sub, &float, Some(FieldKind::Float)), + ( + &histogram, + BinaryOp::Add, + &histogram, + Some(FieldKind::Histogram), + ), + ( + &histogram, + BinaryOp::Sub, + &histogram, + Some(FieldKind::Histogram), + ), + ( + &histogram, + BinaryOp::Mul, + &histogram, + Some(FieldKind::Float), + ), + ( + &histogram, + BinaryOp::Div, + &histogram, + Some(FieldKind::Float), + ), + (&float, BinaryOp::Add, &float, Some(FieldKind::Float)), + ] { + let node = NativeNode { + expr: NativeExpr::Binary { + left: Box::new(left.clone()), + right: Box::new(right.clone()), + matching: BinaryMatching { op, ..add() }, + }, + output: histogram.output.clone(), + }; + assert_eq!( + node.output_field_kind(FieldKind::Histogram), + expected, + "{op:?}" + ); + } +} + +/// Arithmetic kinds cover both histogram/float directions without declining empty results. +#[test] +fn binary_arithmetic_field_kind_matrix() { + use super::binary_output_field_kind; + use crate::extension_plan::FieldKind::{Float, Histogram}; + + for op in [ + BinaryOp::Add, + BinaryOp::Sub, + BinaryOp::Mul, + BinaryOp::Div, + BinaryOp::Mod, + BinaryOp::Pow, + BinaryOp::Atan2, + ] { + let histogram_pair = if matches!(op, BinaryOp::Add | BinaryOp::Sub) { + Histogram + } else { + Float + }; + let histogram_float = if matches!(op, BinaryOp::Mul | BinaryOp::Div) { + Histogram + } else { + Float + }; + let float_histogram = if op == BinaryOp::Mul { + Histogram + } else { + Float + }; + for (left, right, expected) in [ + (Float, Float, Float), + (Histogram, Histogram, histogram_pair), + (Histogram, Float, histogram_float), + (Float, Histogram, float_histogram), + ] { + assert_eq!( + binary_output_field_kind(left, right, op, false, FillValues::default()), + Some(expected), + "{left:?} {op:?} {right:?}" + ); + } + } +} + +/// Fill pairs contribute their real payload kind; unsupported pairs contribute no kind. +#[test] +fn binary_arithmetic_field_kind_accounts_for_fill() { + use super::binary_output_field_kind; + use crate::extension_plan::FieldKind::{Float, Histogram}; + + let absent = FillValues::default(); + let left = FillValues { + lhs: Some(2.0), + rhs: None, + }; + let right = FillValues { + lhs: None, + rhs: Some(2.0), + }; + let both = FillValues { + lhs: Some(2.0), + rhs: Some(2.0), + }; + for (op, fill, expected) in [ + (BinaryOp::Mul, absent, Float), + (BinaryOp::Mul, left, Histogram), + (BinaryOp::Mul, right, Histogram), + (BinaryOp::Mul, both, Histogram), + (BinaryOp::Div, absent, Float), + (BinaryOp::Div, left, Float), + (BinaryOp::Div, right, Histogram), + (BinaryOp::Div, both, Histogram), + (BinaryOp::Add, both, Histogram), + (BinaryOp::Sub, both, Histogram), + (BinaryOp::Mod, both, Float), + (BinaryOp::Pow, both, Float), + (BinaryOp::Atan2, both, Float), + ] { + assert_eq!( + binary_output_field_kind(Histogram, Histogram, op, false, fill), + Some(expected), + "{op:?} {fill:?}" + ); + // The tree-level walk must pass the same fill on to the shared kind helper. + let histogram = input_on(grid(0, 60_000, 1)); + let node = NativeNode { + expr: NativeExpr::Binary { + left: Box::new(histogram.clone()), + right: Box::new(histogram.clone()), + matching: BinaryMatching { op, fill, ..add() }, + }, + output: histogram.output.clone(), + }; + assert_eq!( + node.output_field_kind(Histogram), + Some(expected), + "{op:?} {fill:?}" + ); + } + + // The real mixed-kind operation and a fill may emit different kinds in one field. Neither + // silently claims homogeneity; mixed no-fill composition and comparisons keep their old kinds. + assert_eq!( + binary_output_field_kind(Histogram, Float, BinaryOp::Mul, false, left), + None + ); + assert_eq!( + binary_output_field_kind(Float, Histogram, BinaryOp::Mul, false, right), + None + ); + assert_eq!( + binary_output_field_kind(Histogram, Float, BinaryOp::Add, false, left), + Some(Float) + ); + assert_eq!( + binary_output_field_kind(Histogram, Float, BinaryOp::Mul, false, absent), + Some(Histogram) + ); + assert_eq!( + binary_output_field_kind(Histogram, Histogram, BinaryOp::Eq, false, both), + Some(Histogram) + ); + assert_eq!( + binary_output_field_kind(Histogram, Float, BinaryOp::Eq, false, both), + None + ); + assert_eq!( + binary_output_field_kind(Histogram, Float, BinaryOp::Eq, true, both), + Some(Float) + ); +} + /// A compiled tree is accepted when every node's contract holds. #[test] fn a_window_over_raw_samples_is_valid() { @@ -2044,9 +2252,7 @@ fn info_still_refuses_an_evaluated_value_vector() { } #[test] -fn native_tree_depth_limit_holds_across_lifecycle_on_named_worker_stack() { - use super::MAX_NATIVE_NODE_DEPTH; - +fn native_tree_deeper_than_64_round_trips_and_validates() { fn nested_maps(map_count: usize) -> NativeTree { let mut node = input_on(grid(0, 60_000, 1)); let output = node.output.clone(); @@ -2064,100 +2270,20 @@ fn native_tree_depth_limit_holds_across_lifecycle_on_named_worker_stack() { NativeTree { root: node } } - // A leaf counts as one node; test exactly the cap and rejection on a named 2 MiB worker. - std::thread::Builder::new() - .name("native-tree-depth-boundary".to_string()) - .stack_size(2 * 1024 * 1024) - .spawn(|| { - let at_limit = nested_maps(MAX_NATIVE_NODE_DEPTH - 1); - let checked = NativeTree::try_new(at_limit.root.clone()).unwrap(); - let bytes = postcard::to_stdvec(&checked).unwrap(); - let decoded: NativeTree = postcard::from_bytes(&bytes).unwrap(); - decoded.root().validate().unwrap(); - drop((decoded, bytes, checked, at_limit)); + // This tests a representative depth, not a guarantee about the available worker stack. + let deep = nested_maps(80); + let checked = NativeTree::try_new(deep.root.clone()).unwrap(); + let bytes = postcard::to_stdvec(&checked).unwrap(); + let decoded: NativeTree = postcard::from_bytes(&bytes).unwrap(); + assert_eq!(checked, decoded); + decoded.root().validate().unwrap(); - // Construct wire data beyond the cap without passing through try_new. - let over_limit = nested_maps(MAX_NATIVE_NODE_DEPTH); - let bytes = postcard::to_stdvec(&over_limit).unwrap(); - assert_eq!( - postcard::from_bytes::(&bytes).unwrap_err(), - postcard::Error::SerdeDeCustom - ); - assert!(matches!( - NativeTree::try_new(over_limit.root.clone()), - Err(DataFusionError::NotImplemented(_)) - )); - drop(over_limit); - }) - .unwrap() - .join() - .unwrap(); -} - -#[test] -fn native_tree_depth_limit_checks_dynamic_children() { - use super::{MAX_NATIVE_NODE_DEPTH, ParameterizedOp, ensure_native_node_depth}; - use crate::histogram_eval::HistogramFunction; - - let grid = grid(0, 60_000, 1); - let mut deep = input_on(Arc::clone(&grid)); - for _ in 0..MAX_NATIVE_NODE_DEPTH - 1 { - deep = map_over(deep); - } - let scalar_of = |input| NativeNode { - expr: NativeExpr::ScalarOf { - input: Box::new(input), - }, - output: NativeType::Scalar(Arc::clone(&grid)), - }; - let scalar = scalar_on(1.0, Arc::clone(&grid)); - let input = input_on(Arc::clone(&grid)); - let topk = |input: NativeNode, parameter| NativeNode { - output: input.output.clone(), - expr: NativeExpr::ParameterizedTopK { - input: Box::new(input), - op: BoundVectorOp::TopK { - k: 0.0, - top: true, - group: Grouping::By(Arc::from([])), - }, - parameter: Box::new(parameter), - }, - }; - let histogram = |input: NativeNode, parameters| NativeNode { - output: input.output.clone(), - expr: NativeExpr::ParameterizedHistogram { - input: Box::new(input), - op: BoundVectorOp::Histogram(HistogramFunction::Fraction { - lower: 0.0, - upper: 0.0, - }), - parameters, - }, - }; - let map = |input: NativeNode, parameters| NativeNode { - output: input.output.clone(), - expr: NativeExpr::ParameterizedMap { - input: Box::new(input), - op: ParameterizedOp::Clamp, - parameters, - }, - }; - assert!(ensure_native_node_depth(&deep).is_ok()); - for root in [ - scalar_of(deep.clone()), - topk(deep.clone(), scalar.clone()), - topk(input.clone(), scalar_of(deep.clone())), - histogram(deep.clone(), vec![scalar.clone(), scalar.clone()]), - histogram(input.clone(), vec![scalar.clone(), scalar_of(deep.clone())]), - map(deep.clone(), vec![scalar.clone(), scalar.clone()]), - map(input, vec![scalar, scalar_of(deep)]), - ] { - assert!(matches!( - NativeTree::try_new(root), - Err(DataFusionError::NotImplemented(_)) - )); - } + // Decoding must still validate the tree's structural contract. + let mut invalid = nested_maps(80); + invalid.root.output = NativeType::Scalar(grid(0, 60_000, 1)); + assert!(NativeTree::try_new(invalid.root.clone()).is_err()); + let bytes = postcard::to_stdvec(&invalid).unwrap(); + assert!(postcard::from_bytes::(&bytes).is_err()); } /// The index postcard writes for a [`NativeExpr`] variant is a wire contract, not an implementation diff --git a/src/promql/src/vector_match.rs b/src/promql/src/vector_match.rs index 20862270096..3325da5483f 100644 --- a/src/promql/src/vector_match.rs +++ b/src/promql/src/vector_match.rs @@ -24,6 +24,7 @@ use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet}; use std::sync::Arc; +use common_query::promql_annotations::PromqlAnnotationCollector; use datafusion::error::{DataFusionError, Result}; use datafusion::execution::memory_pool::{MemoryConsumer, MemoryPool, MemoryReservation}; use datatypes::series::histogram::{CounterResetHint, HistogramBatchBuilder, HistogramView}; @@ -33,6 +34,7 @@ use datatypes::series::samples::{ use datatypes::series::time::{EvalGrid, GridRunPair, GridSpan, TimeAxis}; use datatypes::series::{Label, SeriesLabels}; +use crate::functions::{NativeHistogramAdd, NativeHistogramSub}; use crate::grid_series::{GridSample, GridSeriesBuilder, METADATA_LABELS, external, owned}; use crate::histogram_eval::float_histogram::FloatHistogram; @@ -201,7 +203,7 @@ pub fn binary_vectors( rhs_schema: &SeriesSchema, rhs: &[Series], ) -> Result> { - binary_vectors_inner(matching, lhs_schema, lhs, rhs_schema, rhs, None, None) + binary_vectors_inner(matching, lhs_schema, lhs, rhs_schema, rhs, None, None, None) } /// [`binary_vectors`], handing the live workspace it holds to `reserve` as it allocates. @@ -225,6 +227,7 @@ struct FieldEvidence<'a> { evidence: &'a DuplicateEvidence, } +#[allow(clippy::too_many_arguments)] pub(crate) fn binary_vectors_with_reservation( matching: &BinaryMatching, lhs_schema: &SeriesSchema, @@ -232,6 +235,7 @@ pub(crate) fn binary_vectors_with_reservation( rhs_schema: &SeriesSchema, rhs: &[Series], evidence: Option>, + annotations: Option<&PromqlAnnotationCollector>, reserve: &mut dyn FnMut(usize) -> Result<()>, ) -> Result> { let mut budget = Budget::new(reserve); @@ -242,12 +246,14 @@ pub(crate) fn binary_vectors_with_reservation( rhs_schema, rhs, evidence, + annotations, Some(&mut budget), ) } /// The one binary kernel: one walk, one lookup and one error order, whether or not a caller /// accounts for the workspace. Every figure is reserved only when `budget` is held. +#[allow(clippy::too_many_arguments)] fn binary_vectors_inner( matching: &BinaryMatching, lhs_schema: &SeriesSchema, @@ -255,6 +261,7 @@ fn binary_vectors_inner( rhs_schema: &SeriesSchema, rhs: &[Series], evidence: Option>, + annotations: Option<&PromqlAnnotationCollector>, mut budget: Option<&mut Budget<'_>>, ) -> Result> { // The grid both operands are evaluated on, checked before anything is reserved for them. @@ -284,6 +291,7 @@ fn binary_vectors_inner( &rhs, &mut errors, field_evidence, + annotations, budget.as_deref_mut(), )?; // The field's flags, tables and sort are destroyed with its frame, so its phase ends here: @@ -734,6 +742,7 @@ fn histogram_phase_bytes(many: &Operand<'_>, one: &Operand<'_>) -> usize { /// The field's tables and the match set it builds are its own workspace: they replace what the field /// before held and are gone when it returns, which is why the caller admits that release after the /// call. The messages it appended and the rows it filed are what stays live. +#[allow(clippy::too_many_arguments)] fn binary_field( matching: &BinaryMatching, grid: &EvalGrid, @@ -741,6 +750,7 @@ fn binary_field( rhs: &Operand<'_>, errors: &mut Vec<(usize, u8, String)>, evidence: Option>, + annotations: Option<&PromqlAnnotationCollector>, mut budget: Option<&mut Budget<'_>>, ) -> Result { let (many, one, include, one_is_left) = match &matching.cardinality { @@ -845,6 +855,7 @@ fn binary_field( let mut pairs = Pairs { matching, one_is_left, + annotations, group_keys: Vec::new(), group_ids: HashMap::new(), // One-to-one: a match group may be matched once per point; Prometheus counts a filtered @@ -913,6 +924,7 @@ fn binary_field( point, many_value, one_value, + true, budget.as_deref_mut(), )?; } @@ -955,6 +967,7 @@ fn binary_field( point, sample, SampleRef::Float(value), + false, budget.as_deref_mut(), )?; } @@ -999,6 +1012,7 @@ fn binary_field( point, SampleRef::Float(value), sample, + false, budget.as_deref_mut(), )?; } @@ -1085,6 +1099,7 @@ fn binary_field( struct Pairs<'a> { matching: &'a BinaryMatching, one_is_left: bool, + annotations: Option<&'a PromqlAnnotationCollector>, group_keys: Vec, group_ids: HashMap, /// The match groups an operation matched at a point, kept where Prometheus consults them: @@ -1125,6 +1140,7 @@ impl Pairs<'_> { point: usize, many_value: SampleRef<'_>, one_value: SampleRef<'_>, + actual_pair: bool, mut budget: Option<&mut Budget<'_>>, ) -> Result> { if matches!(many_value, SampleRef::Missing) || matches!(one_value, SampleRef::Missing) { @@ -1135,7 +1151,27 @@ impl Pairs<'_> { } else { (many_value, one_value) }; - let Some((result, keep)) = elem_binop(self.matching.op, lhs_value, rhs_value)? else { + // Prometheus runs the operation on the float a fill value puts in place of a missing sample, + // so a filled pair reports the legacy dropped-sample Info too. What stays out is not the + // fill but the pair: one missing a side never reaches here, and a supported operation that + // declines for another reason -- incompatible bucket layouts -- is already outside what + // [`unsupported_histogram_arithmetic`] reports. + let dropped_op = if self.annotations.is_some() { + unsupported_histogram_arithmetic(self.matching.op, &lhs_value, &rhs_value) + } else { + None + }; + // A fill value is a float, so a filled pair never holds the two histograms `elem_binop` + // states its reconciliation and reset diagnostics for: those stay off for it. + let pair_annotations = if actual_pair { self.annotations } else { None }; + let Some((result, keep)) = + elem_binop(self.matching.op, lhs_value, rhs_value, pair_annotations)? + else { + if let (Some(collector), Some(op)) = (self.annotations, dropped_op) { + collector.record_info(format!( + "{op}: dropped native histogram samples because this binary operation is not supported for native histograms" + )); + } // The operator skipped the pair and nothing of it was filed. return Ok(slot); }; @@ -2156,14 +2192,45 @@ impl<'a> Operand<'a> { } } +/// The legacy Info applies only to unsupported histogram arithmetic, not to other reasons a +/// supported histogram operation can decline a pair (such as incompatible bucket layouts). +fn unsupported_histogram_arithmetic( + op: BinaryOp, + lhs: &SampleRef<'_>, + rhs: &SampleRef<'_>, +) -> Option<&'static str> { + let symbol = match op { + BinaryOp::Add => "+", + BinaryOp::Sub => "-", + BinaryOp::Mul => "*", + BinaryOp::Div => "/", + BinaryOp::Mod => "%", + BinaryOp::Pow => "^", + BinaryOp::Atan2 => "atan2", + _ => return None, + }; + let unsupported = match (lhs, rhs) { + (SampleRef::Histogram(_), SampleRef::Histogram(_)) => { + !matches!(op, BinaryOp::Add | BinaryOp::Sub) + } + (SampleRef::Histogram(_), SampleRef::Float(_)) => { + !matches!(op, BinaryOp::Mul | BinaryOp::Div) + } + (SampleRef::Float(_), SampleRef::Histogram(_)) => op != BinaryOp::Mul, + _ => false, + }; + unsupported.then_some(symbol) +} + /// Prometheus `vectorElemBinop` over present samples: the value and whether to keep it, or /// `None` when Prometheus reports the pair as an annotation and skips it without counting the /// match: an operator the sample types do not support, or an addition or subtraction of -/// histograms that mix exponential and custom buckets or whose counter reset hints collide. +/// histograms that mix exponential and custom buckets. fn elem_binop( op: BinaryOp, lhs: SampleRef<'_>, rhs: SampleRef<'_>, + annotations: Option<&PromqlAnnotationCollector>, ) -> Result> { let histogram = |view: &HistogramView<'_>| FloatHistogram::try_from_view(view); let mut result = match (lhs, rhs) { @@ -2192,16 +2259,50 @@ fn elem_binop( return Ok(Some((GridSample::Histogram(Box::new(lhs)), keep))); } BinaryOp::Add | BinaryOp::Sub => { + let name = if op == BinaryOp::Add { + NativeHistogramAdd::name() + } else { + NativeHistogramSub::name() + }; + // The layouts decide whether there is a pair at all: Prometheus reports a pair + // whose bucket layouts cannot be combined as that one error, before it looks at + // the reset hints, so a pair that fails both carries only the schema warning. + // Synthetic fill samples have no collector here. if lhs.uses_custom_buckets() != rhs.uses_custom_buckets() { + if let Some(collector) = annotations { + collector.record_warning(format!( + "{name}: dropped native histogram sample with incompatible schemas" + )); + } return Ok(None); } + if let Some(collector) = annotations + && matches!( + (lhs.reset_hint, rhs.reset_hint), + ( + CounterResetHint::CounterReset, + CounterResetHint::NotCounterReset + ) | ( + CounterResetHint::NotCounterReset, + CounterResetHint::CounterReset + ) + ) + { + collector.record_warning(format!( + "{name}: native histogram counter reset hints contradict" + )); + } let outcome = if op == BinaryOp::Add { lhs.add(&rhs)? } else { lhs.sub(&rhs)? }; - if outcome.counter_reset_collision { - return Ok(None); + if outcome.custom_bounds_reconciled + && let Some(collector) = annotations + { + collector.record_info(format!( + "{name}: reconciled native histograms with different custom buckets" + )); } if op == BinaryOp::Sub { lhs.reset_hint = CounterResetHint::Gauge; diff --git a/src/promql/src/vector_match/tests.rs b/src/promql/src/vector_match/tests.rs index 60337e4d363..428d93f4899 100644 --- a/src/promql/src/vector_match/tests.rs +++ b/src/promql/src/vector_match/tests.rs @@ -1154,11 +1154,9 @@ fn histogram_operators_match_prometheus() { ); } -/// Prometheus skips a histogram `+` or `-` whose counter reset hints collide, before counting -/// the match: the colliding pair gives nothing and does not make the other pair of its group a -/// second match. +/// Contradictory reset hints retain the value and therefore count as a successful match. #[test] -fn colliding_reset_hints_skip_the_pair() { +fn colliding_reset_hints_still_count_as_a_match() { let grid = Arc::new(EvalGrid::try_new(GridSpec::try_new(0, 10_000, 1).unwrap()).unwrap()); let schema = Arc::new(SeriesSchema { fields: vec![FieldSpec { @@ -1182,7 +1180,6 @@ fn colliding_reset_hints_skip_the_pair() { ) .unwrap() }; - // Hints: 0 unknown, 1 counter reset, 2 not a counter reset. let lhs = [ series(&[("__name__", "a"), ("instance", "1"), ("job", "x")], 1), series(&[("__name__", "a"), ("instance", "2"), ("job", "x")], 0), @@ -1196,8 +1193,13 @@ fn colliding_reset_hints_skip_the_pair() { cardinality: Cardinality::OneToOne, fill: FillValues::default(), }; - let result = binary_vectors(&matching, &schema, &lhs, &schema, &rhs).unwrap(); - assert_eq!(result.len(), 1, "{op:?}"); + let error = binary_vectors(&matching, &schema, &lhs, &schema, &rhs) + .unwrap_err() + .to_string(); + assert!( + error.contains("many-to-one matching must be explicit"), + "{op:?}: {error}" + ); } } @@ -1254,6 +1256,268 @@ fn a_failed_pair_leaves_its_sample_to_the_fill() { )); } +/// A pair a fill value made is a pair the operation is run on, so an unsupported histogram pairing +/// it makes reports the same dropped-samples Info a matched pair does. +/// +/// Prometheus fills a missing side and then runs `doBinOp` on the pair it formed, so a histogram +/// filled with a float meets the unsupported pairing `histogram + float` at every point and carries +/// the Info a matched pair would. What must not carry it is a fill that answers with a sample the +/// operation supports, and a point whose pair is never formed at all. +#[test] +fn a_filled_pair_reports_the_dropped_histogram_info() { + use common_query::promql_annotations::PromqlAnnotationCollector; + + let grid = Arc::new(EvalGrid::try_new(GridSpec::try_new(0, 10_000, 2).unwrap()).unwrap()); + let schema = Arc::new(schema(1, &grid)); + let labels = |name: &str, job: &str| -> Labels { + Labels::from([ + ("__name__".to_string(), name.to_string()), + ("job".to_string(), job.to_string()), + ]) + }; + // count 5, sum 5: one span of one bucket holding 5. + let text = "0/0/0/4014000000000000/4014000000000000/0/0_1/4014000000000000///"; + let histograms = |job: &str| { + mixed_series( + &schema, + &labels("hist", job), + &BTreeMap::new(), + &BTreeMap::from([(0, text.to_string()), (1, text.to_string())]), + ) + .unwrap() + }; + let floats = |job: &str| { + mixed_series( + &schema, + &labels("float", job), + &BTreeMap::from([(0, 2.0f64.to_bits()), (1, 2.0f64.to_bits())]), + &BTreeMap::new(), + ) + .unwrap() + }; + // The two series carry different `job` labels, so no point of either is matched; with a fill + // value every point of each is paired with the float the modifier names instead. + let diagnostics = |matching: &BinaryMatching, + lhs: &[Series], + rhs: &[Series]| + -> (usize, Vec, Vec) { + let collector = PromqlAnnotationCollector::default(); + let output = binary_vectors_with_reservation( + matching, + &schema, + lhs, + &schema, + rhs, + None, + Some(&collector), + &mut |_| Ok(()), + ) + .unwrap(); + let mut warnings = Vec::new(); + let mut infos = Vec::new(); + collector.append_to(&mut warnings, &mut infos); + (output.len(), warnings, infos) + }; + let matching = |op: BinaryOp, fill: FillValues| BinaryMatching { + op, + return_bool: false, + labels: MatchingLabels::On(vec![Arc::from("job")]), + cardinality: Cardinality::OneToOne, + fill, + }; + let dropped = |symbol: &str| { + vec![format!( + "{symbol}: dropped native histogram samples because this binary operation is not supported for native histograms" + )] + }; + // `fill_right` answers a missing right-hand sample, so the histogram is the left one and each + // point is the pair `histogram + 0`. + assert_eq!( + diagnostics( + &matching( + BinaryOp::Add, + FillValues { + lhs: None, + rhs: Some(0.0) + } + ), + &[histograms("x")], + &[floats("y")], + ), + (0, Vec::new(), dropped("+")), + ); + // `fill_left` answers a missing left-hand one, so the histogram is the right one and the pair + // is `0 + histogram`. + assert_eq!( + diagnostics( + &matching( + BinaryOp::Add, + FillValues { + lhs: Some(0.0), + rhs: None + } + ), + &[floats("y")], + &[histograms("x")], + ), + (0, Vec::new(), dropped("+")), + ); + // A fill the operation supports answers a sample: nothing was dropped, so nothing is reported. + assert_eq!( + diagnostics( + &matching( + BinaryOp::Mul, + FillValues { + lhs: None, + rhs: Some(2.0) + } + ), + &[histograms("x")], + &[floats("y")], + ), + (1, Vec::new(), Vec::new()), + ); + // With no fill value the two sides never pair, so no sample exists to be dropped. + assert_eq!( + diagnostics( + &matching(BinaryOp::Add, FillValues::default()), + &[histograms("x")], + &[floats("y")], + ), + (0, Vec::new(), Vec::new()), + ); +} + +/// A pair whose bucket layouts cannot be combined carries that one error, and no reset-collision +/// warning even when its hints contradict. +/// +/// Prometheus reports the incompatible layouts before it reaches the hints, so a pair that fails +/// both is reported once. Contradictory hints still earn their warning when the layouts can be +/// combined: the check order decides which diagnostic a pair that fails both carries, not whether +/// the collision is ever reported. +#[test] +fn an_incompatible_bucket_layout_drops_the_reset_collision_warning() { + use common_query::promql_annotations::PromqlAnnotationCollector; + use datatypes::series::histogram::CounterResetHint; + + let grid = Arc::new(EvalGrid::try_new(GridSpec::try_new(0, 10_000, 1).unwrap()).unwrap()); + let schema = Arc::new(schema(1, &grid)); + let labels = |job: &str| -> Labels { + Labels::from([ + ("__name__".to_string(), "hist".to_string()), + ("job".to_string(), job.to_string()), + ]) + }; + let operand = |job: &str, text: &str, hint: CounterResetHint| -> Series { + let histogram = OwnedHistogram::parse(text); + let mut read = FloatHistogram::try_from_view(&histogram.view()).unwrap(); + read.reset_hint = hint; + let mut builder = GridSeriesBuilder::try_new(schema.clone()).unwrap(); + builder + .push_samples(0, &[Some(GridSample::Histogram(Box::new(read)))]) + .unwrap(); + builder.finish(metric(&labels(job))).unwrap().unwrap() + }; + // count 5, sum 5: one span of one bucket holding 5, no custom bounds. + let exponential = "0/0/0/4014000000000000/4014000000000000/0/0_1/4014000000000000///"; + // count 2, sum 3: one span of one bucket holding 2, over the custom upper bounds given. + let custom = |bounds: &str| { + format!("-53/0/0/4000000000000000/4008000000000000/0/0_1/4000000000000000///{bounds}") + }; + // Both operands carry the same `job`, so they match on it and the operation is applied to the + // pair; what it reports is what is under test. + let diagnostics = |lhs: &Series, rhs: &Series| -> (usize, Vec, Vec) { + let matching = BinaryMatching { + op: BinaryOp::Add, + return_bool: false, + labels: MatchingLabels::On(vec![Arc::from("job")]), + cardinality: Cardinality::OneToOne, + fill: FillValues::default(), + }; + let collector = PromqlAnnotationCollector::default(); + let output = binary_vectors_with_reservation( + &matching, + &schema, + std::slice::from_ref(lhs), + &schema, + std::slice::from_ref(rhs), + None, + Some(&collector), + &mut |_| Ok(()), + ) + .unwrap(); + let mut warnings = Vec::new(); + let mut infos = Vec::new(); + collector.append_to(&mut warnings, &mut infos); + (output.len(), warnings, infos) + }; + let schema_warning = vec![ + "prom_native_histogram_add: dropped native histogram sample with incompatible schemas" + .to_string(), + ]; + let collision = vec![ + "prom_native_histogram_add: native histogram counter reset hints contradict".to_string(), + ]; + let reconciled = vec![ + "prom_native_histogram_add: reconciled native histograms with different custom buckets" + .to_string(), + ]; + + // One exponential layout and one custom one, in either order: the layouts cannot be combined, + // so the pair is answered with the schema warning and never reaches the hints. + let one_bound = custom("3ff0000000000000"); + for (lhs, rhs, hints) in [ + ( + exponential, + one_bound.as_str(), + ( + CounterResetHint::CounterReset, + CounterResetHint::NotCounterReset, + ), + ), + ( + one_bound.as_str(), + exponential, + ( + CounterResetHint::NotCounterReset, + CounterResetHint::CounterReset, + ), + ), + ] { + assert_eq!( + diagnostics(&operand("a", lhs, hints.0), &operand("a", rhs, hints.1)), + (0, schema_warning.clone(), Vec::new()), + "{lhs} + {rhs}", + ); + } + + // The same contradiction over one exponential layout, and over two custom ones the operation + // reconciles: there the collision is reported, and only the layouts that cannot be combined + // silence it. + assert_eq!( + diagnostics( + &operand("a", exponential, CounterResetHint::CounterReset), + &operand("a", exponential, CounterResetHint::NotCounterReset), + ), + (1, collision.clone(), Vec::new()), + ); + assert_eq!( + diagnostics( + &operand( + "a", + &custom("3ff0000000000000"), + CounterResetHint::CounterReset + ), + &operand( + "a", + &custom("3ff0000000000000,4000000000000000"), + CounterResetHint::NotCounterReset, + ), + ), + (1, collision, reconciled), + ); +} + /// A binary pairing walks two operands of many single-position runs and still reports every /// position exactly once. /// @@ -1630,6 +1894,7 @@ fn budgeted( rhs_schema, rhs, None, + None, &mut |total| { if total > limit { return Err(DataFusionError::ResourcesExhausted(format!( @@ -3238,6 +3503,7 @@ fn a_histogram_comparison_filter_keeps_the_payload_of_the_operand_it_keeps() { &schema, &rhs, None, + None, &mut |total| { reserved.push(total); Ok(()) @@ -3574,6 +3840,7 @@ fn binary_field_publishes_presence_and_records_excluded_duplicate() { evidence: &evidence, }), None, + None, ); assert!( result.is_ok(), diff --git a/src/query/src/promql/native_compile.rs b/src/query/src/promql/native_compile.rs index 92d40507f7e..96a2bebe394 100644 --- a/src/query/src/promql/native_compile.rs +++ b/src/query/src/promql/native_compile.rs @@ -653,21 +653,7 @@ fn binary_operation(context: &Context, binary: &BinaryExpr) -> Result Result Result { + let (Some(lhs), Some(rhs)) = (left.output.samples(), right.output.samples()) else { + return Err(DataFusionError::Internal(format!( + "the binary operation `{binary}` compiled a vector operand to something other than samples" + ))); + }; + let output = NativeType::Samples(binary_output_schema(lhs, rhs)?); + Ok(NativeNode { + expr: NativeExpr::Binary { + left: Box::new(left), + right: Box::new(right), + matching: vector_matching(binary, op)?, + }, + output, + }) +} + +/// Uses the ordinary binary binder for two evaluated frame inputs. +pub(crate) fn bind_evaluated_binary( + binary: &BinaryExpr, + left: NativeNode, + right: NativeNode, +) -> Result { + let op = binary_op(&binary.op).ok_or_else(|| { + not_implemented(&format!( + "the binary operator `{}` in `{binary}`", + binary.op + )) + })?; + NativeTree::try_new(bind_vector_binary(binary, left, right, op)?) +} + /// Compiles `and`, `or` or `unless` between two vectors, matched by the labels the query wrote. /// /// The parser already refuses a scalar operand, `bool` and `group_left` / `group_right` on a set diff --git a/src/query/src/promql/planner.rs b/src/query/src/promql/planner.rs index a23c5004616..199162c3a6a 100644 --- a/src/query/src/promql/planner.rs +++ b/src/query/src/promql/planner.rs @@ -1187,6 +1187,40 @@ impl PromPlanner { ); } + // The same whole native binary the statement root was offered can sit below a + // legacy-only wrapper. A nested retry is valid only on the statement's own grid. + if self.native_fused + && self.ctx.start == self.ctx.stmt_start + && self.ctx.end == self.ctx.stmt_end + { + let saved = self.ctx.clone(); + let native = self + .try_plan_native_fused(&PromExpr::Binary(binary_expr.clone()), query_engine_state) + .await; + self.ctx = saved; + if let Some(plan) = native? { + let mut fields = plan.schema().fields().iter(); + if let Some(time) = fields.next().map(|field| field.name().clone()) { + let values = fields + .clone() + .filter(|field| field.data_type() != &ArrowDataType::Utf8) + .map(|field| field.name().clone()) + .collect(); + let tags = fields + .filter(|field| field.data_type() == &ArrowDataType::Utf8) + .map(|field| field.name().clone()) + .collect(); + self.ctx.reset(); + self.ctx.reset_table_name_and_schema(); + self.ctx.aggregation_field_labels.clear(); + self.ctx.time_index_column = Some(time); + self.ctx.field_columns = values; + self.ctx.tag_columns = tags; + return Ok(plan); + } + } + } + if let Some(plan) = self.try_plan_binary_island(binary_expr).await? { return Ok(plan); } @@ -1377,32 +1411,14 @@ impl PromPlanner { } // both are columns. join them on time index (None, None) => { - let native_pair = - self.native_fused && native::hybrid_binary_admitted(*op, modifier, lhs, rhs); - let native = if native_pair { - match ( - self.try_plan_native_operand(lhs, query_engine_state) - .await?, - self.try_plan_native_operand(rhs, query_engine_state) - .await?, - ) { - (Some(left), Some(right)) => Some((left, right)), - _ => None, - } - } else { - None + // A declined binary uses the existing planner for its operands. + let (mut left_input, mut left_context, mut right_input, mut right_context) = { + let left = self.prom_expr_to_plan(lhs, query_engine_state).await?; + let left_ctx = self.ctx.clone(); + let right = self.prom_expr_to_plan(rhs, query_engine_state).await?; + let right_ctx = self.ctx.clone(); + (left, left_ctx, right, right_ctx) }; - let used_native = native.is_some(); - let (mut left_input, mut left_context, mut right_input, mut right_context) = - if let Some(((left, left_ctx), (right, right_ctx))) = native { - (left, left_ctx, right, right_ctx) - } else { - let left = self.prom_expr_to_plan(lhs, query_engine_state).await?; - let left_ctx = self.ctx.clone(); - let right = self.prom_expr_to_plan(rhs, query_engine_state).await?; - let right_ctx = self.ctx.clone(); - (left, left_ctx, right, right_ctx) - }; let left_field_columns = left_context.field_columns.clone(); let left_time_index_column = left_context.time_index_column.clone(); self.ctx = left_context.clone(); @@ -1419,21 +1435,13 @@ impl PromPlanner { // Both operands are planned first because only the planned contexts tell a tag // from a value field. The rewrite merely adds matchers to a selector, so the // table reference, time index and field columns captured above stay valid. - // Matcher propagation belongs to the legacy route: copying a matcher into the - // other operand's scan drops the groups that have no partner, which is exactly - // what lets the legacy join answer where Prometheus would report a duplicated - // match group. A native operand must not be rewritten this way - the native - // matcher checks the complete one-side groups itself, and filtering them first - // would hide that error. So the rewrite only runs when neither operand is native. - if !used_native - && let Some(rewritten) = matching_filters::propagate( - binary_expr, - &left_context.tag_columns, - &right_context.tag_columns, - &left_context.aggregation_field_labels, - &right_context.aggregation_field_labels, - ) - { + if let Some(rewritten) = matching_filters::propagate( + binary_expr, + &left_context.tag_columns, + &right_context.tag_columns, + &left_context.aggregation_field_labels, + &right_context.aggregation_field_labels, + ) { // A copied matcher belongs to the scan, not to the operand's identity: // `absent()` turns `selector_matcher` into the labels it reports, so the // re-planned context keeps the matchers the operand was written with. diff --git a/src/query/src/promql/planner/native.rs b/src/query/src/promql/planner/native.rs index 7b206fa2d24..b7da118ebd2 100644 --- a/src/query/src/promql/planner/native.rs +++ b/src/query/src/promql/planner/native.rs @@ -46,9 +46,7 @@ use datafusion::common::{DFSchema, DFSchemaRef}; use datafusion::error::DataFusionError; use datafusion::execution::memory_pool::UnboundedMemoryPool; use datafusion::logical_expr::UserDefinedLogicalNode; -use datafusion::logical_expr::{ - EmptyRelation, Extension, LogicalPlan, LogicalPlanBuilder, TableScanBuilder, Union, -}; +use datafusion::logical_expr::{EmptyRelation, Extension, LogicalPlan, TableScanBuilder, Union}; use datafusion::physical_plan::ExecutionPlan; use datafusion::prelude::Expr as DfExpr; use datafusion_expr::Expr; @@ -57,6 +55,7 @@ use datatypes::series::SeriesOrdering; use datatypes::series::frame::{FrameContent, FrameLabelMode, SeriesFrameDeclaration}; use datatypes::series::samples::{FieldSpec, SeriesSchema}; use datatypes::series::time::EvalGrid; +use futures::future::BoxFuture; use promql::extension_plan::{ FieldKind, FusedOutputFormat, FusedSeries, FusedSeriesBinding, FusedSeriesExec, InputLocality, RawSeriesSource, input_locality, matching_operands_drop_key, tree_source_fields, @@ -64,14 +63,14 @@ use promql::extension_plan::{ use promql::native::execute::NativeExecutionState; use promql::native::plan::{ BoundAggregate, BoundInput, BoundVectorOp, Grouping, InstantValue, MapOp, NativeExpr, - NativeNode, NativeTree, NativeType, fused_output_tags, reads_across_series, uniform_field_kind, + NativeNode, NativeTree, NativeType, binary_output_field_kind, fused_output_tags, + reads_across_series, uniform_field_kind, }; use promql::vector_match::{Cardinality, MatchingLabels, SetOp}; 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, - SubqueryExpr, VectorMatchCardinality, VectorSelector, token, + EvalStmt, Expr as PromExpr, MatrixSelector, ParenExpr, SubqueryExpr, VectorSelector, }; use session::context::{MetricDiscoveryScope, QueryContextRef}; use snafu::ResultExt; @@ -80,8 +79,8 @@ use store_api::region_engine::RawLabelSource; use crate::promql::error::{DataFusionPlanningSnafu, Result}; use crate::promql::native_compile::{ - InfoInput, compile_selectors_with_grid, compile_statement_with_grid, info_input, - statement_grid, subquery_child_grid, + InfoInput, bind_evaluated_binary, 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; @@ -186,90 +185,49 @@ type PreparedInput = ( bool, ); -pub(crate) fn hybrid_binary_admitted( - op: token::TokenType, - modifier: &Option, - lhs: &PromExpr, - rhs: &PromExpr, -) -> bool { - let supported_op = matches!( - op.id(), - token::T_ADD - | token::T_SUB - | token::T_MUL - | token::T_DIV - | token::T_MOD - | token::T_POW - | token::T_ATAN2 - ); - let supported_matching = match modifier { - None => true, - Some(modifier) => { - matches!(&modifier.card, VectorMatchCardinality::OneToOne) - && (matches!(&modifier.matching, Some(LabelModifier::Include(labels)) if !labels.labels.is_empty()) - || matches!(&modifier.matching, Some(LabelModifier::Exclude(labels)) if labels.labels.is_empty())) - } - }; - supported_op - && supported_matching - && lhs.value_type() == ValueType::Vector - && rhs.value_type() == ValueType::Vector - && !contains_aggregate_expression(lhs) - && !contains_aggregate_expression(rhs) - && single_selector_operand(lhs) - && single_selector_operand(rhs) +/// A fully placed native operand and the evaluated layout its frame carries. +struct NativeFramePlan { + plan: LogicalPlan, + output: NativeType, + tags: Vec, + kind: FieldKind, + time: String, + grid: Arc, +} + +enum NativePlanned { + Rows(LogicalPlan), + Frame(NativeFramePlan), +} + +fn native_planned( + plan: LogicalPlan, + metadata: Option<(NativeType, Vec)>, + kind: FieldKind, + time: String, + grid: Arc, +) -> NativePlanned { + match metadata { + Some((output, tags)) => NativePlanned::Frame(NativeFramePlan { + plan, + output, + tags, + kind, + time, + grid, + }), + None => NativePlanned::Rows(plan), + } } impl PromPlanner { - pub(super) async fn try_plan_native_operand( - &mut self, - expr: &PromExpr, - query_engine_state: &QueryEngineState, - ) -> Result> { - let saved = self.ctx.clone(); - let (plan, operand_ctx) = match self - .try_plan_native_fused_with_context(expr, query_engine_state) - .await? - { - Some((plan, Some(operand_ctx))) => (plan, operand_ctx), - _ => { - self.ctx = saved; - return Ok(None); - } - }; - // The scan context is only the input layout. Calls such as histogram_quantile can - // remove labels while producing the fused node, so publish only columns actually in - // that node's output schema to the binary matcher. - let mut output_ctx = operand_ctx; - let fields = plan.schema().fields(); - output_ctx.tag_columns = fields - .iter() - .filter(|field| field.data_type() == &ArrowDataType::Utf8) - .map(|field| field.name().clone()) - .collect(); - output_ctx - .field_columns - .retain(|field| fields.iter().any(|column| column.name() == field)); - output_ctx.use_tsid = false; - self.ctx = output_ctx.clone(); - let table_ref = self.table_ref(); - self.ctx = saved; - let table_ref = table_ref?; - let plan = LogicalPlanBuilder::from(plan) - .alias(table_ref) - .context(DataFusionPlanningSnafu)? - .build() - .context(DataFusionPlanningSnafu)?; - Ok(Some((plan, output_ctx))) - } - /// Builds the scan of the one logical table the current context is bound to. The caller has /// already preprocessed this selector's matchers and set the context to its table. #[allow(clippy::too_many_arguments)] async fn prepare_one_scan( &mut self, matchers: Matchers, - prom_expr: &PromExpr, + under_rate_or_increase: bool, logical_scan_id: &mut Option, logical_ids_differ: &mut bool, statement_kind: &mut Option, @@ -289,16 +247,13 @@ impl PromPlanner { if self.ctx.field_column_matcher.is_some() || fields.is_empty() { return Ok(None); } - // This is a deliberate semantic safety gate, not a native-function allowlist. On a - // delta-temporality table, samples are increments; the existing planner sums them - // for `increase` and divides by the range for `rate` per series, while the native - // compiled tree assumes cumulative samples. Keep any statement containing either - // operation on the existing path for these tables. The check covers the whole - // statement, not just this selector occurrence. + // Delta samples are increments: the existing planner sums them for `increase` and + // divides by the range for `rate`, while the native tree assumes cumulative samples. + // Decline only when this selector is read by one of those calls. if tags .iter() .any(|tag| tag == OTLP_AGGREGATION_TEMPORALITY_LABEL) - && calls_rate_or_increase(prom_expr) + && under_rate_or_increase { return Ok(None); } @@ -459,10 +414,229 @@ impl PromPlanner { prom_expr: &PromExpr, query_engine_state: &QueryEngineState, ) -> Result> { - Ok(self - .try_plan_native_fused_with_context(prom_expr, query_engine_state) + match self + .try_plan_native(prom_expr, query_engine_state, FusedOutputFormat::PlainRows) .await? - .map(|(plan, _)| plan)) + { + // A whole statement answers a row table. A frame is what a composition reads from its + // operands, so the statement root is never one. + Some(NativePlanned::Rows(plan)) => Ok(Some(plan)), + _ => Ok(None), + } + } + + /// The one native attempt at `prom_expr`, in the representation the caller needs from it. + /// + /// The whole expression is offered first: the compiler folds a range function and the math + /// functions over it into one node, which is the statement's answer whenever it can bind every + /// selector at once. What it cannot bind that way - two selectors carrying different field + /// kinds, or an operand that is itself a composition - is admitted as a composition of two + /// frames instead. + /// + /// Both attempts write the planner's selector state, so each one is handed the context it was + /// called with, and this restores it before returning whatever the attempt answered. That is + /// what lets a refused operand leave nothing behind for the next one, or for the caller that + /// keeps planning around this expression. + /// + /// `Box::pin` is here only because a composition and this attempt call each other, which an + /// anonymous async future cannot be sized for. It is not the termination guard: the recursion + /// below descends through a parsed expression's own operands. Both futures below are pinned for + /// that reason - a level of the descent keeps this block on the stack, so the block holds the + /// whole-expression attempt behind a pointer rather than by value. + fn try_plan_native<'a>( + &'a mut self, + prom_expr: &'a PromExpr, + query_engine_state: &'a QueryEngineState, + format: FusedOutputFormat, + ) -> BoxFuture<'a, Result>> { + Box::pin(async move { + let saved = self.ctx.clone(); + let whole = Box::pin(self.try_plan_native_fused_with_context( + prom_expr, + query_engine_state, + format, + )) + .await; + self.ctx = saved.clone(); + match whole { + Ok(Some(planned)) => return Ok(Some(planned)), + Ok(None) => {} + Err(error) => return Err(error), + } + let composed = self + .try_plan_native_composed_binary(prom_expr, query_engine_state, format) + .await; + self.ctx = saved; + composed + }) + } + + /// Combines two native operands without a legacy join. + /// + /// Each operand is planned the way the whole expression is - as its own tree where that tree + /// binds every one of its selectors, as a composition of its own operands where it does not - + /// and bound as one evaluated series frame. What admits the pair is the composition itself: the + /// two grids must agree, and the matching, its cardinality, its fill values and the output + /// column's kind must all be ones the kernels answer. A shape they refuse declines to the + /// existing plan rather than to an error. + /// + /// Only a binary expression composes, and only its own two operands are offered below: the + /// recursion descends the parsed expression rather than re-offering it, so it ends at a leaf. + /// A scalar, a raw sample and an aggregate state are not frames, so an operand that is one is + /// refused by the attempt that would have had to produce it. + async fn try_plan_native_composed_binary( + &mut self, + expr: &PromExpr, + query_engine_state: &QueryEngineState, + format: FusedOutputFormat, + ) -> Result> { + if !self.native_fused + || self.ctx.start != self.ctx.stmt_start + || self.ctx.end != self.ctx.stmt_end + { + return Ok(None); + } + // Parentheses are not a shape of their own: the operand inside them is the one that + // composes. + let mut inner = expr; + while let PromExpr::Paren(paren) = inner { + inner = &paren.expr; + } + let PromExpr::Binary(binary) = inner else { + return Ok(None); + }; + // Each attempt restores context even when a scan or compiler fails, and the second operand + // starts from the same context the first one did. + let saved = self.ctx.clone(); + let left = self + .try_plan_native( + &binary.lhs, + query_engine_state, + FusedOutputFormat::SeriesFrame, + ) + .await; + self.ctx = saved.clone(); + let Some(NativePlanned::Frame(left)) = left? else { + return Ok(None); + }; + let right = self + .try_plan_native( + &binary.rhs, + query_engine_state, + FusedOutputFormat::SeriesFrame, + ) + .await; + self.ctx = saved; + let Some(NativePlanned::Frame(right)) = right? else { + return Ok(None); + }; + if left.grid.spec() != right.grid.spec() { + return Ok(None); + } + let NativeFramePlan { + plan: left_plan, + output: left_output, + tags: left_tags, + kind: left_kind, + time: left_time, + grid: left_grid, + } = left; + let NativeFramePlan { + plan: right_plan, + output: right_output, + tags: right_tags, + kind: right_kind, + .. + } = right; + let source_tags = [left_tags, right_tags]; + let evaluated = |source_index, output: &NativeType| NativeNode { + expr: NativeExpr::Input(BoundInput { + source_index, + output: output.clone(), + ordering: SeriesOrdering::Contiguous, + }), + output: output.clone(), + }; + let tree = match bind_evaluated_binary( + binary, + evaluated(0, &left_output), + evaluated(1, &right_output), + ) { + Ok(tree) => tree, + Err(DataFusionError::NotImplemented(_)) => return Ok(None), + Err(error) => return Err(error).context(DataFusionPlanningSnafu), + }; + let NativeExpr::Binary { matching, .. } = &tree.root().expr else { + return Ok(None); + }; + let Some(root_kind) = binary_output_field_kind( + left_kind, + right_kind, + matching.op, + matching.return_bool, + matching.fill, + ) else { + return Ok(None); + }; + if NativeExecutionState::try_new(&tree, Arc::new(UnboundedMemoryPool::default()), None) + .is_err() + { + return Ok(None); + } + // What a consumer of this composition reads, and the layout the node publishes. The row + // layout names one column per value; the frame layout is the evaluated frame this tree's + // own root declares, which is what a frame consumer decodes. + let metadata = match format { + FusedOutputFormat::PlainRows => None, + FusedOutputFormat::SeriesFrame => { + let Some(tags) = native_output_tags(tree.root(), &source_tags) else { + return Ok(None); + }; + Some((tree.root().output.clone(), tags)) + } + }; + let output_schema = match format { + FusedOutputFormat::PlainRows => { + let Some(schema) = + Self::fused_root_schema(&tree, &source_tags, &left_time, root_kind) + else { + return Ok(None); + }; + schema + } + FusedOutputFormat::SeriesFrame => { + let frame = match FusedSeries::series_frame_schema(&tree) { + Ok(frame) => frame, + // A frame this composition cannot declare is a shape it does not answer. + Err(DataFusionError::Plan(_)) => return Ok(None), + Err(error) => return Err(error).context(DataFusionPlanningSnafu), + }; + Arc::new( + DFSchema::try_from(frame.as_ref().clone()).context(DataFusionPlanningSnafu)?, + ) + } + }; + let Ok(node) = FusedSeries::try_new_with_format( + Arc::from(expr.to_string()), + tree, + FusedSeriesBinding { + grid: Arc::clone(&left_grid), + time_index: left_time.clone(), + tags: Vec::new(), + metric_name: None, + }, + vec![left_plan, right_plan], + output_schema, + format, + ) else { + return Ok(None); + }; + let plan = LogicalPlan::Extension(Extension { + node: Arc::new(node), + }); + Ok(Some(native_planned( + plan, metadata, root_kind, left_time, left_grid, + ))) } /// Plans `prom_expr` as one native node, or returns `None` to keep the existing plan. @@ -485,7 +659,8 @@ impl PromPlanner { &mut self, prom_expr: &PromExpr, query_engine_state: &QueryEngineState, - ) -> Result)>> { + format: FusedOutputFormat, + ) -> Result> { if !self.native_fused { return Ok(None); } @@ -493,6 +668,10 @@ impl PromPlanner { // 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. + // + // A composition reads this admission as its own: a subquery root is compiled on its child + // grid, which is the grid its rows carry, so an operand whose grid is not the one a + // composition binds declines rather than being evaluated on the statement's grid. let subquery_root = root_subquery(prom_expr); if prom_expr.value_type() == ValueType::Matrix && (subquery_root.is_none() || self.ctx.start != self.ctx.end) @@ -526,10 +705,14 @@ impl PromPlanner { None => &stmt.expr, }; let mut selectors = Vec::new(); + // The walk starts outside any `rate` or `increase`, and records that per selector as it + // descends: a selector is delta-temporality-sensitive only where one of those calls reads + // it, which is what keeps `rate(a) / b` admissible when only `b` carries the marker. let mut set_operations = Vec::new(); if !collect_selectors_with_set_operations( &stmt, effective_expr, + false, &mut selectors, &mut set_operations, ) { @@ -554,6 +737,16 @@ impl PromPlanner { Err(DataFusionError::NotImplemented(_)) => return Ok(None), Err(error) => return Err(error).context(DataFusionPlanningSnafu), }; + // A frame operand is an evaluated sample root: a composition matches series, and a + // scalar root - `2`, `time()` - carries one value and no labels, so those keep the + // existing plan rather than becoming a frame the matcher cannot pair. + let evaluated = matches!( + &tree.root().output, + NativeType::Samples(schema) if schema.eval_grid.is_some() + ); + if format == FusedOutputFormat::SeriesFrame && !evaluated { + return Ok(None); + } // Bind only input tags: this data-free tree has no input, and its tags belong to its // output schema. let Some(output_tags) = native_output_tags(tree.root(), &[]) else { @@ -579,33 +772,56 @@ impl PromPlanner { } else { DEFAULT_TIME_INDEX_COLUMN.to_string() }; + // The metadata a consumer of a frame operand reads: the evaluated layout this tree's + // root declares on the statement grid, and the labels a `vector(s)` reports. + let metadata = match format { + FusedOutputFormat::PlainRows => None, + FusedOutputFormat::SeriesFrame => { + Some((tree.root().output.clone(), output_tags.clone())) + } + }; // A data-free tree reads no input, so every field it answers is a float. - let output_schema = - fused_output_schema(&values, &output_tags, &time_index, FieldKind::Float)?; - let node = FusedSeries::try_new( + let output_schema = match format { + FusedOutputFormat::PlainRows => { + fused_output_schema(&values, &output_tags, &time_index, FieldKind::Float)? + } + FusedOutputFormat::SeriesFrame => { + let frame = + FusedSeries::series_frame_schema(&tree).context(DataFusionPlanningSnafu)?; + Arc::new( + DFSchema::try_from(frame.as_ref().clone()) + .context(DataFusionPlanningSnafu)?, + ) + } + }; + let node = FusedSeries::try_new_with_format( Arc::from(prom_expr.to_string()), tree, FusedSeriesBinding { grid: Arc::clone(&grid), - time_index, + time_index: time_index.clone(), tags: Vec::new(), metric_name: None, }, Vec::new(), output_schema, + format, ) .context(DataFusionPlanningSnafu)?; - return Ok(Some(( - LogicalPlan::Extension(Extension { - node: Arc::new(node), - }), - None, + let plan = LogicalPlan::Extension(Extension { + node: Arc::new(node), + }); + return Ok(Some(native_planned( + plan, + metadata, + FieldKind::Float, + time_index, + grid, ))); } let original_context = self.ctx.clone(); let preparation = async { let mut prepared: Vec = Vec::new(); - let mut single_input_context = None; // The first metric logical-table id a selector's rewrite carried, and whether a later one // carried a different id. See the `cross_table` use below. let mut logical_scan_id: Option = None; @@ -613,7 +829,7 @@ impl PromPlanner { // The kind every bound field carries. A statement declares one column per field, so // selectors that disagree have no column to declare and keep the existing plan. let mut statement_kind: Option = None; - for selector in &selectors { + for (selector, under_rate_or_increase) in &selectors { self.ctx = original_context.clone(); // These matchers are alternatives the one scan below the node cannot represent, so the // query keeps the plan the existing path builds for it. @@ -744,7 +960,7 @@ impl PromPlanner { let Some((scan, schema, metric, time, mut tags, partitioned, split_series)) = self .prepare_one_scan( matchers.clone(), - prom_expr, + *under_rate_or_increase, &mut logical_scan_id, &mut logical_ids_differ, &mut statement_kind, @@ -835,13 +1051,10 @@ impl PromPlanner { if self.setup_context().await?.is_some() { return Ok(None); } - if selectors.len() == 1 { - single_input_context = Some(self.ctx.clone()); - } let Some(one) = self .prepare_one_scan( matchers, - prom_expr, + *under_rate_or_increase, &mut logical_scan_id, &mut logical_ids_differ, &mut statement_kind, @@ -852,18 +1065,11 @@ impl PromPlanner { }; prepared.push(one); } - Ok(Some(( - prepared, - single_input_context, - logical_ids_differ, - statement_kind, - ))) + Ok(Some((prepared, logical_ids_differ, statement_kind))) } .await; self.ctx = original_context; - let Some((mut prepared, single_input_context, logical_ids_differ, statement_kind)) = - preparation? - else { + let Some((mut prepared, logical_ids_differ, statement_kind)) = preparation? else { return Ok(None); }; // Selectors over one table share one region, so the node runs there over every scan. When @@ -931,7 +1137,7 @@ impl PromPlanner { // two walks agree: a mismatch is a compiler defect, not a query this route can plan. let identities: Vec<(Option, Matchers, i64, Option)> = selectors .iter() - .map(|selector| { + .map(|(selector, _)| { let offset_ms = selector .offset .as_ref() @@ -1255,6 +1461,20 @@ impl PromPlanner { } Some(FieldKind::Float) | None => FieldKind::Float, }; + let frame_metadata = if format == FusedOutputFormat::SeriesFrame { + let NativeType::Samples(schema) = &root.output else { + return Ok(None); + }; + if schema.eval_grid.is_none() { + return Ok(None); + } + let Some(output_tags) = native_output_tags(root, &source_tags) else { + return Ok(None); + }; + Some((root.output.clone(), output_tags)) + } else { + None + }; // A tree over more than one input needs no split: each input already answers from its own // source, and the one node above them folds every partition in one execution. The split is // an optimization for the one-input shapes it can carry, and so is the segmentation below; @@ -1306,7 +1526,7 @@ impl PromPlanner { // partition. if selectors.len() == 1 && matches!(&tree.root().expr, NativeExpr::Merge { input } if pointwise_operand(input) && canonical_local_prefix(input) && !contains_global_consumer(input)) - && let Some(plan) = canonical_boundary_plan( + && let Some((plan, _)) = canonical_boundary_stages( prom_expr, &tree, &children, @@ -1315,10 +1535,17 @@ impl PromPlanner { &time, &tags, &metric, + format, root_kind, ) { - return Ok(Some((plan, single_input_context))); + return Ok(Some(native_planned( + plan, + frame_metadata, + root_kind, + time, + grid, + ))); } if required_placement { // A tree whose fold can be split keeps that plan: it exports aggregate state rather @@ -1343,11 +1570,20 @@ impl PromPlanner { if root_kind != FieldKind::Float { return Ok(None); } - let Some(output_schema) = + // The row-value layout is what the split is validated against whichever format the + // caller asked for: it names one column per value the partial state accumulates, + // while the encoded frame carries those values inside its own layout. + let Some(value_schema) = Self::fused_root_schema(&tree, &source_tags, &time, root_kind) else { return Ok(None); }; + // A frame operand also needs the evaluated layout its consumers decode, which is + // the metadata derived above: a request that could not name one keeps the existing + // plan rather than publishing rows to a frame consumer. + if format == FusedOutputFormat::SeriesFrame && frame_metadata.is_none() { + return Ok(None); + } let Some(rawinput) = children.into_iter().next() else { return Ok(None); }; @@ -1363,12 +1599,19 @@ impl PromPlanner { metric_name: metric, }, rawinput, - output_schema, + value_schema, + format, )?; let plan = LogicalPlan::Extension(Extension { node: Arc::new(node), }); - return Ok(Some((plan, single_input_context))); + return Ok(Some(native_planned( + plan, + frame_metadata, + root_kind, + time, + grid, + ))); } } // A segment over the wire keeps the raw source as its input rather than a bare scan: the @@ -1387,7 +1630,7 @@ impl PromPlanner { &time, &tags, &segment_metric, - FusedOutputFormat::PlainRows, + format, contains_canonical_merge(root), root_kind, // A cut must keep the source whenever the table behind it can split a series @@ -1399,7 +1642,13 @@ impl PromPlanner { self_contained || any_split_series || (per_source_inputs && any_partitioned), )?; if let Some(plan) = plan { - return Ok(Some((plan, single_input_context))); + return Ok(Some(native_planned( + plan, + frame_metadata, + root_kind, + time, + grid, + ))); } // One input with no split or segment above it leaves a raw scan doing the work of a // cross-series consumer, and a single scan does not prove its region holds every @@ -1436,7 +1685,18 @@ impl PromPlanner { { return Ok(None); } - let output_schema = fused_output_schema(&root_fields, &output_tags, &time, root_kind)?; + let output_schema = match format { + FusedOutputFormat::PlainRows => { + fused_output_schema(&root_fields, &output_tags, &time, root_kind)? + } + FusedOutputFormat::SeriesFrame => { + let frame = + FusedSeries::series_frame_schema(&tree).context(DataFusionPlanningSnafu)?; + Arc::new( + DFSchema::try_from(frame.as_ref().clone()).context(DataFusionPlanningSnafu)?, + ) + } + }; // The children a node reads bind it: a `RawSeriesSource` child carries the frames and names // the metric it reads, so a node over them binds neither tags nor one metric name of its // own. A node whose children stay direct scans of one table reads that table through the @@ -1450,24 +1710,30 @@ impl PromPlanner { // 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( + let node = FusedSeries::try_new_with_format( Arc::from(prom_expr.to_string()), tree, FusedSeriesBinding { grid: publish_grid, - time_index: time, + // The node below names the same output column, so the binding takes a copy. + time_index: time.clone(), tags, metric_name: metric, }, children, output_schema, + format, ) .context(DataFusionPlanningSnafu)?; - Ok(Some(( - LogicalPlan::Extension(Extension { - node: Arc::new(node), - }), - single_input_context, + let plan = LogicalPlan::Extension(Extension { + node: Arc::new(node), + }); + Ok(Some(native_planned( + plan, + frame_metadata, + root_kind, + time, + grid, ))) } @@ -1508,10 +1774,8 @@ impl PromPlanner { /// The statement being planned, rebuilt from the evaluation numbers the planner holds. /// /// The compiler reads the statement's grid, lookback and `@` anchors rather than the planner's - /// mutable state. The route is only offered at the statement root, where `ctx.start` and - /// `ctx.end` are the statement's own bounds: they are rewritten while an anchored window or a - /// subquery is folded, so a nested expression would need the bounds it was actually entered - /// with, which is why no nested expression is offered here. + /// mutable state. A nested binary is offered only while `ctx.start` and `ctx.end` are still + /// the statement's bounds; anchored windows and subqueries can rewrite those bounds. /// /// `None` means the bounds are not representable as a [`SystemTime`], which is not a grid a /// tree can be evaluated on either. @@ -2040,33 +2304,6 @@ fn fused_publish_grid(root: &NativeNode, statement_grid: &Arc) -> Arc< /// per set, complementary points joined. /// /// `None` when the boundary cannot be built, so the caller keeps the plan it would have chosen. -#[allow(clippy::too_many_arguments)] -fn canonical_boundary_plan( - prom_expr: &PromExpr, - tree: &NativeTree, - children: &[LogicalPlan], - source_tags: &[Vec], - grid: &Arc, - time_index: &str, - tags: &[String], - metric: &Option>, - root_kind: FieldKind, -) -> Option { - let (plan, _) = canonical_boundary_stages( - prom_expr, - tree, - children, - source_tags, - grid, - time_index, - tags, - metric, - FusedOutputFormat::PlainRows, - root_kind, - )?; - Some(plan) -} - /// The stages of a canonical boundary over `tree`, and the columns its frames carry. /// /// `format` says what the caller above reads: a statement root reads the boundary's rows, and @@ -2771,15 +3008,22 @@ fn fused_output_schema( /// them the physical planner puts a `SeriesRepartitionExec`, which is what makes every row of a /// group reach one final partition. /// -/// The statement's output value columns must match the partial state's accumulator count; a -/// mismatch means the declared output shape does not match the state schema of the split tree. +/// `format` is the representation the caller needs from the second node: the statement's row table, +/// or the evaluated series frame another native node reads as an operand. The producer is +/// unaffected, because partial state is a frame either way. +/// +/// `value_schema` is the statement's row-value layout, and the output value columns it declares +/// must match the partial state's accumulator count; a mismatch means the declared output shape +/// does not match the state schema of the split tree. Only the row layout is counted: a frame +/// declares the values inside its own encoding, so it names no float column to count. fn plan_split_aggregate( promql: Arc, partial: NativeTree, final_tree: NativeTree, binding: FusedSeriesBinding, rawinput: LogicalPlan, - output_schema: DFSchemaRef, + value_schema: DFSchemaRef, + format: FusedOutputFormat, ) -> Result { let state_accumulators = match &partial.root().output { NativeType::AggregateState(state) => state.accumulators.len(), @@ -2790,7 +3034,7 @@ fn plan_split_aggregate( .context(DataFusionPlanningSnafu); } }; - let values = output_schema + let values = value_schema .fields() .iter() .filter(|field| field.data_type() == &ArrowDataType::Float64) @@ -2819,7 +3063,17 @@ fn plan_split_aggregate( vec![rawinput], ) .context(DataFusionPlanningSnafu)?; - FusedSeries::try_new( + let output_schema = match format { + FusedOutputFormat::PlainRows => value_schema, + // The final node's output is the frame its own evaluated root declares, so it is derived + // from the tree it runs rather than from the row layout. + FusedOutputFormat::SeriesFrame => { + let frame = + FusedSeries::series_frame_schema(&final_tree).context(DataFusionPlanningSnafu)?; + Arc::new(DFSchema::try_from(frame.as_ref().clone()).context(DataFusionPlanningSnafu)?) + } + }; + FusedSeries::try_new_with_format( promql, final_tree, binding, @@ -2827,6 +3081,7 @@ fn plan_split_aggregate( node: Arc::new(partial), })], output_schema, + format, ) .context(DataFusionPlanningSnafu) } @@ -3003,10 +3258,13 @@ fn without_metric_name(tags: Vec) -> Vec { /// They follow Prometheus' result labels: a one-to-one match keeps the left side's labels, narrowed /// by `on` or `ignoring`; `and` and `unless` keep the left side's series and `or` adds the right /// side's; `info` adds the data labels of the info table; an operation with a scalar or a function -/// of each value keeps its operand's; a scalar and `vector(s)` have none. `None` for a node whose -/// labels this walk does not derive - a whole-vector operation or a label rewrite, which renames the -/// labels below it - so a tree holding one reports no label set here. Such a tree is declined rather -/// than borrowing output names from the legacy planner. +/// of each value keeps its operand's; a scalar and `vector(s)` have none; a fill that answers a +/// missing many-side sample files the one side's matching labels, so they join the set, while a +/// grouped `include` label joins them only when the one side carries it or the matching keeps it in +/// the key a fill reads. `None` for a node whose labels this walk does not derive - a whole-vector +/// operation or a label rewrite, which renames the labels below it - so a tree holding one reports +/// no label set here. Such a tree is declined rather than borrowing output names from the legacy +/// planner. fn native_output_tags(node: &NativeNode, source_tags: &[Vec]) -> Option> { match &node.expr { NativeExpr::Aggregate { input, op } => { @@ -3208,10 +3466,61 @@ fn native_output_tags(node: &NativeNode, source_tags: &[Vec]) -> Option< tags.push(tag.clone()); } } - // A filled left side takes an unmatched right series' matching labels, so - // those results carry the right side's tags. - if matching.fill.lhs.is_some() { - for tag in kept(native_output_tags(right, source_tags)?) { + // A grouped result carries an include label only when a row can give it one. + // `result_metric` removes the label from the many side's labels unless the one + // side has it, so a paired row and a row filed from a one-side series take it + // from the one series, which is what the loop above declares. A row filed from + // a many-side series takes it from that series' match key instead, so it carries + // the label only when the matching keeps the label in the key. Everything else + // the many side carries is the base above, so a label neither of those can + // provide has to leave the set: nothing would ever fill its column. + if !includes.is_empty() { + let many_rows_filled = match &matching.cardinality { + // `group_right` puts the one side on the left, so the fills trade + // places: `fill_left` is the one that answers a missing one-side sample + // there. + Cardinality::OneToMany(_) => matching.fill.lhs, + Cardinality::ManyToOne(_) | Cardinality::OneToOne => matching.fill.rhs, + } + .is_some(); + let in_match_key = |label: &str| match &matching.labels { + MatchingLabels::On(names) => { + names.iter().any(|name| name.as_ref() == label) + } + // A match key is built from a series' tags, which never hold the metric + // name. + MatchingLabels::Ignoring(names) => { + label != METRIC_NAME + && !names.iter().any(|name| name.as_ref() == label) + } + }; + tags.retain(|tag| { + // The name is not an ordinary label here: `keeps_name` above decides + // whether the walk reports it. + tag == METRIC_NAME + || !includes.iter().any(|label| label.as_ref() == tag.as_str()) + || include_tags.contains(tag) + || (many_rows_filled && in_match_key(tag)) + }); + } + // A fill answers a missing sample from the series of the other side, and the + // kernel files that row at the label set the series it has gives it + // (`vector_match::binary_field`): a many-side series keeps all of its labels, + // which `tags` already holds, while a one-side series keeps only its matching + // labels plus the group labels the modifier names, which the loop above adds. + // So what a fill can add here is the one side's matching labels, and only a + // fill that can file a one-side row has any to add: one where the many side is + // the missing side. Which fill that is follows the kernel, which trades the two + // for `group_right`, whose one side is the left - there it is `fill_right` that + // leaves the many side missing. + let (one_tags, fills_many_side) = match &matching.cardinality { + Cardinality::OneToMany(_) => (left_tags.as_slice(), matching.fill.rhs), + Cardinality::ManyToOne(_) | Cardinality::OneToOne => { + (right_tags.as_slice(), matching.fill.lhs) + } + }; + if fills_many_side.is_some() { + for tag in kept(one_tags.to_vec()) { if !tags.contains(&tag) { tags.push(tag); } @@ -3430,58 +3739,6 @@ pub(crate) fn pointwise_operand(node: &NativeNode) -> bool { } } -/// Whether a hybrid operand contains an aggregate, which needs the split route rather than a join. -fn contains_aggregate_expression(expr: &PromExpr) -> bool { - match expr { - PromExpr::Aggregate(_) => true, - PromExpr::Paren(paren) => contains_aggregate_expression(&paren.expr), - PromExpr::Unary(unary) => contains_aggregate_expression(&unary.expr), - PromExpr::Binary(binary) => { - contains_aggregate_expression(&binary.lhs) || contains_aggregate_expression(&binary.rhs) - } - PromExpr::Call(call) => call - .args - .args - .iter() - .any(|arg| contains_aggregate_expression(arg)), - // A subquery evaluates its expression on its own grid, but a fold inside it still folds a - // group no single region owns, so an operand holding one needs the split route just as a - // direct aggregate does. [`single_selector_operand`] counts a subquery's selector as the - // operand's one selector, so this is what keeps a subquery around an aggregate out of the - // join. - PromExpr::Subquery(subquery) => contains_aggregate_expression(&subquery.expr), - _ => false, - } -} - -/// Whether this hybrid operand is a single-selector expression the native route can place. -fn single_selector_operand(expr: &PromExpr) -> bool { - let stmt = EvalStmt { - expr: expr.clone(), - start: UNIX_EPOCH, - end: UNIX_EPOCH, - interval: Duration::from_millis(1), - lookback_delta: Duration::from_millis(1), - }; - let mut selectors = Vec::new(); - collect_selectors(&stmt, expr, &mut selectors) && selectors.len() == 1 -} - -/// The selectors `expr` reads, in the order the compiler binds them. -/// -/// This is a structural walk, not a function capability list: calls are traversed and the compiler -/// decides whether their shapes have native kernels. `info` additionally reads a selector it does -/// not write; [`info_input`] derives that same selector here and the compiler binds it after the base. -fn collect_selectors<'a>( - stmt: &EvalStmt, - expr: &'a PromExpr, - out: &mut Vec>, -) -> bool { - // A caller that does not read the set operations still records them: the walk is the same one, - // and the list stays empty. - collect_selectors_with_set_operations(stmt, expr, out, &mut Vec::new()) -} - /// One side of a set operation, as the slice of the pre-order selector list it reads. /// /// The slice is the side's whole subtree, so it is also what says whether the side reads a table at @@ -3507,32 +3764,60 @@ struct SetOperation { fn collect_selectors_with_set_operations<'a>( stmt: &EvalStmt, expr: &'a PromExpr, - out: &mut Vec>, + under_rate_or_increase: bool, + out: &mut Vec<(Cow<'a, VectorSelector>, bool)>, set_operations: &mut Vec, ) -> bool { match expr { - PromExpr::VectorSelector(selector) => out.push(Cow::Borrowed(selector)), - PromExpr::MatrixSelector(MatrixSelector { vs, .. }) => out.push(Cow::Borrowed(vs)), + PromExpr::VectorSelector(selector) => { + out.push((Cow::Borrowed(selector), under_rate_or_increase)) + } + PromExpr::MatrixSelector(MatrixSelector { vs, .. }) => { + out.push((Cow::Borrowed(vs), under_rate_or_increase)) + } PromExpr::Paren(ParenExpr { expr }) => { - return collect_selectors_with_set_operations(stmt, expr, out, set_operations); + return collect_selectors_with_set_operations( + stmt, + expr, + under_rate_or_increase, + out, + set_operations, + ); } PromExpr::Call(call) if call.func.name == "info" => { let Ok(InfoInput { selector, .. }) = info_input(stmt, &call.args.args) else { return false; }; - if !collect_selectors_with_set_operations(stmt, &call.args.args[0], out, set_operations) - { + if !collect_selectors_with_set_operations( + stmt, + &call.args.args[0], + under_rate_or_increase, + out, + set_operations, + ) { return false; } - out.push(Cow::Owned(selector)); + out.push((Cow::Owned(selector), under_rate_or_increase)); } PromExpr::Binary(binary) => { let lhs_start = out.len(); - if !collect_selectors_with_set_operations(stmt, &binary.lhs, out, set_operations) { + if !collect_selectors_with_set_operations( + stmt, + &binary.lhs, + under_rate_or_increase, + out, + set_operations, + ) { return false; } let lhs_end = out.len(); - if !collect_selectors_with_set_operations(stmt, &binary.rhs, out, set_operations) { + if !collect_selectors_with_set_operations( + stmt, + &binary.rhs, + under_rate_or_increase, + out, + set_operations, + ) { return false; } if binary.op.is_set_operator() { @@ -3552,7 +3837,13 @@ fn collect_selectors_with_set_operations<'a>( // Literals contribute no selector; the compiler decides which scalar forms can be bound. PromExpr::NumberLiteral(_) => return true, PromExpr::Unary(unary) => { - return collect_selectors_with_set_operations(stmt, &unary.expr, out, set_operations); + return collect_selectors_with_set_operations( + stmt, + &unary.expr, + under_rate_or_increase, + out, + set_operations, + ); } // `histogram_quantile` and `histogram_fraction` take their vector last, and the compiler // compiles that vector before the arguments in front of it, so the walk reads it first: the @@ -3563,10 +3854,21 @@ fn collect_selectors_with_set_operations<'a>( let Some((vector, parameters)) = call.args.args.split_last() else { return false; }; - return collect_selectors_with_set_operations(stmt, vector, out, set_operations) - && parameters.iter().all(|parameter| { - collect_selectors_with_set_operations(stmt, parameter, out, set_operations) - }); + return collect_selectors_with_set_operations( + stmt, + vector, + under_rate_or_increase, + out, + set_operations, + ) && parameters.iter().all(|parameter| { + collect_selectors_with_set_operations( + stmt, + parameter, + under_rate_or_increase, + out, + set_operations, + ) + }); } PromExpr::Call(call) => { if matches!(call.func.name, "label_replace" | "label_join") { @@ -3580,12 +3882,19 @@ fn collect_selectors_with_set_operations<'a>( && collect_selectors_with_set_operations( stmt, &call.args.args[0], + under_rate_or_increase, out, set_operations, ); } for arg in &call.args.args { - if !collect_selectors_with_set_operations(stmt, arg, out, set_operations) { + if !collect_selectors_with_set_operations( + stmt, + arg, + under_rate_or_increase || matches!(call.func.name, "rate" | "increase"), + out, + set_operations, + ) { return false; } } @@ -3599,12 +3908,17 @@ fn collect_selectors_with_set_operations<'a>( return collect_selectors_with_set_operations( stmt, &aggregate.expr, + under_rate_or_increase, out, set_operations, ) && match &aggregate.param { - Some(param) => { - collect_selectors_with_set_operations(stmt, param, out, set_operations) - } + Some(param) => collect_selectors_with_set_operations( + stmt, + param, + under_rate_or_increase, + out, + set_operations, + ), None => true, }; } @@ -3616,6 +3930,7 @@ fn collect_selectors_with_set_operations<'a>( return collect_selectors_with_set_operations( stmt, &subquery.expr, + under_rate_or_increase, out, set_operations, ); @@ -3718,34 +4033,6 @@ fn metric_scan_and_table_id( Some((scan.clone(), filter.predicate.clone(), *table_id)) } -/// Whether `expr` contains `rate`/`increase`, used only by the delta-temporality semantic safety -/// gate, not as a native-function capability list. -fn calls_rate_or_increase(expr: &PromExpr) -> bool { - match expr { - PromExpr::Call(call) => { - matches!(call.func.name, "rate" | "increase") - || call.args.args.iter().any(|arg| calls_rate_or_increase(arg)) - } - PromExpr::Paren(ParenExpr { expr }) => calls_rate_or_increase(expr), - PromExpr::Subquery(subquery) => calls_rate_or_increase(&subquery.expr), - PromExpr::Binary(binary) => { - calls_rate_or_increase(&binary.lhs) || calls_rate_or_increase(&binary.rhs) - } - PromExpr::Unary(unary) => calls_rate_or_increase(&unary.expr), - // An aggregate's parameter is an expression, so `topk(scalar(sum(rate(x[5m]))), y)` reaches - // `rate` only through it. The gate is about the whole statement, and a delta-temporality - // table answers an increment there as it does anywhere else. - PromExpr::Aggregate(aggregate) => { - calls_rate_or_increase(&aggregate.expr) - || aggregate - .param - .as_ref() - .is_some_and(|param| calls_rate_or_increase(param)) - } - _ => false, - } -} - #[cfg(test)] mod tests { use std::sync::Arc; @@ -3760,29 +4047,12 @@ mod tests { use super::{ ArrowDataType, ArrowTimeUnit, FieldKind, METRIC_NAME, NATIVE_PROMQL_EXTENSION_KEY, - NativeType, aggregate_reads_histogram, calls_rate_or_increase, collect_selectors, - cut_segments, fused_output_schema, fused_output_tags, hybrid_binary_admitted, - independent_scan_pair, native_output_tags, native_promql_requested, - native_raw_plan_bounds_region_reads, node_children, plans_over_remote_regions, - scan_time_filter, + NativeType, aggregate_reads_histogram, collect_selectors_with_set_operations, cut_segments, + fused_output_schema, fused_output_tags, independent_scan_pair, native_output_tags, + native_promql_requested, native_raw_plan_bounds_region_reads, node_children, + plans_over_remote_regions, scan_time_filter, }; - #[test] - fn delta_temporality_guard_finds_rate_and_increase_inside_subqueries() { - for query in [ - "rate(x[5m])", - "increase(x[5m])", - "avg_over_time(rate(x[5m])[10m:1m])", - "avg_over_time(increase(x[5m])[10m:1m])", - ] { - let expr = promql_parser::parser::parse(query).unwrap(); - assert!(calls_rate_or_increase(&expr), "{query}"); - } - let query = "avg_over_time(abs(x)[10m:1m])"; - let expr = promql_parser::parser::parse(query).unwrap(); - assert!(!calls_rate_or_increase(&expr), "{query}"); - } - #[test] fn plans_over_remote_regions_matches_engine_construction() { use catalog::memory::new_memory_catalog_manager; @@ -3910,10 +4180,16 @@ mod tests { lookback_delta: Duration::from_secs(5), }; let mut selectors = Vec::new(); - assert!(collect_selectors(&statement, &expr, &mut selectors)); + assert!(collect_selectors_with_set_operations( + &statement, + &expr, + false, + &mut selectors, + &mut Vec::new() + )); selectors .iter() - .map(|selector| selector.name.clone().unwrap_or_default()) + .map(|(selector, _)| selector.name.clone().unwrap_or_default()) .collect() } @@ -4457,75 +4733,6 @@ mod tests { ); } - fn hybrid_admission(query: &str) -> bool { - let expression = promql_parser::parser::parse(query).unwrap(); - let promql_parser::parser::Expr::Binary(binary) = expression else { - panic!("expected binary expression: {query}"); - }; - hybrid_binary_admitted(binary.op, &binary.modifier, &binary.lhs, &binary.rhs) - } - - #[test] - fn hybrid_binary_admission_is_narrow() { - for query in ["A / B", "rate(A[1m]) / rate(B[1m])"] { - assert!(hybrid_admission(query), "{query}"); - } - assert!(hybrid_admission("A + on(job) B")); - for query in [ - "A and B", - "A + on() B", - "A + on(job) group_left B", - "100 / B", - "A / (B + C)", - // A fold on either side: one shared output label set cannot describe both operands, and - // the shapes that produced a `tag_0` planning error before the aggregate gate existed. - "sum(A) / B", - "A / sum(B)", - // The same fold one subquery down. The subquery's selector is the operand's one - // selector, so only the aggregate gate keeps this operand out of the join - without it - // the admitted operand fails at execution, where no planner can build the fused node - // over the gathered subquery. - "avg_over_time((sum by (job, host) (A))[5m:1m]) + on(job, host) B", - "A + on(job, host) avg_over_time((sum by (job, host) (B))[5m:1m])", - ] { - assert!(!hybrid_admission(query), "{query}"); - } - - for query in [ - "ceil(A) / B", - "irate(A[5m]) / B", - "sort(A) / B", - "absent(A) / B", - "-A / B", - ] { - assert!(hybrid_admission(query), "{query}"); - } - } - - /// An aggregate inside a subquery is still an aggregate, so a hybrid operand that holds one is - /// not the per-series evaluation the join needs. - /// - /// This is the admission decision behind a statement that used to fail while executing: both - /// shapes below were admitted, and the operand's subquery then had to be gathered for a join - /// that no installed planner can build. Refusing them keeps the plan the existing route builds. - #[test] - fn subquery_aggregate_operand_is_refused() { - for query in [ - "avg_over_time((sum by (job, host) (A))[5m:1m]) + on(job, host) B", - "A + on(job, host) avg_over_time((sum by (job, host) (B))[5m:1m])", - ] { - assert!(!hybrid_admission(query), "{query}"); - } - // The subquery on its own is admitted: what the gate refuses is the fold inside it, not the - // subquery that carries it. - for query in [ - "avg_over_time((A)[5m:1m]) + on(job, host) B", - "A + on(job, host) avg_over_time((B)[5m:1m])", - ] { - assert!(hybrid_admission(query), "{query}"); - } - } - #[test] fn millisecond_scan_bounds_preserve_open_closed_and_maximum_edges() { let inclusive = scan_time_filter(0, 10, true, "ts").to_string(); diff --git a/src/query/src/promql/planner/test.rs b/src/query/src/promql/planner/test.rs index 9e2c25ef094..ca9993c6f2e 100644 --- a/src/query/src/promql/planner/test.rs +++ b/src/query/src/promql/planner/test.rs @@ -48,7 +48,7 @@ use promql::extension_plan::{ FusedSeries, HistogramFold, InputLocality, RawSeriesSource, input_locality, }; use promql::native::plan::{AggregateStage, BoundVectorOp, NativeExpr, NativeNode, NativeType}; -use promql::vector_match::MatchingLabels; +use promql::vector_match::{Cardinality, MatchingLabels}; use promql_parser::label::Labels; use promql_parser::parser; use session::context::{MetricDiscoveryScope, QueryContext}; @@ -248,6 +248,29 @@ fn operator_metric_table_with_engine( le: Option<&str>, value: DirectOrValue, engine: &str, +) -> table::TableRef { + let tag_name = if engine == NATIVE_ENGINE { + "tag_0" + } else { + "tag" + }; + let mut tags = vec![(tag_name, tag)]; + if let Some(le) = le { + tags.push((LE_COLUMN_NAME, le)); + } + operator_metric_table_with_tags(name, table_id, &tags, value, engine) +} + +/// A metric table whose tag columns are the named ones, in order, one value each. +/// +/// A composed binary hands each operand's own tag set to the label walk, so a test of a match over +/// two operands whose label schemas differ needs two tables that carry different labels. +fn operator_metric_table_with_tags( + name: &str, + table_id: u32, + tags: &[(&str, &str)], + value: DirectOrValue, + engine: &str, ) -> table::TableRef { let value_type = match &value { DirectOrValue::Float64(_) => ConcreteDataType::float64_datatype(), @@ -255,24 +278,17 @@ fn operator_metric_table_with_engine( DirectOrValue::NativeHistogram(_) => native_histogram_value_type().clone(), DirectOrValue::Utf8(_) => ConcreteDataType::string_datatype(), }; - let tag_count = 1 + usize::from(le.is_some()); - let (tag_name, time_name, value_name) = if engine == NATIVE_ENGINE { - ("tag_0", "timestamp", "field_0") + let (time_name, value_name) = if engine == NATIVE_ENGINE { + ("timestamp", "field_0") } else { - ("tag", "ts", "v") + ("ts", "v") }; - let mut columns = vec![ColumnSchema::new( - tag_name.to_string(), - ConcreteDataType::string_datatype(), - false, - )]; - if le.is_some() { - columns.push(ColumnSchema::new( - LE_COLUMN_NAME.to_string(), - ConcreteDataType::string_datatype(), - false, - )); - } + let mut columns = tags + .iter() + .map(|(tag, _)| { + ColumnSchema::new(tag.to_string(), ConcreteDataType::string_datatype(), false) + }) + .collect::>(); columns.extend([ ColumnSchema::new( time_name.to_string(), @@ -283,10 +299,10 @@ fn operator_metric_table_with_engine( ColumnSchema::new(value_name.to_string(), value_type, true), ]); let schema = Arc::new(Schema::new(columns)); - let mut arrays = vec![Arc::new(StringArray::from(vec![tag])) as Arc]; - if let Some(le) = le { - arrays.push(Arc::new(StringArray::from(vec![le]))); - } + let mut arrays = tags + .iter() + .map(|(_, value)| Arc::new(StringArray::from(vec![*value])) as Arc) + .collect::>(); arrays.extend([ Arc::new(TimestampMillisecondArray::from(vec![1_000])) as Arc, value.array(), @@ -299,11 +315,11 @@ fn operator_metric_table_with_engine( DEFAULT_CATALOG_NAME.to_string(), DEFAULT_SCHEMA_NAME.to_string(), ); - let value_index = tag_count + 1; + let value_index = tags.len() + 1; let meta = TableMetaBuilder::empty() .engine(engine.to_string()) .schema(schema) - .primary_key_indices((0..tag_count).collect()) + .primary_key_indices((0..tags.len()).collect()) .value_indices(vec![value_index]) .next_column_id((value_index + 1) as u32) .build() @@ -1289,6 +1305,44 @@ async fn native_engine_temporality_marker_provider() -> DfTableSourceProvider { .is_ok() ); + // An unmarked counter in the same catalog exercises selector-specific temporality. + let unmarked_schema = Arc::new(Schema::new(vec![ + ColumnSchema::new("tag_0", ConcreteDataType::string_datatype(), false), + ColumnSchema::new( + greptime_timestamp().to_string(), + ConcreteDataType::timestamp_millisecond_datatype(), + false, + ) + .with_time_index(true), + ColumnSchema::new( + greptime_value().to_string(), + ConcreteDataType::float64_datatype(), + true, + ), + ])); + let unmarked_meta = TableMetaBuilder::empty() + .schema(unmarked_schema) + .engine(NATIVE_ENGINE.to_string()) + .primary_key_indices(vec![0]) + .value_indices(vec![2]) + .next_column_id(1024) + .build() + .unwrap(); + let unmarked_info = TableInfoBuilder::default() + .name("unmarked_metric") + .meta(unmarked_meta) + .build() + .unwrap(); + catalog_list + .register_table_sync(RegisterTableRequest { + catalog: DEFAULT_CATALOG_NAME.to_string(), + schema: DEFAULT_SCHEMA_NAME.to_string(), + table_name: "unmarked_metric".to_string(), + table_id: 1025, + table: EmptyTable::from_table_info(&unmarked_info), + }) + .unwrap(); + DfTableSourceProvider::new( catalog_list, false, @@ -2464,6 +2518,13 @@ fn find_metric_physical_scan(plan: &LogicalPlan) -> Option bool { + matches!( + plan, + LogicalPlan::Extension(Extension { node }) if node.as_any().is::() + ) +} + fn find_fused_series(plan: &LogicalPlan) -> Option<&FusedSeries> { if let LogicalPlan::Extension(Extension { node }) = plan && let Some(fused) = node.as_any().downcast_ref::() @@ -3533,6 +3594,30 @@ async fn native_fused_delta_temporality_rates_fall_back() { } } +/// A marker on an unrelated selector must not prevent a native binary. +#[tokio::test] +async fn native_fused_delta_temporality_is_selector_scoped() { + for query in [ + "rate(unmarked_metric[5m]) / on(tag_0) some_metric", + "rate(unmarked_metric[5m]) / on(tag_0) clamp(some_metric, 0, 1)", + ] { + let plan = build_native_fused_plan_with_provider( + query, + true, + native_engine_temporality_marker_provider().await, + ) + .await; + let display = plan.display_indent().to_string(); + let fused = find_fused_series(&plan).unwrap_or_else(|| panic!("{query}: {display}")); + assert_eq!(2, fused.children().len(), "{query}: {display}"); + assert!( + matches!(fused.tree().root().expr, NativeExpr::Binary { .. }), + "{query}: {display}" + ); + assert!(!display.contains("Join:"), "{query}: {display}"); + } +} + /// The marker alone does not decline a query: only `rate`/`increase` are refused. #[tokio::test] async fn native_fused_delta_temporality_non_counter_math_still_folds() { @@ -3569,51 +3654,893 @@ async fn native_fused_whole_vector_operations_are_admitted() { } #[tokio::test] -async fn native_fused_depth_limit_falls_back_to_legacy_plan() { - // At 63 selector terms the tree reaches depth 64; the 64th exceeds the limit. +async fn native_fused_deeper_than_64_is_not_a_legacy_join() { let schema = Arc::new(datatypes::series::samples::SeriesSchema { fields: Arc::from([datatypes::series::samples::FieldSpec { name: "v0".into() }]), eval_grid: None, }); - let compile = |terms| { - let query = std::iter::repeat_n("some_metric", terms) - .collect::>() - .join(" + "); - crate::promql::native_compile::compile_selectors( - &build_eval_stmt(&query), - &|_| Ok(Arc::clone(&schema)), - SeriesOrdering::KeyThenTime, - ) - }; - compile(63).expect("63 selector terms must fit native tree depth 64"); - let error = compile(64).expect_err("64 selector terms must exceed native tree depth 64"); - assert!( - matches!( - &error, - datafusion::error::DataFusionError::NotImplemented(_) - ), - "expected a NotImplemented depth-limit rejection, got: {error}" - ); - assert!( - error.to_string().contains("native tree nesting exceeds 64"), - "unexpected compiler rejection: {error}" - ); - let query = std::iter::repeat_n("some_metric", 64) - .collect::>() - .join(" + "); + // Scalar broadcasts stay distinct Binary nodes rather than folding into one math map. + let query = format!("clamp(some_metric{} , 0, 1000)", " + 1".repeat(80)); + let compiled = crate::promql::native_compile::compile_selectors( + &build_eval_stmt(&query), + &|_| Ok(Arc::clone(&schema)), + SeriesOrdering::KeyThenTime, + ) + .expect("a tree deeper than 64 compiles"); + fn depth(node: &NativeNode) -> usize { + 1 + match &node.expr { + NativeExpr::Input(_) | NativeExpr::Scalar(_) => 0, + NativeExpr::Map { input, .. } | NativeExpr::Instant { input, .. } => depth(input), + NativeExpr::Binary { left, right, .. } => depth(left).max(depth(right)), + other => panic!("unexpected node: {other:?}"), + } + } + assert!(depth(compiled.tree.root()) > 64); + let plan = build_native_fused_plan(&query, true).await; + let display = plan.display_indent().to_string(); + let fused = find_fused_series(&plan).expect("deep tree uses the native route"); + assert!(depth(fused.tree().root()) > 64, "{display}"); + assert!(!display.contains("Join:"), "{display}"); +} - let native = build_native_fused_plan(&query, true).await; - let legacy = build_native_fused_plan(&query, false).await; - let display = native.display_indent().to_string(); +async fn native_mixed_kind_provider() -> DfTableSourceProvider { + let catalog = MemoryCatalogManager::with_default_setup(); + for table in [ + operator_metric_table_with_engine( + "hist", + 10_101, + "a", + None, + DirectOrValue::NativeHistogram(direct_or_histogram()), + MITO_ENGINE, + ), + operator_metric_table_with_engine( + "other_hist", + 10_102, + "a", + None, + DirectOrValue::NativeHistogram(direct_or_histogram()), + MITO_ENGINE, + ), + operator_metric_table_with_engine( + "floats", + 10_103, + "a", + None, + DirectOrValue::Float64(2.0), + MITO_ENGINE, + ), + ] { + let info = table.table_info(); + catalog + .register_table_sync(RegisterTableRequest { + catalog: DEFAULT_CATALOG_NAME.to_string(), + schema: DEFAULT_SCHEMA_NAME.to_string(), + table_name: info.name.clone(), + table_id: info.ident.table_id, + table, + }) + .unwrap(); + } + DfTableSourceProvider::new( + catalog, + false, + QueryContext::arc(), + DummyDecoder::arc(), + false, + ) +} + +/// Two operands whose many side carries a label the one side does not: `hist_extra` has `extra`, +/// `floats_plain` only `host`. +async fn native_include_tag_provider() -> DfTableSourceProvider { + let catalog = MemoryCatalogManager::with_default_setup(); + for table in [ + operator_metric_table_with_tags( + "hist_extra", + 10_203, + &[("job", "a"), ("extra", "e")], + DirectOrValue::NativeHistogram(direct_or_histogram()), + MITO_ENGINE, + ), + operator_metric_table_with_tags( + "floats_plain", + 10_204, + &[("host", "h")], + DirectOrValue::Float64(2.0), + MITO_ENGINE, + ), + ] { + let info = table.table_info(); + catalog + .register_table_sync(RegisterTableRequest { + catalog: DEFAULT_CATALOG_NAME.to_string(), + schema: DEFAULT_SCHEMA_NAME.to_string(), + table_name: info.name.clone(), + table_id: info.ident.table_id, + table, + }) + .unwrap(); + } + DfTableSourceProvider::new( + catalog, + false, + QueryContext::arc(), + DummyDecoder::arc(), + false, + ) +} + +/// Two operands whose label schemas differ: `hist` carries `job`, `floats` only `host`. +async fn native_disjoint_tag_provider() -> DfTableSourceProvider { + let catalog = MemoryCatalogManager::with_default_setup(); + for table in [ + operator_metric_table_with_tags( + "hist", + 10_201, + &[("job", "a")], + DirectOrValue::NativeHistogram(direct_or_histogram()), + MITO_ENGINE, + ), + operator_metric_table_with_tags( + "floats", + 10_202, + &[("host", "h")], + DirectOrValue::Float64(2.0), + MITO_ENGINE, + ), + ] { + let info = table.table_info(); + catalog + .register_table_sync(RegisterTableRequest { + catalog: DEFAULT_CATALOG_NAME.to_string(), + schema: DEFAULT_SCHEMA_NAME.to_string(), + table_name: info.name.clone(), + table_id: info.ident.table_id, + table, + }) + .unwrap(); + } + DfTableSourceProvider::new( + catalog, + false, + QueryContext::arc(), + DummyDecoder::arc(), + false, + ) +} + +#[tokio::test] +async fn native_fused_composes_independent_histogram_binary_frames() { + for query in [ + "hist * on(tag_0) floats", + "floats * on(tag_0) hist", + "hist / on(tag_0) floats", + "hist - on(tag_0) other_hist", + "clamp(floats, 1, 2) * on(tag_0) hist", + ] { + let plan = + build_native_fused_plan_with_provider(query, true, native_mixed_kind_provider().await) + .await; + let display = plan.display_indent().to_string(); + let fused = find_fused_series(&plan).unwrap_or_else(|| panic!("{query}: {display}")); + let NativeExpr::Binary { left, right, .. } = &fused.tree().root().expr else { + panic!("expected Binary for {query}: {display}"); + }; + assert!( + matches!(left.expr, NativeExpr::Input(_)) && matches!(right.expr, NativeExpr::Input(_)), + "{display}" + ); + assert_eq!(2, fused.children().len(), "{display}"); + for child in fused.children() { + let frame = find_fused_series(child) + .unwrap_or_else(|| panic!("missing operand frame: {display}")); + assert_eq!( + promql::extension_plan::FusedOutputFormat::SeriesFrame, + frame.format(), + "{display}" + ); + } + assert!(!display.contains("Join:"), "{display}"); + assert_eq!( + vec!["field_0", "tag_0", "timestamp"], + sorted_column_names(&plan), + "{display}" + ); + } +} + +/// Unsupported histogram arithmetic still runs the native matcher and declares an empty float. +#[tokio::test] +async fn native_composed_invalid_histogram_arithmetic_declares_float() { + for query in [ + "clamp(floats, 1, 2) + on(tag_0) hist", + "hist + on(tag_0) clamp(floats, 1, 2)", + "hist * on(tag_0) other_hist", + ] { + let plan = + build_native_fused_plan_with_provider(query, true, native_mixed_kind_provider().await) + .await; + let display = plan.display_indent().to_string(); + let fused = find_fused_series(&plan).unwrap_or_else(|| panic!("{query}: {display}")); + let NativeExpr::Binary { left, right, .. } = &fused.tree().root().expr else { + panic!("expected Binary for {query}: {display}"); + }; + assert!( + matches!(left.expr, NativeExpr::Input(_)) && matches!(right.expr, NativeExpr::Input(_)), + "{display}" + ); + assert_eq!(2, fused.children().len(), "{display}"); + assert!(!display.contains("Join:"), "{display}"); + let fields = plan.schema().fields(); + let field = fields + .iter() + .find(|field| field.name() == "field_0") + .unwrap(); + assert_eq!(field.data_type(), &ArrowDataType::Float64, "{display}"); + assert_eq!( + vec!["field_0", "tag_0", "timestamp"], + sorted_column_names(&plan), + "{display}" + ); + } +} + +/// Fill modifiers are part of the composed binary's matching, decided by the output kind they imply. +#[tokio::test] +async fn native_composed_binary_admits_fill() { + for (query, fills_left, fills_right) in [ + // A filled right side is a float meeting a histogram here, and `histogram * float` answers a + // histogram: the same column carries the matched rows and the filled ones. + ("hist * on(tag_0) fill_right(0) floats", false, true), + // Both sides carry a histogram, so either fill keeps the column a histogram. + ("hist * on(tag_0) fill(0) other_hist", true, true), + ("hist * on(tag_0) fill_left(0) other_hist", true, false), + ] { + let plan = + build_native_fused_plan_with_provider(query, true, native_mixed_kind_provider().await) + .await; + let display = plan.display_indent().to_string(); + let fused = find_fused_series(&plan).unwrap_or_else(|| panic!("{query}: {display}")); + let NativeExpr::Binary { + left, + right, + matching, + } = &fused.tree().root().expr + else { + panic!("expected composed Binary for {query}: {display}"); + }; + assert!( + matches!(left.expr, NativeExpr::Input(_)) && matches!(right.expr, NativeExpr::Input(_)), + "{display}" + ); + assert_eq!( + (fills_left, fills_right), + (matching.fill.lhs.is_some(), matching.fill.rhs.is_some()), + "{query}: {display}" + ); + assert_eq!(2, fused.children().len(), "{display}"); + for child in fused.children() { + let frame = find_fused_series(child) + .unwrap_or_else(|| panic!("missing operand frame: {display}")); + assert_eq!( + promql::extension_plan::FusedOutputFormat::SeriesFrame, + frame.format(), + "{query}: {display}" + ); + } + assert!(!display.contains("Join:"), "{display}"); + let field = plan + .schema() + .fields() + .iter() + .find(|field| field.name() == "field_0") + .unwrap(); + assert_eq!( + field.data_type(), + &native_histogram_value_type().as_arrow_type(), + "{query}: {display}" + ); + } + + // A filled left side meets the right side alone, and `float * float` is a float: with a + // histogram on the right, one column would have to carry a histogram row and a float row. The + // kind walk refuses that shape, so the statement keeps the existing plan, which answers no fill + // at all - the two refusals are the type's, not a whitelist's. + for query in [ + "hist * on(tag_0) fill_left(0) floats", + "hist * on(tag_0) fill(0) floats", + ] { + let stmt = build_eval_stmt(query); + let error = PromPlanner::stmt_to_plan_with_annotations( + native_mixed_kind_provider().await, + &stmt, + &build_query_engine_state(), + None, + true, + ) + .await + .unwrap_err(); + assert!( + matches!( + &error, + crate::promql::error::Error::UnsupportedExpr { name, .. } + if name == "PromQL fill modifiers" + ), + "{query}: {error}" + ); + } + + // Whole-native fill is unaffected: one node over one table's two selectors. + let plan = build_native_fused_plan_with_provider( + "floats + on(tag_0) fill(0) floats", + true, + native_mixed_kind_provider().await, + ) + .await; + let display = plan.display_indent().to_string(); + let fused = find_fused_series(&plan).unwrap_or_else(|| panic!("{display}")); assert!( - !display.contains("FusedSeries"), - "64 selector terms exceed the native tree depth cap: {display}" + matches!(fused.tree().root().expr, NativeExpr::Binary { .. }), + "{display}" + ); + assert!(!display.contains("Join:"), "{display}"); +} + +/// A composed binary reports the labels a grouped match produces, not a one-to-one narrowing. +/// +/// The composed route hands `fused_root_schema` the two operands' own tag sets, in the order their +/// inputs are bound, and the walk over the tree derives the result labels from them. `group_left` +/// keeps the many side's labels as they are, `group_right` keeps the right side's, and each copies +/// the labels its modifier names from the one side; only a one-to-one match narrows them by `on` or +/// `ignoring`. The legacy plan is the reference for each: it projects the many side's label columns +/// (`hist.*` for `group_left`, `floats.*` for `group_right`) and no label column at all for the +/// one-to-one pair. +#[tokio::test] +async fn native_composed_binary_grouped_cardinality_keeps_many_side_labels() { + for (query, many_to_one, many_side) in [ + ("hist * ignoring(tag_0) group_left floats", true, "hist"), + ("hist * ignoring(tag_0) group_right floats", false, "floats"), + ] { + let plan = + build_native_fused_plan_with_provider(query, true, native_mixed_kind_provider().await) + .await; + let display = plan.display_indent().to_string(); + let fused = find_fused_series(&plan).unwrap_or_else(|| panic!("{query}: {display}")); + let NativeExpr::Binary { matching, .. } = &fused.tree().root().expr else { + panic!("expected composed Binary for {query}: {display}"); + }; + if many_to_one { + assert!( + matches!(matching.cardinality, Cardinality::ManyToOne(_)), + "{query}: {display}" + ); + } else { + assert!( + matches!(matching.cardinality, Cardinality::OneToMany(_)), + "{query}: {display}" + ); + } + assert_eq!(2, fused.children().len(), "{query}: {display}"); + // The many side keeps `tag_0` even though the pair ignores it: a grouping result holds one + // series per series of that side, so the label is not narrowed away. + assert_eq!( + vec!["field_0", "tag_0", "timestamp"], + sorted_column_names(&plan), + "{query}: {display}" + ); + assert!(!display.contains("Join:"), "{query}: {display}"); + + let legacy = PromPlanner::stmt_to_plan_with_annotations( + native_mixed_kind_provider().await, + &build_eval_stmt(query), + &build_query_engine_state(), + None, + false, + ) + .await + .unwrap(); + let legacy_display = legacy.display_indent().to_string(); + let legacy_columns = legacy + .schema() + .fields() + .iter() + .map(|field| field.name().clone()) + .collect::>(); + assert!( + legacy_columns.iter().any(|name| name == "tag_0"), + "{query}: the legacy plan reports the many side's label: {legacy_display}" + ); + assert!( + legacy_display.contains(&format!("{many_side}.tag_0")), + "{query}: and it is the many side's: {legacy_display}" + ); + } + + // The same pair without a grouping modifier is one-to-one: the labels both sides match on are + // the only ones the result keeps, so `tag_0` is narrowed away here - and only here. + let plan = build_native_fused_plan_with_provider( + "hist * ignoring(tag_0) floats", + true, + native_mixed_kind_provider().await, + ) + .await; + let display = plan.display_indent().to_string(); + let fused = find_fused_series(&plan).unwrap_or_else(|| panic!("{display}")); + let NativeExpr::Binary { matching, .. } = &fused.tree().root().expr else { + panic!("expected composed Binary: {display}"); + }; + assert!( + matches!(matching.cardinality, Cardinality::OneToOne), + "{display}" ); assert_eq!( - display, - legacy.display_indent().to_string(), - "a native depth-limit rejection must preserve the legacy plan" + vec!["field_0", "timestamp"], + sorted_column_names(&plan), + "{display}" ); + let legacy = PromPlanner::stmt_to_plan_with_annotations( + native_mixed_kind_provider().await, + &build_eval_stmt("hist * ignoring(tag_0) floats"), + &build_query_engine_state(), + None, + false, + ) + .await + .unwrap(); + let legacy_display = legacy.display_indent().to_string(); + assert!( + !legacy + .schema() + .fields() + .iter() + .any(|field| field.name() == "tag_0"), + "the legacy one-to-one plan narrows the label away too: {legacy_display}" + ); +} + +/// A filled step carries the one side's matching labels, `group_right` included. +/// +/// The kernel files a fill row at the label set of the series it has: the many side keeps all of +/// its labels, the one side only its matching ones plus the group labels the modifier names +/// (`vector_match::binary_field`, which trades the two fills under `group_right`, whose one side is +/// the left). `hist` carries `job` and `floats` only `host`, so +/// `hist * on(job) group_right fill_right(0) floats` answers a filled step labelled `job` although +/// the many side never carries it: the declared columns must name it. The rows themselves are +/// pinned by `native_promql_composed_grouped_and_multi_selector_operands`, which runs the same +/// match over a real instance - a composed frame cannot be executed over the in-memory scans here. +#[tokio::test] +async fn native_composed_binary_filled_one_side_carries_its_matching_labels() { + let plan = build_native_fused_plan_with_provider( + "hist * on(job) group_right fill_right(0) floats", + true, + native_disjoint_tag_provider().await, + ) + .await; + let display = plan.display_indent().to_string(); + let fused = find_fused_series(&plan).unwrap_or_else(|| panic!("{display}")); + let NativeExpr::Binary { matching, .. } = &fused.tree().root().expr else { + panic!("expected composed Binary: {display}"); + }; + // `group_right` puts the one side on the left, and the fill written on the right is the one + // that answers a missing many-side sample. + assert!( + matches!(matching.cardinality, Cardinality::OneToMany(_)), + "{display}" + ); + assert!( + matching.fill.lhs.is_none() && matching.fill.rhs.is_some(), + "{display}" + ); + assert!(!display.contains("Join:"), "{display}"); + // The many side's `host` comes with the pairing and the one side's `job` with the filled step. + assert_eq!( + vec!["field_0", "host", "job", "timestamp"], + sorted_column_names(&plan), + "{display}" + ); + // The parallel walk over the same tree, which the segmentation metadata is derived from, names + // the same columns from the two operands' own tags. + let operand_tags = fused + .children() + .iter() + .map(|child| { + let LogicalPlan::Extension(Extension { node }) = child else { + panic!("expected an operand frame: {child}") + }; + node.as_any() + .downcast_ref::() + .unwrap_or_else(|| panic!("expected an operand frame: {child}")) + .binding() + .tags + .clone() + }) + .collect::>(); + let mut walked = promql::native::plan::fused_output_tags(fused.tree().root(), &operand_tags) + .unwrap_or_else(|| panic!("{display}")); + walked.retain(|tag| tag != METRIC_NAME); + walked.sort(); + assert_eq!( + vec!["host".to_string(), "job".to_string()], + walked, + "{display}" + ); +} + +/// A grouped `include` label is declared only when a result row can carry it. +/// +/// `result_metric` removes an include label from the many side's labels unless the one side has it, +/// so a paired row and a row filed from a one-side series take it from the one series. A row filed +/// from a many-side series takes it from that series' match key, which holds it only when the +/// matching keeps it there. `extra` is on the many side alone and `on(job)` keeps it out of the key, +/// so no row carries it and the declared columns must not name it; `ignoring(host)` leaves it in the +/// key, so a fill synthesizes it from there and it stays. +#[tokio::test] +async fn native_composed_binary_declares_only_include_labels_a_row_can_carry() { + use datatypes::series::samples::{FieldSpec, Samples, Series, SeriesFields}; + use datatypes::series::time::{EvalGrid, GridRange, GridSpec, TimeAxis}; + use datatypes::series::{Label, SeriesLabels}; + + // The grid the operands the kernel is asked to match are laid on. + let grid = Arc::new(EvalGrid::try_new(GridSpec::try_new(0, 1_000, 4).unwrap()).unwrap()); + let schema = Arc::new(datatypes::series::samples::SeriesSchema { + fields: vec![FieldSpec { + name: "field_0".into(), + }] + .into(), + eval_grid: Some(Arc::clone(&grid)), + }); + // A dense one-field operand of the label set `labels`. + let operand = |labels: &[(&str, &str)]| { + let metric = SeriesLabels::try_new( + None, + labels + .iter() + .map(|(name, value)| Label { + name: (*name).into(), + value: (*value).into(), + }) + .collect(), + ) + .unwrap(); + Series::try_new( + &schema, + metric, + TimeAxis::Grid(GridRange::try_new(Arc::clone(&grid), 0, 4).unwrap()), + SeriesFields::one(Samples::from_float(Float64Array::from(vec![Some(1.0); 4]))), + ) + .unwrap() + }; + let mut filed = 0; + for (query, left, right, declared, carried) in [ + // `group_left(extra)` makes the left side the many one, and `extra` never reaches a row: the + // one side does not carry it and `on(job)` keeps it out of the key a fill reads. + ( + "hist_extra * on(job) group_left(extra) floats_plain", + &[("job", "a"), ("extra", "e")][..], + &[("host", "h")][..], + &["job"][..], + &[][..], + ), + ( + "hist_extra * on(job) group_left(extra) fill_right(0) floats_plain", + &[("job", "a"), ("extra", "e")][..], + &[("host", "h")][..], + &["job"][..], + &["job"][..], + ), + // The matching keeps `extra` in the key, so the fill that files many-side rows synthesizes + // it from there and the label stays declared. + ( + "hist_extra * ignoring(host) group_left(extra) fill_right(0) floats_plain", + &[("job", "a"), ("extra", "e")][..], + &[("host", "h")][..], + &["extra", "job"][..], + &["extra", "job"][..], + ), + // Mirrored: `group_right(extra)` makes the right side the many one. + ( + "floats_plain * on(job) group_right(extra) hist_extra", + &[("host", "h")][..], + &[("job", "a"), ("extra", "e")][..], + &["job"][..], + &[][..], + ), + ( + "floats_plain * on(job) group_right(extra) fill_left(0) hist_extra", + &[("host", "h")][..], + &[("job", "a"), ("extra", "e")][..], + &["job"][..], + &["job"][..], + ), + ( + "floats_plain * ignoring(host) group_right(extra) fill_left(0) hist_extra", + &[("host", "h")][..], + &[("job", "a"), ("extra", "e")][..], + &["extra", "job"][..], + &["extra", "job"][..], + ), + ] { + let plan = + build_native_fused_plan_with_provider(query, true, native_include_tag_provider().await) + .await; + let display = plan.display_indent().to_string(); + let fused = find_fused_series(&plan).unwrap_or_else(|| panic!("{query}: {display}")); + let NativeExpr::Binary { matching, .. } = &fused.tree().root().expr else { + panic!("expected composed Binary for {query}: {display}"); + }; + // The declared columns: the value and time columns plus the labels the walk names. + let mut columns = sorted_column_names(&plan); + columns.retain(|column| column != "field_0" && column != "timestamp"); + assert_eq!( + declared + .iter() + .map(|tag| tag.to_string()) + .collect::>(), + columns, + "{query}: {display}" + ); + // What the kernel files for the same matching: the labels the fused node carries. + let rows = promql::vector_match::binary_vectors( + matching, + &schema, + &[operand(left)], + &schema, + &[operand(right)], + ) + .unwrap_or_else(|error| panic!("{query}: {error}")); + let mut collected = rows + .iter() + .flat_map(|series| series.metric.tags().iter().map(|tag| tag.name.to_string())) + .collect::>(); + collected.sort(); + collected.dedup(); + assert_eq!( + carried + .iter() + .map(|tag| tag.to_string()) + .collect::>(), + collected, + "{query}: {display}" + ); + // The declared columns hold every label the rows carry. + for label in &collected { + assert!( + columns.contains(label), + "{query}: `{label}` is carried but not declared: {display}" + ); + } + filed += rows.len(); + } + assert!(filed > 0, "the filled cases file rows to compare"); +} + +/// Composition decides by what the kernels and the column can answer, not by operand shape. +/// +/// An operand with more than one selector, and an operand that ranks one, each compile and evaluate +/// on their own, so the pair composes. +#[tokio::test] +async fn native_composed_binary_admits_complex_operands() { + for (query, operand_inputs) in [ + ("hist * on(tag_0) (floats + on(tag_0) floats)", 2), + ("hist * on(tag_0) topk(1, floats)", 1), + ] { + let plan = + build_native_fused_plan_with_provider(query, true, native_mixed_kind_provider().await) + .await; + let display = plan.display_indent().to_string(); + let fused = find_fused_series(&plan).unwrap_or_else(|| panic!("{query}: {display}")); + let NativeExpr::Binary { left, right, .. } = &fused.tree().root().expr else { + panic!("expected composed Binary for {query}: {display}"); + }; + assert!( + matches!(left.expr, NativeExpr::Input(_)) && matches!(right.expr, NativeExpr::Input(_)), + "{query}: {display}" + ); + assert_eq!(2, fused.children().len(), "{query}: {display}"); + let operand = find_fused_series(&fused.children()[1]) + .unwrap_or_else(|| panic!("missing operand frame: {display}")); + assert_eq!( + promql::extension_plan::FusedOutputFormat::SeriesFrame, + operand.format(), + "{query}: {display}" + ); + assert_eq!(operand_inputs, operand.inputs().len(), "{query}: {display}"); + assert!(!display.contains("Join:"), "{query}: {display}"); + } + + // A fold the split cannot carry is still refused: `quantile` folds a group's samples rather + // than a fixed-width accumulator, and a second fold above one is a second cut. Those statements + // keep the existing plan, which answers them. + for query in [ + "hist * on(tag_0) quantile(0.5, floats)", + "hist * on(tag_0) sum by (tag_0) (quantile(0.5, floats))", + ] { + let plan = + build_native_fused_plan_with_provider(query, true, native_mixed_kind_provider().await) + .await; + let display = plan.display_indent().to_string(); + assert!( + find_fused_series(&plan).is_none(), + "`{query}` has no composed answer: {display}" + ); + let legacy = PromPlanner::stmt_to_plan_with_annotations( + native_mixed_kind_provider().await, + &build_eval_stmt(query), + &build_query_engine_state(), + None, + false, + ) + .await + .unwrap(); + assert_eq!( + legacy.display_indent().to_string(), + display, + "`{query}` must keep the existing plan" + ); + } +} + +/// A composed binary that meets a shape its kernels refuse keeps the existing plan. +/// +/// Admission is the composition's own: an operand the native compiler cannot bind, a value that is +/// not a frame, or a pair whose output column cannot carry both operands' kinds declines here +/// exactly as it would on the whole statement. None of these may turn into a planning error, +/// because the existing route answers them. +#[tokio::test] +async fn native_composed_binary_declines_to_the_existing_plan() { + // A comparison between a histogram and a float has no kernel, so the pair is not admitted, and + // nothing below it composes either: the whole statement stays on the existing plan. + let query = "hist > on(tag_0) floats"; + let plan = + build_native_fused_plan_with_provider(query, true, native_mixed_kind_provider().await) + .await; + let display = plan.display_indent().to_string(); + let legacy = PromPlanner::stmt_to_plan_with_annotations( + native_mixed_kind_provider().await, + &build_eval_stmt(query), + &build_query_engine_state(), + None, + false, + ) + .await + .unwrap_or_else(|error| panic!("`{query}`: the existing route must answer it: {error}")); + assert!( + find_fused_series(&plan).is_none(), + "`{query}` has no native answer: {display}" + ); + assert_eq!( + legacy.display_indent().to_string(), + display, + "`{query}` must keep the existing plan" + ); + + // An operand that is not a frame leaves the statement's own wrapper in place: the pair is + // refused, so the existing plan answers the statement while the operand that does compose keeps + // its own answer below it. + for query in [ + // A scalar root is not a frame: composing it by vector matching would broadcast it as if it + // were a one-series vector, which is not what the query asked for. + "(hist * on(tag_0) floats) * 2", + // Neither is a scalar function, however it is written. + "(hist * on(tag_0) floats) / time()", + // A comparison between a histogram and a float has no kernel at this pair either, so the + // operand's own answer stops where it was admitted. + "(hist * on(tag_0) floats) > on(tag_0) floats", + ] { + let plan = + build_native_fused_plan_with_provider(query, true, native_mixed_kind_provider().await) + .await; + let display = plan.display_indent().to_string(); + assert!( + !is_fused_series(&plan), + "`{query}` has no composed answer: {display}" + ); + assert!( + find_fused_series(&plan).is_some(), + "`{query}` keeps the operand's own answer: {display}" + ); + } +} + +/// Whole-native fill preserves histogram row schemas; empty-only arithmetic stays a float. +#[tokio::test] +async fn native_histogram_binary_fill_declares_possible_payload_kind() { + for (query, histogram_output) in [ + ( + "hist{tag_0=\"a\"} * on(tag_0) fill(1) hist{tag_0=\"b\"}", + true, + ), + ( + "hist{tag_0=\"a\"} * on(tag_0) fill_left(1) hist{tag_0=\"b\"}", + true, + ), + ( + "hist{tag_0=\"a\"} * on(tag_0) fill_right(1) hist{tag_0=\"b\"}", + true, + ), + ( + "hist{tag_0=\"a\"} / on(tag_0) fill_right(2) hist{tag_0=\"b\"}", + true, + ), + ( + "hist{tag_0=\"a\"} / on(tag_0) fill_left(2) hist{tag_0=\"b\"}", + false, + ), + ("hist{tag_0=\"a\"} * on(tag_0) hist{tag_0=\"b\"}", false), + ] { + let plan = + build_native_fused_plan_with_provider(query, true, native_mixed_kind_provider().await) + .await; + let display = plan.display_indent().to_string(); + let fused = find_fused_series(&plan).unwrap_or_else(|| panic!("{query}: {display}")); + let NativeExpr::Binary { matching, .. } = &fused.tree().root().expr else { + panic!("expected native Binary for {query}: {display}"); + }; + assert!(!display.contains("Join:"), "{display}"); + let field = plan + .schema() + .fields() + .iter() + .find(|field| field.name() == "field_0") + .unwrap(); + let expected = if histogram_output { + native_histogram_value_type().as_arrow_type() + } else { + ArrowDataType::Float64 + }; + assert_eq!(field.data_type(), &expected, "{query}: {display}"); + if query.contains("fill(") { + assert!( + matching.fill.lhs.is_some() && matching.fill.rhs.is_some(), + "{display}" + ); + } + } +} + +/// A legacy-only outer function reaches a composed native binary, including statement anchors. +#[tokio::test] +async fn native_fused_binary_inside_legacy_math_preserves_anchors() { + for query in [ + "ceil(clamp(histogram_count(hist), 1, 2) + on(tag_0) floats)", + "ceil((clamp(histogram_count(hist @ end() offset 1m), 1, 2) + on(tag_0) floats @ start()))", + ] { + let plan = + build_native_fused_plan_with_provider(query, true, native_mixed_kind_provider().await) + .await; + let display = plan.display_indent().to_string(); + let fused = find_fused_series(&plan).unwrap_or_else(|| panic!("{query}: {display}")); + let NativeExpr::Binary { left, right, .. } = &fused.tree().root().expr else { + panic!("expected composed Binary for {query}: {display}"); + }; + assert!( + matches!(left.expr, NativeExpr::Input(_)) && matches!(right.expr, NativeExpr::Input(_)), + "{display}" + ); + assert_eq!(2, fused.children().len(), "{display}"); + assert!(!display.contains("Join:"), "{display}"); + assert_eq!( + vec!["ceil(field_0)", "tag_0", "timestamp"], + sorted_column_names(&plan), + "{display}" + ); + if query.contains("@ end()") { + let left_tree = find_fused_series(&fused.children()[0]) + .unwrap() + .tree() + .to_string(); + let right_tree = find_fused_series(&fused.children()[1]) + .unwrap() + .tree() + .to_string(); + assert!( + left_tree.contains("@ 100000000ms") && left_tree.contains("offset 60000"), + "{left_tree}" + ); + assert!(right_tree.contains("@ 0ms"), "{right_tree}"); + } + } } /// A window over an evaluated input enters the native route, and only when it is asked for. @@ -5298,30 +6225,111 @@ async fn native_fused_histogram_quantile_binary_drops_le_from_operand_matching() "output schema still contains dropped `le`: {display}" ); - // Check the contexts returned at the contract boundary for both calls independently. - let mut planner = PromPlanner { - table_provider: native_classic_histogram_provider().await, - ctx: PromPlannerContext::from_eval_stmt(&build_eval_stmt( - "histogram_quantile(0.9, m) / histogram_quantile(0.5, m)", - )) - .unwrap(), - promql_annotations: None, - native_fused: true, - }; - let PromExpr::Binary(binary) = - parser::parse("histogram_quantile(0.9, m) / histogram_quantile(0.5, m)").unwrap() - else { - unreachable!() - }; - let query_engine_state = build_query_engine_state(); - for operand in [&binary.lhs, &binary.rhs] { - let (_, context) = planner - .try_plan_native_operand(operand, &query_engine_state) - .await - .unwrap() - .expect("each histogram call is admitted as one native operand"); - assert_eq!(vec!["host"], context.tag_columns); - assert!(!context.tag_columns.contains(&LE_COLUMN_NAME.to_string())); + assert!( + matches!(fused.tree().root().expr, NativeExpr::Binary { .. }), + "{display}" + ); + assert!( + plan.schema() + .fields() + .iter() + .any(|field| field.name() == "host"), + "{display}" + ); + assert!(!display.contains("Join:"), "{display}"); +} + +/// A legacy wrapper or a native one preserves a whole native binary below it, not a join of native +/// operands. +/// +/// `scalar()` is a native node, so it becomes the fused root itself and reads the binary's own fused +/// node as its single bound input. `quantile` is not a native aggregate, so its legacy fold stays +/// above a fused node that still holds the whole binary. Either way the binary matches above its two +/// sources rather than joining two separately evaluated native operands. +#[tokio::test] +async fn native_fused_binary_inside_scalar_and_quantile() { + // `true` marks the wrapper the native compiler admits into the tree; `false` keeps the legacy + // fold above it. + for (query, native_wrapper) in [ + ("scalar(some_metric + on(tag_0) other_metric)", true), + ("quantile(0.5, some_metric + on(tag_0) other_metric)", false), + ( + "scalar(clamp(some_metric, 0, 1) / on(tag_0) other_metric)", + true, + ), + ] { + let provider = build_test_table_provider_with_options( + &[ + (DEFAULT_SCHEMA_NAME.to_string(), "some_metric".to_string()), + (DEFAULT_SCHEMA_NAME.to_string(), "other_metric".to_string()), + ], + 1, + 1, + vec![], + ConcreteDataType::timestamp_millisecond_datatype(), + false, + ConcreteDataType::float64_datatype(), + NATIVE_ENGINE, + ) + .await; + let plan = build_native_fused_plan_with_provider(query, true, provider).await; + let display = plan.display_indent().to_string(); + let fused = find_fused_series(&plan).unwrap_or_else(|| panic!("{query}: {display}")); + + // One binary over both sources, with no join of two separately evaluated native operands. + assert!(!display.contains("Join:"), "{query}: {display}"); + let mut metrics = fused_input_metrics(&plan); + // The selectors are bound in source order, but the walk collects them wherever the plan + // carries them, so compare the two metrics without relying on that order. + metrics.sort(); + assert_eq!( + vec!["other_metric".to_string(), "some_metric".to_string()], + metrics, + "{query}: {display}" + ); + + let tree = if native_wrapper { + // The wrapper is this node's root and binds the binary's fused node as its one input. + let NativeExpr::ScalarOf { input } = &fused.tree().root().expr else { + panic!( + "`{query}`: the native wrapper is the fused root: {}\n{display}", + fused.tree() + ); + }; + let NativeExpr::Input(bound) = &input.expr else { + panic!( + "`{query}`: the wrapper reads one bound source: {}\n{display}", + fused.tree() + ); + }; + // The wrapper's tree has one input, so the binding names child 0: the segment that runs + // the binary. + assert_eq!( + 0, + bound.source_index, + "`{query}`: {}\n{display}", + fused.tree() + ); + assert_eq!(1, fused.children().len(), "`{query}`: {display}"); + let nested = find_fused_series(&fused.children()[0]).unwrap_or_else(|| { + panic!("`{query}`: the wrapper's input is a fused node: {display}") + }); + // The binary's node reads both sources itself, so it has one child per operand. + assert_eq!(2, nested.children().len(), "`{query}`: {display}"); + nested.tree() + } else { + // The legacy fold is still there, above the one fused node that runs the binary. + assert!( + display.contains("Aggregate: groupBy="), + "`{query}`: the legacy aggregate is gone: {display}" + ); + assert_eq!(2, fused.children().len(), "`{query}`: {display}"); + fused.tree() + }; + assert!( + matches!(tree.root().expr, NativeExpr::Binary { .. }), + "`{query}`: the binary is one native operation: {tree}\n{display}" + ); } } @@ -8362,24 +9370,11 @@ async fn native_histogram_plan(query: &str) -> String { plan.display_indent_schema().to_string() } -/// A native histogram statement runs on the native route: the producer exports the canonical -/// histogram struct column, not a float column, and the plan names no legacy histogram fold. -/// A segmented parent is typed by the kind the statement's own root answers. +/// A histogram-consuming aggregate remains a legacy fold even if its child is native. /// -/// Segmentation replaces every cut prefix with an input leaf that carries already-evaluated -/// samples. Re-deriving the parent's kind by walking that rewritten tree reads each leaf as the -/// kind its scan carried, so a float-producing histogram prefix declares a histogram column while -/// its values are floats: `count_over_time(h[5m]) + on (__name__) count_over_time(h[5m])` announced -/// the canonical histogram struct, and the row conversion then refused the floats it was handed. -/// The kind therefore comes from the uncut root, which is what these two statements pin - both -/// answer floats, and `on (__name__)` is what makes them segment. -/// An aggregate is admitted only when the kind entering it is provably a float. -/// -/// The canonicalization pass inserts a merge under an aggregate when a label rewrite can give two -/// series one label set, and the kind walk does not describe that node. Reading "cannot name it" as -/// "not a histogram" admitted `sum(label_replace(h, "tag_0", "x", "tag_0", ".*"))` to a native fold -/// over histograms, which the runtime then refuses sample by sample. The same silence covers a set -/// operation, so both stay on the existing plan. +/// A label rewrite or a set expression can pass histograms to the aggregate. Admitting that fold +/// natively would send histograms into a float-only kernel, but independently supported child +/// expressions may still have their own native producers below the legacy histogram aggregate. #[tokio::test] async fn an_aggregate_over_an_unnamed_kind_keeps_the_existing_plan() { for query in [ @@ -8389,17 +9384,32 @@ async fn an_aggregate_over_an_unnamed_kind_keeps_the_existing_plan() { ] { let table_provider = build_test_native_histogram_table_provider("some_metric").await; let plan = build_native_fused_plan_with_provider(query, true, table_provider).await; + let display = plan.display_indent_schema().to_string(); assert!( - find_fused_series(&plan).is_none(), - "{query} ran a native fold over histograms: {}", - plan.display_indent_schema() + display.contains("Aggregate: groupBy=") + && display.contains("prom_native_histogram_agg_sum("), + "{query} did not retain the legacy histogram aggregate: {display}" ); + // Inspect every fused node's complete native tree, not just its root: a set child and its + // frame producers may be native, but none may perform this histogram fold. + let mut pending = vec![&plan]; + while let Some(node) = pending.pop() { + if let LogicalPlan::Extension(Extension { node }) = node + && let Some(fused) = node.as_any().downcast_ref::() + { + let tree = fused.tree().to_string(); + assert!( + !tree + .lines() + .any(|line| line.split_whitespace().nth(1) == Some("Aggregate")), + "{query} ran a native aggregate: {tree}\n{display}" + ); + } + pending.extend(node.inputs()); + } } - // The reverse case: a float the walk can name still splits. What enters this aggregate is a - // window's count, not a histogram, and no canonical merge sits between them. (A rewrite stays - // conservative: the merge the canonicalization pass inserts is not described by the walk, so - // `sum(label_replace(histogram_count(h), ...))` keeps the existing plan along with the shapes - // above.) + // A float-producing window still permits the aggregate itself to split natively: its count, + // not the input histogram, reaches the fold. let table_provider = build_test_native_histogram_table_provider("some_metric").await; let plan = build_native_fused_plan_with_provider( "sum(count_over_time(some_metric[5m]))", @@ -13382,3 +14392,233 @@ async fn native_fused_metric_engine_count_values_output_labels() { assert_eq!(sorted_column_names(&plan), expected, "{query}"); } } + +/// A composition is an operand of another composition. +/// +/// `(hist * on(tag_0) floats) / on(tag_0) floats` reads three selectors of two field kinds, which no +/// single tree binds, so each operand is planned on its own and the left one is itself a match over +/// two frames. The statement is one fused tree, and no legacy join is left above it. +#[tokio::test] +async fn native_composed_binary_nests_as_one_fused_tree() { + let query = "(hist * on(tag_0) floats) / on(tag_0) floats"; + let plan = + build_native_fused_plan_with_provider(query, true, native_mixed_kind_provider().await) + .await; + let display = plan.display_indent().to_string(); + assert!(!display.contains("Join:"), "{query}: {display}"); + let fused = find_fused_series(&plan).unwrap_or_else(|| panic!("{query}: {display}")); + let NativeExpr::Binary { left, right, .. } = &fused.tree().root().expr else { + panic!("{query}: the statement root matches two frames: {display}"); + }; + assert!( + matches!(left.expr, NativeExpr::Input(_)) && matches!(right.expr, NativeExpr::Input(_)), + "{query}: {display}" + ); + let [nested_plan, right_plan] = fused.children() else { + panic!("{query}: the root matches two operands: {display}"); + }; + let nested = find_fused_series(nested_plan) + .unwrap_or_else(|| panic!("{query}: missing nested operand frame: {display}")); + assert_eq!( + promql::extension_plan::FusedOutputFormat::SeriesFrame, + nested.format(), + "{query}: {display}" + ); + // The nested operand is `hist * on(tag_0) floats` composed on its own, not the statement's + // three selectors bound by one tree. + let NativeExpr::Binary { left, right, .. } = &nested.tree().root().expr else { + panic!("{query}: the nested operand composes: {display}"); + }; + assert!( + matches!(left.expr, NativeExpr::Input(_)) && matches!(right.expr, NativeExpr::Input(_)), + "{query}: {display}" + ); + assert_eq!(2, nested.children().len(), "{query}: {display}"); + for child in nested.children() { + let operand = find_fused_series(child) + .unwrap_or_else(|| panic!("{query}: missing nested leaf frame: {display}")); + assert_eq!( + promql::extension_plan::FusedOutputFormat::SeriesFrame, + operand.format(), + "{query}: {display}" + ); + assert_eq!(1, operand.children().len(), "{query}: {display}"); + } + let right = find_fused_series(right_plan) + .unwrap_or_else(|| panic!("{query}: missing right operand frame: {display}")); + assert_eq!( + promql::extension_plan::FusedOutputFormat::SeriesFrame, + right.format(), + "{query}: {display}" + ); + assert_eq!(1, right.children().len(), "{query}: {display}"); + assert_eq!( + vec!["field_0", "tag_0", "timestamp"], + sorted_column_names(&plan), + "{query}: {display}" + ); +} + +/// An aggregate operand is admitted through the split's own halves. +/// +/// A fold reads across series, but its `Partial` and `Final` stages already answer one group's whole +/// state, so the pair reads the final half as one evaluated frame. The operator never sees aggregate +/// state as if it were a sample input; that is the producer's output, one node lower. +#[tokio::test] +async fn native_composed_binary_admits_a_split_aggregate_frame_operand() { + for (query, aggregate_on_the_left) in [ + ("hist * on(tag_0) sum by (tag_0) (floats)", false), + ("sum by (tag_0) (floats) * on(tag_0) hist", true), + ] { + let plan = + build_native_fused_plan_with_provider(query, true, native_mixed_kind_provider().await) + .await; + let display = plan.display_indent().to_string(); + assert!(!display.contains("Join:"), "{query}: {display}"); + let fused = find_fused_series(&plan).unwrap_or_else(|| panic!("{query}: {display}")); + let NativeExpr::Binary { left, right, .. } = &fused.tree().root().expr else { + panic!("{query}: the statement root matches two frames: {display}"); + }; + // The operand enters the match as evaluated samples on the statement's grid. + let operand_input = if aggregate_on_the_left { left } else { right }; + let NativeType::Samples(schema) = &operand_input.output else { + panic!("{query}: the aggregate enters the match as samples: {display}"); + }; + assert!(schema.eval_grid.is_some(), "{query}: {display}"); + assert_eq!(2, fused.children().len(), "{query}: {display}"); + let operand_plan = &fused.children()[if aggregate_on_the_left { 0 } else { 1 }]; + let operand = find_fused_series(operand_plan) + .unwrap_or_else(|| panic!("{query}: missing aggregate operand frame: {display}")); + assert_eq!( + promql::extension_plan::FusedOutputFormat::SeriesFrame, + operand.format(), + "{query}: {display}" + ); + // The operand is the split's final half: it answers samples and reads state. + assert!( + operand.reads_aggregate_state(), + "{query}: the operand must be the Final half: {display}" + ); + let partial = partial_below(operand).unwrap_or_else(|| { + panic!("{query}: the Final half reads a Partial producer: {display}") + }); + assert!( + matches!(&partial.tree().root().expr, NativeExpr::Aggregate { op, .. } if op.stage == AggregateStage::Partial), + "{query}: {display}" + ); + assert_eq!( + 1, + partial.children().len(), + "{query}: the producer reads one scan: {display}" + ); + } +} + +/// A source-free evaluated vector is a frame operand. +/// +/// `vector(2)` reads no region and answers one series on the statement's grid, so the match pairs it +/// like any other frame. A scalar root is refused by the same rule, which is what keeps `2` from +/// being broadcast as if it were a one-series vector. +#[tokio::test] +async fn native_composed_binary_admits_a_source_free_frame_operand() { + let query = "(hist * on(tag_0) floats) * on() group_left vector(2)"; + let plan = + build_native_fused_plan_with_provider(query, true, native_mixed_kind_provider().await) + .await; + let display = plan.display_indent().to_string(); + assert!(!display.contains("Join:"), "{query}: {display}"); + let fused = find_fused_series(&plan).unwrap_or_else(|| panic!("{query}: {display}")); + let [_nested_plan, right_plan] = fused.children() else { + panic!("{query}: the root matches two operands: {display}"); + }; + let source_free = find_fused_series(right_plan) + .unwrap_or_else(|| panic!("{query}: missing source-free operand frame: {display}")); + assert_eq!( + promql::extension_plan::FusedOutputFormat::SeriesFrame, + source_free.format(), + "{query}: {display}" + ); + assert!( + matches!(source_free.tree().root().expr, NativeExpr::VectorOf { .. }), + "{query}: the operand is the source-free `vector(2)`: {display}" + ); + assert!( + source_free.children().is_empty(), + "{query}: a source-free operand reads no input: {display}" + ); +} + +/// A composition descends a parsed expression, so a deep one ends. +/// +/// Every operand offered below is a strict part of the expression it came from, which is what bounds +/// the descent. A left-deep chain of matches over two field kinds plans as a nested tree here, and a +/// shape that did not would decline to the existing plan; neither may re-offer the same expression. +#[tokio::test] +async fn native_composed_binary_recursion_terminates_on_a_deep_expression() { + fn count_fused(plan: &LogicalPlan) -> usize { + usize::from(is_fused_series(plan)) + + plan.inputs().into_iter().map(count_fused).sum::() + } + + let depth = 32; + let mut query = "hist * on(tag_0) floats".to_string(); + for _ in 1..depth { + query = format!("({query}) / on(tag_0) floats"); + } + let plan = + build_native_fused_plan_with_provider(&query, true, native_mixed_kind_provider().await) + .await; + let display = plan.display_indent().to_string(); + assert!( + find_fused_series(&plan).is_some(), + "{depth}-deep: {display}" + ); + assert!(!display.contains("Join:"), "{depth}-deep: {display}"); + // One composition per operator, plus the leaf frames below them. + assert!( + count_fused(&plan) > depth, + "{depth}-deep: {} fused nodes: {display}", + count_fused(&plan) + ); +} + +/// A refused operand leaves the planner's selector state where it found it. +/// +/// The composed attempt writes the context while it prepares each operand's scan, so a refusal - here +/// the left operand plans and the scalar `2` is no frame - must restore it before the refusal is +/// returned. The same planner then plans another expression natively, from the same context. +#[tokio::test] +async fn native_composed_binary_restores_the_context_after_a_refused_operand() { + let state = build_query_engine_state(); + let stmt = build_eval_stmt("(hist * on(tag_0) floats) / 2"); + let mut planner = PromPlanner { + table_provider: native_mixed_kind_provider().await, + ctx: PromPlannerContext::from_eval_stmt(&stmt).unwrap(), + promql_annotations: None, + native_fused: true, + }; + let before = format!("{:?}", planner.ctx); + let refused = planner + .try_plan_native_fused(&stmt.expr, &state) + .await + .unwrap(); + assert!( + refused.is_none(), + "a scalar operand is no frame, so the pair declines" + ); + assert_eq!( + before, + format!("{:?}", planner.ctx), + "a refused operand must leave no selector state behind" + ); + // The next expression still plans: the context is the statement's own, not an operand's. + let next = build_eval_stmt("hist * on(tag_0) floats"); + assert!( + planner + .try_plan_native_fused(&next.expr, &state) + .await + .unwrap() + .is_some(), + "a refused operand must not stop the next query" + ); +} diff --git a/tests-integration/src/tests/promql_test.rs b/tests-integration/src/tests/promql_test.rs index 4b09e9cb602..2ce367ed839 100644 --- a/tests-integration/src/tests/promql_test.rs +++ b/tests-integration/src/tests/promql_test.rs @@ -2534,6 +2534,102 @@ fn assert_four_native_partitions(plan: &Arc, query: &PromQuery) { + use promql::extension_plan::FusedSeries; + use promql::native::plan::{NativeExpr, NativeNode}; + + fn depth(node: &NativeNode) -> usize { + 1 + match &node.expr { + NativeExpr::Input(_) | NativeExpr::Scalar(_) => 0, + NativeExpr::Instant { input, .. } | NativeExpr::Map { input, .. } => depth(input), + NativeExpr::ParameterizedMap { + input, parameters, .. + } => parameters.iter().map(depth).fold(depth(input), usize::max), + NativeExpr::Binary { left, right, .. } => depth(left).max(depth(right)), + other => panic!("unexpected node in deep expression: {other:?}"), + } + } + fn fused(plan: &datafusion::logical_expr::LogicalPlan) -> Option<&FusedSeries> { + if let datafusion::logical_expr::LogicalPlan::Extension(extension) = plan + && let Some(node) = extension.node.as_any().downcast_ref::() + { + return Some(node); + } + plan.inputs().into_iter().find_map(fused) + } + let ctx = native_query_context(true, 2); + let statement = QueryLanguageParser::parse_promql(query, &ctx).unwrap(); + let plan = instance + .query_engine() + .planner() + .plan(&statement, ctx) + .await + .unwrap(); + let display = plan.display_indent().to_string(); + let tree = fused(&plan) + .unwrap_or_else(|| panic!("native deep tree: {display}")) + .tree(); + assert!(depth(tree.root()) > 64, "{display}"); + assert!(!display.contains("Join:"), "{display}"); +} + +/// The representative deep expression executes on a standalone instance, on the test's own stack. +/// +/// The expression is deep enough that the walks over it used to abort the runtime worker rather +/// than answer: planning it walks the tree once, and so do building its runtime and pulling it. +/// Those walks grow their own stacks now, so nothing here owns a stack large enough to hide the +/// recursion - a build that reintroduces the overflow aborts this test instead of being masked by +/// one. The plan, the runtime and the answer are all the deep expression's own. +#[tokio::test(flavor = "multi_thread")] +async fn native_promql_deep_binary_standalone() { + common_telemetry::init_default_ut_logging(); + let (standalone, observer) = native_test_instance(GreptimeDbStandaloneBuilder::new( + "native_promql_deep_binary_standalone", + )) + .await; + let instance = standalone.fe_instance().clone(); + execute_all( + &instance, + "CREATE TABLE deep_standalone (host STRING, ts TIMESTAMP TIME INDEX, val DOUBLE, PRIMARY KEY(host)); INSERT INTO deep_standalone VALUES ('x', 180000, 3), ('x', 240000, 4), ('y', 180000, 4), ('y', 240000, 5)", + QueryContext::arc(), + ) + .await; + let query = PromQuery { + query: format!("clamp(deep_standalone{}, 0, 1000)", " + 1".repeat(80)), + start: "180".to_string(), + end: "240".to_string(), + step: "60s".to_string(), + ..PromQuery::default() + }; + assert_deep_native_plan(&instance, &query).await; + observer.clear(); + let output = instance + .do_promql_query(&query, native_query_context(true, 2)) + .await + .remove(0) + .unwrap(); + assert_eq!( + native_result_rows(output).await, + vec![ + (180_000, "x".to_string(), 83.0), + (180_000, "y".to_string(), 84.0), + (240_000, "x".to_string(), 84.0), + (240_000, "y".to_string(), 85.0) + ] + ); + let observed = observed_physical_nodes(&observer); + let names: Vec<_> = observed.iter().map(|node| node.name()).collect(); + assert!( + names.contains(&"FusedSeriesExec"), + "no native fused node: {names:?}" + ); + assert!( + names.iter().all(|name| !name.contains("JoinExec")), + "legacy join: {names:?}" + ); +} + /// A standalone instance with the native route opted in, and an observer of the plans it runs. async fn native_test_instance( builder: GreptimeDbStandaloneBuilder, @@ -2978,6 +3074,741 @@ async fn distributed_native_promql_cross_region_targets_match_expected_values() assert_eq!(control_rows, rows, "{expression}"); } +/// A deep native tree executes on the frontend while a true two-DN gRPC query reads fragments. +#[test] +fn distributed_native_promql_deep_binary_over_real_grpc() { + std::thread::Builder::new() + .name("deep-native-grpc".into()) + .stack_size(32 * 1024 * 1024) + .spawn(|| { + tokio::runtime::Builder::new_multi_thread() + .worker_threads(4) + .thread_stack_size(32 * 1024 * 1024) + .enable_all() + .build() + .unwrap() + .block_on(async { + common_telemetry::init_default_ut_logging(); + let cluster = GreptimeDbClusterBuilder::new("distributed_native_promql_deep_binary") + .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 deep_dist_phy (host STRING, val DOUBLE, ts TIMESTAMP TIME INDEX, PRIMARY KEY(host)) PARTITION ON COLUMNS (ts) (ts < '1970-01-01 00:02:00', ts >= '1970-01-01 00:02:00' AND ts < '1970-01-01 00:04:00', ts >= '1970-01-01 00:04:00') ENGINE=metric WITH ('physical_metric_table'='true'); CREATE TABLE deep_dist_b (host STRING, val DOUBLE, ts TIMESTAMP TIME INDEX, PRIMARY KEY(host)) ENGINE=metric WITH ('on_physical_table'='deep_dist_phy'); INSERT INTO deep_dist_b (host,val,ts) VALUES ('x',1,60000),('x',2,120000),('x',3,180000),('x',4,240000),('y',2,60000),('y',3,120000),('y',4,180000),('y',5,240000)", QueryContext::arc()).await; + let query = PromQuery { + query: format!("clamp(deep_dist_b{}, 0, 1000)", " + 1".repeat(80)), + start: "180".to_string(), + end: "240".to_string(), + step: "60s".to_string(), + ..PromQuery::default() + }; + assert_deep_native_plan(&instance, &query).await; + cluster.reset_network_do_get_peak(); + let output = instance.do_promql_query(&query, native_query_context(true, 2)).await.remove(0).unwrap(); + let plan = output.meta.plan.as_ref().expect("frontend physical plan"); + assert_native_merge_scans(plan, 1, 3); + let display = datafusion::physical_plan::displayable(plan.as_ref()).indent(true).to_string(); + assert!(display.contains("FusedSeriesExec") && !display.contains("JoinExec"), "{display}"); + assert_eq!(native_result_rows(output).await, vec![(180_000, "x".to_string(), 83.0), (180_000, "y".to_string(), 84.0), (240_000, "x".to_string(), 84.0), (240_000, "y".to_string(), 85.0)]); + assert!(cluster.network_do_get_peak_active() > 0, "remote gRPC reads occurred"); + assert_eq!(cluster.network_do_get_active(), 0, "all response bodies closed"); + }); + }) + .unwrap() + .join() + .unwrap(); +} + +/// A composed binary consumes a real float canonical boundary before scaling histograms. +#[tokio::test(flavor = "multi_thread")] +async fn native_promql_composed_binary_canonical_operand() { + use api::greptime_proto::io::prometheus::write::v2::Histogram; + use api::greptime_proto::io::prometheus::write::v2::histogram::{Count, ZeroCount}; + use axum::body::{Body, to_bytes}; + use common_query::native_histogram::read_histogram; + use datatypes::arrow::array::StructArray; + use http::Request; + use prost::Message; + use servers::prom_remote_write::v2::test_util as remote_write_v2; + use servers::prom_store::snappy_compress; + use tower::ServiceExt; + + common_telemetry::init_default_ut_logging(); + let (standalone, observer) = native_test_instance(GreptimeDbStandaloneBuilder::new( + "native_promql_composed_binary_canonical_operand", + )) + .await; + let instance = standalone.fe_instance().clone(); + execute_all(&instance, "CREATE TABLE canonical_float (job STRING, host STRING, ts TIMESTAMP TIME INDEX, val DOUBLE, PRIMARY KEY(job,host)); INSERT INTO canonical_float VALUES ('api','a',0,2),('api','b',600000,3)", QueryContext::arc()).await; + let server = crate::test_util::build_test_prom_server(instance.clone(), false, None).build(); + let router = server.build(server.make_app()).unwrap(); + let request = remote_write_v2::request_with_labels_and_histograms( + vec![("__name__", "canonical_hist"), ("job", "api")], + [0, 600_000] + .into_iter() + .map(|timestamp| Histogram { + timestamp, + count: Some(Count::CountInt(4)), + zero_count: Some(ZeroCount::ZeroCountInt(4)), + sum: 8.0, + ..Default::default() + }) + .collect(), + ); + let response = router + .oneshot( + Request::post("/v1/prometheus/write") + .header("Content-Encoding", "snappy") + .header( + "Content-Type", + "application/x-protobuf;proto=io.prometheus.write.v2.Request", + ) + .body(Body::from( + snappy_compress(&request.encode_to_vec()).unwrap(), + )) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!( + response.status(), + 204, + "{:?}", + to_bytes(response.into_body(), usize::MAX).await.unwrap() + ); + + let expression = + r#"label_replace(canonical_float, "host", "same", "host", ".*") * on(job) canonical_hist"#; + let query = PromQuery { + query: expression.to_string(), + start: "0".to_string(), + end: "600".to_string(), + step: "600s".to_string(), + ..PromQuery::default() + }; + let ctx = native_query_context(true, 4); + let stmt = QueryLanguageParser::parse_promql(&query, &ctx).unwrap(); + let logical = instance + .query_engine() + .planner() + .plan(&stmt, ctx) + .await + .unwrap(); + let logical_display = logical.display_indent().to_string(); + let datafusion::logical_expr::LogicalPlan::Extension(extension) = &logical else { + panic!("{logical_display}") + }; + let binary = extension + .node + .as_any() + .downcast_ref::() + .unwrap_or_else(|| panic!("{logical_display}")); + assert!( + matches!( + binary.tree().root().expr, + promql::native::plan::NativeExpr::Binary { .. } + ), + "{logical_display}" + ); + assert_eq!(binary.children().len(), 2, "{logical_display}"); + assert!( + logical_display.contains("output: series_frame") && !logical_display.contains("Join:"), + "{logical_display}" + ); + + observer.clear(); + let output = instance + .do_promql_query(&query, native_query_context(true, 4)) + .await + .remove(0) + .unwrap(); + let batches = match output.data { + OutputData::Stream(stream) => collect_batches(stream).await.unwrap(), + OutputData::RecordBatches(batches) => batches, + _ => unreachable!(), + }; + let mut rows = Vec::new(); + for batch in batches.iter() { + let batch = batch.df_record_batch(); + let time = batch + .columns() + .iter() + .find(|column| matches!(column.data_type(), DataType::Timestamp(..))) + .unwrap(); + let time = cast(time.as_ref(), &DataType::Int64).unwrap(); + let time = time.as_any().downcast_ref::().unwrap(); + let job = batch.column_by_name("job").unwrap(); + let job = cast(job.as_ref(), &DataType::Utf8).unwrap(); + let job = job.as_any().downcast_ref::().unwrap(); + let histogram = batch + .columns() + .iter() + .find_map(|column| column.as_any().downcast_ref::()) + .unwrap(); + for row in 0..batch.num_rows() { + let h = read_histogram(histogram, row).unwrap().unwrap(); + rows.push(( + time.value(row), + job.value(row).to_string(), + h.count, + h.zero_count, + h.sum, + )); + } + } + rows.sort_by_key(|row| row.0); + assert_eq!( + rows, + vec![ + (0, "api".to_string(), 8.0, 8.0, 16.0), + (600_000, "api".to_string(), 12.0, 12.0, 24.0) + ] + ); + assert!( + canonical_router(&observer).is_some(), + "canonical producer must route its complete float label set" + ); + + // Overlapping inputs must fail at the canonical boundary before the binary can conceal them. + execute_all( + &instance, + "INSERT INTO canonical_float VALUES ('api','b',0,3)", + QueryContext::arc(), + ) + .await; + observer.clear(); + let result = instance + .do_promql_query(&query, native_query_context(true, 4)) + .await + .remove(0); + let error = match result { + Ok(output) => collect_error(output).await, + Err(error) => error.to_string(), + }; + assert!( + error.contains("cannot contain metrics with the same labelset"), + "{error}" + ); + assert!( + canonical_router(&observer).is_some(), + "the canonical producer must check the overlapping series" + ); +} + +/// Two independently planned histogram frames retain reset-collision values and diagnostics. +#[tokio::test(flavor = "multi_thread")] +async fn native_promql_composed_histogram_reset_warning() { + use api::greptime_proto::io::prometheus::write::v2::histogram::Count; + use api::greptime_proto::io::prometheus::write::v2::{BucketSpan, Histogram}; + use axum::body::{Body, to_bytes}; + use common_query::native_histogram::{CounterResetHint, read_histogram}; + use common_query::promql_annotations::promql_annotation_collector; + use datatypes::arrow::array::StructArray; + use http::Request; + use prost::Message; + use servers::prom_remote_write::v2::test_util as remote_write_v2; + use servers::prom_store::snappy_compress; + use tower::ServiceExt; + + common_telemetry::init_default_ut_logging(); + let (standalone, _) = native_test_instance(GreptimeDbStandaloneBuilder::new( + "native_promql_composed_histogram_reset_warning", + )) + .await; + let instance = standalone.fe_instance().clone(); + let server = crate::test_util::build_test_prom_server(instance.clone(), false, None).build(); + let router = server.build(server.make_app()).unwrap(); + for (metric, count, sum, reset_hint) in [ + ("reset_hist_a", 4_u64, 8.0, 1), + ("reset_hist_b", 2_u64, 4.0, 2), + ] { + let request = remote_write_v2::request_with_labels_and_histograms( + vec![("__name__", metric), ("job", "api")], + vec![Histogram { + timestamp: 60_000, + count: Some(Count::CountInt(count)), + sum, + schema: 1, + positive_spans: vec![BucketSpan { + offset: 0, + length: 2, + }], + positive_deltas: vec![(count / 2) as i64, 0], + reset_hint, + ..Default::default() + }], + ); + let response = router + .clone() + .oneshot( + Request::post("/v1/prometheus/write") + .header("Content-Encoding", "snappy") + .header( + "Content-Type", + "application/x-protobuf;proto=io.prometheus.write.v2.Request", + ) + .body(Body::from( + snappy_compress(&request.encode_to_vec()).unwrap(), + )) + .unwrap(), + ) + .await + .unwrap(); + let status = response.status(); + assert_eq!( + status, + 204, + "{:?}", + to_bytes(response.into_body(), usize::MAX).await.unwrap() + ); + } + let query = PromQuery { + query: "reset_hist_a + on(job) reset_hist_b".to_string(), + start: "60".to_string(), + end: "60".to_string(), + step: "60s".to_string(), + ..PromQuery::default() + }; + let ctx = native_query_context(true, 2); + let statement = QueryLanguageParser::parse_promql(&query, &ctx).unwrap(); + let logical = instance + .query_engine() + .planner() + .plan(&statement, Arc::clone(&ctx)) + .await + .unwrap(); + let display = logical.display_indent().to_string(); + let datafusion::logical_expr::LogicalPlan::Extension(extension) = &logical else { + panic!("{display}") + }; + let binary = extension + .node + .as_any() + .downcast_ref::() + .unwrap_or_else(|| panic!("{display}")); + let promql::native::plan::NativeExpr::Binary { left, right, .. } = &binary.tree().root().expr + else { + panic!("{display}") + }; + assert!( + matches!(left.expr, promql::native::plan::NativeExpr::Input(_)) + && matches!(right.expr, promql::native::plan::NativeExpr::Input(_)), + "{display}" + ); + assert_eq!(binary.children().len(), 2, "{display}"); + assert!(!display.contains("Join:"), "{display}"); + + // Hold a strong reference to the query-scoped collector until the output stream has finished. + let collector = promql_annotation_collector(ctx.remote_query_id().unwrap()); + let output = instance + .do_promql_query(&query, ctx) + .await + .remove(0) + .unwrap(); + let batches = match output.data { + OutputData::Stream(stream) => collect_batches(stream).await.unwrap(), + OutputData::RecordBatches(batches) => batches, + _ => unreachable!(), + }; + let mut rows = Vec::new(); + for batch in batches.iter() { + let batch = batch.df_record_batch(); + let time = batch + .columns() + .iter() + .find(|column| matches!(column.data_type(), DataType::Timestamp(..))) + .unwrap(); + let time = cast(time.as_ref(), &DataType::Int64).unwrap(); + let time = time.as_any().downcast_ref::().unwrap(); + let job = cast( + batch.column_by_name("job").unwrap().as_ref(), + &DataType::Utf8, + ) + .unwrap(); + let job = job.as_any().downcast_ref::().unwrap(); + let histogram = batch + .columns() + .iter() + .find_map(|column| column.as_any().downcast_ref::()) + .unwrap(); + for row in 0..batch.num_rows() { + let h = read_histogram(histogram, row).unwrap().unwrap(); + rows.push((time.value(row), job.value(row).to_string(), h)); + } + } + assert_eq!(rows.len(), 1); + let (timestamp, job, h) = &rows[0]; + assert_eq!((*timestamp, job.as_str()), (60_000, "api")); + assert_eq!((h.count, h.sum, h.zero_count), (6.0, 12.0, 0.0)); + assert_eq!((h.schema, h.zero_threshold), (1, 0.0)); + assert_eq!(h.positive_spans.len(), 1); + assert_eq!( + (h.positive_spans[0].offset, h.positive_spans[0].length), + (0, 2) + ); + assert_eq!(h.positive_buckets, vec![3.0, 3.0]); + assert!(h.negative_spans.is_empty() && h.negative_buckets.is_empty()); + assert!(h.custom_values.is_empty()); + assert_eq!(h.reset_hint, CounterResetHint::Unknown); + assert_eq!(h.start_timestamp, None); + let mut warnings = Vec::new(); + let mut infos = Vec::new(); + collector.append_to(&mut warnings, &mut infos); + assert_eq!( + warnings, + vec!["prom_native_histogram_add: native histogram counter reset hints contradict"] + ); + assert!(infos.is_empty()); +} + +/// A composed native binary answers grouped matches and multi-selector operands as one fused node +/// over two evaluated frames, and fills a missing side the way its modifier says. +/// +/// The rows are written out rather than taken from the existing route: these pairs mix a native +/// histogram with a float, which is the shape the composed route exists for. What they pin is that +/// a grouped match reports the many side's labels with the declared ones copied in from the one +/// side (`group_left` keeps the left side's labels, `group_right` the right side's and drops the +/// label the one side does not carry), and that the arithmetic is the modifier's: `histogram * k` +/// scales count and sum, and a missing right side filled with zero scales them to zero. A filled +/// step is filed at the match group of the series that has the sample, so a `group_right` fill +/// answers at the one side's labels - `job` below, which the many side never carries - and the +/// answer has to declare them for the rows to keep them. A grouped `include` label is the other +/// way round: it is copied from the one side, which removes it from the many side's labels when it +/// does not have it, so a label only the many side carries is on no row and the answer must not +/// declare it either. +#[tokio::test(flavor = "multi_thread")] +async fn native_promql_composed_grouped_and_multi_selector_operands() { + use api::greptime_proto::io::prometheus::write::v2::Histogram; + use api::greptime_proto::io::prometheus::write::v2::histogram::{Count, ZeroCount}; + use axum::body::{Body, to_bytes}; + use http::Request; + use prost::Message; + use servers::prom_remote_write::v2::test_util as remote_write_v2; + use servers::prom_store::snappy_compress; + use tower::ServiceExt; + + common_telemetry::init_default_ut_logging(); + let (standalone, _) = native_test_instance(GreptimeDbStandaloneBuilder::new( + "native_promql_composed_grouped_operands", + )) + .await; + let instance = standalone.fe_instance().clone(); + execute_all( + &instance, + "CREATE TABLE grouped_float (job STRING, host STRING, unit STRING, ts TIMESTAMP TIME INDEX, val DOUBLE, PRIMARY KEY(job, host, unit)); + INSERT INTO grouped_float VALUES ('api','a','x',0,2),('api','a','x',600000,3); + CREATE TABLE grouped_gap (job STRING, host STRING, ts TIMESTAMP TIME INDEX, val DOUBLE, PRIMARY KEY(job, host)); + INSERT INTO grouped_gap VALUES ('api','a',600000,4); + CREATE TABLE grouped_host (host STRING, ts TIMESTAMP TIME INDEX, val DOUBLE, PRIMARY KEY(host)); + INSERT INTO grouped_host VALUES ('a',600000,4)", + QueryContext::arc(), + ) + .await; + // The histogram side is one series, labeled `job="api"`, sampled at the two steps: four counts + // and a sum of eight. + let server = crate::test_util::build_test_prom_server(instance.clone(), false, None).build(); + let router = server.build(server.make_app()).unwrap(); + let request = remote_write_v2::request_with_labels_and_histograms( + vec![("__name__", "grouped_hist"), ("job", "api")], + [0, 600_000] + .into_iter() + .map(|timestamp| Histogram { + timestamp, + count: Some(Count::CountInt(4)), + zero_count: Some(ZeroCount::ZeroCountInt(4)), + sum: 8.0, + ..Default::default() + }) + .collect(), + ); + let response = router + .oneshot( + Request::post("/v1/prometheus/write") + .header("Content-Encoding", "snappy") + .header( + "Content-Type", + "application/x-protobuf;proto=io.prometheus.write.v2.Request", + ) + .body(Body::from( + snappy_compress(&request.encode_to_vec()).unwrap(), + )) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!( + response.status(), + 204, + "{:?}", + to_bytes(response.into_body(), usize::MAX).await.unwrap() + ); + + for (expression, expected, undeclared) in [ + // `group_left(host)` reports the left (many) side's labels and copies `host` in from the one + // side, which carries it. + ( + "grouped_hist * on(job) group_left(host) grouped_float", + vec![ + ( + 0, + vec!["host=a".to_string(), "job=api".to_string()], + 8.0, + 16.0, + ), + ( + 600_000, + vec!["host=a".to_string(), "job=api".to_string()], + 12.0, + 24.0, + ), + ], + &[][..], + ), + // `group_right(host)` reports the right (many) side's labels: `host` is dropped because the + // one side does not carry it, and `unit` survives because a grouping does not narrow. + ( + "grouped_hist * on(job) group_right(host) grouped_float", + vec![ + ( + 0, + vec!["job=api".to_string(), "unit=x".to_string()], + 8.0, + 16.0, + ), + ( + 600_000, + vec!["job=api".to_string(), "unit=x".to_string()], + 12.0, + 24.0, + ), + ], + // The one side never carries `host`, and `on(job)` keeps it out of the key a fill reads. + &["host"][..], + ), + // A one-to-one match narrows the labels to what it matches on. + ( + "grouped_hist * on(job) grouped_float", + vec![ + (0, vec!["job=api".to_string()], 8.0, 16.0), + (600_000, vec!["job=api".to_string()], 12.0, 24.0), + ], + &[][..], + ), + // `group_right` fills a missing many side from the one side's series, at that series' match + // group label set. `grouped_host` carries no `job`, so no step pairs and every step is + // filled with zero at the one side's `job` - a label the many side never carries, and one + // the answer therefore has to declare. + ( + "grouped_hist * on(job) group_right fill_right(0) grouped_host", + vec![ + (0, vec!["job=api".to_string()], 0.0, 0.0), + (600_000, vec!["job=api".to_string()], 0.0, 0.0), + ], + &[][..], + ), + // `group_left(host)` copies `host` from the one side, and the one side here is the histogram + // series, which carries no `host`: the many side's own `host` never reaches a row, so the + // answer declares neither it nor a column nothing fills. `unit` is not an include label and + // survives, as a grouping does not narrow. + ( + "grouped_float * on(job) group_left(host) grouped_hist", + vec![ + ( + 0, + vec!["job=api".to_string(), "unit=x".to_string()], + 8.0, + 16.0, + ), + ( + 600_000, + vec!["job=api".to_string(), "unit=x".to_string()], + 12.0, + 24.0, + ), + ], + // The one side never carries `host`, and `on(job)` keeps it out of the key a fill reads. + &["host"][..], + ), + // An operand that reads more than one selector still evaluates on its own: the right side + // is `2 + 2` at the first step and `3 + 3` at the second, so the histogram is scaled by 4 + // and then by 6. + ( + "grouped_hist * on(job) (grouped_float + on(job, host) grouped_float)", + vec![ + (0, vec!["job=api".to_string()], 16.0, 32.0), + (600_000, vec!["job=api".to_string()], 24.0, 48.0), + ], + &[][..], + ), + ] { + let query = PromQuery { + query: expression.to_string(), + start: "0".to_string(), + end: "600".to_string(), + step: "600s".to_string(), + ..PromQuery::default() + }; + // The composed plan itself: one fused binary over two evaluated operands, with no join + // between them. + let ctx = native_query_context(true, 4); + let statement = QueryLanguageParser::parse_promql(&query, &ctx).unwrap(); + let logical = instance + .query_engine() + .planner() + .plan(&statement, ctx) + .await + .unwrap(); + let display = logical.display_indent().to_string(); + let datafusion::logical_expr::LogicalPlan::Extension(extension) = &logical else { + panic!("{expression}: {display}") + }; + let binary = extension + .node + .as_any() + .downcast_ref::() + .unwrap_or_else(|| panic!("{expression}: {display}")); + assert!( + matches!( + binary.tree().root().expr, + promql::native::plan::NativeExpr::Binary { .. } + ), + "{expression}: {display}" + ); + assert_eq!(binary.children().len(), 2, "{expression}: {display}"); + assert!( + display.contains("output: series_frame") && !display.contains("Join:"), + "{expression}: {display}" + ); + + let rows = histogram_label_rows( + instance + .do_promql_query(&query, native_query_context(true, 4)) + .await + .remove(0) + .unwrap(), + ) + .await; + // Every label an answer carries is one the plan declares: a filled step answers at the one + // side's match group, which is a label set the many side never carries. + for (_, labels, _, _) in &rows { + for label in labels { + let name = label.split('=').next().unwrap(); + assert!( + logical + .schema() + .fields() + .iter() + .any(|field| field.name().as_str() == name), + "{expression}: `{name}` is carried but not declared: {display}" + ); + } + } + // And no column is declared that no row can fill: a grouped `include` label is on a row only + // when the one side carries it or the matching keeps it in the key a fill reads. + for name in undeclared { + assert!( + !logical + .schema() + .fields() + .iter() + .any(|field| field.name().as_str() == *name), + "{expression}: `{name}` is declared but no row can carry it: {display}" + ); + } + assert_eq!(expected, rows, "{expression}"); + } + + // `fill_right` answers the step whose right side is missing: a histogram scaled by zero keeps + // the label set of the many side and answers zero count and sum. + let query = PromQuery { + query: "grouped_hist * on(job) fill_right(0) grouped_gap".to_string(), + start: "0".to_string(), + end: "600".to_string(), + step: "600s".to_string(), + ..PromQuery::default() + }; + let filled = histogram_label_rows( + instance + .do_promql_query(&query, native_query_context(true, 4)) + .await + .remove(0) + .unwrap(), + ) + .await; + assert_eq!( + vec![ + (0, vec!["job=api".to_string()], 0.0, 0.0), + (600_000, vec!["job=api".to_string()], 16.0, 32.0), + ], + filled, + "the missing right side is filled with zero" + ); +} + +/// The rows of a histogram-answering PromQL result, read by column kind rather than by name. +/// +/// The two routes name their value column differently - the compiled field of a native plan against +/// the legacy expression text - and order their columns differently, so each row is flattened to +/// its timestamp, its labels as `name=value`, and the histogram's count and sum. +async fn histogram_label_rows(output: Output) -> Vec<(i64, Vec, f64, f64)> { + use common_query::native_histogram::read_histogram; + use datatypes::arrow::array::StructArray; + + let batches = match output.data { + OutputData::Stream(stream) => collect_batches(stream).await.unwrap(), + OutputData::RecordBatches(batches) => batches, + _ => unreachable!("PromQL answers record batches"), + }; + let mut rows = Vec::new(); + for batch in batches.iter() { + let batch = batch.df_record_batch(); + let (time_index, _) = batch + .columns() + .iter() + .enumerate() + .find(|(_, column)| matches!(column.data_type(), DataType::Timestamp(..))) + .expect("a PromQL result carries its time index"); + let time = cast(batch.column(time_index).as_ref(), &DataType::Int64).unwrap(); + let time = time.as_any().downcast_ref::().unwrap(); + let names = batch + .schema() + .fields() + .iter() + .map(|field| field.name().clone()) + .collect::>(); + let histogram = batch + .columns() + .iter() + .find_map(|column| column.as_any().downcast_ref::()) + .expect("a histogram result carries a histogram column"); + for row in 0..batch.num_rows() { + let mut labels = Vec::new(); + for (index, column) in batch.columns().iter().enumerate() { + if !matches!( + column.data_type(), + DataType::Utf8 | DataType::Dictionary(_, _) + ) { + continue; + } + let text = cast(column.as_ref(), &DataType::Utf8).unwrap(); + let text = text.as_any().downcast_ref::().unwrap(); + if !text.is_null(row) && !text.value(row).is_empty() { + labels.push(format!("{}={}", names[index], text.value(row))); + } + } + labels.sort(); + let histogram = read_histogram(histogram, row).unwrap().unwrap(); + rows.push((time.value(row), labels, histogram.count, histogram.sum)); + } + } + rows.sort_by(|left, right| left.0.cmp(&right.0).then_with(|| left.1.cmp(&right.1))); + rows +} + /// The rows of the repeated-selector fixture, inserted into the one logical table /// `{prefix}_b`. Both hosts are counters sampled every minute for ten minutes, longer than the /// widest window the test reads, so a 5m window and a 10m window over the same series cover @@ -4033,11 +4864,17 @@ 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. +/// A union composed under a grouping by the metric name reports one group per metric, over the +/// wire. +/// +/// `sum by (__name__) (metric or mito)` reads a metric table beside an ordinary Mito one. The +/// union is one fused producer whose segments read the two sources, and the name each side reports +/// is the label the outer grouping folds by, so the answer is two groups whose label sets differ +/// only in their metric name. That grouping is what this asserts, together with the route: a plan +/// that dropped the name would fold the two series into one group whose single value still looks +/// plausible, and a plan answered by the legacy join would have no name column to fold at all. #[tokio::test(flavor = "multi_thread")] -async fn distributed_native_promql_remote_refuses_unsupported_discovery_and_name_grouping() { +async fn distributed_native_promql_remote_answers_a_name_grouping_over_a_native_union() { common_telemetry::init_default_ut_logging(); let cluster = GreptimeDbClusterBuilder::new("distributed_native_promql_refusal") .await @@ -4103,17 +4940,37 @@ async fn distributed_native_promql_remote_refuses_unsupported_discovery_and_name 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; + .remove(0) + .expect("the composed plan answers a grouping by the metric name"); + let plan = Arc::clone(output.meta.plan.as_ref().expect("frontend physical plan")); + // The route has to stay honest: the union is a native producer rather than the legacy join + // the existing plan builds for it, so the name column the grouping reads exists at all. + assert!( + native_plan_shape(&plan).fused >= 1, + "the union stays native: {plan:?}" + ); + let display = datafusion::physical_plan::displayable(plan.as_ref()) + .indent(true) + .to_string(); + assert!( + !display.contains("JoinExec"), + "the union is not a legacy join: {display}" + ); + let (_, rows) = native_labelled_rows(output).await; + let named = |name: &str| { + std::collections::BTreeMap::from([("__name__".to_string(), name.to_string())]) }; - let plan = output.meta.plan.as_ref().expect("frontend physical plan"); + // Grouping by the name keeps no other label, and the 180s sample is still inside the 5m + // lookback at 240s, so both steps report the same value under each name. assert_eq!( - native_plan_shape(plan).fused, - 0, - "two sources cannot report groups that differ only by metric name: {plan:?}" + vec![ + (180_000, named("dist_native_refuse_a"), 6.0), + (240_000, named("dist_native_refuse_a"), 6.0), + (180_000, named("dist_native_refuse_mito"), 6.0), + (240_000, named("dist_native_refuse_mito"), 6.0), + ], + rows, + "{expression}: one group per metric name, each named after its own metric" ); } } @@ -8500,16 +9357,15 @@ async fn native_promql_metric_two_aggregate_pair_matches_legacy() { } } -/// A grouping by the metric name keeps series the schema would have no column for. +/// A grouping by the metric name reports one group per metric. /// -/// `sum by (__name__) (a or b)` folds two metrics into two groups that differ only in their metric -/// name. The native aggregate keeps that name and folds by it, but the label set the node declares is -/// taken from the existing planner's columns, and `__name__` is no column of any scan - so the two -/// groups would be reported under one label set, as two rows a reader cannot tell apart. The number -/// of rows and their full label sets are what this asserts: the values alone would still look -/// plausible, and only the labels say the identity was lost. +/// `sum by (__name__) (a or b)` reads two metrics whose only distinguishing label is their metric +/// name. The union reports the name of the side each series came from, and the outer grouping folds +/// by that name, so the answer is two groups, each named after its own metric. The number of rows +/// and their full label sets are what this asserts: a plan that dropped the name would report the +/// two contributions as one group, and the value alone would still look plausible. #[tokio::test(flavor = "multi_thread")] -async fn native_promql_declines_a_grouping_by_the_metric_name() { +async fn native_promql_answers_a_grouping_by_the_metric_name() { common_telemetry::init_default_ut_logging(); let (standalone, observer) = native_test_instance(GreptimeDbStandaloneBuilder::new( "native_promql_group_by_name", @@ -8544,30 +9400,37 @@ async fn native_promql_declines_a_grouping_by_the_metric_name() { // whose only distinguishing label is the metric name. let query = r#"sum by (__name__) (native_name_demo{host="a"} or native_name_other{host="b"})"#; let sql = format!("TQL EVAL (0, 0, '1s') {query}"); - let (_, legacy_rows) = - native_labelled_rows(native_tql_output(&instance, &sql, false).await).await; - assert!( - !legacy_rows.is_empty(), - "{query}: the existing plan must answer this, otherwise there is nothing to keep" - ); - observer.clear(); let (_, native_rows) = native_labelled_rows(native_tql_output(&instance, &sql, true).await).await; + let root = datafusion::physical_plan::displayable(observer.root_plan().as_ref()) + .indent(true) + .to_string(); + // The union has to stay native: the legacy plan joins the two tables and reports no name column + // the outer grouping could fold by. assert!( - observer.fused().is_none(), - "{query}: the route must decline a grouping by the metric name, whose groups its declared \ - schema cannot name" + observer.fused().is_some(), + "{query}: the union runs as one fused node: {root}" ); + assert!( + !root.contains("JoinExec"), + "{query}: the union is not a legacy join: {root}" + ); + let named = + |name: &str| std::collections::BTreeMap::from([("__name__".to_string(), name.to_string())]); assert_eq!( - native_rows, legacy_rows, - "{query}: the full label sets of every row, not only the values" + vec![ + (0, named("native_name_demo"), 2.0), + (0, named("native_name_other"), 8.0), + ], + native_rows, + "{query}: one group per metric name, each named after its own metric" ); } -/// Ordinary Mito sources also decline a multi-source grouping by metric name. +/// Ordinary Mito sources also report one group per metric under a grouping by the metric name. #[tokio::test(flavor = "multi_thread")] -async fn native_promql_declines_mito_cross_table_grouping_by_metric_name() { +async fn native_promql_answers_mito_cross_table_grouping_by_metric_name() { common_telemetry::init_default_ut_logging(); let (standalone, observer) = native_test_instance(GreptimeDbStandaloneBuilder::new( "native_promql_mito_group_by_name", @@ -8586,30 +9449,37 @@ async fn native_promql_declines_mito_cross_table_grouping_by_metric_name() { let query = r#"sum by (__name__) (name_a{host="a"} or name_b{host="b"})"#; let sql = format!("TQL EVAL (0, 0, '1s') {query}"); - let (_, legacy_rows) = - native_labelled_rows(native_tql_output(&instance, &sql, false).await).await; - assert!( - !legacy_rows.is_empty(), - "{query}: existing plan must answer" - ); - observer.clear(); let (_, native_rows) = native_labelled_rows(native_tql_output(&instance, &sql, true).await).await; + let root = datafusion::physical_plan::displayable(observer.root_plan().as_ref()) + .indent(true) + .to_string(); assert!( - observer.fused().is_none(), - "{query}: native must decline multi-source grouping by __name__" + observer.fused().is_some(), + "{query}: the union runs as one fused node: {root}" ); + assert!( + !root.contains("JoinExec"), + "{query}: the union is not a legacy join: {root}" + ); + let named = + |name: &str| std::collections::BTreeMap::from([("__name__".to_string(), name.to_string())]); assert_eq!( - native_rows, legacy_rows, - "{query}: compare the full labelled rows from both routes" + vec![(0, named("name_a"), 2.0), (0, named("name_b"), 8.0)], + native_rows, + "{query}: compare the full labelled rows" ); } -/// Unsupported `count_values` groupings keep the legacy plan before labels are lost or execution -/// reaches a kernel refusal. The outer `sum` prevents a final-schema check from masking the latter. +/// `count_values` over a union reports one series per metric name, and per value it counted. +/// +/// `by (__name__)` folds the two inputs into two groups that differ only in their metric name; +/// `by (val)` names a label no series carries, so both inputs fall into one group and the outer +/// `sum` folds what `count_values` wrote there. The full label sets of every row are what this +/// asserts: a plan that dropped the name would report one series where two metrics were counted. #[tokio::test(flavor = "multi_thread")] -async fn native_promql_declines_count_values_groupings_the_node_cannot_honor() { +async fn native_promql_answers_count_values_groupings_over_a_union() { common_telemetry::init_default_ut_logging(); let (standalone, observer) = native_test_instance(GreptimeDbStandaloneBuilder::new( "native_promql_count_values_groups", @@ -8640,37 +9510,46 @@ async fn native_promql_declines_count_values_groupings_the_node_cannot_honor() { ) .await; - // `by (__name__)` names a label absent from the declared schema; the outer sum hides the - // unsupported `by (val)` grouping from the final schema. + // `count_values` writes the value it counted into the label the first argument names, so each + // row carries that label beside the group's own labels. `val` is no label of either series, so + // the second query groups both inputs together and counts each of them once. let by_name = r#"count_values by (__name__) ("sample", native_cv_demo{host="a"} or native_cv_other{host="b"})"#; let by_field = r#"sum(count_values by (val) ("sample", native_cv_demo{host="a"} or native_cv_other{host="b"}))"#; - for query in [by_name, by_field] { + let counted = |name: &str| { + std::collections::BTreeMap::from([ + ("__name__".to_string(), name.to_string()), + ("sample".to_string(), "1".to_string()), + ]) + }; + for (query, expected) in [ + ( + by_name, + vec![ + (0, counted("native_cv_demo"), 1.0), + (0, counted("native_cv_other"), 1.0), + ], + ), + (by_field, vec![(0, std::collections::BTreeMap::new(), 2.0)]), + ] { let sql = format!("TQL EVAL (0, 0, '1s') {query}"); - let (_, legacy_rows) = - native_labelled_rows(native_tql_output(&instance, &sql, false).await).await; - assert!( - !legacy_rows.is_empty(), - "{query}: the existing plan must answer this, otherwise there is nothing to keep" - ); - observer.clear(); let (_, native_rows) = native_labelled_rows(native_tql_output(&instance, &sql, true).await).await; + let root = datafusion::physical_plan::displayable(observer.root_plan().as_ref()) + .indent(true) + .to_string(); assert!( - observer.fused().is_none(), - "{query}: the route must decline a grouping its declared schema or kernel cannot honor" + observer.fused().is_some(), + "{query}: the union runs as one fused node: {root}" + ); + assert!( + !root.contains("JoinExec"), + "{query}: the union is not a legacy join: {root}" ); assert_eq!( - native_rows, legacy_rows, + expected, native_rows, "{query}: the full label sets of every row, not only the values" ); - if query == by_field { - assert_eq!( - native_rows, - vec![(0, std::collections::BTreeMap::new(), 2.0)], - "the outer sum counts both input series" - ); - } } } @@ -8801,10 +9680,11 @@ async fn native_promql_rewrites_the_label_count_values_writes() { /// `topk`/`bottomk` key their groups by series labels, while the existing plan also accepts a value /// field there and forms one group per sample value. A `by()` therefore keeps every -/// group's winner on the existing plan and must not reach the native node, which would fold both -/// series into one group and return fewer rows. +/// group's winner, and the native route has to report the same rows: the union below it reports its +/// series, and the window above it partitions by the same value column. The rows are what this +/// asserts - one winner per value group - rather than that no native node answers the union. #[tokio::test(flavor = "multi_thread")] -async fn native_promql_declines_ranking_grouped_by_a_value_field() { +async fn native_promql_keeps_a_ranking_grouped_by_a_value_field() { common_telemetry::init_default_ut_logging(); let (standalone, observer) = native_test_instance(GreptimeDbStandaloneBuilder::new( "native_promql_ranking_value_field", @@ -8836,8 +9716,8 @@ async fn native_promql_declines_ranking_grouped_by_a_value_field() { .await; let pair = r#"native_rank_demo{host="x"} or native_rank_other{host="y"}"#; - // `by (val)` forms two groups of one sample each, so both series survive: the existing plan - // answers two rows where the native node would keep the single winner of one group. + // `by (val)` forms two groups of one sample each, so both series survive: two rows, one winner + // per value group, on both routes. for query in [ format!("topk by (val) (1, {pair})"), format!("bottomk by (val) (1, {pair})"), @@ -8846,23 +9726,25 @@ async fn native_promql_declines_ranking_grouped_by_a_value_field() { observer.clear(); let legacy_rows = native_result_rows(native_tql_output(&instance, &sql, false).await).await; assert_eq!( - legacy_rows.len(), - 2, + legacy_rows, + vec![(0, "x".to_string(), 2.0), (0, "y".to_string(), 8.0)], "{query}: the existing plan keeps one row per value group" ); - assert!( - legacy_rows - .iter() - .map(|(_, _, value)| *value) - .eq([2.0, 8.0]), - "{query}: {legacy_rows:?}" - ); observer.clear(); let native_rows = native_result_rows(native_tql_output(&instance, &sql, true).await).await; + let root = datafusion::physical_plan::displayable(observer.root_plan().as_ref()) + .indent(true) + .to_string(); + // The union has to stay native rather than become the legacy join, and it must not fold the + // two value groups into one. assert!( - observer.fused().is_none(), - "{query}: the route must decline a grouping its groups cannot honor" + observer.fused().is_some(), + "{query}: the union runs as one fused node: {root}" + ); + assert!( + !root.contains("JoinExec"), + "{query}: the union is not a legacy join: {root}" ); assert_eq!(native_rows, legacy_rows, "{query}: full rows"); } @@ -8878,19 +9760,26 @@ async fn native_promql_declines_ranking_grouped_by_a_value_field() { assert_eq!(native_rows.len(), 2); } -/// `limitk` keeps the series that arrive first, so a multi-input node must not answer it. +/// A selection answers natively, and the shape it cannot answer reports Prometheus's own error. /// -/// A node with more than one input hands its inputs' partitions over in the order they became ready, -/// while `limitk` keeps the first `k` series of each group in input order: the schedule would decide -/// which series a query keeps, where Prometheus defines the selection over a series set. The -/// aggregate split declines it for the same reason, and that refusal is repeated for a multi-input -/// node. +/// `limitk` keeps the first `k` series of each group in input order, and Prometheus leaves which +/// series those are to the engine: a query that selects some of a series set may keep any subset, +/// so a route that hands its input over in a different order still answers correctly. What the +/// native route must not do is keep the statement from being answered at all. /// -/// The existing planner cannot plan `limitk` at all - it has no kernel for the operator - so the -/// statement fails on both routes today. What this pins is that the native route does not change -/// that outcome by answering it, and in particular does not answer it differently between runs. +/// That is what the earlier version of this test pinned, and it was wrong: the existing planner has +/// no kernel for `limitk`, so "the plan this statement already had" was an error, and holding the +/// native route to it was a coverage loss rather than a safety property. The route answers these +/// statements now, and the assertions below are on the answers. +/// +/// Two shapes are checked. The wrapper case is the one that can be answered *incorrectly*: with +/// `vector(scalar(...))` on the left, `on()` matches the right side's two series against one +/// label set on the left, and Prometheus refuses that as many-to-many. The route must report that +/// refusal, not pick a side silently. The plain case selects from a one-series input, so the +/// first-k rule has one answer and the test can name it; a two-series input would leave the pick to +/// the engine, which is exactly the freedom Prometheus grants. #[tokio::test(flavor = "multi_thread")] -async fn native_promql_declines_a_selection_that_depends_on_arrival_order() { +async fn native_promql_answers_a_selection_that_depends_on_input_order() { common_telemetry::init_default_ut_logging(); let (standalone, observer) = native_test_instance(GreptimeDbStandaloneBuilder::new( "native_promql_limitk_order", @@ -8915,51 +9804,58 @@ async fn native_promql_declines_a_selection_that_depends_on_arrival_order() { val DOUBLE, PRIMARY KEY (host) ) ENGINE=metric WITH ("on_physical_table" = 'native_order_phy'); + CREATE TABLE native_order_only ( + host STRING, + ts TIMESTAMP TIME INDEX, + val DOUBLE, + PRIMARY KEY (host) + ) ENGINE=metric WITH ("on_physical_table" = 'native_order_phy'); INSERT INTO native_order_left (host, ts, val) VALUES ('a', 0, 2), ('b', 0, 4); - INSERT INTO native_order_right (host, ts, val) VALUES ('a', 0, 8), ('b', 0, 8);"#, + INSERT INTO native_order_right (host, ts, val) VALUES ('a', 0, 8), ('b', 0, 8); + INSERT INTO native_order_only (host, ts, val) VALUES ('a', 0, 8);"#, QueryContext::arc(), ) .await; - for query in [ - "native_order_left and on(host) limitk(1, native_order_right)", - // The same selection under a `vector(scalar(...))` wrapper. The wrapper answers one value - // from its input, so the order-dependent selection is still in the tree: a walk that read - // `vector()` as a leaf would admit this shape and answer `10` or `12` depending on which - // partition the gather handed over first. - "vector(scalar(limitk(1, native_order_left))) + on() native_order_right", - ] { - let sql = format!("TQL EVAL (0, 0, '1s') {query}"); + // The wrapper answers one value from its input, so the selection below it decides the value + // and the `on()` match then pairs one left label set with two right series. Prometheus refuses + // that pair, and so must the route: answering `10` or `12` here would be a silent choice + // between the two matches. + let query = "vector(scalar(limitk(1, native_order_left))) + on() native_order_right"; + let sql = format!("TQL EVAL (0, 0, '1s') {query}"); + observer.clear(); + let native = instance + .do_query(&sql, native_query_context(true, 4)) + .await + .remove(0) + .expect("the route plans the statement"); + assert!( + observer.fused().is_some(), + "{query}: the route answers a selection, it does not decline it" + ); + // The pair is refused while the stream is read, so the error is the answer's, not a planning + // refusal that would have kept the statement on the existing plan. + let error = collect_error(native).await; + assert!( + error.contains("found duplicate series for the match group {} on the right hand-side"), + "{query}: {error}" + ); + assert!( + error.contains("many-to-many matching not allowed"), + "{query}: {error}" + ); - // The route declines, so the statement is planned by the one that was always its answer. - observer.clear(); - let native = instance - .do_query(&sql, native_query_context(true, 4)) - .await - .remove(0); - assert!( - observer.fused().is_none(), - "{query}: the route must decline a selection whose answer depends on arrival order" - ); - let legacy = instance - .do_query(&sql, native_query_context(false, 4)) - .await - .remove(0); - // Both routes report the same outcome: the decline leads to the plan this statement already - // had, not to an answer the schedule could change. - assert_eq!( - native.is_err(), - legacy.is_err(), - "{query}: the route must not change whether the statement is answerable" - ); - if let (Err(native_error), Err(legacy_error)) = (native, legacy) { - assert_eq!( - native_error.to_string(), - legacy_error.to_string(), - "{query}: the decline must lead to the plan this statement already had" - ); - } - } + // One candidate, so the first-`k` rule keeps it and the answer is the series itself. + let query = "limitk(1, native_order_only)"; + let sql = format!("TQL EVAL (0, 0, '1s') {query}"); + observer.clear(); + let rows = native_result_rows(native_tql_output(&instance, &sql, true).await).await; + assert_eq!( + observer.take().name(), + "FusedSeriesExec", + "{query}: the route answers it natively" + ); + assert_eq!(rows, vec![(0, "a".to_string(), 8.0)], "{query}"); } /// A tree the native runtime cannot execute keeps the existing plan, rather than failing the @@ -9141,6 +10037,50 @@ async fn native_promql_delta_temporality_rate_and_increase_match_legacy() { ); } + // Only the denominator carries the temporality tag; the rated counter is unmarked. + // Its zero-point extrapolation yields a rate of 0.25. The second form also requires clamp. + execute_all( + &instance, + &format!( + "INSERT INTO {TABLE} (host, ts, val, otlp_aggregation_temporality) VALUES ('a', 60000, 2, '{GREPTIME_TEMPORALITY_DELTA}'), ('a', 120000, 2, '{GREPTIME_TEMPORALITY_DELTA}')" + ), + QueryContext::arc(), + ) + .await; + for (query, expected) in [ + (format!("rate({CONTROL}[2m]) / on(host) {TABLE}"), 1.0 / 8.0), + ( + format!("rate({CONTROL}[2m]) / on(host) clamp({TABLE}, 0, 1)"), + 1.0 / 4.0, + ), + ] { + let sql = format!("TQL EVAL (120, 120, '1m') {query}"); + observer.clear(); + let (columns, rows) = + native_labelled_rows(native_tql_output(&instance, &sql, true).await).await; + assert_eq!(columns, vec!["host", "ts", "val"]); + assert_eq!(rows.len(), 1, "{query}: {rows:?}"); + assert_eq!(rows[0].0, 120_000, "{query}"); + assert_eq!( + rows[0].1, + [("host".to_string(), "a".to_string())].into(), + "{query}" + ); + assert!((rows[0].2 - expected).abs() < 1e-10, "{query}: {rows:?}"); + let binary = observer.two_input_fused(); + let display = datafusion::physical_plan::displayable(binary.as_ref()) + .indent(true) + .to_string(); + assert!(display.contains("Binary"), "{query}: {display}"); + assert!( + !datafusion::physical_plan::displayable(observer.root_plan().as_ref()) + .indent(true) + .to_string() + .contains("JoinExec"), + "{query}" + ); + } + // The control: a marker-free table (samples 10 and 30 here, unlike the table above) still fuses // and agrees with the existing plan, so the decline is about the marker, not the table shape. let control_sql = format!("TQL EVAL (120, 120, '1m') sum_over_time({CONTROL}[2m])"); @@ -9161,6 +10101,69 @@ async fn native_promql_delta_temporality_rate_and_increase_match_legacy() { ); } +/// Legacy wrappers over a native binary retain the binary's values and evaluation grid. +#[tokio::test(flavor = "multi_thread")] +async fn native_promql_binary_inside_scalar_and_quantile() { + let (standalone, observer) = native_test_instance(GreptimeDbStandaloneBuilder::new( + "native_promql_binary_inside_scalar_and_quantile", + )) + .await; + let instance = standalone.fe_instance().clone(); + execute_all( + &instance, + "CREATE TABLE nested_native_a (job STRING, ts TIMESTAMP(3) TIME INDEX, val DOUBLE, PRIMARY KEY (job)); CREATE TABLE nested_native_b (job STRING, ts TIMESTAMP(3) TIME INDEX, val DOUBLE, PRIMARY KEY (job)); INSERT INTO nested_native_a VALUES ('api', 60000, 6), ('api', 120000, 8); INSERT INTO nested_native_b VALUES ('api', 60000, 2), ('api', 120000, 4);", + QueryContext::arc(), + ) + .await; + for query in [ + "scalar(nested_native_a + on(job) nested_native_b)", + "quantile(0.5, nested_native_a + on(job) nested_native_b)", + ] { + let sql = format!("TQL EVAL (60, 120, '1m') {query}"); + let (_, legacy) = + native_labelled_rows(native_tql_output(&instance, &sql, false).await).await; + observer.clear(); + let (_, rows) = native_labelled_rows(native_tql_output(&instance, &sql, true).await).await; + assert_eq!(rows, legacy, "{query}"); + assert_eq!( + rows, + vec![ + (60_000, Default::default(), 8.0), + (120_000, Default::default(), 12.0) + ], + "{query}" + ); + let binary = observer.two_input_fused(); + let display = datafusion::physical_plan::displayable(binary.as_ref()) + .indent(true) + .to_string(); + assert!(display.contains("Binary"), "{query}: {display}"); + let root = datafusion::physical_plan::displayable(observer.root_plan().as_ref()) + .indent(true) + .to_string(); + assert!(!root.contains("JoinExec"), "{query}: {root}"); + } + let query = "scalar(clamp(nested_native_a, 0, 1) / on(job) nested_native_b)"; + let sql = format!("TQL EVAL (60, 120, '1m') {query}"); + observer.clear(); + let (_, rows) = native_labelled_rows(native_tql_output(&instance, &sql, true).await).await; + assert_eq!( + rows, + vec![ + (60_000, Default::default(), 0.5), + (120_000, Default::default(), 0.25) + ] + ); + let binary = observer.two_input_fused(); + let display = datafusion::physical_plan::displayable(binary.as_ref()) + .indent(true) + .to_string(); + assert!( + display.contains("Binary") && display.contains("clamp"), + "{query}: {display}" + ); +} + /// Enabling query fallback — by the engine option or by the per-request `query_fallback` hint — must /// keep an otherwise fusible query on the existing plan without changing its answer. #[tokio::test(flavor = "multi_thread")] diff --git a/tests-integration/tests/http.rs b/tests-integration/tests/http.rs index 6bdfea38926..0ea70d41e8d 100644 --- a/tests-integration/tests/http.rs +++ b/tests-integration/tests/http.rs @@ -199,6 +199,7 @@ macro_rules! http_tests { test_remote_write_v2_histogram_float_prefix_native_route, test_remote_write_v2_histogram_native_annotations, test_remote_write_v2_histogram_native_arithmetic, + test_remote_write_v2_histogram_native_composition, test_remote_write_v2_histogram_flush_native_consistency, test_remote_write_v2_histogram_native_partitions, test_remote_write_v2_histogram_native_range_function, @@ -5644,6 +5645,402 @@ pub async fn test_remote_write_v2_histogram_native_arithmetic(store_type: Storag ); } +/// Executes a public PromQL request and checks the binary node actually used, without assuming how +/// many fused producers or children the physical plan contains. +async fn native_histogram_composition_query( + client: &TestClient, + observer: &NativeFusedObserver, + route: &PromRouteObserver, + query: &str, + parameters: &str, +) -> (StatusCode, Value) { + observer.clear(); + let response = client + .get(&format!( + "/v1/prometheus/api/v1/{parameters}&query={}", + encode(query) + )) + .header("x-greptime-hints", "query.experimental_native_promql=true") + .send() + .await; + let status = response.status(); + let body: Value = response.json().await; + + let descriptions = observer + .take_all(query) + .iter() + .map(|node| { + datafusion::physical_plan::displayable(node.as_ref()) + .one_line() + .to_string() + }) + .collect::>(); + assert!( + descriptions.iter().any(|description| { + description.lines().any(|line| { + let mut words = line.split_whitespace(); + matches!(words.next(), Some("root" | "input" | "left" | "right")) + && words.next() == Some("Binary") + }) + }), + "`{query}` did not execute a native Binary: {descriptions:?}; response: {body}" + ); + let nodes = plan_nodes(&route.take_plan()); + assert!( + nodes + .iter() + .all(|node| { !node["name"].as_str().unwrap_or_default().contains("Join") }), + "`{query}` executed a legacy join: {nodes:?}" + ); + (status, body) +} + +/// Composed histogram frames retain their values, annotations and matching semantics over HTTP. +pub async fn test_remote_write_v2_histogram_native_composition(store_type: StorageType) { + let observer = Arc::new(NativeFusedObserver::default()); + let route = Arc::new(PromRouteObserver::default()); + let plugins = Plugins::default(); + let wrapper: PhysicalPlanWrapperRef = observer.clone(); + plugins.insert(wrapper); + plugins.insert::>(route.clone()); + let instance = tests_integration::standalone::GreptimeDbStandaloneBuilder::new( + "remote_write_v2_histogram_native_composition", + ) + .with_default_store_type(store_type) + .with_query_options(QueryOptions { + experimental_native_promql: true, + ..Default::default() + }) + .with_plugin(plugins) + .build() + .await; + let server = build_test_prom_server(instance.fe_instance().clone(), false, None) + .with_greptime_config_options(instance.opts.datanode_options().to_toml().unwrap()) + .build(); + let client = TestClient::new(server.build(server.make_app()).unwrap()).await; + + for (metric, job, instance_label, timestamp, count, sum) in [ + ("native_comp_h", "api", "a", 1_000, 10, 20.0), + // A later count of 1 makes losing the offset observable at the second range step. + ("native_comp_h", "api", "a", 121_000, 1, 2.0), + // For this other job, losing @ end() instead makes the first step return 3 rather than 4. + ("native_comp_h", "anchor", "a", 1_000, 1, 2.0), + ("native_comp_h", "anchor", "a", 61_000, 10, 20.0), + ("native_comp_h", "anchor", "a", 121_000, 1, 2.0), + ("native_comp_h2", "api", "a", 1_000, 4, 8.0), + ("native_comp_h2", "other", "a", 1_000, 4, 8.0), + ("native_comp_dups", "duplicate", "a", 1_000, 4, 8.0), + ("native_comp_dups", "duplicate", "b", 1_000, 4, 8.0), + ] { + write_native_histogram( + &client, + &[ + (prom_store::METRIC_NAME_LABEL, metric), + ("job", job), + ("instance", instance_label), + ], + timestamp, + count, + sum, + ) + .await; + } + for sql in [ + "CREATE TABLE native_comp_f (ts TIMESTAMP TIME INDEX, val DOUBLE, job STRING, PRIMARY KEY (job))", + "INSERT INTO native_comp_f(job, val, ts) VALUES ('api', 2.0, 1000), ('duplicate', 2.0, 1000), ('api', 2.0, 61000), ('api', 9.0, 121000), ('anchor', 2.0, 61000), ('anchor', 9.0, 121000)", + ] { + tests_integration::test_util::execute_sql(instance.fe_instance(), sql).await; + } + + // The first sample has two equal buckets of 5; h2 has two equal buckets of 2. + // Repeat the same public requests after flushing so the source can be memtable or SST. + for phase in ["memtable", "flushed"] { + if phase == "flushed" { + for table in [ + "native_comp_h", + "native_comp_h2", + "native_comp_dups", + "native_comp_f", + ] { + tests_integration::test_util::execute_sql( + instance.fe_instance(), + &format!("ADMIN FLUSH_TABLE('{table}')"), + ) + .await; + } + } + + for (query, count, sum, bucket) in [ + ( + "native_comp_f{job=\"api\"} * on(job) native_comp_h", + "20", + "40", + "10", + ), + ( + "native_comp_h / on(job) native_comp_f{job=\"api\"}", + "5", + "10", + "2.5", + ), + ( + "native_comp_h + on(job) native_comp_h2{job=\"api\"}", + "14", + "28", + "7", + ), + ( + "native_comp_h - on(job) native_comp_h2{job=\"api\"}", + "6", + "12", + "3", + ), + ] { + let (status, body) = native_histogram_composition_query( + &client, + &observer, + &route, + query, + "query?time=1", + ) + .await; + assert_eq!(status, StatusCode::OK, "{phase}: {query}: {body}"); + assert_eq!( + body, + json!({ + "status": "success", + "data": {"resultType": "vector", "result": [{ + "metric": {"job": "api"}, + "histogram": [1.0, {"count": count, "sum": sum, "buckets": [ + [0, "0.7071067811865476", "1", bucket], + [0, "1", "1.4142135623730951", bucket] + ]}] + }]} + }), + "{phase}: {query}" + ); + } + + // These selectors read one unpartitioned histogram table but hold disjoint match groups. + // The real histogram/histogram pair cannot emit for `*` or `/`, so only a synthetic float + // can produce a result. Whether that result carries the unsupported-pair Info follows the + // operation the synthesized sample enters, not whether the pair was synthesized: + // Prometheus runs the same `doBinOp` on a filled sample, so a pairing it supports + // (`float * histogram`, `histogram / float`) stays silent, while one it does not + // (`float / histogram`) is dropped and reported exactly as a matched pair would be. + let left = "native_comp_h{job=\"api\"}"; + let right = "native_comp_h{job=\"anchor\"}"; + for (operator, expected, dropped_op) in [ + ( + "* on(job) fill(1)", + vec![("anchor", "1", "2", "1"), ("api", "10", "20", "5")], + None, + ), + ( + "* on(job) fill_left(1)", + vec![("anchor", "1", "2", "1")], + None, + ), + ( + "* on(job) fill_right(1)", + vec![("api", "10", "20", "5")], + None, + ), + ( + "/ on(job) fill_right(2)", + vec![("api", "5", "10", "2.5")], + None, + ), + // `fill_left(2)` answers the anchor series with the synthesized float on the left, so + // the pair is `float / histogram`: division by a histogram has no kernel, and the + // sample Prometheus would drop here is the one this reports. + ("/ on(job) fill_left(2)", vec![], Some("/")), + ] { + let query = format!("{left} {operator} {right}"); + let (status, body) = native_histogram_composition_query( + &client, + &observer, + &route, + &query, + "query?time=1", + ) + .await; + assert_eq!(status, StatusCode::OK, "{phase}: {query}: {body}"); + assert_eq!(body["status"], "success", "{phase}: {query}: {body}"); + assert_eq!( + body["data"]["resultType"], "vector", + "{phase}: {query}: {body}" + ); + match dropped_op { + Some(op) => assert_eq!( + body["infos"], + json!([format!( + "{op}: dropped native histogram samples because this binary operation is not supported for native histograms" + )]), + "{phase}: {query}: {body}" + ), + None => assert!( + body.get("infos").is_none(), + "a supported fill is not a dropped pair: {phase}: {query}: {body}" + ), + } + assert!(body.get("warnings").is_none(), "{phase}: {query}: {body}"); + let mut rows = body["data"]["result"].as_array().unwrap().clone(); + rows.sort_by(|a, b| { + a["metric"]["job"] + .as_str() + .cmp(&b["metric"]["job"].as_str()) + }); + let expected_rows: Vec<_> = expected + .into_iter() + .map(|(job, count, sum, bucket)| { + // The anchor fixture's count=1 has bucket counts [0, 1]; the HTTP histogram + // omits the first, zero-count bucket. The api fixture has two equal buckets. + let buckets = if job == "anchor" { + json!([[0, "1", "1.4142135623730951", bucket]]) + } else { + json!([ + [0, "0.7071067811865476", "1", bucket], + [0, "1", "1.4142135623730951", bucket] + ]) + }; + json!({ + "metric": {"job": job}, + "histogram": [1.0, {"count": count, "sum": sum, "buckets": buckets}] + }) + }) + .collect(); + assert_eq!(rows, expected_rows, "{phase}: {query}: {body}"); + } + // A present, matched histogram/histogram pair still drops and reports its existing Info. + let query = format!("{left} * on(job) {left}"); + let (status, body) = + native_histogram_composition_query(&client, &observer, &route, &query, "query?time=1") + .await; + assert_eq!(status, StatusCode::OK, "{phase}: {query}: {body}"); + assert_eq!( + body, + json!({ + "status": "success", + "data": {"resultType": "vector", "result": []}, + "infos": ["*: dropped native histogram samples because this binary operation is not supported for native histograms"] + }), + "{phase}: {query}" + ); + + // `clamp` is native-only here, so legacy fallback cannot produce these answers. + for (query, op) in [ + ( + "clamp(native_comp_f{job=\"api\"}, 1, 2) + on(job) native_comp_h", + "+", + ), + ( + "native_comp_h + on(job) clamp(native_comp_f{job=\"api\"}, 1, 2)", + "+", + ), + ("native_comp_h * on(job) native_comp_h2{job=\"api\"}", "*"), + ] { + let (status, body) = native_histogram_composition_query( + &client, + &observer, + &route, + query, + "query?time=1", + ) + .await; + assert_eq!(status, StatusCode::OK, "{phase}: {query}: {body}"); + assert_eq!( + body, + json!({ + "status": "success", + "data": {"resultType": "vector", "result": []}, + "infos": [format!("{op}: dropped native histogram samples because this binary operation is not supported for native histograms")] + }), + "{phase}: {query}" + ); + } + + // A different match group, or a selector with no samples, must not fabricate a drop Info. + for query in [ + "clamp(native_comp_f{job=\"api\"}, 1, 2) + on(job) native_comp_h2{job=\"other\"}", + "native_comp_h * on(job) native_comp_h2{job=\"other\"}", + "clamp(native_comp_f{job=\"absent\"}, 1, 2) + on(job) native_comp_h", + "native_comp_h + on(job) clamp(native_comp_f{job=\"absent\"}, 1, 2)", + ] { + let (status, body) = native_histogram_composition_query( + &client, + &observer, + &route, + query, + "query?time=1", + ) + .await; + assert_eq!(status, StatusCode::OK, "{phase}: {query}: {body}"); + assert_eq!( + body, + json!({"status": "success", "data": {"resultType": "vector", "result": []}}), + "{phase}: {query}" + ); + } + + // The duplicate group is on the right. It is an error both when it matches and when it + // does not: both sides have samples, so the native matcher's presence gate is open. + for job in ["duplicate", "api"] { + let query = + format!("clamp(native_comp_f{{job=\"{job}\"}}, 1, 2) + on(job) native_comp_dups"); + let (status, body) = native_histogram_composition_query( + &client, + &observer, + &route, + &query, + "query?time=1", + ) + .await; + assert!(!status.is_success(), "{phase}: {query}: {body}"); + assert_eq!(body["status"], "error", "{phase}: {query}: {body}"); + let error = body["error"].as_str().expect("matching error message"); + assert!( + error.contains("found duplicate series for the match group {job=\"duplicate\"} on the right hand-side of the operation at 1000 ms; many-to-many matching not allowed"), + "{phase}: {query}: {body}" + ); + } + + // The 121s end minus 1m reads the 61s histogram; @ start() reads the 61s float. + // Both are 2 after clamp, at both points. A 121s float of 9 exposes a lost @ start(). + // The `anchor` job also exposes a lost @ end() at the first point; both jobs expose a + // lost offset at the second point. Neither expected series carries its metric name. + let query = "ceil(clamp(histogram_count(native_comp_h @ end() offset 1m), 1, 2) + on(job) native_comp_f{job=~\"api|anchor\"} @ start())"; + let (status, body) = native_histogram_composition_query( + &client, + &observer, + &route, + query, + "query_range?start=61&end=121&step=60s", + ) + .await; + assert_eq!(status, StatusCode::OK, "{phase}: {query}: {body}"); + assert_eq!(body["status"], "success", "{phase}: {query}: {body}"); + assert_eq!( + body["data"]["resultType"], "matrix", + "{phase}: {query}: {body}" + ); + let results = body["data"]["result"].as_array().expect("range result"); + assert_eq!(results.len(), 2, "{phase}: {query}: {body}"); + for job in ["api", "anchor"] { + let expected = json!({ + "metric": {"job": job}, "values": [[61.0, "4.0"], [121.0, "4.0"]] + }); + assert_eq!( + results.iter().filter(|result| **result == expected).count(), + 1, + "{phase}: {query}: {body}" + ); + } + assert!(body.get("infos").is_none(), "{phase}: {query}: {body}"); + assert!(body.get("warnings").is_none(), "{phase}: {query}: {body}"); + } +} + /// A native histogram query that has something to warn about puts it in the response. /// /// `rate` over a gauge histogram is the case: the kernel drops nothing but reports that the input