diff --git a/src/promql/src/lib.rs b/src/promql/src/lib.rs index 4ba3a66b96a..c9ee89ff62c 100644 --- a/src/promql/src/lib.rs +++ b/src/promql/src/lib.rs @@ -27,6 +27,7 @@ pub mod map; pub mod map_label; pub mod map_time; mod metrics; +pub mod native; pub mod range_array; pub mod range_eval; pub mod select; diff --git a/src/promql/src/native/batch.rs b/src/promql/src/native/batch.rs new file mode 100644 index 00000000000..f3982a0115e --- /dev/null +++ b/src/promql/src/native/batch.rs @@ -0,0 +1,34 @@ +// 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. + +//! The batches native nodes hand to each other. +//! +//! These are ordinary Rust values passed by call, not DataFusion streams and not encoded messages: +//! the Arrow arrays, buffers and shared schemas inside them are reused as they are. +//! +//! Two rules make a chunk self-describing: +//! +//! - A grid position is absolute on the chunk's grid. Batch-local row numbers never stand in for +//! one, and an inner grid is never addressed with an outer grid's index. +//! - A series closes exactly once, on the chunk that carries [`EvaluatedChunk::is_last`]. A closed +//! prefix still has to be followed by a final chunk even when that chunk holds no values, so a +//! consumer can tell "nothing more here" from "no more input". + +//! +//! The chunk type itself is [`crate::evaluated::EvaluatedChunk`]: one chunk protocol serves both the +//! evaluator that produces chunks and the native nodes that consume them. Defining a second one +//! here would let the two drift apart, and a chunk produced by the evaluator could not be handed to +//! a node without translation. + +pub use crate::evaluated::EvaluatedChunk; diff --git a/src/promql/src/native/display.rs b/src/promql/src/native/display.rs new file mode 100644 index 00000000000..7e0e82c2b5e --- /dev/null +++ b/src/promql/src/native/display.rs @@ -0,0 +1,281 @@ +// 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. + +//! One line per node of a [`NativeTree`]. +//! +//! The position a node is shown at is derived by [`metrics::walk`], the same traversal that names +//! the node's metrics, so a node cannot be shown at one place and measured at another. 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. + +use std::fmt::{self, Write}; +use std::sync::Arc; + +use datatypes::series::SeriesOrdering; +use datatypes::series::time::EvalGrid; + +use crate::map::MathOp; +use crate::native::metrics::{self, NodePath}; +use crate::native::plan::{ + AggregateKind, AggregateStage, BoundWindow, Grouping, NativeExpr, NativeNode, NativeTree, + NativeType, +}; +use crate::range_eval::RangeFunction; +use crate::vector_match::{BinaryOp, Cardinality, MatchingLabels}; + +/// The header line of every tree. +const HEADER: &str = "NativeTree"; + +/// How deep `path` is: the root is at depth zero, its input at one. +fn depth(path: &NodePath) -> usize { + path.as_str().matches('.').count() +} + +/// The last segment of `path`, which is the name of the node in its parent. +fn name(path: &NodePath) -> &str { + // `NodePath` only ever appends `.segment`, so the last segment is never empty. + path.as_str().rsplit('.').next().unwrap_or(path.as_str()) +} + +/// A grid as `/x`. +fn grid_name(grid: &EvalGrid) -> String { + let spec = grid.spec(); + format!( + "{}/{step}x{len}", + spec.start_ms, + step = spec.step_ms, + len = spec.len + ) +} + +/// A label list in the order it was bound. +fn label_list(labels: &[Arc]) -> String { + labels + .iter() + .map(|label| label.to_string()) + .collect::>() + .join(", ") +} + +/// The ordering an input guarantees, when it is weaker than the scan's default. +fn ordering(ordering: SeriesOrdering) -> &'static str { + // `KeyThenTime` is what a scan that orders anything at all provides, so it is the + // uninteresting case; a weaker guarantee is what a consumer has to know about. + match ordering { + SeriesOrdering::KeyThenTime => "", + SeriesOrdering::Contiguous => ", contiguous", + SeriesOrdering::Interleaved => ", interleaved", + } +} + +/// The operation a node computes, in the words of the query that produced it. +fn describe(node: &NativeNode) -> String { + match &node.expr { + NativeExpr::Input(input) => match &input.output { + NativeType::Samples(schema) if schema.eval_grid.is_some() => { + format!("evaluated samples{}", ordering(input.ordering)) + } + NativeType::Samples(_) => format!("raw samples{}", ordering(input.ordering)), + NativeType::Scalar(grid) => format!("scalar on grid {}", grid_name(grid)), + NativeType::AggregateState(_) => "aggregate state".to_string(), + }, + NativeExpr::Scalar(scalar) => scalar.value.to_string(), + NativeExpr::Instant { op, .. } => format!( + "lookback {}ms offset {} on grid {}", + op.lookback_ms, + op.offset_ms, + grid_name(&op.output_grid) + ), + NativeExpr::Window { op, .. } => window(op), + NativeExpr::Map { op, .. } => { + let ops: Vec = op.ops.iter().map(math_op).collect(); + ops.join(", ") + } + NativeExpr::Aggregate { op, .. } => format!( + "{} {} [{}] {}", + match op.kind { + AggregateKind::Sum => "sum", + }, + match &op.group { + Grouping::By(_) => "by", + Grouping::Without(_) => "without", + }, + label_list(match &op.group { + Grouping::By(labels) | Grouping::Without(labels) => labels, + }), + match op.stage { + AggregateStage::Complete => "complete", + AggregateStage::Partial => "partial", + AggregateStage::Final => "final", + } + ), + NativeExpr::Binary { matching, .. } => format!( + "{}{} {} {}", + binary_op(matching.op), + if matching.return_bool { " bool" } else { "" }, + match &matching.labels { + MatchingLabels::On(labels) => format!("on [{}]", label_list(labels)), + MatchingLabels::Ignoring(labels) => format!("ignoring [{}]", label_list(labels)), + }, + match &matching.cardinality { + Cardinality::OneToOne => "one-to-one".to_string(), + Cardinality::ManyToOne(labels) => format!("many-to-one [{}]", label_list(labels)), + Cardinality::OneToMany(labels) => format!("one-to-many [{}]", label_list(labels)), + } + ), + } +} + +/// A window: what it runs, over which range, and where it lands. +fn window(op: &BoundWindow) -> String { + format!( + "{} [{}ms] offset {} anchor {} on grid {}", + function(op.function), + op.window.range_ms, + op.window.offset_ms, + match op.window.anchor_ms { + Some(anchor_ms) => anchor_ms.to_string(), + None => "-".to_string(), + }, + grid_name(&op.output_grid) + ) +} + +/// The PromQL name of a range function, with the constant argument it was bound to. +fn function(function: RangeFunction) -> String { + match function { + RangeFunction::Rate => "rate".to_string(), + RangeFunction::Increase => "increase".to_string(), + RangeFunction::Delta => "delta".to_string(), + RangeFunction::Irate => "irate".to_string(), + RangeFunction::Idelta => "idelta".to_string(), + RangeFunction::AvgOverTime => "avg_over_time".to_string(), + RangeFunction::MinOverTime => "min_over_time".to_string(), + RangeFunction::MaxOverTime => "max_over_time".to_string(), + RangeFunction::SumOverTime => "sum_over_time".to_string(), + RangeFunction::CountOverTime => "count_over_time".to_string(), + RangeFunction::LastOverTime => "last_over_time".to_string(), + RangeFunction::PresentOverTime => "present_over_time".to_string(), + RangeFunction::StddevOverTime => "stddev_over_time".to_string(), + RangeFunction::StdvarOverTime => "stdvar_over_time".to_string(), + RangeFunction::QuantileOverTime(quantile) => format!("quantile_over_time {quantile}"), + RangeFunction::Resets => "resets".to_string(), + RangeFunction::Changes => "changes".to_string(), + RangeFunction::Deriv => "deriv".to_string(), + RangeFunction::PredictLinear(seconds) => format!("predict_linear {seconds}"), + RangeFunction::DoubleExponentialSmoothing { smoothing, trend } => { + format!("double_exponential_smoothing {smoothing} {trend}") + } + RangeFunction::FirstOverTime => "first_over_time".to_string(), + RangeFunction::MadOverTime => "mad_over_time".to_string(), + RangeFunction::TsOfFirstOverTime => "ts_of_first_over_time".to_string(), + RangeFunction::TsOfLastOverTime => "ts_of_last_over_time".to_string(), + RangeFunction::TsOfMinOverTime => "ts_of_min_over_time".to_string(), + RangeFunction::TsOfMaxOverTime => "ts_of_max_over_time".to_string(), + } +} + +/// The PromQL name of a point-wise operation, with the constant arguments it was bound to. +fn math_op(op: &MathOp) -> String { + match op { + MathOp::Abs => "abs".to_string(), + MathOp::Ceil => "ceil".to_string(), + MathOp::Floor => "floor".to_string(), + MathOp::Sqrt => "sqrt".to_string(), + MathOp::Round { nearest } => format!("round {nearest}"), + MathOp::Exp => "exp".to_string(), + MathOp::Ln => "ln".to_string(), + MathOp::Log2 => "log2".to_string(), + MathOp::Log10 => "log10".to_string(), + MathOp::Sin => "sin".to_string(), + MathOp::Cos => "cos".to_string(), + MathOp::Tan => "tan".to_string(), + MathOp::Asin => "asin".to_string(), + MathOp::Acos => "acos".to_string(), + MathOp::Atan => "atan".to_string(), + MathOp::Sinh => "sinh".to_string(), + MathOp::Cosh => "cosh".to_string(), + MathOp::Tanh => "tanh".to_string(), + MathOp::Asinh => "asinh".to_string(), + MathOp::Acosh => "acosh".to_string(), + MathOp::Atanh => "atanh".to_string(), + MathOp::Deg => "deg".to_string(), + MathOp::Rad => "rad".to_string(), + MathOp::Sgn => "sgn".to_string(), + MathOp::Clamp { min, max } => format!("clamp {min} {max}"), + MathOp::ClampMin { min } => format!("clamp_min {min}"), + MathOp::ClampMax { max } => format!("clamp_max {max}"), + } +} + +/// The PromQL symbol of a binary operator. +fn binary_op(op: BinaryOp) -> &'static str { + match op { + BinaryOp::Add => "+", + BinaryOp::Sub => "-", + BinaryOp::Mul => "*", + BinaryOp::Div => "/", + BinaryOp::Mod => "%", + BinaryOp::Pow => "^", + BinaryOp::Atan2 => "atan2", + BinaryOp::Eq => "==", + BinaryOp::Ne => "!=", + BinaryOp::Gt => ">", + BinaryOp::Lt => "<", + BinaryOp::Ge => ">=", + BinaryOp::Le => "<=", + } +} + +/// What kind of node this is, as its line names it. +fn kind(expr: &NativeExpr) -> &'static str { + match expr { + NativeExpr::Input(_) => "Input", + NativeExpr::Scalar(_) => "Scalar", + NativeExpr::Instant { .. } => "Instant", + NativeExpr::Window { .. } => "Window", + NativeExpr::Map { .. } => "Map", + NativeExpr::Aggregate { .. } => "Aggregate", + NativeExpr::Binary { .. } => "Binary", + } +} + +/// The line a node is shown as, without its indentation. +fn line(node: &NativeNode, path: &NodePath) -> String { + format!("{} {} {}", name(path), kind(&node.expr), describe(node)) +} + +impl fmt::Display for NativeTree { + /// One line per node, outermost first, indented two spaces per depth below the header. + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(HEADER)?; + let mut lines = Vec::new(); + metrics::walk(self.root(), &mut |node, path| { + lines.push((depth(path), line(node, path))); + }); + for (depth, line) in lines { + f.write_char('\n')?; + // The root sits one level in from the header, and each input one level below its + // consumer, so the shape of the tree is the shape of the output. + for _ in 0..depth + 1 { + f.write_str(" ")?; + } + f.write_str(&line)?; + } + Ok(()) + } +} + +#[cfg(test)] +mod tests; diff --git a/src/promql/src/native/display/tests.rs b/src/promql/src/native/display/tests.rs new file mode 100644 index 00000000000..8166bad3c98 --- /dev/null +++ b/src/promql/src/native/display/tests.rs @@ -0,0 +1,380 @@ +// 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. + +use std::sync::Arc; + +use datatypes::series::SeriesOrdering; +use datatypes::series::samples::{FieldSpec, SeriesSchema}; +use datatypes::series::time::{EvalGrid, GridSpec}; + +use super::super::plan::{ + AggregateKind, AggregateStage, BoundAggregate, BoundInput, BoundInstant, BoundMap, BoundScalar, + BoundWindow, Grouping, NativeExpr, NativeNode, NativeTree, NativeType, +}; +use crate::map::MathOp; +use crate::range_eval::{RangeFunction, RangeWindow}; +use crate::vector_match::{BinaryMatching, BinaryOp, Cardinality, MatchingLabels}; + +fn grid(start: i64, step: i64, len: u64) -> Arc { + Arc::new(EvalGrid::try_new(GridSpec::try_new(start, step, len).unwrap()).unwrap()) +} + +fn fields() -> Arc<[FieldSpec]> { + Arc::new([FieldSpec { + name: "value".into(), + }]) +} + +/// The samples a node produces on `grid`. +fn evaluated(grid: Arc) -> NativeType { + NativeType::Samples(Arc::new(SeriesSchema { + fields: fields(), + eval_grid: Some(grid), + })) +} + +/// An unqualified scan over raw samples, which is what a compiled query starts from. +fn raw_input() -> NativeNode { + // One allocation shared by the binding and the output: `validate` checks allocation identity. + // The fields are the ones a window over this input reports, because a window keeps its input's + // layout and `validate` rejects a node that declares a different count. + let output = NativeType::Samples(Arc::new(SeriesSchema { + fields: fields(), + eval_grid: None, + })); + NativeNode { + expr: NativeExpr::Input(BoundInput { + output: output.clone(), + ordering: SeriesOrdering::KeyThenTime, + }), + output, + } +} + +fn window_over( + inner: NativeNode, + function: RangeFunction, + range_ms: i64, + offset_ms: i64, + anchor_ms: Option, + out: Arc, +) -> NativeNode { + NativeNode { + expr: NativeExpr::Window { + input: Box::new(inner), + op: BoundWindow { + function, + window: RangeWindow { + range_ms, + offset_ms, + anchor_ms, + }, + input_grid: None, + output_grid: Arc::clone(&out), + }, + }, + output: evaluated(out), + } +} + +fn map(inner: NativeNode, ops: Vec) -> NativeNode { + let out = inner.output.grid().expect("a map needs a grid").clone(); + NativeNode { + expr: NativeExpr::Map { + input: Box::new(inner), + op: BoundMap { + ops: Arc::from(ops), + }, + }, + output: evaluated(out), + } +} + +/// The expected display of a tree, one string per line, so a change in wording, order or +/// indentation fails the test rather than passing as a substring. +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. +#[test] +fn the_first_line_names_the_tree() { + let tree = NativeTree::try_new(raw_input()).unwrap(); + + assert_eq!( + tree.to_string(), + shown(&["NativeTree", " root Input raw samples"]) + ); + 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 +/// folds is shown beneath it. +#[test] +fn a_window_over_raw_samples_is_shown_once_per_node() { + let tree = NativeTree::try_new(window_over( + raw_input(), + RangeFunction::Rate, + 300_000, + 0, + None, + grid(0, 60_000, 4), + )) + .unwrap(); + + assert_eq!( + tree.to_string(), + shown(&[ + "NativeTree", + " root Window rate [300000ms] offset 0 anchor - on grid 0/60000x4", + " input Input raw samples", + ]) + ); +} + +/// A map nests one level deeper than the window it reads, and lists its operations in the order +/// they are applied. +#[test] +fn a_map_nests_below_the_window_it_maps() { + let tree = NativeTree::try_new(map( + window_over( + raw_input(), + RangeFunction::Rate, + 300_000, + 60_000, + None, + grid(0, 60_000, 4), + ), + vec![MathOp::Abs, MathOp::Round { nearest: 3.0 }], + )) + .unwrap(); + + 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", + ]) + ); +} + +/// A parameterised window names the constant it was bound to, and an anchored one names its +/// anchor instead of printing the absent case. +#[test] +fn a_window_names_its_constant_argument_and_its_anchor() { + let tree = NativeTree::try_new(window_over( + raw_input(), + RangeFunction::QuantileOverTime(0.9), + 60_000, + 0, + Some(42), + grid(1_000, 60_000, 4), + )) + .unwrap(); + + 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", + ]) + ); +} + +/// An input says whether its samples are raw or already evaluated, and reports the ordering it +/// guarantees when that is weaker than the scan default. +#[test] +fn an_input_reports_whether_it_is_raw_and_what_ordering_it_guarantees() { + let contiguous = NativeType::Samples(Arc::new(SeriesSchema { + fields: fields(), + eval_grid: Some(grid(0, 60_000, 4)), + })); + let evaluated_input = NativeNode { + expr: NativeExpr::Input(BoundInput { + output: contiguous.clone(), + ordering: SeriesOrdering::Contiguous, + }), + output: contiguous, + }; + let tree = NativeTree::try_new(evaluated_input).unwrap(); + assert_eq!( + tree.to_string(), + shown(&["NativeTree", " root Input evaluated samples, contiguous",]) + ); + + let interleaved = NativeType::Samples(Arc::new(SeriesSchema { + fields: fields(), + eval_grid: None, + })); + let raw_input = NativeNode { + expr: NativeExpr::Input(BoundInput { + output: interleaved.clone(), + ordering: SeriesOrdering::Interleaved, + }), + output: interleaved, + }; + let tree = NativeTree::try_new(raw_input).unwrap(); + assert_eq!( + tree.to_string(), + shown(&["NativeTree", " root Input raw samples, interleaved"]) + ); +} + +/// A leaf kind renders what it holds, whether or not anything could run it: the line names the +/// node it is and makes no claim about executability. +#[test] +fn a_scalar_and_an_instant_line_show_what_they_hold() { + let out = grid(0, 60_000, 4); + let scalar = NativeNode { + expr: NativeExpr::Scalar(BoundScalar { value: 2.0 }), + output: NativeType::Scalar(Arc::clone(&out)), + }; + let tree = NativeTree::try_new(scalar).unwrap(); + assert_eq!(tree.to_string(), shown(&["NativeTree", " root Scalar 2"])); + + let instant = NativeNode { + expr: NativeExpr::Instant { + input: Box::new(raw_input()), + op: BoundInstant { + lookback_ms: 300_000, + offset_ms: 60_000, + output_grid: Arc::clone(&out), + }, + }, + output: evaluated(out), + }; + let tree = NativeTree::try_new(instant).unwrap(); + assert_eq!( + tree.to_string(), + shown(&[ + "NativeTree", + " root Instant lookback 300000ms offset 60000 on grid 0/60000x4", + " input Input raw samples", + ]) + ); +} + +/// An aggregate and a binary operation name the binding they were compiled with, which is what +/// makes the line worth reading. +#[test] +fn an_aggregate_and_a_binary_name_their_binding() { + let out = grid(0, 60_000, 4); + let aggregate = 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(Arc::clone(&out)), + }; + let tree = NativeTree::try_new(aggregate).unwrap(); + 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", + ]) + ); + + // A scalar broadcast, which is the one binary shape whose matching rules are never consulted: + // the display still prints them, because the binding is what the compiler decided. + let binary = NativeNode { + expr: NativeExpr::Binary { + left: Box::new(NativeNode { + expr: NativeExpr::Scalar(BoundScalar { value: 2.0 }), + output: NativeType::Scalar(Arc::clone(&out)), + }), + right: Box::new(window_over( + raw_input(), + RangeFunction::Rate, + 300_000, + 0, + None, + Arc::clone(&out), + )), + matching: BinaryMatching { + op: BinaryOp::Add, + return_bool: false, + labels: MatchingLabels::Ignoring(Vec::new()), + cardinality: Cardinality::OneToOne, + }, + }, + output: evaluated(out), + }; + let tree = NativeTree::try_new(binary).unwrap(); + 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", + ]) + ); +} + +/// A `without` grouping reads as `without [...]`, not as `by [...]`. +/// +/// The two are different queries over the same label list: `by(job)` keeps `job` and drops the +/// rest, `without(job)` drops `job` and keeps the rest. A rendering that spelled both `by` would +/// show two different queries as one. +#[test] +fn a_without_grouping_does_not_read_as_by() { + let out = grid(0, 60_000, 4); + let aggregate = 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::Without(Arc::new(["job".into()])), + stage: AggregateStage::Complete, + }, + }, + output: evaluated(Arc::clone(&out)), + }; + let tree = NativeTree::try_new(aggregate).unwrap(); + 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", + ]) + ); +} diff --git a/src/promql/src/native/metrics.rs b/src/promql/src/native/metrics.rs new file mode 100644 index 00000000000..bcae9ddcdd0 --- /dev/null +++ b/src/promql/src/native/metrics.rs @@ -0,0 +1,609 @@ +// 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. + +//! Where a node sits in the final execution tree, and the metrics named after that position. +//! +//! A node is identified by its structural position — `root`, `.input`, `.left`, `.right` — rather +//! than by an id it carries. The position is not stored on the node: it is derived while walking +//! the tree. [`walk`] is the one traversal rule, and [`register_tree`] is its caller. +//! +//! The position stays in the metric name because the collector aggregates by name first: two +//! `rate` nodes sharing a bare name would be merged into one number. Positions are only an +//! observation detail of one plan — they are not stable across queries, plan rewrites or versions, +//! so nothing may depend on them beyond correlating a metric with a tree it was taken from. + +use std::sync::Arc; + +use datafusion::physical_plan::metrics::MetricValue; +use datafusion::physical_plan::metrics::{ + Count, ExecutionPlanMetricsSet, Gauge, MetricBuilder, Time, +}; + +use crate::native::plan::{NativeExpr, NativeNode}; + +/// The reserved first segment of every native metric name. +const NATIVE_PREFIX: &str = "native"; + +/// A node's position in the final tree. +/// +/// Rendering allocates a short string once, when the run-time state is created; the hot path holds +/// the metric handles instead of looking anything up by name. +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct NodePath(String); + +impl NodePath { + /// The position of the root node. + pub fn root() -> Self { + Self("root".to_string()) + } + + /// The position of a unary node's input. + pub fn input(&self) -> Self { + Self(format!("{}.input", self.0)) + } + + /// The position of a binary node's left input. + pub fn left(&self) -> Self { + Self(format!("{}.left", self.0)) + } + + /// The position of a binary node's right input. + pub fn right(&self) -> Self { + Self(format!("{}.right", self.0)) + } + + /// The path without the metric namespace. + pub fn as_str(&self) -> &str { + &self.0 + } + + /// The full metric name for `metric` at this position. + pub fn metric_name(&self, metric: &str) -> String { + format!("{NATIVE_PREFIX}.{}.{}", self.0, metric) + } + + /// The full metric name for `metric` at this position in `partition`. + /// + /// A figure that must not be merged across partitions needs the partition in its name: the + /// collector aggregates by name first, and a sum of peaks is not a peak. + pub fn partitioned_metric_name(&self, metric: &str, partition: usize) -> String { + format!("{NATIVE_PREFIX}.p{partition}.{}.{metric}", self.0) + } +} + +impl std::fmt::Display for NodePath { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&self.0) + } +} + +/// Visits every node of `root` with its position, outermost first. +/// +/// This is the one traversal rule: [`register_tree`] is its caller, so a node's metric is always +/// named after the position this walk gives it. +pub fn walk(root: &NativeNode, visit: &mut dyn FnMut(&NativeNode, &NodePath)) { + fn go(node: &NativeNode, path: &NodePath, visit: &mut dyn FnMut(&NativeNode, &NodePath)) { + visit(node, path); + match &node.expr { + NativeExpr::Input(_) | NativeExpr::Scalar(_) => {} + NativeExpr::Instant { input, .. } + | NativeExpr::Window { input, .. } + | NativeExpr::Map { input, .. } + | NativeExpr::Aggregate { input, .. } => go(input, &path.input(), visit), + NativeExpr::Binary { left, right, .. } => { + go(left, &path.left(), visit); + go(right, &path.right(), visit); + } + } + } + go(root, &NodePath::root(), visit); +} + +/// The metric names one node reports. +/// +/// [`Self::ALL`] is the list a reader should treat as complete: [`NodeMetrics::register`] names each +/// handle itself, and `registered_metrics_are_exactly_the_declared_set` fails when the two sides +/// drift apart. +pub struct NodeMetric; + +impl NodeMetric { + /// How many batches this node consumed. + pub const INPUT_BATCHES: &'static str = "input_batches"; + /// How many batches this node produced. + pub const OUTPUT_BATCHES: &'static str = "output_batches"; + /// How many sample positions this node ingested. + pub const INPUT_SAMPLES: &'static str = "input_samples"; + /// How many grid positions this node evaluated, including those with no valid result. + pub const EVALUATED_GRID_POINTS: &'static str = "evaluated_grid_points"; + /// How many valid values this node produced. + pub const OUTPUT_VALUES: &'static str = "output_values"; + /// How many series this node closed. + pub const COMPLETED_SERIES: &'static str = "completed_series"; + /// Time this node spent in its own work, excluding its inputs. + pub const SELF_COMPUTE_TIME_NS: &'static str = "self_compute_time_ns"; + /// Bytes this node currently reserves. + pub const CURRENT_RESERVED_BYTES: &'static str = "current_reserved_bytes"; + /// The highest byte count this node reserved during the execution. + pub const PEAK_RESERVED_BYTES: &'static str = "peak_reserved_bytes"; + /// How many groups this node tracks, counted per partition: a group reached by several + /// partitions is one entry in each. + pub const GROUPS: &'static str = "groups"; + /// How many state entries this node tracks. + pub const STATE_ENTRIES: &'static str = "state_entries"; + + /// Every metric a node may report. + pub const ALL: &'static [&'static str] = &[ + Self::INPUT_BATCHES, + Self::OUTPUT_BATCHES, + Self::INPUT_SAMPLES, + Self::EVALUATED_GRID_POINTS, + Self::OUTPUT_VALUES, + Self::COMPLETED_SERIES, + Self::SELF_COMPUTE_TIME_NS, + Self::CURRENT_RESERVED_BYTES, + Self::PEAK_RESERVED_BYTES, + Self::GROUPS, + Self::STATE_ENTRIES, + ]; +} + +/// 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 +/// on the hot path. +#[derive(Clone, Debug)] +pub struct NodeMetrics { + input_batches: Count, + output_batches: Count, + input_samples: Count, + evaluated_grid_points: Count, + output_values: Count, + completed_series: Count, + self_compute_time: Time, + current_reserved_bytes: Gauge, + peak_reserved_bytes: Gauge, + groups: Gauge, + state_entries: Gauge, +} + +impl Default for NodeMetrics { + fn default() -> Self { + Self { + input_batches: Count::new(), + output_batches: Count::new(), + input_samples: Count::new(), + evaluated_grid_points: Count::new(), + output_values: Count::new(), + completed_series: Count::new(), + self_compute_time: Time::new(), + current_reserved_bytes: Gauge::new(), + peak_reserved_bytes: Gauge::new(), + groups: Gauge::new(), + state_entries: Gauge::new(), + } + } +} + +impl NodeMetrics { + /// Registers this node's metrics under `path` in `set`. + /// + /// `partition` is part of the registration, so two partitions of one plan report separately. + /// It is also part of the peak's name, because a peak is the one figure a collector must not + /// add up across partitions. + pub fn register(&self, path: &NodePath, set: &ExecutionPlanMetricsSet, partition: usize) { + fn count(set: &ExecutionPlanMetricsSet, partition: usize, name: String, count: &Count) { + MetricBuilder::new(set) + .with_partition(partition) + .build(MetricValue::Count { + name: name.into(), + count: count.clone(), + }); + } + fn gauge(set: &ExecutionPlanMetricsSet, partition: usize, name: String, gauge: &Gauge) { + MetricBuilder::new(set) + .with_partition(partition) + .build(MetricValue::Gauge { + name: name.into(), + gauge: gauge.clone(), + }); + } + + count( + set, + partition, + path.metric_name(NodeMetric::INPUT_BATCHES), + &self.input_batches, + ); + count( + set, + partition, + path.metric_name(NodeMetric::OUTPUT_BATCHES), + &self.output_batches, + ); + count( + set, + partition, + path.metric_name(NodeMetric::INPUT_SAMPLES), + &self.input_samples, + ); + count( + set, + partition, + path.metric_name(NodeMetric::EVALUATED_GRID_POINTS), + &self.evaluated_grid_points, + ); + count( + set, + partition, + path.metric_name(NodeMetric::OUTPUT_VALUES), + &self.output_values, + ); + count( + set, + partition, + path.metric_name(NodeMetric::COMPLETED_SERIES), + &self.completed_series, + ); + gauge( + set, + partition, + path.metric_name(NodeMetric::CURRENT_RESERVED_BYTES), + &self.current_reserved_bytes, + ); + // The peak is the one figure named per partition. The collector groups by name and sums + // gauges, and a sum of two peaks is neither peak: partitions peak at different moments. + // Summing the other gauges and counts stays meaningful — the memory held now and the + // samples seen add up across partitions. `groups` does not: a `Partial` aggregate sees the + // same group in every partition it reaches, so the sum is per-partition entries, not + // distinct groups. + gauge( + set, + partition, + path.partitioned_metric_name(NodeMetric::PEAK_RESERVED_BYTES, partition), + &self.peak_reserved_bytes, + ); + gauge( + set, + partition, + path.metric_name(NodeMetric::GROUPS), + &self.groups, + ); + gauge( + set, + partition, + path.metric_name(NodeMetric::STATE_ENTRIES), + &self.state_entries, + ); + MetricBuilder::new(set) + .with_partition(partition) + .build(MetricValue::Time { + name: path.metric_name(NodeMetric::SELF_COMPUTE_TIME_NS).into(), + time: self.self_compute_time.clone(), + }); + } + + /// Records that this node ingested `batches` batches carrying `samples` sample positions. + pub fn record_input(&self, batches: usize, samples: usize) { + self.input_batches.add(batches); + self.input_samples.add(samples); + } + + /// Records that this node produced `batches` batches carrying `values` valid values. + pub fn record_output(&self, batches: usize, values: usize) { + self.output_batches.add(batches); + self.output_values.add(values); + } + + /// Records that this node evaluated `points` grid positions. + pub fn record_grid_points(&self, points: usize) { + self.evaluated_grid_points.add(points); + } + + /// Records that this node closed `series` series. + pub fn record_completed_series(&self, series: usize) { + self.completed_series.add(series); + } + + /// Records that this node now reserves `bytes`. + /// + /// The peak is a high-water mark of this node in this partition, never a sum of the peaks of + /// different nodes: those peaks can happen at different times. + pub fn record_reserved_bytes(&self, bytes: usize) { + self.current_reserved_bytes.set(bytes); + // A high-water mark, not a sum: two nodes peak at different moments. + self.peak_reserved_bytes.set_max(bytes); + } + + /// Records the size of the grouping and state this node holds. + pub fn record_state(&self, groups: usize, entries: usize) { + self.groups.set(groups); + self.state_entries.set(entries); + } + + /// Starts timing this node's own work. + /// + /// The returned timer must be dropped before the node polls its input: a node's self time + /// excludes the work its inputs do. + pub fn timer(&self) -> datafusion::physical_plan::metrics::ScopedTimerGuard<'_> { + self.self_compute_time.timer() + } +} + +/// Registers every node of `root` in `set` and returns the handles in traversal order. +pub fn register_tree( + root: &NativeNode, + set: &ExecutionPlanMetricsSet, + partition: usize, +) -> Vec<(NodePath, Arc)> { + let mut out = Vec::new(); + walk(root, &mut |_, path| { + let metrics = Arc::new(NodeMetrics::default()); + metrics.register(path, set, partition); + out.push((path.clone(), metrics)); + }); + out +} + +#[cfg(test)] +mod tests { + use std::sync::Arc; + + use datafusion::physical_plan::metrics::{ExecutionPlanMetricsSet, MetricValue}; + use datatypes::series::samples::SeriesSchema; + use datatypes::series::time::{EvalGrid, GridSpec}; + + use super::{NodeMetric, NodePath, register_tree, walk}; + use crate::map::MathOp; + use crate::native::plan::{ + BoundInput, BoundMap, BoundWindow, NativeExpr, NativeNode, NativeType, + }; + use crate::range_eval::{RangeFunction, RangeWindow}; + + fn grid() -> Arc { + Arc::new(EvalGrid::try_new(GridSpec::try_new(0, 60_000, 4).unwrap()).unwrap()) + } + + fn schema() -> Arc { + Arc::new(SeriesSchema { + fields: Arc::new([]), + eval_grid: None, + }) + } + + fn input() -> NativeNode { + let output = NativeType::Samples(schema()); + NativeNode { + expr: NativeExpr::Input(BoundInput { + output: output.clone(), + ordering: datatypes::series::SeriesOrdering::KeyThenTime, + }), + output, + } + } + + fn window(inner: NativeNode) -> NativeNode { + let grid = grid(); + NativeNode { + expr: NativeExpr::Window { + input: Box::new(inner), + op: BoundWindow { + function: RangeFunction::Rate, + window: RangeWindow { + range_ms: 300_000, + offset_ms: 0, + anchor_ms: None, + }, + input_grid: None, + output_grid: Arc::clone(&grid), + }, + }, + output: NativeType::Samples(Arc::new(SeriesSchema { + fields: Arc::new([]), + eval_grid: Some(grid), + })), + } + } + + fn map(inner: NativeNode) -> NativeNode { + let grid = inner.output.grid().expect("a map needs a grid").clone(); + NativeNode { + expr: NativeExpr::Map { + input: Box::new(inner), + op: BoundMap { + ops: Arc::new([MathOp::Abs]), + }, + }, + output: NativeType::Samples(Arc::new(SeriesSchema { + fields: Arc::new([]), + eval_grid: Some(grid), + })), + } + } + + /// A unary chain is addressed by `.input`, so a metric can be attributed to one node. + #[test] + fn a_unary_chain_is_addressed_by_input_steps() { + let root = map(window(input())); + let mut paths = Vec::new(); + walk(&root, &mut |_, path| paths.push(path.to_string())); + assert_eq!(paths, vec!["root", "root.input", "root.input.input"]); + } + + /// A binary node addresses its two sides by name, so the two `rate` subtrees stay distinct. + #[test] + fn a_binary_node_addresses_both_sides() { + let binary = crate::native::plan::NativeNode { + expr: NativeExpr::Binary { + left: Box::new(window(input())), + right: Box::new(map(window(input()))), + matching: crate::vector_match::BinaryMatching { + op: crate::vector_match::BinaryOp::Add, + return_bool: false, + labels: crate::vector_match::MatchingLabels::Ignoring(Vec::new()), + cardinality: crate::vector_match::Cardinality::OneToOne, + }, + }, + output: { + let grid = grid(); + NativeType::Samples(Arc::new(SeriesSchema { + fields: Arc::new([]), + eval_grid: Some(grid), + })) + }, + }; + let mut paths = Vec::new(); + walk(&binary, &mut |_, path| paths.push(path.to_string())); + assert_eq!( + paths, + vec![ + "root", + "root.left", + "root.left.input", + "root.right", + "root.right.input", + "root.right.input.input", + ] + ); + } + + /// The name carries the position because the collector aggregates by name; a bare name would + /// merge two nodes of the same operation into one number. + #[test] + fn a_metric_name_carries_its_position() { + let path = NodePath::root().left().input(); + assert_eq!(path.as_str(), "root.left.input"); + assert_eq!( + path.metric_name(NodeMetric::SELF_COMPUTE_TIME_NS), + "native.root.left.input.self_compute_time_ns" + ); + assert_eq!( + path.partitioned_metric_name(NodeMetric::PEAK_RESERVED_BYTES, 1), + "native.p1.root.left.input.peak_reserved_bytes" + ); + } + + /// The peak keeps the partition in its name, so the collector's aggregation by name cannot add + /// two partitions' peaks: 1024 and 2048 peak at different moments, and 3072 is neither peak. + #[test] + fn a_peak_is_not_merged_across_partitions() { + let root = map(window(input())); + let set = ExecutionPlanMetricsSet::new(); + let peaks = [(0, 1024), (1, 2048)]; + for (partition, peak) in peaks { + let registered = register_tree(&root, &set, partition); + // The root comes first in traversal order. + registered[0].1.record_reserved_bytes(peak); + } + + // The real collector path: `aggregate_by_name` groups by metric name and sums the + // values in each group. + let aggregated = set.clone_inner().aggregate_by_name(); + let peak = |partition: usize| -> Option { + let expected = NodePath::root() + .partitioned_metric_name(NodeMetric::PEAK_RESERVED_BYTES, partition); + aggregated.iter().find_map(|metric| match metric.value() { + MetricValue::Gauge { name, gauge } if name.as_ref() == expected.as_str() => { + Some(gauge.value()) + } + _ => None, + }) + }; + // Two separate names, each holding its own partition's peak: had the name not carried the + // partition, aggregation would have merged them into one gauge summing to 3072. + assert_eq!(peak(0), Some(1024)); + assert_eq!(peak(1), Some(2048)); + } + + /// Registration covers every node, so no node is executed without a way to observe it. + /// + /// The set is read back rather than named again from the tree, so this checks what + /// registration actually did. + #[test] + fn registration_covers_every_node() { + let root = map(window(input())); + let set = datafusion::physical_plan::metrics::ExecutionPlanMetricsSet::new(); + let registered = register_tree(&root, &set, 2); + assert_eq!(registered.len(), 3); + + // Read the names back from the set registration wrote to, so the check is against what + // registration did rather than against the names built a second time here. + let set = set.clone_inner(); + let names: Vec<&str> = set.iter().map(|metric| metric.value().name()).collect(); + // One entry per node per declared metric, and every node's position among them: no node + // is executed without a way to observe it. + assert_eq!(names.len(), registered.len() * NodeMetric::ALL.len()); + for (path, _) in ®istered { + // The peak is named per partition, so a plain name stands for the node here. + let name = path.metric_name(NodeMetric::OUTPUT_VALUES); + assert!( + names.contains(&name.as_str()), + "{path} registered nothing under its own position" + ); + } + } + + /// Every metric a node reports is listed in one place, so registration cannot drift from the + /// documented set. + #[test] + fn every_metric_is_declared() { + assert_eq!(NodeMetric::ALL.len(), 11); + assert!(NodeMetric::ALL.contains(&NodeMetric::SELF_COMPUTE_TIME_NS)); + assert!(NodeMetric::ALL.contains(&NodeMetric::PEAK_RESERVED_BYTES)); + } + + /// Registration reports exactly the metrics `ALL` declares, in both directions. + /// + /// The names are read back from the set a node registered in, so the check is against what + /// registration actually did, not against `ALL` itself. Adding a metric to `ALL` without + /// registering it in `register`, or registering a metric that `ALL` does not list, fails this + /// test: it compares the two sides instead of trusting either. + #[test] + fn registered_metrics_are_exactly_the_declared_set() { + let root = map(window(input())); + let set = ExecutionPlanMetricsSet::new(); + register_tree(&root, &set, 0); + + // One node reports each metric once; the node paths and the partition prefix separate + // nodes, so the bare name is the last dot-separated segment. + let set = set.clone_inner(); + let mut registered: Vec<&str> = set + .iter() + .map(|metric| { + metric + .value() + .name() + .rsplit('.') + .next() + .expect("a metric name has a last segment") + }) + .collect(); + registered.sort_unstable(); + registered.dedup(); + + let mut declared: Vec<&str> = NodeMetric::ALL.to_vec(); + declared.sort_unstable(); + + assert_eq!(registered, declared); + } + + /// A peak is a high-water mark of one node, so it survives a later drop in reservations. + #[test] + fn a_peak_is_a_high_water_mark() { + let metrics = super::NodeMetrics::default(); + metrics.record_reserved_bytes(1024); + metrics.record_reserved_bytes(64); + assert_eq!(metrics.peak_reserved_bytes.value(), 1024); + assert_eq!(metrics.current_reserved_bytes.value(), 64); + } +} diff --git a/src/promql/src/native/mod.rs b/src/promql/src/native/mod.rs new file mode 100644 index 00000000000..e843c0169be --- /dev/null +++ b/src/promql/src/native/mod.rs @@ -0,0 +1,33 @@ +// 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. + +//! The native PromQL evaluation model. +//! +//! A query compiles to a [`plan::NativeTree`]: nodes whose operations, fields and grids are already +//! bound. Nodes hand each other [`batch::EvaluatedChunk`]s as ordinary Rust values, so no row batch +//! is materialised between them. [`metrics`] names each node after its position in that tree. +//! +//! Three properties keep this apart from recognising a query shape in a finished physical plan: +//! +//! - Legality is a type and data-dependency question ([`plan::NativeNode::validate`]), so a new +//! combination of supported operations is an ordinary tree, not another matcher. +//! - A window binds its input and output grids separately, so a subquery's inner grid is consumed +//! as it is instead of being resampled onto the outer grid. +//! - A series closes exactly once, on the chunk that says so, so a consumer can tell "nothing more +//! here" from "no more input". + +pub mod batch; +mod display; +pub mod metrics; +pub mod plan; diff --git a/src/promql/src/native/plan.rs b/src/promql/src/native/plan.rs new file mode 100644 index 00000000000..6cca42134f3 --- /dev/null +++ b/src/promql/src/native/plan.rs @@ -0,0 +1,635 @@ +// 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. + +//! The bound tree a native PromQL evaluation compiles to. +//! +//! A query is compiled into [`NativeTree`]: [`NativeNode`]s whose operations are already bound to +//! kernels, fields and grids. Nothing in this module interprets a function name, resolves a label +//! or picks a column at run time; those decisions are made before the tree exists, so the hot +//! path only feeds batches to kernels it was handed. +//! +//! The tree is also the unit of legality. [`NativeNode::validate`] checks the contract every +//! operation states about its input, which is what lets a new combination of supported operations +//! be an ordinary tree instead of another hand-written shape. +//! +//! What the tree cannot express yet is deliberate for this stage: [`NativeExpr::Binary`] holds an +//! arithmetic or comparison operation, so `and`, `or` and `unless` have no node, and +//! [`NativeExpr::Scalar`] holds a constant, so `time()` and `scalar(v)` have no node either. The +//! legality below is therefore about the operations that exist, not about PromQL's whole surface. + +use std::sync::Arc; + +use datafusion::error::{DataFusionError, Result}; +use datatypes::series::SeriesOrdering; +use datatypes::series::samples::{FieldSpec, SeriesSchema}; +use datatypes::series::time::EvalGrid; + +use crate::map::MathOp; +use crate::range_eval::{RangeFunction, RangeWindow}; +use crate::vector_match::BinaryMatching; + +/// What one node produces. +/// +/// A sample type with an evaluation grid is an evaluated series: one value per grid point. Without +/// a grid it is raw samples carrying their own timestamps. A scalar is one value per grid point +/// and no labels. Intermediate aggregate state is a different type altogether, so an operation +/// that cannot consume it is rejected by type rather than by convention. +#[derive(Clone, Debug)] +pub enum NativeType { + /// Samples, evaluated when the schema carries a grid and raw otherwise. + Samples(Arc), + /// One value per grid point and no labels, which binary operations broadcast. + /// + /// PromQL scalars vary with the evaluation time (`time()`, `scalar(v)`), so this carries the + /// grid it is evaluated on. Its value carrier is a series without labels, but its type is + /// what keeps a broadcast from becoming label matching. + Scalar(Arc), + /// Intermediate aggregate state, not a query value. + AggregateState(Arc), +} + +impl NativeType { + /// The schema of a sample type, or `None` for a scalar or aggregate state. + pub fn samples(&self) -> Option<&Arc> { + match self { + Self::Samples(schema) => Some(schema), + Self::Scalar(_) | Self::AggregateState(_) => None, + } + } + + /// The grid this type is evaluated on, or `None` when it holds raw samples or state. + /// + /// A scalar is evaluated on its grid like a vector, which is what lets a binary operation + /// line it up with a vector point by point; only raw samples and state have no positions. + pub fn grid(&self) -> Option<&Arc> { + match self { + Self::Samples(schema) => schema.eval_grid.as_ref(), + Self::Scalar(grid) => Some(grid), + Self::AggregateState(_) => None, + } + } + + /// What this type is, for a rejection that has to say what a consumer was handed. + fn describe(&self) -> &'static str { + match self { + Self::Samples(schema) if schema.eval_grid.is_some() => "an evaluated vector", + Self::Samples(_) => "raw samples", + Self::Scalar(_) => "a scalar", + Self::AggregateState(_) => "aggregate state", + } + } + + /// Whether `self` binds the same thing as `other`: the same samples or the same state. + /// + /// Bindings are compared by allocation identity, because a compiler that meant one schema or + /// one state has no reason to allocate it twice. A scalar is produced by a node and never + /// handed to one by a source, so it never matches. This is about the binding itself: grids are + /// aligned by [`EvalGrid::spec`] everywhere else, here and in the kernels. + fn same_binding(&self, other: &NativeType) -> bool { + match (self, other) { + (Self::Samples(mine), Self::Samples(theirs)) => Arc::ptr_eq(mine, theirs), + (Self::AggregateState(mine), Self::AggregateState(theirs)) => Arc::ptr_eq(mine, theirs), + _ => false, + } + } +} + +/// The layout of intermediate aggregate state. +/// +/// State is indexed by `(group labels, absolute grid index)`, so it carries the grid it was +/// accumulated on. It is not a query value and does not pretend to be one. +#[derive(Clone, Debug)] +pub struct AggregateStateSchema { + /// How the groups its rows are indexed by are identified. + pub group: Grouping, + /// The accumulator columns, one per aggregated field. + pub accumulators: Arc<[FieldSpec]>, + /// The grid the state is indexed on. + pub grid: Arc, +} + +/// A scan or exchange boundary bound as a node input. +/// +/// The binding records what the input was created for: the output a consumer type-checks against, +/// and the ordering it guarantees. A window reads the ordering, rejecting an interleaved input; no +/// other consumer reads it yet. +#[derive(Clone, Debug)] +pub struct BoundInput { + /// What the input produces: raw samples from a scan, or state from an exchange boundary. + pub output: NativeType, + /// The ordering the input guarantees. + pub ordering: SeriesOrdering, +} + +/// A constant scalar bound for broadcast. +/// +/// A scalar that varies per grid point is the same [`NativeType::Scalar`] as this constant: what +/// keeps a binary operation from label-matching it is its type, not its constness. This binding +/// only holds the constant case. +#[derive(Clone, Debug)] +pub struct BoundScalar { + /// The constant value. + pub value: f64, +} + +/// An instant selection: the last sample at or before each grid point, within a lookback. +#[derive(Clone, Debug)] +pub struct BoundInstant { + /// How far back a sample may lie before it counts as stale. + pub lookback_ms: i64, + /// Selector offset; positive reads earlier samples. + pub offset_ms: i64, + /// The grid the selection produces values on. + pub output_grid: Arc, +} + +/// A window function over raw samples or over an evaluated input. +/// +/// The input and output grids are bound separately: a subquery evaluates its inner expression on +/// its own grid and consumes the result directly, without resampling it onto the outer grid. +#[derive(Clone, Debug)] +pub struct BoundWindow { + /// The function this node runs. + pub function: RangeFunction, + /// The window length, offset and anchor. + pub window: RangeWindow, + /// The grid the input is evaluated on, or `None` when the input is raw samples. + pub input_grid: Option>, + /// The grid this node produces values on. + pub output_grid: Arc, +} + +/// A chain of point-wise operations applied to a whole evaluated value. +#[derive(Clone, Debug)] +pub struct BoundMap { + /// The operations, applied in order. + pub ops: Arc<[MathOp]>, +} + +/// Which aggregate a node computes. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum AggregateKind { + /// `sum` over the values of each group at each grid point. + Sum, +} + +/// Where an aggregate sits in the execution, which decides whether its output is a query value. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum AggregateStage { + /// Every contribution to each group lies within this execution, so the output is complete. + Complete, + /// Only this execution's contributions were seen, so the output is state for a later merge. + Partial, + /// The state was collected from every contributing execution, so the output is complete. + Final, +} + +/// How an aggregate's groups are identified, in the binding's own terms. +/// +/// The kernel already takes both shapes ([`aggregate::Grouping`]), and the two are not +/// interchangeable: `by(job)` keeps `job` and drops everything else, while `without(job)` keeps +/// every other label and drops `job` too. A list of group labels can only spell the first, so a +/// `without` written that way would group a query by labels it meant to remove. +/// +/// [`aggregate::Grouping`]: crate::aggregate::Grouping +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Grouping { + /// The labels a group is identified by; every other label is dropped. + By(Arc<[Arc]>), + /// The labels a group is not identified by; these are dropped, and so is `__name__`. + Without(Arc<[Arc]>), +} + +/// An aggregate bound to its kind, grouping and stage. +#[derive(Clone, Debug)] +pub struct BoundAggregate { + /// Which aggregate to compute. + pub kind: AggregateKind, + /// How its groups are identified. + pub group: Grouping, + /// Where this aggregate sits in the execution. + pub stage: AggregateStage, +} + +/// What a node computes. Every variant holds a bound operation, never a name to resolve. +#[derive(Clone, Debug)] +pub enum NativeExpr { + /// A scan or an exchange boundary. + Input(BoundInput), + /// A constant scalar, evaluated on the statement's grid. + Scalar(BoundScalar), + /// An instant selection over its input. + Instant { + input: Box, + op: BoundInstant, + }, + /// A window function over its input. + Window { + input: Box, + op: BoundWindow, + }, + /// Point-wise operations over its input. + Map { + input: Box, + op: BoundMap, + }, + /// An aggregate over its input. + Aggregate { + input: Box, + op: BoundAggregate, + }, + /// A binary operation between two inputs, with its matching rules. + /// + /// Scalar operands are broadcast, so `matching` only applies to vector–vector operations. + Binary { + left: Box, + right: Box, + matching: BinaryMatching, + }, +} + +/// One node of a bound tree. +#[derive(Clone, Debug)] +pub struct NativeNode { + /// What this node computes, including its bound operands. + pub expr: NativeExpr, + /// What this node produces. + pub output: NativeType, +} + +/// A bound evaluation tree. +/// +/// The tree itself carries no run-time state: every execution builds its own, so one compiled plan +/// can serve every partition. +#[derive(Clone, Debug)] +pub struct NativeTree { + root: NativeNode, +} + +impl NativeTree { + /// Creates a tree over `root`, checking that every node's contract holds. + /// + /// A rejected tree is a compiler defect rather than a query error: the compiler only builds a + /// node after deciding that this combination is supported, so the error is + /// [`DataFusionError::Internal`] and not a statement the client wrote wrong. + pub fn try_new(root: NativeNode) -> Result { + root.validate()?; + Ok(Self { root }) + } + + /// The root node. + pub fn root(&self) -> &NativeNode { + &self.root + } +} + +impl NativeNode { + /// Checks this node and its inputs against the contract of its operation. + pub fn validate(&self) -> Result<()> { + match &self.expr { + NativeExpr::Input(input) => { + // An input node's output is exactly the binding it was bound to: a scan hands over + // the samples it was created for, and an exchange boundary the state. + if !self.output.same_binding(&input.output) { + return Err(invalid( + "an input node's output is not the binding it was bound to", + )); + } + } + NativeExpr::Scalar(_) => { + // A scalar's output is the scalar type: one value per grid point and no labels. + if !matches!(self.output, NativeType::Scalar(_)) { + return Err(invalid("a scalar node does not produce a scalar")); + } + } + NativeExpr::Instant { input, op } => { + input.validate()?; + // An instant selection reads raw samples and stamps them onto its own grid: an + // evaluated vector is already stamped, and a scalar has no timestamps at all. + match &input.output { + NativeType::Samples(schema) if schema.eval_grid.is_none() => {} + NativeType::Samples(_) => { + return Err(invalid("an instant selection reads evaluated values")); + } + other => { + return Err(invalid(&format!( + "an instant selection consumes raw samples, not {}", + other.describe() + ))); + } + } + ensure_samples(&self.output, "an instant selection")?; + // A selection is one source row per grid point: `select_instants` picks row + // indices and every field is taken at them, so it carries one value per input + // field. + ensure_same_fields(&input.output, &self.output, "an instant selection")?; + ensure_grid(&self.output, &op.output_grid, "an instant selection")?; + // `select_instants` reads one series at a time and holds no cross-series state, so + // an interleaved input is enough for it: no ordering check here. + } + NativeExpr::Window { input, op } => { + input.validate()?; + // A window reads a range of samples per grid point, so it consumes samples: state + // is not a sample stream and a scalar has no timestamps. + let NativeType::Samples(schema) = &input.output else { + return Err(invalid(&format!( + "a window consumes samples, not {}", + input.output.describe() + ))); + }; + // A range that is not positive reads nothing at every grid point. + if op.window.range_ms <= 0 { + return Err(invalid( + "a window reads an empty range: its range must be positive", + )); + } + // A range evaluator consumes one series to its last chunk before it starts the + // next, so interleaved chunks of different series cannot be fed to it. Only an + // input node carries an ordering, so only there is there one to check. + if let NativeExpr::Input(binding) = &input.expr + && binding.ordering == SeriesOrdering::Interleaved + { + return Err(invalid( + "a window reads one series to its last chunk before the next, so \ + interleaved chunks cannot be fed to it", + )); + } + // What is checked here and just below is that the declared input grid equals the + // input's grid, and that the output grid matches the declared one. The grid is + // not checked to cover the window's read range + // `(out.start - offset - range, out.end - offset]`: only a subquery feeds an + // evaluated input to a window, and this compiler does not accept subqueries yet, + // so that coverage check waits for it. + match (op.input_grid.as_ref(), schema.eval_grid.as_ref()) { + (None, None) => {} + (Some(bound), Some(actual)) if bound.spec() == actual.spec() => {} + _ => { + return Err(invalid( + "a window's declared input grid is not the input's grid", + )); + } + } + ensure_samples(&self.output, "a window")?; + ensure_same_fields(&input.output, &self.output, "a window")?; + ensure_grid(&self.output, &op.output_grid, "a window")?; + } + NativeExpr::Map { input, op } => { + input.validate()?; + // A map is point-wise over an evaluated vector: raw samples have no shared + // positions to be point-wise over, and a scalar is not a vector. + let Some((_, input_grid)) = evaluated_vector(&input.output) else { + return Err(invalid(&format!( + "a map consumes an evaluated vector, not {}", + input.output.describe() + ))); + }; + if op.ops.is_empty() { + return Err(invalid("a map holds no operation")); + } + ensure_samples(&self.output, "a map")?; + ensure_same_fields(&input.output, &self.output, "a map")?; + ensure_grid(&self.output, input_grid, "a map")?; + } + NativeExpr::Aggregate { input, op } => { + input.validate()?; + // A stage's contract is a pair of types, not only what it produces: `Partial` + // turns a vector into state and `Final` turns state back into a vector, so state + // never sits where a value is expected and a value never sits where state is. + match op.stage { + AggregateStage::Complete => { + // Every contribution lies within this execution: vector in, vector out, + // both on the input's grid, one value per input field. + let Some((_, input_grid)) = evaluated_vector(&input.output) else { + return Err(invalid(&format!( + "a complete aggregate consumes an evaluated vector, not {}", + input.output.describe() + ))); + }; + ensure_samples(&self.output, "a complete aggregate")?; + ensure_same_fields(&input.output, &self.output, "a complete aggregate")?; + ensure_grid(&self.output, input_grid, "a complete aggregate")?; + } + AggregateStage::Partial => { + // Only this execution's contributions were seen, so the output is state + // for a later merge: on the input's grid, grouped the same way, one + // accumulator per input field. + let Some((input_schema, input_grid)) = evaluated_vector(&input.output) + else { + return Err(invalid(&format!( + "a partial aggregate consumes an evaluated vector, not {}", + input.output.describe() + ))); + }; + let NativeType::AggregateState(state) = &self.output else { + return Err(invalid(&format!( + "a partial aggregate produces aggregate state, not {}", + self.output.describe() + ))); + }; + ensure_field_count( + state.accumulators.len(), + input_schema.fields.len(), + "a partial aggregate", + )?; + if state.grid.spec() != input_grid.spec() { + return Err(invalid( + "aggregate state is indexed on a grid the input does not produce", + )); + } + if state.group != op.group { + return Err(invalid( + "aggregate state groups differ from the binding: state built for \ + one `by` or `without` cannot be merged into another", + )); + } + } + AggregateStage::Final => { + // The state was collected from every contributing execution, so the + // output is the complete value on the grid that state is indexed on. + let NativeType::AggregateState(state) = &input.output else { + return Err(invalid(&format!( + "a final aggregate consumes aggregate state, not {}", + input.output.describe() + ))); + }; + if state.group != op.group { + return Err(invalid( + "aggregate state groups differ from the binding: state built for \ + one `by` or `without` cannot be merged into another", + )); + } + let output = ensure_samples(&self.output, "a final aggregate")?; + // The merged value carries one value per accumulator the state holds. + ensure_field_count( + output.fields.len(), + state.accumulators.len(), + "a final aggregate", + )?; + ensure_grid(&self.output, &state.grid, "a final aggregate")?; + } + } + } + NativeExpr::Binary { left, right, .. } => { + left.validate()?; + right.validate()?; + // A binary operation is point-wise, so both sides are evaluated on the statement's + // grid: state has no positions at all, and raw samples carry their own timestamps + // instead of positions on a shared grid. + let Some(left_grid) = left.output.grid() else { + return Err(invalid(&format!( + "a binary operation consumes evaluated values, not {} on its left", + left.output.describe() + ))); + }; + let Some(right_grid) = right.output.grid() else { + return Err(invalid(&format!( + "a binary operation consumes evaluated values, not {} on its right", + right.output.describe() + ))); + }; + if left_grid.spec() != right_grid.spec() { + return Err(invalid("a binary operation aligns two different grids")); + } + match (&left.output, &right.output) { + // Two scalars make a scalar: there is nothing to match and nothing to keep. + (NativeType::Scalar(_), NativeType::Scalar(_)) => { + if !matches!(&self.output, NativeType::Scalar(grid) if grid.spec() == left_grid.spec()) + { + return Err(invalid( + "two scalars do not produce a scalar on the statement's grid", + )); + } + } + // A scalar side carries no labels and no fields, so the operation is a + // broadcast on the statement's grid and `matching` is not consulted. A scalar + // has no fields of its own, so the broadcast keeps the vector side's fields. + (NativeType::Scalar(_), NativeType::Samples(vector)) + | (NativeType::Samples(vector), NativeType::Scalar(_)) => { + let Some(output) = self.output.samples() else { + return Err(invalid(&format!( + "a scalar broadcast produces the vector side's samples, not {}", + self.output.describe() + ))); + }; + ensure_grid(&self.output, left_grid, "a scalar broadcast")?; + if output.fields.len() != vector.fields.len() { + return Err(invalid( + "a scalar broadcast does not produce the vector side's fields", + )); + } + } + // Two vectors are matched by `matching` on the grid they share, and the match + // pairs fields by position, so it keeps the shorter side's fields. + (NativeType::Samples(left_vector), NativeType::Samples(right_vector)) => { + let Some(output) = self.output.samples() else { + return Err(invalid(&format!( + "a binary operation over two vectors produces samples, not {}", + self.output.describe() + ))); + }; + ensure_field_count( + output.fields.len(), + left_vector.fields.len().min(right_vector.fields.len()), + "a binary operation over two vectors", + )?; + ensure_grid(&self.output, left_grid, "a binary operation")?; + } + // State has no grid, so the grid checks above return for either side before + // this match and these two arms cannot be reached at run time. They are + // spelled out because `NativeType` names `AggregateState` and the match has to + // cover the pair; the message says so rather than pretending a value arrived. + (NativeType::AggregateState(_), _) | (_, NativeType::AggregateState(_)) => { + return Err(invalid( + "a binary operation reached aggregate state, which has no grid: the \ + grid check above should have rejected it", + )); + } + } + } + } + Ok(()) + } +} + +/// The schema and grid of an evaluated vector, or `None` when `ty` is raw samples, a scalar or +/// state. +/// +/// An operation over values needs both halves: that its input is samples at all, which rules out +/// a scalar, and that those samples are stamped onto a grid, which rules out raw samples. The +/// schema comes back with the grid because a consumer that needs the grid also declares a field +/// layout for the fields the schema carries. +fn evaluated_vector(ty: &NativeType) -> Option<(&Arc, &Arc)> { + let schema = ty.samples().filter(|schema| schema.eval_grid.is_some())?; + Some((schema, schema.eval_grid.as_ref()?)) +} + +/// Checks that a node declares the field layout its input produces. +/// +/// A window, a map, an instant selection and a complete aggregate all report one value per input +/// field, so they keep the layout of their input. A node that declares a different number of +/// fields than it consumes describes a computation this model has no meaning for, and letting it +/// through would only surface at run time as a field count mismatch against a schema the tree was +/// supposed to have validated. +fn ensure_same_fields(input: &NativeType, output: &NativeType, what: &str) -> Result<()> { + let (NativeType::Samples(input), NativeType::Samples(output)) = (input, output) else { + return Ok(()); + }; + ensure_field_count(output.fields.len(), input.fields.len(), what) +} + +/// Checks that a node declares `declared` fields where its input provides `expected`. +/// +/// Some nodes compute their layout instead of keeping their input's: a final aggregate produces one +/// value per accumulator the state holds, and a vector-vector binary operation produces one field +/// per matched pair, which is the shorter side's count. Both are checked against the count the +/// kernel produces, so the message names the node and both numbers. +fn ensure_field_count(declared: usize, expected: usize, what: &str) -> Result<()> { + if declared != expected { + return Err(invalid(&format!( + "{what} declares {declared} field(s) but its input provides {expected}" + ))); + } + Ok(()) +} + +/// Checks that `ty` produces samples and returns them. +fn ensure_samples<'a>(ty: &'a NativeType, what: &str) -> Result<&'a Arc> { + ty.samples() + .ok_or_else(|| invalid(&format!("{what} does not produce samples"))) +} + +/// Checks that `ty` is evaluated on the grid `expected` describes. +/// +/// The comparison is by [`EvalGrid::spec`], not by allocation: that is the rule the kernels apply +/// (`vector_match::same_grid`), so a compiler that allocates one grid per window but binds equal +/// specs produces a tree the kernel runs correctly. +fn ensure_grid(ty: &NativeType, expected: &Arc, what: &str) -> Result<()> { + match ty.grid() { + Some(actual) if actual.spec() == expected.spec() => Ok(()), + Some(_) => Err(invalid(&format!( + "{what} declares a grid its output does not carry" + ))), + None => Err(invalid(&format!("{what} produces values without a grid"))), + } +} + +fn invalid(reason: &str) -> DataFusionError { + // A rejected tree is a compiler defect, not a query error: the compiler only builds a node + // after deciding that this combination is supported. `Internal` keeps that meaning on the wire + // instead of reporting a mistake in the client's statement, which `Plan` would. + DataFusionError::Internal(reason.to_string()) +} + +#[cfg(test)] +mod tests; diff --git a/src/promql/src/native/plan/tests.rs b/src/promql/src/native/plan/tests.rs new file mode 100644 index 00000000000..85be7426a58 --- /dev/null +++ b/src/promql/src/native/plan/tests.rs @@ -0,0 +1,1187 @@ +// 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. + +use std::sync::Arc; + +use datatypes::series::SeriesOrdering; +use datatypes::series::samples::{FieldSpec, SeriesSchema}; +use datatypes::series::time::{EvalGrid, GridSpec}; + +use super::{ + AggregateKind, AggregateStage, BoundAggregate, BoundInput, BoundInstant, BoundMap, BoundScalar, + BoundWindow, Grouping, NativeExpr, NativeNode, NativeTree, NativeType, +}; +use crate::map::MathOp; +use crate::range_eval::{RangeFunction, RangeWindow}; +use crate::vector_match::{BinaryMatching, BinaryOp, Cardinality, MatchingLabels}; + +fn grid(start: i64, step: i64, len: u64) -> Arc { + Arc::new(EvalGrid::try_new(GridSpec::try_new(start, step, len).unwrap()).unwrap()) +} + +fn evaluated( + schema_fields: Arc<[datatypes::series::samples::FieldSpec]>, + grid: Arc, +) -> NativeType { + NativeType::Samples(Arc::new(SeriesSchema { + fields: schema_fields, + eval_grid: Some(grid), + })) +} + +/// `count` fields named `f0`, `f1`, ..., which is the layout a multi-field source has. +fn fields(count: usize) -> Arc<[FieldSpec]> { + (0..count) + .map(|index| FieldSpec { + name: Arc::from(format!("f{index}")), + }) + .collect::>() + .into() +} + +fn raw() -> NativeType { + NativeType::Samples(Arc::new(SeriesSchema { + fields: Arc::new([]), + eval_grid: None, + })) +} + +fn input() -> NativeNode { + let output = raw(); + NativeNode { + expr: NativeExpr::Input(BoundInput { + output: output.clone(), + ordering: SeriesOrdering::KeyThenTime, + }), + output, + } +} + +fn input_on(grid: Arc) -> NativeNode { + // One schema allocation shared by the binding and the output: `validate` checks that the output + // is the binding it was bound to, which is about the binding itself, not the grid's spec. + let output = evaluated(Arc::new([]), grid); + NativeNode { + expr: NativeExpr::Input(BoundInput { + output: output.clone(), + ordering: SeriesOrdering::Contiguous, + }), + output, + } +} + +/// A scan of `count` fields, so a consumer's field check has something to disagree with. +fn input_with_fields(count: usize) -> NativeNode { + let output = NativeType::Samples(Arc::new(SeriesSchema { + fields: fields(count), + eval_grid: None, + })); + NativeNode { + expr: NativeExpr::Input(BoundInput { + output: output.clone(), + ordering: SeriesOrdering::KeyThenTime, + }), + output, + } +} + +/// A constant scalar, which is a scalar type evaluated on the statement's grid. +fn scalar_on(value: f64, grid: Arc) -> NativeNode { + NativeNode { + expr: NativeExpr::Scalar(BoundScalar { value }), + output: NativeType::Scalar(grid), + } +} + +/// The grid a state node is indexed on. +fn state_grid(node: &NativeNode) -> Arc { + let NativeType::AggregateState(schema) = &node.output else { + unreachable!("expected aggregate state") + }; + Arc::clone(&schema.grid) +} + +fn partial_aggregate(inner: NativeNode) -> NativeNode { + let grid = inner + .output + .grid() + .expect("a partial aggregate consumes an evaluated vector") + .clone(); + NativeNode { + expr: NativeExpr::Aggregate { + input: Box::new(inner), + op: BoundAggregate { + kind: AggregateKind::Sum, + group: Grouping::By(Arc::new(["job".into()])), + stage: AggregateStage::Partial, + }, + }, + output: NativeType::AggregateState(Arc::new(super::AggregateStateSchema { + group: Grouping::By(Arc::new(["job".into()])), + accumulators: Arc::new([]), + grid, + })), + } +} + +fn final_aggregate(state: NativeNode) -> NativeNode { + let grid = state_grid(&state); + NativeNode { + expr: NativeExpr::Aggregate { + input: Box::new(state), + op: BoundAggregate { + kind: AggregateKind::Sum, + group: Grouping::By(Arc::new(["job".into()])), + stage: AggregateStage::Final, + }, + }, + output: evaluated(Arc::new([]), grid), + } +} + +fn window_over(inner: NativeNode, input_grid: Option>) -> NativeNode { + window_on(inner, input_grid, grid(0, 60_000, 4)) +} + +/// A window over `inner` that produces `count` fields on `out`, so a consumer of it can be given a +/// layout the window itself holds legally. +fn window_fields(inner: NativeNode, out: Arc, count: usize) -> NativeNode { + NativeNode { + expr: NativeExpr::Window { + input: Box::new(inner), + op: BoundWindow { + function: RangeFunction::Rate, + window: RangeWindow { + range_ms: 300_000, + offset_ms: 0, + anchor_ms: None, + }, + input_grid: None, + output_grid: Arc::clone(&out), + }, + }, + output: evaluated(fields(count), out), + } +} + +/// A window over `inner` that produces `out`, so two windows can be given the same grid. +fn window_on( + inner: NativeNode, + input_grid: Option>, + out: Arc, +) -> NativeNode { + NativeNode { + expr: NativeExpr::Window { + input: Box::new(inner), + op: BoundWindow { + function: RangeFunction::Rate, + window: RangeWindow { + range_ms: 300_000, + offset_ms: 0, + anchor_ms: None, + }, + input_grid, + output_grid: Arc::clone(&out), + }, + }, + output: evaluated(Arc::new([]), out), + } +} + +fn map_over(inner: NativeNode) -> NativeNode { + let out = inner.output.grid().expect("a map needs a grid").clone(); + NativeNode { + expr: NativeExpr::Map { + input: Box::new(inner), + op: BoundMap { + ops: Arc::new([MathOp::Abs]), + }, + }, + output: evaluated(Arc::new([]), out), + } +} + +fn add() -> BinaryMatching { + BinaryMatching { + op: BinaryOp::Add, + return_bool: false, + labels: MatchingLabels::Ignoring(Vec::new()), + cardinality: Cardinality::OneToOne, + } +} + +/// A compiled tree is accepted when every node's contract holds. +#[test] +fn a_window_over_raw_samples_is_valid() { + let root = window_over(input(), None); + assert!(NativeTree::try_new(root).is_ok()); +} + +/// Adding a map to a window changes the tree and nothing else: no new shape is recognised. +#[test] +fn adding_a_map_only_changes_the_tree() { + let bare = window_over(input(), None); + let mapped = map_over(window_over(input(), None)); + // Both trees are legal, so the comparison below is about the shape and not about both failing. + assert!(bare.validate().is_ok(), "{}", bare.validate().unwrap_err()); + assert!( + mapped.validate().is_ok(), + "{}", + mapped.validate().unwrap_err() + ); + // The mapped tree has one more node, and it is the same window below it. + let mut depth = 0; + super::super::metrics::walk(&mapped, &mut |_, _| depth += 1); + assert_eq!(depth, 3); + let NativeExpr::Map { input, .. } = &mapped.expr else { + unreachable!("the mapped tree's root is a map") + }; + assert!(matches!(input.expr, NativeExpr::Window { .. })); +} + +/// A map is point-wise over values, so raw samples are not something it can consume. +#[test] +fn a_map_cannot_consume_raw_samples() { + let node = NativeNode { + expr: NativeExpr::Map { + input: Box::new(input()), + op: BoundMap { + ops: Arc::new([MathOp::Abs]), + }, + }, + output: evaluated(Arc::new([]), grid(0, 60_000, 4)), + }; + let error = node.validate().unwrap_err(); + assert!(error.to_string().contains("an evaluated vector"), "{error}"); +} + +/// A scalar is one value per grid point, not a vector, so there are no fields to map. +#[test] +fn a_map_cannot_consume_a_scalar() { + let out = grid(0, 60_000, 4); + let node = NativeNode { + expr: NativeExpr::Map { + input: Box::new(scalar_on(1.0, Arc::clone(&out))), + op: BoundMap { + ops: Arc::new([MathOp::Abs]), + }, + }, + output: evaluated(Arc::new([]), out), + }; + let error = node.validate().unwrap_err(); + assert!(error.to_string().contains("not a scalar"), "{error}"); +} + +/// A map with no operation would be a node that does nothing, which is a compiler mistake. +#[test] +fn a_map_needs_an_operation() { + let inner = window_over(input(), None); + let out = inner.output.grid().unwrap().clone(); + let node = NativeNode { + expr: NativeExpr::Map { + input: Box::new(inner), + op: BoundMap { ops: Arc::new([]) }, + }, + output: evaluated(Arc::new([]), out), + }; + let error = node.validate().unwrap_err(); + assert!(error.to_string().contains("no operation"), "{error}"); +} + +/// A map reports one value per input field, so it cannot declare a layout its input does not +/// have. Without this check the tree would validate and only fail when the runtime compared the +/// node's schema against the one its input actually produces. +#[test] +fn a_map_cannot_change_the_field_count() { + let inner = window_over(input(), None); + let out = inner.output.grid().unwrap().clone(); + let node = NativeNode { + expr: NativeExpr::Map { + input: Box::new(inner), + op: BoundMap { + ops: Arc::new([MathOp::Abs]), + }, + }, + output: evaluated( + Arc::new([datatypes::series::samples::FieldSpec { + name: Arc::from("value"), + }]), + out, + ), + }; + let error = node.validate().unwrap_err(); + assert!( + error + .to_string() + .contains("a map declares 1 field(s) but its input provides 0"), + "{error}" + ); +} + +/// A window reports one value per input field, so it carries the same rule as a map. +#[test] +fn a_window_cannot_change_the_field_count() { + let inner = window_over(input(), None); + // The output grid is the input's, so the grid check passes and the field check is what the + // test actually exercises. + let out = inner.output.grid().unwrap().clone(); + let node = NativeNode { + expr: NativeExpr::Window { + input: Box::new(inner), + op: BoundWindow { + function: RangeFunction::Rate, + window: RangeWindow { + range_ms: 30_000, + offset_ms: 0, + anchor_ms: None, + }, + input_grid: Some(Arc::clone(&out)), + output_grid: Arc::clone(&out), + }, + }, + output: evaluated( + Arc::new([datatypes::series::samples::FieldSpec { + name: Arc::from("value"), + }]), + out, + ), + }; + let error = node.validate().unwrap_err(); + assert!( + error + .to_string() + .contains("a window declares 1 field(s) but its input provides 0"), + "{error}" + ); +} + +/// An instant selection picks one row of the source per grid point, so it carries one value per +/// input field. A tree that declares anything else would only fail at run time, against a schema +/// the tree was supposed to have validated. +#[test] +fn an_instant_selection_cannot_change_the_field_count() { + let out = grid(0, 60_000, 4); + // The source has two fields; the selection declares one value. + let node = NativeNode { + expr: NativeExpr::Instant { + input: Box::new(input_with_fields(2)), + op: BoundInstant { + lookback_ms: 300_000, + offset_ms: 0, + output_grid: Arc::clone(&out), + }, + }, + output: evaluated(fields(1), out), + }; + let error = node.validate().unwrap_err(); + assert!( + error + .to_string() + .contains("an instant selection declares 1 field(s) but its input provides 2"), + "{error}" + ); +} + +/// An instant selection reads raw samples; feeding it evaluated values would re-anchor a grid +/// that is already fixed. +#[test] +fn an_instant_selection_reads_raw_samples() { + let out = grid(0, 60_000, 4); + let node = NativeNode { + expr: NativeExpr::Instant { + input: Box::new(window_over(input(), None)), + op: BoundInstant { + lookback_ms: 300_000, + offset_ms: 0, + output_grid: Arc::clone(&out), + }, + }, + output: evaluated(Arc::new([]), out), + }; + let error = node.validate().unwrap_err(); + assert!(error.to_string().contains("reads evaluated"), "{error}"); +} + +/// Aggregate state is not a sample stream, so an instant selection cannot read it. +#[test] +fn an_instant_selection_cannot_consume_state() { + let out = grid(0, 60_000, 4); + let node = NativeNode { + expr: NativeExpr::Instant { + input: Box::new(partial_aggregate(window_on( + input(), + None, + Arc::clone(&out), + ))), + op: BoundInstant { + lookback_ms: 300_000, + offset_ms: 0, + output_grid: Arc::clone(&out), + }, + }, + output: evaluated(Arc::new([]), out), + }; + let error = node.validate().unwrap_err(); + assert!(error.to_string().contains("aggregate state"), "{error}"); +} + +/// A window's declared input grid has to be the grid its input actually produces. +#[test] +fn a_window_checks_its_declared_input_grid() { + let inner_grid = grid(0, 60_000, 4); + let other_grid = grid(0, 30_000, 8); + // The input produces `inner_grid`, but the window claims a different one. + let node = window_over(input_on(inner_grid), Some(other_grid)); + let error = node.validate().unwrap_err(); + assert!( + error + .to_string() + .contains("input grid is not the input's grid"), + "{error}" + ); +} + +/// A range evaluator reads one raw series to its last chunk before it starts the next, so raw +/// chunks of different series may not interleave. +#[test] +fn a_window_cannot_read_interleaved_input() { + let output = raw(); + let interleaved = NativeNode { + expr: NativeExpr::Input(BoundInput { + output: output.clone(), + ordering: SeriesOrdering::Interleaved, + }), + output, + }; + let error = window_over(interleaved, None).validate().unwrap_err(); + assert!( + error + .to_string() + .contains("interleaved chunks cannot be fed to it"), + "{error}" + ); +} + +/// A window whose range is empty or negative reads nothing at every grid point. +#[test] +fn a_window_reads_a_non_empty_range() { + let mut node = window_over(input(), None); + let NativeExpr::Window { op, .. } = &mut node.expr else { + unreachable!("a window tree has a window root") + }; + op.window.range_ms = 0; + let error = node.validate().unwrap_err(); + assert!(error.to_string().contains("empty range"), "{error}"); +} + +/// A node that declares an output grid its output does not carry would be addressed on one grid +/// and hand out positions on another. +#[test] +fn a_window_checks_its_output_grid() { + let mut node = window_over(input(), None); + let NativeExpr::Window { op, .. } = &mut node.expr else { + unreachable!("a window tree has a window root") + }; + op.output_grid = grid(0, 30_000, 8); + let error = node.validate().unwrap_err(); + assert!( + error + .to_string() + .contains("declares a grid its output does not carry"), + "{error}" + ); +} + +/// A window consumes samples: state holds accumulators, not a sample stream to range over. +#[test] +fn a_window_cannot_consume_state() { + let out = grid(0, 60_000, 4); + let node = window_on( + partial_aggregate(window_on(input(), None, Arc::clone(&out))), + None, + grid(0, 60_000, 4), + ); + let error = node.validate().unwrap_err(); + assert!(error.to_string().contains("aggregate state"), "{error}"); +} + +/// A window ranges over timestamps, and a scalar has none of its own. +#[test] +fn a_window_cannot_consume_a_scalar() { + let out = grid(0, 60_000, 4); + let node = window_on( + scalar_on(1.0, Arc::clone(&out)), + Some(out), + grid(0, 60_000, 4), + ); + let error = node.validate().unwrap_err(); + assert!(error.to_string().contains("not a scalar"), "{error}"); +} + +/// A binary node aligns two inputs, so two different grids cannot be aligned implicitly. +#[test] +fn a_binary_operation_rejects_two_grids() { + let left = window_over(input(), None); + let left_grid = left.output.grid().unwrap().clone(); + let right = window_on(input(), None, grid(0, 30_000, 8)); + let node = NativeNode { + expr: NativeExpr::Binary { + left: Box::new(left), + right: Box::new(right), + matching: add(), + }, + output: evaluated(Arc::new([]), left_grid), + }; + let error = node.validate().unwrap_err(); + assert!(error.to_string().contains("different grids"), "{error}"); +} + +/// Two inputs on the same grid are a legal binary operation, which is how +/// `rate(a[5m]) + rate(b[5m])` becomes an ordinary tree. +#[test] +fn a_binary_operation_over_one_grid_is_valid() { + // Both sides evaluate on the statement's grid, so the compiler binds one grid to both. + let shared = grid(0, 60_000, 4); + let node = NativeNode { + expr: NativeExpr::Binary { + left: Box::new(window_on(input(), None, Arc::clone(&shared))), + right: Box::new(window_on(input(), None, Arc::clone(&shared))), + matching: add(), + }, + output: evaluated(Arc::new([]), shared), + }; + assert!(node.validate().is_ok()); +} + +/// A compiler that allocates one grid per window still produces one grid: the kernel aligns +/// operands by `EvalGrid::spec` (`vector_match::same_grid`), so `validate` compares specs too and +/// does not reject a tree the runtime would run correctly. +#[test] +fn a_binary_operation_aligns_separately_allocated_equal_specs() { + // Two windows, one fresh `EvalGrid` each, and the statement's grid a third: equal specs, no + // shared allocation anywhere in the tree. + let node = NativeNode { + expr: NativeExpr::Binary { + left: Box::new(window_on(input(), None, grid(0, 60_000, 4))), + right: Box::new(window_on(input(), None, grid(0, 60_000, 4))), + matching: add(), + }, + output: evaluated(Arc::new([]), grid(0, 60_000, 4)), + }; + assert!(node.validate().is_ok(), "{}", node.validate().unwrap_err()); + + // The same tree with a different spec is still rejected: the kernel rejects that pair too. + let node = NativeNode { + expr: NativeExpr::Binary { + left: Box::new(window_on(input(), None, grid(0, 60_000, 4))), + right: Box::new(window_on(input(), None, grid(30_000, 60_000, 4))), + matching: add(), + }, + output: evaluated(Arc::new([]), grid(0, 60_000, 4)), + }; + let error = node.validate().unwrap_err(); + assert!(error.to_string().contains("different grids"), "{error}"); +} + +/// A scalar side is broadcast, not matched: `sum by(job)(rate(x[5m])) + time()` has no labels to +/// line up, so `matching` is not consulted and the output keeps the vector side's fields. +#[test] +fn a_binary_operation_broadcasts_a_scalar() { + let shared = grid(0, 60_000, 4); + let vector = window_on(input(), None, Arc::clone(&shared)); + let node = NativeNode { + expr: NativeExpr::Binary { + left: Box::new(vector), + right: Box::new(scalar_on(1.0, Arc::clone(&shared))), + matching: add(), + }, + output: evaluated(Arc::new([]), Arc::clone(&shared)), + }; + assert!(node.validate().is_ok()); + + // The mirror case: the scalar on the left keeps the right side's fields. + let node = NativeNode { + expr: NativeExpr::Binary { + left: Box::new(scalar_on(1.0, Arc::clone(&shared))), + right: Box::new(window_on(input(), None, Arc::clone(&shared))), + matching: add(), + }, + output: evaluated(Arc::new([]), Arc::clone(&shared)), + }; + assert!(node.validate().is_ok()); +} + +/// A scalar has no fields of its own, so a broadcast that declares a different count than the +/// vector side would report a layout the kernel does not produce. +#[test] +fn a_scalar_broadcast_cannot_change_the_field_count() { + let shared = grid(0, 60_000, 4); + // The vector side has two fields; the broadcast declares one. + let node = NativeNode { + expr: NativeExpr::Binary { + left: Box::new(window_fields(input_with_fields(2), Arc::clone(&shared), 2)), + right: Box::new(scalar_on(1.0, Arc::clone(&shared))), + matching: add(), + }, + output: evaluated(fields(1), shared), + }; + let error = node.validate().unwrap_err(); + assert!( + error + .to_string() + .contains("does not produce the vector side's fields"), + "{error}" + ); +} + +/// Two vectors pair their fields by position, so the match reports the shorter side's count +/// (`vector_match::binary_output_schema` takes `min(lhs, rhs)`); a tree declaring anything else is +/// rejected instead of failing at run time. +#[test] +fn a_binary_operation_over_two_vectors_matches_the_shorter_side() { + let shared = grid(0, 60_000, 4); + // Two fields matched against one: the match produces one field, not two. + let node = NativeNode { + expr: NativeExpr::Binary { + left: Box::new(window_fields(input_with_fields(2), Arc::clone(&shared), 2)), + right: Box::new(window_fields(input_with_fields(1), Arc::clone(&shared), 1)), + matching: add(), + }, + output: evaluated(fields(2), Arc::clone(&shared)), + }; + let error = node.validate().unwrap_err(); + assert!( + error.to_string().contains( + "a binary operation over two vectors declares 2 field(s) but its input provides 1" + ), + "{error}" + ); + + // Declaring the shorter side's count is the legal tree, so the check is not vacuous. + let node = NativeNode { + expr: NativeExpr::Binary { + left: Box::new(window_fields(input_with_fields(2), Arc::clone(&shared), 2)), + right: Box::new(window_fields(input_with_fields(1), Arc::clone(&shared), 1)), + matching: add(), + }, + output: evaluated(fields(1), shared), + }; + assert!(node.validate().is_ok(), "{}", node.validate().unwrap_err()); +} + +/// Two scalars make a scalar, because there is nothing to attach labels or fields to. +#[test] +fn two_scalars_make_a_scalar() { + let shared = grid(0, 60_000, 4); + let node = NativeNode { + expr: NativeExpr::Binary { + left: Box::new(scalar_on(1.0, Arc::clone(&shared))), + right: Box::new(scalar_on(2.0, Arc::clone(&shared))), + matching: add(), + }, + output: NativeType::Scalar(Arc::clone(&shared)), + }; + assert!(node.validate().is_ok()); +} + +/// Two scalars have nothing to attach labels or fields to, so they cannot produce a vector. +#[test] +fn two_scalars_cannot_produce_a_vector() { + let shared = grid(0, 60_000, 4); + let node = NativeNode { + expr: NativeExpr::Binary { + left: Box::new(scalar_on(1.0, Arc::clone(&shared))), + right: Box::new(scalar_on(2.0, Arc::clone(&shared))), + matching: add(), + }, + output: evaluated(Arc::new([]), shared), + }; + let error = node.validate().unwrap_err(); + assert!( + error + .to_string() + .contains("two scalars do not produce a scalar"), + "{error}" + ); +} + +/// A binary operation is point-wise on a grid, so a side of raw samples has no positions to line +/// up with the other side. +#[test] +fn a_binary_operation_cannot_consume_raw_samples() { + let shared = grid(0, 60_000, 4); + let vector = || window_on(input(), None, Arc::clone(&shared)); + + let left_raw = NativeNode { + expr: NativeExpr::Binary { + left: Box::new(input()), + right: Box::new(vector()), + matching: add(), + }, + output: evaluated(Arc::new([]), Arc::clone(&shared)), + }; + let error = left_raw.validate().unwrap_err(); + assert!( + error.to_string().contains("not raw samples on its left"), + "{error}" + ); + + let right_raw = NativeNode { + expr: NativeExpr::Binary { + left: Box::new(vector()), + right: Box::new(input()), + matching: add(), + }, + output: evaluated(Arc::new([]), shared), + }; + let error = right_raw.validate().unwrap_err(); + assert!( + error.to_string().contains("not raw samples on its right"), + "{error}" + ); +} + +/// State is not a value, so neither side of a binary operation may be state. +#[test] +fn a_binary_operation_cannot_consume_state() { + let shared = grid(0, 60_000, 4); + let node = NativeNode { + expr: NativeExpr::Binary { + left: Box::new(window_on(input(), None, Arc::clone(&shared))), + right: Box::new(partial_aggregate(window_on( + input(), + None, + Arc::clone(&shared), + ))), + matching: add(), + }, + output: evaluated(Arc::new([]), Arc::clone(&shared)), + }; + let error = node.validate().unwrap_err(); + assert!(error.to_string().contains("aggregate state"), "{error}"); +} + +/// A partial aggregate produces state, not a value; a complete one produces a value on the same +/// grid as its input. +#[test] +fn an_aggregate_output_matches_its_stage() { + let inner = window_over(input(), None); + let value_grid = inner.output.grid().unwrap().clone(); + + let complete = NativeNode { + expr: NativeExpr::Aggregate { + input: Box::new(inner.clone()), + op: BoundAggregate { + kind: AggregateKind::Sum, + group: Grouping::By(Arc::new(["job".into()])), + stage: AggregateStage::Complete, + }, + }, + output: evaluated(Arc::new([]), Arc::clone(&value_grid)), + }; + assert!(complete.validate().is_ok()); + + let partial = NativeNode { + expr: NativeExpr::Aggregate { + input: Box::new(inner), + op: BoundAggregate { + kind: AggregateKind::Sum, + group: Grouping::By(Arc::new(["job".into()])), + stage: AggregateStage::Partial, + }, + }, + output: NativeType::AggregateState(Arc::new(super::AggregateStateSchema { + group: Grouping::By(Arc::new(["job".into()])), + accumulators: Arc::new([]), + grid: value_grid, + })), + }; + assert!(partial.validate().is_ok()); +} + +/// A complete aggregate folds each field on its own, so it reports one value per input field +/// (`aggregate::aggregate_float` builds its rows from the input schema's fields). +#[test] +fn a_complete_aggregate_cannot_change_the_field_count() { + let out = grid(0, 60_000, 4); + // The input is a legal two-field window; the aggregate declares one value. + let node = NativeNode { + expr: NativeExpr::Aggregate { + input: Box::new(window_fields(input_with_fields(2), Arc::clone(&out), 2)), + op: BoundAggregate { + kind: AggregateKind::Sum, + group: Grouping::By(Arc::new(["job".into()])), + stage: AggregateStage::Complete, + }, + }, + output: evaluated(fields(1), out), + }; + let error = node.validate().unwrap_err(); + assert!( + error + .to_string() + .contains("a complete aggregate declares 1 field(s) but its input provides 2"), + "{error}" + ); +} + +/// Declaring a value where the stage produces state is a compiler mistake, and is rejected. +#[test] +fn an_aggregate_stage_must_agree_with_its_type() { + let inner = window_over(input(), None); + let value_grid = inner.output.grid().unwrap().clone(); + let node = NativeNode { + expr: NativeExpr::Aggregate { + input: Box::new(inner), + op: BoundAggregate { + kind: AggregateKind::Sum, + group: Grouping::By(Arc::new(["job".into()])), + stage: AggregateStage::Partial, + }, + }, + output: evaluated(Arc::new([]), value_grid), + }; + let error = node.validate().unwrap_err(); + assert!( + error.to_string().contains("produces aggregate state"), + "{error}" + ); +} + +/// A `Partial` aggregate keeps one accumulator per input field (`aggregate_float` lays its state +/// out in `schema.fields.len()`-sized points), so state declaring another count is rejected. +#[test] +fn a_partial_aggregate_cannot_change_the_accumulator_count() { + let out = grid(0, 60_000, 4); + // The input is a legal two-field window; the state declares one accumulator. + let node = NativeNode { + expr: NativeExpr::Aggregate { + input: Box::new(window_fields(input_with_fields(2), Arc::clone(&out), 2)), + op: BoundAggregate { + kind: AggregateKind::Sum, + group: Grouping::By(Arc::new(["job".into()])), + stage: AggregateStage::Partial, + }, + }, + output: NativeType::AggregateState(Arc::new(super::AggregateStateSchema { + group: Grouping::By(Arc::new(["job".into()])), + accumulators: fields(1), + grid: out, + })), + }; + let error = node.validate().unwrap_err(); + assert!( + error + .to_string() + .contains("a partial aggregate declares 1 field(s) but its input provides 2"), + "{error}" + ); +} + +/// A `Partial` aggregate's state is grouped by the binding's group labels, so state declaring +/// other labels is not the state this aggregate produces. +#[test] +fn a_partial_aggregate_groups_its_state_like_its_binding() { + let mut node = partial_aggregate(window_over(input(), None)); + let NativeExpr::Aggregate { op, .. } = &mut node.expr else { + unreachable!("a partial aggregate tree has an aggregate root") + }; + op.group = Grouping::By(Arc::new(["instance".into()])); + let error = node.validate().unwrap_err(); + assert!( + error.to_string().contains("groups differ from the binding"), + "{error}" + ); +} + +/// A `Partial` aggregate indexes its state on the grid its input produces, so state on another +/// grid is addressed with positions the input never wrote. +#[test] +fn a_partial_aggregate_indexes_its_state_on_the_inputs_grid() { + let node = NativeNode { + expr: NativeExpr::Aggregate { + input: Box::new(window_over(input(), None)), + op: BoundAggregate { + kind: AggregateKind::Sum, + group: Grouping::By(Arc::new(["job".into()])), + stage: AggregateStage::Partial, + }, + }, + output: NativeType::AggregateState(Arc::new(super::AggregateStateSchema { + group: Grouping::By(Arc::new(["job".into()])), + accumulators: Arc::new([]), + grid: grid(0, 30_000, 8), + })), + }; + let error = node.validate().unwrap_err(); + assert!( + error + .to_string() + .contains("indexed on a grid the input does not produce"), + "{error}" + ); +} + +/// A `Final` aggregate merges into the binding's group labels, so the state it reads has to be +/// grouped the same way. +#[test] +fn a_final_aggregate_groups_its_state_like_its_binding() { + let mut node = final_aggregate(partial_aggregate(window_over(input(), None))); + let NativeExpr::Aggregate { op, .. } = &mut node.expr else { + unreachable!("a final aggregate tree has an aggregate root") + }; + op.group = Grouping::By(Arc::new(["instance".into()])); + let error = node.validate().unwrap_err(); + assert!( + error.to_string().contains("groups differ from the binding"), + "{error}" + ); +} + +/// `by` and `without` are not interchangeable, even over the same label. +/// +/// `by(job)` keeps `job` and drops every other label; `without(job)` drops `job` and keeps the +/// rest. A binding that spelled `without` as a label list could only produce the first, so state +/// built for one has to be refused for the other rather than merged under the wrong labels. +#[test] +fn a_grouping_cannot_swap_by_for_without() { + let mut node = final_aggregate(partial_aggregate(window_over(input(), None))); + let NativeExpr::Aggregate { op, .. } = &mut node.expr else { + unreachable!("a final aggregate tree has an aggregate root") + }; + op.group = Grouping::Without(Arc::new(["job".into()])); + let error = node.validate().unwrap_err(); + assert!( + error.to_string().contains("groups differ from the binding"), + "{error}" + ); + + // The same labels under the same shape validate, so the refusal is about the shape. + let mut node = final_aggregate(partial_aggregate(window_over(input(), None))); + let NativeExpr::Aggregate { op, .. } = &mut node.expr else { + unreachable!("a final aggregate tree has an aggregate root") + }; + op.group = Grouping::By(Arc::new(["job".into()])); + assert!(node.validate().is_ok()); +} + +/// A `Final` aggregate consumes the state a `Partial` produced, which is what an exchange +/// boundary hands over between the two stages. +#[test] +fn a_final_aggregate_merges_partial_state() { + let shared = grid(0, 60_000, 4); + let state = partial_aggregate(window_on(input(), None, Arc::clone(&shared))); + let merged = final_aggregate(state); + assert!(merged.validate().is_ok()); + assert!( + matches!(&merged.output, NativeType::Samples(schema) if schema.eval_grid.as_ref().is_some_and(|grid| Arc::ptr_eq(grid, &shared))) + ); +} + +/// A `Final` aggregate merges the state into one value per accumulator, so it reports the state's +/// count and nothing else. +#[test] +fn a_final_aggregate_cannot_change_the_field_count() { + let out = grid(0, 60_000, 4); + // A legal partial aggregate: two input fields, two accumulators. + let state = NativeNode { + expr: NativeExpr::Aggregate { + input: Box::new(window_fields(input_with_fields(2), Arc::clone(&out), 2)), + op: BoundAggregate { + kind: AggregateKind::Sum, + group: Grouping::By(Arc::new(["job".into()])), + stage: AggregateStage::Partial, + }, + }, + output: NativeType::AggregateState(Arc::new(super::AggregateStateSchema { + group: Grouping::By(Arc::new(["job".into()])), + accumulators: fields(2), + grid: Arc::clone(&out), + })), + }; + assert!( + state.validate().is_ok(), + "{}", + state.validate().unwrap_err() + ); + + // The merge declares one value for the state's two accumulators. + let node = NativeNode { + expr: NativeExpr::Aggregate { + input: Box::new(state), + op: BoundAggregate { + kind: AggregateKind::Sum, + group: Grouping::By(Arc::new(["job".into()])), + stage: AggregateStage::Final, + }, + }, + output: evaluated(fields(1), out), + }; + let error = node.validate().unwrap_err(); + assert!( + error + .to_string() + .contains("a final aggregate declares 1 field(s) but its input provides 2"), + "{error}" + ); +} + +/// A `Final` aggregate consumes state, so a vector is not something it can merge. +#[test] +fn a_final_aggregate_cannot_consume_a_window() { + let inner = window_over(input(), None); + let value_grid = inner.output.grid().unwrap().clone(); + let node = NativeNode { + expr: NativeExpr::Aggregate { + input: Box::new(inner), + op: BoundAggregate { + kind: AggregateKind::Sum, + group: Grouping::By(Arc::new(["job".into()])), + stage: AggregateStage::Final, + }, + }, + output: evaluated(Arc::new([]), value_grid), + }; + let error = node.validate().unwrap_err(); + assert!( + error.to_string().contains("consumes aggregate state"), + "{error}" + ); +} + +/// A `Complete` aggregate aggregates vectors, so state is not something it can consume. +#[test] +fn a_complete_aggregate_cannot_consume_state() { + let state = partial_aggregate(window_over(input(), None)); + let state_grid = state_grid(&state); + let node = NativeNode { + expr: NativeExpr::Aggregate { + input: Box::new(state), + op: BoundAggregate { + kind: AggregateKind::Sum, + group: Grouping::By(Arc::new(["job".into()])), + stage: AggregateStage::Complete, + }, + }, + output: evaluated(Arc::new([]), state_grid), + }; + let error = node.validate().unwrap_err(); + assert!( + error.to_string().contains("consumes an evaluated vector"), + "{error}" + ); +} + +/// The first-phase walkthrough shapes compile to trees, which is the acceptance the RFC states +/// for composition. +#[test] +fn the_walkthrough_shapes_are_trees() { + // Every node in one query evaluates on the statement's grid; `validate` compares the specs of + // the grids, not their allocations, which is the rule the kernels apply. + let statement_grid = grid(0, 60_000, 4); + + // `abs(rate(x[5m]))` + let abs_rate = map_over(window_over(input(), None)); + assert!(abs_rate.validate().is_ok()); + + // `sum by(job)(rate(x[5m]))`, complete because one execution sees every contribution. + let sum = NativeNode { + expr: NativeExpr::Aggregate { + input: Box::new(window_on(input(), None, Arc::clone(&statement_grid))), + op: BoundAggregate { + kind: AggregateKind::Sum, + group: Grouping::By(Arc::new(["job".into()])), + stage: AggregateStage::Complete, + }, + }, + output: evaluated(Arc::new([]), Arc::clone(&statement_grid)), + }; + assert!(sum.validate().is_ok()); + + // `sum by(job)(rate(x[5m])) + 1`: the aggregate is complete, and the scalar side is + // broadcast, so `matching` is not consulted for it. + let plus_one = NativeNode { + expr: NativeExpr::Binary { + left: Box::new(sum.clone()), + right: Box::new(scalar_on(1.0, Arc::clone(&statement_grid))), + matching: add(), + }, + output: evaluated(Arc::new([]), statement_grid), + }; + assert!(plus_one.validate().is_ok()); +} + +/// A scalar node's output is the scalar type, which is what keeps a broadcast from turning into +/// label matching. +#[test] +fn a_scalar_node_is_valid() { + let node = scalar_on(1.0, grid(0, 60_000, 4)); + assert!(node.validate().is_ok()); +} + +/// A scalar node declared as a vector would let a broadcast be planned as a match. +#[test] +fn a_scalar_node_must_produce_a_scalar() { + let node = NativeNode { + expr: NativeExpr::Scalar(BoundScalar { value: 1.0 }), + output: evaluated(Arc::new([]), grid(0, 60_000, 4)), + }; + let error = node.validate().unwrap_err(); + assert!( + error.to_string().contains("does not produce a scalar"), + "{error}" + ); +} + +/// An input whose declared output is not the binding it was bound to cannot be trusted. +#[test] +fn an_input_must_report_the_binding_it_was_bound_to() { + let node = NativeNode { + expr: NativeExpr::Input(BoundInput { + output: raw(), + ordering: SeriesOrdering::KeyThenTime, + }), + output: raw(), + }; + let error = node.validate().unwrap_err(); + assert!(error.to_string().contains("not the binding"), "{error}"); +} + +/// An exchange boundary binds state, and the node has to report that same state. +#[test] +fn an_input_can_bind_aggregate_state() { + let state = partial_aggregate(window_over(input(), None)); + let bound = NativeNode { + expr: NativeExpr::Input(BoundInput { + output: state.output.clone(), + ordering: SeriesOrdering::Contiguous, + }), + output: state.output.clone(), + }; + assert!(bound.validate().is_ok()); + + // A `Final` aggregate can then consume it, which is the shape of a distributed aggregate. + assert!(final_aggregate(bound).validate().is_ok()); + + // A fresh state allocation is a different binding, so it cannot be reported as the bound one. + let other = NativeNode { + expr: NativeExpr::Input(BoundInput { + output: state.output.clone(), + ordering: SeriesOrdering::Contiguous, + }), + output: partial_aggregate(window_over(input(), None)).output, + }; + let error = other.validate().unwrap_err(); + assert!(error.to_string().contains("not the binding"), "{error}"); +} diff --git a/src/query/src/promql.rs b/src/query/src/promql.rs index db682ca6a89..424204b5d95 100644 --- a/src/query/src/promql.rs +++ b/src/query/src/promql.rs @@ -14,6 +14,7 @@ pub mod error; pub mod label_values; +pub mod native_compile; pub mod planner; use datafusion_common::tree_node::{TreeNode as _, TreeNodeRecursion}; diff --git a/src/query/src/promql/native_compile.rs b/src/query/src/promql/native_compile.rs new file mode 100644 index 00000000000..88e5023aa42 --- /dev/null +++ b/src/query/src/promql/native_compile.rs @@ -0,0 +1,466 @@ +// 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. + +//! Compiles a PromQL AST into a [`NativeTree`]. +//! +//! This is the front half of the native evaluation model: it reads the parsed statement and writes +//! down what it means as bound nodes. It runs nothing, and nothing calls it yet, so the subset it +//! accepts is deliberately three shapes: +//! +//! ```text +//! e.g. x +//! ([]) e.g. rate(x[5m]) +//! (, []) e.g. quantile_over_time(0.9, x[5m]) +//! ``` +//! +//! which become `Input` (raw samples) → `Instant` (the last sample within the statement's lookback) +//! and `Input` (raw samples) → `Window` (the range function over the statement's grid). A selector +//! may carry `offset` in either shape and, in the window shape, an `@` anchor. Everything else is a +//! [`DataFusionError::NotImplemented`] that names what was found and quotes the shapes above, +//! because rejecting a query the compiler does not understand is the only honest alternative to +//! compiling it into something else. +//! +//! The statement's grid, the selector's lookback, window, offset and anchor are the only numbers +//! derived here; the legality of the tree is not checked here. [`NativeTree::try_new`] is the +//! single authority on that, so a node added later cannot be legal in one place and illegal in +//! another. + +use std::sync::Arc; +use std::time::Duration; + +use datafusion::error::{DataFusionError, Result}; +use datatypes::series::SeriesOrdering; +use datatypes::series::samples::SeriesSchema; +use datatypes::series::time::{EvalGrid, GridSpec}; +use promql::native::plan::{ + BoundInput, BoundInstant, BoundWindow, NativeExpr, NativeNode, NativeTree, NativeType, +}; +use promql::range_eval::{RangeFunction, RangeWindow}; +use promql_parser::parser::{ + AtModifier, Call, EvalStmt, Expr as PromExpr, MatrixSelector, NumberLiteral, Offset, + VectorSelector, +}; + +use crate::promql::label_values::signed_millis_since_epoch; + +/// The shapes this compiler accepts, quoted in every rejection. +const SUPPORTED: &str = "a bare selector, a range function over a selector — \ + `([])` — or a parameterised one, \ + `(, [])`, for example `x`, \ + `rate(x[5m])` or `quantile_over_time(0.9, x[5m])`"; + +/// Compiles `stmt` into a tree that reads `input` and either selects from it or folds it with the +/// statement's one window. +/// +/// `input` is the raw series schema the caller bound from the table, and the caller has to apply the +/// selector's matchers before binding it: `__field__`, `__schema__` and `__database__` select rows +/// and fields the same way they do in the existing planner, and the AST cannot restate that +/// afterwards. `input.fields` is therefore the post-`__field__` field set, not the table's; a caller +/// that binds every field of the table instead compiles a different, wrong window, because +/// `rate(x{__field__="a"}[5m])` over fields `[a, b]` would fold two fields where the planner folds +/// one. +/// +/// `ordering` is what the caller's scan guarantees, and the tree records it rather than assuming +/// one. Only `Contiguous` and `KeyThenTime` are accepted: a native range window consumes one series +/// to its last chunk before the next, so chunks of different series may not interleave, and an +/// `Interleaved` declaration is refused here — which is where the refusal belongs, since a tree that +/// declares it under a window cannot be built by this compiler at all. [`NativeTree::validate`]'s +/// window branch checks the same thing, so if both land this refusal comes first and the tree check +/// is a backstop for a tree built some other way. +/// +/// The tree carries no metric name and no matchers, so `rate(x{job="a"}[5m])` and `rate(y[5m])` +/// compile to identical trees, and an `EXPLAIN` built on this display cannot tell them apart. That +/// is fine while a tree has one selector; a tree with two inputs will need each `Input` to name its +/// selector. +pub fn compile( + stmt: &EvalStmt, + input: Arc, + ordering: SeriesOrdering, +) -> Result { + // A bare selector is the other shape: an instant selection over the same raw input. + if let PromExpr::VectorSelector(selector) = unparen(&stmt.expr) { + return instant_selection(stmt, selector, input, ordering); + } + let PromExpr::Call(Call { func, args }) = unparen(&stmt.expr) else { + return Err(not_implemented(&format!("the expression `{}`", stmt.expr))); + }; + let Some(signature) = range_signature(func.name) else { + return Err(not_implemented(&format!( + "a call to `{}`, which has no native range kernel", + func.name + ))); + }; + // The count is the signature's, so the rejection names what this function takes rather than + // what one argument happens to be. + let expected = signature.parameters() + 1; + if args.args.len() != expected { + return Err(not_implemented(&format!( + "a call to `{}` with {} argument(s), where it takes {expected}: {}", + func.name, + args.args.len(), + signature.arguments() + ))); + } + // The parameters come first and the range selector is last, for both signatures: a function + // without parameters has no `parameters` to read. + let (parameters, selector) = args.args.split_at(signature.parameters()); + let function = match signature { + RangeSignature::Selector(function) => function, + RangeSignature::Scalar(bind) => bind(scalar_parameter(func.name, ¶meters[0])?), + }; + let argument = &selector[0]; + let PromExpr::MatrixSelector(MatrixSelector { vs, range }) = unparen(argument) else { + return Err(not_implemented(&format!( + "a call to `{}` whose argument is `{}`", + func.name, argument + ))); + }; + // A native range window reads one series to its last chunk before it reads the next, so raw + // samples whose chunks of different series interleave cannot be folded by it. The declaration + // is refused here rather than recorded onto an input the tree is built over and fails on. + if ordering == SeriesOrdering::Interleaved { + return Err(not_implemented( + "raw samples whose series interleave, which this compiler cannot consume: its range \ + window reads one series to its last chunk before the next, so chunks of different \ + series may not interleave", + )); + } + window_selection(stmt, function, vs, range, input, ordering) +} + +/// Binds `function` over the range selector `vs[range]` into a window over the raw input. +/// +/// Both shapes reach this: the range, the offset, the anchor, the grid and the output are the same +/// whichever function reads them, so a parameterised function only decides *what* folds the window, +/// never *which* window is folded. +fn window_selection( + stmt: &EvalStmt, + function: RangeFunction, + vs: &VectorSelector, + range: &Duration, + input: Arc, + ordering: SeriesOrdering, +) -> Result { + // An `@` anchor is step-invariant: every step folds the same samples, which the window's + // `anchor_ms` tells `RangeEvaluator` to replay over the grid. + let range_ms = range_millis(range.as_millis())?; + let offset_ms = selector_offset_ms(&vs.offset)?; + let anchor_ms = selector_anchor_ms(&vs.at, stmt)?; + // The grid is the last thing built: a query refused above, or a window that cannot exist, + // never pays for a grid nothing would read. + let output_grid = statement_grid(stmt)?; + + // A range function reports one value per input field, so the layout is the input's. + let output = NativeType::Samples(Arc::new(SeriesSchema { + fields: Arc::clone(&input.fields), + eval_grid: Some(Arc::clone(&output_grid)), + })); + + // One allocation shared by the binding and the node's output: `NativeTree::try_new` compares + // them by identity, because a compiler that meant one schema has no reason to allocate it + // twice. + let raw = NativeType::Samples(input); + + NativeTree::try_new(NativeNode { + expr: NativeExpr::Window { + input: Box::new(NativeNode { + expr: NativeExpr::Input(BoundInput { + output: raw.clone(), + ordering, + }), + output: raw, + }), + op: BoundWindow { + function, + window: RangeWindow { + range_ms, + offset_ms, + anchor_ms, + }, + // The input is raw samples, so it has no grid of its own. + input_grid: None, + output_grid, + }, + }, + output, + }) +} + +/// Compiles a bare selector into an instant selection over the raw input. +/// +/// The lookback, the offset and the grid are the whole of an instant selection: it reports the last +/// sample at or before each grid point, within the statement's lookback. +fn instant_selection( + stmt: &EvalStmt, + selector: &VectorSelector, + input: Arc, + ordering: SeriesOrdering, +) -> Result { + // An `@` anchor is bound on a range window, which carries `anchor_ms`; an instant selection has + // no such field, so the anchor has nowhere to go. Refusing it is the only alternative to + // dropping it, which would silently select around every grid point instead of one anchor. + if selector.at.is_some() { + return Err(not_implemented(&format!( + "the `@`-anchored selector `{selector}`, whose anchor the instant selection node has \ + no field for: an instant selection binds a lookback, an offset and a grid, while \ + anchoring is bound on a range window, as in `rate(x[5m] @ 1234)`" + ))); + } + let lookback_ms = lookback_millis(stmt.lookback_delta.as_millis())?; + let offset_ms = selector_offset_ms(&selector.offset)?; + let output_grid = statement_grid(stmt)?; + + // An instant selection reports one value per input field, so the layout is the input's. + let output = NativeType::Samples(Arc::new(SeriesSchema { + fields: Arc::clone(&input.fields), + eval_grid: Some(Arc::clone(&output_grid)), + })); + + // One allocation shared by the binding and the node's output, as in the window shape. + let raw = NativeType::Samples(input); + + NativeTree::try_new(NativeNode { + expr: NativeExpr::Instant { + input: Box::new(NativeNode { + expr: NativeExpr::Input(BoundInput { + output: raw.clone(), + ordering, + }), + output: raw, + }), + op: BoundInstant { + lookback_ms, + offset_ms, + output_grid, + }, + }, + output, + }) +} + +/// The grid the statement is evaluated on: `(end - start) / step + 1` points, the last of which +/// does not pass `end`. +fn statement_grid(stmt: &EvalStmt) -> Result> { + let start_ms = signed_millis_since_epoch(stmt.start)?; + let end_ms = signed_millis_since_epoch(stmt.end)?; + // The step is narrowed before [`GridSpec`] sees it, so an interval to which no millisecond + // step corresponds is reported as its own value rather than as a step of zero. + let step_ms = millis(stmt.interval.as_millis()).ok_or_else(|| { + DataFusionError::Plan(format!( + "the statement's evaluation step of {}ms is out of range", + stmt.interval.as_millis() + )) + })?; + // A positive step, ordered endpoints and an inclusive length that fits are the grid's contract, + // so [`GridSpec`] owns them; re-deriving any of them here would be a second copy of it. + let spec = GridSpec::try_from_range(start_ms, end_ms, step_ms).map_err(|error| { + DataFusionError::Plan(format!( + "the statement's grid of {start_ms}ms..{end_ms}ms at {step_ms}ms per step is invalid: \ + {error}" + )) + })?; + Ok(Arc::new(EvalGrid::try_new(spec).map_err(grid_error)?)) +} + +/// The window length of a range selector, which has to be longer than zero to select anything. +fn range_millis(range: u128) -> Result { + let range_ms = millis(range).ok_or_else(|| { + DataFusionError::Plan(format!( + "the range selector's window of {range}ms is out of range" + )) + })?; + if range_ms <= 0 { + return Err(DataFusionError::Plan(format!( + "the range selector's window is {range_ms}ms, and a window has to be longer than zero" + ))); + } + Ok(range_ms) +} + +/// The statement's lookback in milliseconds, which is how far back an instant selection reads. +fn lookback_millis(lookback: u128) -> Result { + millis(lookback).ok_or_else(|| { + DataFusionError::Plan(format!( + "the statement's lookback of {lookback}ms is out of range" + )) + }) +} + +/// The selector's `@` anchor in milliseconds since the Unix epoch, or `None` when it has none. +/// +/// `start()` and `end()` stay symbolic in the AST, because only the statement knows which evaluation +/// range they name, so they are resolved against `stmt.start` and `stmt.end` here. The offset is not +/// folded in: [`RangeWindow`] keeps anchor and offset apart and ends the window at +/// `anchor_ms - offset_ms` at run time. +/// +/// An anchor decouples the window's read range from the statement's: it reads +/// `(anchor_ms - offset_ms - range_ms, anchor_ms - offset_ms]`, which for an anchor like `@ 1234` can +/// lie entirely outside `[stmt.start - range_ms, stmt.end]`. Whoever binds the scan therefore has to +/// derive its range from the window's anchor, offset and range rather than from `stmt.start` and +/// `stmt.end`; a scan bound to the statement alone leaves such a window with no samples, and the +/// query silently returns empty instead of failing. +fn selector_anchor_ms(at: &Option, stmt: &EvalStmt) -> Result> { + match at { + None => Ok(None), + Some(AtModifier::Start) => Ok(Some(signed_millis_since_epoch(stmt.start)?)), + Some(AtModifier::End) => Ok(Some(signed_millis_since_epoch(stmt.end)?)), + Some(AtModifier::At(time)) => Ok(Some(signed_millis_since_epoch(*time)?)), + } +} + +/// The selector's offset in milliseconds; positive reads earlier samples. +fn selector_offset_ms(offset: &Option) -> Result { + let Some(offset) = offset else { + return Ok(0); + }; + let (magnitude, sign) = match offset { + Offset::Pos(duration) => (duration.as_millis(), 1), + Offset::Neg(duration) => (duration.as_millis(), -1), + }; + millis(magnitude) + .and_then(|magnitude| magnitude.checked_mul(sign)) + .ok_or_else(|| { + DataFusionError::Plan(format!( + "the selector's offset of {magnitude}ms is out of range" + )) + }) +} + +/// A millisecond magnitude as an `i64`, or `None` when it does not fit. +fn millis(millis: u128) -> Option { + i64::try_from(millis).ok() +} + +fn grid_error(error: datatypes::error::Error) -> DataFusionError { + DataFusionError::Plan(format!("the statement's grid is invalid: {error}")) +} + +/// What a call to a range function has to be: the parameters it reads, and how to bind them. +#[derive(Clone, Copy)] +enum RangeSignature { + /// A function that reads only its range selector, as in `rate(x[5m])`. + Selector(RangeFunction), + /// A function that reads one number literal before its range selector, and the function that + /// number binds, as in `quantile_over_time(0.9, x[5m])`. + /// + /// The number is kept out of the variant name because only the call site knows it. + Scalar(fn(f64) -> RangeFunction), +} + +impl RangeSignature { + /// How many scalar parameters the function reads before its range selector. + fn parameters(self) -> usize { + match self { + Self::Selector(_) => 0, + Self::Scalar(_) => 1, + } + } + + /// What the call's arguments are, for the arity rejection to name them. + fn arguments(self) -> &'static str { + match self { + Self::Selector(_) => "its range selector", + Self::Scalar(_) => "a number and its range selector", + } + } +} + +/// The signature the PromQL name calls, or `None` when this compiler has no kernel for it. +/// +/// The map is explicit on purpose, as in [`range_function`]: a name that is not listed is rejected, +/// so supporting one more function is a decision made in one place rather than a consequence of a +/// name looking familiar. A parameterised function is named here rather than in the map, because +/// its name alone cannot produce the variant the tree binds. +fn range_signature(name: &str) -> Option { + match name { + "quantile_over_time" => Some(RangeSignature::Scalar(RangeFunction::QuantileOverTime)), + _ => range_function(name).map(RangeSignature::Selector), + } +} + +/// The number a parameterised range function was given, as the literal it was written as. +/// +/// A number literal is the only parameter this compiler binds. A parameter whose value is only +/// known at run time, `time()` being the real case, would make the kernel's constant a query-time +/// value, which the tree has no field for. +/// +/// A parameter written as an expression the existing planner folds to a literal is refused here +/// and is a gap this stage leaves open on purpose: `quantile_over_time(1 - 0.1, x[5m])` and +/// `quantile_over_time(-(0.9), x[5m])` plan today through `try_build_literal_expr`, which folds +/// `Paren`, `Unary` and literal-only `Binary`, and reach this function still unfolded. They are +/// refused rather than answered from the wrong number, and folding them here would be the work +/// to close the gap. +/// +/// A quantile outside `[0, 1]` is not refused here: Prometheus binds it and the kernel reports +/// `-Infinity` below zero and `+Infinity` above one, so refusing it would be this compiler +/// inventing a rule the query engine does not have. +fn scalar_parameter(name: &str, parameter: &PromExpr) -> Result { + match unparen(parameter) { + PromExpr::NumberLiteral(NumberLiteral { val }) => Ok(*val), + found => Err(not_implemented(&format!( + "a call to `{name}` whose first argument is `{found}`, where it has to be a number \ + literal" + ))), + } +} + +/// The range function a PromQL name denotes, or `None` when this compiler has no kernel for it. +/// +/// The map is explicit on purpose: a name that is not listed here is rejected, so supporting one +/// more function is a decision made in one place rather than a consequence of a name looking +/// familiar. +fn range_function(name: &str) -> Option { + match name { + "rate" => Some(RangeFunction::Rate), + "increase" => Some(RangeFunction::Increase), + "delta" => Some(RangeFunction::Delta), + "irate" => Some(RangeFunction::Irate), + "idelta" => Some(RangeFunction::Idelta), + "avg_over_time" => Some(RangeFunction::AvgOverTime), + "min_over_time" => Some(RangeFunction::MinOverTime), + "max_over_time" => Some(RangeFunction::MaxOverTime), + "sum_over_time" => Some(RangeFunction::SumOverTime), + "count_over_time" => Some(RangeFunction::CountOverTime), + "last_over_time" => Some(RangeFunction::LastOverTime), + "present_over_time" => Some(RangeFunction::PresentOverTime), + "stddev_over_time" => Some(RangeFunction::StddevOverTime), + "stdvar_over_time" => Some(RangeFunction::StdvarOverTime), + "resets" => Some(RangeFunction::Resets), + "changes" => Some(RangeFunction::Changes), + "deriv" => Some(RangeFunction::Deriv), + _ => None, + } +} + +/// The expression a parenthesised one wraps, however many parentheses were written. +/// +/// Parentheses group, so they say nothing about the expression inside them: `rate((x[5m]))` asks +/// for exactly what `rate(x[5m])` asks for. Refusing it would reject a statement on the way it was +/// typed rather than on what it means. +fn unparen(mut expr: &PromExpr) -> &PromExpr { + while let PromExpr::Paren(paren) = expr { + expr = &paren.expr; + } + expr +} + +/// A rejection of a shape outside [`SUPPORTED`]. +fn not_implemented(found: &str) -> DataFusionError { + DataFusionError::NotImplemented(format!( + "the native PromQL compiler supports only {SUPPORTED} for now, but found {found}" + )) +} + +#[cfg(test)] +mod tests; diff --git a/src/query/src/promql/native_compile/tests.rs b/src/query/src/promql/native_compile/tests.rs new file mode 100644 index 00000000000..5e375858111 --- /dev/null +++ b/src/query/src/promql/native_compile/tests.rs @@ -0,0 +1,802 @@ +// 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. + +use std::sync::Arc; +use std::time::{Duration, UNIX_EPOCH}; + +use datafusion::error::DataFusionError; +use datatypes::series::SeriesOrdering; +use datatypes::series::samples::{FieldSpec, SeriesSchema}; +use promql::native::plan::{ + BoundInstant, BoundWindow, NativeExpr, NativeNode, NativeTree, NativeType, +}; +use promql::range_eval::RangeFunction; +use promql_parser::parser::{EvalStmt, Expr as PromExpr, NumberLiteral}; + +use super::compile; + +/// The raw series schema a caller binds from the table: one field and no evaluation grid. +fn raw_schema() -> Arc { + Arc::new(SeriesSchema { + fields: Arc::new([FieldSpec { + name: "value".into(), + }]), + eval_grid: None, + }) +} + +/// A statement over 100 seconds in 5-second steps, which is 21 grid points. +fn eval_stmt(query: &str) -> EvalStmt { + eval_stmt_over(query, Duration::from_secs(100)) +} + +/// The statement `query` over `UNIX_EPOCH..=end`, on a 5s grid. +/// +/// The end is a parameter because a grid stops before an end it does not land on, which is the only +/// way `end()` and the last grid point can be told apart. +fn eval_stmt_over(query: &str, end: Duration) -> EvalStmt { + EvalStmt { + expr: promql_parser::parser::parse(query).unwrap(), + start: UNIX_EPOCH, + end: UNIX_EPOCH + end, + interval: Duration::from_secs(5), + lookback_delta: Duration::from_secs(5), + } +} + +/// `rate(x[])` with the selector's range set directly: the parser refuses a zero-length +/// range, so a zero-length window can only be handed to the compiler here. +fn with_selector_range(range: Duration) -> EvalStmt { + let mut stmt = eval_stmt("rate(x[5m])"); + let PromExpr::Call(call) = &mut stmt.expr else { + unreachable!("`rate(x[5m])` parses to a call"); + }; + let PromExpr::MatrixSelector(selector) = &mut *call.args.args[0] else { + unreachable!("`rate(x[5m])`'s argument parses to a range selector"); + }; + selector.range = range; + stmt +} + +/// `query`'s statement with its call's arguments edited by `edit`. +/// +/// The parser checks arity, so a call with the wrong number of arguments cannot be parsed: the only +/// way to hand one to the compiler is to edit a tree the parser accepted. +fn with_call_arguments(query: &str, edit: impl FnOnce(&mut Vec>)) -> EvalStmt { + let mut stmt = eval_stmt(query); + let PromExpr::Call(call) = &mut stmt.expr else { + unreachable!("`{query}` parses to a call"); + }; + edit(&mut call.args.args); + stmt +} + +/// The window at the root of `tree`, for the range-function shape. +fn root_window(tree: &NativeTree) -> (&NativeNode, &BoundWindow) { + let NativeExpr::Window { input, op } = &tree.root().expr else { + panic!("the root is not a window: {:?}", tree.root().expr); + }; + (input, op) +} + +/// The instant selection at the root of `tree`, for the bare-selector shape. +fn root_instant(tree: &NativeTree) -> (&NativeNode, &BoundInstant) { + let NativeExpr::Instant { input, op } = &tree.root().expr else { + panic!( + "the root is not an instant selection: {:?}", + tree.root().expr + ); + }; + (input, op) +} + +/// `rate(x[5m])` becomes a window over the raw input, on the statement's grid. +#[test] +fn a_rate_over_a_selector_becomes_a_window_over_raw_input() { + // `compile` returns `NativeTree::try_new`'s result, so unwrapping is the validation. + let tree = compile( + &eval_stmt("rate(x[5m])"), + raw_schema(), + SeriesOrdering::KeyThenTime, + ) + .unwrap(); + + let NativeExpr::Window { input, op } = &tree.root().expr else { + panic!("the root is not a window: {:?}", tree.root().expr); + }; + assert_eq!(op.function, RangeFunction::Rate); + assert_eq!(op.window.range_ms, 300_000); + assert_eq!(op.window.offset_ms, 0); + assert_eq!(op.window.anchor_ms, None); + assert!(op.input_grid.is_none(), "raw input has no grid to declare"); + + // The statement's grid: 0, 5s, ..., 100s. + let spec = op.output_grid.spec(); + assert_eq!( + (spec.start_ms, spec.step_ms, spec.len), + (0, 5_000, 21), + "the window does not land on the statement's grid" + ); + + let NativeExpr::Input(bound) = &input.expr else { + panic!("the window's input is not an input: {:?}", input.expr); + }; + assert!( + matches!(&bound.output, NativeType::Samples(schema) if schema.eval_grid.is_none()), + "the input is not raw samples: {:?}", + bound.output + ); + assert_eq!(bound.ordering, SeriesOrdering::KeyThenTime); +} + +/// Every mapped name produces its own kernel; a name that fell through to the same kernel as +/// another would be caught here. +#[test] +fn the_supported_range_functions_map_to_their_kernels() { + let cases = [ + ("rate", RangeFunction::Rate), + ("increase", RangeFunction::Increase), + ("delta", RangeFunction::Delta), + ("irate", RangeFunction::Irate), + ("idelta", RangeFunction::Idelta), + ("avg_over_time", RangeFunction::AvgOverTime), + ("min_over_time", RangeFunction::MinOverTime), + ("max_over_time", RangeFunction::MaxOverTime), + ("sum_over_time", RangeFunction::SumOverTime), + ("count_over_time", RangeFunction::CountOverTime), + ("last_over_time", RangeFunction::LastOverTime), + ("present_over_time", RangeFunction::PresentOverTime), + ("stddev_over_time", RangeFunction::StddevOverTime), + ("stdvar_over_time", RangeFunction::StdvarOverTime), + ("resets", RangeFunction::Resets), + ("changes", RangeFunction::Changes), + ("deriv", RangeFunction::Deriv), + ]; + + for (name, kernel) in cases { + let tree = compile( + &eval_stmt(&format!("{name}(x[1m])")), + raw_schema(), + SeriesOrdering::KeyThenTime, + ) + .unwrap(); + let NativeExpr::Window { op, .. } = &tree.root().expr else { + panic!( + "`{name}` did not compile to a window: {:?}", + tree.root().expr + ); + }; + assert_eq!(op.function, kernel, "`{name}` produced the wrong kernel"); + assert_eq!(op.window.range_ms, 60_000, "`{name}` lost its window"); + } +} + +/// `quantile_over_time(0.9, x[5m])` binds the quantile the query wrote, over the same window a +/// function without parameters would fold: a parameter decides what folds the window, not which +/// window is folded. +#[test] +fn a_quantile_over_time_binds_its_quantile_and_its_window() { + let tree = compile( + &eval_stmt("quantile_over_time(0.9, x[5m])"), + raw_schema(), + SeriesOrdering::KeyThenTime, + ) + .unwrap(); + + let (_, op) = root_window(&tree); + assert_eq!(op.function, RangeFunction::QuantileOverTime(0.9)); + assert_eq!(op.window.range_ms, 300_000); + assert_eq!(op.window.offset_ms, 0); + assert_eq!(op.window.anchor_ms, None); + assert!(op.input_grid.is_none(), "raw input has no grid to declare"); + + // The statement's grid: 0, 5s, ..., 100s. + let spec = op.output_grid.spec(); + assert_eq!( + (spec.start_ms, spec.step_ms, spec.len), + (0, 5_000, 21), + "the window does not land on the statement's grid" + ); +} + +/// A quantile outside `[0, 1]` compiles, because it is not an error in Prometheus: the kernel +/// returns `-Infinity` below zero, `+Infinity` above one, and a real quantile at either bound. A +/// compiler that refused it would invent a rule the query engine does not have, so the number is +/// bound exactly as written. +#[test] +fn a_quantile_outside_the_unit_interval_is_bound_as_given() { + let cases = [("-1.0", -1.0), ("2.0", 2.0), ("0.0", 0.0), ("1.0", 1.0)]; + + for (written, quantile) in cases { + let query = format!("quantile_over_time({written}, x[5m])"); + let tree = compile( + &eval_stmt(&query), + raw_schema(), + SeriesOrdering::KeyThenTime, + ) + .unwrap_or_else(|error| panic!("`{query}` did not compile: {error}")); + let (_, op) = root_window(&tree); + assert_eq!( + op.function, + RangeFunction::QuantileOverTime(quantile), + "`{query}` did not bind the quantile it wrote" + ); + assert_eq!(op.window.range_ms, 300_000, "`{query}` lost its window"); + } +} + +/// A parameter in parentheses binds the same quantile, because it is the same query: the planner +/// already looks through the parentheses when it reads a literal, so the native compiler does too. +#[test] +fn a_parenthesised_quantile_binds_as_written() { + let tree = compile( + &eval_stmt("quantile_over_time((0.9), x[5m])"), + raw_schema(), + SeriesOrdering::KeyThenTime, + ) + .expect("a parenthesised literal is the same quantile"); + + let (_, op) = root_window(&tree); + assert_eq!(op.function, RangeFunction::QuantileOverTime(0.9)); +} + +/// The selector of a parameterised function is looked through its parentheses too, so the range +/// function and the parameter are both read from a statement that was merely typed with grouping. +#[test] +fn a_parenthesised_selector_binds_as_written() { + let bare = compile( + &eval_stmt("quantile_over_time(0.9, x[5m])"), + raw_schema(), + SeriesOrdering::KeyThenTime, + ) + .expect("the bare selector binds"); + + for query in [ + "quantile_over_time(0.9, (x[5m]))", + "quantile_over_time((0.9), (x[5m]))", + "(quantile_over_time(0.9, (x[5m])))", + ] { + let tree = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime) + .unwrap_or_else(|error| panic!("`{query}` was refused: {error}")); + assert_eq!( + tree.to_string(), + bare.to_string(), + "`{query}` did not compile to the tree `quantile_over_time(0.9, x[5m])` does" + ); + } +} + +/// A parameter the planner folds to a literal is still refused, and that gap is deliberate: the +/// kernel's constant is a field of the tree, so binding `1 - 0.1` means folding it here rather than +/// carrying an expression. A refusal is the agreed behaviour for this stage; an answer computed +/// from the wrong number would not be. +#[test] +fn a_parameter_folded_from_an_expression_is_refused() { + for query in [ + "quantile_over_time(1 - 0.1, x[5m])", + "quantile_over_time(-(0.9), x[5m])", + ] { + let error = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime) + .expect_err(&format!("`{query}` compiled, and it should not have")); + assert!( + matches!(error, DataFusionError::NotImplemented(_)), + "`{query}` did not report NotImplemented: {error}" + ); + let message = error.to_string(); + assert!( + message.contains("number literal"), + "`{query}` did not say what the argument has to be: {message}" + ); + } +} + +/// A parameter computed at query time is refused, and the error names it: the tree binds a kernel's +/// constant, so a quantile that would only exist at run time has no field to go in. +#[test] +fn a_computed_quantile_is_refused_and_names_what_it_found() { + // `time()` is a scalar, so the parser accepts it as the quantile; the compiler binds literals. + let query = "quantile_over_time(time(), x[5m])"; + let error = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime) + .expect_err(&format!("`{query}` compiled, and it should not have")); + assert!( + matches!(error, DataFusionError::NotImplemented(_)), + "`{query}` did not report NotImplemented: {error}" + ); + let message = error.to_string(); + assert!( + message.contains("whose first argument is `time()`"), + "`{query}` did not name the argument it found: {message}" + ); + assert!( + message.contains("number literal"), + "`{query}` did not say what the argument has to be: {message}" + ); +} + +/// A parameterised window binds its anchor and its offset exactly as an unparameterised one does. +/// The two are asserted apart because the runtime folds `anchor - offset`, so a binding that +/// dropped either number would still compile a window, and only these assertions would notice. +#[test] +fn a_quantile_over_time_binds_an_anchor_and_an_offset_apart() { + let tree = compile( + &eval_stmt("quantile_over_time(0.9, x[5m] @ 1234 offset 1m)"), + raw_schema(), + SeriesOrdering::KeyThenTime, + ) + .unwrap(); + + let (_, op) = root_window(&tree); + assert_eq!(op.function, RangeFunction::QuantileOverTime(0.9)); + assert_eq!(op.window.range_ms, 300_000, "the window was not bound"); + assert_eq!( + op.window.anchor_ms, + Some(1_234_000), + "the anchor was not bound" + ); + assert_eq!(op.window.offset_ms, 60_000, "the offset was not bound"); +} + +/// A call with the wrong number of arguments names the count its function takes, rather than +/// reporting whatever the compiler does with one argument. The parser checks arity too, so a +/// mismatched call can only reach the compiler as an edited tree. +#[test] +fn a_call_with_the_wrong_number_of_arguments_names_the_count_it_takes() { + let cases = [ + // `quantile_over_time` takes its quantile and its range selector: two arguments. + ( + with_call_arguments("quantile_over_time(0.9, x[5m])", |args| { + args.remove(1); + }), + "quantile_over_time", + "where it takes 2", + ), + // `rate` takes one argument, so nothing can be passed in front of its range selector. + ( + with_call_arguments("rate(x[5m])", |args| { + args.push(Box::new(PromExpr::NumberLiteral(NumberLiteral::new(1.0)))); + }), + "rate", + "where it takes 1", + ), + ]; + + for (stmt, name, expected) in cases { + let error = compile(&stmt, raw_schema(), SeriesOrdering::KeyThenTime) + .expect_err(&format!("`{name}` with the wrong argument count compiled")); + assert!( + matches!(error, DataFusionError::NotImplemented(_)), + "`{name}` did not report NotImplemented: {error}" + ); + let message = error.to_string(); + assert!( + message.contains(&format!("a call to `{name}`")), + "`{name}` did not name itself: {message}" + ); + assert!( + message.contains(expected), + "`{name}` did not name the count it takes: {message}" + ); + } +} + +/// `x` becomes an instant selection over the raw input, on the statement's grid, with the +/// statement's lookback and the input's fields. +#[test] +fn a_bare_selector_becomes_an_instant_over_raw_input() { + let input = raw_schema(); + // A lookback the statement chose, so the binding cannot be a default the compiler invented. + let stmt = EvalStmt { + lookback_delta: Duration::from_secs(120), + ..eval_stmt("x") + }; + let tree = compile(&stmt, Arc::clone(&input), SeriesOrdering::KeyThenTime).unwrap(); + + let (input_node, op) = root_instant(&tree); + assert_eq!(op.lookback_ms, 120_000); + assert_eq!(op.offset_ms, 0); + + // The statement's grid: 0, 5s, ..., 100s. + let spec = op.output_grid.spec(); + assert_eq!( + (spec.start_ms, spec.step_ms, spec.len), + (0, 5_000, 21), + "the instant selection does not land on the statement's grid" + ); + + // The output is the input's fields, evaluated on the statement's grid. + let output = tree + .root() + .output + .samples() + .expect("an instant selection produces samples"); + assert!( + Arc::ptr_eq(&output.fields, &input.fields), + "the instant selection did not keep the input's fields" + ); + let output_grid = output + .eval_grid + .as_ref() + .expect("an instant selection produces evaluated values"); + assert!( + Arc::ptr_eq(output_grid, &op.output_grid), + "the output grid is not the bound grid" + ); + + let NativeExpr::Input(bound) = &input_node.expr else { + panic!( + "the instant selection's input is not an input: {:?}", + input_node.expr + ); + }; + assert!( + matches!(&bound.output, NativeType::Samples(schema) if schema.eval_grid.is_none()), + "the input is not raw samples: {:?}", + bound.output + ); + assert_eq!(bound.ordering, SeriesOrdering::KeyThenTime); +} + +/// A bare selector's offset reaches the instant binding with its sign. +#[test] +fn an_offset_on_a_bare_selector_keeps_its_sign() { + let cases = [("x offset 1m", 60_000), ("x offset -1m", -60_000)]; + + for (query, offset_ms) in cases { + let tree = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime).unwrap(); + let (_, op) = root_instant(&tree); + assert_eq!(op.offset_ms, offset_ms, "`{query}` lost the offset's sign"); + assert_eq!(op.lookback_ms, 5_000, "`{query}` lost the lookback"); + } +} + +/// An `@`-anchored bare selector is refused because the instant node has no anchor field, not +/// because anchoring is unsupported: the window shape binds an anchor. +#[test] +fn an_anchored_instant_selector_is_refused_for_the_missing_anchor_field() { + let error = compile( + &eval_stmt("x @ 100"), + raw_schema(), + SeriesOrdering::KeyThenTime, + ) + .expect_err("`x @ 100` compiled, and it should not have"); + assert!( + matches!(error, DataFusionError::NotImplemented(_)), + "`x @ 100` did not report NotImplemented: {error}" + ); + let message = error.to_string(); + assert!( + message.contains("whose anchor the instant selection node has no field for"), + "`x @ 100` did not say why its anchor cannot be bound: {message}" + ); +} + +/// A window's `@` anchor is bound as an absolute millisecond timestamp: `start()` and `end()` are +/// the statement's own bounds, and a number is the unix second it names. +#[test] +fn an_anchor_on_a_window_resolves_to_absolute_milliseconds() { + let cases = [ + // `@ `: 1234 is unix second 1234, which is 1_234_000 ms. + ("rate(x[5m] @ 1234)", 1_234_000), + // A sub-second anchor resolves to the millisecond it names. + ("rate(x[5m] @ 1.234)", 1_234), + // Prometheus allows a pre-epoch anchor: `signed_millis_since_epoch` takes `Err(earlier)`, + // where a `duration_since(UNIX_EPOCH).unwrap_or_default()` helper would silently give 0. + ("rate(x[5m] @ -1.5)", -1_500), + // The statement here is UNIX_EPOCH..100s, which is what `start()` and `end()` mean. + ("rate(x[5m] @ start())", 0), + ("rate(x[5m] @ end())", 100_000), + ]; + + for (query, anchor_ms) in cases { + let tree = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime).unwrap(); + let (_, op) = root_window(&tree); + assert_eq!( + op.window.anchor_ms, + Some(anchor_ms), + "`{query}` bound the wrong anchor" + ); + assert_eq!(op.window.offset_ms, 0, "`{query}` invented an offset"); + } +} + +/// `end()` is the statement's end, not the last grid point. +/// +/// The grid stops before an end it does not land on, so an end of 103s over a 5s step has its last +/// point at 100s. Binding `end()` to that point instead of to the statement's own end would pass a +/// test whose end is a multiple of the step; here the two numbers differ. +#[test] +fn end_resolves_to_the_statement_end_not_the_last_grid_point() { + let stmt = eval_stmt_over("rate(x[5m] @ end())", Duration::from_secs(103)); + let tree = compile(&stmt, raw_schema(), SeriesOrdering::KeyThenTime).unwrap(); + let (_, op) = root_window(&tree); + assert_eq!( + op.window.anchor_ms, + Some(103_000), + "`@ end()` bound something other than the statement's end" + ); + + // The grid the same statement compiles onto ends at 100s, so the two are not the same number. + let spec = op.output_grid.spec(); + let last_point = spec.start_ms + (spec.len as i64 - 1) * spec.step_ms; + assert_eq!( + last_point, 100_000, + "the test's own grid does not stop before the statement end" + ); +} + +/// An anchor and an offset are bound apart: the window ends at `anchor - offset`, which the +/// runtime folds, so both numbers have to survive compilation. +#[test] +fn an_anchor_and_an_offset_are_bound_apart() { + let cases = [ + ("rate(x[5m] @ 1.234 offset 1m)", 1_234, 60_000), + ("rate(x[5m] @ 1234 offset 1m)", 1_234_000, 60_000), + ("rate(x[5m] @ 1.234 offset -1m)", 1_234, -60_000), + ]; + + for (query, anchor_ms, offset_ms) in cases { + let tree = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime).unwrap(); + let (_, op) = root_window(&tree); + assert_eq!( + op.window.anchor_ms, + Some(anchor_ms), + "`{query}` lost the anchor" + ); + assert_eq!(op.window.offset_ms, offset_ms, "`{query}` lost the offset"); + } +} + +/// The numbers a tree is made of, at the edges: a span the step does not divide, offsets in either +/// direction, ranges that cannot be a window, and an ordering the scan did not default to. +#[test] +fn the_numeric_edges_of_a_statement_are_bound_as_given() { + /// What a row asserts about the tree `compile` returns. + enum Expected { + /// The statement's grid, by its start, step and length. + Grid { step_ms: i64, len: u64 }, + /// The window's offset, in milliseconds. + Offset(i64), + /// The input's ordering. + Ordering(SeriesOrdering), + /// The query is refused. + Rejected(Rejection), + } + + /// How a refused query is reported. + enum Rejection { + Plan, + NotImplemented, + } + + let cases = [ + // 100s at 7s per step is 14 whole steps and a remainder: the grid keeps the last point + // that does not pass the end, so the remainder is not a point. + ( + "a span the step does not divide", + EvalStmt { + end: UNIX_EPOCH + Duration::from_secs(100), + interval: Duration::from_secs(7), + ..eval_stmt("rate(x[5m])") + }, + SeriesOrdering::KeyThenTime, + Expected::Grid { + step_ms: 7_000, + len: 15, + }, + ), + // The offset's sign is what decides whether the window reads earlier or later samples. + ( + "a positive offset", + eval_stmt("rate(x[5m] offset 1m)"), + SeriesOrdering::KeyThenTime, + Expected::Offset(60_000), + ), + ( + "a negative offset", + eval_stmt("rate(x[5m] offset -1m)"), + SeriesOrdering::KeyThenTime, + Expected::Offset(-60_000), + ), + // A selector that selects nothing, and one whose length no millisecond count can hold. + ( + "a zero-length range", + with_selector_range(Duration::ZERO), + SeriesOrdering::KeyThenTime, + Expected::Rejected(Rejection::Plan), + ), + ( + "a range wider than i64 milliseconds", + eval_stmt("rate(x[4294967295y])"), + SeriesOrdering::KeyThenTime, + Expected::Rejected(Rejection::Plan), + ), + // An instant selection's lookback is narrowed like every other duration. + ( + "a lookback wider than i64 milliseconds", + EvalStmt { + lookback_delta: Duration::from_millis(u64::MAX), + ..eval_stmt("x") + }, + SeriesOrdering::KeyThenTime, + Expected::Rejected(Rejection::Plan), + ), + // A range window reads one series to its last chunk before the next, so a scan whose + // chunks of different series interleave is refused instead of recorded onto an input the + // window would fail on. + ( + "an interleaved ordering, which a window cannot consume to its last chunk", + eval_stmt("rate(x[5m])"), + SeriesOrdering::Interleaved, + Expected::Rejected(Rejection::NotImplemented), + ), + // A supported ordering is recorded on the input rather than dropped. + ( + "a non-default ordering a window can consume", + eval_stmt("rate(x[5m])"), + SeriesOrdering::Contiguous, + Expected::Ordering(SeriesOrdering::Contiguous), + ), + // A shape outside the subset is refused for its shape, whatever its timing: the statement + // range of an unsupported query is not read, so it cannot turn a refusal into a plan error. + ( + "an unsupported shape with an inverted range", + EvalStmt { + end: UNIX_EPOCH, + start: UNIX_EPOCH + Duration::from_secs(100), + ..eval_stmt("x + 1") + }, + SeriesOrdering::KeyThenTime, + Expected::Rejected(Rejection::NotImplemented), + ), + ]; + + for (case, stmt, ordering, expected) in cases { + match (expected, &compile(&stmt, raw_schema(), ordering)) { + (Expected::Rejected(rejection), Err(error)) => { + let as_expected = match rejection { + Rejection::Plan => matches!(error, DataFusionError::Plan(_)), + Rejection::NotImplemented => { + matches!(error, DataFusionError::NotImplemented(_)) + } + }; + assert!( + as_expected, + "`{case}` was rejected with the wrong kind of error: {error}" + ); + } + (Expected::Rejected(_), Ok(_)) => panic!("`{case}` compiled, and it should not have"), + (_, Err(error)) => panic!("`{case}` did not compile: {error}"), + (Expected::Grid { step_ms, len }, Ok(tree)) => { + let (_, op) = root_window(tree); + let spec = op.output_grid.spec(); + assert_eq!( + (spec.start_ms, spec.step_ms, spec.len), + (0, step_ms, len), + "`{case}` did not land on the statement's grid" + ); + } + (Expected::Offset(offset_ms), Ok(tree)) => { + let (_, op) = root_window(tree); + assert_eq!( + op.window.offset_ms, offset_ms, + "`{case}` lost the offset's sign" + ); + } + (Expected::Ordering(want), Ok(tree)) => { + let (input, _) = root_window(tree); + let NativeExpr::Input(bound) = &input.expr else { + panic!("`{case}`'s window has no input: {:?}", input.expr); + }; + assert_eq!( + bound.ordering, want, + "`{case}` dropped the caller's ordering" + ); + } + } + } +} + +/// A shape outside the subset is refused, and the error says what was found so the query can be +/// rewritten rather than guessed at. +#[test] +fn an_unsupported_shape_is_rejected_and_names_what_it_found() { + let cases = [ + ("x + 1", "the expression `x + 1`"), + ("sum(x)", "the expression `sum(x)`"), + ("x[5m:]", "the expression `x[5m:]`"), + // Nested shapes are refused by the argument check, not skipped over. A subquery is the + // reachable case of an argument that is not a range selector: the parser type-checks + // function arguments, so `rate(x)` never gets past parsing. + ( + "rate(x[5m:])", + "a call to `rate` whose argument is `x[5m:]`", + ), + // A function with no kernel, whether or not it takes a constant argument. + ( + "predict_linear(x[5m], 300)", + "a call to `predict_linear`, which has no native range kernel", + ), + ( + "absent(x)", + "a call to `absent`, which has no native range kernel", + ), + ]; + + for (query, named) in cases { + let error = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime) + .expect_err(&format!("`{query}` compiled, and it should not have")); + assert!( + matches!(error, DataFusionError::NotImplemented(_)), + "`{query}` did not report NotImplemented: {error}" + ); + let message = error.to_string(); + assert!( + message.contains(named), + "`{query}` did not name `{named}`: {message}" + ); + assert!( + message.contains("rate(x[5m])"), + "`{query}` did not quote the supported shape: {message}" + ); + } +} + +/// The display of a compiled tree, checked as a whole so the compiler and the display cannot drift +/// apart: a wrong grid, kernel or nesting shows up on a line someone would read. +#[test] +fn the_display_of_a_compiled_tree_is_the_tree() { + let tree = compile( + &eval_stmt("rate(x[5m])"), + raw_schema(), + SeriesOrdering::KeyThenTime, + ) + .unwrap(); + + assert_eq!( + tree.to_string(), + [ + "NativeTree", + " root Window rate [300000ms] offset 0 anchor - on grid 0/5000x21", + " input Input raw samples", + ] + .join("\n") + ); +} + +/// Parentheses group; they do not change what a statement asks for. A compiler that refused them +/// would reject a query on how it was typed rather than on what it means, and a statement written +/// with them is otherwise exactly the shape the compiler accepts. +#[test] +fn parentheses_around_the_accepted_shape_are_transparent() { + let bare = compile( + &eval_stmt("rate(x[5m])"), + raw_schema(), + SeriesOrdering::KeyThenTime, + ) + .unwrap(); + for query in [ + "(rate(x[5m]))", + "rate((x[5m]))", + "rate(((x[5m])))", + "((rate((x[5m]))))", + ] { + let tree = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime) + .unwrap_or_else(|error| panic!("`{query}` was refused: {error}")); + assert_eq!( + tree.to_string(), + bare.to_string(), + "`{query}` did not compile to the tree `rate(x[5m])` does" + ); + } +}