diff --git a/src/common/recordbatch/src/adapter.rs b/src/common/recordbatch/src/adapter.rs index 12a617d4858..a053003a7df 100644 --- a/src/common/recordbatch/src/adapter.rs +++ b/src/common/recordbatch/src/adapter.rs @@ -17,8 +17,8 @@ use std::future::Future; use std::marker::PhantomData; use std::pin::Pin; use std::str::FromStr; -use std::sync::Arc; use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, Mutex}; use std::task::{Context, Poll}; use common_base::readable_size::ReadableSize; @@ -33,7 +33,7 @@ use datafusion::logical_expr::Expr; use datafusion::logical_expr::physical_planning_context::PhysicalPlanningContext; use datafusion::logical_expr::utils::conjunction; use datafusion::physical_expr::create_physical_expr; -use datafusion::physical_plan::metrics::{BaselineMetrics, MetricValue}; +use datafusion::physical_plan::metrics::{BaselineMetrics, CustomMetricValue, MetricValue}; use datafusion::physical_plan::{ DisplayFormatType, ExecutionPlan, ExecutionPlanVisitor, PhysicalExpr, RecordBatchStream as DfRecordBatchStream, accept, @@ -470,6 +470,7 @@ fn collect_lightweight_query_load_metrics_inner( plan_name: df_plan.name().to_string(), level, metrics: vec![("output_bytes".to_string(), output_bytes)], + native: None, }); } @@ -588,6 +589,7 @@ impl ExecutionPlanVisitor for MetricCollector { plan_name: plan.name().to_string(), level: self.current_level, metrics: vec![], + native: None, }); self.current_level += 1; return Ok(true); @@ -603,8 +605,20 @@ impl ExecutionPlanVisitor for MetricCollector { plan_name: plan.name().to_string(), level: self.current_level, metrics: Vec::with_capacity(metric.iter().size_hint().0), + native: None, }; for m in metric.iter() { + // An operator that runs nodes inside itself registers the tree as one of its metric + // values. It travels here, where a plain value cannot: the tree is structure, not a + // figure, so it is reported apart from the flat list instead of being flattened into it. + if let MetricValue::Custom { name, value } = m.value() + && name.as_ref() == NATIVE_TREE_METRIC + { + if let Some(tree) = value.as_any().downcast_ref::() { + plan_metric.native = Some(tree.report()); + } + continue; + } plan_metric .metrics .push((m.value().name().to_string(), m.value().as_usize())); @@ -694,45 +708,59 @@ pub struct RegionWatermarkEntry { } /// Determines if a metric name represents a time measurement that should be formatted. -fn is_time_metric(metric_name: &str) -> bool { +pub fn is_time_metric(metric_name: &str) -> bool { metric_name.contains("elapsed") || metric_name.contains("time") || metric_name.contains("cost") } /// Determines if a metric name represents a bytes measurement that should be formatted. -fn is_bytes_metric(metric_name: &str) -> bool { +pub fn is_bytes_metric(metric_name: &str) -> bool { metric_name.contains("bytes") || metric_name.contains("mem") } -fn format_bytes_human_readable(bytes: usize) -> String { +/// Renders `bytes` the way a plan line reports a memory figure. +pub fn format_bytes_human_readable(bytes: usize) -> String { format!("{}", ReadableSize(bytes as u64)) } +/// Renders one metric as `label: value`, the way every `ANALYZE` line reports it. +/// +/// A time is a readable duration, a byte count a readable size, and anything else a plain integer. +/// This is the one formatter for a plan line, so a node rendered by its own operator and a node +/// rendered by the collector read alike. +pub fn format_metric(label: &str, value: usize) -> String { + if is_time_metric(label) { + format!("{}: {}", label, format_nanoseconds_human_readable(value)) + } else if is_bytes_metric(label) { + format!("{}: {}", label, format_bytes_human_readable(value)) + } else { + format!("{}: {}", label, value) + } +} + /// Only display `plan_metrics` with indent ` ` (2 spaces). +/// +/// An operator's own figures go at the end of its own line, which is the line that names it. An +/// operator whose line continues - one that renders the nodes it runs below itself - therefore +/// keeps those figures rather than receiving every figure of the subtree it drew, and the lines it +/// drew keep the figures and the indentation they were rendered with, shifted to sit under it. impl Display for RecordBatchMetrics { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { for metric in &self.plan_metrics { + let indent = metric.level * 2; + let mut lines = metric.plan.trim_end().lines(); write!( f, "{:indent$}{} metrics=[", " ", - metric.plan.trim_end(), - indent = metric.level * 2, + lines.next().unwrap_or_default(), )?; for (label, value) in &metric.metrics { - if is_time_metric(label) { - write!( - f, - "{}: {}, ", - label, - format_nanoseconds_human_readable(*value), - )?; - } else if is_bytes_metric(label) { - write!(f, "{}: {}, ", label, format_bytes_human_readable(*value),)?; - } else { - write!(f, "{}: {}, ", label, value)?; - } + write!(f, "{}, ", format_metric(label, *value))?; } writeln!(f, "]")?; + for line in lines { + writeln!(f, "{:indent$}{line}", " ")?; + } } Ok(()) @@ -751,6 +779,131 @@ pub struct PlanMetrics { /// An ordered key-value list of metrics. /// Key is metric label and value is metric value. pub metrics: Vec<(String, usize)>, + /// The tree of nodes an operator runs inside itself, when it runs one. + /// + /// Such an operator reports its own figures through `metrics` and each node it runs through + /// this: they are not the same operator, so they are not the same entry. Absent for every other + /// operator, and for a record written before this field existed. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub native: Option, +} + +/// One node of a tree an operator runs inside itself, with the figures that node recorded. +/// +/// The shape is the plan line's own: the node's line without figures, the figures, and the nodes +/// that consume its output, in the order the operator feeds them. +#[derive(serde::Serialize, serde::Deserialize, Default, Debug, Clone, PartialEq, Eq)] +pub struct NativeTreeMetrics { + /// The node's line without figures, as `: `. + pub line: String, + /// An ordered key-value list of the figures this node recorded. + #[serde(default)] + pub metrics: Vec<(String, usize)>, + /// The nodes that consume this one's output. + #[serde(default)] + pub children: Vec, +} + +/// Builds the tree a reader sees, when it asks for it. +type NativeTreeReader = Arc NativeTreeMetrics + Send + Sync>; + +/// The tree an operator runs, registered as one of that operator's metrics. +/// +/// It is a metric value so it travels with the operator's own figures through the same collection +/// and the same record: a plan's figures are only final once it ran, which is when `ANALYZE` reads +/// them, so what is registered is how to read the tree rather than a copy of it. +pub struct NativeTreeMetric { + /// Reads the tree as it is now. `None` for a value created to accumulate a combination. + reader: Option, + /// A report this value took over while combining with another, which wins over `reader`. + adopted: Mutex>, +} + +/// The name an operator registers its tree under. +/// +/// It carries the same `native.` prefix as the per-node figures so a reader that lists an +/// operator's own figures leaves it out. +pub const NATIVE_TREE_METRIC: &str = "native.tree"; + +impl NativeTreeMetric { + /// Registers `reader` as the way to read the tree when it is asked for. + pub fn new(reader: impl Fn() -> NativeTreeMetrics + Send + Sync + 'static) -> Self { + Self { + reader: Some(Arc::new(reader)), + adopted: Mutex::new(None), + } + } + + /// The tree with the figures it has now. + pub fn report(&self) -> NativeTreeMetrics { + if let Some(adopted) = self + .adopted + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) + .clone() + { + return adopted; + } + self.reader + .as_ref() + .map_or_else(NativeTreeMetrics::default, |reader| reader()) + } +} + +impl std::fmt::Display for NativeTreeMetric { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "a tree of {} nodes", self.report().node_count()) + } +} + +impl std::fmt::Debug for NativeTreeMetric { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("NativeTreeMetric").finish_non_exhaustive() + } +} + +impl NativeTreeMetrics { + /// How many nodes this node and everything below it cover. + pub fn node_count(&self) -> usize { + 1 + self + .children + .iter() + .map(NativeTreeMetrics::node_count) + .sum::() + } +} + +impl CustomMetricValue for NativeTreeMetric { + /// A value to accumulate a combination into: it holds nothing until it combines with one. + fn new_empty(&self) -> Arc { + Arc::new(Self { + reader: None, + adopted: Mutex::new(None), + }) + } + + /// Combining takes the report of `other`, because one operator registers one tree: what a + /// combination of two registrations would mean is not defined, and cannot happen today. + fn aggregate(&self, other: Arc) { + let Some(other) = other.as_any().downcast_ref::() else { + return; + }; + *self + .adopted + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) = Some(other.report()); + } + + fn as_any(&self) -> &dyn std::any::Any { + self + } + + fn is_eq(&self, other: &Arc) -> bool { + other + .as_any() + .downcast_ref::() + .is_some_and(|other| other.report() == self.report()) + } } enum AsyncRecordBatchStreamAdapterState { @@ -1235,6 +1388,7 @@ mod test { plan_name: REGION_SCAN_EXEC_NAME.to_string(), level: 0, metrics: vec![("output_bytes".to_string(), 2)], + native: None, }], ..Default::default() }; @@ -1604,6 +1758,7 @@ mod test { plan_name: REGION_SCAN_EXEC_NAME.to_string(), level: 0, metrics: vec![("output_bytes".to_string(), 42)], + native: None, }], ..Default::default() }; @@ -1638,6 +1793,7 @@ mod test { plan_name: REGION_SCAN_EXEC_NAME.to_string(), level: 0, metrics: vec![("output_bytes".to_string(), 42)], + native: None, }], ..Default::default() }; @@ -1717,6 +1873,108 @@ mod test { assert_eq!(decoded.region_watermarks, metrics.region_watermarks); } + fn plan_metrics(level: usize, plan: &str, metrics: &[(&str, usize)]) -> PlanMetrics { + PlanMetrics { + plan: plan.to_string(), + plan_name: "Test".to_string(), + level, + metrics: metrics + .iter() + .map(|(label, value)| (label.to_string(), *value)) + .collect(), + native: None, + } + } + + /// A one-line operator keeps its figures on its line, rendered as before: this is the shape + /// every operator other than one that draws a subtree has always had. + #[test] + fn a_single_line_plan_keeps_its_figures_on_its_line() { + let metrics = RecordBatchMetrics { + plan_metrics: vec![ + plan_metrics(0, "CooperativeExec", &[("elapsed_compute", 1_000_000)]), + plan_metrics(1, "RegionScanExec", &[("output_bytes", 2048)]), + ], + ..Default::default() + }; + + assert_eq!( + metrics.to_string(), + " CooperativeExec metrics=[elapsed_compute: 1.0ms, ]\n RegionScanExec metrics=[output_bytes: 2.0KiB, ]\n" + ); + } + + /// An operator that draws nodes below itself keeps its own figures on its own - first - line. + /// Its subtree keeps the figures and the indentation it was rendered with, shifted to sit under + /// the operator it hangs from. + #[test] + fn a_multi_line_plan_receives_its_figures_before_the_subtree_it_drew() { + let metrics = RecordBatchMetrics { + plan_metrics: vec![ + plan_metrics( + 1, + "FusedSeriesExec: rate(x[5m])\n Window: rate, metrics=[output_values: 3]\n Input: source=0", + &[("elapsed_compute", 2_000_000)], + ), + plan_metrics(2, "RegionScanExec", &[("output_bytes", 4096)]), + ], + ..Default::default() + }; + + assert_eq!( + metrics.to_string(), + concat!( + " FusedSeriesExec: rate(x[5m]) metrics=[elapsed_compute: 2.0ms, ]\n", + " Window: rate, metrics=[output_values: 3]\n", + " Input: source=0\n", + " RegionScanExec metrics=[output_bytes: 4.0KiB, ]\n", + ) + ); + } + + /// A record written before the native tree was carried here still reads, and a record without + /// one does not grow a field: the payload is optional on the wire in both directions. + #[test] + fn a_plan_record_without_a_native_tree_reads_and_writes_no_field() { + let older: PlanMetrics = serde_json::from_str( + r#"{"plan":"CooperativeExec","plan_name":"CooperativeExec","level":0,"metrics":[]}"#, + ) + .expect("a record written before the field existed reads"); + + assert_eq!(older.native, None); + let written = serde_json::to_value(&older).unwrap(); + assert!(written.get("native").is_none()); + } + + /// A tree crosses the record as structure: each node's line, its own figures, and its children. + #[test] + fn a_native_tree_round_trips_through_the_record() { + let tree = NativeTreeMetrics { + line: "Map: abs".to_string(), + metrics: vec![("output_values".to_string(), 4)], + children: vec![NativeTreeMetrics { + line: "Input: raw samples source=0".to_string(), + metrics: vec![], + children: vec![], + }], + }; + let record = RecordBatchMetrics { + plan_metrics: vec![PlanMetrics { + plan: "FusedSeriesExec: abs(rate(x[5m]))".to_string(), + plan_name: "FusedSeriesExec".to_string(), + level: 0, + metrics: vec![], + native: Some(tree.clone()), + }], + ..Default::default() + }; + + let written = serde_json::to_string(&record).unwrap(); + let read: RecordBatchMetrics = serde_json::from_str(&written).unwrap(); + + assert_eq!(read.plan_metrics[0].native, Some(tree)); + } + #[test] fn test_recordbatch_metrics_skips_empty_region_watermarks_on_serialize() { let value = serde_json::to_value(RecordBatchMetrics::default()).unwrap(); diff --git a/src/promql/Cargo.toml b/src/promql/Cargo.toml index 21870efb8fa..83b15818c0a 100644 --- a/src/promql/Cargo.toml +++ b/src/promql/Cargo.toml @@ -18,6 +18,7 @@ bytemuck.workspace = true common-error.workspace = true common-macro.workspace = true common-query.workspace = true +common-recordbatch.workspace = true common-runtime.workspace = true common-telemetry.workspace = true datafusion.workspace = true @@ -42,7 +43,6 @@ twox-hash.workspace = true [dev-dependencies] api.workspace = true -common-recordbatch.workspace = true criterion.workspace = true datafusion-common.workspace = true datafusion-expr.workspace = true diff --git a/src/promql/src/extension_plan/fused_series.rs b/src/promql/src/extension_plan/fused_series.rs index 5226f5d0469..c9ba686b962 100644 --- a/src/promql/src/extension_plan/fused_series.rs +++ b/src/promql/src/extension_plan/fused_series.rs @@ -14,17 +14,14 @@ //! One node that runs a compiled native tree over raw series. //! -//! [`FusedSeries`] is the logical half: it holds the PromQL text the tree was compiled from, the -//! tree itself and the binding its input was compiled against. [`FusedSeriesExec`] is the physical -//! half: it owns one execution of that tree per partition and decodes its input frames into raw -//! series chunks and converts its evaluated chunks back into batches. +//! [`FusedSeries`] is the logical half: it holds the compiled tree and the binding its input was +//! compiled against. [`FusedSeriesExec`] is the physical half: it owns one execution of that tree +//! per partition, decodes its input frames into raw series chunks, and converts evaluated chunks +//! back into batches. //! -//! The two halves show different things in `EXPLAIN`, which is the point of having both. The -//! logical node prints the query a reader wrote ([`FusedSeries`]'s `fmt_for_explain`), and the -//! physical node prints the tree it will run ([`FusedSeriesExec`]'s `fmt_as`, which embeds -//! [`NativeTree`]'s own display rather than a second renderer). A reader comparing the two plans -//! can therefore see whether a query was compiled the way they expected without decoding node -//! names. +//! Both halves show their own compiled tree in `EXPLAIN`, rendered by [`NativeTree`]'s display. +//! The logical node shows its tree without metrics; the physical node includes each tree node's +//! recorded metrics when available. //! //! Scope of this layer: the input is a stream of native raw series frames //! ([`raw_series_frame_schema`](datatypes::series::frame::raw_series_frame_schema)) — one row per @@ -57,6 +54,7 @@ use common_query::series_frame::{ frame_schema_with, pack_aggregate_state, pack_evaluated_series, series_frame_ordering, unpack_aggregate_state, unpack_evaluated_series, unpack_raw_series, }; +use common_recordbatch::adapter::{NATIVE_TREE_METRIC, NativeTreeMetric, NativeTreeMetrics}; use datafusion::arrow::array::{ArrayRef, Float64Array, StringArray, TimestampMillisecondArray}; use datafusion::arrow::buffer::ScalarBuffer; use datafusion::arrow::datatypes::{DataType, SchemaRef, TimeUnit}; @@ -70,7 +68,9 @@ use datafusion::logical_expr::{Expr, LogicalPlan, UserDefinedLogicalNodeCore}; use datafusion::physical_expr::{EquivalenceProperties, PhysicalExpr}; use datafusion::physical_plan::coop::CooperativeExec; use datafusion::physical_plan::execution_plan::{Boundedness, EmissionType}; -use datafusion::physical_plan::metrics::{ExecutionPlanMetricsSet, MetricsSet}; +use datafusion::physical_plan::metrics::{ + ExecutionPlanMetricsSet, MetricBuilder, MetricValue, MetricsSet, +}; use datafusion::physical_plan::stream::RecordBatchStreamAdapter; use datafusion::physical_plan::{ DisplayAs, DisplayFormatType, Distribution, ExecutionPlan, InputDistributionRequirements, @@ -96,6 +96,7 @@ use crate::extension_plan::{RawSeriesSourceExec, SeriesRouting}; use crate::grid_series::METADATA_LABELS; use crate::histogram_eval::HISTOGRAM_ROUTING_EXCLUDED_LABELS; use crate::native::execute::{InputId, NativeExecutionState, NativeExecutor}; +use crate::native::metrics::{NodeMetricsSnapshot, is_node_metric}; use crate::native::plan::{ AggregateStage, BoundMap, Grouping, InstantValue, MapOp, NativeExpr, NativeNode, NativeTree, NativeType, reads_across_series, @@ -306,13 +307,10 @@ fn series_frame_output_schema(schema: &SeriesSchema) -> DataFusionResult, /// The compiled tree to run. tree: NativeTree, /// What the tree's input was compiled against. @@ -367,7 +365,6 @@ struct FusedSeriesPayload { time_index: String, tags: Vec, format: FusedOutputFormat, - promql: Arc, output_schema: datafusion::arrow::datatypes::Schema, /// Whether this node is the partition-local producer of a canonical merge; part of the node's /// identity, not of where it may be placed. @@ -377,8 +374,7 @@ struct FusedSeriesPayload { impl FusedSeries { /// Serializes the native tree and the binding required to reconstruct this plan node. - /// The PromQL text is included for diagnostics only and never drives execution; children - /// remain ordinary Substrait inputs. + /// Children remain ordinary Substrait inputs. /// /// This is only the node's own encoding. Whether a planner is allowed to place this node on /// another machine is a separate decision, made by the planner and the transport, not here. @@ -393,7 +389,6 @@ impl FusedSeries { time_index: self.binding.time_index.clone(), tags: self.binding.tags.clone(), format: self.format, - promql: Arc::clone(&self.promql), output_schema: self.output_schema.as_arrow().clone(), canonical_producer: self.canonical_producer, }; @@ -450,7 +445,6 @@ impl FusedSeries { time_index, tags, format, - promql, output_schema, canonical_producer: _, } = payload; @@ -478,8 +472,7 @@ impl FusedSeries { } let output_schema = Arc::new(datafusion::common::DFSchema::try_from(output_schema)?); let canonical_producer = payload.canonical_producer; - let mut node = - Self::try_new_with_format(promql, tree, binding, children, output_schema, format)?; + let mut node = Self::try_new_with_format(tree, binding, children, output_schema, format)?; if canonical_producer { if !matches!(node.tree.root().expr, NativeExpr::Merge { .. }) { return Err(DataFusionError::Plan( @@ -509,14 +502,12 @@ impl FusedSeries { /// Creates the node, checking that `tree` is legal against `binding`. pub fn try_new( - promql: Arc, tree: NativeTree, binding: FusedSeriesBinding, children: Vec, output_schema: DFSchemaRef, ) -> DataFusionResult { Self::try_new_with_format( - promql, tree, binding, children, @@ -532,7 +523,6 @@ impl FusedSeries { /// state frame the tree does not produce, which `try_new_with_format` would then only catch for /// some of its columns. pub fn try_new_state_export( - promql: Arc, tree: NativeTree, binding: FusedSeriesBinding, children: Vec, @@ -544,7 +534,6 @@ impl FusedSeries { )) })?; Self::try_new_with_format( - promql, tree, binding, children, @@ -583,7 +572,6 @@ impl FusedSeries { /// Creates a node with an explicitly selected output format. pub fn try_new_with_format( - promql: Arc, tree: NativeTree, binding: FusedSeriesBinding, children: Vec, @@ -618,7 +606,6 @@ impl FusedSeries { } } Ok(Self { - promql, tree, binding, children, @@ -640,7 +627,6 @@ impl FusedSeries { /// `output_schema` is the frame the evaluated root declares; it is derived here rather than /// taken from the caller so the two cannot disagree. pub fn try_new_canonical_producer( - promql: Arc, tree: NativeTree, binding: FusedSeriesBinding, children: Vec, @@ -656,7 +642,6 @@ impl FusedSeries { frame.as_ref().clone(), )?); Ok(Self { - promql, tree, binding, children, @@ -678,11 +663,6 @@ impl FusedSeries { self.canonical_producer } - /// The PromQL text this node was compiled from. - pub fn promql(&self) -> &str { - &self.promql - } - /// The selected output representation. pub fn format(&self) -> FusedOutputFormat { self.format @@ -981,7 +961,6 @@ impl FusedSeries { }) .collect::>>()?; let exec: Arc = Arc::new(FusedSeriesExec::build( - Arc::clone(&self.promql), inputs, Arc::new(self.tree.clone()), output_schema, @@ -1098,23 +1077,29 @@ impl UserDefinedLogicalNodeCore for FusedSeries { } fn fmt_for_explain(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "FusedSeries")?; + let mut has_params = false; match self.format { - FusedOutputFormat::SeriesFrame if self.canonical_producer => write!( - f, - "FusedSeries: {} (output: series_frame, per-partition canonical merge)", - self.promql - )?, - FusedOutputFormat::SeriesFrame => { - write!(f, "FusedSeries: {} (output: series_frame)", self.promql)? + FusedOutputFormat::SeriesFrame if self.canonical_producer => { + write!(f, ": (output: series_frame, per-partition canonical merge)")?; + has_params = true; } - FusedOutputFormat::PlainRows => write!(f, "FusedSeries: {}", self.promql)?, + FusedOutputFormat::SeriesFrame => { + write!(f, ": (output: series_frame)")?; + has_params = true; + } + FusedOutputFormat::PlainRows => {} } if let Some((_, SeriesRouting::CanonicalLabelsExcept(excluded))) = state_routing(self.tree.root()) { - write!(f, " (state routing: canonical labels except {excluded:?})")?; + let separator = if has_params { " " } else { ": " }; + write!( + f, + "{separator}(state routing: canonical labels except {excluded:?})" + )?; } - Ok(()) + write!(f, "\nnative_tree=[\n{}\n]", self.tree) } fn with_exprs_and_inputs( @@ -1124,7 +1109,6 @@ impl UserDefinedLogicalNodeCore for FusedSeries { ) -> DataFusionResult { validate_binding(&self.tree, &self.binding, &inputs)?; Ok(Self { - promql: Arc::clone(&self.promql), tree: self.tree.clone(), binding: self.binding.clone(), children: inputs, @@ -1137,8 +1121,7 @@ impl UserDefinedLogicalNodeCore for FusedSeries { impl PartialEq for FusedSeries { fn eq(&self, other: &Self) -> bool { - self.promql == other.promql - && self.tree == other.tree + self.tree == other.tree && self.binding == other.binding && self.children == other.children && self.output_schema == other.output_schema @@ -1151,7 +1134,6 @@ impl Eq for FusedSeries {} impl Hash for FusedSeries { fn hash(&self, state: &mut H) { - self.promql.hash(state); self.binding.hash(state); self.children.hash(state); self.output_schema.hash(state); @@ -1163,10 +1145,6 @@ impl Hash for FusedSeries { impl PartialOrd for FusedSeries { fn partial_cmp(&self, other: &Self) -> Option { - match self.promql.partial_cmp(&other.promql) { - Some(Ordering::Equal) => {} - ord => return ord, - } match self.binding.partial_cmp(&other.binding) { Some(Ordering::Equal) => {} ord => return ord, @@ -2037,8 +2015,6 @@ fn claim_evidence( /// The physical node that runs a compiled native tree over its input's raw series. pub struct FusedSeriesExec { - /// The PromQL text the tree was compiled from, which is what the plan line shows. - promql: Arc, /// The tree this node runs, shared by every partition's execution. tree: Arc, /// Each source's compiled raw schema, used to decode only that source's frames. @@ -2057,8 +2033,11 @@ pub struct FusedSeriesExec { output_schema: SchemaRef, /// The evidence claimed by this plan's output partitions. duplicate_evidence: Mutex, - /// Per-node metrics, registered by each execution of the tree. - metric: ExecutionPlanMetricsSet, + /// Per-node metrics, registered by each execution of the tree, and the tree itself. + /// + /// Shared with the tree's own registration, which reads these figures when a reader asks for + /// the tree: that reader outlives this borrow, and must never keep the set alive. + metric: Arc, format: FusedOutputFormat, matching_parallelism: Option, /// Whether this node is a partition-local canonical producer. @@ -2067,6 +2046,25 @@ pub struct FusedSeriesExec { properties: Arc, } +/// Registers the tree `tree` runs as one of the metrics `metric` holds. +/// +/// What is registered reads the tree when it is asked for, because a node's figures are only final +/// once the tree ran, and `ANALYZE` reads them after it did. The reader holds `metric` weakly: the +/// set holds the reader, so a strong handle would keep the set alive for as long as the reader. +fn register_tree_metric(tree: &Arc, metric: &Arc) { + let tree = Arc::clone(tree); + let figures = Arc::downgrade(metric); + MetricBuilder::new(metric).build(MetricValue::Custom { + name: NATIVE_TREE_METRIC.into(), + value: Arc::new(NativeTreeMetric::new(move || { + let Some(figures) = figures.upgrade() else { + return NativeTreeMetrics::default(); + }; + NativeTreeMetrics::from(tree.report(&NodeMetricsSnapshot::read(&figures))) + })), + }); +} + /// The label facts a per-series scan states about the series below a tree. /// /// A `PrimaryKey` binding names one metric and lists the tags it binds, so every stored series under @@ -2377,12 +2375,11 @@ impl FusedSeriesExec { /// [`RawLabelSource::Explicit`]. `time_column` and `tags` name the columns of the output, which /// is a row table rather than a frame. /// - /// The parameters stay separate because each is a fact its own caller has: the query, the plan + /// The parameters stay separate because each is a fact its own caller has: the plan /// below, the frame's label binding and ordering, the tree, and the three things the output /// schema is classified by. A struct over them would be the same list under another name. #[allow(clippy::too_many_arguments)] pub fn try_new( - promql: Arc, inputs: Vec<(Arc, RawLabelSource, SeriesOrdering)>, tree: Arc, output_schema: SchemaRef, @@ -2390,7 +2387,6 @@ impl FusedSeriesExec { tags: Vec, ) -> DataFusionResult { Self::build( - promql, inputs, tree, output_schema, @@ -2404,7 +2400,6 @@ impl FusedSeriesExec { #[allow(clippy::too_many_arguments)] fn build( - promql: Arc, inputs: Vec<(Arc, RawLabelSource, SeriesOrdering)>, tree: Arc, output_schema: SchemaRef, @@ -2681,8 +2676,9 @@ impl FusedSeriesExec { input.properties().boundedness }), )); + let metric = Arc::new(ExecutionPlanMetricsSet::new()); + register_tree_metric(&tree, &metric); Ok(Self { - promql, tree, series_schemas: raw_schemas, source_kinds, @@ -2695,7 +2691,7 @@ impl FusedSeriesExec { evidence: None, claimed: Vec::new(), }), - metric: ExecutionPlanMetricsSet::new(), + metric, properties, format, matching_parallelism, @@ -2707,6 +2703,20 @@ impl FusedSeriesExec { pub const fn name() -> &'static str { "FusedSeriesExec" } + + /// The figures the tree this node runs recorded, keyed by the path each node registered under. + fn node_metrics(&self) -> NodeMetricsSnapshot { + NodeMetricsSnapshot::read(&self.metric) + } + + /// Every registration this node's tree made, exactly as it was registered. + /// + /// [`Self::metrics`] leaves these out because a flat set cannot say which node recorded what; + /// this is the same set unfiltered, for a reader that keys by name - the names are + /// `native..` (see [`crate::native::metrics`]). + pub fn node_metric_set(&self) -> MetricsSet { + self.metric.clone_inner() + } } #[cfg(feature = "test-utils")] @@ -2722,14 +2732,23 @@ impl std::fmt::Debug for FusedSeriesExec { } impl DisplayAs for FusedSeriesExec { - /// The query, then the tree it compiles to: the tree is rendered by [`NativeTree`]'s own - /// `Display`, so `EXPLAIN` and the node's metrics name the same nodes. + /// The compiled tree, one line per node. + /// + /// Each node's own figures are read from the metrics this node registered, so a statement that + /// never ran - every plain `EXPLAIN`, verbose included - draws the tree without them. That is + /// the whole difference between `EXPLAIN` and `ANALYZE` here: nothing switches it on. fn fmt_as(&self, _t: DisplayFormatType, f: &mut Formatter) -> fmt::Result { - write!(f, "FusedSeriesExec: {}\n{}", self.promql, self.tree)?; + write!(f, "FusedSeriesExec")?; + // On the operator's own line, where the logical node also puts it: a second line would read + // as another node beside the tree this one draws. if self.format == FusedOutputFormat::SeriesFrame { - write!(f, "\noutput: series_frame")?; + write!(f, ": (output: series_frame)")?; } - Ok(()) + write!( + f, + "\nnative_tree=[\n{}\n]", + self.tree.display_with(&self.node_metrics()) + ) } } @@ -2738,8 +2757,24 @@ impl ExecutionPlan for FusedSeriesExec { Self::name() } + /// The metrics this node records itself, which is none of its tree's. + /// + /// A [`MetricsSet`] is a flat, name-keyed bag: reporting the tree's figures through it would + /// attribute every node's figures to this node, because the reader cannot tell them apart. Each + /// node reports its own on its own line instead - and, as data, through the tree this node + /// registers as [`NativeTreeMetric`] - so they are left out here and this node reports only what + /// an operator of its own would add. fn metrics(&self) -> Option { - Some(self.metric.clone_inner()) + let mut own = MetricsSet::new(); + for metric in self.metric.clone_inner().iter() { + // The tree is not a figure of this node: it is what the node runs, reported on its own + // (see `NativeTreeMetric`), so it stays here rather than being flattened into a number. + let tree = matches!(metric.value(), MetricValue::Custom { .. }); + if tree || !is_node_metric(metric.value().name()) { + own.push(Arc::clone(metric)); + } + } + Some(own) } fn properties(&self) -> &Arc { @@ -2796,7 +2831,6 @@ impl ExecutionPlan for FusedSeriesExec { // Rebuilt rather than patched: the new child's schema is what the frame is validated // against, so a child that changed shape is refused instead of decoded with a stale layout. Ok(Arc::new(Self::build( - Arc::clone(&self.promql), children, Arc::clone(&self.tree), self.output_schema.clone(), diff --git a/src/promql/src/extension_plan/fused_series/tests.rs b/src/promql/src/extension_plan/fused_series/tests.rs index 20740e87025..3a90fd30bb9 100644 --- a/src/promql/src/extension_plan/fused_series/tests.rs +++ b/src/promql/src/extension_plan/fused_series/tests.rs @@ -29,7 +29,7 @@ //! second per second, so the value it has to produce is exactly 1 (or 2) at every grid point and //! any position or finalization mistake shows up as a wrong, missing or duplicated row. -use std::collections::VecDeque; +use std::collections::{BTreeSet, VecDeque}; use std::fmt::{self, Display, Formatter}; use std::pin::Pin; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; @@ -39,6 +39,7 @@ use std::task::{Context, Poll}; use api::v1::SemanticType; use common_error::ext::BoxedError; use common_query::series_frame::{pack_evaluated_series, unpack_evaluated_series}; +use common_recordbatch::adapter::{NATIVE_TREE_METRIC, format_metric}; use common_recordbatch::{ EmptyRecordBatchStream, SendableRecordBatchStream as CommonSendableRecordBatchStream, }; @@ -92,15 +93,17 @@ use table::table::scan::RegionScanExec; mod frame_output; use super::{ - FieldKind, FusedOutputFormat, FusedSeries, FusedSeriesBinding, OutputColumn, POLL_BUDGET, - series_to_record_batch, + FieldKind, FusedOutputFormat, FusedSeries, FusedSeriesBinding, FusedSeriesExec, OutputColumn, + POLL_BUDGET, series_to_record_batch, }; use crate::evaluated::EvaluatedChunk; use crate::extension_plan::raw_series_source::tests::{per_series_scan, raw_source}; use crate::extension_plan::{RawSeriesSourceExec, SeriesRepartitionExec, SeriesRouting}; use crate::histogram_eval::HistogramFunction; +use crate::info::InfoSelector; use crate::map_label::LabelRewrite; use crate::native::execute::{InputId, NativeExecutionState, NativeExecutor}; +use crate::native::metrics::{NodeMetric, NodeMetricsSnapshot}; use crate::native::plan::{ AggregateKind, AggregateStage, AggregateStateSchema, BoundAggregate, BoundInput, BoundInstant, BoundMap, BoundScalar, BoundVectorOp, BoundWindow, Grouping, InstantValue, MapOp, NativeExpr, @@ -111,9 +114,7 @@ use crate::vector_match::{BinaryMatching, BinaryOp, Cardinality, FillValues, Mat use common_query::series_frame::series_frame_ordering; use common_query::series_frame::unpack_aggregate_state; -/// The query every tree here is compiled from. -const PROMQL: &str = "rate(x[5m])"; -/// The range the statement's window selects, which is the `5m` of [`PROMQL`]. +/// The range the statement's window selects, which is the `5m` of `rate(x[5m])`. const RANGE_MS: i64 = 300_000; /// The statement's grid: `rate(x[5m])` evaluated every minute from five minutes in. const GRID_START: i64 = 300_000; @@ -880,7 +881,6 @@ fn partition_disjoint_over( label_source: RawLabelSource, ) -> bool { let node = FusedSeries::try_new( - Arc::from(PROMQL), tree.clone(), binding(&tree), vec![input_plan(frame_schema())], @@ -1199,7 +1199,6 @@ fn partition_disjoint_admits_a_label_rewrite_only_when_it_writes_a_fresh_label() fn partition_disjoint_rejects_a_multi_input_tree() { let tree = binary_tree_over_sources(BinaryOp::Add, raw_schema(), raw_schema(), 0, 1); let node = FusedSeries::try_new( - Arc::from("left + right"), tree.clone(), binding(&tree), vec![input_plan(frame_schema()), input_plan(frame_schema())], @@ -1313,7 +1312,6 @@ async fn scalar_binary_matching_key_is_not_routed_by_dropped_names() { let left_input = memory_exec_partitioned(vec![vec![frame(&left)], vec![]]); let right_input = memory_exec_partitioned(vec![vec![], vec![frame(&right)]]); let node = FusedSeries::try_new( - Arc::from("(x * 2) + on(__name__) (y * 2)"), tree.clone(), binding(&tree), vec![input_plan(frame_schema()), input_plan(frame_schema())], @@ -1932,7 +1930,6 @@ fn zone_node() -> FusedSeries { vec![HOST.to_string(), ZONE.to_string(), NAME.to_string()], ); FusedSeries::try_new( - Arc::from(PROMQL), tree, binding, vec![input_plan(frame_schema())], @@ -2302,7 +2299,6 @@ fn canonical_producer_node() -> FusedSeries { }) .unwrap(); FusedSeries::try_new_canonical_producer( - Arc::from(PROMQL), tree.clone(), binding(&tree), vec![input_plan(frame_schema())], @@ -2314,7 +2310,6 @@ fn canonical_producer_node() -> FusedSeries { fn fused_series_canonical_producer_exec_tracks_physical_routing() { let canonical = canonical_producer_node(); let ordinary = FusedSeries::try_new_with_format( - Arc::from(PROMQL), canonical.tree().clone(), canonical.binding().clone(), vec![input_plan(frame_schema())], @@ -2374,7 +2369,6 @@ fn fused_series_encoding_preserves_state_and_canonical_producer() { // capability, not a decision about where it may run. let tree = partial_tree(rate_tree(), AggregateKind::Sum, &[HOST, JOB]); let producer = FusedSeries::try_new_state_export( - Arc::from(PROMQL), tree.clone(), binding(&tree), vec![input_plan(frame_schema())], @@ -2403,7 +2397,6 @@ fn fused_series_ordinary_encoding_round_trip_and_rejects_unsupported_payloads() // `canonical_frame_schema` rather than with the plain row `output_schema`. let declared = canonical_frame_schema(&tree); let node = FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree, original_binding.clone(), vec![input_plan(frame_schema())], @@ -2430,7 +2423,6 @@ fn fused_series_ordinary_encoding_round_trip_and_rejects_unsupported_payloads() canonical.field_with_name(TIME).unwrap().clone(), ])); let reordered = FusedSeries::try_new( - Arc::from(PROMQL), rows.tree().clone(), rows.binding().clone(), rows.children().to_vec(), @@ -2446,7 +2438,6 @@ fn fused_series_ordinary_encoding_round_trip_and_rejects_unsupported_payloads() // labels after a tree operation such as label rewriting. let empty_binding = binding_of(rows.tree(), raw_schema(), Vec::new()); let empty_tags = FusedSeries::try_new( - Arc::from(PROMQL), rate_tree(), empty_binding, vec![input_plan(frame_schema())], @@ -2488,20 +2479,18 @@ fn fused_series_ordinary_encoding_round_trip_and_rejects_unsupported_payloads() assert!(FusedSeries::deserialize(&trailing).is_err()); } -/// A payload written under another version is refused by its version, before its body is read. +/// A payload marked with an unsupported version is refused before its body is read. /// -/// The body here is the current payload's body with the version field set to a number this reader -/// does not accept: it decodes cleanly under the current reader, which is exactly what makes it a -/// control. A reader that checked the version only after decoding would accept it and hand back a -/// node whose fields were read at the wrong offsets; this asserts the version is what answers, by -/// name. +/// The body here is the current payload's body with the version field set to a future number this +/// reader does not accept: it decodes cleanly under the current reader, which makes it a control. +/// A reader that checked the version only after decoding would accept it and hand back a node whose +/// fields were read at the wrong offsets; this asserts the version is what answers, by name. #[test] -fn fused_series_envelope_refuses_the_older_payload_version_before_reading_it() { +fn fused_series_envelope_refuses_an_unsupported_payload_version() { let tree = rate_tree(); let declared = canonical_frame_schema(&tree); let bound = binding(&tree); let node = FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree, bound, vec![input_plan(frame_schema())], @@ -2519,9 +2508,11 @@ fn fused_series_envelope_refuses_the_older_payload_version_before_reading_it() { // The control: the same body under the accepted version decodes. assert!(FusedSeries::deserialize(&bytes).is_ok()); - let mut old = bytes.clone(); - old[..4].copy_from_slice(&2u32.to_le_bytes()); - let message = FusedSeries::deserialize(&old).unwrap_err().to_string(); + let mut unsupported = bytes.clone(); + unsupported[..4].copy_from_slice(&2u32.to_le_bytes()); + let message = FusedSeries::deserialize(&unsupported) + .unwrap_err() + .to_string(); assert!( message.contains("version") && message.contains('2'), "the version must be what is reported, not a field read at the wrong offset: {message}" @@ -2537,7 +2528,6 @@ fn envelope_node_with_tags(tags: Vec) -> FusedSeries { let binding = binding_of(&tree, raw_schema(), tags); let output_schema = canonical_frame_schema(&tree); FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree, binding, vec![input_plan(frame_schema())], @@ -2577,7 +2567,6 @@ fn scalar_node(tree: NativeTree, value_name: &str) -> FusedSeries { Field::new(value_name, DataType::Float64, true), ])); FusedSeries::try_new( - Arc::from(PROMQL), tree, FusedSeriesBinding { grid: grid(), @@ -2665,7 +2654,6 @@ fn plain_rows_decode_rejects_invalid_schemas() { // Binding tags describe the source; the carried schema independently describes outputs. let empty_binding = binding_of(node.tree(), raw_schema(), Vec::new()); let cross_table = FusedSeries::try_new( - Arc::from(PROMQL), node.tree().clone(), empty_binding, node.children().to_vec(), @@ -2696,7 +2684,6 @@ fn a_non_default_column_order_survives_the_round_trip() { [("schema-origin".to_string(), "carried".to_string())].into(), ); let node = FusedSeries::try_new( - Arc::from(PROMQL), tree, binding, vec![input_plan(frame_schema())], @@ -2732,7 +2719,6 @@ fn series_frame_decode_rejects_schema_mismatch() { fn state_export_decode_rejects_schema_and_format_mismatch() { let tree = partial_tree(rate_tree(), AggregateKind::Sum, &[HOST, JOB]); let node = FusedSeries::try_new_state_export( - Arc::from(PROMQL), tree.clone(), binding_of(&tree, raw_schema(), vec![HOST.to_string(), JOB.to_string()]), vec![input_plan(frame_schema())], @@ -2949,7 +2935,6 @@ fn fused(tree: NativeTree) -> FusedSeries { output_schema() }; FusedSeries::try_new( - Arc::from(PROMQL), tree, binding, vec![input_plan(frame_schema())], @@ -3382,6 +3367,21 @@ fn assert_series_rates(rows: &[Row], rates: &[(f64, &str)]) { } } +/// The figures the fused node at the top of `plan` registered, exactly as the tree registered them. +fn node_metric_set(plan: &Arc) -> MetricsSet { + plan.downcast_ref::() + .expect("the plan under test is one fused node") + .node_metric_set() +} + +/// How many figures - as opposed to structure, such as the tree a node runs - `metrics` holds. +fn node_figures(metrics: &MetricsSet) -> usize { + metrics + .iter() + .filter(|metric| !matches!(metric.value(), MetricValue::Custom { .. })) + .count() +} + fn count(metrics: &MetricsSet, name: &str) -> usize { match metrics.sum_by_name(name) { Some(MetricValue::Count { count, .. }) => count.value(), @@ -3398,13 +3398,16 @@ impl Display for Explain<'_> { } } -/// The logical node prints the query a reader wrote, not the tree it compiled to. +/// The logical node prints the compiled tree without metrics. #[test] -fn fmt_for_explain_prints_the_promql() { - assert_eq!( - "FusedSeries: rate(x[5m])", - Explain(&fused(rate_tree())).to_string() +fn fmt_for_explain_prints_the_native_tree() { + let text = Explain(&fused(rate_tree())).to_string(); + assert!( + text.starts_with("FusedSeries\nnative_tree=[\nWindow: rate [300000ms]"), + "{text}" ); + assert!(text.contains("\n Input: raw samples source=0"), "{text}"); + assert!(!text.contains("metrics="), "{text}"); } /// The physical node prints the tree it was built from, rendered by the tree's own display. @@ -3413,22 +3416,397 @@ fn fmt_as_prints_the_native_tree() { let exec = plan(rate_tree(), memory_exec(Vec::new())).expect("the runtime executes a rate tree"); let text = displayable(exec.as_ref()).indent(false).to_string(); + assert!(!text.contains("metrics="), "{text}"); assert!( - text.starts_with("FusedSeriesExec: rate(x[5m])"), - "the node line names the query first: {text}" + text.starts_with("FusedSeriesExec\nnative_tree=[\nWindow: rate"), + "the node line precedes its tree: {text}" ); // A distinctive line of the compiled tree: the window's bound range, offset, anchor and grid. assert!( - text.contains("root Window rate [300000ms] offset 0 anchor - on grid 300000/60000x5"), + text.contains("\nWindow: rate [300000ms] offset 0 anchor - on grid 300000/60000x5"), "the node line carries the tree it runs: {text}" ); assert!( - text.contains("input Input raw samples"), + text.contains("\n Input: raw samples"), "the node line carries the tree's source: {text}" ); } +/// Physical ANALYZE renders the figures registered on each tree node on that node's own line. +#[tokio::test] +async fn fmt_as_reports_node_metrics_after_execution() { + let exec = plan(rate_tree(), memory_exec(two_series_frames())).unwrap(); + collect(Arc::clone(&exec), SessionContext::default().task_ctx()) + .await + .unwrap(); + let text = displayable(exec.as_ref()).indent(false).to_string(); + assert!( + text.starts_with("FusedSeriesExec\nnative_tree=[\nWindow: rate"), + "{text}" + ); + // Each node carries its own figures. A figure anywhere in the text says nothing about which + // node recorded it: the window's and the source's are found on their own lines. + let window = tree_line(&text, "Window: rate"); + assert!( + window.contains(", metrics=["), + "the window's line carries no figures of its own: {window}\n{text}" + ); + assert!(window.contains("output_values:"), "{window}\n{text}"); + let input = tree_line(&text, "Input: raw samples source=0"); + assert!( + input.contains(", metrics=["), + "the source's line carries no figures of its own: {input}\n{text}" + ); + assert!(input.contains("input_batches:"), "{input}\n{text}"); +} + +/// The one line of the tree block that starts with `prefix`, without its indentation. +fn tree_line<'a>(text: &'a str, prefix: &str) -> &'a str { + text.lines() + .map(str::trim_start) + .find(|line| line.starts_with(prefix)) + .unwrap_or_else(|| panic!("no line starts with {prefix:?}: {text}")) +} + +/// The figures of the tree a node runs are the tree's, not the node's own. +/// +/// [`ExecutionPlan::metrics`] is a flat, name-keyed bag, so reporting the tree's figures through it +/// would attribute every node's figures to this one node. What this node reports is therefore only +/// what an operator of its own adds, and the tree - which reads as structure, not as a figure - is +/// still there to be read. +#[tokio::test] +async fn metrics_reports_no_figure_of_the_tree_it_runs() { + let exec = plan(rate_tree(), memory_exec(two_series_frames())) + .expect("the runtime executes a rate tree"); + collect(Arc::clone(&exec), SessionContext::default().task_ctx()) + .await + .expect("the node runs"); + + let reported = exec + .metrics() + .expect("the node reports what it records itself"); + let names = reported + .iter() + .map(|metric| metric.value().name().to_string()) + .collect::>(); + // The tree is reported as itself rather than flattened into a number: it is the one entry a + // node keeps of the tree it runs, and the assertion below is about the figures it leaves out. + assert!( + names.iter().any(|name| name == NATIVE_TREE_METRIC), + "the node reports no tree at all: {names:?}" + ); + let own = names + .iter() + .filter(|name| is_node_figure(name)) + .collect::>(); + assert!( + own.is_empty(), + "the node reports the figures of the tree it runs as its own: {own:?}" + ); + + // The same run's figures are registered under the path of the node that recorded each one, so + // what `metrics()` leaves out has not been dropped: it has moved to the set that keys by path. + let registered = node_metric_set(&exec); + let registered_names = registered + .iter() + .map(|metric| metric.value().name().to_string()) + .collect::>(); + let tree_figures = registered_names + .iter() + .filter(|name| is_node_figure(name)) + .count(); + assert!( + tree_figures > 0, + "the run registered no figure under a node's path: {registered_names:?}" + ); + assert!( + registered_names + .iter() + .any(|name| name == "native.root.output_values"), + "{registered_names:?}" + ); + assert!( + registered_names + .iter() + .any(|name| name == "native.root.input.output_values"), + "{registered_names:?}" + ); + assert!( + count(®istered, "native.root.output_values") > 0, + "the run recorded no output for the window" + ); +} + +/// Whether `name` is one of the figures a node of a tree registered, `native..`. +/// +/// The tree an operator runs is registered under [`NATIVE_TREE_METRIC`], which shares the prefix +/// but names no node and reports no figure. +fn is_node_figure(name: &str) -> bool { + NodeMetric::ALL.iter().any(|metric| { + name.strip_suffix(metric) + .and_then(|head| head.strip_suffix('.')) + .is_some_and(|path| { + path.strip_prefix("native.") + .is_some_and(|path| !path.is_empty()) + }) + }) +} + +/// An instant selection over `source_index` that reads `lookback_ms` back and reports `value`. +fn instant_selecting( + source_index: usize, + grid: &Arc, + lookback_ms: i64, + value: InstantValue, +) -> NativeNode { + let raw = NativeType::Samples(raw_schema()); + NativeNode { + expr: NativeExpr::Instant { + input: Box::new(NativeNode { + expr: NativeExpr::Input(BoundInput { + source_index, + output: raw.clone(), + ordering: SeriesOrdering::Contiguous, + }), + output: raw, + }), + op: BoundInstant { + lookback_ms, + offset_ms: 0, + at_ms: None, + value, + output_grid: Arc::clone(grid), + }, + }, + output: evaluated_of(&raw_schema(), grid), + } +} + +/// `info(up, {version}) + clamp_min(up, scalar(threshold))`. +/// +/// The sum's two sides are an info join and a parameterized map, so the tree holds every segment +/// the display and the runtime name by hand: `left` and `right` twice, and `parameter0` once. Its +/// four sources are `up` for the join, the info series that join reads, `up` again for the map, and +/// the `threshold` the map is parameterized by. +fn info_and_parameterized_tree() -> NativeTree { + let grid = grid(); + let evaluated = evaluated_of(&raw_schema(), &grid); + let enriched = NativeNode { + expr: NativeExpr::Info { + base: Box::new(instant_selecting(0, &grid, 0, InstantValue::Sample)), + // The kernel reads the info side's values as sample times, so the selection reports + // those times rather than the values it selected. + info: Box::new(instant_selecting( + 1, + &grid, + RANGE_MS, + InstantValue::InfoTimestamp, + )), + selector: InfoSelector { + name_matchers: Vec::new(), + data_labels: vec![Arc::from("version")], + keep_unmatched: false, + }, + }, + output: evaluated.clone(), + }; + let clamped = NativeNode { + expr: NativeExpr::ParameterizedMap { + input: Box::new(instant_selecting(2, &grid, 0, InstantValue::Sample)), + op: ParameterizedOp::ClampMin, + parameters: vec![NativeNode { + expr: NativeExpr::ScalarOf { + input: Box::new(instant_selecting(3, &grid, 0, InstantValue::Sample)), + }, + output: NativeType::Scalar(Arc::clone(&grid)), + }], + }, + output: evaluated.clone(), + }; + let NativeType::Samples(schema) = &evaluated else { + panic!("an evaluated vector is samples"); + }; + let output = crate::vector_match::binary_output_schema(schema, schema) + .expect("two operands on one grid are matched"); + NativeTree::try_new(NativeNode { + expr: NativeExpr::Binary { + left: Box::new(enriched), + right: Box::new(clamped), + matching: BinaryMatching { + op: BinaryOp::Add, + return_bool: false, + labels: MatchingLabels::On(vec![Arc::from(HOST)]), + cardinality: Cardinality::OneToOne, + fill: FillValues::default(), + }, + }, + output: NativeType::Samples(output), + }) + .expect("an info join and a parameterized map are legal operands of a sum") +} + +/// One chunk of the series `name` under `labels`, keyed `key`: `rows` as its samples, and the chunk +/// that closes the series when `last`. +fn named_chunk( + name: &str, + key: &str, + labels: &[(&str, &str)], + rows: &[(i64, f64)], + last: bool, +) -> RawSeriesChunk { + chunk_of( + &raw_schema(), + key, + metric(labels, Some(name)), + ×_of(rows), + vec![values_of(rows)], + last, + ) +} + +/// The node paths `figures` was registered under, read off the names `native..`. +/// +/// A figure that must not be merged across partitions carries its partition in its name, and a +/// partition belongs to the registration rather than to the node, so it is dropped here. The +/// tree's own record is not a figure of any node, so it names no path. +fn registered_paths(figures: &MetricsSet) -> BTreeSet { + figures + .iter() + .filter_map(|metric| { + let name = metric.value().name(); + let rest = name.strip_prefix("native.")?; + let rest = match rest.split_once('.') { + Some((head, tail)) + if head.len() > 1 + && head.starts_with('p') + && head[1..].bytes().all(|byte| byte.is_ascii_digit()) => + { + tail + } + _ => rest, + }; + let (path, _) = rest.rsplit_once('.')?; + Some(path.to_string()) + }) + .collect() +} + +/// Every node of an executed tree shows its own figures, whichever side of a pair or parameter it +/// sits on. +/// +/// The display names a node's child by the segment the runtime registered it under, and the two +/// lists are kept by hand: `left` and `right` for the sides of a pair, `parameter0` for the +/// parameter of a parameterized map. A display that names a child where nothing was registered +/// loses that node's figures without failing anything else - what it prints is still a tree, and +/// the run still records what it recorded - so what this test pins is that no path the run filed +/// figures under goes unshown. +#[tokio::test] +async fn every_node_of_an_executed_tree_reaches_its_own_figures() { + let tree = info_and_parameterized_tree(); + let up = named_chunk( + METRIC, + "web-1", + &[(HOST, "web-1"), ("instance", "a")], + &series_samples(1.0), + true, + ); + // The info series the join takes `version` from, sampled where the grid reads it. + let info = named_chunk( + "target_info", + "info", + &[(HOST, "web-1"), ("instance", "a"), ("version", "v1")], + &[(GRID_START, 1.0)], + true, + ); + let threshold = named_chunk( + METRIC, + "web-1", + &[(HOST, "web-1"), ("instance", "a")], + &grid_timestamps() + .into_iter() + .map(|time| (time, 2.0)) + .collect::>(), + true, + ); + let exec = FusedSeries::try_new( + tree.clone(), + binding(&tree), + (0..4).map(|_| input_plan(frame_schema())).collect(), + binary_output_schema().to_dfschema_ref().unwrap(), + ) + .expect("the binding matches the tree") + .to_execution_plan( + [frame(&up), frame(&info), frame(&up), frame(&threshold)] + .into_iter() + .map(|batch| { + ( + memory_exec(vec![batch]), + RawLabelSource::Explicit, + SeriesOrdering::Contiguous, + ) + }) + .collect(), + ) + .expect("the tree runs"); + collect(Arc::clone(&exec), SessionContext::default().task_ctx()) + .await + .expect("the node runs"); + + // What the run filed, keyed by the path of the node that recorded it. Both sides of the sum + // and the parameter of the map are among the paths, so the tree below is the one described. + let figures = node_metric_set(&exec); + let paths = registered_paths(&figures); + for path in [ + "root.left", + "root.left.left", + "root.left.right", + "root.right", + "root.right.input", + "root.right.parameter0", + ] { + assert!( + paths.contains(path), + "the run filed nothing under {path}: {paths:?}" + ); + } + + // The displayed tree, one line per node. + let text = displayable(exec.as_ref()).indent(false).to_string(); + let lines = text + .lines() + .skip_while(|line| *line != "native_tree=[") + .skip(1) + .take_while(|line| *line != "]") + .collect::>(); + assert_eq!( + paths.len(), + lines.len(), + "the display prints {} nodes where {} recorded figures:\n{text}", + lines.len(), + paths.len() + ); + // Each line shows the figures of the path that node was registered under - its own, and only + // its own - so a node whose figures the display never reached has no line that shows them. + let registered: ExecutionPlanMetricsSet = figures.into(); + let snapshot = NodeMetricsSnapshot::read(®istered); + for path in &paths { + let described = snapshot.described(path); + assert!(!described.is_empty(), "{path} recorded no figure at all"); + let shown = described + .iter() + .map(|(name, value)| format_metric(name, *value)) + .collect::>() + .join(", "); + assert!( + lines + .iter() + .any(|line| line.ends_with(&format!("metrics=[{shown}]"))), + "no node line shows what {path} recorded ({shown}):\n{text}" + ); + } +} + /// `rate(x[5m])` runs end to end over two raw series: values, tags and the dropped metric name. #[tokio::test] async fn runs_rate_over_two_series() { @@ -3487,9 +3865,7 @@ async fn a_series_spanning_chunks_keeps_absolute_positions_and_closes_once() { // The source closed the series on exactly one chunk, and the window saw exactly that one // closing: one `is_last`, not one per input batch. - let metrics = exec - .metrics() - .expect("the node reports its runtime's metrics"); + let metrics = node_metric_set(&exec); assert_eq!(1, count(&metrics, "native.root.input.completed_series")); assert_eq!(1, count(&metrics, "native.root.completed_series")); } @@ -3523,7 +3899,6 @@ fn a_tree_bound_to_another_grid_is_rejected() { ..binding(&tree) }; let error = FusedSeries::try_new( - Arc::from(PROMQL), tree, binding, vec![input_plan(frame_schema())], @@ -3554,7 +3929,6 @@ fn a_separately_allocated_grid_with_an_equal_spec_is_accepted() { ..binding(&tree) }; FusedSeries::try_new( - Arc::from(PROMQL), tree, binding, vec![input_plan(frame_schema())], @@ -3575,7 +3949,6 @@ fn scalar_logical_node_has_no_children_and_refuses_a_child() { tags: vec![HOST.to_string()], }; let node = FusedSeries::try_new( - Arc::from("vector(7)"), tree.clone(), binding.clone(), Vec::new(), @@ -3586,7 +3959,6 @@ fn scalar_logical_node_has_no_children_and_refuses_a_child() { assert!(node.inputs().is_empty()); assert!( FusedSeries::try_new( - Arc::from("vector(7)"), tree, binding, vec![input_plan(frame_schema())], @@ -3604,7 +3976,6 @@ fn logical_children_and_inputs_roundtrip_in_source_order() { let left = input_plan_with_rows(frame_schema(), false); let right = input_plan_with_rows(frame_schema(), true); let node = FusedSeries::try_new( - Arc::from("left + right"), tree, binding(&rate_tree()), vec![left.clone(), right.clone()], @@ -3672,7 +4043,6 @@ fn logical_source_indexes_must_match_dense_children() { ] { assert!( FusedSeries::try_new( - Arc::from(name), tree.clone(), binding(&tree), children, @@ -3691,7 +4061,6 @@ fn repeated_source_bindings_validate_fields_and_second_ordering() { binary_tree_over_sources(BinaryOp::Add, raw_schema(), two_field_schema(), 0, 0); assert!( FusedSeries::try_new( - Arc::from("x + x"), fields_mismatch.clone(), binding(&fields_mismatch), vec![input_plan(frame_schema())], @@ -3847,7 +4216,6 @@ fn fragmented_raw_multi_input_is_reconciled_before_matching_router() { use common_query::series_frame::frame_schema_with; let tree = binary_tree_over_sources(BinaryOp::Add, raw_schema(), raw_schema(), 0, 1); let node = FusedSeries::try_new( - Arc::from("left + right"), tree.clone(), binding(&tree), vec![input_plan(frame_schema()), input_plan(frame_schema())], @@ -3905,7 +4273,6 @@ fn fragmented_raw_multi_input_is_reconciled_before_matching_router() { async fn logical_two_source_tree_is_accepted_by_physical_adapter() { let tree = binary_tree_over_sources(BinaryOp::Add, raw_schema(), raw_schema(), 0, 1); let node = FusedSeries::try_new( - Arc::from("left + right"), tree.clone(), binding(&tree), vec![input_plan(frame_schema()), input_plan(frame_schema())], @@ -3985,7 +4352,6 @@ fn binary_identity_rows(batches: &[RecordBatch]) -> Vec<(Option, i64, Op async fn matching_parallelism_uses_configured_width_without_changing_rows() { let tree = binary_tree_over_sources(BinaryOp::Add, raw_schema(), raw_schema(), 0, 1); let node = FusedSeries::try_new( - Arc::from("left + right"), tree.clone(), binding(&tree), vec![input_plan(frame_schema()), input_plan(frame_schema())], @@ -4098,7 +4464,6 @@ async fn independent_sources_execute_all_partitions() { calls: Arc::clone(&right_calls), }); let node = FusedSeries::try_new( - Arc::from("left - right"), tree.clone(), binding(&tree), vec![ @@ -4171,7 +4536,6 @@ async fn both_inputs_are_read_concurrently() { } }); let node = FusedSeries::try_new( - Arc::from("left - right"), tree.clone(), binding(&tree), vec![input_plan(frame_schema()), input_plan(frame_schema())], @@ -4208,7 +4572,6 @@ async fn both_inputs_are_read_concurrently() { async fn binary_output_tags_are_projected_but_raw_binding_is_full() { let tree = binary_tree_over_sources(BinaryOp::Sub, raw_schema(), raw_schema(), 0, 1); let node = FusedSeries::try_new( - Arc::from("left - right"), tree.clone(), binding(&tree), vec![input_plan(frame_schema()), input_plan(frame_schema())], @@ -4253,7 +4616,6 @@ async fn binary_output_tags_are_projected_but_raw_binding_is_full() { async fn empty_first_source_does_not_end_gather_early() { let tree = binary_tree_over_sources(BinaryOp::Sub, raw_schema(), raw_schema(), 0, 1); let node = FusedSeries::try_new( - Arc::from("left - right"), tree.clone(), binding(&tree), vec![input_plan(frame_schema()), input_plan(frame_schema())], @@ -4289,7 +4651,6 @@ fn multiparty_gather_rejects_key_then_time_leaf_requirement() { &[SeriesOrdering::KeyThenTime, SeriesOrdering::Contiguous], ); let node = FusedSeries::try_new( - Arc::from("left + right"), tree.clone(), binding(&tree), vec![input_plan(frame_schema()), input_plan(frame_schema())], @@ -4318,7 +4679,6 @@ fn multiparty_gather_rejects_key_then_time_leaf_requirement() { async fn later_source_error_drops_all_gathered_inputs() { let tree = binary_tree_over_sources(BinaryOp::Sub, raw_schema(), raw_schema(), 0, 1); let node = FusedSeries::try_new( - Arc::from("left - right"), tree.clone(), binding(&tree), vec![input_plan(frame_schema()), input_plan(frame_schema())], @@ -4488,7 +4848,6 @@ async fn physical_plan_accepts_scalar_root_without_sources() { ), ])); let node = FusedSeries::try_new( - Arc::from("4"), tree, binding.clone(), Vec::new(), @@ -4562,7 +4921,7 @@ async fn physical_plan_accepts_scalar_root_without_sources() { drop(early_batch); assert_eq!(0, drop_pool.reserved()); assert!(exec.execute(1, task_context(&pool)).is_err()); - let metrics = exec.metrics().unwrap(); + let metrics = node_metric_set(&exec); assert_eq!(1, count(&metrics, "native.root.output_batches")); assert_eq!( GRID_LEN as usize, @@ -4617,7 +4976,6 @@ async fn physical_scalar_rejects_malformed_output_schema() { ]; for schema in schemas { let node = FusedSeries::try_new( - Arc::from("4"), tree.clone(), binding.clone(), Vec::new(), @@ -4627,7 +4985,6 @@ async fn physical_scalar_rejects_malformed_output_schema() { assert!(node.to_execution_plan(Vec::new()).is_err()); } let tagged = FusedSeries::try_new( - Arc::from("4"), tree, FusedSeriesBinding { tags: vec![HOST.to_string()], @@ -4659,14 +5016,9 @@ async fn physical_scalar_conversion_preserves_special_float_bits() { false, ), ])); - let node = FusedSeries::try_new( - Arc::from("4"), - tree, - binding, - Vec::new(), - schema.to_dfschema_ref().unwrap(), - ) - .unwrap(); + let node = + FusedSeries::try_new(tree, binding, Vec::new(), schema.to_dfschema_ref().unwrap()) + .unwrap(); let exec = node.to_execution_plan(Vec::new()).unwrap(); let batches = exec .execute(0, SessionContext::default().task_ctx()) @@ -4722,9 +5074,7 @@ async fn an_empty_input_runs_the_tree_and_yields_no_rows() { .expect("a tree with no series in it still closes"); assert!(rows(&batches).is_empty(), "{:?}", rows(&batches)); // The run closed on no series, so nothing was completed and nothing was decided. - let metrics = exec - .metrics() - .expect("the node reports its runtime's metrics"); + let metrics = node_metric_set(&exec); assert_eq!(0, count(&metrics, "native.root.completed_series")); assert_eq!(0, count(&metrics, "native.root.input.completed_series")); } @@ -5444,7 +5794,6 @@ fn unequal_binding_and_output_schema_are_not_ordered_equal() { ), ])); let right_node = FusedSeries::try_new( - Arc::from(PROMQL), tree, binding(&rate_tree()), vec![input_plan(frame_schema())], @@ -5579,108 +5928,7 @@ async fn a_later_repeated_source_enqueue_failure_releases_the_input() { assert_eq!(batches_at_failure, pulled.load(Ordering::SeqCst)); } -/// A single-consumer enqueue failure is reported once and releases the input. -/// -/// One runtime input takes the chunk by value, so its own reservation is what fails: a pool one byte -/// short of what the chunk charges refuses it. The error is the pool's, yielded once, and the failed -/// stream releases both the execution's reservations and its upstream while it stays alive. -#[tokio::test] -async fn a_single_consumer_enqueue_failure_is_reported_once_and_releases_the_input() { - use common_query::series_frame::unpack_raw_series; - - let schema = raw_schema(); - let chunk = series_chunk("web-1", &series_samples(1.0)[..4], false); - let one_frame = frame_of(&schema, &chunk); - - // What one decoded chunk charges, read from a probe: the bounded pool below is that figure. - let decoded = unpack_raw_series( - Box::pin(RecordBatchStreamAdapter::new( - frame_schema(), - futures::stream::iter(vec![Ok(one_frame.clone())]), - )), - Some(Arc::clone(&schema)), - ) - .expect("the frame decodes using the source decoder"); - let mut decoded = crate::source::materialize_raw_labels(decoded) - .expect("the labels materialize as the source does"); - let decoded_chunk = decoded - .next() - .await - .expect("the decoder returns its frame") - .expect("the frame decodes successfully"); - let probe_pool = Arc::new(GreedyMemoryPool::new(1 << 30)); - let probe_state = NativeExecutionState::try_new( - &rate_tree(), - Arc::clone(&probe_pool) as Arc, - None, - ) - .expect("the one-source runtime builds"); - let ids = probe_state.input_ids(0).collect::>(); - assert_eq!(1, ids.len(), "the tree has one runtime recipient"); - let mut probe = NativeExecutor::new(probe_state); - probe - .push_raw(ids[0], decoded_chunk) - .expect("the one recipient accepts the decoded chunk"); - let charge = probe_pool.reserved(); - assert!(charge > 0, "the decoded chunk reserves memory"); - drop(probe); - assert_eq!(0, probe_pool.reserved(), "the probe releases it"); - - // One byte short of the charge: the one recipient's own reservation is refused. - let dropped = Arc::new(AtomicBool::new(false)); - let pulled = Arc::new(AtomicUsize::new(0)); - let source_schema = frame_schema(); - let source = staged(source_schema.clone(), { - let dropped = Arc::clone(&dropped); - let pulled = Arc::clone(&pulled); - move || { - Box::pin( - StagedStream::new(source_schema.clone(), vec![one_frame.clone()]) - .dropped(&dropped) - .counted(&pulled), - ) - } - }); - let pool = Arc::new(GreedyMemoryPool::new(charge - 1)); - let mut stream = plan(rate_tree(), source) - .expect("the one-source plan builds") - .execute(0, task_context(&pool)) - .expect("the node executes"); - let error = match stream.next().await { - Some(Ok(batch)) => panic!("a batch before the chunk is queued: {batch:?}"), - Some(Err(error)) => error, - None => panic!("the enqueue failure was swallowed"), - }; - assert!( - error_chain(&error).to_lowercase().contains("memory") - || error_chain(&error).to_lowercase().contains("reservation") - || error_chain(&error).to_lowercase().contains("resource"), - "the failure is the exhausted memory pool: {error}" - ); - assert_eq!( - 0, - pool.reserved(), - "failure immediately releases reservations" - ); - assert!( - dropped.load(Ordering::SeqCst), - "failure releases the upstream stream" - ); - assert!( - pulled.load(Ordering::SeqCst) >= 1, - "the frame reached the decoder" - ); - assert!( - stream.next().await.is_none(), - "the failure is yielded once, not repeated" - ); - assert!( - stream.next().await.is_none(), - "the terminated stream stays ended" - ); -} - -/// Logical equality distinguishes source field layouts, even when the PromQL and output match. +/// Logical equality distinguishes source field layouts even when the binding and output match. #[test] fn logical_identity_includes_native_source_field_order() { let ordered_schema = two_field_schema(); @@ -5692,7 +5940,6 @@ fn logical_identity_includes_native_source_field_order() { let make_node = |schema: Arc| { let tree = rate_tree_of(schema); FusedSeries::try_new( - Arc::from(PROMQL), tree.clone(), binding(&tree), vec![input_plan(frame_schema())], @@ -5702,7 +5949,6 @@ fn logical_identity_includes_native_source_field_order() { }; let left = make_node(Arc::clone(&ordered_schema)); let right = make_node(Arc::new(reversed_schema)); - assert_eq!(left.promql(), right.promql()); assert_eq!(left.binding(), right.binding()); assert_ne!(left, right); assert_ne!(Some(std::cmp::Ordering::Equal), left.partial_cmp(&right)); @@ -5768,9 +6014,7 @@ async fn a_rebuilt_node_reports_only_its_own_execution() { first_rows.iter().map(|row| row.0).collect::>(), "the series is answered at every grid position" ); - let original = exec - .metrics() - .expect("the node reports its runtime's metrics"); + let original = node_metric_set(&exec); assert_eq!(1, count(&original, "native.root.completed_series")); // Rebuilt over a fresh input of the same frames, the way an optimizer replaces a child. @@ -5780,13 +6024,12 @@ async fn a_rebuilt_node_reports_only_its_own_execution() { ReplaceChildrenOptions::new(ChildrenPropertiesMode::Recompute), ) .expect("the node rebuilds over another input"); - // No execution of the rebuilt node has run, so it registered no handles to report. - let before = rebuilt - .metrics() - .expect("a plan that has not run reports an empty set rather than none"); + // No execution of the rebuilt node has run, so it registered no figures to report: the only + // entry is the tree it runs, which is structure rather than a figure. + let before = node_metric_set(&rebuilt); assert_eq!( 0, - before.iter().count(), + node_figures(&before), "the rebuilt node inherited an execution's metrics" ); // Rebuilding does not reset the node it was built from. @@ -5797,16 +6040,12 @@ async fn a_rebuilt_node_reports_only_its_own_execution() { .expect("the rebuilt node runs"); assert_eq!(first_rows, rows(&rebuilt_batches)); - let after = rebuilt - .metrics() - .expect("the rebuilt node reports the execution it ran"); + let after = node_metric_set(&rebuilt); assert_eq!(1, count(&after, "native.root.completed_series")); assert_eq!(1, count(&after, "native.root.input.completed_series")); // One execution's set, not two: a copy of the original's roots would answer 2 here. assert_eq!(original.iter().count(), after.iter().count()); - let retained = exec - .metrics() - .expect("the original keeps the handles its execution registered"); + let retained = node_metric_set(&exec); assert_eq!(original.iter().count(), retained.iter().count()); assert_eq!(1, count(&retained, "native.root.completed_series")); assert_eq!(1, count(&retained, "native.root.input.completed_series")); @@ -5832,16 +6071,14 @@ async fn repeated_metrics_reads_register_nothing_and_outlive_the_execution() { // metrics are read: the reads below happen while an execution is alive. assert!(futures::poll!(stream.next()).is_pending()); - let first = exec - .metrics() - .expect("the metrics are registered before the run"); + let first = node_metric_set(&exec); let names = |metrics: &MetricsSet| -> Vec { metrics .iter() .map(|metric| metric.value().name().to_string()) .collect() }; - let second = exec.metrics().expect("metrics can be read repeatedly"); + let second = node_metric_set(&exec); assert_eq!( names(&first), names(&second), @@ -5864,9 +6101,7 @@ async fn repeated_metrics_reads_register_nothing_and_outlive_the_execution() { drop(stream); // What the execution recorded is still readable once it is gone, and nothing it held is charged // any more. - let after = exec - .metrics() - .expect("the handles outlive the execution they were registered for"); + let after = node_metric_set(&exec); assert_eq!( names(&first), names(&after), @@ -5896,16 +6131,14 @@ async fn repeated_metrics_reads_register_nothing_and_outlive_the_execution() { // A second execution of the same plan registers its own tree's handles. It is dropped without // being polled, so what it adds is its own counters at zero -- the first execution's figures are // untouched -- and no part of the tree it built outlives it. - let registered = after.iter().count(); + let registered = node_figures(&after); let second_stream = exec .execute(0, task_context(&pool)) .expect("the plan runs a second time"); drop(second_stream); - let twice = exec - .metrics() - .expect("the metrics of both executions are read"); + let twice = node_metric_set(&exec); assert_eq!( - twice.iter().count(), + node_figures(&twice), registered * 2, "the second execution did not register its own node handles" ); @@ -5924,8 +6157,8 @@ async fn repeated_metrics_reads_register_nothing_and_outlive_the_execution() { assert_eq!(0, pool.reserved(), "the dropped executions hold no bytes"); // Reading both executions' metrics is still a read: the same entries come back. - let again = exec.metrics().expect("metrics can be read repeatedly"); - assert_eq!(twice.iter().count(), again.iter().count()); + let again = node_metric_set(&exec); + assert_eq!(node_figures(&twice), node_figures(&again)); for (early, late) in twice.iter().zip(again.iter()) { assert!( Arc::ptr_eq(early, late), @@ -6020,7 +6253,6 @@ fn partial_node(kind: AggregateKind) -> FusedSeries { let tree = partial_tree(tree, kind, &[HOST]); let schema = canonical_state_frame_schema(&tree); FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree.clone(), binding_of(&tree, raw_schema(), vec![HOST.to_string()]), vec![input_plan(frame_schema())], @@ -6041,7 +6273,6 @@ fn partial_plan( ) -> DataFusionResult> { let tree = partial_tree(tree, op.kind, tags); FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree.clone(), binding_of( &tree, @@ -6106,7 +6337,6 @@ async fn decode_state_batches(batches: Vec) -> Vec { async fn a_state_export_keys_its_labels_by_the_cut_aggregate_grouping() { let tree = partial_tree(rate_tree(), AggregateKind::Sum, &[JOB]); let node = FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree.clone(), // The scan is bound with `host`; the aggregate groups by `job`. binding_of(&tree, raw_schema(), vec![HOST.to_string()]), @@ -6468,7 +6698,6 @@ async fn mixed_raw_and_evaluated_inputs_execute_with_source_local_decoders() { ) .unwrap(); let exec = FusedSeries::try_new( - Arc::from("mixed raw/evaluated"), tree.clone(), binding_of( &tree, @@ -6582,7 +6811,6 @@ fn final_aggregate_state_input_constructs_with_evaluated_schema() { let tree = state_tree(AggregateStage::Final, NativeType::AggregateState(state)); let schema = canonical_frame_schema(&tree); let node = FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree.clone(), binding(&tree), vec![input_plan(frame_schema())], @@ -6604,7 +6832,6 @@ fn partial_aggregate_declares_state_frame_schema() { ); let state_schema = canonical_state_frame_schema(&tree); let node = FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree.clone(), binding(&tree), vec![input_plan(frame_schema())], @@ -6618,7 +6845,6 @@ fn partial_aggregate_declares_state_frame_schema() { ); let error = FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree.clone(), binding(&tree), vec![input_plan(frame_schema())], @@ -6758,7 +6984,6 @@ fn series_frame_output_schema_matches_evaluated_packer() { async fn series_frame_output_uses_evaluated_codec_schema() { let tree = rate_tree(); let frame_node = FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree.clone(), binding(&tree), vec![input_plan(frame_schema())], @@ -6775,7 +7000,6 @@ async fn series_frame_output_uses_evaluated_codec_schema() { "frame schema comes from the codec rather than the row output" ); let wrong_schema = FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree.clone(), binding(&tree), vec![input_plan(frame_schema())], @@ -6800,7 +7024,6 @@ async fn series_frame_output_uses_evaluated_codec_schema() { fn series_frame_accepts_multiple_sources_with_equal_partition_counts() { let tree = binary_tree_over_sources(BinaryOp::Add, raw_schema(), raw_schema(), 0, 1); let node = FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree.clone(), binding(&tree), vec![input_plan(frame_schema()), input_plan(frame_schema())], @@ -6839,7 +7062,6 @@ async fn series_frame_preserves_four_input_partitions() { .collect::>(); let tree = rate_tree(); let node = FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree.clone(), binding(&tree), vec![input_plan(frame_schema())], @@ -6890,7 +7112,6 @@ async fn series_frame_rate_matches_plain_rows_bitwise() { ); let tree = rate_tree(); let node = FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree.clone(), binding(&tree), vec![input_plan(frame_schema())], @@ -6949,7 +7170,6 @@ async fn series_frame_rate_matches_plain_rows_bitwise() { async fn partial_empty_input_yields_valid_empty_state_frame_stream() { let tree = partial_tree(rate_tree(), AggregateKind::Sum, &[HOST]); let node = FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree.clone(), binding(&tree), vec![input_plan(frame_schema())], @@ -6972,7 +7192,6 @@ async fn partial_root_requires_frame_output() { let prefix = rate_tree(); let tree = partial_tree(prefix, AggregateKind::Sum, &[HOST]); let error = FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree.clone(), binding(&tree), vec![input_plan(frame_schema())], @@ -6985,18 +7204,16 @@ async fn partial_root_requires_frame_output() { /// `EXPLAIN` says nothing extra about the row table and names the frame output. /// -/// The row table is what a query returns, so its node line has to stay exactly what it was; a frame -/// output answers something else, and the line is the only place a reader can see that. +/// The row-table header has no parameters; a frame output names its format in the header. #[test] fn the_explain_text_names_the_frame_format_only() { let rows = fused(rate_tree()); - assert_eq!( - "FusedSeries: rate(x[5m])", - Explain(&rows).to_string(), - "the row-table line is what it always was" + assert!( + Explain(&rows) + .to_string() + .starts_with("FusedSeries\nnative_tree=[\nWindow:") ); let framed = FusedSeries::try_new_with_format( - Arc::from(PROMQL), rows.tree().clone(), rows.binding().clone(), rows.inputs().into_iter().cloned().collect(), @@ -7007,16 +7224,30 @@ fn the_explain_text_names_the_frame_format_only() { assert!( Explain(&framed) .to_string() - .contains("(output: series_frame)"), + .starts_with("FusedSeries: (output: series_frame)\nnative_tree=[\nWindow:"), "a frame output names its format: {}", Explain(&framed) ); + let framed_exec = plan_of(framed, memory_exec(Vec::new())).unwrap(); + let text = displayable(framed_exec.as_ref()).indent(false).to_string(); + assert!( + text.starts_with("FusedSeriesExec: (output: series_frame)\nnative_tree=[\nWindow:"), + "{text}" + ); + let canonical = canonical_producer_node(); + let text = Explain(&canonical).to_string(); + assert!( + text.starts_with( + "FusedSeries: (output: series_frame, per-partition canonical merge)\nnative_tree=[\nMerge:" + ), + "{text}" + ); let plain = plan(rate_tree(), memory_exec(Vec::new())).expect("the runtime executes the tree"); let text = displayable(plain.as_ref()).indent(false).to_string(); assert!( - text.starts_with("FusedSeriesExec: rate(x[5m])\n"), - "the row-table node line is unchanged: {text}" + text.starts_with("FusedSeriesExec\nnative_tree=[\nWindow:"), + "the row-table node line has no parameters: {text}" ); } @@ -7031,7 +7262,6 @@ fn the_logical_schema_follows_the_output_format() { assert_eq!(&output_schema(), rows.schema().inner()); let framed = FusedSeries::try_new_with_format( - Arc::from(PROMQL), rows.tree().clone(), rows.binding().clone(), rows.inputs().into_iter().cloned().collect(), @@ -7060,7 +7290,6 @@ fn the_logical_schema_follows_the_output_format() { fn rebuilding_a_frame_node_keeps_its_output_format() { let rows = fused(rate_tree()); let framed = FusedSeries::try_new_with_format( - Arc::from(PROMQL), rows.tree().clone(), rows.binding().clone(), rows.inputs().into_iter().cloned().collect(), @@ -7097,7 +7326,6 @@ fn a_series_frame_refuses_a_root_it_cannot_carry() { tags: vec![HOST.to_string(), NAME.to_string()], }; let error = FusedSeries::try_new_with_format( - Arc::from(PROMQL), scalar, binding, Vec::new(), @@ -7152,7 +7380,6 @@ async fn a_pending_input_does_not_hold_the_rows_a_frame_buffered() { }); let tree = rate_tree(); let node = FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree.clone(), binding(&tree), vec![input_plan(schema)], @@ -7258,7 +7485,6 @@ async fn fused_frame_input_error_terminates_and_releases_input() { }); let tree = rate_tree(); let node = FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree.clone(), binding(&tree), vec![input_plan(schema)], @@ -7308,7 +7534,6 @@ async fn a_frame_carries_the_closing_chunk_a_row_table_drops() { ); let tree = rate_tree(); let node = FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree.clone(), binding(&tree), vec![input_plan(frame_schema())], @@ -7397,7 +7622,6 @@ async fn a_single_consumer_keeps_the_empty_closing_chunk_in_its_frame() { drop(execution); let node = FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree.clone(), binding(&tree), vec![input_plan(frame_schema())], @@ -7465,7 +7689,6 @@ async fn two_chains_that_close_on_one_label_set_each_keep_their_frame() { ]; let tree = rate_tree(); let node = FusedSeries::try_new_with_format( - Arc::from(PROMQL), tree.clone(), binding(&tree), vec![input_plan(frame_schema())], @@ -7614,7 +7837,6 @@ fn duplicate_evidence_partitioned_exec( let tree = NativeTree::try_new(root).expect("differently bound instant selectors form a valid tree"); let node = FusedSeries::try_new( - Arc::from(PROMQL), tree.clone(), binding(&tree), vec![input_plan(frame_schema()), input_plan(frame_schema())], @@ -7855,7 +8077,6 @@ fn histogram_over_sum_splits_and_declares_complete_bucket_routing() { if op.stage == AggregateStage::Final && op.group == group )); let node = FusedSeries::try_new_with_format( - Arc::from("histogram over sum"), final_tree.clone(), binding(&final_tree), vec![input_plan( @@ -7870,8 +8091,8 @@ fn histogram_over_sum_splits_and_declares_complete_bucket_routing() { }); let display = logical.display_indent().to_string(); assert!( - display.lines().next().unwrap().ends_with( - " (state routing: canonical labels except [\"le\", \"__name__\", \"__type__\", \"__unit__\"])" + display.lines().next().unwrap().starts_with( + "FusedSeries: (output: series_frame) (state routing: canonical labels except [\"le\", \"__name__\", \"__type__\", \"__unit__\"])" ), "{display}" ); @@ -8105,7 +8326,6 @@ fn histogram_final_replaces_incompatible_state_router() { let tree = histogram_over_sum_tree(group, HistogramFunction::Quantile(0.5)); let (partial, final_tree) = FusedSeries::split_aggregate(&tree, TIME).unwrap().unwrap(); let partial_node = FusedSeries::try_new_state_export( - Arc::from("partial"), partial.clone(), binding(&partial), vec![input_plan(frame_schema())], @@ -8127,7 +8347,6 @@ fn histogram_final_replaces_incompatible_state_router() { .unwrap(), ) as Arc; let final_node = FusedSeries::try_new_with_format( - Arc::from("final"), final_tree.clone(), binding(&final_tree), vec![input_plan( @@ -8213,7 +8432,6 @@ async fn histogram_split_routes_whole_state_batches_and_preserves_each_bound() { ]), ]; let partial_node = FusedSeries::try_new_state_export( - Arc::from("partial"), partial.clone(), binding(&partial), vec![input_plan(frame_schema())], @@ -8260,7 +8478,6 @@ async fn histogram_split_routes_whole_state_batches_and_preserves_each_bound() { ); } let final_node = FusedSeries::try_new_with_format( - Arc::from("final"), final_tree.clone(), binding(&final_tree), vec![input_plan( @@ -8405,7 +8622,6 @@ async fn histogram_split_colocates_metadata_distinct_results_before_their_merge( } } let partial_node = FusedSeries::try_new_state_export( - Arc::from("partial"), partial.clone(), binding(&partial), vec![input_plan(frame_schema())], @@ -8419,7 +8635,6 @@ async fn histogram_split_colocates_metadata_distinct_results_before_their_merge( )]) .unwrap(); let final_node = FusedSeries::try_new_with_format( - Arc::from("final"), final_tree.clone(), binding(&final_tree), vec![input_plan( diff --git a/src/promql/src/extension_plan/fused_series/tests/frame_output.rs b/src/promql/src/extension_plan/fused_series/tests/frame_output.rs index ee7f59402e2..88faa9580ee 100644 --- a/src/promql/src/extension_plan/fused_series/tests/frame_output.rs +++ b/src/promql/src/extension_plan/fused_series/tests/frame_output.rs @@ -158,7 +158,6 @@ async fn sparse_instant_frame_preserves_positions_nulls_and_nan_across_formats() let binding = binding_of(&tree, Arc::clone(&schema), vec![HOST.into(), NAME.into()]); let make_node = |format| { FusedSeries::try_new_with_format( - Arc::from("instant(x)"), tree.clone(), binding.clone(), vec![input_plan(frame_schema_of(&schema))], @@ -217,7 +216,6 @@ async fn abs_rate_frame_matches_plain_rows_with_exact_values_and_labels() { let binding = binding(&tree); let make_node = |format| { FusedSeries::try_new_with_format( - Arc::from("abs(rate(x[5m]))"), tree.clone(), binding.clone(), vec![input_plan(frame_schema())], @@ -321,7 +319,6 @@ async fn rewritten_identical_labels_keep_disjoint_chains_in_one_encoder() { let binding = binding_of(&tree, Arc::clone(&schema), vec![HOST.into(), NAME.into()]); let make_node = |format| { FusedSeries::try_new_with_format( - Arc::from("rewritten instant"), tree.clone(), binding.clone(), vec![input_plan(frame_schema())], diff --git a/src/promql/src/extension_plan/series_repartition/tests.rs b/src/promql/src/extension_plan/series_repartition/tests.rs index 1061c81bc2c..e69f7e607eb 100644 --- a/src/promql/src/extension_plan/series_repartition/tests.rs +++ b/src/promql/src/extension_plan/series_repartition/tests.rs @@ -2413,7 +2413,6 @@ async fn fused_instant_complete_disjoint_publishes_before_other_input_eof() { .unwrap(); let output_frame_schema = FusedSeries::series_frame_schema(&tree).unwrap(); let logical = FusedSeries::try_new_with_format( - Arc::from("m"), tree, FusedSeriesBinding { grid: Arc::clone(&grid), diff --git a/src/promql/src/native/display.rs b/src/promql/src/native/display.rs index ed313af2cb7..95b34eda0b8 100644 --- a/src/promql/src/native/display.rs +++ b/src/promql/src/native/display.rs @@ -17,15 +17,23 @@ //! The tree is displayed by recursively visiting its bound expressions. The display is an //! observation of a compiled tree: it runs nothing, and every line reads as the node it is, //! making no claim about whether that node could run. +//! +//! A node that recorded figures shows them at the end of its own line, and only its own: the walk +//! carries the path the runtime registered that node under, so the figures of one node are never +//! reported as those of another. A tree whose nodes recorded nothing - one that was never run, +//! which is what `EXPLAIN` shows - prints structure only. +use std::borrow::Cow; use std::fmt::{self, Write}; use std::sync::Arc; +use common_recordbatch::adapter::{NativeTreeMetrics, format_metric}; use datatypes::series::time::EvalGrid; use datatypes::series::{SeriesLabels, SeriesOrdering}; use crate::histogram_eval::HistogramFunction; use crate::map::MathOp; +use crate::native::metrics::NodeMetricsSnapshot; use crate::native::plan::{ AggregateKind, AggregateStage, BoundVectorOp, BoundWindow, Grouping, InstantValue, MapOp, NativeExpr, NativeNode, NativeTree, NativeType, ParameterizedOp, @@ -34,8 +42,39 @@ use crate::range_eval::RangeFunction; use crate::select::{MatchOp, SortOrder}; use crate::vector_match::{BinaryOp, Cardinality, FillValues, MatchingLabels, SetOp}; -/// The header line of every tree. -const HEADER: &str = "NativeTree"; +/// The path the runtime registers a tree's root node under. +/// +/// Every other path is this one and the segments of [`child_segments`], so the walk below asks for +/// a node's figures by the same key its registration used. +pub const ROOT_PATH: &str = "root"; + +/// One displayed node: its line, the figures it recorded, and the nodes that consume its output. +/// +/// The same report feeds the tree's text and, through the operator that owns the tree, the +/// structure a machine-readable `ANALYZE` publishes, so the two cannot describe different trees. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct NativeNodeReport { + /// The node's line without figures, as `: `. + pub line: String, + /// The figures this node recorded, in [`crate::native::metrics::NodeMetric::ALL`] order. + pub metrics: Vec<(String, usize)>, + /// The nodes that consume this one's output, in the order the runtime feeds them. + pub children: Vec, +} + +impl From for NativeTreeMetrics { + /// The same nodes, as the plain data a machine-readable `ANALYZE` publishes. + /// + /// The text and that record are built from one report, so a reader of either sees the same + /// nodes with the same figures. + fn from(report: NativeNodeReport) -> Self { + Self { + line: report.line, + metrics: report.metrics, + children: report.children.into_iter().map(Self::from).collect(), + } + } +} /// A grid as `/x`. fn grid_name(grid: &EvalGrid) -> String { @@ -439,19 +478,20 @@ fn kind(expr: &NativeExpr) -> &'static str { } } -/// One recursively displayed node and its children. -fn display_node( - f: &mut fmt::Formatter<'_>, - node: &NativeNode, - depth: usize, - edge: &str, -) -> fmt::Result { - f.write_char('\n')?; - // The root sits one level in from the header, and each child one level below its consumer. - for _ in 0..depth + 1 { - f.write_str(" ")?; +/// The child nodes of `node` and the path segment each is registered under, in child order. +/// +/// The segments are the ones the runtime registers, not the names the bound tree happens to give +/// the operands: `info` binds its operands as `base` and `info`, while the runtime registers them +/// as the kernel's `left` and `right`, so the display asks for the figures where they were put. +fn child_segments(node: &NativeNode) -> Vec<(&NativeNode, Cow<'static, str>)> { + let mut children = Vec::new(); + fn child<'a>( + children: &mut Vec<(&'a NativeNode, Cow<'static, str>)>, + segment: &'static str, + node: &'a NativeNode, + ) { + children.push((node, Cow::Borrowed(segment))); } - write!(f, "{edge} {} {}", kind(&node.expr), describe(node))?; match &node.expr { NativeExpr::Input(_) | NativeExpr::Scalar(_) | NativeExpr::EvalTime { .. } => {} NativeExpr::Instant { input, .. } @@ -462,46 +502,115 @@ fn display_node( | NativeExpr::Aggregate { input, .. } | NativeExpr::Vector { input, .. } | NativeExpr::VectorOf { input } - | NativeExpr::ScalarOf { input } => display_node(f, input, depth + 1, "input")?, - NativeExpr::ParameterizedHistogram { - input, parameters, .. - } => { - display_node(f, input, depth + 1, "input")?; - for parameter in parameters { - display_node(f, parameter, depth + 1, "parameter")?; - } - } + | NativeExpr::ScalarOf { input } => child(&mut children, "input", input), NativeExpr::ParameterizedTopK { input, parameter, .. } => { - display_node(f, input, depth + 1, "input")?; - display_node(f, parameter, depth + 1, "parameter")?; + child(&mut children, "input", input); + child(&mut children, "parameter", parameter); + } + NativeExpr::ParameterizedHistogram { + input, parameters, .. + } => { + child(&mut children, "input", input); + for (index, parameter) in parameters.iter().enumerate() { + children.push((parameter, Cow::Owned(format!("parameter{index}")))); + } } NativeExpr::ParameterizedMap { input, parameters, .. } => { - display_node(f, input, depth + 1, "input")?; - for parameter in parameters { - display_node(f, parameter, depth + 1, "parameter")?; + child(&mut children, "input", input); + for (index, parameter) in parameters.iter().enumerate() { + children.push((parameter, Cow::Owned(format!("parameter{index}")))); } } NativeExpr::Binary { left, right, .. } | NativeExpr::Set { left, right, .. } => { - display_node(f, left, depth + 1, "left")?; - display_node(f, right, depth + 1, "right")?; + child(&mut children, "left", left); + child(&mut children, "right", right); } NativeExpr::Info { base, info, .. } => { - display_node(f, base, depth + 1, "base")?; - display_node(f, info, depth + 1, "info")?; + child(&mut children, "left", base); + child(&mut children, "right", info); } } + children +} + +/// The node at `path` and everything below it, with the figures each node recorded. +fn report_node(node: &NativeNode, path: &str, metrics: &NodeMetricsSnapshot) -> NativeNodeReport { + NativeNodeReport { + line: format!("{}: {}", kind(&node.expr), describe(node)), + metrics: metrics.described(path), + children: child_segments(node) + .into_iter() + .map(|(child, segment)| report_node(child, &format!("{path}.{segment}"), metrics)) + .collect(), + } +} + +impl NativeTree { + /// This tree, one report per node, carrying the figures each node recorded. + /// + /// A snapshot with no entries reports the tree's structure only, which is what a plan that was + /// never executed has to show. + pub fn report(&self, metrics: &NodeMetricsSnapshot) -> NativeNodeReport { + report_node(self.root(), ROOT_PATH, metrics) + } + + /// This tree rendered from column zero, with each node's own figures when `metrics` holds them. + pub fn display_with<'a>(&'a self, metrics: &'a NodeMetricsSnapshot) -> NativeTreeDisplay<'a> { + NativeTreeDisplay { + tree: self, + metrics, + } + } +} + +/// A tree as it appears below the operator line: one node per line, two spaces per depth, and each +/// node's figures on its own line. +pub struct NativeTreeDisplay<'a> { + tree: &'a NativeTree, + metrics: &'a NodeMetricsSnapshot, +} + +impl fmt::Display for NativeTreeDisplay<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // The root starts at column zero: the block the caller brackets the tree in carries the + // indentation, so each line here is depth-relative to that block rather than to the + // caller's own place in the plan. + write_report(f, &self.tree.report(self.metrics), 0) + } +} + +fn write_report( + f: &mut fmt::Formatter<'_>, + report: &NativeNodeReport, + depth: usize, +) -> fmt::Result { + write!(f, "{:indent$}{}", "", report.line, indent = depth * 2)?; + if !report.metrics.is_empty() { + let figures = report + .metrics + .iter() + .map(|(name, value)| format_metric(name, *value)) + .collect::>() + .join(", "); + write!(f, ", metrics=[{figures}]")?; + } + for child in &report.children { + f.write_char('\n')?; + write_report(f, child, depth + 1)?; + } Ok(()) } impl fmt::Display for NativeTree { - /// One line per node, outermost first, indented two spaces per depth below the header. + /// One line per node, outermost first, from column zero with two spaces per depth and without + /// figures: this is the tree's own shape, which is what a display that holds no metrics + /// snapshot can show. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(HEADER)?; - display_node(f, self.root(), 0, "root") + write_report(f, &self.report(&NodeMetricsSnapshot::default()), 0) } } diff --git a/src/promql/src/native/display/tests.rs b/src/promql/src/native/display/tests.rs index 146058dc497..84dc72483c8 100644 --- a/src/promql/src/native/display/tests.rs +++ b/src/promql/src/native/display/tests.rs @@ -18,12 +18,17 @@ use datatypes::series::samples::{FieldSpec, SeriesSchema}; use datatypes::series::time::{EvalGrid, GridSpec}; use datatypes::series::{Label, SeriesLabels, SeriesOrdering}; +use datafusion::physical_plan::metrics::ExecutionPlanMetricsSet; + use super::super::plan::{ AggregateKind, AggregateStage, BoundAggregate, BoundInput, BoundInstant, BoundMap, BoundScalar, BoundSetOp, BoundVectorOp, BoundWindow, Grouping, NativeExpr, NativeNode, NativeTree, - NativeType, + NativeType, ParameterizedOp, }; +use super::child_segments; +use crate::info::InfoSelector; use crate::map::MathOp; +use crate::native::metrics::{NodeMetrics, NodeMetricsSnapshot}; use crate::native::plan::{InstantValue, MapOp}; use crate::range_eval::{RangeFunction, RangeModifier, RangeWindow}; use crate::select::SortOrder; @@ -114,17 +119,29 @@ fn shown(lines: &[&str]) -> String { lines.join("\n") } -/// The header comes first and is exactly `NativeTree`, so the output says what it is before it -/// says anything about the tree. +/// A tree is plan lines and nothing else: no header above them and no word beside a node, so every +/// line reads as the operator it is, one level in from the operator line it is drawn under. #[test] -fn the_first_line_names_the_tree() { +fn a_tree_is_plan_lines_only() { let tree = NativeTree::try_new(raw_input()).unwrap(); + assert_eq!(tree.to_string(), shown(&["Input: raw samples source=0"])); assert_eq!( - tree.to_string(), - shown(&["NativeTree", " root Input raw samples source=0"]) + NativeTree::try_new(window_over( + raw_input(), + RangeFunction::Rate, + 300_000, + 0, + None, + grid(0, 60_000, 4), + )) + .unwrap() + .to_string(), + shown(&[ + "Window: rate [300000ms] offset 0 anchor - on grid 0/60000x4", + " Input: raw samples source=0", + ]) ); - assert_eq!(tree.to_string().lines().next(), Some("NativeTree")); } /// A compiled tree is one line per node, outermost first: the window is the root and the scan it @@ -144,9 +161,8 @@ fn a_window_over_raw_samples_is_shown_once_per_node() { assert_eq!( tree.to_string(), shown(&[ - "NativeTree", - " root Window rate [300000ms] offset 0 anchor - on grid 0/60000x4", - " input Input raw samples source=0", + "Window: rate [300000ms] offset 0 anchor - on grid 0/60000x4", + " Input: raw samples source=0", ]) ); } @@ -171,10 +187,9 @@ fn a_map_nests_below_the_window_it_maps() { assert_eq!( tree.to_string(), shown(&[ - "NativeTree", - " root Map abs, round 3", - " input Window rate [300000ms] offset 60000 anchor - on grid 0/60000x4", - " input Input raw samples source=0", + "Map: abs, round 3", + " Window: rate [300000ms] offset 60000 anchor - on grid 0/60000x4", + " Input: raw samples source=0", ]) ); } @@ -196,9 +211,8 @@ fn a_window_names_its_constant_argument_and_its_anchor() { assert_eq!( tree.to_string(), shown(&[ - "NativeTree", - " root Window quantile_over_time 0.9 [60000ms] offset 0 anchor 42 on grid 1000/60000x4", - " input Input raw samples source=0", + "Window: quantile_over_time 0.9 [60000ms] offset 0 anchor 42 on grid 1000/60000x4", + " Input: raw samples source=0", ]) ); } @@ -222,10 +236,7 @@ fn an_input_reports_whether_it_is_raw_and_what_ordering_it_guarantees() { let tree = NativeTree::try_new(evaluated_input).unwrap(); assert_eq!( tree.to_string(), - shown(&[ - "NativeTree", - " root Input evaluated samples source=0, contiguous", - ]) + shown(&["Input: evaluated samples source=0, contiguous",]) ); let interleaved = NativeType::Samples(Arc::new(SeriesSchema { @@ -243,10 +254,7 @@ fn an_input_reports_whether_it_is_raw_and_what_ordering_it_guarantees() { let tree = NativeTree::try_new(raw_input).unwrap(); assert_eq!( tree.to_string(), - shown(&[ - "NativeTree", - " root Input raw samples source=0, interleaved" - ]) + shown(&["Input: raw samples source=0, interleaved"]) ); } @@ -260,7 +268,7 @@ fn a_scalar_and_an_instant_line_show_what_they_hold() { output: NativeType::Scalar(Arc::clone(&out)), }; let tree = NativeTree::try_new(scalar).unwrap(); - assert_eq!(tree.to_string(), shown(&["NativeTree", " root Scalar 2"])); + assert_eq!(tree.to_string(), shown(&["Scalar: 2"])); let instant = NativeNode { expr: NativeExpr::Instant { @@ -279,9 +287,8 @@ fn a_scalar_and_an_instant_line_show_what_they_hold() { assert_eq!( tree.to_string(), shown(&[ - "NativeTree", - " root Instant lookback 300000ms offset 60000 on grid 0/60000x4", - " input Input raw samples source=0", + "Instant: lookback 300000ms offset 60000 on grid 0/60000x4", + " Input: raw samples source=0", ]) ); } @@ -313,10 +320,9 @@ fn an_aggregate_and_a_binary_name_their_binding() { assert_eq!( tree.to_string(), shown(&[ - "NativeTree", - " root Aggregate sum by [job] complete", - " input Window rate [300000ms] offset 0 anchor - on grid 0/60000x4", - " input Input raw samples source=0", + "Aggregate: sum by [job] complete", + " Window: rate [300000ms] offset 0 anchor - on grid 0/60000x4", + " Input: raw samples source=0", ]) ); @@ -350,11 +356,10 @@ fn an_aggregate_and_a_binary_name_their_binding() { assert_eq!( tree.to_string(), shown(&[ - "NativeTree", - " root Binary + ignoring [] one-to-one", - " left Scalar 2", - " right Window rate [300000ms] offset 0 anchor - on grid 0/60000x4", - " input Input raw samples source=0", + "Binary: + ignoring [] one-to-one", + " Scalar: 2", + " Window: rate [300000ms] offset 0 anchor - on grid 0/60000x4", + " Input: raw samples source=0", ]) ); @@ -369,8 +374,8 @@ fn an_aggregate_and_a_binary_name_their_binding() { .unwrap() .to_string() .lines() - .nth(1), - Some(" root Binary + ignoring [] one-to-one fill_right 0.5") + .next(), + Some("Binary: + ignoring [] one-to-one fill_right 0.5") ); } @@ -400,19 +405,19 @@ fn a_set_operation_names_its_operator_and_labels() { output: evaluated(Arc::clone(&out)), }) .unwrap(); - tree.to_string().lines().nth(1).unwrap().to_string() + tree.to_string().lines().next().unwrap().to_string() }; assert_eq!( root_line(SetOp::And, MatchingLabels::On(vec!["job".into()])), - " root Set and on [job]" + "Set: and on [job]" ); assert_eq!( root_line(SetOp::Or, MatchingLabels::Ignoring(vec!["job".into()])), - " root Set or ignoring [job]" + "Set: or ignoring [job]" ); assert_eq!( root_line(SetOp::Unless, MatchingLabels::Ignoring(Vec::new())), - " root Set unless ignoring []" + "Set: unless ignoring []" ); } @@ -446,10 +451,9 @@ fn a_without_grouping_does_not_read_as_by() { assert_eq!( tree.to_string(), shown(&[ - "NativeTree", - " root Aggregate sum without [job] complete", - " input Window rate [300000ms] offset 0 anchor - on grid 0/60000x4", - " input Input raw samples source=0", + "Aggregate: sum without [job] complete", + " Window: rate [300000ms] offset 0 anchor - on grid 0/60000x4", + " Input: raw samples source=0", ]) ); } @@ -497,22 +501,22 @@ fn a_vector_operation_names_its_binding() { top: false, group: Grouping::Without(Arc::new(["job".into()])), }, - " root Vector bottomk 3 without [job]", + "Vector: bottomk 3 without [job]", ), ( BoundVectorOp::LimitK { k: 2.0, group: Grouping::By(Arc::new([])), }, - " root Vector limitk 2 by []", + "Vector: limitk 2 by []", ), ( BoundVectorOp::LimitRatio { ratio: -0.5 }, - " root Vector limit_ratio -0.5", + "Vector: limit_ratio -0.5", ), ( BoundVectorOp::Sort(SortOrder::ByLabelDesc(vec!["job".into(), "le".into()])), - " root Vector sort_by_label_desc [job, le]", + "Vector: sort_by_label_desc [job, le]", ), ( BoundVectorOp::Absent { @@ -525,14 +529,14 @@ fn a_vector_operation_names_its_binding() { ) .unwrap(), }, - r#" root Vector absent {job="x"}"#, + r#"Vector: absent {job="x"}"#, ), ( BoundVectorOp::CountValues { label: "value".into(), group: Grouping::Without(Arc::new(["job".into()])), }, - r#" root Vector count_values "value" without [job]"#, + r#"Vector: count_values "value" without [job]"#, ), ]; for (op, line) in lines { @@ -544,7 +548,7 @@ fn a_vector_operation_names_its_binding() { output: evaluated(Arc::clone(&out)), }) .unwrap(); - assert_eq!(tree.to_string().lines().nth(1), Some(line)); + assert_eq!(tree.to_string().lines().next(), Some(line)); } } @@ -567,10 +571,129 @@ fn a_modified_window_names_its_modifier() { } let tree = NativeTree::try_new(node).unwrap(); assert_eq!( - tree.to_string().lines().nth(1), + tree.to_string().lines().next(), Some( - " root Window rate [300000ms] smoothed lookback 300000ms offset 0 anchor - on grid \ + "Window: rate [300000ms] smoothed lookback 300000ms offset 0 anchor - on grid \ 0/60000x4" ) ); } + +/// An aggregate over a window over a scan, which is the shape whose three nodes each record +/// something different. +fn aggregate_over_window(out: Arc) -> NativeTree { + NativeTree::try_new(NativeNode { + expr: NativeExpr::Aggregate { + input: Box::new(window_over( + raw_input(), + RangeFunction::Rate, + 300_000, + 0, + None, + Arc::clone(&out), + )), + op: BoundAggregate { + kind: AggregateKind::Sum, + group: Grouping::By(Arc::new(["job".into()])), + stage: AggregateStage::Complete, + }, + }, + output: evaluated(out), + }) + .unwrap() +} + +/// A node's figures end its own line, and only its own: the walk asks for them by the path the +/// runtime registered that node under, so one node never reports another's work. +#[test] +fn each_node_shows_only_the_figures_it_recorded() { + let tree = aggregate_over_window(grid(0, 60_000, 4)); + let set = ExecutionPlanMetricsSet::new(); + + let aggregate = NodeMetrics::default(); + aggregate.record_state(120, 0); + aggregate.register("root", &set, 0); + let window = NodeMetrics::default(); + window.record_output(4, 7200); + window.register("root.input", &set, 0); + let input = NodeMetrics::default(); + input.record_input(1, 1_486_080); + input.register("root.input.input", &set, 0); + + let metrics = NodeMetricsSnapshot::read(&set); + assert_eq!( + tree.display_with(&metrics).to_string(), + shown(&[ + "Aggregate: sum by [job] complete, metrics=[groups: 120]", + " Window: rate [300000ms] offset 0 anchor - on grid 0/60000x4, \ + metrics=[output_batches: 4, output_values: 7200]", + " Input: raw samples source=0, metrics=[input_batches: 1, input_samples: 1486080]", + ]) + ); +} + +/// A tree whose nodes recorded nothing - one that was never run, which is what `EXPLAIN` shows - +/// is the same lines without figures. No flag says so: an empty snapshot is what an unexecuted +/// registration leaves. +#[test] +fn a_tree_that_recorded_nothing_shows_no_figures() { + let tree = aggregate_over_window(grid(0, 60_000, 4)); + + assert_eq!( + tree.display_with(&NodeMetricsSnapshot::default()) + .to_string(), + tree.to_string() + ); + assert!(!tree.to_string().contains("metrics=")); +} + +/// A node's figures are read from the path the runtime registers, not from the name the bound tree +/// gives the operand: `info` binds its operands as `base` and `info`, and the runtime registers them +/// as the kernel's `left` and `right`. +#[test] +fn every_child_is_read_from_the_path_the_runtime_registers() { + let out = grid(0, 60_000, 4); + let scalar = NativeNode { + expr: NativeExpr::Scalar(BoundScalar { value: 2.0 }), + output: NativeType::Scalar(Arc::clone(&out)), + }; + let parameterized = NativeNode { + expr: NativeExpr::ParameterizedMap { + input: Box::new(window_over( + raw_input(), + RangeFunction::Rate, + 300_000, + 0, + None, + Arc::clone(&out), + )), + op: ParameterizedOp::Round, + parameters: vec![scalar.clone(), scalar], + }, + output: evaluated(Arc::clone(&out)), + }; + let segments = |node: &NativeNode| { + child_segments(node) + .into_iter() + .map(|(_, segment)| segment.to_string()) + .collect::>() + }; + assert_eq!( + segments(¶meterized), + ["input", "parameter0", "parameter1"] + ); + + let info = NativeNode { + expr: NativeExpr::Info { + base: Box::new(raw_input()), + info: Box::new(raw_input()), + selector: InfoSelector { + name_matchers: Vec::new(), + data_labels: Vec::new(), + keep_unmatched: false, + }, + }, + output: evaluated(out), + }; + assert_eq!(segments(&info), ["left", "right"]); +} diff --git a/src/promql/src/native/execute/tests.rs b/src/promql/src/native/execute/tests.rs index 174c02eee47..2bf54c9f1de 100644 --- a/src/promql/src/native/execute/tests.rs +++ b/src/promql/src/native/execute/tests.rs @@ -988,7 +988,6 @@ fn deep_native_tree_executes_after_fused_series_round_trip() { 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), diff --git a/src/promql/src/native/metrics.rs b/src/promql/src/native/metrics.rs index 65cc9fdae67..22381001511 100644 --- a/src/promql/src/native/metrics.rs +++ b/src/promql/src/native/metrics.rs @@ -32,6 +32,8 @@ //! - `peak_reserved_bytes` alone carries `p`, because the reader sums gauges by name and //! a sum of peaks is not a peak. +use std::collections::HashMap; + use datafusion::physical_plan::metrics::MetricValue; use datafusion::physical_plan::metrics::{ Count, ExecutionPlanMetricsSet, Gauge, MetricBuilder, Time, @@ -88,6 +90,103 @@ impl NodeMetric { ]; } +/// The figures every node of one execution left, keyed by the path it was registered under. +/// +/// A path is the one [`NodeMetrics::register`] was called with (`root`, `root.left`, ...), which is +/// exactly what the runtime builds while it compiles the tree: a reader that walks the *same* tree +/// reaches the same path for the same node, so a figure is found rather than guessed. +/// +/// Partitions are combined here because a node is registered once per partition: counts and times +/// add up, and `peak_reserved_bytes` — the one figure whose name carries its partition, because a +/// sum of peaks is not a peak — is the highest of them. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct NodeMetricsSnapshot { + nodes: HashMap>, +} + +impl NodeMetricsSnapshot { + /// Reads every native node figure in `set`. + /// + /// `set` may hold other metrics, and holds nothing at all before an execution: a snapshot of a + /// plan that never ran is empty, which is how a plain `EXPLAIN` shows a tree without figures. + /// + /// The partitions of one node are combined here rather than by + /// [`MetricsSet::aggregate_by_name`](datafusion::physical_plan::metrics::MetricsSet::aggregate_by_name), + /// which adds at least one nanosecond to every time it touches: a node whose timer never ran + /// would then report time it did not spend. + pub fn read(set: &ExecutionPlanMetricsSet) -> Self { + let mut nodes: HashMap> = HashMap::new(); + for metric in set.clone_inner().iter() { + let Some((path, name)) = split_name(metric.value().name()) else { + continue; + }; + let value = metric.value().as_usize(); + let figures = nodes.entry(path.to_string()).or_default(); + match figures.iter_mut().find(|(figure, _)| figure == name) { + // A peak is per partition because partitions peak at different moments: the node's + // peak is the highest of theirs, never their sum. + Some((_, accumulated)) if name == NodeMetric::PEAK_RESERVED_BYTES => { + *accumulated = (*accumulated).max(value) + } + Some((_, accumulated)) => *accumulated = accumulated.saturating_add(value), + None => figures.push((name.to_string(), value)), + } + } + for figures in nodes.values_mut() { + figures.sort_by_key(|(name, _)| node_metric_order(name)); + } + Self { nodes } + } + + /// What this node recorded, as `name: value`, dropping the figures that are zero. + /// + /// A node registers every figure it may record, so a node that did one thing would otherwise + /// report ten things it did not: the tree would stop reading as a tree. A node that recorded + /// nothing therefore reports nothing, the same way one that never ran does. + pub fn described(&self, path: &str) -> Vec<(String, usize)> { + self.of(path) + .iter() + .filter(|(_, value)| *value != 0) + .cloned() + .collect() + } + + /// What this node recorded, in [`NodeMetric::ALL`] order, or an empty list when it recorded + /// nothing. + pub fn of(&self, path: &str) -> &[(String, usize)] { + self.nodes.get(path).map(Vec::as_slice).unwrap_or_default() + } +} + +/// Splits a registered metric name into the node path and the figure it reports. +/// +/// A name is `native..`, and `native.p..` for a figure that +/// is registered per partition. The partition belongs to that registration rather than to the +/// node, so it is dropped here and its values are combined by [`NodeMetricsSnapshot::read`]. A name +/// that is not one of this module's is not a node figure, and is left to whoever else reads `set`. +fn split_name(name: &str) -> Option<(&str, &str)> { + let rest = name.strip_prefix(NATIVE_PREFIX)?.strip_prefix('.')?; + let rest = match rest.strip_prefix('p').and_then(|rest| rest.split_once('.')) { + Some((partition, rest)) + if !partition.is_empty() && partition.bytes().all(|byte| byte.is_ascii_digit()) => + { + rest + } + _ => rest, + }; + let (path, metric) = rest.rsplit_once('.')?; + NodeMetric::ALL.contains(&metric).then_some((path, metric)) +} + +/// Where `metric` sits in the declared order, so a node's figures read the same on every node and +/// on every run. +fn node_metric_order(metric: &str) -> usize { + NodeMetric::ALL + .iter() + .position(|declared| *declared == metric) + .unwrap_or(usize::MAX) +} + /// The metric handles one node updates as it runs. /// /// The node writes these directly; it never looks a metric up by name, and it never formats a path @@ -125,6 +224,12 @@ impl Default for NodeMetrics { } } +/// Whether `name` is a figure a node of a native tree reports, rather than one of an operator. +pub fn is_node_metric(name: &str) -> bool { + name.strip_prefix(NATIVE_PREFIX) + .is_some_and(|rest| rest.starts_with('.')) +} + /// The full metric name for `metric` reported by the node called `name`. fn named(name: &str, metric: &str) -> String { format!("{NATIVE_PREFIX}.{name}.{metric}") @@ -286,9 +391,11 @@ impl NodeMetrics { #[cfg(test)] mod tests { - use datafusion::physical_plan::metrics::{ExecutionPlanMetricsSet, MetricValue}; + use datafusion::physical_plan::metrics::{ + Count, ExecutionPlanMetricsSet, MetricBuilder, MetricValue, + }; - use super::{NodeMetric, NodeMetrics}; + use super::{NodeMetric, NodeMetrics, NodeMetricsSnapshot}; fn metrics_named(set: &ExecutionPlanMetricsSet) -> Vec { set.clone_inner() @@ -382,6 +489,66 @@ mod tests { ); } + /// A node is registered once per partition, so one node's figures are the sum of its + /// partitions' - except the peak, which is the highest one, because partitions peak at + /// different moments. + #[test] + fn a_node_reports_one_set_of_figures_across_its_partitions() { + let set = ExecutionPlanMetricsSet::new(); + for (partition, batches, samples, peak) in [(0, 2, 10, 1024), (1, 3, 20, 4096)] { + let metrics = NodeMetrics::default(); + metrics.record_input(batches, samples); + metrics.record_reserved_bytes(peak); + metrics.register("root", &set, partition); + } + + let snapshot = NodeMetricsSnapshot::read(&set); + let figure = |name: &str| { + snapshot + .of("root") + .iter() + .find_map(|(figure, value)| (figure == name).then_some(*value)) + .unwrap_or_else(|| panic!("{name} is reported")) + }; + assert_eq!(figure(NodeMetric::INPUT_BATCHES), 5); + assert_eq!(figure(NodeMetric::INPUT_SAMPLES), 30); + assert_eq!(figure(NodeMetric::PEAK_RESERVED_BYTES), 4096); + } + + /// The path is what the registration named, and a partition segment is not part of it: the two + /// partitions above file under one node rather than under `p0.root` and `p1.root`. + #[test] + fn a_partition_belongs_to_the_registration_and_not_to_the_path() { + let set = ExecutionPlanMetricsSet::new(); + NodeMetrics::default().register("root.left", &set, 7); + + let snapshot = NodeMetricsSnapshot::read(&set); + assert!(!snapshot.of("root.left").is_empty()); + assert!(snapshot.of("root").is_empty()); + assert!(snapshot.of("p7.root.left").is_empty()); + } + + /// A metric this module did not name is not a node figure, so it is not filed under a node. + #[test] + fn a_name_outside_the_grammar_is_not_a_node_figure() { + let set = ExecutionPlanMetricsSet::new(); + let metrics = NodeMetrics::default(); + metrics.register("root", &set, 0); + MetricBuilder::new(&set).build(MetricValue::Count { + name: "output_rows".into(), + count: Count::new(), + }); + + let snapshot = NodeMetricsSnapshot::read(&set); + assert_eq!(snapshot.of("root").len(), NodeMetric::ALL.len()); + assert!( + snapshot + .of("root") + .iter() + .all(|(name, _)| NodeMetric::ALL.contains(&name.as_str())) + ); + } + #[test] fn a_peak_is_a_high_water_mark() { let metrics = NodeMetrics::default(); diff --git a/src/promql/src/native/mod.rs b/src/promql/src/native/mod.rs index 06e1525f264..3f561281e3d 100644 --- a/src/promql/src/native/mod.rs +++ b/src/promql/src/native/mod.rs @@ -30,7 +30,7 @@ mod aggregate; pub mod batch; pub mod canonicalize; -mod display; +pub mod display; pub mod execute; pub mod metrics; pub mod plan; diff --git a/src/promql/src/native/plan/tests.rs b/src/promql/src/native/plan/tests.rs index efefef74ff5..efd8eeb3d02 100644 --- a/src/promql/src/native/plan/tests.rs +++ b/src/promql/src/native/plan/tests.rs @@ -1848,14 +1848,8 @@ fn a_scalar_root_claiming_samples_is_refused() { metric_name: None, }; - let error = FusedSeries::try_new( - Arc::from("1"), - tree, - binding, - Vec::new(), - Arc::new(DFSchema::empty()), - ) - .expect_err("a samples root reads no source while reporting samples"); + let error = FusedSeries::try_new(tree, binding, Vec::new(), Arc::new(DFSchema::empty())) + .expect_err("a samples root reads no source while reporting samples"); assert!( error.to_string().contains("does not produce a scalar"), @@ -1873,14 +1867,8 @@ fn a_samples_root_without_an_input_is_refused() { tags: vec![], metric_name: None, }; - let error = FusedSeries::try_new( - Arc::from("some_metric"), - tree, - binding, - Vec::new(), - Arc::new(DFSchema::empty()), - ) - .expect_err("a samples root without an input is refused"); + let error = FusedSeries::try_new(tree, binding, Vec::new(), Arc::new(DFSchema::empty())) + .expect_err("a samples root without an input is refused"); assert!(error.to_string().contains("source indexes"), "{error}"); } diff --git a/src/promql/src/native/segment.rs b/src/promql/src/native/segment.rs index ca7eb7994b6..c3f154097d5 100644 --- a/src/promql/src/native/segment.rs +++ b/src/promql/src/native/segment.rs @@ -612,7 +612,6 @@ fn fused_publish_grid(root: &NativeNode, statement_grid: &Arc) -> Arc< /// anything else reads the assembled frames as an ordinary evaluated input. #[allow(clippy::too_many_arguments)] pub fn canonical_boundary_stages( - promql: Arc, tree: &NativeTree, children: &[LogicalPlan], source_tags: &[Vec], @@ -649,7 +648,6 @@ pub fn canonical_boundary_stages( // The producer: the statement's own tree, run per partition, exporting a frame. It runs above // the regions, where its router collects each label set from every region's contribution. let producer = FusedSeries::try_new_canonical_producer( - Arc::clone(&promql), tree.clone(), FusedSeriesBinding { grid: Arc::clone(grid), @@ -688,7 +686,6 @@ pub fn canonical_boundary_stages( None }; let consumer = FusedSeries::try_new_with_format( - Arc::clone(&promql), consumer_tree, FusedSeriesBinding { grid: Arc::clone(grid), @@ -756,7 +753,6 @@ pub fn is_empty_discovered_input(plan: &LogicalPlan) -> bool { #[allow(clippy::too_many_arguments, clippy::type_complexity)] fn build_evaluated_segment( - promql: Arc, node: &NativeNode, children: &[LogicalPlan], source_tags: &[Vec], @@ -818,7 +814,6 @@ fn build_evaluated_segment( // boundary and read as a frame. if matches!(tree.root().expr, NativeExpr::Merge { .. }) { let (plan, tags) = canonical_boundary_stages( - Arc::clone(&promql), &tree, &inputs, &local_tags, @@ -841,7 +836,6 @@ fn build_evaluated_segment( let frame = DFSchema::try_from(frame.as_ref().clone()).ok()?; let publish_grid = fused_publish_grid(tree.root(), grid); let plan = FusedSeries::try_new_with_format( - Arc::from("segment"), tree, FusedSeriesBinding { grid: publish_grid, @@ -932,7 +926,6 @@ fn build_inner_segment( let frame = FusedSeries::series_frame_schema(&tree).ok()?; let frame = DFSchema::try_from(frame.as_ref().clone()).ok()?; let plan = FusedSeries::try_new_with_format( - Arc::from("segment"), tree, FusedSeriesBinding { grid: segment_grid, @@ -962,7 +955,6 @@ fn build_inner_segment( /// root publishes rows. #[allow(clippy::too_many_arguments, clippy::type_complexity)] pub fn segment_tree( - promql: Arc, tree: &NativeTree, children: &[LogicalPlan], source_tags: &[Vec], @@ -1046,7 +1038,6 @@ pub fn segment_tree( let built = if first_iteration && contains_global_consumer(cut) { let cut_tree = NativeTree::try_new(cut.clone()).ok()?; let plan = segment_tree( - Arc::clone(&promql), &cut_tree, ¤t_children, ¤t_tags, @@ -1086,7 +1077,6 @@ pub fn segment_tree( // will not place region-locally, so this fallback only ever sees the cuts it admitted. let built = built.or_else(|| { build_evaluated_segment( - Arc::clone(&promql), cut, ¤t_children, ¤t_tags, @@ -1153,7 +1143,6 @@ pub fn segment_tree( } } return canonical_boundary_stages( - Arc::clone(&promql), &parent, &inputs, &child_tags, @@ -1211,7 +1200,6 @@ pub fn segment_tree( // rejects such a segment, and the rejection aborts the whole route. let publish_grid = fused_publish_grid(parent.root(), grid); let Ok(plan) = FusedSeries::try_new_with_format( - Arc::clone(&promql), parent, FusedSeriesBinding { grid: publish_grid, diff --git a/src/query/src/analyze.rs b/src/query/src/analyze.rs index 34f1c69ee30..e66e3be1c7b 100644 --- a/src/query/src/analyze.rs +++ b/src/query/src/analyze.rs @@ -22,7 +22,9 @@ use std::sync::Arc; use ahash::HashMap; use arrow::array::{StringBuilder, UInt32Builder}; use arrow_schema::{DataType, Field, Schema, SchemaRef}; -use common_recordbatch::adapter::{MetricCollector, PlanMetrics, RecordBatchMetrics}; +use common_recordbatch::adapter::{ + MetricCollector, NativeTreeMetrics, PlanMetrics, RecordBatchMetrics, +}; use common_recordbatch::{DfRecordBatch, DfSendableRecordBatchStream}; use datafusion::error::Result as DfResult; use datafusion::execution::TaskContext; @@ -354,6 +356,7 @@ struct JsonMetrics { } impl JsonMetrics { + /// Rebuilds the plan tree from the collector's flat, level-tagged list. fn from_record_batch_metrics(record_batch_metrics: RecordBatchMetrics) -> Self { let mut layers: HashMap> = HashMap::default(); @@ -361,7 +364,9 @@ impl JsonMetrics { for plan_metrics in record_batch_metrics.plan_metrics.into_iter().rev() { let (level, mut metrics) = Self::from_plan_metrics(plan_metrics); if let Some(next_layer) = layers.remove(&(level + 1)) { - metrics.children = next_layer; + // The nodes an operator runs come first (it already collected them), then the + // physical children the plan gives it: both hang from the same line in the text. + metrics.children.extend(next_layer); } if level == 0 { metrics.memory_usage = Some(memory_usage); @@ -374,15 +379,48 @@ impl JsonMetrics { Self::default() } + /// One node of a tree an operator runs inside itself, and everything below it, as a plan node. + /// + /// The text `ANALYZE` prints has the same nodes inside the operator's line; here each one is a + /// node, so a reader reaches a native operator by name instead of by parsing that text. + fn from_native_metrics(native: NativeTreeMetrics) -> Self { + let NativeTreeMetrics { + line, + metrics, + children, + } = native; + let (name, param) = line.split_once(": ").unwrap_or((line.as_str(), "")); + Self { + name: name.to_string(), + param: param.to_string(), + // A native node reports figures of its own kind (`groups`, `output_values`, ...) and + // neither of the two figures a DataFusion operator leads with, so they stay unset rather + // than being invented from a figure that means something else. + output_rows: 0, + elapsed_compute: 0, + metrics: metrics.into_iter().collect(), + memory_usage: None, + children: children + .into_iter() + .map(Self::from_native_metrics) + .collect(), + } + } + /// Convert a [`PlanMetrics`] to a [`JsonMetrics`] without children. /// /// Returns the level of the plan and the [`JsonMetrics`]. fn from_plan_metrics(plan_metrics: PlanMetrics) -> (usize, Self) { + // An operator's line is its own line: one that draws nodes below itself carries them in the + // same string for the text, and they are nodes of their own here (`from_native_metrics`). let raw_name = plan_metrics.plan.trim_end(); + let operator_line = raw_name.lines().next().unwrap_or_default(); let mut elapsed_compute = 0; let mut output_rows = 0; let mut other_metrics = HashMap::default(); - let (name, param) = raw_name.split_once(": ").unwrap_or((raw_name, "")); + let (name, param) = operator_line + .split_once(": ") + .unwrap_or((operator_line, "")); for (name, value) in plan_metrics.metrics.into_iter() { if name == "elapsed_compute" { @@ -394,6 +432,13 @@ impl JsonMetrics { } } + // The nodes an operator runs consume what it consumes, which is what a child position + // means: they come first, then the physical children the plan gives it. + let children = plan_metrics + .native + .map(Self::from_native_metrics) + .into_iter() + .collect(); ( plan_metrics.level, Self { @@ -403,7 +448,7 @@ impl JsonMetrics { elapsed_compute, metrics: other_metrics, memory_usage: None, - children: vec![], + children, }, ) } @@ -418,6 +463,15 @@ impl Display for JsonMetrics { #[cfg(test)] mod tests { use datafusion::physical_plan::empty::EmptyExec; + use datatypes::series::SeriesOrdering; + use datatypes::series::frame::{FrameLabelMode, raw_series_frame_schema}; + use datatypes::series::samples::{FieldSpec, SeriesSchema}; + use datatypes::series::time::{EvalGrid, GridSpec}; + use promql::extension_plan::{FusedSeries, FusedSeriesBinding}; + use promql::native::plan::{ + BoundInput, BoundInstant, InstantValue, NativeExpr, NativeNode, NativeTree, NativeType, + }; + use store_api::region_engine::RawLabelSource; use super::*; @@ -519,6 +573,124 @@ mod tests { assert!(plan_param.is_empty()); } + /// A native operator's tree is nested below the operator, not carried inside its `param`: the + /// operator line stops at its own line, and each node it runs is a node with its own figures. + /// + /// The tree arrives with the collected record, so this also holds for a stage collected on + /// another node, which is the only place a fused operator runs in a standalone server. + #[test] + fn qbs_analyze_json_nests_the_native_tree_below_its_operator() { + let grid = Arc::new(EvalGrid::try_new(GridSpec::try_new(0, 1_000, 2).unwrap()).unwrap()); + let schema = Arc::new(SeriesSchema { + fields: Arc::from([FieldSpec { + name: Arc::from("value"), + }]), + eval_grid: None, + }); + let raw = NativeType::Samples(Arc::clone(&schema)); + let evaluated = NativeType::Samples(Arc::new(SeriesSchema { + fields: schema.fields.clone(), + eval_grid: Some(Arc::clone(&grid)), + })); + let tree = NativeTree::try_new(NativeNode { + expr: NativeExpr::Instant { + input: Box::new(NativeNode { + expr: NativeExpr::Input(BoundInput { + source_index: 0, + output: raw.clone(), + ordering: SeriesOrdering::Contiguous, + }), + output: raw, + }), + op: BoundInstant { + lookback_ms: 0, + offset_ms: 0, + at_ms: None, + value: InstantValue::Sample, + output_grid: Arc::clone(&grid), + }, + }, + output: evaluated, + }) + .unwrap(); + let frame_schema = raw_series_frame_schema(&schema, FrameLabelMode::Explicit).unwrap(); + let child = datafusion::logical_expr::LogicalPlan::EmptyRelation( + datafusion::logical_expr::EmptyRelation { + produce_one_row: false, + schema: Arc::new( + datafusion_common::DFSchema::try_from(frame_schema.clone()).unwrap(), + ), + }, + ); + let binding = FusedSeriesBinding { + grid, + metric_name: None, + time_index: "time".to_string(), + tags: vec![], + }; + let rows = Arc::new(Schema::new(vec![ + Field::new("value", DataType::Float64, true), + Field::new( + "time", + DataType::Timestamp(arrow_schema::TimeUnit::Millisecond, None), + false, + ), + ])); + let frame = FusedSeries::series_frame_schema(&tree).unwrap(); + for (output, format, header, param) in [ + ( + rows, + promql::extension_plan::FusedOutputFormat::PlainRows, + "FusedSeriesExec", + "", + ), + ( + frame, + promql::extension_plan::FusedOutputFormat::SeriesFrame, + "FusedSeriesExec: (output: series_frame)", + "(output: series_frame)", + ), + ] { + let fused = FusedSeries::try_new_with_format( + tree.clone(), + binding.clone(), + vec![child.clone()], + Arc::new(datafusion_common::DFSchema::try_from(output).unwrap()), + format, + ) + .unwrap(); + let exec = fused + .to_execution_plan(vec![( + Arc::new(EmptyExec::new(frame_schema.clone())), + RawLabelSource::Explicit, + SeriesOrdering::Contiguous, + )]) + .unwrap(); + let mut collector = MetricCollector::new(false); + accept(exec.as_ref(), &mut collector).unwrap(); + let mut collected = collector.record_batch_metrics; + collected.plan_metrics.truncate(1); + let plan = &collected.plan_metrics[0]; + assert_eq!(plan.plan.lines().next().unwrap(), header, "{}", plan.plan); + assert!(plan.native.is_some(), "the native tree must be collected"); + let metrics = JsonMetrics::from_record_batch_metrics(collected); + let value = serde_json::to_value(metrics).unwrap(); + + assert_eq!(value["name"], "FusedSeriesExec"); + assert_eq!(value["param"], param); + assert_eq!(value["children"][0]["name"], "Instant"); + assert_eq!( + value["children"][0]["param"], + "lookback 0ms offset 0 on grid 0/1000x2" + ); + assert_eq!(value["children"][0]["children"][0]["name"], "Input"); + assert_eq!( + value["children"][0]["children"][0]["param"], + "raw samples source=0, contiguous" + ); + } + } + #[test] fn qbs_analyze_json_splits_plan_name_and_parameters() { let (_, metrics) = JsonMetrics::from_plan_metrics(PlanMetrics { @@ -526,6 +698,7 @@ mod tests { plan_name: "FilterExec".to_string(), level: 0, metrics: vec![], + native: None, }); assert_eq!(metrics.name, "FilterExec"); @@ -542,12 +715,14 @@ mod tests { plan_name: "RootExec".to_string(), level: 0, metrics: vec![("mem_used".to_string(), 24)], + native: None, }, PlanMetrics { plan: "ChildExec".to_string(), plan_name: "ChildExec".to_string(), level: 1, metrics: vec![("mem_used".to_string(), 18)], + native: None, }, ], ..Default::default() diff --git a/src/query/src/dist_plan/analyzer/test.rs b/src/query/src/dist_plan/analyzer/test.rs index 86e66d85587..fabbdcbf394 100644 --- a/src/query/src/dist_plan/analyzer/test.rs +++ b/src/query/src/dist_plan/analyzer/test.rs @@ -337,7 +337,6 @@ fn hybrid_fused(scan: LogicalPlan, metric: &str) -> LogicalPlan { }); let tree = compile(&stmt, series, SeriesOrdering::KeyThenTime).unwrap(); let fused = FusedSeries::try_new( - Arc::from(metric), tree, FusedSeriesBinding { grid: statement_grid(&stmt).unwrap(), @@ -422,7 +421,12 @@ fn hybrid_binary_join_rewrites_each_native_operand_boundary() { .as_any() .downcast_ref::() .expect("MergeScan remote input must be a FusedSeries root"); - assert_eq!(expected_table, fused.promql()); + assert!( + fused + .tree() + .to_string() + .contains("Input: raw samples source=0") + ); assert_eq!(1, fused.children().len()); let [LogicalPlan::TableScan(table_scan)] = fused.children() else { panic!("FusedSeries operand must directly consume a TableScan: {fused:?}"); diff --git a/src/query/src/dist_plan/commutativity.rs b/src/query/src/dist_plan/commutativity.rs index 35fd401077d..7d9a48a6aaa 100644 --- a/src/query/src/dist_plan/commutativity.rs +++ b/src/query/src/dist_plan/commutativity.rs @@ -559,14 +559,7 @@ mod tests { ); LogicalPlan::Extension(Extension { node: Arc::new( - FusedSeries::try_new( - Arc::from("some_metric{host=\"left\"} + on(host) some_metric{host=\"right\"}"), - tree, - binding, - vec![left, right], - output_schema, - ) - .unwrap(), + FusedSeries::try_new(tree, binding, vec![left, right], output_schema).unwrap(), ), }) } @@ -613,14 +606,7 @@ mod tests { LogicalPlan::Extension(Extension { node: Arc::new( - FusedSeries::try_new( - Arc::from("some_metric"), - tree, - binding, - vec![input], - output_schema, - ) - .unwrap(), + FusedSeries::try_new(tree, binding, vec![input], output_schema).unwrap(), ), }) } @@ -660,13 +646,7 @@ mod tests { let tree = NativeTree::try_new(merge).unwrap(); LogicalPlan::Extension(Extension { node: Arc::new( - FusedSeries::try_new_canonical_producer( - Arc::from("some_metric"), - tree, - binding, - vec![input], - ) - .unwrap(), + FusedSeries::try_new_canonical_producer(tree, binding, vec![input]).unwrap(), ), }) } @@ -1246,15 +1226,9 @@ mod tests { let (partial_tree, final_tree) = FusedSeries::split_aggregate(&tree, "ts").unwrap().unwrap(); - let partial = FusedSeries::try_new_state_export( - Arc::from("sum(some_metric)"), - partial_tree, - binding.clone(), - vec![input], - ) - .unwrap(); + let partial = + FusedSeries::try_new_state_export(partial_tree, binding.clone(), vec![input]).unwrap(); let final_node = FusedSeries::try_new( - Arc::from("sum(some_metric)"), final_tree, binding, vec![LogicalPlan::Extension(Extension { diff --git a/src/query/src/dist_plan/merge_scan.rs b/src/query/src/dist_plan/merge_scan.rs index 7f2c50f6346..7348267ed12 100644 --- a/src/query/src/dist_plan/merge_scan.rs +++ b/src/query/src/dist_plan/merge_scan.rs @@ -1772,6 +1772,7 @@ mod tests { plan_name: "higher region".to_string(), level: 0, metrics: Vec::new(), + native: None, }], ..Default::default() }; @@ -1781,6 +1782,7 @@ mod tests { plan_name: "lower region".to_string(), level: 0, metrics: Vec::new(), + native: None, }], ..Default::default() }; @@ -4155,6 +4157,7 @@ mod tests { plan_name: REGION_SCAN_EXEC_NAME.to_string(), level: 0, metrics: vec![("output_bytes".to_string(), 42)], + native: None, }], ..Default::default() }; @@ -4170,6 +4173,7 @@ mod tests { plan_name: "ProjectionExec".to_string(), level: 0, metrics: vec![("output_bytes".to_string(), 42)], + native: None, }], ..Default::default() }; @@ -4186,12 +4190,14 @@ mod tests { plan_name: REGION_SCAN_EXEC_NAME.to_string(), level: 0, metrics: vec![("output_bytes".to_string(), 42)], + native: None, }, PlanMetrics { plan: "RegionScanExec: region=2".to_string(), plan_name: REGION_SCAN_EXEC_NAME.to_string(), level: 0, metrics: vec![("output_bytes".to_string(), 18)], + native: None, }, ], ..Default::default() @@ -4237,6 +4243,7 @@ mod tests { plan_name: REGION_SCAN_EXEC_NAME.to_string(), level: 0, metrics: vec![("output_bytes".to_string(), 24)], + native: None, }], ..Default::default() }; @@ -4306,6 +4313,7 @@ mod tests { plan_name: REGION_SCAN_EXEC_NAME.to_string(), level: 0, metrics: vec![("output_bytes".to_string(), 24)], + native: None, }], ..Default::default() }; diff --git a/src/query/src/optimizer/scan_hint.rs b/src/query/src/optimizer/scan_hint.rs index a76371fe233..e56ea4a900e 100644 --- a/src/query/src/optimizer/scan_hint.rs +++ b/src/query/src/optimizer/scan_hint.rs @@ -1657,14 +1657,7 @@ mod test { LogicalPlan::Extension(Extension { node: Arc::new( - FusedSeries::try_new( - Arc::from("some_metric"), - tree, - binding, - vec![input], - output_schema, - ) - .unwrap(), + FusedSeries::try_new(tree, binding, vec![input], output_schema).unwrap(), ), }) } diff --git a/src/query/src/optimizer/series_frame_output.rs b/src/query/src/optimizer/series_frame_output.rs index c27dbf04ba4..389f2cd42ac 100644 --- a/src/query/src/optimizer/series_frame_output.rs +++ b/src/query/src/optimizer/series_frame_output.rs @@ -382,7 +382,6 @@ mod tests { .expect("the frame is a legal schema"), ); let fused = FusedSeries::try_new_with_format( - Arc::from("rate(x[5m])"), tree, FusedSeriesBinding { grid, @@ -449,7 +448,6 @@ mod tests { .expect("the frame is a legal schema"), ); let fused = FusedSeries::try_new_with_format( - Arc::from("cpu"), tree, FusedSeriesBinding { grid, diff --git a/src/query/src/promql/native_compile/tests.rs b/src/query/src/promql/native_compile/tests.rs index ddac7cb7d81..95cc20190c6 100644 --- a/src/query/src/promql/native_compile/tests.rs +++ b/src/query/src/promql/native_compile/tests.rs @@ -1056,9 +1056,8 @@ fn the_display_of_a_compiled_tree_is_the_tree() { assert_eq!( tree.to_string(), [ - "NativeTree", - " root Window rate [300000ms] offset 0 anchor - on grid 0/5000x21", - " input Input raw samples source=0", + "Window: rate [300000ms] offset 0 anchor - on grid 0/5000x21", + " Input: raw samples source=0", ] .join("\n") ); @@ -1073,32 +1072,29 @@ fn the_display_of_a_tree_the_batch_added_to_is_the_query() { ( "sum by(job)(rate(x[5m]) * 100)", &[ - "NativeTree", - " root Aggregate sum by [job] complete", - " input Binary * ignoring [] one-to-one", - " left Window rate [300000ms] offset 0 anchor - on grid 0/5000x21", - " input Input raw samples source=0", - " right Scalar 100", + "Aggregate: sum by [job] complete", + " Binary: * ignoring [] one-to-one", + " Window: rate [300000ms] offset 0 anchor - on grid 0/5000x21", + " Input: raw samples source=0", + " Scalar: 100", ], ), ( "-x", &[ - "NativeTree", - " root Binary * ignoring [] one-to-one", - " left Scalar -1", - " right Instant lookback 5000ms offset 0 on grid 0/5000x21", - " input Input raw samples source=0", + "Binary: * ignoring [] one-to-one", + " Scalar: -1", + " Instant: lookback 5000ms offset 0 on grid 0/5000x21", + " Input: raw samples source=0", ], ), ( "0 < x", &[ - "NativeTree", - " root Binary < ignoring [] one-to-one", - " left Scalar 0", - " right Instant lookback 5000ms offset 0 on grid 0/5000x21", - " input Input raw samples source=0", + "Binary: < ignoring [] one-to-one", + " Scalar: 0", + " Instant: lookback 5000ms offset 0 on grid 0/5000x21", + " Input: raw samples source=0", ], ), ]; @@ -1307,10 +1303,10 @@ fn the_supported_math_functions_map_to_their_kernels() { let tree = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime) .unwrap_or_else(|error| panic!("`{query}` was refused: {error}")); let display = tree.to_string(); - let root = display.lines().nth(1).expect("a compiled tree has a root"); + let root = display.lines().next().expect("a compiled tree has a root"); assert_eq!( root, - format!(" root Map {op}"), + format!("Map: {op}"), "`{query}` bound the wrong kernel" ); } @@ -2974,8 +2970,8 @@ fn the_aggregates_that_are_not_a_fold_bind_a_vector_operation() { let tree = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime) .unwrap_or_else(|error| panic!("`{query}` was refused: {error}")); let display = tree.to_string(); - let root = display.lines().nth(1).expect("a compiled tree has a root"); - assert_eq!(root, format!(" root Vector {op}"), "`{query}`"); + let root = display.lines().next().expect("a compiled tree has a root"); + assert_eq!(root, format!("Vector: {op}"), "`{query}`"); } let compiled = compile_selectors( @@ -4419,8 +4415,8 @@ fn whole_vector_functions_bind_a_vector_operation() { let tree = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime) .unwrap_or_else(|error| panic!("`{query}` was refused: {error}")); let display = tree.to_string(); - let root = display.lines().nth(1).expect("a compiled tree has a root"); - assert_eq!(root, format!(" root Vector {op}"), "`{query}`"); + let root = display.lines().next().expect("a compiled tree has a root"); + assert_eq!(root, format!("Vector: {op}"), "`{query}`"); } } @@ -4451,12 +4447,12 @@ fn absent_over_time_binds_absent_over_a_presence_read() { let tree = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime) .unwrap_or_else(|error| panic!("`{query}` was refused: {error}")); let display = tree.to_string(); - let root = display.lines().nth(1).expect("a compiled tree has a root"); - assert_eq!(root, format!(" root Vector {op}"), "`{query}`"); + let root = display.lines().next().expect("a compiled tree has a root"); + assert_eq!(root, format!("Vector: {op}"), "`{query}`"); // The fold that reports presence keeps the metric name, so two discovered metrics that // share their ordinary labels stay two series on the way up: `present_over_time` drops it. assert!( - display.contains("input Window last_over_time [300000ms]"), + display.contains(" Window: last_over_time [300000ms]"), "`{query}` does not read its window through the presence fold: {display}" ); } @@ -5221,8 +5217,8 @@ fn label_functions_bind_their_change_into_a_map() { let tree = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime) .unwrap_or_else(|error| panic!("`{query}` was refused: {error}")); let display = tree.to_string(); - let root = display.lines().nth(1).expect("a compiled tree has a root"); - assert_eq!(root, format!(" root Map {op}"), "`{query}`"); + let root = display.lines().next().expect("a compiled tree has a root"); + assert_eq!(root, format!("Map: {op}"), "`{query}`"); } } @@ -5244,8 +5240,8 @@ fn histogram_functions_bind_their_kernel_into_a_map() { ) .unwrap_or_else(|error| panic!("`{query}` was refused: {error}")); let display = tree.to_string(); - let root = display.lines().nth(1).expect("a compiled tree has a root"); - assert_eq!(root, format!(" root Map {name}"), "`{query}`"); + let root = display.lines().next().expect("a compiled tree has a root"); + assert_eq!(root, format!("Map: {name}"), "`{query}`"); } } diff --git a/src/query/src/promql/planner/native.rs b/src/query/src/promql/planner/native.rs index da6ff6c4a79..2ebf124afac 100644 --- a/src/query/src/promql/planner/native.rs +++ b/src/query/src/promql/planner/native.rs @@ -562,7 +562,6 @@ impl PromPlanner { } }; let Ok(node) = FusedSeries::try_new_with_format( - Arc::from(expr.to_string()), tree, FusedSeriesBinding { grid: Arc::clone(&left_grid), @@ -732,7 +731,6 @@ impl PromPlanner { } }; let node = FusedSeries::try_new_with_format( - Arc::from(prom_expr.to_string()), tree, FusedSeriesBinding { grid: Arc::clone(&grid), @@ -1466,7 +1464,6 @@ impl PromPlanner { 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_stages( - Arc::from(prom_expr.to_string()), &tree, &children, &source_tags, @@ -1556,7 +1553,6 @@ impl PromPlanner { if required_placement || matches!(locality, InputLocality::SinglePartition) { let segment_metric = if cross_table { None } else { metric.clone() }; let plan = segment_tree( - Arc::from(prom_expr.to_string()), &tree, &children, &source_tags, @@ -1645,7 +1641,6 @@ impl PromPlanner { // binding is that grid rather than a per-shape choice. let publish_grid = Arc::clone(&grid); let node = FusedSeries::try_new_with_format( - Arc::from(prom_expr.to_string()), tree, FusedSeriesBinding { grid: publish_grid, @@ -1889,13 +1884,8 @@ fn plan_split_aggregate( }, ..binding.clone() }; - let partial = FusedSeries::try_new_state_export( - Arc::clone(&promql), - partial, - partial_binding, - vec![rawinput], - ) - .context(DataFusionPlanningSnafu)?; + let partial = FusedSeries::try_new_state_export(partial, partial_binding, vec![rawinput]) + .context(DataFusionPlanningSnafu)?; 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 @@ -1907,7 +1897,6 @@ fn plan_split_aggregate( } }; FusedSeries::try_new_with_format( - promql, final_tree, binding, vec![LogicalPlan::Extension(Extension { diff --git a/src/query/src/promql/planner/test.rs b/src/query/src/promql/planner/test.rs index 7f56f3fd2c3..ebd38d6564c 100644 --- a/src/query/src/promql/planner/test.rs +++ b/src/query/src/promql/planner/test.rs @@ -2575,14 +2575,14 @@ async fn native_absent_over_time_plans_over_discovered_metrics() { let fused = find_fused_series(&plan).expect("fused plan"); let tree = fused.tree().to_string(); assert!( - tree.contains(r#"root Vector absent {no_such_label="x", tag_0="api"}"#), + tree.contains(r#"Vector: absent {no_such_label="x", tag_0="api"}"#), "the labels are the selector's matchers: {tree}" ); // A statement whose window runs where its samples are is planned as one tree per stage, so the // presence read is asserted across the plan rather than under the root node the frontend holds. let trees = fused_trees(&plan).join("\n"); assert!( - trees.contains("Window last_over_time [300000ms]"), + trees.contains("Window: last_over_time [300000ms]"), "the presence read folds the window: {trees}" ); // The metric name is not a label of an `absent` answer, so the output has none. @@ -2620,7 +2620,7 @@ async fn native_absent_over_time_plans_over_discovered_metrics() { let fused = find_fused_series(&plan).expect("fused plan"); let tree = fused.tree().to_string(); assert!( - tree.contains(r#"root Vector absent {no_such_label="x", tag_0="api"}"#), + tree.contains(r#"Vector: absent {no_such_label="x", tag_0="api"}"#), "a selector that matched no table still answers its matchers: {tree}" ); @@ -2658,7 +2658,7 @@ async fn native_absent_over_time_plans_over_discovered_metrics() { let fused = find_fused_series(&plan).expect("fused plan"); let tree = fused.tree().to_string(); assert!( - tree.contains(r#"root Vector absent {tag_0="x"}"#), + tree.contains(r#"Vector: absent {tag_0="x"}"#), "a named metric is not a label of the answer: {tree}" ); } @@ -3139,14 +3139,13 @@ async fn native_fused_rate_plans_to_a_fused_series_node() { let display = plan.display_indent().to_string(); assert!( - display.contains("FusedSeries: rate(some_metric[5m])"), + display.contains("native_tree=[\nWindow: rate [300000ms]"), "{display}" ); // The node reads the selector's own scan; nothing between them packs the frame it reads. assert!(display.contains("TableScan: some_metric"), "{display}"); - // The node carries the query a reader wrote, not the shape it compiled to. + // The node keeps the compiled tree, not the query text. let fused = find_fused_series(&plan).expect("the plan holds a native node"); - assert_eq!("rate(some_metric[5m])", fused.promql()); assert_eq!( // The rows layout declares the per-row name column behind the labels. vec!["timestamp", "field_0", "tag_0", "__name__"], @@ -4802,7 +4801,6 @@ async fn native_histogram_count_drops_float_input() { let query = "histogram_count(some_metric)"; let plan = build_native_fused_plan(query, true).await; let fused = find_fused_series(&plan).expect("histogram_count uses the native route"); - assert_eq!(parser::parse(query).unwrap().to_string(), fused.promql()); assert!(format!("{:?}", fused.tree()).contains("Count")); // The in-memory table scan is not a RegionScanExec or an explicit raw-frame source, so this // unit harness cannot execute a region-backed native plan. Native histogram kernels test that @@ -4814,7 +4812,9 @@ async fn native_ceil_maps_raw_float_input() { let query = "ceil(some_metric)"; let plan = build_native_fused_plan(query, true).await; let fused = find_fused_series(&plan).expect("ceil uses the native route"); - assert_eq!(parser::parse(query).unwrap().to_string(), fused.promql()); + let tree = fused.tree().to_string(); + assert!(tree.contains("Map: ceil"), "{tree}"); + assert!(tree.contains("Input: raw samples source=0"), "{tree}"); let mut names = plan .schema() .fields() @@ -4842,7 +4842,6 @@ async fn native_fused_binary_keeps_independent_scan_occurrences() { ] { let plan = build_native_fused_plan(query, true).await; let fused = find_fused_series(&plan).expect("the plan holds a native node"); - assert_eq!(parser::parse(query).unwrap().to_string(), fused.promql()); assert_eq!(2, fused.inputs().len(), "{query}"); if query.contains("on()") || query.contains("ignoring(tag_0)") { assert_eq!( @@ -5646,6 +5645,52 @@ fn sorted_column_names(plan: &LogicalPlan) -> Vec { names } +/// Render a split exactly as EXPLAIN displays the frontend and remote region plans. +#[tokio::test] +async fn native_split_explain_renders_each_tree_including_remote_input() { + let query = "100 - avg by (tag_0) (irate(some_metric[1m])) * 100"; + let plan = build_native_fused_plan(query, true).await; + let final_node = find_final_split(&plan).expect("split final"); + let [partial] = final_node.children() else { + panic!("split partial"); + }; + let remote = + crate::dist_plan::MergeScanLogicalPlan::new(partial.clone(), false).into_logical_plan(); + let final_node = final_node + .with_exprs_and_inputs(vec![], vec![remote]) + .unwrap(); + let logical = LogicalPlan::Extension(Extension { + node: Arc::new(final_node), + }); + let text = logical.display_indent().to_string(); + let verbose = logical.display_indent_schema().to_string(); + let (final_text, remote_text) = text + .split_once("MergeScan [is_placeholder=false, remote_input=[") + .expect("the partial is inside a merge scan"); + assert!( + final_text.starts_with("FusedSeries\nnative_tree=[\nBinary:"), + "{text}" + ); + assert!( + final_text.contains("Aggregate: avg by [tag_0] final"), + "{text}" + ); + assert!( + remote_text.contains( + "FusedSeries: (output: series_frame)\nnative_tree=[\nAggregate: avg by [tag_0] partial" + ), + "{text}" + ); + assert!(remote_text.contains("Window: irate [60000ms]"), "{text}"); + assert!( + remote_text.contains("Input: raw samples source=0"), + "{text}" + ); + assert_ne!(final_text, remote_text); + assert!(!text.contains("metrics="), "{text}"); + assert!(!verbose.contains("metrics="), "{verbose}"); +} + /// An aggregate is split: the merge stays on the frontend and the per-series prefix runs in the /// producer. Native names follow the compiled tree; grouping labels, values and timestamps retain /// their existing schema roles. @@ -5702,13 +5747,13 @@ async fn native_fused_aggregate_splits_into_a_final_over_a_partial_producer() { // above the Final aggregate rather than the literal `Histogram` op the constant form binds. match query { "histogram_quantile(time() / 150, sum by (le) (rate(some_metric[5m])))" => assert!( - final_display.contains("ParameterizedHistogram histogram_quantile"), + final_display.contains("ParameterizedHistogram: histogram_quantile"), "`{query}`: the split must carry the per-point parameter: {final_display}" ), "histogram_fraction(time() / 150, time() / 75 + 0.5, sum by (le) (rate(some_metric[5m])))" => { assert!( - final_display.contains("ParameterizedHistogram histogram_fraction"), + final_display.contains("ParameterizedHistogram: histogram_fraction"), "`{query}`: the split must carry the per-point parameters: {final_display}" ) } @@ -5879,7 +5924,6 @@ async fn native_fused_data_free_vector_does_not_block_segmentation() { .as_any() .downcast_ref::() .unwrap_or_else(|| panic!("expected a fused root rather than a legacy root: {native}")); - assert_eq!(parent.promql(), parser::parse(query).unwrap().to_string()); let NativeExpr::Binary { left, right, .. } = &parent.tree().root().expr else { panic!("expected the whole binary expression in the native parent: {native}"); }; @@ -6055,7 +6099,6 @@ async fn native_split_and_complete_aggregate_produce_identical_values() { let final_node = find_final_split(&split).expect("the query must use a split aggregate"); let partial = partial_below(final_node).unwrap(); let complete = FusedSeries::try_new( - Arc::from(query), complete_tree, final_node.binding().clone(), partial.children().to_vec(), @@ -6792,7 +6835,7 @@ async fn native_fused_histogram_above_aggregate_splits_with_bucket_routing() { // Only the Final node over state publishes this suffix, so the plan's own text pins it. assert!( display.contains( - " (state routing: canonical labels except [\"le\", \"__name__\", \"__type__\", \"__unit__\"])" + "FusedSeries: (state routing: canonical labels except [\"le\", \"__name__\", \"__type__\", \"__unit__\"])" ), "`{query}`: {display}" ); @@ -10006,7 +10049,7 @@ async fn native_histogram_selector_plans_a_native_producer() { let table_provider = build_test_native_histogram_table_provider("some_metric").await; let plan = build_native_fused_plan_with_provider("some_metric", true, table_provider).await; let display = plan.display_indent_schema().to_string(); - assert!(display.contains("FusedSeries: "), "{display}"); + assert!(display.contains("Input: raw samples source=0"), "{display}"); assert!(!display.contains("HistogramFold:"), "{display}"); let fused = find_fused_series(&plan).expect("the statement plans to a native producer"); // The output field column is the canonical native histogram struct. diff --git a/src/query/src/query_engine/state.rs b/src/query/src/query_engine/state.rs index 409ad798996..039cc26cb45 100644 --- a/src/query/src/query_engine/state.rs +++ b/src/query/src/query_engine/state.rs @@ -940,7 +940,6 @@ mod tests { }); for (disjoint, needs_router) in [(true, true), (false, true)] { let fused = FusedSeries::try_new( - Arc::from("metric"), tree.clone(), FusedSeriesBinding { grid: Arc::clone(&grid), diff --git a/tests-integration/src/tests/native_aggregate_test.rs b/tests-integration/src/tests/native_aggregate_test.rs index b70c6611a25..827d3fb4512 100644 --- a/tests-integration/src/tests/native_aggregate_test.rs +++ b/tests-integration/src/tests/native_aggregate_test.rs @@ -16,30 +16,37 @@ //! the real datanode in a single process. //! //! The table is ordinary - one region, no `PARTITION` clause - and its five series sit in four SST -//! files and one memtable range. With the scan asked for four partitions, each of them ends up with -//! at least one series; the per-partition metric assertion below checks that instead of assuming it. +//! files and one memtable range. The scan is asked for four partitions, and each of them ends up +//! with at least one series; what the producer folds is a different matter: the canonical router +//! hashes each series' stored key onto one of the scan-width owners over however many series each +//! scan partition delivers, so which owner folds a series, and how many owners fold anything at all, +//! is not a property of this fixture. The metric assertions below therefore pin totals only - the +//! values the partitions published between them and the sample positions they consumed between them - +//! and never which owner reported what. //! Every query runs twice: once with the native route asked for per request, once on the plan the //! existing planner builds. Both are compared with expectations worked out from the fixture, so //! neither run is the other's oracle. //! //! The native run plans two trees, and both are checked in the plans the engines really executed. -//! The region's tree must hold one fused producer reading the raw series scan directly - the scan -//! itself, or the one transparent cooperative-scheduling wrapper the engine may put above it, but -//! never an exchange - and `native.root.output_values` for every one of the four scan partitions; -//! the metric is read after the result stream was drained, the only point at which it is complete. -//! The frontend tree holds a Final `FusedSeriesExec` over a `SeriesRepartitionExec` and its -//! `MergeScanExec` leaf. The Partial producer and its raw `SeriesScanExec` input are in the -//! separately captured region physical plan, not in that frontend tree. Both trees are checked -//! independently through [`PhysicalPlanWrapper`], which both query engines install, so what is -//! checked is the plan that ran rather than a re-plan made for an `EXPLAIN`. +//! The fused producer must read the raw series scan itself - the scan, or the one transparent +//! cooperative-scheduling wrapper the engine may put above it, or the canonical +//! `SeriesRepartitionExec` that restores the scan width over a `MergeScanExec`, but never an +//! arbitrary exchange - at the width of the four scan partitions, and publish the values it folded +//! from them; the metric is read after the result stream was drained, the only point at which it is +//! complete. The frontend tree holds a Final `FusedSeriesExec` over a `SeriesRepartitionExec` and +//! its `MergeScanExec` leaf. The Partial producer sits in that tree over the region boundary, while +//! the raw `SeriesScanExec` it reads is in the separately captured region physical plan. Both trees +//! are checked independently through [`PhysicalPlanWrapper`], which both query engines install, so +//! what is checked is the plan that ran rather than a re-plan made for an `EXPLAIN`. //! //! Two tables carry the queries. [`COUNTER_TABLE`] holds the same five series in the same four //! persisted files and one memtable range as [`GAUGE_TABLE`], but its samples are a counter's, so //! `rate` over it is a nonzero number that is derived by hand in [`COUNTER_SERIES`]: the gauge's //! constant value has a rate of exactly zero and cannot tell a computed rate from a dropped one. The -//! counter queries are the acceptance ones for the state bridge - four nonempty partitions fold -//! partial states that only a merge over *all* of them turns into `sum`, `avg`, `count` and -//! `stddev` - while the gauge queries keep covering the shapes with round answers. +//! counter queries are the acceptance ones for the state bridge - the owners the router hashes the +//! five series onto fold partial states that only a merge over *all* of them finishes as `sum`, +//! `avg`, `count` and `stddev` - while the gauge queries keep covering the shapes with round +//! answers. //! //! The counter table carries one tag the gauge table does not, `region`: the four flushed series - //! the four files - are in one region and the series left in the memtable range is in another, so @@ -407,15 +414,19 @@ impl QueryResult { } /// One query, and what it must answer. +/// +/// Nothing here says which of the scan-width owners folds what: the canonical router hashes each +/// series' stored key onto one owner over however many series each scan partition delivers, so an +/// owner can be left with nothing even though every scan partition read rows. What a case pins is +/// distribution-independent - the rows and their values, the values the partitions published between +/// them, and the input samples they consumed between them. struct AggregateCase { promql: String, expected: Vec, - /// Whether every one of the four scan partitions must report a folded value. - per_partition: bool, - /// The number of values the partitions' producers must report together, when the answer fixes - /// that figure: one value per series that folded, which is what the counter fixture's queries - /// make checkable. With `per_partition` those are all four partitions' values and their spread - /// is bounded too. `None` leaves only "some value" to the assertion. + /// The number of input sample positions the producers must consume together, when the answer + /// fixes that figure: one per series that folded, which is what the counter fixture's queries make + /// checkable. The owners' shares are the router's hash, so only the total is pinned; `None` leaves + /// only "some value" to the assertion. root_values: Option, /// The stage marker the producer must show (`sum by [hostname] partial`), when the fixture /// fixes which aggregate the split cut out. The producer then exports that aggregate's *state*, @@ -430,7 +441,7 @@ struct AggregateCase { } /// Builds one case from a PromQL expression and its `(labels, value)` rows. -fn case(promql: String, expected: &[(&[(&str, &str)], f64)], per_partition: bool) -> AggregateCase { +fn case(promql: String, expected: &[(&[(&str, &str)], f64)]) -> AggregateCase { AggregateCase { promql, expected: expected @@ -444,7 +455,6 @@ fn case(promql: String, expected: &[(&[(&str, &str)], f64)], per_partition: bool value: Some(*value), }) .collect(), - per_partition, root_values: None, partial_state: None, single_owner: false, @@ -452,13 +462,14 @@ fn case(promql: String, expected: &[(&[(&str, &str)], f64)], per_partition: bool } } -/// A case over [`COUNTER_TABLE`], which additionally pins the values the four partitions fold and -/// the state the producer exports. +/// A case over [`COUNTER_TABLE`], which additionally pins the values the producer folds and the state +/// it exports. /// /// Every counter series is one series the scan keeps whole, and each of them decides exactly one -/// `rate` at the single grid point [`tql_eval`] evaluates, so the four partitions' producers together -/// report exactly as many values as there are counter series - and, because each partition folds at -/// least one of them, no partition can fold more than `series - (partitions - 1)`. +/// `rate` at the single grid point [`tql_eval`] evaluates, so the producers together consume exactly +/// one sample position per counter series and the total below is the fixture's series count. Which +/// owner folds which series - and how many owners fold anything - is the canonical router's hash of +/// each series' stored key, so no per-owner share and no count of used owners is pinned. /// /// `partial_state` is the aggregate the split cut out, as the producer's plan line names it: the /// innermost aggregate of the tree, whose state the partitions export. @@ -470,7 +481,7 @@ fn counter_case( AggregateCase { root_values: Some(COUNTER_SERIES.len()), partial_state: Some(partial_state), - ..case(promql, expected, true) + ..case(promql, expected) } } @@ -665,34 +676,40 @@ async fn native_hybrid_binary_05_06_08() { left.iter().chain(&right).all(Option::is_some), "{unpartitioned}: operand metrics must be registered for every partition: left={left:?}, right={right:?}" ); - // The fixture has one LHS api series, one RHS api series, and two RHS other series. - // The on(job) routers co-locate each group, so the only way to get these exact counts is - // api on the LHS partition and both other samples on a different matching partition. - let left_partition = left - .iter() - .position(|count| *count == Some(1)) - .unwrap_or_else(|| panic!("{unpartitioned}: missing single left value: {left:?}")); - let other_partition = right - .iter() - .position(|count| *count == Some(2)) - .unwrap_or_else(|| panic!("{unpartitioned}: missing duplicated right group: {right:?}")); - assert_ne!( - left_partition, other_partition, - "{unpartitioned}: left={left:?}, right={right:?}" - ); - let mut expected_left = vec![Some(0); SCAN_PARTITIONS]; - expected_left[left_partition] = Some(1); - let mut expected_right = vec![Some(0); SCAN_PARTITIONS]; - expected_right[left_partition] = Some(1); - expected_right[other_partition] = Some(2); - // Exact vectors also fix totals at 1 and 3 and require every other partition to be empty. + // The fixture has one LHS api series, one RHS api series, and two RHS other series. Which owner + // the `on(job)` routers place each group on is their hash of the group key, so the vectors are + // read for their totals and for the co-location the routers promise, never for which partition + // got what: + // + // * the LHS is one series, so its operand publishes one value, on one owner; + // * the RHS is one api series and the two `other` series of one duplicated group, so its operand + // publishes three values, and the group's pair ends up together - either on an owner of its own + // (the counts are 1 and 2) or on the api owner's (the counts are one 3); + // * api is api on both sides, so the RHS value sits on the owner the LHS value does. assert_eq!( - expected_left, left, - "{unpartitioned}: LHS values per partition" + vec![1], + nonzero_counts(&left), + "{unpartitioned}: the one LHS series is evaluated once, on one owner: {left:?}" ); assert_eq!( - expected_right, right, - "{unpartitioned}: RHS values per partition" + 3, + right.iter().flatten().sum::(), + "{unpartitioned}: the RHS publishes api and the duplicated other group: {right:?}" + ); + let right_counts = nonzero_counts(&right); + assert!( + right_counts == vec![1, 2] || right_counts == vec![3], + "{unpartitioned}: the duplicated `other` group must stay on one owner, so the RHS counts \ + are either api and the pair apart or all three together: {right:?}" + ); + let left_owner = left + .iter() + .position(|count| count.is_some_and(|count| count > 0)) + .unwrap_or_else(|| panic!("{unpartitioned}: no owner evaluated the LHS: {left:?}")); + assert!( + right[left_owner].is_some_and(|count| count > 0), + "{unpartitioned}: the matching router must put the RHS api value on the LHS api's owner \ + {left_owner}, but left={left:?} and right={right:?}" ); common_telemetry::info!( "`{unpartitioned}` operand values per matching partition: left={left:?}, right={right:?}" @@ -718,14 +735,21 @@ async fn native_hybrid_binary_05_06_08() { fused_node_count_per_partition(binary[0], "native.root.left.output_values", &empty_left); let control_right = fused_node_count_per_partition(binary[0], "native.root.right.output_values", &empty_left); - assert_eq!( - vec![Some(0); SCAN_PARTITIONS], - control_left, - "{empty_left}: left must be globally empty" + assert!( + control_left.iter().all(|count| *count == Some(0)), + "{empty_left}: left must be globally empty: {control_left:?}" ); + // The same duplicated RHS reaches the binary, in the same shape as above: its totals and the + // co-location of the pair are the fixture's, its placement is the router's hash. assert_eq!( - expected_right, control_right, - "{empty_left}: the same duplicated RHS must reach the binary" + 3, + control_right.iter().flatten().sum::(), + "{empty_left}: the same duplicated RHS must reach the binary: {control_right:?}" + ); + let control_counts = nonzero_counts(&control_right); + assert!( + control_counts == vec![1, 2] || control_counts == vec![3], + "{empty_left}: the duplicated `other` group must stay on one owner: {control_right:?}" ); common_telemetry::info!( "`{empty_left}` operand values per matching partition: left={control_left:?}, right={control_right:?}" @@ -1308,9 +1332,14 @@ fn fused_node_count_per_partition( ) -> Vec> { let mut per_partition = vec![None; SCAN_PARTITIONS]; let plan = displayable(fused.as_ref()).indent(false).to_string(); + // The tree's registrations live in the node's own set: `ExecutionPlan::metrics` leaves them + // out, since a flat set cannot say which node recorded what. let metrics = fused - .metrics() - .unwrap_or_else(|| panic!("{context}: the fused producer reports no metrics:\n{plan}")); + .downcast_ref::() + .unwrap_or_else(|| { + panic!("{context}: the fused producer is not a FusedSeriesExec:\n{plan}") + }) + .node_metric_set(); for metric in metrics.iter() { let MetricValue::Count { count, .. } = metric.value() else { continue; @@ -1331,6 +1360,22 @@ fn fused_node_count_per_partition( per_partition } +/// The nonzero entries of `values`, in ascending order. +/// +/// A vector of per-partition counts is read this way when what matters is how the values were +/// grouped rather than which partition reported them: an owner the router handed nothing is a zero, +/// and its position says nothing about the statement. +fn nonzero_counts(values: &[Option]) -> Vec { + let mut counts = values + .iter() + .flatten() + .copied() + .filter(|count| *count > 0) + .collect::>(); + counts.sort_unstable(); + counts +} + /// The named root metric of all fused producers in `capture`, per partition. fn fused_root_metric_per_partition( capture: &NativeCapture, @@ -1357,8 +1402,9 @@ fn final_input_samples_per_partition(capture: &NativeCapture, context: &str) -> for finalize in &capture.finalize { let plan = displayable(finalize.as_ref()).indent(false).to_string(); let metrics = finalize - .metrics() - .unwrap_or_else(|| panic!("{context}: Final reports no metrics:\n{plan}")); + .downcast_ref::() + .unwrap_or_else(|| panic!("{context}: Final is not a FusedSeriesExec:\n{plan}")) + .node_metric_set(); for metric in metrics.iter() { let MetricValue::Count { count, .. } = metric.value() else { continue; @@ -1380,14 +1426,59 @@ fn final_input_samples_per_partition(capture: &NativeCapture, context: &str) -> per_partition } -/// Asserts the fused producer reads the raw series scan itself, with no exchange between them. +/// The Final merges' root `native.root.output_values` counts, summed per partition. /// -/// One node may sit in between: the cooperative-scheduling decorator clones its input's properties, -/// changes only the scheduling type, and forwards `execute` unchanged, so it neither re-partitions -/// nor re-orders the frames. It is accepted only as a single typed wrapper above the scan, never as -/// a wildcard. An exchange above the scan would re-partition the frames, and the producer's -/// per-partition fold would then no longer be one series at a time - so the plan would be wrong even -/// if a result appeared. +/// The root of a Final tree is the node that publishes the finished answer, so these counts are the +/// statement's own values summed over the partitions that published them; which partition published +/// which value is the router's hash over the partial frames, so the total is what is read. +fn final_output_values_per_partition(capture: &NativeCapture, context: &str) -> Vec { + let mut per_partition = vec![0; SCAN_PARTITIONS]; + for finalize in &capture.finalize { + let plan = displayable(finalize.as_ref()).indent(false).to_string(); + let metrics = finalize + .downcast_ref::() + .unwrap_or_else(|| panic!("{context}: Final is not a FusedSeriesExec:\n{plan}")) + .node_metric_set(); + for metric in metrics.iter() { + if metric.value().name() != ROOT_OUTPUT_VALUES_METRIC { + continue; + } + let MetricValue::Count { count, .. } = metric.value() else { + continue; + }; + let partition = metric.partition().unwrap_or_else(|| { + panic!( + "{context}: Final `{ROOT_OUTPUT_VALUES_METRIC}` is not labelled with its \ + partition" + ) + }); + assert!( + partition < SCAN_PARTITIONS, + "{context}: Final `{ROOT_OUTPUT_VALUES_METRIC}` partition {partition} exceeds \ + {SCAN_PARTITIONS}:\n{plan}" + ); + per_partition[partition] += count.value(); + } + } + per_partition +} + +/// Asserts the fused producer reads the raw series scan itself, with no arbitrary exchange between +/// them. +/// +/// Two nodes may sit in between, and only these two, each accepted as its own type and never as a +/// wildcard "some wrapper": +/// +/// * the cooperative-scheduling decorator clones its input's properties, changes only the scheduling +/// type, and forwards `execute` unchanged, so it neither re-partitions nor re-orders the frames. +/// * the canonical `SeriesRepartitionExec` router the distributed plan puts between the producer and +/// the scans it reads: the region boundary merges the regions into one stream, and the router lifts +/// that stream back to the scan width by stored primary key, so every series still lands on exactly +/// one owner. +/// +/// An arbitrary exchange above the scan would re-partition the frames by anything but their series +/// identity, and the producer's per-partition fold would then no longer be one series at a time - so +/// the plan would be wrong even if a result appeared. fn assert_fused_reads_scan_directly(fused: &Arc, context: &str) { let plan = displayable(fused.as_ref()).indent(false).to_string(); assert_eq!( @@ -1401,34 +1492,62 @@ fn assert_fused_reads_scan_directly(fused: &Arc, context: &st children.len(), "{context}: the fused producer must take exactly one input:\n{plan}" ); - let child = children[0]; - // At most one transparent wrapper is allowed between the producer and the scan. - let scan = if child.is::() { - let wrapper_children = child.children(); - assert_eq!( - 1, - wrapper_children.len(), - "{context}: the cooperative wrapper must wrap exactly one input:\n{plan}" - ); + // At most one transparent wrapper is allowed between the producer and its input. + let input = through_cooperative(children[0]); + if let Some(router) = input.downcast_ref::() { + // The canonical router: the region boundary hands the producer one merged stream, and this + // node restores the width the producer folds at, per series and not per merged row. assert_eq!( SCAN_PARTITIONS, - child.properties().output_partitioning().partition_count(), - "{context}: the cooperative wrapper must not change the {SCAN_PARTITIONS} scan \ - partitions:\n{plan}" + router.outputs(), + "{context}: the canonical router must restore the {SCAN_PARTITIONS} scan partitions the \ + fused producer folds one series at a time, but it routes into {}:\n{plan}", + router.outputs() ); - wrapper_children[0] - } else { - child - }; + assert_eq!( + &SeriesRouting::RawPrimaryKey, + router.routing_policy(), + "{context}: the canonical router must route raw frames by their stored primary key so \ + each series stays whole on one owner, but it routes by {:?}:\n{plan}", + router.routing_policy() + ); + let routed_children = input.children(); + assert_eq!( + 1, + routed_children.len(), + "{context}: the canonical router must read exactly one input:\n{plan}" + ); + let routed = through_cooperative(routed_children[0]); + assert!( + routed.is::() || routed.is::(), + "{context}: the canonical router must read the raw series scan or the merge over the \ + region scans, but its input is `{}`:\n{plan}", + routed.name() + ); + if routed.is::() { + assert_eq!( + SCAN_PARTITIONS, + routed.properties().output_partitioning().partition_count(), + "{context}: `SeriesScanExec` must expose the {SCAN_PARTITIONS} scan partitions the \ + fused producer reads:\n{plan}" + ); + } else { + assert!( + routed.children().is_empty(), + "{context}: `MergeScanExec` must be a leaf:\n{plan}" + ); + } + return; + } assert!( - scan.is::(), + input.is::(), "{context}: the fused producer must read the raw series scan directly, with no \ `CoalesceExec` or `RepartitionExec` between them, but its input is `{}`:\n{plan}", - scan.name() + input.name() ); assert_eq!( SCAN_PARTITIONS, - scan.properties().output_partitioning().partition_count(), + input.properties().output_partitioning().partition_count(), "{context}: `SeriesScanExec` must expose the {SCAN_PARTITIONS} scan partitions the fused \ producer reads:\n{plan}" ); @@ -1474,7 +1593,13 @@ fn through_cooperative(node: &Arc) -> &Arc } } -/// Asserts the frontend Final node routes through the unified series repartitioner into the region boundary. +/// Asserts the frontend Final node gathers the Partial producer through the unified series +/// repartitioner. +/// +/// The region boundary is planned under the Partial producer, not under the Final: the producer folds +/// over the scan-width router that closes the region merge, so the Final's own router reads that +/// producer instead of reading the merged regions. Its width says how many owners finish the +/// aggregate - one when the statement specified one, the scan width otherwise. fn assert_finalize_reads_region_state( finalize: &Arc, producer: &Arc, @@ -1489,74 +1614,48 @@ fn assert_finalize_reads_region_state( "{context}: Final node must have one input:\n{plan}" ); let input = through_cooperative(children[0]); - if expect_single_owner { - assert!( - input.is::(), - "{context}: the specified single-owner Final must read MergeScanExec directly, got {}:\n{plan}", - input.name() - ); - assert_eq!( - 1, - input.properties().output_partitioning().partition_count(), - "{context}: single-owner Final input must have one partition:\n{plan}" - ); - assert_eq!( - 1, - finalize - .properties() - .output_partitioning() - .partition_count(), - "{context}: single-owner Final must have one partition:\n{plan}" - ); - assert!( - input.children().is_empty(), - "{context}: MergeScanExec must be a leaf:\n{plan}" - ); + let owners = if expect_single_owner { + 1 } else { - let repartition = input; - assert!( - repartition.is::(), - "{context}: Final must route its input through SeriesRepartitionExec, got {}:\n{plan}", - repartition.name() - ); - assert_eq!( - SCAN_PARTITIONS, - repartition - .properties() - .output_partitioning() - .partition_count(), - "{context}: grouped Final input router must have {SCAN_PARTITIONS} partitions:\n{plan}" - ); - assert_eq!( - SCAN_PARTITIONS, - finalize - .properties() - .output_partitioning() - .partition_count(), - "{context}: grouped Final must have {SCAN_PARTITIONS} partitions:\n{plan}" - ); - let routed = repartition.children(); - assert_eq!( - 1, - routed.len(), - "{context}: repartition must have one input:\n{plan}" - ); - let boundary = through_cooperative(routed[0]); - assert!( - boundary.is::(), - "{context}: Final repartition must read the region MergeScanExec, got {}:\n{plan}", - boundary.name() - ); - assert!( - boundary.children().is_empty(), - "{context}: MergeScanExec must be a leaf:\n{plan}" - ); - assert_eq!( - producer.schema(), - repartition.schema(), - "{context}: frontend router must preserve the Partial state-frame schema:\n{plan}" - ); - } + SCAN_PARTITIONS + }; + assert!( + input.is::(), + "{context}: Final must route its input through SeriesRepartitionExec, got {}:\n{plan}", + input.name() + ); + assert_eq!( + owners, + input.properties().output_partitioning().partition_count(), + "{context}: the Final's router must gather onto {owners} owners - one when the statement \ + specified one, the {SCAN_PARTITIONS} scan partitions otherwise:\n{plan}" + ); + assert_eq!( + owners, + finalize + .properties() + .output_partitioning() + .partition_count(), + "{context}: the Final must fold on {owners} owners:\n{plan}" + ); + let routed = input.children(); + assert_eq!( + 1, + routed.len(), + "{context}: repartition must have one input:\n{plan}" + ); + let gathered = through_cooperative(routed[0]); + assert!( + Arc::ptr_eq(gathered, producer), + "{context}: the Final's router must gather the Partial `FusedSeriesExec` it finishes, but it \ + reads `{}`:\n{plan}", + gathered.name() + ); + assert_eq!( + producer.schema(), + input.schema(), + "{context}: frontend router must preserve the Partial state-frame schema:\n{plan}" + ); let producer_schema = producer.schema(); assert_eq!( @@ -1565,13 +1664,17 @@ fn assert_finalize_reads_region_state( "{context}: Final input must preserve the Partial state-frame schema:\n{plan}" ); let producer_plan = NodeDisplay(producer.as_ref()).to_string(); + // The node's own line, then `native_tree=[`, then the tree: the first line inside the brackets + // is the root of the tree it runs, unindented and without a path prefix. let root_line = producer_plan .lines() .map(str::trim) - .find(|line| line.starts_with("root ") || line.starts_with("root:")) + .find(|line| { + !line.starts_with("FusedSeriesExec") && !matches!(*line, "native_tree=[" | "]") + }) .unwrap_or_default(); assert!( - root_line.contains("Aggregate") && root_line.contains("partial"), + root_line.starts_with("Aggregate:") && root_line.contains(" partial"), "{context}: captured region producer must have a Partial root, got `{root_line}`:\n{producer_plan}" ); let declaration = @@ -1789,12 +1892,13 @@ async fn check_two_source_segregation( both segment vectors: {vectors:?}; parent plan:\n{parent_display}; segment plan:\n{plan}" ); } + // Each segment folds the series the canonical router hashed onto its owners, so which of them + // reported a value is the hash's, not the statement's; that the segment evaluated its source + // at all is the statement's, and it is what this says. assert!( - values - .iter() - .all(|count| count.is_some_and(|count| count > 0)), - "`{query}`: each source segment must produce values on all {SCAN_PARTITIONS} partitions; \ - both segment vectors: {vectors:?}; parent plan:\n{parent_display}; segment plan:\n{plan}" + values.iter().flatten().sum::() > 0, + "`{query}`: source segment {index} must produce the values it folded, but it reported \ + none; both segment vectors: {vectors:?}; parent plan:\n{parent_display}; segment plan:\n{plan}" ); } } @@ -1958,7 +2062,9 @@ async fn check_case(instance: &Arc, observer: &NativePlanObserver, cas if !case.single_owner { // These counters belong to the Final runtime tree, not the Partial producer. The Final - // aggregate consumes routed AggregateState chunks at `native.root.input_samples`. + // aggregate consumes routed AggregateState chunks at `native.root.input_samples`. Which of + // its owners were handed state is the router's hash over the partial frames, so only the + // total is asserted: a Final that consumed nothing would answer nothing. let final_samples = final_input_samples_per_partition(&capture, &format!("native `{query}`")); common_telemetry::info!( @@ -1968,10 +2074,6 @@ async fn check_case(instance: &Arc, observer: &NativePlanObserver, cas final_samples.iter().sum::() > 0, "`{query}`: Final must consume aggregate state, got {final_samples:?}" ); - assert!( - final_samples.iter().filter(|&&samples| samples > 0).count() >= 2, - "`{query}`: aggregate state must reach at least two of the {SCAN_PARTITIONS} Final partitions, got {final_samples:?}" - ); } let per_partition = fused_root_metric_per_partition( @@ -1982,24 +2084,36 @@ async fn check_case(instance: &Arc, observer: &NativePlanObserver, cas common_telemetry::info!( "`{query}` native per-partition `{ROOT_OUTPUT_VALUES_METRIC}` counts: {per_partition:?}" ); - if case.per_partition { - for (partition, values) in per_partition.iter().enumerate() { - assert!( - values.is_some_and(|values| values > 0), - "`{query}`: every one of the {SCAN_PARTITIONS} scan partitions must fold at least \ - one value, but partition {partition} reported {values:?}; all partitions: \ - {per_partition:?}" - ); - } - } else { - assert!( - per_partition.iter().flatten().sum::() > 0, - "`{query}`: the fused producer must report folded values, got {per_partition:?}" - ); - } + // Which owner folds which series is the canonical router's hash of the series' stored key, over + // however many series each scan partition delivered - not the scan partition the series was read + // on. An owner can therefore be handed nothing, and the fixture's four nonempty scan partitions + // place nothing: neither how many owners folded nor what each one folded is asserted. A producer + // that folded nothing at all is still a failure. + assert!( + per_partition.iter().flatten().sum::() > 0, + "`{query}`: the fused producer must report the values it folded, got {per_partition:?}" + ); + // The finished answer is the statement's, though: the frontend merge publishes one value per row + // the result holds, wherever the partial states were folded. + let answered = final_output_values_per_partition(&capture, &format!("native `{query}`")); + common_telemetry::info!( + "`{query}` native Final per-partition `{ROOT_OUTPUT_VALUES_METRIC}` counts: {answered:?}" + ); + assert_eq!( + case.expected + .iter() + .filter(|row| row.value.is_some()) + .count(), + answered.iter().sum::(), + "`{query}`: the Final merge must publish one value per row the result holds, \ + {per_partition:?} were folded below it; its own partitions reported {answered:?}" + ); if let Some(root_values) = case.root_values { - // One value per series that folded, so the partitions' counts add up to the number of series - // the scan selected - every counter series, or only the ones a matcher kept. + // One input sample position per series that folded - the single instant this statement + // evaluates at - so the owners' counts add up to the number of series the scan selected: + // every counter series, or only the ones a matcher kept. Which owner each series lands on is + // the canonical router's hash of its stored key, so only that total is pinned, together with + // each owner reporting samples exactly when it reports folded values. let input_samples = fused_root_metric_per_partition( &capture, ROOT_INPUT_SAMPLES_METRIC, @@ -2008,23 +2122,23 @@ async fn check_case(instance: &Arc, observer: &NativePlanObserver, cas common_telemetry::info!( "`{query}` native per-partition `{ROOT_INPUT_SAMPLES_METRIC}` counts: {input_samples:?}" ); - assert!( - input_samples - .iter() - .all(|samples| samples.is_some_and(|n| n > 0)), - "`{query}`: every partition must report positive root input samples, got {input_samples:?}" - ); assert_eq!( root_values, input_samples.iter().flatten().sum::(), "`{query}`: root input sample total must match the selected fixture series" ); - if root_values == COUNTER_SERIES.len() { - assert_eq!(2, input_samples.iter().flatten().copied().max().unwrap()); + for (partition, values) in per_partition.iter().enumerate() { + assert_eq!( + values.is_some_and(|values| values > 0), + input_samples[partition].is_some_and(|samples| samples > 0), + "`{query}`: owner {partition} must consume samples exactly when it folds values; \ + folded: {per_partition:?}, samples: {input_samples:?}" + ); } - // Root output counts are grid points grouped into output series, not input-series - // contributions. Keep the per-partition activity check above; contribution totals belong - // to the exact native root input_samples metric checked here. + // The output counts are one value per group an owner published, not per-series + // contributions, so they do not add up to these input figures: the contribution total is the + // root `input_samples` total pinned here, and the answer's value count is the Final's own + // total above. Neither is read per owner. } } @@ -2139,19 +2253,26 @@ async fn native_aggregate_topk_over_global_partitions() { expected.sort_by(|a, b| (a.ts_millis, &a.labels).cmp(&(b.ts_millis, &b.labels))); assert_native_schema(&native, "v", &expected, &sql); assert_rows(&native.rows, &expected, &format!("expected `{sql}`")); - // `topk` above a global aggregate is not split, so the scan side folds per series and the - // ranking runs above it. A plan may hold several fused nodes (the frontend root and the region - // producers), so select them by what they do instead of counting nodes. + // `topk` above a global aggregate is not split, so the scan side answers the rate it reads and + // the ranking runs above the merged groups. A plan may hold several fused nodes (the frontend + // root and the region producers), so select them by what they do instead of counting nodes: a + // producer's tree reads raw samples, and only the node above them ranks. let plan_text = |node: &Arc| displayable(node.as_ref()).indent(false).to_string(); let producers: Vec<_> = capture .fused .iter() - .filter(|node| plan_text(node).contains("segment")) + .filter(|node| plan_text(node).contains("Input: raw samples")) .collect(); assert!( !producers.is_empty(), - "a segmented producer answers the fold" + "a segmented producer answers the scan it was given; fused nodes:\n{}", + capture + .fused + .iter() + .map(plan_text) + .collect::>() + .join("\n=====\n") ); for producer in &producers { assert!( @@ -2167,6 +2288,13 @@ async fn native_aggregate_topk_over_global_partitions() { .filter(|node| plan_text(node).to_ascii_lowercase().contains("topk")) .collect(); assert_eq!(1, ranked.len(), "topk runs once, above the merged groups"); + let ranked_text = plan_text(ranked[0]); + assert!( + ranked_text.contains("Vector: topk 10 by []") + && ranked_text.contains("Aggregate: sum by [hostname] complete") + && ranked_text.contains("Input: evaluated samples source=0, contiguous"), + "the fold answers the merged sources whole, with the ranking above it:\n{ranked_text}" + ); // The cross-producer requirement lives in the deterministic fixtures instead, which pin which // suffixes the split accepts above the cut and which it declines // (`native_fused_split_declines_a_whole_vector_suffix`). @@ -2269,76 +2397,62 @@ async fn native_aggregate_complete_over_four_nonempty_mito_partitions() { // Both tables have exactly one value field (`v`), so a bare table selector reads it. A // `__field__` matcher would narrow the field set, and the native route declines such a query. let cases = [ - case( - format!("sum by (job) ({GAUGE_TABLE})"), - &by_job(18.0, 12.0), - true, - ), + case(format!("sum by (job) ({GAUGE_TABLE})"), &by_job(18.0, 12.0)), // The gauge's groups average to 6 and 6, and the producer's plan line has to name the // `avg by (job)` the split cut out: what a partition exports is that aggregate's state, not // the group's mean. The counter table's cases below are the ones whose answers can tell a // merged state from a finished one. AggregateCase { partial_state: Some("avg by [job] partial"), - ..case( - format!("avg by (job) ({GAUGE_TABLE})"), - &by_job(6.0, 6.0), - true, - ) + ..case(format!("avg by (job) ({GAUGE_TABLE})"), &by_job(6.0, 6.0)) }, - case( - format!("count by (job) ({GAUGE_TABLE})"), - &by_job(3.0, 2.0), - true, - ), + case(format!("count by (job) ({GAUGE_TABLE})"), &by_job(3.0, 2.0)), // `a` deviates by sqrt(32/3) and `b` by 2. case( format!("stddev by (job) ({GAUGE_TABLE})"), &by_job((32.0f64 / 3.0).sqrt(), 2.0), - true, ), case( format!("sum without (instance) ({GAUGE_TABLE})"), &by_job(18.0, 12.0), - true, ), case( format!("abs(sum by (job) ({GAUGE_TABLE}))"), &by_job(18.0, 12.0), - true, ), // A literal scalar above the fold: `sum` over the gauge's 2, 6, 10, 4 and 8 is 30, and the - // merge's suffix doubles the merged value. The global aggregate has one owner. + // merge's suffix doubles the merged value. The global aggregate finishes on the one owner + // that row is routed to. AggregateCase { single_owner: true, - ..case(format!("sum({GAUGE_TABLE}) * 2"), &[(&[], 60.0)], true) + ..case(format!("sum({GAUGE_TABLE}) * 2"), &[(&[], 60.0)]) }, // A gauge over a constant value has a rate of exactly zero, so these two only cover the - // zero-rate case: every series has both samples inside the 5m window, hence every partition - // must fold a value. They cannot tell a computed rate from a dropped one, which is what the - // counter table's cases below are for. The global aggregate has one owner. + // zero-rate case: every series has both samples inside the 5m window, hence the fold reports + // values on the owners the router hands series to. They cannot tell a computed rate from a + // dropped one, which is what the counter table's cases below are for. The global aggregate + // finishes on the one owner that row is routed to. AggregateCase { single_owner: true, - ..case(format!("sum(rate({GAUGE_TABLE}[5m]))"), &[(&[], 0.0)], true) + ..case(format!("sum(rate({GAUGE_TABLE}[5m]))"), &[(&[], 0.0)]) }, case( format!("sum by (job) (rate({GAUGE_TABLE}[5m]))"), &by_job(0.0, 0.0), - true, ), // The counter table: the same five series in the same four files, twice each, with the // increases of `COUNTER_SERIES`, so every value below is a nonzero rate that // `increase / 150` derives by hand - 1, 3 and 5 for `job = a`, 2 and 4 for `job = b`. // - // These are the acceptance cases for the partial/final state bridge. Each of the four - // partitions folds the states of the series its own scan returned and the merge above it - // folds them together, so the answers are only reachable when every partition's state - // arrives: `count` reports 3 and 2, and `avg`/`stddev` fold states that do not decompose - // into per-partition values. `job = a`'s three series cannot all be in one of the four - // partitions - the per-partition counts asserted for the counter cases that read every - // series rule that out - so its mean and its standard deviation are merged across at least - // two partitions. A standard deviation finished per partition and then averaged would - // report 0.5 or 1.0 for the 2+1 splits the counts leave possible, never sqrt(8/3). + // These are the acceptance cases for the partial/final state bridge. Each owner folds the + // states of the series the router handed it and the merge above it folds them together, so the + // answers are only reachable when every owner's state arrives: `count` reports 3 and 2, and + // `avg`/`stddev` fold states that do not decompose into per-owner values. The five series are + // hashed onto however many of the four owners the router picks - the test pins the totals the + // owners report between them, never their shares - and a series' state may reach a different + // owner than the one it was read on, so the mean and the standard deviation are merged across + // owners. A standard deviation finished per owner and then averaged would report 0.5 or 1.0 + // for a 2+1 split, never sqrt(8/3). AggregateCase { single_owner: true, ..counter_case( @@ -2350,7 +2464,8 @@ async fn native_aggregate_complete_over_four_nonempty_mito_partitions() { // The same sum with a literal scalar above the fold: the merge's suffix doubles the merged // 15 to 30, where a suffix run over one partition's share - at most 10 of the 15 - would // answer at most 20, and a suffix that divided instead would answer 7.5. The cut is still - // the ungrouped `sum`, which the producer's plan line names. The global aggregate has one owner. + // the ungrouped `sum`, which the producer's plan line names. The global aggregate finishes on + // the one owner that row is routed to. AggregateCase { single_owner: true, ..counter_case( @@ -2365,26 +2480,25 @@ async fn native_aggregate_complete_over_four_nonempty_mito_partitions() { "sum by [job] partial", ), // The matcher selects only `job = a`'s three series. Their rates are 1, 3 and 5, so the - // native partial producer must still run and the merged result must be 9. Three selected - // series cannot populate all four scan partitions, so this case checks that some producer - // output exists without applying the unfiltered fixture's four-nonempty-partition assertion. + // native partial producer must still run and the merged result must be 9. Which owners the + // router hands those three series to is its own hash, so this case checks that some producer + // output exists without applying the unfiltered fixture's spread assertion. AggregateCase { single_owner: true, partial_state: Some("sum by [] partial"), ..case( format!("sum(rate({COUNTER_TABLE}{{job=\"a\"}}[5m]))"), &[(&[], 9.0)], - false, ) }, // A matcher on the tag only the counter table carries. The four flushed series are the four // `region = us-west-2` files and the series left in the memtable range is `us-east-1`, so // this selects four of the five scan ranges: `job = a` keeps its 1, 3 and 5 - nine - while - // `job = b` keeps only `h4`'s 2, where the case above answers 6. The pruned range still - // runs and may leave its partition with nothing to fold, so `per_partition` is left off and - // only the total is pinned: one value for each of the four selected series. The answer's - // labels and values say which series survived, and every case here still requires the - // producer to read the four-partition scan. + // `job = b` keeps only `h4`'s 2, where the case above answers 6. The pruned range still runs + // and the four survivors are hashed onto some owners, so only the total is pinned: one sample + // position for each of the four selected series. The answer's labels and values say which + // series survived, and every case here still requires the producer to read the + // four-partition scan. AggregateCase { root_values: Some(COUNTER_SERIES.len() - 1), partial_state: Some("sum by [job] partial"), @@ -2393,7 +2507,6 @@ async fn native_aggregate_complete_over_four_nonempty_mito_partitions() { "sum by (job) (rate({COUNTER_TABLE}{{region=\"{COUNTER_WEST_REGION}\"}}[5m]))" ), &by_job(9.0, 2.0), - false, ) }, counter_case( @@ -2473,15 +2586,14 @@ async fn native_aggregate_complete_over_four_nonempty_mito_partitions() { "sum by [] partial", ) }, - // The range table holds one series in one partition, so this is a correctness smoke test of - // one populated partition rather than a parallel acceptance. Its one series folds one - // `count_over_time` value at the single grid point. + // The range table holds one series, so this is a correctness smoke test of one populated + // owner rather than a parallel acceptance. Its one series folds one `count_over_time` value + // at the single grid point. AggregateCase { single_owner: true, ..case( format!("sum(count_over_time({RANGE_TABLE}[10m]))"), &[(&[], 2.0)], - false, ) }, ]; @@ -2493,11 +2605,7 @@ async fn native_aggregate_complete_over_four_nonempty_mito_partitions() { let nested_cases = [ AggregateCase { single_owner: true, - ..case( - format!("sum(avg({GAUGE_TABLE}) by (job))"), - &[(&[], 12.0)], - true, - ) + ..case(format!("sum(avg({GAUGE_TABLE}) by (job))"), &[(&[], 12.0)]) }, counter_case( format!("sum(abs(avg(rate({COUNTER_TABLE}[5m])) by (job)))"), @@ -2514,7 +2622,7 @@ async fn native_aggregate_complete_over_four_nonempty_mito_partitions() { check_nested_aggregate_case(&instance, &observer, case).await; } - let time = case("time()".to_string(), &[(&[], EVAL_SECS as f64)], false); + let time = case("time()".to_string(), &[(&[], EVAL_SECS as f64)]); let (native, capture) = run_query(&instance, &time.promql, true, &observer).await; assert_rows(&native.rows, &time.expected, "native `time()`"); assert_eq!(1, capture.fused.len(), "`time()` runs one fused node"); @@ -2525,7 +2633,7 @@ async fn native_aggregate_complete_over_four_nonempty_mito_partitions() { &instance, &observer, &two_source_promql, - &case(two_source_promql.clone(), &[(&[], 15.0 / 30.0)], false).expected, + &case(two_source_promql.clone(), &[(&[], 15.0 / 30.0)]).expected, ) .await; @@ -2535,11 +2643,7 @@ async fn native_aggregate_complete_over_four_nonempty_mito_partitions() { // partition at a time would report the value of whichever partition held a single series, so // the NaN also says the whole vector was read at once. The native run must therefore hold a // fused producer and no frontend merge, and answer the same rows the existing plan answers. - let scalar_case = case( - format!("scalar({COUNTER_TABLE})"), - &[(&[], f64::NAN)], - false, - ); + let scalar_case = case(format!("scalar({COUNTER_TABLE})"), &[(&[], f64::NAN)]); let scalar_query = tql_eval(&scalar_case.promql); let (legacy, legacy_capture) = run_query(&instance, &scalar_case.promql, false, &observer).await; @@ -2583,7 +2687,6 @@ async fn native_aggregate_complete_over_four_nonempty_mito_partitions() { let split_cases = [case( format!("max_over_time((sum(rate({COUNTER_TABLE}[5m])))[2s:1s])"), &[(&[], 15.0)], - false, )]; for case in &split_cases { check_split_case(&instance, &observer, case).await; diff --git a/tests-integration/src/tests/promql_test.rs b/tests-integration/src/tests/promql_test.rs index 9b7bbb01ff1..47605cbd575 100644 --- a/tests-integration/src/tests/promql_test.rs +++ b/tests-integration/src/tests/promql_test.rs @@ -2375,7 +2375,7 @@ async fn native_promql_clamp_aggregates_execute_on_native_segments() { .to_string(); if split { assert!( - display.contains("Aggregate sum by [] final"), + display.contains("Aggregate: sum by [] final"), "{query}: {display}" ); let producer = observer.fused().expect("native partial producer"); @@ -2389,11 +2389,11 @@ async fn native_promql_clamp_aggregates_execute_on_native_segments() { } else { assert!(display.contains("FusedSeriesExec"), "{query}: {display}"); assert!( - display.contains("Vector topk 1 by []"), + display.contains("Vector: topk 1 by []"), "{query}: {display}" ); assert!( - display.contains("Aggregate sum by [] complete"), + display.contains("Aggregate: sum by [] complete"), "{query}: {display}" ); } @@ -2766,6 +2766,28 @@ fn assert_two_source_native_plan( assert_eq!(display.matches("source=1").count(), 1, "{display}"); } +/// The registrations the fused node's tree made, exactly as they were recorded. +/// +/// [`datafusion::physical_plan::ExecutionPlan::metrics`] leaves them out: a flat set cannot say +/// which tree node recorded what, so the node keeps its own set for readers that key by name. +fn fused_metric_set( + plan: &Arc, +) -> datafusion::physical_plan::metrics::MetricsSet { + plan.downcast_ref::() + .unwrap_or_else(|| panic!("the plan is a FusedSeriesExec, not {}", plan.name())) + .node_metric_set() +} + +/// The node's `native.root.output_values` counts, indexed by the partition the node ran on. +/// +/// `None` when the node registered no such metric. A `Some` vector is as wide as the node, and an +/// entry is zero when that partition published nothing - which is a partition the router handed no +/// series. +/// +/// Those zeros are not a property of the statement: each series is placed on the owner the canonical +/// router hashes its stored key onto, over however many series each scan partition delivers, so a +/// caller may sum this vector or ask that some partition published, but it must not assert which +/// entry is non-zero, how many owners were used, or that every one of them was. fn output_values_per_partition( plan: &Arc, ) -> Option> { @@ -2775,7 +2797,7 @@ fn output_values_per_partition( return None; } let partition_count = plan.output_partitioning().partition_count(); - let metrics = plan.metrics()?; + let metrics = fused_metric_set(plan); let mut values_by_partition = vec![0_u64; partition_count]; let mut found_metric = false; for metric in metrics.iter() { @@ -2944,31 +2966,34 @@ fn fused_vectors_report(nodes: &[FusedNodeVector]) -> String { .join(" ") } -/// Asserts the native work of the last statement reached every one of the -/// [`DECLARED_PARTITIONS`] partitions it runs at, and reports the vectors it checked. +/// Asserts the native work of the last statement ran at the width of the +/// [`DECLARED_PARTITIONS`] partitions it declares and published values, and reports the vectors it +/// checked. /// -/// `native.root.output_values` counts what a fused node published on the partition it ran, so a -/// zero in a node's vector is a partition where it published nothing. Both placements are read, -/// and each is asserted node by node rather than by looking for one node that happens to span -/// partitions: +/// `native.root.output_values` counts what a fused node published on the partition it ran, and the +/// canonical router places each series on one owner by hashing its stored key over whatever the scan +/// partitions delivered, so an owner can be left with nothing to publish even when every scan +/// partition read rows. Which partition published what - and how many partitions published at all - +/// is therefore not a property of the statement, and no vector below is asserted entry by entry. +/// What is asserted is per node: it registers the metric, it runs at the width the statement +/// declares, and it published the values it evaluated. Both placements are read: /// /// - A statement that runs native work inside a region has one fused node per region-side tree. -/// Every one of those must report the metric, be as wide as the statement, and have all counts -/// positive, so a region prefix that collapsed onto one partition fails here even when another -/// region's prefix is wide. The frontend's own node is the finalize over the region streams; its -/// vector is reported but not asserted, because a final combine is not required to publish on -/// every partition (it may even be a single partition, and the answer is assembled from the -/// per-region streams above it). +/// Every one of those must report the metric, be as wide as the statement, and have published +/// something. The frontend's own node is the finalize over the region streams; its vector is +/// reported but not asserted, because a final combine is not required to publish on every +/// partition (it may even be a single partition, and the answer is assembled from the per-region +/// streams above it). /// - A statement that only runs native work in the frontend (the frontend-raw routes, which run no /// native node inside a region) is read from [`NativePlanObserver::root_plan`] instead: its /// fused nodes must be found there, and every one of them that is as wide as the statement must -/// report the metric with all counts positive. A narrower final merge is ignored, because it is -/// the last combine rather than a partition-wide execution; a tree whose fused nodes are all -/// narrow is refused, because nothing in it shows a partition-wide execution. +/// report the metric and publish something. A narrower final merge is ignored, because it is the +/// last combine rather than a partition-wide execution; a tree whose fused nodes are all narrow is +/// refused, because nothing in it shows a partition-wide execution. /// -/// A width-4 node without the metric, or with an empty partition, is a failure wherever the metric -/// is required, never an exemption. The failure messages name the label, the node's site and the -/// vectors, so a regression that collapses one region or one width-4 node is reported as itself. +/// A width-4 node without the metric, or one that published nothing, is a failure wherever the +/// metric is required, never an exemption. The failure messages name the label, the node's site and +/// the vectors, so a regression that loses the metric or a node's values is reported as itself. fn assert_cross_partition_native_values(label: &str, observer: &NativePlanObserver) -> String { let mut region_nodes = Vec::new(); for (index, plan) in observer.region_plans().iter().enumerate() { @@ -2996,11 +3021,14 @@ fn assert_cross_partition_native_values(label: &str, observer: &NativePlanObserv {counts:?}; region vectors: {region_report}", node.site, node.partitions ); - let empty_partitions = counts.iter().filter(|count| **count == 0).count(); - assert_eq!( - 0, empty_partitions, - "{label}: the region-side fused node {} published nothing on {empty_partitions} \ - of {DECLARED_PARTITIONS} partitions: {counts:?}; region vectors: {region_report}", + // Which of the partitions were handed series is the canonical router's hash, so a zero + // is not a lost value; that the node published the values it evaluated is. + let published: u64 = counts.iter().sum(); + assert!( + published > 0, + "{label}: the region-side fused node {} published no value on any of the \ + {DECLARED_PARTITIONS} partitions it runs: {counts:?}; region vectors: \ + {region_report}", node.site ); } @@ -3040,15 +3068,14 @@ fn assert_cross_partition_native_values(label: &str, observer: &NativePlanObserv display_plan_tree(&observer.root_plan()) ), }; - let empty_partitions = counts.iter().filter(|count| **count == 0).count(); // An ordinary partition can be empty: the raw frames are routed by their stored key, so a // small fixture need not put work on every one of them. The producer's width is still - // required, but a partition that reports nothing is not evidence of lost work. + // required, and so is that it published the values it evaluated. + let published: u64 = counts.iter().sum(); assert!( - empty_partitions < DECLARED_PARTITIONS, - "{label}: the {}-partition frontend fused node {} published nothing on \ - {empty_partitions} of {DECLARED_PARTITIONS} partitions: {counts:?}; root vectors: \ - {root_report}", + published > 0, + "{label}: the {}-partition frontend fused node {} published nothing on any of its \ + partitions: {counts:?}; root vectors: {root_report}", node.partitions, node.site ); @@ -3070,9 +3097,7 @@ fn nonempty_partitions( use datafusion::physical_plan::metrics::MetricValue; let partition_count = plan.output_partitioning().partition_count(); - let metrics = plan - .metrics() - .unwrap_or_else(|| panic!("the plan reports metrics: {}", display_plan_tree(plan))); + let metrics = fused_metric_set(plan); let mut recorded = vec![0_u64; partition_count]; let mut found = false; for entry in metrics.iter() { @@ -3142,7 +3167,18 @@ fn canonical_router( }) } -fn assert_four_native_partitions(plan: &Arc) { +/// Asserts the fused producer keeps the four partitions of the scan it reads, and that the +/// partitions published `expected_values` values between them. +/// +/// The width is the statement's: the producer folds over the scan-width router, so it must not +/// collapse onto one partition. Which of those partitions were handed a series is not: the canonical +/// router hashes each series' stored key onto one owner, so an owner can be left with nothing even +/// when every scan partition read rows. The distribution-independent figure is the total, which is +/// one published value per value the statement answers with. +fn assert_four_native_partitions( + plan: &Arc, + expected_values: usize, +) { assert_eq!(plan.name(), "FusedSeriesExec"); assert_eq!(plan.output_partitioning().partition_count(), 4); assert_eq!( @@ -3152,13 +3188,15 @@ fn assert_four_native_partitions(plan: &Arc 0), - "every native execution partition must produce values: {values_by_partition:?}" + assert_eq!( + expected_values, + values_by_partition.iter().sum::() as usize, + "the native partitions together must publish one value per value the statement answers \ + with: {values_by_partition:?}" ); } @@ -5234,7 +5272,17 @@ async fn distributed_native_promql_zero_match_or_real() { "{}", datafusion::physical_plan::displayable(fused.as_ref()).one_line() ); - assert!(display.contains("root Set or"), "{expression}: {display}"); + // The node draws the tree it runs, one line per node: no query string, no path prefix and + // no `segment` wording, and the set names its own operator and matching labels. + assert!( + display.contains("native_tree=[\nSet: or ignoring []"), + "{expression}: {display}" + ); + assert!( + display.contains("\n Input: evaluated samples source=0, contiguous") + && display.contains("\n Input: evaluated samples source=1, contiguous"), + "{expression}: {display}" + ); let children = fused.children(); assert_eq!(children.len(), 2, "{expression}: {display}"); assert!( @@ -5784,7 +5832,7 @@ async fn native_promql_label_rewrites_reach_the_native_route_and_keep_prometheus .unwrap(); let fresh_rows = native_result_rows(fresh).await; let fused = observer.take(); - assert_four_native_partitions(&fused); + assert_four_native_partitions(&fused, fresh_rows.len()); assert_eq!( fresh_rows, (0..16) @@ -5874,9 +5922,7 @@ async fn native_promql_label_rewrites_reach_the_native_route_and_keep_prometheus let split_rows = native_result_rows(split_parts).await; assert_eq!(split_rows.len(), 2, "{split_rows:?}"); let split_plan = observer.take(); - let nonempty = split_plan - .metrics() - .expect("fused plan metrics") + let nonempty = fused_metric_set(&split_plan) .iter() .filter(|metric| metric.value().name() == "native.root.output_values") .filter_map(|metric| match metric.value() { @@ -5937,13 +5983,13 @@ async fn native_promql_label_rewrites_reach_the_native_route_and_keep_prometheus let histogram_producer = histogram_router.children()[0].clone(); // The boundary's own tree is the merge over the rewrite: `histogram_count` is not in it, so the // router that validates the collected label set runs below the map rather than above it. The - // node's explain line names the whole statement, so the tree is read from its own header on. + // physical node renders its own tree directly below its operator line. let producer_display = datafusion::physical_plan::displayable(histogram_producer.as_ref()) - .indent(false) + .one_line() .to_string(); let producer_tree = producer_display - .split_once("NativeTree") - .map(|(_, tree)| format!("NativeTree{tree}")) + .split_once('\n') + .map(|(_, tree)| tree) .unwrap_or_else(|| panic!("the producer renders its tree: {producer_display}")); assert!( producer_tree.contains("Merge"), @@ -6471,7 +6517,7 @@ async fn native_promql_executes_fused_node_and_matches_legacy() { .unwrap(); let native_rows = native_result_rows(native).await; let plan = observer.take(); - assert_four_native_partitions(&plan); + assert_four_native_partitions(&plan, native_rows.len()); let expected = (b'a'..=b'p') .zip((1..=16).map(|index| index as f64 * 2.0)) .map(|(host, value)| (0, (host as char).to_string(), value)) @@ -7215,9 +7261,7 @@ async fn native_promql_instant_at_uses_sample_time_and_anchor() { let native_rows = native_result_rows(native).await; let plan = observer.take(); assert_eq!(plan.name(), "FusedSeriesExec", "{expr}"); - let output_count = plan - .metrics() - .unwrap() + let output_count = fused_metric_set(&plan) .iter() .filter_map(|metric| match metric.value() { datafusion::physical_plan::metrics::MetricValue::Count { name, count } @@ -7470,9 +7514,7 @@ async fn native_promql_binary_join_uses_two_independent_scans() { let plan = observer.take(); assert_eq!(plan.name(), "FusedSeriesExec"); assert_two_source_native_plan(&observer, &plan, true); - let input_samples = plan - .metrics() - .expect("fused metrics") + let input_samples = fused_metric_set(&plan) .iter() .filter_map(|metric| match metric.value() { datafusion::physical_plan::metrics::MetricValue::Count { name, count } @@ -7492,9 +7534,7 @@ async fn native_promql_binary_join_uses_two_independent_scans() { ("native.root.left.input_samples", HOSTS * 4), ("native.root.right.input_samples", HOSTS * 2), ] { - let source_samples = plan - .metrics() - .expect("fused metrics") + let source_samples = fused_metric_set(&plan) .iter() .filter_map(|metric| match metric.value() { datafusion::physical_plan::metrics::MetricValue::Count { name, count } @@ -9832,9 +9872,7 @@ async fn native_promql_repeated_selector_binary_uses_independent_scans() { scan_frame_rows, 1, "the anchored range returns one packed raw-series frame" ); - let root_values = bounded_plan - .metrics() - .expect("fused metrics") + let root_values = fused_metric_set(&bounded_plan) .iter() .filter_map(|metric| match metric.value() { datafusion::physical_plan::metrics::MetricValue::Count { name, count } @@ -9846,9 +9884,7 @@ async fn native_promql_repeated_selector_binary_uses_independent_scans() { }) .sum::(); assert_eq!(root_values, 1, "the bounded native result was executed"); - let root_input_samples = bounded_plan - .metrics() - .expect("fused metrics") + let root_input_samples = fused_metric_set(&bounded_plan) .iter() .filter_map(|metric| match metric.value() { datafusion::physical_plan::metrics::MetricValue::Count { name, count } @@ -10228,7 +10264,7 @@ async fn native_promql_rate_and_abs_rate_match_legacy_across_storage_encodings() "fused execution must retain partitioning for {table}" ); if sparse { - assert_four_native_partitions(&plan); + assert_four_native_partitions(&plan, native_rows.len()); } let child = plan.children()[0]; let child = child @@ -10273,9 +10309,7 @@ async fn native_promql_explain_and_analyze_execute_fused_plan() { assert!(text.contains("FusedSeriesExec")); assert!(text.contains("output_values")); let plan = observer.take(); - let output_values = plan - .metrics() - .expect("fused plan metrics") + let output_values = fused_metric_set(&plan) .iter() .filter_map(|metric| match metric.value() { datafusion::physical_plan::metrics::MetricValue::Count { name, count } @@ -10288,10 +10322,11 @@ async fn native_promql_explain_and_analyze_execute_fused_plan() { .sum::(); assert_eq!(output_values, 1, "ANALYZE executes the fused series"); } else if statement.contains("EXPLAIN") && enabled { - assert!(text.contains("FusedSeries: native_explain")); + assert!(text.contains("Input: raw samples source=0"), "{text}"); assert!(text.contains("MergeScanExec")); + assert!(!text.contains("metrics="), "{text}"); } else if !enabled { - assert!(!text.contains("FusedSeries:")); + assert!(!text.contains("FusedSeries")); } } } @@ -10468,7 +10503,7 @@ fn holds_split_merge(plan: &Arc) - let mut stack = vec![plan.clone()]; while let Some(node) = stack.pop() { let display = datafusion::physical_plan::displayable(node.as_ref()) - .indent(false) + .one_line() .to_string(); if node.name() == "FusedSeriesExec" && display.contains(" final") { let mut descendants = node.children().into_iter().cloned().collect::>(); @@ -11048,18 +11083,36 @@ async fn cte_native_statement_runs_the_fused_plan() { "a fused plan joins nothing, but these nodes ran: {joins:?}\n{}", cte_native_observed_plan_trees(&observer) ); - // One logical table keeps both occurrences region-local: the node reads one scan per - // occurrence instead of one raw source per occurrence. + // One logical table keeps both occurrences on their own region boundary: the node reads one + // merged region stream per occurrence, and the raw source behind each of them is the region's + // own, not a frontend scan per input. + let trees = cte_native_observed_plan_trees(&observer); let scans = observed_series_scans(&observer); assert_eq!( scans.len(), 2, - "one independent scan per occurrence of the repeated selector:\n{}", - cte_native_observed_plan_trees(&observer) + "one independent scan per occurrence of the repeated selector:\n{trees}" ); - assert!( - observer.take_raw_sources().is_empty(), - "one logical table keeps both scans region-local instead of one raw source per input" + let occurrences = fused[0].children(); + assert_eq!( + 2, + occurrences.len(), + "one region boundary per occurrence of the repeated selector:\n{trees}" + ); + for (index, occurrence) in occurrences.iter().enumerate() { + assert!( + find_plan_node(occurrence, "MergeScanExec").is_some(), + "occurrence {index} must read its own region merge:\n{trees}" + ); + assert!( + find_plan_node(occurrence, "SeriesScanExec").is_none(), + "occurrence {index} must leave the series scan to its region:\n{trees}" + ); + } + assert_eq!( + observer.take_raw_sources().len(), + occurrences.len(), + "the region reads one raw source per occurrence of the repeated selector:\n{trees}" ); assert_eq!( native_schema, @@ -12432,9 +12485,7 @@ async fn native_promql_columnar_fused_series_matches_legacy() { .all(|entry| entry["artifact"]["format"] == "columnar-series") ); // One value per series: the node ran, rather than being merely planned. - let output_values = plan - .metrics() - .expect("fused plan metrics") + let output_values = fused_metric_set(&plan) .iter() .filter_map(|metric| match metric.value() { datafusion::physical_plan::metrics::MetricValue::Count { name, count } @@ -12535,9 +12586,7 @@ async fn native_promql_columnar_unindexed_float64_matches_legacy( // fused node to have run. let plan = observer.take(); assert_eq!(native_rows, expected, "{stage}: fused route must answer"); - let output_values = plan - .metrics() - .expect("fused plan metrics") + let output_values = fused_metric_set(&plan) .iter() .filter_map(|metric| match metric.value() { datafusion::physical_plan::metrics::MetricValue::Count { name, count } @@ -12624,9 +12673,7 @@ async fn native_promql_columnar_partial_index_float64_matches_legacy( // fused node to have run. let plan = observer.take(); assert_eq!(native_rows, expected, "{stage}: fused route must answer"); - let output_values = plan - .metrics() - .expect("fused plan metrics") + let output_values = fused_metric_set(&plan) .iter() .filter_map(|metric| match metric.value() { datafusion::physical_plan::metrics::MetricValue::Count { name, count } @@ -13106,21 +13153,22 @@ async fn native_promql_sum_topk_uses_global_topk_segment() { "a global topk answers one partition" ); let parent_display = datafusion::physical_plan::displayable(parent.as_ref()) - .indent(true) + .one_line() .to_string(); let parent_tree = parent_display - .split_once("NativeTree") - .map_or(parent_display.as_str(), |(_, tree)| tree); + .split_once('\n') + .expect("the parent renders its own tree") + .1; assert!( - parent_tree.contains("Aggregate sum by [] complete"), + parent_tree.contains("Aggregate: sum by [] complete"), "the parent owns the complete sum: {parent_tree}" ); assert!( - parent_tree.contains("Vector topk 1 by []"), + parent_tree.contains("Vector: topk 1 by []"), "the parent owns the global topk: {parent_tree}" ); assert!( - parent_tree.contains("Input evaluated samples source=0"), + parent_tree.contains("Input: evaluated samples source=0"), "the parent reads its child's evaluated frame, not a scan: {parent_tree}" ); assert!( @@ -13143,13 +13191,13 @@ async fn native_promql_sum_topk_uses_global_topk_segment() { "the prefix keeps the scan's partitions, which is the point of cutting below the topk" ); let child_display = datafusion::physical_plan::displayable(child.as_ref()) - .indent(true) + .one_line() .to_string(); - // The first line names the statement the node was built for, which is the same for both - // segments; what a segment computes is the tree below it. + // Each segment renders its own compiled tree below the operator line. let child_tree = child_display - .split_once("NativeTree") - .map_or(child_display.as_str(), |(_, tree)| tree); + .split_once('\n') + .expect("the child renders its own tree") + .1; assert!( child_tree.contains("Instant"), "the child owns the per-series prefix: {child_tree}" @@ -13743,20 +13791,20 @@ async fn native_promql_cuts_segments_only_where_a_gather_would_swallow_them() { } if query == "sum(topk(1, seg_a)) + vector(1)" { let parent = datafusion::physical_plan::displayable(frontend[0].as_ref()) - .indent(true) + .one_line() .to_string(); - let parent_tree = parent.split_once("NativeTree").unwrap().1; + let parent_tree = parent.split_once('\n').unwrap().1; assert!(parent_tree.contains("VectorOf"), "{parent_tree}"); assert!( - parent_tree.contains("Input evaluated samples source=0"), + parent_tree.contains("Input: evaluated samples source=0"), "the parent must read the data segment: {parent_tree}" ); assert!(!parent_tree.contains("Instant"), "{parent_tree}"); assert_eq!(regions.len(), 1, "{query}: the data prefix must be cut"); let prefix = datafusion::physical_plan::displayable(regions[0].as_ref()) - .indent(true) + .one_line() .to_string(); - let prefix_tree = prefix.split_once("NativeTree").unwrap().1; + let prefix_tree = prefix.split_once('\n').unwrap().1; assert!(prefix_tree.contains("Instant"), "{prefix_tree}"); assert!(!prefix_tree.contains("VectorOf"), "{prefix_tree}"); let mut rows = eval_values(native_tql_output(&instance, &sql, true).await); @@ -15725,7 +15773,7 @@ async fn native_promql_root_canonical_merge_keeps_the_scan_width() { .indent(true) .to_string(); assert!( - pair_sum_display.contains("Aggregate sum by [] complete"), + pair_sum_display.contains("Aggregate: sum by [] complete"), "the fold is a complete aggregate over the assembled series: {pair_sum_display}" ); assert!( @@ -15800,7 +15848,7 @@ async fn native_promql_root_canonical_merge_keeps_the_scan_width() { // state standing in for the collection. if shape == "a fold" { assert!( - display.contains("Aggregate sum by [] complete"), + display.contains("Aggregate: sum by [] complete"), "{shape}: the fold is a complete aggregate over the assembled frames: {display}" ); assert!( @@ -15895,7 +15943,7 @@ async fn native_promql_root_canonical_merge_keeps_the_scan_width() { .indent(true) .to_string(); assert!( - sum_display.contains("Aggregate sum by [] complete"), + sum_display.contains("Aggregate: sum by [] complete"), "the fold runs natively as a complete aggregate over the boundary: {sum_display}" ); assert!( diff --git a/tests-integration/tests/http.rs b/tests-integration/tests/http.rs index 270a8d0c438..87f1c3fe919 100644 --- a/tests-integration/tests/http.rs +++ b/tests-integration/tests/http.rs @@ -1684,10 +1684,14 @@ fn executes_fused_series(plan: &Arc) -> bool { /// One row of a PromQL result: its tag columns in schema order, its timestamp and its value. type PromqlRow = (Vec<(String, String)>, i64, f64); -/// Checks that a native result reads no scan for a constant, and one real scan over -/// `scan_partitions` non-empty partitions for a vector, and returns the fused node it ran. +/// Checks that a native result plans one fused node, that a constant plans no scan and a vector one +/// scan exposing `scan_partitions` partitions, and returns the plan's view of the fused node. +/// +/// `exec` is the fused node that really ran. For a vector it is the region-side node, which the JSON +/// view reports as a separate stage, so it is passed in rather than found in `plan`. fn assert_native_plan( plan: &Arc, + exec: &Arc, scan_partitions: Option, context: &str, ) -> Value { @@ -1700,8 +1704,16 @@ fn assert_native_plan( ); nodes.remove(0) }; - // The tree the node will run, rendered the way `EXPLAIN` renders it: one line per node. - let tree = fused["param"].as_str().unwrap_or_default(); + // The tree the node ran, as its own line renders it: `native_tree=[...]`. The JSON view reports + // each node of that tree as its own child, next to the plans the operator reads, so this is the + // one place the tree is still a single text. + let display = datafusion::physical_plan::displayable(exec.as_ref()) + .indent(false) + .to_string(); + let tree = display + .split_once("native_tree=[") + .map(|(_, tree)| tree.to_string()) + .unwrap_or_else(|| panic!("{context}: the fused node renders no tree: {display}")); let scans = plan_nodes_named(plan, "SeriesScanExec"); let Some(partitions) = scan_partitions else { // A constant reads no frame: its one value per grid point comes from the tree alone, so the @@ -1710,10 +1722,9 @@ fn assert_native_plan( scans.is_empty(), "{context}: a constant planned a scan: {scans:?}" ); - assert_eq!( - fused["children"].as_array().map(Vec::len), - Some(0), - "{context}: a constant's fused node has children: {fused}" + assert!( + exec.children().is_empty(), + "{context}: a constant's fused node reads a child: {fused}" ); assert!( !tree.contains("Input"), @@ -1721,11 +1732,6 @@ fn assert_native_plan( ); return fused; }; - assert_eq!( - fused["children"].as_array().map(Vec::len), - Some(1), - "{context}: a vector must read exactly one child: {fused}" - ); assert_eq!( scans.len(), 1, @@ -1754,49 +1760,40 @@ fn assert_native_plan( !tree.contains("source=1"), "{context}: the tree reads a second source: {tree}" ); - // Every partition the scan exposes did its own work: a gather of the raw input would leave the - // partitions without the input figure, and a partition that never ran would leave both at zero. - // These are input figures: the JSON drops `metric.partition()`, so it cannot say which partition - // produced which values. - let metrics = fused["metrics"] - .as_object() - .unwrap_or_else(|| panic!("{context}: the fused node reports no metrics: {fused}")); - // A partition-local tree evaluates on the partitions its input owns, which need not be every - // declared partition: the router puts each series on one owner, so a small fixture can leave - // some of them empty. What must hold is that the partitions that did work are reported. - let reported = (0..partitions) - .filter(|partition| { - let root_peak = format!("native.p{partition}.root.peak_reserved_bytes"); - metrics - .get(&root_peak) - .and_then(Value::as_u64) - .unwrap_or_default() - > 0 - }) - .count(); + // What the node did is in its own set: the registrations name the partition they ran on, and the + // samples and values are the counts the tree recorded. Which partition did that work is the + // canonical router's hash over the series it was delivered, so these are "some partition" + // claims; the figure the statement fixes - how many values the partitions published between them + // - is asserted against the result by [`assert_native_partition_output`]. + let metrics = node_metric_set(exec); + let published = metrics + .iter() + .filter(|metric| metric.value().name() == "native.root.output_values") + .map(|metric| metric.value().as_usize()) + .sum::(); assert!( - reported > 0, - "no partition of `{context}` reported native work: {fused}" + published > 0, + "no partition of `{context}` published a value: {display}" ); - let read_input = (0..partitions).any(|partition| { - let prefix = format!("native.p{partition}.root."); - metrics.iter().any(|(name, value)| { - name.starts_with(&prefix) - && name.ends_with(".input.peak_reserved_bytes") - && value.as_u64().unwrap_or_default() > 0 - }) - }); + let read = metrics + .iter() + .filter(|metric| metric.value().name() == "native.root.input_samples") + .map(|metric| metric.value().as_usize()) + .sum::(); assert!( - read_input, - "no partition of `{context}` read any input: {fused}" + read > 0, + "no partition of `{context}` read any input sample: {display}" ); fused } -/// Checks that the fused node that actually ran answered the whole result, one partition at a time. +/// Checks that the fused node that actually ran answered the whole result, and that the values its +/// partitions published add up to the rows the result holds. /// -/// The JSON drops `metric.partition()`, so which partition produced which values is only on the -/// captured node. +/// The JSON drops `metric.partition()`, so the per-partition figures are only on the captured node. +/// Which partition produced which value is the canonical router's hash over the series it was +/// delivered, so the total is the assertion; the split is reported in the failure messages, never +/// asserted. fn assert_native_partition_output( fused: &Arc, scan_partitions: Option, @@ -1825,9 +1822,7 @@ fn assert_native_partition_output( {partitions}" ); } - let metrics = fused - .metrics() - .unwrap_or_else(|| panic!("{context}: the executed fused node reports no metrics")); + let metrics = node_metric_set(fused); let mut values_by_partition = vec![0_usize; partitions]; for metric in metrics.iter() { if metric.value().name() != "native.root.output_values" { @@ -1844,9 +1839,11 @@ fn assert_native_partition_output( }); *values += count.value(); } - // A partition-local tree publishes on the partitions that own the series it evaluates: a small - // fixture can leave some of them empty, which is not evidence of lost work. The total below is - // the assertion that every value was produced. + // A partition-local tree publishes on the owners the canonical router hashed each series onto, + // so which partition folds what - and how many partitions fold anything at all - is the router's + // hash of the series it was delivered, not a property of the statement: a small fixture can leave + // some owners empty, which is not evidence of lost work. The total below is the assertion that + // every value was produced. assert!( values_by_partition.iter().any(|values| *values > 0), "{context}: no native execution partition reported values: {values_by_partition:?}" @@ -1858,6 +1855,18 @@ fn assert_native_partition_output( ); } +/// The registrations the fused node's tree made, exactly as they were recorded. +/// +/// [`datafusion::physical_plan::ExecutionPlan::metrics`] leaves them out: a flat set cannot say +/// which tree node recorded what, so the node keeps its own set for readers that key by name. +fn node_metric_set( + plan: &Arc, +) -> datafusion::physical_plan::metrics::MetricsSet { + plan.downcast_ref::() + .unwrap_or_else(|| panic!("the plan is a FusedSeriesExec, not {}", plan.name())) + .node_metric_set() +} + /// The columns of a result as (name, type, nullable). fn result_columns(schema: &SchemaRef) -> Vec<(String, DataType, bool)> { schema @@ -2033,46 +2042,18 @@ async fn assert_raw_scan_fused_formats(observer: &NativeFusedObserver, frontend: observer.clear(); let (_, row_schema, _) = promql_output(frontend, "native_demo", true, "0", "2").await; - let origin = observer - .take_all("native_demo") - .into_iter() - .next() - .expect("the statement ran a fused node"); - // A fused node may read its region through a router and a merge scan rather than directly, so - // the prepared series scan is found in the region's own plan. - let scan = observer - .1 - .lock() - .unwrap() - .last() - .cloned() - .or_else(|| { - let mut candidates = vec![Arc::clone(&origin)]; - let mut stack = candidates.clone(); - while let Some(node) = stack.pop() { - for child in node.children() { - candidates.push(Arc::clone(child)); - stack.push(Arc::clone(child)); - } - } - candidates.into_iter().find(|plan| { - plan.downcast_ref::() - .is_some() - }) - }) - .unwrap_or_else(|| { - panic!( - "the fused input is a real SeriesScanExec: {}", - datafusion::physical_plan::displayable(origin.as_ref()).indent(false) - ) - }); - let scan_plan = Arc::clone(&scan).reset_state().unwrap(); - let scan_ref = scan + let origin = observer.take("native_demo"); + let children = origin.children(); + assert_eq!(children.len(), 1, "the fused origin has exactly one child"); + // The fused node's child is the router over the statement's dispatched raw read, so the raw + // scan is the one the region planned: it is observed on its own rather than nested here. + let scan_plan = observer.take_scan("native_demo"); + let scan = scan_plan .downcast_ref::() - .expect("the fused input is a real SeriesScanExec"); - let raw_schema = Arc::clone(scan_ref.series_schema()); - let label_source = scan_ref.label_source().clone(); - let ordering = scan_ref.raw_series_ordering(); + .expect("the observed raw input is a real SeriesScanExec"); + let raw_schema = Arc::clone(scan.series_schema()); + let label_source = scan.label_source().clone(); + let ordering = scan.raw_series_ordering(); let grid = Arc::new(EvalGrid::try_new(GridSpec::try_new(0, 1_000, 3).unwrap()).unwrap()); let output_schema = Arc::new(SeriesSchema { fields: Arc::clone(&raw_schema.fields), @@ -2128,7 +2109,6 @@ async fn assert_raw_scan_fused_formats(observer: &NativeFusedObserver, frontend: ] { let scan_plan = scan_plan.clone().reset_state().unwrap(); let exec = promql::extension_plan::FusedSeries::try_new_with_format( - Arc::from("native_demo"), tree.clone(), binding.clone(), vec![logical_child.clone()], @@ -2544,10 +2524,13 @@ pub async fn test_prometheus_native_promql_route(store_type: StorageType) { instance.guard.remove_all().await; } -/// Keeps the native fused node the request executed, for the figures only the node records. +/// Keeps the native fused node and the raw scan the request executed, for the figures only they +/// record. /// -/// Uses the existing [`PhysicalPlanWrapper`] hook; `clear` before a query and `take` after its body -/// is drained keep each query reading only the node it ran. +/// The two are observed from different plans: the fused node is the frontend's, while the raw scan +/// is the one the region planned for the statement's raw read. Uses the existing +/// [`PhysicalPlanWrapper`] hook, which runs per plan; `clear` before a query and `take`/`take_scan` +/// after its body is drained keep each query reading only the nodes it ran. #[derive(Default)] struct NativeFusedObserver( Mutex>>, @@ -2555,9 +2538,10 @@ struct NativeFusedObserver( ); impl NativeFusedObserver { - /// Drops what an earlier request captured. + /// Drops both what an earlier request captured: its fused node and its raw scan. fn clear(&self) { self.0.lock().unwrap().clear(); + self.1.lock().unwrap().clear(); } /// The one fused node the request just executed. @@ -2572,6 +2556,23 @@ impl NativeFusedObserver { captured.remove(0) } + /// The one raw scan the request just executed. + /// + /// The fused node reads its region through a router over a merge scan, whose region half is + /// dispatched rather than nested: the plan below the fused node is the router and the merge + /// scan, so the raw scan the statement read is taken from the plan the region planned for it + /// instead of by walking down from the fused node. + fn take_scan(&self, context: &str) -> Arc { + let mut captured = std::mem::take(&mut *self.1.lock().unwrap()); + assert_eq!( + captured.len(), + 1, + "`{context}`: the request executed {} series scans: {captured:?}", + captured.len() + ); + captured.remove(0) + } + /// Every fused node the request just executed. /// /// A statement the route answers with one node yields one; a split aggregate yields the @@ -2745,19 +2746,12 @@ pub async fn test_native_promql_route_plans_the_query(store_type: StorageType) { let (plan, schema, rows) = promql_output(frontend, query, true, start, end).await; assert_rows_eq(&rows, &expected, query); assert_native_result_schema(&schema, tags, query); - let fused = assert_native_plan(&plan, scan_partitions, query); - assert_eq!( - fused["metrics"]["native.root.output_values"].as_u64(), - Some(expected.len() as u64), - "`{query}`: the tree reported a different number of values than the result holds" - ); - // The body is drained, so every partition has finished. - assert_native_partition_output( - &observer.take(query), - scan_partitions, - expected.len(), - query, - ); + // The body is drained, so every partition has finished and the node this statement ran is + // complete; its own registrations - not the flat set the JSON view reads - carry the + // per-partition figures. + let exec = observer.take(query); + assert_native_plan(&plan, &exec, scan_partitions, query); + assert_native_partition_output(&exec, scan_partitions, expected.len(), query); // Without the per-query hint the same statement runs the legacy pipeline, which must answer // with the same rows. @@ -2799,19 +2793,28 @@ pub async fn test_native_promql_route_plans_the_query(store_type: StorageType) { 1, "`{query}`: expected one fused node: {fused_nodes:?}" ); - let fused = &fused_nodes[0]; - let tree = fused["param"].as_str().unwrap_or_default(); - assert_eq!( - fused["children"].as_array().map(Vec::len), - Some(1), - "`{query}`: an empty vector must read exactly one child: {fused}" - ); let scans = plan_nodes_named(&plan, "SeriesScanExec"); assert_eq!( scans.len(), 1, "`{query}`: an empty vector must read exactly one scan: {scans:?}" ); + // The body is drained, so the one fused node the request ran has finished. Its own child list is + // what says how many plans it reads, and its own line is where the tree it ran is still one text: + // the JSON view lists that tree's nodes as children of the operator, next to the plans it reads. + let exec = observer.take(query); + assert_eq!( + exec.children().len(), + 1, + "`{query}`: an empty vector must read exactly one child" + ); + let display = datafusion::physical_plan::displayable(exec.as_ref()) + .indent(false) + .to_string(); + let tree = display + .split_once("native_tree=[") + .map(|(_, tree)| tree.to_string()) + .unwrap_or_else(|| panic!("`{query}`: the fused node renders no tree: {display}")); // The tree binds the one source instead of answering as a constant, whose tree binds no input. assert!( tree.contains("Input"), @@ -2826,8 +2829,6 @@ pub async fn test_native_promql_route_plans_the_query(store_type: StorageType) { !tree.contains("source=1"), "`{query}`: the tree reads a second source: {tree}" ); - // The body is drained, so the one fused node the request ran has finished. - observer.take(query); // Without the per-query hint the same statement runs the legacy pipeline, which must answer // with the same empty typed result. @@ -5660,26 +5661,44 @@ pub async fn test_remote_write_v2_histogram_native_partitions(store_type: Storag } let fused = observer.take("histogram partitions"); assert_eq!(fused.name(), "FusedSeriesExec"); - let metrics = fused.metrics().expect("fused metrics"); + let metrics = node_metric_set(&fused); let mut partitions = BTreeMap::new(); + let mut samples = BTreeMap::new(); for metric in metrics.iter() { - if metric.value().name() == "native.root.output_values" - && let MetricValue::Count { count, .. } = metric.value() - { - *partitions - .entry(metric.partition().expect("partition label")) - .or_insert(0usize) += count.value(); - } + let Some(partition) = metric.partition() else { + continue; + }; + let MetricValue::Count { count, .. } = metric.value() else { + continue; + }; + let by_partition = match metric.value().name() { + "native.root.output_values" => &mut partitions, + "native.root.input_samples" => &mut samples, + _ => continue, + }; + *by_partition.entry(partition).or_insert(0usize) += count.value(); } + // A partition-local tree publishes on the owners the canonical router hashed the series onto, + // so which partition reports a given histogram - and how many partitions report at all - is the + // router's hash of the series it was delivered, not a property of the statement. The total is: + // every series answers once at the single instant this query evaluates at, so the partitions + // together report exactly as many values as the result holds. assert_eq!( - partitions.len(), - 4, - "expected all four scan partitions to report output: {partitions:?}" - ); - assert!( - partitions.values().all(|count| *count > 0), - "empty native output partition: {partitions:?}" + results.len(), + partitions.values().sum::(), + "the native partitions reported a different number of values than the result holds: \ + {partitions:?}" ); + // An owner that published values is one that read the samples they were computed from; an owner + // that was handed nothing reports neither. + for (partition, values) in &partitions { + assert_eq!( + *values > 0, + samples.get(partition).is_some_and(|samples| *samples > 0), + "partition {partition} must read samples exactly when it publishes values; values: \ + {partitions:?}, samples: {samples:?}" + ); + } } pub async fn test_remote_write_v2_histogram_native_range_function(store_type: StorageType) { diff --git a/tests-integration/tests/native_prefix.rs b/tests-integration/tests/native_prefix.rs index 4004c8069dc..7edeb30817a 100644 --- a/tests-integration/tests/native_prefix.rs +++ b/tests-integration/tests/native_prefix.rs @@ -157,7 +157,6 @@ async fn native_prefix_multi_partition_single_position_chains() { }; let frame_schema = evaluated_frame_schema(&tree); let fused = FusedSeries::try_new_with_format( - Arc::from("rate(native_prefix_partitioned[10s])"), tree, binding, vec![child], @@ -347,7 +346,6 @@ async fn native_prefix_multi_partition_multi_chunk_chains() { }; let frame_schema = evaluated_frame_schema(&tree); let fused = FusedSeries::try_new_with_format( - Arc::from("rate(native_prefix_partitioned[10s])"), tree, binding, vec![child], @@ -532,7 +530,6 @@ async fn native_prefix_executes_over_real_mito_region() { // the scan below it: `try_new_with_format` compares the two and refuses a mismatch. let frame_schema = evaluated_frame_schema(&tree); let fused = FusedSeries::try_new_with_format( - Arc::from("rate(native_prefix[3s])"), tree, binding, vec![child],