From 20d49d7b44eb77292babf914d18068247243bbca Mon Sep 17 00:00:00 2001 From: discord9 <55937128+discord9@users.noreply.github.com> Date: Wed, 21 Oct 2026 01:39:04 +0800 Subject: [PATCH] feat(promql): drive a native tree from a DataFusion plan The tree executor ends at `EvaluatedChunk`, a Rust value, so nothing could put a native tree into a query plan: `RecordBatch` appears nowhere under `native/`. This adds the plan that drives it. `NativeTreeExec` is a caller, not a converter. Each partition builds its own `NativeExecutionState` from the child's memory pool, feeds it the raw chunks the child's frame decodes into, and converts what the root reports into batches. The tree is compiled before the plan exists and never changes, so nothing here interprets a function name or resolves a field. Which output column reads which part of the root output is decided once, in `try_new`: the time column name, the tag set and the sample types are the caller's, because a tree carries the series a node computes, not the table a query was written against. A column that matches nothing is an error rather than being assigned to an unclaimed field. Two limits are stated in the module documentation rather than papered over. A raw frame is a per-series stream, so a series must arrive whole in one partition; detecting a label set that recurs across partitions needs an owner above the partitions, which this plan does not have. And a raw frame declares both a float and a histogram carrier for every field, so a tree whose output holds native histograms is refused when such a sample is converted, not before the child runs. The planner still builds the old path: this makes the tree reachable from a real execution, not from a real query. Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> --- src/promql/src/extension_plan.rs | 2 + src/promql/src/extension_plan/native_tree.rs | 727 +++++++ .../src/extension_plan/native_tree/tests.rs | 1891 +++++++++++++++++ 3 files changed, 2620 insertions(+) create mode 100644 src/promql/src/extension_plan/native_tree.rs create mode 100644 src/promql/src/extension_plan/native_tree/tests.rs diff --git a/src/promql/src/extension_plan.rs b/src/promql/src/extension_plan.rs index 750ee41b5e8..f3b3af9e672 100644 --- a/src/promql/src/extension_plan.rs +++ b/src/promql/src/extension_plan.rs @@ -16,6 +16,7 @@ mod absent; mod empty_metric; mod histogram_fold; mod instant_manipulate; +mod native_tree; mod normalize; mod planner; mod range_manipulate; @@ -41,6 +42,7 @@ pub use histogram_fold::{ HistogramFold, HistogramFoldExec, HistogramFoldOperation, HistogramFoldStream, }; pub use instant_manipulate::{InstantManipulate, InstantManipulateExec, InstantManipulateStream}; +pub use native_tree::NativeTreeExec; pub use normalize::{SeriesNormalize, SeriesNormalizeExec, SeriesNormalizeStream}; pub use planner::PromExtensionPlanner; pub use range_manipulate::{RangeManipulate, RangeManipulateExec, RangeManipulateStream}; diff --git a/src/promql/src/extension_plan/native_tree.rs b/src/promql/src/extension_plan/native_tree.rs new file mode 100644 index 00000000000..e1c4e4de06e --- /dev/null +++ b/src/promql/src/extension_plan/native_tree.rs @@ -0,0 +1,727 @@ +// Copyright 2023 Greptime Team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! A DataFusion plan that drives one [`NativeTree`] over a prepared raw-series frame. +//! +//! The plan is the caller of [`NativeExecutor`]: every partition builds its own +//! [`NativeExecutionState`] from the child's pool, feeds it the raw chunks the child's frame +//! decodes into, and converts the evaluated chunks the root hands back into record batches. The +//! tree is compiled before this plan exists and never changes, so nothing here interprets a +//! function name, resolves a field or picks a column. +//! +//! Which column of this plan's output reads which part of the root output is decided once, in +//! [`NativeTreeExec::try_new`], and every batch is assembled from that binding. The time column +//! name, the tag set and the sample types are caller parameters: the tree carries the series a node +//! computes, not the table a query was written against, so it has none of them. +//! +//! # Not covered here +//! +//! A tree is fed by exactly one raw-frame child, and a raw frame is a per-series stream, so a +//! series must arrive whole in one partition. Detecting a label set that recurs across partitions +//! needs an owner above the partitions (the plan-level `Emitted` of the aggregate stage), which +//! this plan does not have: a caller must keep every series of one label set in one partition. +//! +//! The sample types are likewise part of the caller's declaration, not something this plan can +//! prove: a raw frame declares both a float and a histogram carrier for every field, so a tree +//! whose output turns out to hold native histograms is refused when such a sample is converted +//! rather than before the child runs. + +use std::collections::VecDeque; +use std::fmt; +use std::pin::Pin; +use std::sync::Arc; +use std::task::{Context, Poll}; + +use common_query::series_frame::{raw_series_frame_ordering, unpack_raw_series}; +use datafusion::arrow::array::{ArrayRef, Float64Array, StringArray, TimestampMillisecondArray}; +use datafusion::arrow::datatypes::{DataType, SchemaRef, TimeUnit}; +use datafusion::arrow::record_batch::RecordBatch; +use datafusion::common::tree_node::TreeNodeRecursion; +use datafusion::error::{DataFusionError, Result as DfResult}; +use datafusion::execution::context::TaskContext; +use datafusion::physical_expr::EquivalenceProperties; +use datafusion::physical_plan::execution_plan::{Boundedness, EmissionType}; +use datafusion::physical_plan::metrics::{ExecutionPlanMetricsSet, MetricsSet}; +use datafusion::physical_plan::stream::RecordBatchStreamAdapter; +use datafusion::physical_plan::{ + DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning, PhysicalExpr, PlanProperties, + RecordBatchStream, SendableRecordBatchStream, +}; +use datatypes::series::samples::{SampleRef, Series, SeriesSchema}; +use datatypes::series::{MetricName, SeriesOrdering}; +use futures::{Stream, ready}; +use store_api::region_engine::{RawLabelSource, SendableRawSeriesStream}; + +use crate::evaluated::EvaluatedChunk; +use crate::native::execute::{NativeExecutionState, NativeExecutor}; +use crate::native::metrics::walk; +use crate::native::plan::{NativeExpr, NativeTree}; +use crate::source::materialize_raw_labels; + +#[cfg(test)] +#[path = "native_tree/tests.rs"] +mod tests; + +/// How many push or pull steps one poll performs before it wakes itself. +/// +/// A step hands one raw chunk to the tree, asks the root for one chunk, or closes the tree. The +/// budget keeps one poll bounded even when the child's frames hold many small chunks that settle +/// nothing, and it is what stops a poll from reading the child to its end before answering. +const POLL_BUDGET: usize = 64; + +/// One column of the output, and where in a root chunk it reads from. +#[derive(Clone, Debug)] +enum OutputColumn { + /// The timestamp the chunk's own axis carries. + Time, + /// Field `index` of the chunk's series, by its position in the root schema. + Field { index: usize }, + /// One label of the series' final metric. + Tag { + /// The label name. + name: Arc, + /// Whether a series without this label reports NULL rather than the empty string. + nullable: bool, + }, +} + +impl OutputColumn { + /// What this column is, for a rejection that has to say which column it reports on. + fn describe(&self) -> &'static str { + match self { + Self::Time => "the time column", + Self::Field { .. } => "a field", + Self::Tag { .. } => "a tag", + } + } +} + +/// Which part of a root chunk every output column reads. +/// +/// The binding is built once, from the root output's field list and the caller's time column and +/// tag set, and then only read: converting a chunk is a walk over `columns`. +#[derive(Debug)] +struct OutputBinding { + /// The batch schema this plan produces. + schema: SchemaRef, + /// One entry per column of `schema`, in its order. + columns: Vec, +} + +impl OutputBinding { + /// Binds every column of `schema` to the role it reads from `root`. + /// + /// A column that is not the time column, one of `tags` or a field of `root` is an error rather + /// than a column filled by guesswork, and so is a name two roles claim, a field of `root` that + /// `schema` does not carry, a name `schema` declares twice, and a column whose declared type + /// the role cannot fill. + fn try_new( + root: &SeriesSchema, + schema: SchemaRef, + time_column: &str, + tags: &[String], + ) -> DfResult { + let names = schema + .fields() + .iter() + .map(|field| field.name().as_str()) + .collect::>(); + for (index, name) in names.iter().enumerate() { + if names[..index].contains(name) { + return Err(plan_error(format!( + "the output schema declares column `{name}` twice" + ))); + } + } + let mut columns: Vec> = vec![None; names.len()]; + let mut claim = |role: OutputColumn, name: &str| -> DfResult<()> { + let index = names + .iter() + .position(|column| *column == name) + .ok_or_else(|| { + plan_error(format!( + "the output schema has no column `{name}` for {}", + role.describe() + )) + })?; + let claimed = role.describe(); + match columns[index].replace(role) { + None => Ok(()), + Some(previous) => Err(plan_error(format!( + "output column `{name}` is claimed as both {} and {claimed}", + previous.describe() + ))), + } + }; + claim(OutputColumn::Time, time_column)?; + for tag in tags { + // A tag column that is not nullable reports the empty string for a series without the + // label, which is a declaration this plan honours; the schema says which it is. + let nullable = schema + .field_with_name(tag) + .map(|field| field.is_nullable()) + .unwrap_or(false); + claim( + OutputColumn::Tag { + name: Arc::from(tag.as_str()), + nullable, + }, + tag, + )?; + } + for (index, field) in root.fields.iter().enumerate() { + claim(OutputColumn::Field { index }, &field.name)?; + } + let columns = columns + .into_iter() + .enumerate() + .map(|(index, column)| { + column.ok_or_else(|| { + plan_error(format!( + "output column `{}` is not the time column, one of the tags, or a field of \ + the native tree's root output", + names[index] + )) + }) + }) + .collect::>>()?; + for (field, column) in schema.fields().iter().zip(columns.iter()) { + check_column_type(field.data_type(), field.is_nullable(), column)?; + } + Ok(Self { schema, columns }) + } + + /// Converts one evaluated chunk into a batch, or `None` when it carries no row. + /// + /// The rows are the positions the chunk's axis actually holds, so a sparse series keeps its + /// runs and its gaps instead of being filled out to its grid. A field that holds no sample at + /// a row reports NULL there while the row itself stays, and a sample that is a native + /// histogram is an error: it is not a float, and neither NULL nor a dropped row would say so. + fn convert(&self, chunk: EvaluatedChunk) -> DfResult> { + let rows = chunk.series.time.len(); + if rows == 0 { + return Ok(None); + } + let mut arrays = Vec::with_capacity(self.columns.len()); + for (field, column) in self.schema.fields().iter().zip(self.columns.iter()) { + arrays.push(match column { + OutputColumn::Time => Arc::new(timestamps(&chunk.series, rows)?) as ArrayRef, + OutputColumn::Field { index } => Arc::new(field_column( + &chunk.series, + *index, + rows, + field.name().as_str(), + )?), + OutputColumn::Tag { name, nullable } => { + Arc::new(tag_column(&chunk.series.metric, name, *nullable, rows)) + } + }); + } + Ok(Some(RecordBatch::try_new(self.schema.clone(), arrays)?)) + } +} + +/// Checks that a column declared as `data_type` can hold what `column` reads. +fn check_column_type(data_type: &DataType, nullable: bool, column: &OutputColumn) -> DfResult<()> { + let (expected, covered) = match column { + OutputColumn::Time => ( + "a Timestamp(Millisecond, None)", + matches!(data_type, DataType::Timestamp(TimeUnit::Millisecond, None)), + ), + // A position a field holds no sample at is a NULL in the batch, so a field column that + // cannot carry one would only fail once a series was missing a value. + OutputColumn::Field { .. } => ( + "a nullable Float64", + matches!(data_type, DataType::Float64) && nullable, + ), + OutputColumn::Tag { .. } => ("a Utf8", matches!(data_type, DataType::Utf8)), + }; + if covered { + Ok(()) + } else { + Err(plan_error(format!( + "{} must be declared as {expected} (nullable: {nullable}), not {data_type}", + column.describe() + ))) + } +} + +/// The timestamps `series` carries, one per row. +fn timestamps(series: &Series, rows: usize) -> DfResult { + let mut values = Vec::with_capacity(rows); + for row in 0..rows { + let timestamp = series.time.timestamp(row).ok_or_else(|| { + DataFusionError::Execution(format!( + "the evaluated chunk carries no timestamp at row {row}" + )) + })?; + values.push(timestamp); + } + Ok(TimestampMillisecondArray::from(values)) +} + +/// Field `index` of `series` as the column named `name`, one row per carried position. +fn field_column(series: &Series, index: usize, rows: usize, name: &str) -> DfResult { + let field = series.fields.get(index).ok_or_else(|| { + DataFusionError::Execution(format!( + "column `{name}` reads field {index} of the root output, which the evaluated chunk does \ + not carry" + )) + })?; + if field.len() != rows { + return Err(DataFusionError::Execution(format!( + "field {index} of column `{name}` holds {} sample(s) but the chunk's axis holds {rows}", + field.len() + ))); + } + let mut values = Vec::with_capacity(rows); + for sample in field.iter() { + match sample { + SampleRef::Missing => values.push(None), + // An ordinary NaN is a value rather than a missing sample: it stays a value here. + SampleRef::Float(value) => values.push(Some(value)), + SampleRef::Histogram(_) => { + return Err(DataFusionError::Execution(format!( + "column `{name}` reads field {index} of the root output as Float64, but the \ + evaluated chunk holds a native histogram there" + ))); + } + } + } + Ok(Float64Array::from(values)) +} + +/// Label `name` of `metric`, repeated for every row. +fn tag_column(metric: &MetricName, name: &str, nullable: bool, rows: usize) -> StringArray { + let value = match metric.get(name) { + Some(value) => Some(value), + // A nullable column separates "this series has no such label" from an empty value; a + // non-nullable one cannot, and reports the empty string it declared. + None if nullable => None, + None => Some(""), + }; + StringArray::from(vec![value; rows]) +} + +/// The raw field layout of the tree's one input node. +fn raw_source(tree: &NativeTree) -> DfResult> { + let mut sources = Vec::new(); + let mut unsupported = None; + walk(tree.root(), &mut |node, path| match &node.expr { + NativeExpr::Input(_) => sources.push((node.output.samples().cloned(), path.clone())), + NativeExpr::Instant { .. } | NativeExpr::Window { .. } | NativeExpr::Map { .. } => {} + // The kinds below are legal in a bound tree and are executed elsewhere; this plan is not + // the runtime for them, so it names the one it met instead of running a partial tree. + NativeExpr::Scalar(_) => { + unsupported.get_or_insert(("a scalar", path.clone())); + } + NativeExpr::Aggregate { .. } => { + unsupported.get_or_insert(("an aggregate", path.clone())); + } + NativeExpr::Binary { .. } => { + unsupported.get_or_insert(("a binary operation", path.clone())); + } + }); + if let Some((kind, path)) = unsupported { + return Err(plan_error(format!( + "the native tree node at `{path}` is {kind}, which this plan does not execute: it runs \ + Input, Instant, Window and Map nodes only" + ))); + } + let [(source, path)] = sources.as_slice() else { + return Err(plan_error(format!( + "the native tree has {} raw input node(s), but a raw frame feeds exactly one", + sources.len() + ))); + }; + let schema = source.as_ref().ok_or_else(|| { + plan_error(format!( + "the native tree's input node at `{path}` does not produce samples" + )) + })?; + if schema.eval_grid.is_some() { + return Err(plan_error(format!( + "the native tree's input node at `{path}` consumes evaluated chunks, but this plan \ + feeds raw series frames" + ))); + } + Ok(schema.clone()) +} + +/// The root output of `tree`, which is what every column of this plan reads. +fn root_output(tree: &NativeTree) -> DfResult> { + match tree.root().output.samples() { + Some(schema) if schema.eval_grid.is_some() => Ok(schema.clone()), + Some(_) => Err(plan_error( + "the native tree's root produces raw samples, but this plan converts evaluated chunks", + )), + None => Err(plan_error( + "the native tree's root produces a scalar or aggregate state, which is not a query \ + value this plan can convert", + )), + } +} + +/// A DataFusion plan that executes one [`NativeTree`] over a raw-series frame. +/// +/// The tree, the label source and the output binding are the same for every partition; only the +/// run-time state is per execution, and it is built when a partition is executed, from that +/// execution's pool. The plan registers no metrics of its own: it exposes the per-node metrics +/// [`NativeExecutionState::try_new`] registers, as the nodes report them. +pub struct NativeTreeExec { + /// The prepared raw-series frame this tree reads, one chunk stream per partition. + input: Arc, + /// The bound tree, shared by every partition and never mutated. + tree: Arc, + /// Where the labels of every series come from. + label_source: RawLabelSource, + /// The ordering the child's frames declare. + ordering: SeriesOrdering, + /// The raw field layout the tree's input node consumes. + series_schema: Arc, + /// Which part of a root chunk every output column reads. + binding: Arc, + /// The per-node metrics of every execution of this plan. + metrics: ExecutionPlanMetricsSet, + properties: Arc, +} + +impl NativeTreeExec { + /// Builds a plan that drives `tree` over the raw-series frame `input` produces. + /// + /// Every check that can be made without running the child is made here: the tree must have + /// exactly one raw input and only the node kinds this plan executes, its root must produce an + /// evaluated vector, and every column of `output_schema` must be `time_column`, one of `tags` + /// or one of the root's fields. `tags` names the label set the caller wrote its query against; + /// it is not read from the tree, which carries no tag list. + pub fn try_new( + input: Arc, + tree: Arc, + label_source: RawLabelSource, + output_schema: SchemaRef, + time_column: String, + tags: Vec, + ) -> DfResult { + let series_schema = raw_source(&tree)?; + let root = root_output(&tree)?; + let binding = Arc::new(OutputBinding::try_new( + &root, + output_schema, + &time_column, + &tags, + )?); + Self::assemble( + input, + tree, + label_source, + series_schema, + binding, + ExecutionPlanMetricsSet::new(), + ) + } + + /// Assembles a plan from bindings that were already validated, deriving what the child + /// declares. + /// + /// The declared frame is validated against the tree's raw layout without executing the child: + /// `unpack_raw_series` decodes an empty batch of the declared schema, which is where a frame + /// that does not describe this tree's series is rejected. + fn assemble( + input: Arc, + tree: Arc, + label_source: RawLabelSource, + series_schema: Arc, + binding: Arc, + metrics: ExecutionPlanMetricsSet, + ) -> DfResult { + let declared = input.schema(); + let ordering = raw_series_frame_ordering(declared.as_ref())?; + unpack_raw_series( + Box::pin(RecordBatchStreamAdapter::new( + declared.clone(), + futures::stream::empty(), + )), + series_schema.clone(), + label_source.clone(), + ordering, + )?; + let properties = Arc::new(PlanProperties::new( + // A root chunk's ordering is not the child's: a window or a selection stamps its own + // grid onto what it produces, so nothing is carried over from the child. + EquivalenceProperties::new(binding.schema.clone()), + // Every partition of the child is kept, and this plan says nothing about how they + // relate: which series a partition holds is the caller's arrangement. A label set that + // recurs across two of them is not detected here -- see the module documentation -- so + // a caller keeps every series of one label set in one partition. + Partitioning::UnknownPartitioning(input.properties().partitioning.partition_count()), + EmissionType::Incremental, + Boundedness::Bounded, + )); + Ok(Self { + input, + tree, + label_source, + ordering, + series_schema, + binding, + metrics, + properties, + }) + } +} + +impl fmt::Debug for NativeTreeExec { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("NativeTreeExec") + .field("columns", &self.binding.columns) + .field("label_source", &self.label_source) + .finish() + } +} + +impl DisplayAs for NativeTreeExec { + fn fmt_as(&self, _t: DisplayFormatType, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "NativeTreeExec: partitions={}, columns={}", + self.properties.partitioning.partition_count(), + self.binding.schema.fields().len() + ) + } +} + +impl ExecutionPlan for NativeTreeExec { + fn name(&self) -> &str { + "NativeTreeExec" + } + + fn schema(&self) -> SchemaRef { + self.binding.schema.clone() + } + + fn properties(&self) -> &Arc { + &self.properties + } + + /// The metrics of every node of every partition, as `NativeExecutionState::try_new` registered + /// them. This plan registers nothing itself, so it only hands the set over. + fn metrics(&self) -> Option { + Some(self.metrics.clone_inner()) + } + + fn apply_expressions( + &self, + _f: &mut dyn FnMut(&Arc) -> DfResult, + ) -> DfResult { + Ok(TreeNodeRecursion::Continue) + } + + fn children(&self) -> Vec<&Arc> { + vec![&self.input] + } + + fn with_new_children( + self: Arc, + children: Vec>, + ) -> DfResult> { + let mut children = children.into_iter(); + let (Some(input), None) = (children.next(), children.next()) else { + return Err(plan_error( + "NativeTreeExec requires exactly one raw-frame child", + )); + }; + // Only the child changes: the tree, the label source and the output binding stay, and the + // partition count follows the new child. No run-time state is carried over, because none + // of it outlives one execution. + Ok(Arc::new(Self::assemble( + input, + self.tree.clone(), + self.label_source.clone(), + self.series_schema.clone(), + self.binding.clone(), + self.metrics.clone(), + )?)) + } + + fn reset_state(self: Arc) -> DfResult> { + // A reset execution starts from an empty metrics set and from the pool chosen when it is + // executed: no pool is cached here, and the child is not reset through this plan. + Ok(Arc::new(Self::assemble( + self.input.clone(), + self.tree.clone(), + self.label_source.clone(), + self.series_schema.clone(), + self.binding.clone(), + ExecutionPlanMetricsSet::new(), + )?)) + } + + fn execute( + &self, + partition: usize, + context: Arc, + ) -> DfResult { + let partitions = self.properties.partitioning.partition_count(); + if partition >= partitions { + return Err(DataFusionError::Execution(format!( + "the native tree plan has {partitions} partition(s), so partition {partition} has \ + no work" + ))); + } + let frame = self.input.execute(partition, Arc::clone(&context))?; + let state = NativeExecutionState::try_new( + &self.tree, + context.runtime_env().memory_pool.clone(), + &self.metrics, + partition, + )?; + let source = unpack_raw_series( + frame, + self.series_schema.clone(), + self.label_source.clone(), + self.ordering, + )?; + let source = materialize_raw_labels(source) + .map_err(|error| DataFusionError::External(Box::new(error)))?; + Ok(Box::pin(NativeTreeStream { + source: Some(source), + executor: Some(NativeExecutor::new(state)), + binding: self.binding.clone(), + closing: VecDeque::new(), + closed: false, + })) + } +} + +/// One partition's execution of the tree. +struct NativeTreeStream { + /// The raw chunks this execution feeds the tree, until the child ends. + source: Option, + /// The driver of this execution's tree, until it ends or fails. + executor: Option, + /// Which part of a root chunk every output column reads. + binding: Arc, + /// What closing the tree yielded, handed out one chunk at a time. + closing: VecDeque, + /// Whether the tree is closed, so nothing can settle any more. + closed: bool, +} + +impl NativeTreeStream { + /// Ends this execution, returning `error` once and nothing after it. + /// + /// The driver is dropped rather than stopped: dropping it returns every byte it reserved to + /// the pool, and an execution that failed must not be resumed. + fn fail(&mut self, error: DataFusionError) -> Poll>> { + self.source = None; + self.executor = None; + self.closing.clear(); + self.closed = true; + Poll::Ready(Some(Err(error))) + } + + /// The next raw chunk of the child, or `None` once it ended. + fn poll_source(&mut self, cx: &mut Context<'_>) -> Poll>> { + let Some(source) = self.source.as_mut() else { + return Poll::Ready(None); + }; + let chunk = match ready!(source.as_mut().poll_next(cx)) { + Some(Ok(chunk)) => chunk, + Some(Err(error)) => { + return Poll::Ready(Some(Err(DataFusionError::External(Box::new(error))))); + } + None => return Poll::Ready(None), + }; + // A chunk that carries a series' last flag is pushed like any other one: the flag is what + // lets the tree decide a series is over, so it is never dropped for carrying no row. + match self.executor.as_mut() { + Some(executor) => Poll::Ready(Some(executor.push_raw(chunk))), + None => Poll::Ready(None), + } + } + + /// Asks the root for the next chunk it settled. + fn next_from_tree(&mut self) -> DfResult> { + let next = match self.executor.as_mut() { + Some(executor) => executor.next_chunk(), + None => return Ok(None), + }; + match next? { + Some(chunk) => self.binding.convert(chunk), + // Nothing settled at this step; the tree either needs more input or has no chunk to + // report yet, which is not the same as having no input left. + None => Ok(None), + } + } +} + +impl Stream for NativeTreeStream { + type Item = DfResult; + + fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + for _ in 0..POLL_BUDGET { + // What the tree already settled is handed over first, so a chunk that is ready now + // never waits for another raw chunk to arrive. + let settled = match self.closing.pop_front() { + Some(chunk) => self.binding.convert(chunk), + None if !self.closed => self.next_from_tree(), + None => return Poll::Ready(None), + }; + match settled { + Ok(Some(batch)) => return Poll::Ready(Some(Ok(batch))), + // A closing chunk is a lifecycle signal rather than a row: it is handed to the + // tree, and dropped here only once it converted to no row at all. + Ok(None) => {} + Err(error) => return self.fail(error), + } + if self.source.is_none() { + // The child ended: closing the tree once is what reports a series that never + // closed, and what hands over what the tree held back until then. + let closed = match self.executor.as_mut() { + Some(executor) => executor.finish(), + None => return Poll::Ready(None), + }; + match closed { + Ok(chunks) => { + self.closing = chunks.into(); + self.closed = true; + } + Err(error) => return self.fail(error), + } + continue; + } + match self.poll_source(cx) { + Poll::Pending => return Poll::Pending, + Poll::Ready(Some(Err(error))) => return self.fail(error), + Poll::Ready(Some(Ok(()))) => {} + // The child's stream ended here rather than at a chunk boundary: the next step + // closes the tree. + Poll::Ready(None) => self.source = None, + } + } + cx.waker().wake_by_ref(); + Poll::Pending + } +} + +impl RecordBatchStream for NativeTreeStream { + fn schema(&self) -> SchemaRef { + self.binding.schema.clone() + } +} + +/// A rejected construction, which is a caller mistake rather than a query result. +fn plan_error(reason: impl Into) -> DataFusionError { + DataFusionError::Plan(reason.into()) +} diff --git a/src/promql/src/extension_plan/native_tree/tests.rs b/src/promql/src/extension_plan/native_tree/tests.rs new file mode 100644 index 00000000000..33ea1f67f99 --- /dev/null +++ b/src/promql/src/extension_plan/native_tree/tests.rs @@ -0,0 +1,1891 @@ +// Copyright 2023 Greptime Team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! What the shell has to get right: the tree it drives, the columns it binds, the loop it runs, +//! and the states it refuses. + +use std::collections::VecDeque; +use std::fmt; +use std::pin::Pin; +use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::task::{Context, Poll}; + +use api::v1::SemanticType; +use common_query::series_frame::raw_series_frame_schema_with_ordering; +use datafusion::arrow::array::{Float64Array, StringArray, UInt32Array}; +use datafusion::arrow::datatypes::{DataType, Field, Schema, TimeUnit}; +use datafusion::common::tree_node::TreeNodeRecursion; +use datafusion::error::DataFusionError; +use datafusion::execution::context::TaskContext; +use datafusion::execution::memory_pool::{GreedyMemoryPool, MemoryPool}; +use datafusion::execution::runtime_env::RuntimeEnvBuilder; +use datafusion::physical_plan::execution_plan::{Boundedness, EmissionType}; +use datafusion::physical_plan::metrics::{ExecutionPlanMetricsSet, MetricValue}; +use datafusion::physical_plan::{ + DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning, PhysicalExpr, PlanProperties, + SendableRecordBatchStream, +}; +use datatypes::arrow::buffer::{Buffer, ScalarBuffer}; +use datatypes::arrow::record_batch::RecordBatch; +use datatypes::data_type::ConcreteDataType; +use datatypes::schema::ColumnSchema; +use datatypes::series::frame::{RawLabelMode, RawSeriesChunkCursor, RawSeriesFrameBuilder}; +use datatypes::series::histogram::{ + BucketSideView, CounterResetHint, HistogramBatchBuilder, HistogramView, +}; +use datatypes::series::raw::{RawSeriesChunk, RawTimestamps}; +use datatypes::series::samples::{FieldSpec, Samples, SeriesFields, SeriesSchema}; +use datatypes::series::time::{EvalGrid, GridSpec}; +use datatypes::series::{Label, MetricName, SeriesOrdering}; +use futures::{Stream, StreamExt}; +use mito_codec::row_converter::SortField; +use mito_codec::row_converter::sparse::SparsePrimaryKeyCodec; +use store_api::codec::PrimaryKeyEncoding; +use store_api::metadata::{ColumnMetadata, RegionMetadata, RegionMetadataBuilder}; +use store_api::region_engine::{RawLabelSource, RawSeriesStream}; +use store_api::storage::RegionId; + +use super::*; +use crate::map::MathOp; +use crate::native::metrics::{NodeMetric, NodePath}; +use crate::native::plan::{ + BoundInput, BoundInstant, BoundMap, BoundScalar, BoundWindow, NativeNode, NativeType, +}; +use crate::range_eval::{RangeFunction, RangeWindow}; + +// --------------------------------------------------------------------------------------------- +// Trees +// --------------------------------------------------------------------------------------------- + +fn grid(start: i64, step: i64, len: u64) -> Arc { + Arc::new(EvalGrid::try_new(GridSpec::try_new(start, step, len).unwrap()).unwrap()) +} + +fn raw_schema(names: &[&str]) -> Arc { + Arc::new(SeriesSchema { + fields: names + .iter() + .map(|name| FieldSpec { + name: Arc::from(*name), + }) + .collect(), + eval_grid: None, + }) +} + +fn evaluated_schema(fields: &Arc, eval_grid: Arc) -> NativeType { + NativeType::Samples(Arc::new(SeriesSchema { + fields: Arc::clone(&fields.fields), + eval_grid: Some(eval_grid), + })) +} + +fn input_node(raw: Arc) -> NativeNode { + let output = NativeType::Samples(raw); + NativeNode { + expr: NativeExpr::Input(BoundInput { + output: output.clone(), + ordering: SeriesOrdering::KeyThenTime, + }), + output, + } +} + +/// An input node whose chunks are already evaluated, which a raw frame cannot feed. +fn evaluated_input_node(eval_grid: Arc) -> NativeNode { + let output = NativeType::Samples(Arc::new(SeriesSchema { + fields: Arc::from([FieldSpec { + name: Arc::from("v0"), + }]), + eval_grid: Some(eval_grid), + })); + NativeNode { + expr: NativeExpr::Input(BoundInput { + output: output.clone(), + ordering: SeriesOrdering::KeyThenTime, + }), + output, + } +} + +fn instant(inner: NativeNode, lookback_ms: i64, output_grid: Arc) -> NativeNode { + let raw = inner.output.samples().expect("a selection reads samples"); + let output = evaluated_schema(raw, output_grid.clone()); + NativeNode { + expr: NativeExpr::Instant { + input: Box::new(inner), + op: BoundInstant { + lookback_ms, + offset_ms: 0, + output_grid, + }, + }, + output, + } +} + +fn window( + inner: NativeNode, + function: RangeFunction, + range_ms: i64, + output_grid: Arc, +) -> NativeNode { + let raw = inner.output.samples().expect("a window reads samples"); + let output = evaluated_schema(raw, output_grid.clone()); + NativeNode { + expr: NativeExpr::Window { + input: Box::new(inner), + op: BoundWindow { + function, + window: RangeWindow { + range_ms, + offset_ms: 0, + anchor_ms: None, + }, + input_grid: None, + output_grid, + }, + }, + output, + } +} + +fn map(inner: NativeNode, ops: Vec) -> NativeNode { + let eval_grid = inner.output.grid().expect("a map needs a grid").clone(); + let raw = inner.output.samples().expect("a map reads samples"); + let output = evaluated_schema(raw, eval_grid); + NativeNode { + expr: NativeExpr::Map { + input: Box::new(inner), + op: BoundMap { ops: ops.into() }, + }, + output, + } +} + +// --------------------------------------------------------------------------------------------- +// Series and chunks +// --------------------------------------------------------------------------------------------- + +fn metric(tags: &[(&str, &str)]) -> MetricName { + MetricName::try_new( + Some("requests".into()), + tags.iter() + .map(|(name, value)| Label { + name: Arc::from(*name), + value: Arc::from(*value), + }) + .collect(), + ) + .unwrap() +} + +fn timestamps(values: &[i64]) -> RawTimestamps { + RawTimestamps::try_new(ScalarBuffer::from(values.to_vec())).unwrap() +} + +/// One chunk of the series `key` carrying `columns`, one column per field. +fn chunk( + schema: &SeriesSchema, + key: &[u8], + metric: Option, + time: &[i64], + columns: Vec>>, + is_last: bool, +) -> RawSeriesChunk { + let fields = SeriesFields::try_from_vec( + columns + .into_iter() + .map(|values| Samples::from_float(Float64Array::from(values))) + .collect(), + ) + .unwrap(); + RawSeriesChunk::try_new( + schema, + Buffer::from_slice_ref(key), + metric, + timestamps(time), + fields, + is_last, + ) + .unwrap() +} + +/// One one-field chunk of `key`. +fn one_field( + schema: &SeriesSchema, + key: &[u8], + metric: Option, + time: &[i64], + values: Vec>, + is_last: bool, +) -> RawSeriesChunk { + chunk(schema, key, metric, time, vec![values], is_last) +} + +/// One chunk whose one field holds a two-bucket native histogram at each timestamp. +fn histogram_chunk(schema: &SeriesSchema, key: &[u8], time: &[i64]) -> RawSeriesChunk { + let mut builder = HistogramBatchBuilder::new(); + let offsets = [0]; + let lengths = [2]; + let counts = vec![1.0; 2]; + let indices: UInt32Array = time + .iter() + .map(|_| { + Some( + builder + .push(HistogramView { + schema: 0, + zero_threshold: 0.0, + zero_count: 0.0, + count: 2.0, + sum: 2.0, + reset_hint: CounterResetHint::Unknown, + start_timestamp: None, + custom_values: &[], + positive: BucketSideView { + span_offsets: &offsets, + span_lengths: &lengths, + buckets: &counts, + }, + negative: BucketSideView { + span_offsets: &[], + span_lengths: &[], + buckets: &[], + }, + }) + .unwrap(), + ) + }) + .collect(); + let samples = Samples::try_from_histogram(indices, builder.finish().unwrap()).unwrap(); + RawSeriesChunk::try_new( + schema, + Buffer::from_slice_ref(key), + Some(metric(&[])), + timestamps(time), + SeriesFields::one(samples), + true, + ) + .unwrap() +} + +// --------------------------------------------------------------------------------------------- +// A raw-frame child with a controllable number of partitions +// --------------------------------------------------------------------------------------------- + +/// A child that publishes the frame batches a test hands it, and counts how they were read. +struct FrameChild { + schema: SchemaRef, + partitions: Vec>, + /// Whether the stream fails once its batches are exhausted, which is what an upstream failure + /// looks like from here. + fails: bool, + /// Whether the stream reports `Pending` once it has handed over its first batch, which is what + /// a source that is still arriving looks like from here. + stalls: bool, + polls: Arc, + drops: Arc, + properties: Arc, +} + +impl FrameChild { + fn new(schema: SchemaRef, partitions: Vec>) -> Arc { + Self::build(schema, partitions, false, false) + } + + /// A child whose stream reports a failure once it has published `partitions`. + fn failing(schema: SchemaRef, partitions: Vec>) -> Arc { + Self::build(schema, partitions, true, false) + } + + /// A child whose stream stalls once after its first batch, so a series stays open. + fn stalling(schema: SchemaRef, partitions: Vec>) -> Arc { + Self::build(schema, partitions, false, true) + } + + fn build( + schema: SchemaRef, + partitions: Vec>, + fails: bool, + stalls: bool, + ) -> Arc { + let properties = Arc::new(PlanProperties::new( + EquivalenceProperties::new(schema.clone()), + Partitioning::UnknownPartitioning(partitions.len()), + EmissionType::Incremental, + Boundedness::Bounded, + )); + Arc::new(Self { + schema, + partitions, + fails, + stalls, + polls: Arc::new(AtomicUsize::new(0)), + drops: Arc::new(AtomicUsize::new(0)), + properties, + }) + } + + /// How many batches the child's streams handed out, poll by poll. + fn polls(&self) -> usize { + self.polls.load(Ordering::SeqCst) + } + + fn drops(&self) -> usize { + self.drops.load(Ordering::SeqCst) + } +} + +impl fmt::Debug for FrameChild { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("FrameChild").finish() + } +} + +impl DisplayAs for FrameChild { + fn fmt_as(&self, _t: DisplayFormatType, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "FrameChild") + } +} + +struct FrameStream { + schema: SchemaRef, + batches: VecDeque, + /// Whether one failure follows the batches, and whether it was reported already. + fails: bool, + failed: bool, + /// Whether one `Pending` follows the first batch, and whether it was reported already. + stalls: bool, + stalled: bool, + yielded: usize, + polls: Arc, + drops: Arc, +} + +impl Stream for FrameStream { + type Item = DfResult; + + fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { + self.polls.fetch_add(1, Ordering::SeqCst); + if self.stalls && !self.stalled && self.yielded > 0 { + self.stalled = true; + return Poll::Pending; + } + if let Some(batch) = self.batches.pop_front() { + self.yielded += 1; + return Poll::Ready(Some(Ok(batch))); + } + if self.fails && !self.failed { + self.failed = true; + return Poll::Ready(Some(Err(DataFusionError::Execution( + "the frame child failed".to_string(), + )))); + } + Poll::Ready(None) + } +} + +impl Drop for FrameStream { + fn drop(&mut self) { + self.drops.fetch_add(1, Ordering::SeqCst); + } +} + +impl datafusion::physical_plan::RecordBatchStream for FrameStream { + fn schema(&self) -> SchemaRef { + self.schema.clone() + } +} + +impl ExecutionPlan for FrameChild { + fn name(&self) -> &str { + "FrameChild" + } + + fn schema(&self) -> SchemaRef { + self.schema.clone() + } + + fn properties(&self) -> &Arc { + &self.properties + } + + fn apply_expressions( + &self, + _f: &mut dyn FnMut(&Arc) -> DfResult, + ) -> DfResult { + Ok(TreeNodeRecursion::Continue) + } + + fn children(&self) -> Vec<&Arc> { + Vec::new() + } + + fn with_new_children( + self: Arc, + children: Vec>, + ) -> DfResult> { + assert!(children.is_empty(), "the frame child has no children"); + Ok(self) + } + + fn execute( + &self, + partition: usize, + _context: Arc, + ) -> DfResult { + Ok(Box::pin(FrameStream { + schema: self.schema.clone(), + batches: self.partitions[partition].clone().into(), + fails: self.fails, + failed: false, + stalls: self.stalls, + stalled: false, + yielded: 0, + polls: self.polls.clone(), + drops: self.drops.clone(), + })) + } +} + +/// The frame schema a child declares for `schema` under `mode`. +fn frame_schema(schema: &SeriesSchema, mode: RawLabelMode) -> SchemaRef { + raw_series_frame_schema_with_ordering(schema, mode, SeriesOrdering::KeyThenTime).unwrap() +} + +/// Packs `chunks` into the frame batches of one partition, declared under `declared`. +fn frame_batches( + schema: &SeriesSchema, + mode: RawLabelMode, + declared: &SchemaRef, + chunks: Vec, +) -> Vec { + let mut builder = RawSeriesFrameBuilder::try_new(schema, mode, 1024, 1 << 20).unwrap(); + let mut out = Vec::new(); + for chunk in chunks { + let mut cursor = RawSeriesChunkCursor::try_new(schema, mode, chunk).unwrap(); + while let Some(batch) = cursor.append_to(&mut builder).unwrap() { + out.push(batch.with_schema(declared.clone()).unwrap()); + } + } + if let Some(batch) = builder.flush().unwrap() { + out.push(batch.with_schema(declared.clone()).unwrap()); + } + out +} + +/// A one-partition child whose frames carry `chunks` of `schema` under explicit labels. +fn explicit_child(schema: &Arc, chunks: Vec) -> Arc { + let declared = frame_schema(schema, RawLabelMode::Explicit); + let batches = frame_batches(schema, RawLabelMode::Explicit, &declared, chunks); + FrameChild::new(declared, vec![batches]) +} + +fn context(pool: Arc) -> Arc { + let runtime = RuntimeEnvBuilder::new() + .with_memory_pool(pool) + .build() + .unwrap(); + Arc::new(TaskContext::default().with_runtime(Arc::new(runtime))) +} + +fn greedy(bytes: usize) -> Arc { + Arc::new(GreedyMemoryPool::new(bytes)) +} + +// --------------------------------------------------------------------------------------------- +// Output schemas and the plan +// --------------------------------------------------------------------------------------------- + +fn time_field() -> Field { + Field::new( + "time", + DataType::Timestamp(TimeUnit::Millisecond, None), + false, + ) +} + +fn value_field(name: &str) -> Field { + Field::new(name, DataType::Float64, true) +} + +fn tag_field(name: &str, nullable: bool) -> Field { + Field::new(name, DataType::Utf8, nullable) +} + +fn schema_of(fields: Vec) -> SchemaRef { + Arc::new(Schema::new(fields)) +} + +/// The schema of the trees below: the time column, one tag and one field. +fn default_schema() -> SchemaRef { + schema_of(vec![ + time_field(), + tag_field("job", true), + value_field("v0"), + ]) +} + +fn plan( + child: Arc, + tree: NativeTree, + label_source: RawLabelSource, + schema: SchemaRef, + tags: &[&str], +) -> DfResult { + NativeTreeExec::try_new( + child, + Arc::new(tree), + label_source, + schema, + "time".to_string(), + tags.iter().map(|tag| (*tag).to_string()).collect(), + ) +} + +// --------------------------------------------------------------------------------------------- +// Reading a stream +// --------------------------------------------------------------------------------------------- + +fn next_batch(stream: &mut SendableRecordBatchStream) -> Option> { + futures::executor::block_on(stream.next()) +} + +/// Polls the stream once, which is a step a stalled source can answer with `Pending`. +fn poll_once(stream: &mut SendableRecordBatchStream) -> Poll>> { + let waker = futures::task::noop_waker(); + let mut cx = Context::from_waker(&waker); + stream.as_mut().poll_next(&mut cx) +} + +fn rest(stream: &mut SendableRecordBatchStream) -> Vec> { + let mut out = Vec::new(); + while let Some(batch) = next_batch(stream) { + out.push(batch); + } + out +} + +fn batches(stream: &mut SendableRecordBatchStream) -> Vec { + rest(stream) + .into_iter() + .map(|batch| batch.expect("the stream must not fail here")) + .collect() +} + +fn first_batch(stream: &mut SendableRecordBatchStream) -> RecordBatch { + match next_batch(stream) { + Some(Ok(batch)) => batch, + other => panic!("expected one batch, got {other:?}"), + } +} + +fn values_of(batch: &RecordBatch, index: usize) -> Vec> { + batch + .column(index) + .as_any() + .downcast_ref::() + .unwrap() + .iter() + .collect() +} + +fn strings_of(batch: &RecordBatch, index: usize) -> Vec> { + batch + .column(index) + .as_any() + .downcast_ref::() + .unwrap() + .iter() + .map(|value| value.map(str::to_string)) + .collect() +} + +fn times_of(batch: &RecordBatch, index: usize) -> Vec { + batch + .column(index) + .as_any() + .downcast_ref::() + .unwrap() + .values() + .to_vec() +} + +/// How many metrics the plan reports, summed over every node and partition. +fn metrics_count(exec: &Arc) -> usize { + exec.metrics().map_or(0, |set| set.iter().count()) +} + +/// Replaces the child through `with_new_children`, which is what a plan rewrite calls. +/// +/// DataFusion marks the method deprecated in favour of `replace_children`, and its default +/// `replace_children` is exactly this call with `ChildrenPropertiesMode::Recompute`, so the plan +/// has to keep answering it. +#[expect(deprecated)] +fn replace_child( + plan: &Arc, + children: Vec>, +) -> DfResult> { + plan.clone().with_new_children(children) +} + +fn error_of(stream: &mut SendableRecordBatchStream) -> DataFusionError { + let error = match next_batch(stream) { + Some(Err(error)) => error, + other => panic!("expected one error, got {other:?}"), + }; + // Every failure is reported once, and the stream is terminal afterwards. + assert!( + next_batch(stream).is_none(), + "the failure was reported twice" + ); + assert!( + next_batch(stream).is_none(), + "the stream resumed after a failure" + ); + error +} + +// --------------------------------------------------------------------------------------------- +// End to end +// --------------------------------------------------------------------------------------------- + +/// `abs(instant[v0])` over one series: the tree evaluates and the shell converts every value. +#[test] +fn an_instant_selection_then_a_map_produces_the_trees_values() { + let raw = raw_schema(&["v0"]); + let tree = NativeTree::try_new(map( + instant(input_node(raw.clone()), 0, grid(0, 10_000, 3)), + vec![MathOp::Abs], + )) + .unwrap(); + let child = explicit_child( + &raw, + vec![one_field( + &raw, + b"a", + Some(metric(&[("job", "web-1")])), + &[0, 10_000, 20_000], + vec![Some(-1.0), Some(2.0), Some(-3.0)], + true, + )], + ); + let exec = plan( + child.clone(), + tree, + RawLabelSource::Explicit, + default_schema(), + &["job"], + ) + .unwrap(); + let mut stream = exec.execute(0, context(greedy(1 << 20))).unwrap(); + let batch = first_batch(&mut stream); + + assert_eq!(batch.num_rows(), 3); + assert_eq!(times_of(&batch, 0), vec![0, 10_000, 20_000]); + assert_eq!(strings_of(&batch, 1), vec![Some("web-1".to_string()); 3]); + assert_eq!(values_of(&batch, 2), vec![Some(1.0), Some(2.0), Some(3.0)]); + assert!( + rest(&mut stream).is_empty(), + "the stream must end after one series" + ); + assert_eq!(child.drops(), 1, "the child's stream must be dropped"); +} + +/// `abs(last_over_time(v0[1ms]))`: a window drives the same kernel path, on its own grid. +#[test] +fn a_window_then_a_map_produces_the_trees_values() { + let raw = raw_schema(&["v0"]); + let tree = NativeTree::try_new(map( + window( + input_node(raw.clone()), + RangeFunction::LastOverTime, + 1, + grid(0, 10_000, 3), + ), + vec![MathOp::Abs], + )) + .unwrap(); + let child = explicit_child( + &raw, + vec![one_field( + &raw, + b"a", + Some(metric(&[("job", "web-1")])), + &[0, 10_000, 20_000], + vec![Some(-4.0), Some(5.0), Some(-6.0)], + true, + )], + ); + let exec = plan( + child, + tree, + RawLabelSource::Explicit, + default_schema(), + &["job"], + ) + .unwrap(); + let mut stream = exec.execute(0, context(greedy(1 << 20))).unwrap(); + let batch = first_batch(&mut stream); + + assert_eq!(times_of(&batch, 0), vec![0, 10_000, 20_000]); + assert_eq!(values_of(&batch, 2), vec![Some(4.0), Some(5.0), Some(6.0)]); + assert!(rest(&mut stream).is_empty()); +} + +/// Columns are read by name, so the batch order is the schema's, and two fields stay paired with +/// their own columns. +#[test] +fn columns_are_reordered_and_each_field_reads_its_own_column() { + let raw = raw_schema(&["v0", "v1"]); + let tree = + NativeTree::try_new(instant(input_node(raw.clone()), 0, grid(0, 10_000, 2))).unwrap(); + let child = explicit_child( + &raw, + vec![chunk( + &raw, + b"a", + Some(metric(&[("job", "web-1")])), + &[0, 10_000], + vec![vec![Some(1.0), Some(2.0)], vec![Some(10.0), Some(20.0)]], + true, + )], + ); + let schema = schema_of(vec![ + tag_field("job", true), + value_field("v1"), + time_field(), + value_field("v0"), + ]); + let exec = plan( + child, + tree, + RawLabelSource::Explicit, + schema.clone(), + &["job"], + ) + .unwrap(); + assert_eq!( + exec.schema(), + schema, + "the plan must report the schema it was given" + ); + let mut stream = exec.execute(0, context(greedy(1 << 20))).unwrap(); + let batch = first_batch(&mut stream); + + assert_eq!(batch.schema(), schema); + assert_eq!( + strings_of(&batch, 0), + vec![Some("web-1".to_string()), Some("web-1".to_string())] + ); + assert_eq!(values_of(&batch, 1), vec![Some(10.0), Some(20.0)]); + assert_eq!(times_of(&batch, 2), vec![0, 10_000]); + assert_eq!(values_of(&batch, 3), vec![Some(1.0), Some(2.0)]); +} + +/// A series with holes on its grid keeps the timestamps it actually carries: the runs are not +/// filled out to the grid's every point. +#[test] +fn sparse_runs_keep_the_timestamps_they_carry() { + let raw = raw_schema(&["v0"]); + let tree = + NativeTree::try_new(instant(input_node(raw.clone()), 0, grid(0, 10_000, 6))).unwrap(); + let child = explicit_child( + &raw, + vec![one_field( + &raw, + b"a", + Some(metric(&[("job", "web-1")])), + &[10_000, 40_000], + vec![Some(2.0), Some(5.0)], + true, + )], + ); + let exec = plan( + child, + tree, + RawLabelSource::Explicit, + default_schema(), + &["job"], + ) + .unwrap(); + let mut stream = exec.execute(0, context(greedy(1 << 20))).unwrap(); + let batch = first_batch(&mut stream); + + assert_eq!(batch.num_rows(), 2, "the two holes must not become rows"); + assert_eq!(times_of(&batch, 0), vec![10_000, 40_000]); + assert_eq!(values_of(&batch, 2), vec![Some(2.0), Some(5.0)]); +} + +/// An ordinary NaN is a value; a position a field holds no sample at is a NULL; a row another +/// field fills is kept. +#[test] +fn nan_stays_a_value_and_a_missing_sample_becomes_null() { + let raw = raw_schema(&["v0", "v1"]); + let tree = + NativeTree::try_new(instant(input_node(raw.clone()), 0, grid(0, 10_000, 3))).unwrap(); + let child = explicit_child( + &raw, + vec![chunk( + &raw, + b"a", + Some(metric(&[("job", "web-1")])), + &[0, 10_000, 20_000], + vec![ + vec![Some(f64::NAN), Some(2.0), Some(3.0)], + vec![Some(1.0), None, Some(3.0)], + ], + true, + )], + ); + let schema = schema_of(vec![time_field(), value_field("v0"), value_field("v1")]); + let exec = plan(child, tree, RawLabelSource::Explicit, schema, &[]).unwrap(); + let mut stream = exec.execute(0, context(greedy(1 << 20))).unwrap(); + let batch = first_batch(&mut stream); + + let nan = values_of(&batch, 1); + assert!( + nan[0].unwrap().is_nan(), + "an ordinary NaN must stay a value" + ); + assert_eq!(nan[1], Some(2.0)); + assert_eq!( + values_of(&batch, 2), + vec![Some(1.0), None, Some(3.0)], + "a field with no sample at a row reports NULL and keeps the row" + ); +} + +/// A series that produced no row still closes, and its closing chunk becomes no batch at all. +#[test] +fn a_series_with_no_row_closes_without_a_batch() { + let raw = raw_schema(&["v0"]); + let tree = + NativeTree::try_new(instant(input_node(raw.clone()), 0, grid(0, 10_000, 3))).unwrap(); + let child = explicit_child( + &raw, + vec![ + one_field( + &raw, + b"a", + Some(metric(&[("job", "web-1")])), + &[0], + vec![Some(7.0)], + true, + ), + // Every sample of `b` lies past the grid, so its selection decides no row. + one_field( + &raw, + b"b", + Some(metric(&[("job", "web-2")])), + &[100_000], + vec![Some(9.0)], + true, + ), + ], + ); + let exec = plan( + child, + tree, + RawLabelSource::Explicit, + default_schema(), + &["job"], + ) + .unwrap(); + let mut stream = exec.execute(0, context(greedy(1 << 20))).unwrap(); + let out = batches(&mut stream); + + assert_eq!( + out.len(), + 1, + "the empty closing chunk must not become a batch" + ); + assert_eq!(times_of(&out[0], 0), vec![0]); + assert_eq!(values_of(&out[0], 2), vec![Some(7.0)]); + assert_eq!( + strings_of(&out[0], 1), + vec![Some("web-1".to_string())], + "the batch must belong to the series that had a row" + ); +} + +/// A tag the series does not carry is NULL in a nullable column and the empty string otherwise, +/// while a tag it carries is its value. +#[test] +fn a_missing_tag_is_null_or_empty_as_the_column_declares() { + let raw = raw_schema(&["v0"]); + let tree = + NativeTree::try_new(instant(input_node(raw.clone()), 0, grid(0, 10_000, 1))).unwrap(); + let child = explicit_child( + &raw, + vec![one_field( + &raw, + b"a", + Some(metric(&[("job", "web-1")])), + &[0], + vec![Some(1.0)], + true, + )], + ); + let schema = schema_of(vec![ + time_field(), + tag_field("job", true), + tag_field("zone", true), + tag_field("host", false), + value_field("v0"), + ]); + let exec = plan( + child, + tree, + RawLabelSource::Explicit, + schema, + &["job", "zone", "host"], + ) + .unwrap(); + let mut stream = exec.execute(0, context(greedy(1 << 20))).unwrap(); + let batch = first_batch(&mut stream); + + assert_eq!(strings_of(&batch, 1), vec![Some("web-1".to_string())]); + assert_eq!(strings_of(&batch, 2), vec![None], "a nullable tag is NULL"); + assert_eq!( + strings_of(&batch, 3), + vec![Some(String::new())], + "a non-nullable tag is the empty string" + ); +} + +// --------------------------------------------------------------------------------------------- +// Label sources +// --------------------------------------------------------------------------------------------- + +/// The metadata and label binding of a primary-key source whose one tag is `job`. +fn primary_key_source(metric_name: &str) -> (RawLabelSource, Arc) { + let mut builder = RegionMetadataBuilder::new(RegionId::new(7, 1)); + builder.push_column_metadata(ColumnMetadata { + column_schema: ColumnSchema::new( + "ts", + ConcreteDataType::timestamp_millisecond_datatype(), + false, + ), + semantic_type: SemanticType::Timestamp, + column_id: 0, + }); + builder.push_column_metadata(ColumnMetadata { + column_schema: ColumnSchema::new("job", ConcreteDataType::string_datatype(), true), + semantic_type: SemanticType::Tag, + column_id: 1, + }); + builder.push_column_metadata(ColumnMetadata { + column_schema: ColumnSchema::new("v0", ConcreteDataType::float64_datatype(), true), + semantic_type: SemanticType::Field, + column_id: 2, + }); + builder.primary_key(vec![1]); + builder.primary_key_encoding(PrimaryKeyEncoding::Sparse); + let metadata = Arc::new(builder.build().unwrap()); + let source = RawLabelSource::PrimaryKey { + metric_name: Arc::from(metric_name), + physical_metadata: Arc::clone(&metadata), + logical_tags: Arc::from([store_api::region_engine::BoundTag { + column_id: 1, + name: Arc::from("job"), + }]), + }; + (source, metadata) +} + +/// The sparse primary key of one series whose `job` tag is `job`. +fn sparse_key(job: &str) -> Vec { + let codec = SparsePrimaryKeyCodec::with_fields(vec![( + 1, + SortField::new(ConcreteDataType::string_datatype()), + )]); + let mut key = Vec::new(); + codec.encode_internal(42, 9, &mut key).unwrap(); + codec + .encode_raw_tag_value(std::iter::once((1, job.as_bytes())), &mut key) + .unwrap(); + key +} + +/// A primary-key source has its labels decoded on the way in, so the tag column reads them. +#[test] +fn primary_key_labels_are_materialized_before_the_tree_sees_them() { + let raw = raw_schema(&["v0"]); + let tree = + NativeTree::try_new(instant(input_node(raw.clone()), 0, grid(0, 10_000, 1))).unwrap(); + let (label_source, _metadata) = primary_key_source("requests"); + let declared = frame_schema(&raw, RawLabelMode::Key); + let batches = frame_batches( + &raw, + RawLabelMode::Key, + &declared, + vec![one_field( + &raw, + &sparse_key("web-1"), + None, + &[0], + vec![Some(3.0)], + true, + )], + ); + let child = FrameChild::new(declared, vec![batches]); + let exec = plan(child, tree, label_source, default_schema(), &["job"]).unwrap(); + let mut stream = exec.execute(0, context(greedy(1 << 20))).unwrap(); + let batch = first_batch(&mut stream); + + assert_eq!( + strings_of(&batch, 1), + vec![Some("web-1".to_string())], + "the label must be decoded from the primary key" + ); + assert_eq!(values_of(&batch, 2), vec![Some(3.0)]); +} + +// --------------------------------------------------------------------------------------------- +// Partitions +// --------------------------------------------------------------------------------------------- + +/// Two partitions are each executed on their own, an out-of-range partition is refused, and the +/// partition count follows a replaced child. +#[test] +fn partitions_are_executed_separately_and_out_of_range_is_an_error() { + let raw = raw_schema(&["v0"]); + let tree = + NativeTree::try_new(instant(input_node(raw.clone()), 0, grid(0, 10_000, 1))).unwrap(); + let declared = frame_schema(&raw, RawLabelMode::Explicit); + let partition = |declared: &SchemaRef, key: &[u8], job: &str, value: f64| { + frame_batches( + &raw, + RawLabelMode::Explicit, + declared, + vec![one_field( + &raw, + key, + Some(metric(&[("job", job)])), + &[0], + vec![Some(value)], + true, + )], + ) + }; + let child = FrameChild::new( + declared.clone(), + vec![ + partition(&declared, b"a", "web-1", 1.0), + partition(&declared, b"b", "web-2", 2.0), + ], + ); + let exec = Arc::new( + plan( + child.clone(), + tree, + RawLabelSource::Explicit, + default_schema(), + &["job"], + ) + .unwrap(), + ); + assert_eq!( + exec.properties().partitioning.partition_count(), + 2, + "the child's two partitions must both survive" + ); + assert!( + matches!( + exec.properties().partitioning, + Partitioning::UnknownPartitioning(2) + ), + "the partitioning must be unknown with the child's count, not a scheme it cannot promise" + ); + assert!( + exec.properties().output_ordering().is_none(), + "no ordering is inherited from the child" + ); + + for (partition, value, job) in [(0, 1.0, "web-1"), (1, 2.0, "web-2")] { + let mut stream = exec + .execute(partition, context(greedy(1 << 20))) + .unwrap_or_else(|error| panic!("partition {partition} must run: {error}")); + let batch = first_batch(&mut stream); + assert_eq!(values_of(&batch, 2), vec![Some(value)]); + assert_eq!(strings_of(&batch, 1), vec![Some(job.to_string())]); + assert!(rest(&mut stream).is_empty()); + } + let error = match exec.execute(2, context(greedy(1 << 20))) { + Err(error) => error, + Ok(_) => panic!("partition 2 does not exist"), + }; + assert!( + error.to_string().contains("2 partition(s)"), + "unexpected error: {error}" + ); + + let wider = FrameChild::new( + declared.clone(), + vec![ + partition(&declared, b"a", "web-1", 1.0), + partition(&declared, b"b", "web-2", 2.0), + vec![], + ], + ); + let replaced = + replace_child(&exec, vec![wider]).expect("a child with the same frames may be put in"); + assert_eq!( + replaced.properties().partitioning.partition_count(), + 3, + "the partition count must follow the replaced child" + ); + assert_eq!(replaced.schema(), exec.schema(), "the binding must survive"); + + let error = replace_child(&exec, vec![]).expect_err("a plan without a child is invalid"); + assert!( + error.to_string().contains("exactly one"), + "unexpected error: {error}" + ); +} + +// --------------------------------------------------------------------------------------------- +// Reset and metrics +// --------------------------------------------------------------------------------------------- + +/// A reset plan starts from an empty metrics set and takes its pool from the next execution, not +/// from the one before it. +#[test] +fn reset_state_takes_the_next_pool_and_starts_from_empty_metrics() { + let raw = raw_schema(&["v0"]); + let tree = + NativeTree::try_new(instant(input_node(raw.clone()), 0, grid(0, 10_000, 1))).unwrap(); + let child = explicit_child( + &raw, + vec![one_field( + &raw, + b"a", + Some(metric(&[("job", "web-1")])), + &[0], + vec![Some(4.0)], + true, + )], + ); + let exec = Arc::new( + plan( + child, + tree, + RawLabelSource::Explicit, + default_schema(), + &["job"], + ) + .unwrap(), + ) as Arc; + assert_eq!(metrics_count(&exec), 0); + + // A pool that cannot hold the chunk the tree reserves fails at the reservation. + let mut close = exec + .execute(0, context(greedy(1))) + .expect("the state is built before anything is reserved"); + let error = error_of(&mut close); + assert!( + matches!(error, DataFusionError::ResourcesExhausted(_)), + "unexpected error: {error}" + ); + + let reset = exec.clone().reset_state().unwrap(); + assert_eq!( + metrics_count(&reset), + 0, + "a reset plan must not keep the metrics of the execution it replaced" + ); + assert_eq!( + reset.properties().partitioning.partition_count(), + 1, + "a reset plan keeps the partition count" + ); + let mut stream = reset.execute(0, context(greedy(1 << 20))).unwrap(); + let batch = first_batch(&mut stream); + assert_eq!(values_of(&batch, 2), vec![Some(4.0)]); + assert!(rest(&mut stream).is_empty()); + assert!( + metrics_count(&reset) > 0, + "the reset plan must register the nodes again on execution" + ); +} + +/// The metrics the plan reports are the per-node ones the runtime registered, per partition. +#[test] +fn the_plan_exposes_the_metrics_the_runtime_registered() { + let raw = raw_schema(&["v0"]); + let tree = NativeTree::try_new(map( + instant(input_node(raw.clone()), 0, grid(0, 10_000, 1)), + vec![MathOp::Abs], + )) + .unwrap(); + let child = explicit_child( + &raw, + vec![one_field( + &raw, + b"a", + Some(metric(&[("job", "web-1")])), + &[0], + vec![Some(-4.0)], + true, + )], + ); + let exec = Arc::new( + plan( + child, + tree, + RawLabelSource::Explicit, + default_schema(), + &["job"], + ) + .unwrap(), + ); + let mut stream = exec.execute(0, context(greedy(1 << 20))).unwrap(); + batches(&mut stream); + + let set = exec.metrics().expect("the plan reports metrics"); + let source = NodePath::root().input().input(); + let name = source.metric_name(NodeMetric::INPUT_BATCHES); + match set.sum_by_name(&name) { + Some(MetricValue::Count { count, .. }) => { + assert!(count.value() > 0, "the source node saw no batch"); + } + other => panic!("no count named {name}: {other:?}"), + } +} + +// --------------------------------------------------------------------------------------------- +// Refusals +// --------------------------------------------------------------------------------------------- + +/// Every column of the output must be exactly one of the three roles, of the declared type. +#[test] +fn a_schema_that_does_not_match_the_root_output_is_refused_at_construction() { + let raw = raw_schema(&["v0"]); + let tree = + || NativeTree::try_new(instant(input_node(raw.clone()), 0, grid(0, 10_000, 1))).unwrap(); + let child = || { + explicit_child( + &raw, + vec![one_field( + &raw, + b"a", + Some(metric(&[("job", "web-1")])), + &[0], + vec![Some(1.0)], + true, + )], + ) as Arc + }; + let cases: Vec<(&str, SchemaRef, &str, &[&str], &str)> = vec![ + ( + "two columns of one name", + schema_of(vec![ + time_field(), + tag_field("job", true), + value_field("v0"), + value_field("v0"), + ]), + "time", + &["job"], + "twice", + ), + ( + "a column no role claims", + schema_of(vec![ + time_field(), + tag_field("job", true), + value_field("v0"), + value_field("mystery"), + ]), + "time", + &["job"], + "is not the time column, one of the tags, or a field", + ), + ( + "no column of the time column's name", + schema_of(vec![tag_field("job", true), value_field("v0")]), + "when", + &["job"], + "has no column `when` for the time column", + ), + ( + "no column of a tag's name", + schema_of(vec![time_field(), value_field("v0")]), + "time", + &["job"], + "has no column `job` for a tag", + ), + ( + "no column of a field's name", + schema_of(vec![time_field(), tag_field("job", true)]), + "time", + &["job"], + "has no column `v0` for a field", + ), + ( + "the time column is also a field", + schema_of(vec![ + Field::new( + "v0", + DataType::Timestamp(TimeUnit::Millisecond, None), + false, + ), + tag_field("job", true), + ]), + "v0", + &["job"], + "claimed as both", + ), + ( + "a tag is also the time column", + schema_of(vec![ + time_field(), + tag_field("job", true), + value_field("v0"), + ]), + "job", + &["job"], + "claimed as both", + ), + ( + "the time column is not a timestamp", + schema_of(vec![ + value_field("time"), + tag_field("job", true), + value_field("v0"), + ]), + "time", + &["job"], + "a Timestamp(Millisecond, None)", + ), + ( + "the time column is not in milliseconds", + schema_of(vec![ + Field::new( + "time", + DataType::Timestamp(TimeUnit::Microsecond, None), + false, + ), + tag_field("job", true), + value_field("v0"), + ]), + "time", + &["job"], + "a Timestamp(Millisecond, None)", + ), + ( + "a field column that cannot be null", + schema_of(vec![ + time_field(), + tag_field("job", true), + Field::new("v0", DataType::Float64, false), + ]), + "time", + &["job"], + "a nullable Float64", + ), + ( + "a field column of another type", + schema_of(vec![ + time_field(), + tag_field("job", true), + Field::new("v0", DataType::Int64, true), + ]), + "time", + &["job"], + "a nullable Float64", + ), + ( + "a tag column of another type", + schema_of(vec![time_field(), value_field("job"), value_field("v0")]), + "time", + &["job"], + "a Utf8", + ), + ]; + for (case, schema, time_column, tags, expected) in cases { + let error = NativeTreeExec::try_new( + child(), + Arc::new(tree()), + RawLabelSource::Explicit, + schema, + time_column.to_string(), + tags.iter().map(|tag| (*tag).to_string()).collect(), + ) + .expect_err("this schema must be refused"); + assert!( + error.to_string().contains(expected), + "{case}: expected a message holding `{expected}`, got {error}" + ); + } +} + +/// A tree this plan does not execute is refused, and the message says which node it met. +#[test] +fn an_unsupported_tree_is_refused() { + let raw = raw_schema(&["v0"]); + let cases: Vec<(&str, NativeTree, &str)> = vec![ + ( + "a scalar root", + NativeTree::try_new(NativeNode { + expr: NativeExpr::Scalar(BoundScalar { value: 1.0 }), + output: NativeType::Scalar(grid(0, 10_000, 1)), + }) + .unwrap(), + "is a scalar", + ), + ( + "a raw root", + NativeTree::try_new(input_node(raw.clone())).unwrap(), + "root produces raw samples", + ), + ( + // A map consumes an evaluated vector, so its input node is one a raw frame cannot feed. + "an evaluated input", + NativeTree::try_new(map( + evaluated_input_node(grid(0, 10_000, 1)), + vec![MathOp::Abs], + )) + .unwrap(), + "consumes evaluated chunks", + ), + ]; + for (case, tree, expected) in cases { + let child = explicit_child( + &raw, + vec![one_field( + &raw, + b"a", + Some(metric(&[("job", "web-1")])), + &[0], + vec![Some(1.0)], + true, + )], + ) as Arc; + let error = NativeTreeExec::try_new( + child, + Arc::new(tree), + RawLabelSource::Explicit, + default_schema(), + "time".to_string(), + vec!["job".to_string()], + ) + .expect_err("this tree must be refused"); + assert!( + error.to_string().contains(expected), + "{case}: expected a message holding `{expected}`, got {error}" + ); + } +} + +/// A child whose declared frame is not this tree's raw layout is refused before it runs. +#[test] +fn a_child_whose_frame_does_not_match_the_tree_is_refused() { + let raw = raw_schema(&["v0"]); + let other = raw_schema(&["other"]); + let tree = + NativeTree::try_new(instant(input_node(raw.clone()), 0, grid(0, 10_000, 1))).unwrap(); + let declared = frame_schema(&other, RawLabelMode::Explicit); + let batches = frame_batches( + &other, + RawLabelMode::Explicit, + &declared, + vec![one_field( + &other, + b"a", + Some(metric(&[("job", "web-1")])), + &[0], + vec![Some(1.0)], + true, + )], + ); + let child = FrameChild::new(declared, vec![batches]); + let error = plan( + child.clone(), + tree, + RawLabelSource::Explicit, + default_schema(), + &["job"], + ) + .expect_err("a frame of another layout must be refused"); + assert!(!error.to_string().is_empty()); + + // The same child is refused when it replaces one the tree did match. + let matching = explicit_child(&raw, Vec::new()); + let exec = Arc::new( + plan( + matching, + NativeTree::try_new(instant(input_node(raw.clone()), 0, grid(0, 10_000, 1))).unwrap(), + RawLabelSource::Explicit, + default_schema(), + &["job"], + ) + .unwrap(), + ); + let error = replace_child(&exec, vec![child]) + .expect_err("the replaced child must still describe the tree"); + assert!(!error.to_string().is_empty()); +} + +// --------------------------------------------------------------------------------------------- +// Failures on the way out +// --------------------------------------------------------------------------------------------- + +/// A histogram is not a float: converting one is an explicit error, not a NULL and not a dropped +/// row. +#[test] +fn a_histogram_sample_is_an_error() { + let raw = raw_schema(&["v0"]); + let tree = NativeTree::try_new(window( + input_node(raw.clone()), + RangeFunction::LastOverTime, + 1, + grid(0, 10_000, 1), + )) + .unwrap(); + let child = explicit_child(&raw, vec![histogram_chunk(&raw, b"a", &[0])]); + let schema = schema_of(vec![time_field(), value_field("v0")]); + let exec = plan(child, tree, RawLabelSource::Explicit, schema, &[]).unwrap(); + let mut stream = exec.execute(0, context(greedy(1 << 20))).unwrap(); + let error = error_of(&mut stream); + assert!( + error.to_string().contains("native histogram"), + "unexpected error: {error}" + ); +} + +/// A child that fails is reported once, and what it already handed over does not make the stream +/// a success. +#[test] +fn a_child_failure_is_reported_once_after_its_prefix() { + let raw = raw_schema(&["v0"]); + let tree = + NativeTree::try_new(instant(input_node(raw.clone()), 0, grid(0, 10_000, 1))).unwrap(); + let declared = frame_schema(&raw, RawLabelMode::Explicit); + let batches = frame_batches( + &raw, + RawLabelMode::Explicit, + &declared, + vec![one_field( + &raw, + b"a", + Some(metric(&[("job", "web-1")])), + &[0], + vec![Some(1.0)], + true, + )], + ); + let child = FrameChild::failing(declared, vec![batches]); + let exec = plan( + child, + tree, + RawLabelSource::Explicit, + default_schema(), + &["job"], + ) + .unwrap(); + let mut stream = exec.execute(0, context(greedy(1 << 20))).unwrap(); + // The prefix the child did deliver is produced, and the failure follows it once. + assert_eq!(values_of(&first_batch(&mut stream), 2), vec![Some(1.0)]); + let error = error_of(&mut stream); + assert!( + error.to_string().contains("the frame child failed"), + "unexpected error: {error}" + ); +} + +/// A frame that ends inside a series is refused by the boundary, so the prefix is not a success. +#[test] +fn a_frame_that_ends_inside_a_series_is_refused() { + let raw = raw_schema(&["v0"]); + let tree = + NativeTree::try_new(instant(input_node(raw.clone()), 0, grid(0, 10_000, 1))).unwrap(); + // The chunk does not say `is_last`, and no chunk after it says so either. + let child = explicit_child( + &raw, + vec![one_field( + &raw, + b"a", + Some(metric(&[("job", "web-1")])), + &[0], + vec![Some(1.0)], + false, + )], + ); + let exec = plan( + child, + tree, + RawLabelSource::Explicit, + default_schema(), + &["job"], + ) + .unwrap(); + let mut stream = exec.execute(0, context(greedy(1 << 20))).unwrap(); + let error = error_of(&mut stream); + assert!( + error.to_string().contains("before key was final"), + "unexpected error: {error}" + ); +} + +/// A source that ends inside a series is a truncation error from the runtime itself, reported once. +#[test] +fn a_source_that_ends_inside_a_series_is_a_truncation_error() { + let raw = raw_schema(&["v0"]); + let tree = Arc::new( + NativeTree::try_new(instant(input_node(raw.clone()), 0, grid(0, 10_000, 1))).unwrap(), + ); + let set = ExecutionPlanMetricsSet::new(); + let state = NativeExecutionState::try_new(&tree, greedy(1 << 20), &set, 0).unwrap(); + let binding = Arc::new( + OutputBinding::try_new( + tree.root().output.samples().unwrap(), + default_schema(), + "time", + &["job".to_string()], + ) + .unwrap(), + ); + let source = ChunkSource { + schema: raw.clone(), + labels: RawLabelSource::Explicit, + chunks: vec![one_field( + &raw, + b"a", + Some(metric(&[("job", "web-1")])), + &[0], + vec![Some(1.0)], + false, + )] + .into(), + }; + let mut stream: SendableRecordBatchStream = Box::pin(NativeTreeStream { + source: Some(Box::pin(source)), + executor: Some(NativeExecutor::new(state)), + binding, + closing: VecDeque::new(), + closed: false, + }); + let error = error_of(&mut stream); + assert!( + error + .to_string() + .contains("before the last chunk of a series"), + "unexpected error: {error}" + ); +} + +/// A raw-series source a test writes by hand, which the shell can be given directly. +struct ChunkSource { + schema: Arc, + labels: RawLabelSource, + chunks: VecDeque, +} + +impl Stream for ChunkSource { + type Item = std::result::Result; + + fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { + Poll::Ready(self.chunks.pop_front().map(Ok)) + } +} + +impl RawSeriesStream for ChunkSource { + fn schema(&self) -> &Arc { + &self.schema + } + + fn label_source(&self) -> &RawLabelSource { + &self.labels + } + + fn ordering(&self) -> SeriesOrdering { + SeriesOrdering::Contiguous + } +} + +// --------------------------------------------------------------------------------------------- +// The loop +// --------------------------------------------------------------------------------------------- + +/// A settled batch is handed over without draining the child first, and without waiting for it. +/// +/// The first half reads a child that has two batches: handing over the first series must cost one +/// poll, so an implementation that reads the child to its end before answering is caught. The +/// second half reads a child that stalls after its single batch: the series that batch closes is +/// still handed over, which is what "answer before reading on" means for a source that never ends. +#[test] +fn a_settled_batch_does_not_wait_for_the_child() { + let raw = raw_schema(&["v0"]); + let tree = + NativeTree::try_new(instant(input_node(raw.clone()), 0, grid(0, 10_000, 1))).unwrap(); + let declared = frame_schema(&raw, RawLabelMode::Explicit); + let batch = |declared: &SchemaRef, key: &[u8], job: &str| { + frame_batches( + &raw, + RawLabelMode::Explicit, + declared, + vec![one_field( + &raw, + key, + Some(metric(&[("job", job)])), + &[0], + vec![Some(1.0)], + true, + )], + ) + }; + // One partition, two frame batches: the second series is only reachable after the first. + let child = FrameChild::new( + declared.clone(), + vec![ + [ + batch(&declared, b"a", "web-1"), + batch(&declared, b"b", "web-2"), + ] + .concat(), + ], + ); + let exec = plan( + child.clone(), + tree, + RawLabelSource::Explicit, + default_schema(), + &["job"], + ) + .unwrap(); + let mut stream = exec.execute(0, context(greedy(1 << 20))).unwrap(); + + assert_eq!( + strings_of(&first_batch(&mut stream), 1), + vec![Some("web-1".to_string())] + ); + assert_eq!( + child.polls(), + 1, + "the second raw batch was read before the first series was handed over" + ); + assert_eq!( + strings_of(&first_batch(&mut stream), 1), + vec![Some("web-2".to_string())] + ); + assert_eq!( + child.polls(), + 2, + "the second series must have been read for the second batch" + ); + assert!(rest(&mut stream).is_empty()); + + // A child that stalls after one batch still has its series answered: the tree settled the + // chunk, so the batch is handed over rather than waited on. + let stalling = FrameChild::stalling(declared.clone(), vec![batch(&declared, b"a", "web-1")]); + let exec = plan( + stalling.clone(), + NativeTree::try_new(instant(input_node(raw.clone()), 0, grid(0, 10_000, 1))).unwrap(), + RawLabelSource::Explicit, + default_schema(), + &["job"], + ) + .unwrap(); + let mut stream = exec.execute(0, context(greedy(1 << 20))).unwrap(); + // One poll, not a blocked read: an implementation that polls the child before answering would + // report `Pending` here even though the tree has a batch ready. + match poll_once(&mut stream) { + Poll::Ready(Some(Ok(batch))) => assert_eq!( + strings_of(&batch, 1), + vec![Some("web-1".to_string())], + "the batch must be handed over while the child is still open" + ), + other => panic!("the settled batch must be handed over at once, got {other:?}"), + } + assert!( + poll_once(&mut stream).is_pending(), + "the child stalled, so there is nothing more to hand over yet" + ); +} + +/// Dropping the stream returns everything the execution reserved, and releases the child. +/// +/// The series is still open when the stream is dropped -- the child has stalled between its two +/// chunks -- so what the pool holds belongs to a series nothing will ever close. +#[test] +fn dropping_the_stream_releases_the_pool() { + let raw = raw_schema(&["v0"]); + let tree = + NativeTree::try_new(instant(input_node(raw.clone()), 0, grid(0, 10_000, 2))).unwrap(); + let declared = frame_schema(&raw, RawLabelMode::Explicit); + // One frame batch per chunk, so the child can stall between the two halves of the series. + let opening = frame_batches( + &raw, + RawLabelMode::Explicit, + &declared, + vec![one_field( + &raw, + b"a", + Some(metric(&[("job", "web-1")])), + &[0], + vec![Some(1.0)], + false, + )], + ); + let closing = frame_batches( + &raw, + RawLabelMode::Explicit, + &declared, + vec![one_field( + &raw, + b"a", + Some(metric(&[("job", "web-1")])), + &[10_000], + vec![Some(2.0)], + true, + )], + ); + let child = FrameChild::stalling(declared, vec![[opening, closing].concat()]); + let exec = plan( + child.clone(), + tree, + RawLabelSource::Explicit, + default_schema(), + &["job"], + ) + .unwrap(); + let pool = greedy(1 << 20); + let mut stream = exec.execute(0, context(pool.clone())).unwrap(); + + assert!( + poll_once(&mut stream).is_pending(), + "an open series produces no batch, and the child has stalled" + ); + assert!( + pool.reserved() > 0, + "the execution must hold the open series it reserved" + ); + assert_eq!(child.drops(), 0, "the child's stream is still being read"); + + drop(stream); + assert_eq!( + pool.reserved(), + 0, + "dropping the stream must return the pool" + ); + assert_eq!( + child.drops(), + 1, + "dropping the stream must drop the child's stream" + ); +}