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 9dce9565c4b..82644ada436 100644 --- a/src/promql/src/extension_plan/fused_series.rs +++ b/src/promql/src/extension_plan/fused_series.rs @@ -16,15 +16,13 @@ //! //! [`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. +//! 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. The text is what names the value columns, not a plan line: +//! neither half echoes the query. //! //! 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 +55,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 +69,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 +97,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,12 +308,12 @@ fn series_frame_output_schema(schema: &SeriesSchema) -> DataFusionResult, /// The compiled tree to run. tree: NativeTree, @@ -679,7 +681,7 @@ impl FusedSeries { self.canonical_producer } - /// The PromQL text this node was compiled from. + /// The PromQL text this node was compiled from, which names the value columns it publishes. pub fn promql(&self) -> &str { &self.promql } @@ -1115,23 +1117,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( @@ -1154,8 +1162,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 @@ -1168,7 +1175,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); @@ -1180,10 +1186,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, @@ -2107,7 +2109,7 @@ 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. + /// The PromQL text the tree was compiled from, which names the value columns it publishes. promql: Arc, /// The tree this node runs, shared by every partition's execution. tree: Arc, @@ -2127,8 +2129,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. @@ -2137,6 +2142,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 @@ -2752,6 +2776,8 @@ impl FusedSeriesExec { input.properties().boundedness }), )); + let metric = Arc::new(ExecutionPlanMetricsSet::new()); + register_tree_metric(&tree, &metric); Ok(Self { promql, tree, @@ -2766,7 +2792,7 @@ impl FusedSeriesExec { evidence: None, claimed: Vec::new(), }), - metric: ExecutionPlanMetricsSet::new(), + metric, properties, format, matching_parallelism, @@ -2778,6 +2804,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")] @@ -2793,14 +2833,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()) + ) } } @@ -2809,8 +2858,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 { diff --git a/src/promql/src/extension_plan/fused_series/tests.rs b/src/promql/src/extension_plan/fused_series/tests.rs index ba3082031d2..cb826202e1f 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,7 +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. +/// The query every tree here is compiled from, which names the value columns it publishes. const PROMQL: &str = "rate(x[5m])"; /// The range the statement's window selects, which is the `5m` of [`PROMQL`]. const RANGE_MS: i64 = 300_000; @@ -2501,15 +2504,14 @@ 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); @@ -2532,9 +2534,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}" @@ -3584,6 +3588,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(), @@ -3600,13 +3619,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. @@ -3615,22 +3637,600 @@ 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, `parameter` for the `k` of +/// a `topk` and `parameter` for the arguments of a histogram function or a 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( + Arc::from(PROMQL), + 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}" + ); + } + + // `topk(scalar(up), up)`: a `k` that changes per point is the topk's `parameter`, so the + // segment the runtime files it under is `parameter` and not `left`, `input` or a bare list. + let grid = grid(); + let vector = instant_of(0, &grid); + let output = vector.output.clone(); + let per_point_k = NativeTree::try_new(NativeNode { + expr: NativeExpr::ParameterizedTopK { + input: Box::new(vector), + op: BoundVectorOp::TopK { + k: 0.0, + top: true, + group: Grouping::By(Arc::from([])), + }, + parameter: Box::new(NativeNode { + expr: NativeExpr::ScalarOf { + input: Box::new(instant_of(1, &grid)), + }, + output: NativeType::Scalar(Arc::clone(&grid)), + }), + }, + output, + }) + .expect("a topk over a per-point k is a legal tree"); + let (lines, snapshot) = displayed_tree_of( + &per_point_k, + vec![ + frames(&[series_chunk("web-1", &series_samples(1.0), true)]), + frames(&[series_chunk("web-2", &series_samples(1.0), true)]), + ], + ) + .await; + assert_eq!( + per_point_k.to_string().lines().count(), + lines.len(), + "the display prints {} lines for a tree of {} nodes:\n{}", + lines.len(), + per_point_k.to_string().lines().count(), + lines.join("\n") + ); + assert_lines_show_their_own_figures( + &per_point_k, + &lines, + &snapshot, + &[ + (0, "root"), + (1, "root.input"), + (2, "root.input.input"), + (3, "root.parameter"), + (4, "root.parameter.input"), + (5, "root.parameter.input.input"), + ], + ); + + // `histogram_fraction(, , rate(buckets[5m]))`: both arguments are read at every + // grid point, so each is one of the histogram's parameters, filed under `parameter` in + // the order the statement wrote them. A histogram function is bound over its bucket vector + // whether or not a fold sits in between, so the window is the whole input here. + let rate = rate_tree(); + let histogram_output = rate.root().output.clone(); + let eval_time = || NativeNode { + expr: NativeExpr::EvalTime { date: None }, + output: NativeType::Scalar(Arc::clone(&grid)), + }; + let fraction = NativeTree::try_new(NativeNode { + expr: NativeExpr::ParameterizedHistogram { + input: Box::new(rate.root().clone()), + op: BoundVectorOp::Histogram(HistogramFunction::Fraction { + lower: 0.0, + upper: 0.0, + }), + parameters: vec![ + eval_time(), + NativeNode { + expr: NativeExpr::Binary { + left: Box::new(eval_time()), + right: Box::new(NativeNode { + expr: NativeExpr::Scalar(BoundScalar { value: 0.5 }), + output: NativeType::Scalar(Arc::clone(&grid)), + }), + matching: BinaryMatching { + op: BinaryOp::Add, + return_bool: false, + labels: MatchingLabels::Ignoring(Vec::new()), + cardinality: Cardinality::OneToOne, + fill: FillValues::default(), + }, + }, + output: NativeType::Scalar(Arc::clone(&grid)), + }, + ], + }, + output: histogram_output, + }) + .expect("a histogram over two per-point parameters is a legal tree"); + let (lines, snapshot) = displayed_tree_of( + &fraction, + vec![frames(&[ + labelled_chunk("a-1", &[("le", "1")], &series_samples(1.0), true), + labelled_chunk("a-inf", &[("le", "+Inf")], &series_samples(4.0), true), + ])], + ) + .await; + assert_eq!( + fraction.to_string().lines().count(), + lines.len(), + "the display prints {} lines for a tree of {} nodes:\n{}", + lines.len(), + fraction.to_string().lines().count(), + lines.join("\n") + ); + assert_lines_show_their_own_figures( + &fraction, + &lines, + &snapshot, + &[ + (0, "root"), + (1, "root.input"), + (2, "root.input.input"), + (3, "root.parameter0"), + (4, "root.parameter1"), + (5, "root.parameter1.left"), + ], + ); +} + +/// `tree` executed over `sources` - one source's frames per element - and the tree block the +/// executed node displays, one line per node, with the figures the run filed. +async fn displayed_tree_of( + tree: &NativeTree, + sources: Vec>, +) -> (Vec, NodeMetricsSnapshot) { + let exec = FusedSeries::try_new( + Arc::from(PROMQL), + tree.clone(), + binding(tree), + (0..sources.len()) + .map(|_| input_plan(frame_schema())) + .collect(), + output_schema().to_dfschema_ref().unwrap(), + ) + .expect("the binding matches the tree") + .to_execution_plan( + sources + .into_iter() + .map(|batches| { + ( + memory_exec(batches), + RawLabelSource::Explicit, + SeriesOrdering::Contiguous, + ) + }) + .collect(), + ) + .expect("the tree runs"); + collect(Arc::clone(&exec), SessionContext::default().task_ctx()) + .await + .expect("the node runs"); + 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 != "]") + .map(str::to_string) + .collect::>(); + let registered: ExecutionPlanMetricsSet = node_metric_set(&exec).into(); + (lines, NodeMetricsSnapshot::read(®istered)) +} + +/// The line at `index` of the display of `tree` is the tree's own line for the node at `path`, +/// with exactly that node's figures appended. +/// +/// The line is compared whole - its indentation, its words and its figures - so a display that +/// reads a child's figures from a segment the runtime did not file them under prints a line that +/// has no figures at all, and one that shows another node's figures prints figures that differ. +fn assert_lines_show_their_own_figures( + tree: &NativeTree, + lines: &[String], + snapshot: &NodeMetricsSnapshot, + lines_of_paths: &[(usize, &str)], +) { + let shape = tree.to_string(); + let shape = shape.lines().collect::>(); + for (index, path) in lines_of_paths { + let described = snapshot.described(path); + assert!( + !described.is_empty(), + "{path} recorded no figure at all: {snapshot:?}" + ); + let shown = described + .iter() + .map(|(name, value)| format_metric(name, *value)) + .collect::>() + .join(", "); + assert_eq!( + lines[*index], + format!("{}, metrics=[{shown}]", shape[*index]), + "the line at {index} is not {path} with its own figures:\n{}", + lines.join("\n") + ); + } +} + /// `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() { @@ -3689,9 +4289,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")); } @@ -4763,7 +5361,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, @@ -4923,9 +5521,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")); } @@ -5802,108 +6398,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(); @@ -5925,7 +6420,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)); @@ -5991,9 +6485,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. @@ -6003,13 +6495,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. @@ -6020,16 +6511,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")); @@ -6055,16 +6542,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), @@ -6087,9 +6572,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), @@ -6119,16 +6602,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" ); @@ -6147,8 +6628,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), @@ -7208,15 +7689,14 @@ 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), @@ -7230,16 +7710,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}" ); } @@ -8093,8 +8587,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}" ); 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/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/query/src/analyze.rs b/src/query/src/analyze.rs index 34f1c69ee30..7b0448fa471 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,161 @@ 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. + /// Both kinds of child hang from that one line - the nodes the operator runs first, then the + /// physical input it reads - so neither may replace the other. + /// + /// 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( + Arc::from("value"), + 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 collected = collector.record_batch_metrics; + // The fused node keeps its physical child: the record below is the whole plan, which + // is what makes the two kinds of child - the nodes it runs and the input it reads - + // both reachable from the operator's line. + let collected_levels = collected + .plan_metrics + .iter() + .map(|plan_metrics| { + ( + plan_metrics.level, + plan_metrics.plan.lines().next().unwrap(), + ) + }) + .collect::>(); + assert_eq!( + collected_levels, + vec![(0, header), (1, "EmptyExec")], + "the fused node and its physical child are both collected" + ); + let plan = &collected.plan_metrics[0]; + 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); + // The nodes the operator runs hang below its own line, outermost first. + 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" + ); + // And the physical child the plan gives it follows them, so a record that keeps only + // one of the two kinds of child is not the plan the operator runs. + assert_eq!( + value["children"].as_array().map(Vec::len), + Some(2), + "the operator's record carries its tree and its physical child: {value}" + ); + assert_eq!(value["children"][1]["name"], "EmptyExec"); + assert_eq!( + value["children"][1]["param"], "", + "a physical child with no parameters names none: {value}" + ); + assert_eq!( + value["children"][1]["children"].as_array().map(Vec::len), + Some(0), + "the fused node's physical child reads nothing below it: {value}" + ); + } + } + #[test] fn qbs_analyze_json_splits_plan_name_and_parameters() { let (_, metrics) = JsonMetrics::from_plan_metrics(PlanMetrics { @@ -526,6 +735,7 @@ mod tests { plan_name: "FilterExec".to_string(), level: 0, metrics: vec![], + native: None, }); assert_eq!(metrics.name, "FilterExec"); @@ -542,12 +752,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..0aa387a04b6 100644 --- a/src/query/src/dist_plan/analyzer/test.rs +++ b/src/query/src/dist_plan/analyzer/test.rs @@ -422,7 +422,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/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/promql/native_compile/tests.rs b/src/query/src/promql/native_compile/tests.rs index 8be77c1c5fd..31f61ffc2b7 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", ], ), ]; @@ -1309,10 +1305,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" ); } @@ -3024,8 +3020,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( @@ -4469,8 +4465,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}`"); } } @@ -4501,12 +4497,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}" ); } @@ -5271,8 +5267,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}`"); } } @@ -5294,8 +5290,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/test.rs b/src/query/src/promql/planner/test.rs index 7545b006e77..0c90c6f7a43 100644 --- a/src/query/src/promql/planner/test.rs +++ b/src/query/src/promql/planner/test.rs @@ -2592,14 +2592,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. @@ -2637,7 +2637,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}" ); @@ -2675,7 +2675,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}" ); } @@ -3185,14 +3185,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 plan line is the compiled tree, not the query text the tree was compiled from. let fused = find_fused_series(&plan).expect("the plan holds a native node"); - assert_eq!("rate(some_metric[5m])", fused.promql()); assert_eq!( // The value is named after the expression, and the rows layout declares the per-row name // column behind the labels. @@ -5059,7 +5058,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 @@ -5071,7 +5069,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() @@ -5102,7 +5102,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!( @@ -5999,6 +5998,52 @@ fn expected_native_column_names(query: &str, tags: &[&str]) -> 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. Grouping labels, values and timestamps retain their existing schema roles. #[tokio::test] @@ -6054,13 +6099,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}" ) } @@ -6191,7 +6236,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}"); }; @@ -7324,7 +7368,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}" ); @@ -10552,7 +10596,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/tests-integration/src/tests/native_aggregate_test.rs b/tests-integration/src/tests/native_aggregate_test.rs index 325190c5e93..35fb5078670 100644 --- a/tests-integration/src/tests/native_aggregate_test.rs +++ b/tests-integration/src/tests/native_aggregate_test.rs @@ -18,31 +18,37 @@ //! The table is ordinary - one region, no `PARTITION` clause - and its five series sit in four SST //! files and one memtable range. The region's scan still splits them into the four partitions the //! request asks for, but that split stays in the region: above the boundary the raw frames are -//! routed by stored key over the four owners the execution asks for, so the metric assertion below -//! checks that every owner runs the fold and the fixture's totals check what they fold together. +//! routed by stored key over the four owners the execution asks for. Which owner folds a series, and +//! how many owners fold anything at all, is therefore that router's hash of each series' stored key +//! and not a property of this fixture, so the metric assertions below 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 a split aggregate above the region boundary, and that tree is checked in the -//! plan the engine really executed. Both halves of the split live there: the Partial -//! `FusedSeriesExec` reads the regions' raw frames through a `SeriesRepartitionExec` that routes -//! whole series by their stored key out to the four partitions the execution was asked for, and the -//! Final `FusedSeriesExec` above it reads that state through a second `SeriesRepartitionExec` that -//! gathers each group's contributions where they are folded. `native.root.output_values` is reported -//! by the Partial producer for every one of the four owners; the metric is read after the result -//! stream was drained, the only point at which it is complete. A per-series segment the analyzer -//! accepts instead runs beside the region's scan, where its input is that scan itself. Each chain is -//! checked 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 native run plans two trees, and both are checked in the plans the engines really executed. +//! Both halves of the split live in the frontend one: the Partial `FusedSeriesExec` reads the +//! regions' raw frames through a `SeriesRepartitionExec` that routes whole series by their stored key +//! out to the four partitions the execution was asked for - or, for a per-series segment the +//! analyzer accepts instead, the region's `SeriesScanExec` itself, which is already that wide - and +//! the Final `FusedSeriesExec` above it reads that state through a second `SeriesRepartitionExec` +//! that gathers each group's contributions where they are folded. The raw `SeriesScanExec` behind the +//! producer's merge is in the separately captured region physical plan. The fused node publishes the +//! values it folded from them; the metric is read after the result stream was drained, the only point +//! at which it is complete. 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 - every owner folds a partial state -//! and only a merge over *all* of them turns them 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 @@ -412,16 +418,18 @@ 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 the fold must be spread over more than one of the four owners the raw-frame router - /// assigns. Which owner a series lands on is that router's decision, so this is not a claim that - /// every owner folds something - the fixture's five series need not fill four of them. - per_partition: bool, - /// The number of values the owners' 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` the fold must also not collapse onto one owner. `None` leaves + /// 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 @@ -434,7 +442,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 @@ -448,20 +456,20 @@ 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, } } -/// A case over [`COUNTER_TABLE`], which additionally pins the total the producers 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 owners' producers together report -/// exactly as many values as there are counter series. Which owner folds which series is the -/// raw-frame router's decision, so only the total is pinned. +/// `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. @@ -473,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) } } @@ -681,34 +689,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:?}" @@ -734,14 +748,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:?}" @@ -1324,9 +1345,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; @@ -1347,6 +1373,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, @@ -1373,8 +1415,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; @@ -1396,6 +1439,43 @@ fn final_input_samples_per_partition(capture: &NativeCapture, context: &str) -> per_partition } +/// The Final merges' root `native.root.output_values` counts, summed per partition. +/// +/// 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 its raw source at the scan width, wherever it was placed. /// /// The dist analyzer decides where a producer runs, and both placements execute at the session's @@ -1411,11 +1491,11 @@ fn final_input_samples_per_partition(capture: &NativeCapture, context: &str) -> /// One stored key routes to exactly one owner, so each owner's fold still sees whole series, and /// the router re-declares the frames the merge scan publishes instead of reshaping them. /// -/// Both chains are pinned rather than allowed: the exchange in the frontend chain is exactly one -/// `SeriesRepartitionExec` with raw-key routing over the merge scan, the merge scan is its single -/// leaf input, and the region chain has no exchange above the scan. A `CoalesceExec`, a second -/// exchange, a narrower scan, or a producer over anything else is a different topology and fails -/// here. +/// Both chains are pinned rather than allowed: the exchange is exactly one `SeriesRepartitionExec` +/// with raw-key routing over the merge scan - or over the scan itself, where that scan stayed local - +/// the merge scan is its single leaf input, and the region chain has no exchange above the scan. A +/// `CoalesceExec`, a second exchange, a narrower scan, or a producer over anything else is a +/// different topology and fails here. fn assert_fused_reads_raw_source(fused: &Arc, context: &str) { let plan = displayable(fused.as_ref()).indent(false).to_string(); assert_eq!( @@ -1472,13 +1552,27 @@ fn assert_fused_reads_raw_source(fused: &Arc, context: &str) routed[0].schema(), "{context}: the raw-frame router must re-declare the frames the region stream publishes:\n{plan}" ); + // The router either closes the region boundary over the merge scan, or reads the scan itself + // where that scan stayed local; nothing else may sit below it. let boundary = through_cooperative(routed[0]); assert!( - boundary.is::(), - "{context}: the raw-frame router must read the region `MergeScanExec` itself, with no other \ - exchange between them, but it reads `{}`:\n{plan}", + boundary.is::() || boundary.is::(), + "{context}: the raw-frame router must read the region `MergeScanExec` or the raw series \ + scan itself, with no other exchange between them, but it reads `{}`:\n{plan}", boundary.name() ); + if boundary.is::() { + assert_eq!( + SCAN_PARTITIONS, + boundary + .properties() + .output_partitioning() + .partition_count(), + "{context}: `SeriesScanExec` must expose the {SCAN_PARTITIONS} scan partitions the \ + fused producer reads:\n{plan}" + ); + return; + } assert!( boundary.children().is_empty(), "{context}: `MergeScanExec` must be a leaf:\n{plan}" @@ -1548,10 +1642,10 @@ fn through_cooperative(node: &Arc) -> &Arc /// every group's contributions where they meet, so the Final's physical planning routes its state /// input: to a single owner when it folds one group or one histogram's bounds /// (`histogram_quantile` over a `le`-only grouping), and out to the session's width otherwise. Both -/// halves of the split are planned in the frontend above the region boundary, so the router reads the -/// captured Partial producer itself - not a region merge scan - and the producer keeps the width its -/// raw-frame router gave it. The router re-declares the Partial state frame and routes canonical -/// labels, never raw keys. +/// halves of the split are planned in the frontend, and the region boundary is planned under the +/// producer, so the Final's router reads the captured Partial producer itself - not a region merge +/// scan - and the producer keeps the width its raw-frame router gave it. The router re-declares the +/// Partial state frame and routes canonical labels, never raw keys. fn assert_finalize_routes_partial_state( finalize: &Arc, producer: &Arc, @@ -1637,14 +1731,18 @@ fn assert_finalize_routes_partial_state( let producer_schema = producer.schema(); 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"), - "{context}: the captured Partial producer must have a Partial root, got `{root_line}`:\n{producer_plan}" + 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 = SeriesFrameDeclaration::parse(producer_schema.as_ref()).unwrap_or_else(|error| { @@ -1855,29 +1953,23 @@ async fn check_two_source_segregation( let plan = displayable(segments[index].as_ref()) .indent(false) .to_string(); - assert!( - values.iter().all(Option::is_some), - "`{query}`: segment {index} must run on every owner it is planned with, but one of them \ - reported no `{ROOT_OUTPUT_VALUES_METRIC}`; both segment vectors: {vectors:?}; parent \ - plan:\n{parent_display}; segment plan:\n{plan}" - ); + if values.iter().all(Option::is_none) { + panic!( + "`{query}`: segment {index} does not publish `{ROOT_OUTPUT_VALUES_METRIC}`; \ + both segment vectors: {vectors:?}; parent plan:\n{parent_display}; segment plan:\n{plan}" + ); + } // What each owner folds is the placement's decision: a region-local segment keeps the // region scan's partitions, while a frontend one is spread by the raw-key router over the - // target width, so the same series can leave an owner empty. The totals are what the - // fixture fixes - both fixture tables hold the same five series, and a segment answers one - // value per series of its source - so those are asserted instead of the spread. + // target width, so the same series can leave an owner empty. The total is what the fixture + // fixes - both fixture tables hold the same five series, and a segment answers one value per + // series of its source - so that is asserted instead of the spread. assert_eq!( COUNTER_SERIES.len(), values.iter().flatten().sum::(), "`{query}`: segment {index} must answer one value per series of its source; both \ segment vectors: {vectors:?}; parent plan:\n{parent_display}; segment plan:\n{plan}" ); - assert!( - values.iter().flatten().filter(|count| **count > 0).count() > 1, - "`{query}`: segment {index} must produce values on more than one owner, otherwise the \ - segment is not parallel at all; both segment vectors: {vectors:?}; parent plan:\n\ - {parent_display}; segment plan:\n{plan}" - ); } } @@ -2040,7 +2132,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!( @@ -2050,10 +2144,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( @@ -2064,39 +2154,36 @@ async fn check_case(instance: &Arc, observer: &NativePlanObserver, cas common_telemetry::info!( "`{query}` native per-partition `{ROOT_OUTPUT_VALUES_METRIC}` counts: {per_partition:?}" ); - // The producer's owners are the raw-frame router's buckets, not the region scan's partitions: - // every one of the four runs the producer, but which of them a series lands on is the router's - // decision, and five series spread over four owners need not fill each one. What the fixture - // fixes is the fold's *total*, which the `native.root.input_samples` check below pins, so this - // block asserts that every owner is live and, for a whole-vector fold, that the work was spread - // rather than collapsed onto one owner. - for (partition, values) in per_partition.iter().enumerate() { - assert!( - values.is_some(), - "`{query}`: the fused producer must run on all {SCAN_PARTITIONS} owners the raw-frame \ - router gives it, but owner {partition} reported no `{ROOT_OUTPUT_VALUES_METRIC}`; all \ - owners: {per_partition:?}" - ); - } - let folded_owners = per_partition - .iter() - .flatten() - .filter(|values| **values > 0) - .count(); + // 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 which partition read a series says nothing + // about where it is folded: neither how many owners folded nor what each one folded is asserted. + // A producer that folded nothing at all is still a failure. assert!( - folded_owners > 0, - "`{query}`: the fused producer must report folded values, got {per_partition:?}" + 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 case.per_partition { - assert!( - folded_owners > 1, - "`{query}`: the fold must be spread over more than one of the {SCAN_PARTITIONS} owners, \ - otherwise no partition of the split did any work; got {per_partition:?}" - ); - } if let Some(root_values) = case.root_values { - // One value per series that folded, so the owners' 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, @@ -2105,24 +2192,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(Option::is_some), - "`{query}`: every owner must report 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 case.per_partition { - assert!( - input_samples.iter().flatten().copied().max().unwrap_or(0) < root_values, - "`{query}`: the split must not fold every series on one owner, got {input_samples:?}" + 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 owner 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. } } @@ -2237,19 +2323,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, &expression_name(&promql), &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!( @@ -2265,6 +2358,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`). @@ -2388,76 +2488,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( @@ -2469,7 +2555,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( @@ -2484,26 +2571,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 pins the answer and the + // producer's activity without asserting anything per owner. 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"), @@ -2512,7 +2598,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( @@ -2592,15 +2677,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, ) }, ]; @@ -2612,11 +2696,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)))"), @@ -2633,7 +2713,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"); @@ -2644,7 +2724,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; @@ -2654,11 +2734,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; @@ -2702,7 +2778,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 9efc2eaa76f..01e9c26b5c5 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!( @@ -5792,7 +5840,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) @@ -5882,9 +5930,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() { @@ -5945,13 +5991,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"), @@ -6479,7 +6525,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)) @@ -7351,9 +7397,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 } @@ -7606,9 +7650,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 } @@ -7628,9 +7670,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 } @@ -10029,9 +10069,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 } @@ -10043,9 +10081,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 } @@ -10433,7 +10469,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 @@ -10478,9 +10514,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 } @@ -10493,10 +10527,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")); } } } @@ -10673,7 +10708,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::>(); @@ -11271,20 +11306,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) ); - // Every selector reads through its own raw source, so the two occurrences of the repeated - // selector name two of them, and each source wraps the region scan it reads. + // 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}" ); + 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(), - "each occurrence of the repeated selector reads its own raw source:\n{}", - cte_native_observed_plan_trees(&observer) + occurrences.len(), + "the region reads one raw source per occurrence of the repeated selector:\n{trees}" ); assert_eq!( native_schema, @@ -12728,9 +12779,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 } @@ -12831,9 +12880,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 } @@ -12920,9 +12967,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 } @@ -13403,21 +13448,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!( @@ -13440,13 +13486,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}" @@ -14046,20 +14092,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); @@ -16035,7 +16081,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!( @@ -16110,7 +16156,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!( @@ -16205,7 +16251,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 c5353da192f..af312b5ec5c 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 @@ -2031,46 +2040,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), @@ -2546,10 +2527,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>>, @@ -2557,9 +2541,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. @@ -2574,6 +2559,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 @@ -2747,19 +2749,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, 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. @@ -2821,19 +2816,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"), @@ -2848,8 +2852,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. @@ -5689,36 +5691,53 @@ pub async fn test_remote_write_v2_histogram_native_partitions(store_type: Storag assert!(written.is_empty(), "missing series: {written:?}"); 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:?}" + results.len(), + partitions.values().sum::(), + "the native partitions reported a different number of values than the result holds: \ + {partitions:?}" ); - // Routing hashes each series' label set, so eight series over four partitions can legitimately - // leave one of them empty. What the topology has to guarantee is that the series are spread - // over more than one partition - otherwise the merge this test exists for is never exercised - - // and that the routing loses none of them. + // Routing hashes each series' label set, so eight series over four owners can legitimately leave + // one of them empty. What the topology has to guarantee is that the series are spread over more + // than one owner - otherwise the merge this test exists for is never exercised - and that the + // routing loses none of them. let non_empty = partitions.values().filter(|count| **count > 0).count(); assert!( non_empty >= 2, "the fused producer must run on more than one partition: {partitions:?}" ); - assert_eq!( - 8, - partitions.values().sum::(), - "every series must produce exactly one value: {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) {