diff --git a/crates/executor/README.md b/crates/executor/README.md index b9e8075e3..f7cc8220f 100644 --- a/crates/executor/README.md +++ b/crates/executor/README.md @@ -28,10 +28,10 @@ consumer), as its `Lifetime` and the run's scope say: one consumer: the node runs once, to the end, before any consumer reads it, and every consumer reads its finished output independently, as the planner charges one run whose output is held. Likewise a computed - `AcrossEvaluations` node with more than one consumer, which only a - hand-built DAG has at query time (a compiled plan reads it through its - view slot). For example, a `OneEvaluation` scan read by three queries - is one store read. The output is charged to the run's byte budget until + `AcrossEvaluations` node with more than one consumer: an unbound view + (a compile with its view slot not bound computes it for its readers in + the query run), or a node of a hand-built DAG. For example, a + `OneEvaluation` scan read by three queries is one store read. The output is charged to the run's byte budget until every consumer has read it; over the budget the run fails with `MemoryLimit` (nothing spills). With one consumer the node streams to it, since no sibling can wait on that consumer. diff --git a/crates/executor/src/operators/mod.rs b/crates/executor/src/operators/mod.rs index f45e20ce6..ded0132f9 100644 --- a/crates/executor/src/operators/mod.rs +++ b/crates/executor/src/operators/mod.rs @@ -38,6 +38,7 @@ pub use aggregate::Reduction; pub use series_window::SubquerySteps; pub use sort::SortKey; pub use summary::{RowWeight, SummaryEvaluation}; +pub(crate) use time_range::TUMBLING_WINDOW_END_COLUMN; #[derive(Clone, serde::Serialize, serde::Deserialize)] enum Kind { #[serde(skip)] diff --git a/crates/executor/src/operators/time_range.rs b/crates/executor/src/operators/time_range.rs index 929cec29e..51b02d794 100644 --- a/crates/executor/src/operators/time_range.rs +++ b/crates/executor/src/operators/time_range.rs @@ -54,13 +54,15 @@ impl Operator { }) } - /// This range window (not an instant window), each kept row's time - /// replaced by the end of the tumbling window of `width_ms` it lies in, - /// the windows counted back from the range's end `E`: `(E - (k+1) · - /// width_ms, E - k · width_ms]` ends at `E - k · width_ms`. A - /// tumbling-window chain built at query time groups its rows by that - /// end. Example: with `E` = 600,000 and windows of 60,000 ms, rows at - /// 540,001 and 600,000 end at 600,000, a row at 540,000 at 540,000. + /// This range window (not an instant window), each kept row followed + /// by the end of the tumbling window of `width_ms` its time lies in, in + /// a trailing [`TUMBLING_WINDOW_END_COLUMN`]; the row's own time is + /// kept, as a counter's samples need it. The windows count back from + /// the range's end `E`: `(E - (k+1) · width_ms, E - k · width_ms]` ends + /// at `E - k · width_ms`. A tumbling-window chain built for its readers + /// groups its rows by that end. Example: with `E` = 600,000 and windows + /// of 60,000 ms, rows at 540,001 and 600,000 end at 600,000, a row at + /// 540,000 at 540,000. pub fn with_tumbling_window_ends(mut self, width_ms: i64) -> Result { let Kind::TimeRange { latest_per_series: None, @@ -73,11 +75,30 @@ impl Operator { if width_ms <= 0 { return Err(invalid("a tumbling window has a positive width")); } + if self + .output + .fields + .iter() + .any(|field| field.name == TUMBLING_WINDOW_END_COLUMN) + { + return Err(invalid("a range window's input already has a window end")); + } *slot = Some(width_ms); + let mut output = (*self.output).clone(); + output.fields.push(result_field( + TUMBLING_WINDOW_END_COLUMN, + DataType::Timestamp, + false, + )); + self.output = Arc::new(output); Ok(self) } } +/// The column [`Operator::with_tumbling_window_ends`] appends: each row's +/// tumbling window end. +pub(crate) const TUMBLING_WINDOW_END_COLUMN: &str = "$tumbling_window_end"; + impl Operator { /// A `TimeShift { offset_ms, at }`: its rows are its input's, which a /// run evaluates once, at the anchor's time minus `offset_ms` @@ -228,7 +249,7 @@ pub(super) fn execute<'a>( batch.filter(&keep)? }; match tumbling_window_width_ms { - Some(width_ms) => window_ends(batch, time, end, width_ms), + Some(width_ms) => window_ends(batch, &operator.output, time, end, width_ms), None => Ok(batch), } }) @@ -274,10 +295,16 @@ pub(super) fn execute<'a>( .boxed_local()) } -/// `batch` (its rows in `(.., end]`) with each row's `time` replaced by the -/// end of its tumbling window of `width_ms` counted back from `end` -/// ([`Operator::with_tumbling_window_ends`]). -fn window_ends(batch: Batch, time: usize, end: i64, width_ms: i64) -> Result { +/// `batch` (its rows in `(.., end]`) as `output`: each row followed by the +/// end of its `time`'s tumbling window of `width_ms` counted back from +/// `end` ([`Operator::with_tumbling_window_ends`]). +fn window_ends( + batch: Batch, + output: &SchemaRef, + time: usize, + end: i64, + width_ms: i64, +) -> Result { let times = batch .column(time) .as_any() @@ -290,16 +317,109 @@ fn window_ends(batch: Batch, time: usize, end: i64, width_ms: i64) -> Result = (0..arrays.len()) + .map(|column| batch.state_column(column).cloned()) + .collect(); + arrays.push(std::sync::Arc::new(ends) as arrow::array::ArrayRef); + states.push(None); + Batch::from_parts(output.clone(), arrays, states, rows) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::plan::PhysicalDAG; + use crate::runtime::{Limits, Scope}; + use futures::executor::block_on; + use planner_types::stages::Lifetime; + + const MINUTE_MS: i64 = 60_000; + + // A range window with tumbling-window ends: at T = 10m, `(T - 3m, T]` + // in windows of 1 minute keeps the rows at 7m (out), 7m + 1 ms, 8m, + // 9m 30 s and 10m, each keeping its time and followed, in + // `TUMBLING_WINDOW_END_COLUMN`, by its window's end: 8m, 8m, 10m and + // 10m. + #[test] + fn a_range_window_stamps_each_row_with_its_tumbling_windows_end() { + let schema = Arc::new(Schema::with_time_index( + vec![ + SummaryField::plain("job", DataType::Utf8, false), + SummaryField::plain("ts", DataType::Timestamp, false), + SummaryField::plain("value", DataType::Float64, false), + ], + 1, + vec![], + )); + let times = [ + 7 * MINUTE_MS, + 7 * MINUTE_MS + 1, + 8 * MINUTE_MS, + 570_000, + 10 * MINUTE_MS, + ]; + let rows = times + .iter() + .map(|&t| { + vec![ + Value::Utf8("a".into()), + Value::Timestamp(t), + Value::Float64(1.0), + ] + }) + .collect(); + let batch = Batch::try_new(schema.clone(), rows).unwrap(); + let mut dag = PhysicalDAG::default(); + dag.add( + 0, + vec![], + Operator::source(schema.clone(), vec![batch]).unwrap(), + Lifetime::None, + ) + .unwrap(); + let window = Operator::time_range(schema, 1, 0, 3 * MINUTE_MS, None, None) + .unwrap() + .with_tumbling_window_ends(MINUTE_MS) + .unwrap(); + let output = window.schema(); + let end = output + .fields + .iter() + .position(|field| field.name == TUMBLING_WINDOW_END_COLUMN) + .unwrap(); + dag.add(1, vec![0], window, Lifetime::None).unwrap(); + let context = RunContext::new( + Scope::Query { + evaluation_time_ms: 10 * MINUTE_MS, + }, + Limits::default(), + ) + .unwrap(); + let mut outputs = dag.execute(&[1], context).unwrap(); + let rows = block_on(async { + let mut rows = Vec::new(); + while let Some(batch) = outputs[0].next().await { + rows.extend(batch.unwrap().to_rows()); + } + rows + }); + // Each kept row's own time and, appended, its window's end. + let times_and_ends: Vec<(i64, i64)> = rows + .into_iter() + .map(|row| match (&row[1], &row[end]) { + (Value::Timestamp(time), Value::Timestamp(end)) => (*time, *end), + other => panic!("{other:?}"), + }) + .collect(); + assert_eq!( + times_and_ends, + [ + (7 * MINUTE_MS + 1, 8 * MINUTE_MS), + (8 * MINUTE_MS, 8 * MINUTE_MS), + (570_000, 10 * MINUTE_MS), + (10 * MINUTE_MS, 10 * MINUTE_MS), + ] + ); } - Batch::from_parts(batch.schema().clone(), arrays, states, rows) } diff --git a/crates/executor/src/physical_planner/mod.rs b/crates/executor/src/physical_planner/mod.rs index ffd9d1b6a..f15c12e96 100644 --- a/crates/executor/src/physical_planner/mod.rs +++ b/crates/executor/src/physical_planner/mod.rs @@ -20,6 +20,7 @@ use planner_types::plan::physical_asap_dag::{ PhysicalASAPDAG, PhysicalASAPDAGNode, PhysicalASAPNodeId, PhysicalASAPOperatorPayload as Payload, }; +use planner_types::plan::time_window::{InputRows, TimeIndexedPlan}; use planner_types::plan::TimeWindow; use planner_types::summary_type::{ SketchParams, SketchStatistic, SummaryInputExpr, SummaryTypeCapability, @@ -513,14 +514,17 @@ fn compile_internal( } continue; } - // A chain builds, in an ingestion run, the window of its input - // ending at the run's end: one row per key, stamped with that - // `window_end`. Older windows are earlier runs', in the store. - // Built at query time, it builds at each evaluation `T` every - // window `(T - (k+1)c, T - kc]` its readers read, in one pass: - // each row is grouped with its key by the end of its window, - // and each state is stamped with that end, as the stored - // windows are, for the `TimeRange`s above it to select. + // A chain refreshed in an ingestion run builds the window of + // its input ending at the run's end: one row per key, stamped + // with that `window_end`. Older windows are earlier runs', in + // the store. Built for its readers instead (at query time, or + // an unbound view: it then reads what its readers read, + // [`CompileGraph::input_time_access`]), it + // builds at each evaluation `T` every window + // `(T - (k+1)c, T - kc]` its readers read, in one pass: each + // row is grouped with its key by the end of its window, and + // each state is stamped with that end, as the stored windows + // are, for the `TimeRange`s above it to select. if let Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { summary_type, input: update, @@ -530,9 +534,9 @@ fn compile_internal( .. }) = &node.payload { - let query_time = node.output_state.timing - == planner_types::ir::properties::ExecutionTiming::QueryTime; - if !query_time { + if node.output_state.timing + == planner_types::ir::properties::ExecutionTiming::IngestionTime + { crate::capability::validate_ingestion_time_summary(filter.as_ref()) .map_err(Error::Invalid)?; } @@ -559,42 +563,79 @@ fn compile_internal( })) => Some(named_column(input, &ColumnRef::SampleValue)?), _ => None, }; - let window = if query_time { + let millis = |ms: u64| { + i64::try_from(ms).map_err(|_| invalid("a chain's read overflows Int64")) + }; + let (window, built_for_readers) = match graph.input_time_access(id).reads { + // Refreshed: its newest window, `(T - length_ms, T]`. + InputRows::LastWindow { length_ms } => ( + Operator::time_range( + input.clone(), + time, + 0, + millis(length_ms)?, + sample_value, + None, + )?, + false, + ), // The windows its readers read, whole tumbling windows. - let read = window_read_above(id, &graph).ok_or_else(|| { - invalid(format!( - "node {id}: a query-time tumbling-window chain is read \ - outside the time ranges above it" - )) - })?; - let millis = |ms: u64| { - i64::try_from(ms).map_err(|_| invalid("a chain's read overflows Int64")) - }; - let (end_offset_ms, read_width_ms) = ( - millis(read.ends_before_evaluation_ms)?, - millis(read.length_ms)?, - ); - if end_offset_ms % width_ms != 0 || read_width_ms % width_ms != 0 { - return Err(invalid(format!( - "node {id}: a window ({end_offset_ms} ms back, {read_width_ms} ms \ - wide) is not whole tumbling windows of {width_ms} ms" - ))); + _ => { + let read = window_read_above(id, &graph).ok_or_else(|| { + invalid(format!( + "node {id}: a tumbling-window chain built for its readers is \ + read outside the time ranges above it" + )) + })?; + let (end_offset_ms, read_width_ms) = ( + millis(read.ends_before_evaluation_ms)?, + millis(read.length_ms)?, + ); + if end_offset_ms % width_ms != 0 || read_width_ms % width_ms != 0 { + return Err(invalid(format!( + "node {id}: a window ({end_offset_ms} ms back, {read_width_ms} ms \ + wide) is not whole tumbling windows of {width_ms} ms" + ))); + } + ( + Operator::time_range( + input.clone(), + time, + end_offset_ms, + read_width_ms, + sample_value, + None, + )? + .with_tumbling_window_ends(width_ms)?, + true, + ) } - Operator::time_range( - input.clone(), - time, - end_offset_ms, - read_width_ms, - sample_value, - None, - )? - .with_tumbling_window_ends(width_ms)? - } else { - Operator::time_range(input.clone(), time, 0, width_ms, sample_value, None)? }; - // At query time each key's rows group by their window end - // too. - let window_end = query_time.then_some(time); + // The rows the build reads: built for its readers, each + // followed by its tumbling window's end, which groups each + // key's rows by window too; the sample time stays the + // update's time (a counter's rate needs it). + let windowed = window.schema(); + // The window end's column in `schema`, when built for its + // readers. + let window_end_in = |schema: &SchemaRef| { + built_for_readers + .then(|| { + schema + .fields + .iter() + .position(|field| { + field.name == crate::operators::TUMBLING_WINDOW_END_COLUMN + }) + .ok_or_else(|| { + invalid( + "a chain built for its readers has no window-end column", + ) + }) + }) + .transpose() + }; + let window_end = window_end_in(&windowed)?; let build = match reduction { PlannerReduction::PerEntity => { let SummaryInputExpr::Column(value) = &update.weight else { @@ -602,14 +643,12 @@ fn compile_internal( "per-entity update requires a projected value column", )); }; - let value = named_column(input, value)?; - let groups = (0..input.fields.len()) - .filter(|&column| { - column != value && (column != time || window_end.is_some()) - }) + let value = named_column(&windowed, value)?; + let groups = (0..windowed.fields.len()) + .filter(|&column| column != value && column != time) .collect(); Operator::per_group_summary_build( - input.clone(), + windowed.clone(), summary_type.clone(), node.parameters.clone(), value, @@ -618,7 +657,7 @@ fn compile_internal( )? } _ => summary_agg_build( - input, + &windowed, summary_type, node.parameters.as_ref(), update, @@ -627,20 +666,12 @@ fn compile_internal( )?, }; let build = match filter { - Some(filter) => build.with_row_filter(expression(&filter.0, input)?)?, + Some(filter) => build.with_row_filter(expression(&filter.0, &windowed)?)?, None => build, }; let compact = build.schema(); // The window end's column among the build's output columns. - let window_end = window_end - .map(|time| { - compact - .fields - .iter() - .position(|field| field.name == input.fields[time].name) - .ok_or_else(|| invalid("a chain's build drops its window end")) - }) - .transpose()?; + let window_end = window_end_in(&compact)?; physical_dag.add(auxiliary, inputs, window)?; physical_dag.add(helper_id(id, 1)?, vec![auxiliary], build)?; physical_dag.add( diff --git a/crates/executor/src/physical_planner/time_window.rs b/crates/executor/src/physical_planner/time_window.rs index 9826d33be..37e4b11cd 100644 --- a/crates/executor/src/physical_planner/time_window.rs +++ b/crates/executor/src/physical_planner/time_window.rs @@ -6,10 +6,12 @@ //! ([`CompileGraph`]). use super::*; +use planner_types::ir::properties::ExecutionTiming; use planner_types::plan::time_window::{ shift_back_ms_below, windows_computed_from, windows_readers_need, InputRows, InputTimeAccess, TimeIndexedPlan, }; +use planner_types::stages::Lifetime; /// The nodes one compile reads, as the time walks see them: each node's /// inputs (`dependencies`), the compiled nodes reading it (`consumers`; @@ -38,15 +40,30 @@ impl TimeIndexedPlan for CompileGraph<'_> { self.dependencies.get(&node).cloned().unwrap_or_default() } + /// An unbound view (a non-root `AcrossEvaluations` ingestion-time node) + /// reads as if placed at query time. The walks ask this only of + /// compiled nodes (every reader is one), and such a node is a view this + /// compile does not bind: it is computed here for the compiled nodes + /// reading it. Example: the + /// plan of `sum by (job)(rate(c[5m]))` compiled with no view bound, a + /// 10 s chain at ingestion time under `TimeRange(5m)`: the chain reads + /// what its readers read, `(T - 5m, T]`, not only its newest window + /// `(T - 10s, T]`; a stored `TimeShift(1h)` reads its readers' windows + /// 1 h back, not only the current time. A planned per-edge read window + /// replaces this override: such a node then reads the union of its + /// outgoing edges' windows. fn input_time_access(&self, node: NodeId) -> InputTimeAccess { let compiled = self.nodes[&node]; - let access = InputTimeAccess::of( - &compiled.payload, - compiled.output_state.timing, - compiled.lifetime, - ); + let root = self.roots.contains(&node); + let timing = match (compiled.output_state.timing, compiled.lifetime) { + (ExecutionTiming::IngestionTime, Lifetime::AcrossEvaluations(_)) if !root => { + ExecutionTiming::QueryTime + } + (timing, _) => timing, + }; + let access = InputTimeAccess::of(&compiled.payload, timing, compiled.lifetime); match access.reads { - InputRows::WhatItsReadersRead if self.roots.contains(&node) => InputTimeAccess { + InputRows::WhatItsReadersRead if root => InputTimeAccess { reads: InputRows::AllRows, shifts_back_ms: 0, }, @@ -196,17 +213,20 @@ pub(super) fn time_range_window(id: NodeId, graph: &CompileGraph<'_>) -> Option< /// The window back from the evaluation time that the compiled consumers of /// `id` read ([`windows_readers_need`]): through the /// `TimeShift`s (without an `@` anchor) above it, a `TimeRange(w)` or a -/// chain of width `w` built at ingestion time (its window ending at the -/// evaluation time) over shifts summing to `s` reads `{s, w}`; several read -/// the smallest window holding all of theirs. A chain built at query time -/// builds the windows its own readers read, so the walk passes up through -/// it. `None` when `id` or a shift above it is a compile root, another -/// consumer reads it outside a window, or nothing reads it. +/// chain of width `w` refreshed as a compile root (its window ending at +/// the evaluation time) over shifts summing to `s` reads `{s, w}`; several +/// read the smallest window holding all of theirs. A chain built for its +/// readers (at query time, or an unbound view, +/// [`CompileGraph::input_time_access`]) builds the windows they read, so +/// the walk passes up through it. `None` when `id` or a shift above it is +/// a compile root, another consumer reads it outside a window, or nothing +/// reads it. /// -/// A `TimeShift` materialized at ingestion time reads its input at its -/// evaluation time, which this walk reads unbounded (every consumer -/// outside a window is): its refresh over a chain slot is refused ("read -/// unbounded"), and over a raw `Scan` it compiles and reads every row. +/// A `TimeShift` materialized at ingestion time and refreshed as a compile +/// root reads its input at its evaluation time, which this walk reads +/// unbounded (every consumer outside a window is): its refresh over a +/// chain slot is refused ("read unbounded"), and over a raw `Scan` it +/// compiles and reads every row. /// /// Example: a `Scan` read by a query-time chain of 1-minute windows, which /// `TimeRange(5m)` and `TimeShift(1m) -> TimeRange(5m)` read, is read for diff --git a/crates/executor/src/runtime/mod.rs b/crates/executor/src/runtime/mod.rs index 163c89a37..3e5462f66 100644 --- a/crates/executor/src/runtime/mod.rs +++ b/crates/executor/src/runtime/mod.rs @@ -103,10 +103,12 @@ enum NodeRun { /// than one consumer: run to the end once before any consumer reads it, /// then read by each consumer, as the planner charges one run whose /// output is held. Likewise a computed `AcrossEvaluations` node with - /// more than one consumer, which only a hand-built DAG has at query time - /// (a compiled plan reads it through its view slot). Example: a - /// `OneEvaluation` scan read by three queries is one store read whose - /// batches every query reads. + /// more than one consumer: an unbound view, which a compile with its + /// view slot not bound computes for its readers in the query run, or a + /// node of a hand-built DAG. Example: a `OneEvaluation` scan read by + /// three queries is one store read whose batches every query reads; a + /// stored chain no view slot binds, read by two queries, is built once + /// and read by both. Held, /// Every other node: one run whose batches reach each consumer as they /// come. At query time it has one consumer (a computed `None` node with diff --git a/crates/executor/tests/tumbling_window_chain.rs b/crates/executor/tests/tumbling_window_chain.rs index 9a9bee44d..75245ddbc 100644 --- a/crates/executor/tests/tumbling_window_chain.rs +++ b/crates/executor/tests/tumbling_window_chain.rs @@ -14,7 +14,6 @@ use planner_types::ir::scalar::ColumnRef; use planner_types::ir::schema::{DataType, Field, FieldDataType, Schema}; use planner_types::ir::{ASAPOp, NonASAPOp, Operator, OperatorNode, QueryRoot, TimeRangeKind}; use planner_types::plan::physical_asap_dag::{PhysicalASAPDAG, PhysicalASAPNodeId}; -use planner_types::stages::Lifetime; use planner_types::summary_type::{ExactKind, ExactParams, SummaryUpdate}; use std::collections::BTreeMap; use std::rc::Rc; @@ -361,61 +360,62 @@ fn a_chain_built_at_query_time_answers_queries_through_its_time_ranges() { ); } -// A range window with tumbling-window ends: at T = 10m, `(T - 3m, T]` in -// windows of 1 minute keeps the rows at 7m (out), 7m + 1 ms, 8m, 9m 30 s and -// 10m, their times replaced by their windows' ends 8m, 8m, 10m and 10m. +// `sum_over_time(latency[5m] offset 1h)` over a stored chain of 1-minute +// windows and a stored `TimeShift(1h)` above it, both views at ingestion +// time, run in one query run with no view bound: both are computed here +// for their readers. At T = 4h the shift reads `(T - 1h - 5m, T - 1h]` = +// `(175m, 180m]`, so the chain builds those five windows from the raw +// samples: `a` = 176 + 177 + 178 + 179 + 180 = 890, `b` twice that. #[test] -fn a_range_window_stamps_each_row_with_its_tumbling_windows_end() { - use asap_executor::operators::Operator as ExecutorOperator; - use asap_executor::plan::PhysicalDAG; - let schema = Arc::new(samples_schema()); - let times = [ - 7 * MINUTE_MS, - 7 * MINUTE_MS + 1, - 8 * MINUTE_MS, - 570_000, - 10 * MINUTE_MS, - ]; - let rows = times - .iter() - .map(|&t| { - vec![ - Value::Utf8("a".into()), - Value::Timestamp(t), - Value::Float64(1.0), - ] - }) - .collect(); - let batch = Batch::try_new(schema.clone(), rows).unwrap(); - let mut dag = PhysicalDAG::default(); - dag.add( - 0, - vec![], - ExecutorOperator::source(schema.clone(), vec![batch]).unwrap(), - Lifetime::None, - ) - .unwrap(); - let window = ExecutorOperator::time_range(schema, 1, 0, 3 * MINUTE_MS, None, None) - .unwrap() - .with_tumbling_window_ends(MINUTE_MS) +fn an_unbound_stored_time_shift_over_an_unbound_chain_reads_its_readers_windows() { + let chain = node(Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { + child: scan(), + summary_type: sum(), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::by(vec![0]), + filter: None, + tumbling_window_width_ms: MINUTE_MS as u64, + })); + let shifted_windows = shift(HOUR_MS, None, chain.clone()); + let shifted = finalized_sum(node(Operator::ASAP(ASAPOp::SummaryMerge { + children: vec![range(5 * MINUTE_MS, shifted_windows.clone())], + group_by: None, + }))); + let (mut dag, nodes) = + planner_types::test_support::forwarders::physical_asap_dag_at_query_time_with_node_ids(&[ + QueryRoot::Operator(shifted.clone()), + ]) .unwrap(); - dag.add(1, vec![0], window, Lifetime::None).unwrap(); + let stored = [id_of(&nodes, &chain), id_of(&nodes, &shifted_windows)]; + views_at(&mut dag, &stored.map(|id| id as u64)).unwrap(); + for id in stored { + assert_eq!( + dag.nodes + .iter() + .find(|node| node.id == id) + .unwrap() + .output_state + .timing, + planner_types::ir::properties::ExecutionTiming::IngestionTime + ); + } + let root = id_of(&nodes, &shifted) as u64; + let query = compile(&dag, &[], &[root]).unwrap(); let context = RunContext::new( Scope::Query { - evaluation_time_ms: 10 * MINUTE_MS, + evaluation_time_ms: 4 * HOUR_MS, }, Limits::default(), ) .unwrap(); - let ends: Vec = run(&dag, &[1], context) - .remove(0) - .into_iter() - .map(|row| match row[1] { - Value::Timestamp(t) => t, - ref other => panic!("{other:?}"), - }) - .collect(); - assert_eq!(ends, [8, 8, 10, 10].map(|minutes| minutes * MINUTE_MS)); + let answers = { + let query = query.instantiate(Arc::new(store_with_samples())).unwrap(); + run(&query, &[root], context) + }; + assert_eq!( + sums_of(answers[0].clone()), + BTreeMap::from([("a".to_string(), 890.0), ("b".to_string(), 1780.0)]) + ); } // `sum_over_time(latency[5m] @ end())` in a range request over (1h, 3h]: diff --git a/crates/planner/tests/runtime_install.rs b/crates/planner/tests/runtime_install.rs index a43ce2a01..32804d4be 100644 --- a/crates/planner/tests/runtime_install.rs +++ b/crates/planner/tests/runtime_install.rs @@ -550,19 +550,32 @@ async fn grouped_and_per_series_counts_over_a_shared_query_time_chain_install_an // ── Counter rate and increase, per series and per group ───────────────────── -/// `query` every 10 s within relative error 0.01 over 1,000 `requests_total` -/// counter series sampled every second, planned with the built-in models -/// on a deployment that keeps raw data or not. -async fn plan_counter_query(query: &str, raw_data_retained: bool) -> InstallablePlan { +/// `queries`, each every `interval_ms` within relative error 0.01, over +/// 1,000 `requests_total` counter series sampled every second, planned +/// together with the built-in models on a deployment that keeps raw data +/// or not. +async fn plan_counter_queries( + queries: &[&str], + interval_ms: u64, + raw_data_retained: bool, +) -> InstallablePlan { + let entries: Vec = queries + .iter() + .map(|query| { + format!( + r#"{{"query": {query:?}, + "demand": {{"fixed_interval_at": {{"interval": {interval_ms}, "evaluation_phase": 0}}}}, + "requirements": {{"accuracy": {{"max_error": 0.01}}}}, + "predictability": {{"predictable": {{"known_at": null}}}}}}"# + ) + }) + .collect(); let workload: PlanningWorkload = serde_json::from_str(&format!( - r#"{{"query_workload": {{"language": "promql", "repeating_queries": [{{ - "query": {query:?}, - "demand": {{"fixed_interval_at": {{"interval": 10000, "evaluation_phase": 0}}}}, - "requirements": {{"accuracy": {{"max_error": 0.01}}}}, - "predictability": {{"predictable": {{"known_at": null}}}}}}]}}, + r#"{{"query_workload": {{"language": "promql", "repeating_queries": [{}]}}, "data_workload": {{"arrival": {{"ingesting": {{"rows_per_second": 1000.0}}}}, "sources": {{"requests_total": {{"series_count": 1000, - "metric_type": "counter", "sample_interval": 1000}}}}}}}}"# + "metric_type": "counter", "sample_interval": 1000}}}}}}}}"#, + entries.join(",") )) .unwrap(); let capabilities = DeploymentCapabilities { @@ -572,7 +585,7 @@ async fn plan_counter_query(query: &str, raw_data_retained: bool) -> Installable let models = PlanningModels::builtin().with_deployment_capabilities(&capabilities); plan_installable(UserInput::new(&workload, FrontendInput::Promql, models)) .await - .unwrap_or_else(|e| panic!("{query} (raw data retained: {raw_data_retained}): {e}")) + .unwrap_or_else(|e| panic!("{queries:?} (raw data retained: {raw_data_retained}): {e}")) .0 } @@ -699,7 +712,7 @@ async fn counter_query_mismatches( let mut mismatches = Vec::new(); for raw_data_retained in [true, false] { for (query, expected) in counter_queries() { - let plan = plan_counter_query(query, raw_data_retained).await; + let plan = plan_counter_queries(&[query], 10_000, raw_data_retained).await; let answered = answer(&plan, &samples).remove(0); let matches = answered.len() == expected.len() && answered.iter().zip(&expected).all(|(answer, expected)| { @@ -736,3 +749,42 @@ async fn counter_rates_and_increases_answer_at_query_time_from_raw_samples() { let mismatches = counter_query_mismatches(query_answers).await; assert!(mismatches.is_empty(), "{mismatches:#?}"); } + +/// `rate(requests_total[5m] offset 1m)` and `rate(requests_total[5m])`, +/// each every minute, share a tumbling-window chain of 1-minute windows +/// that the plan builds at query time. Installed, the query run builds it +/// from the raw samples, each update at its sample's own time: over +/// [`counter_samples`] (four samples per window) both queries answer the +/// hand-worked rates `(i + 1) * 20 / 300` per series. `(T - 6m, T - 1m]` +/// holds steps 9..=28, 20 samples as `(T - 5m, T]` does. +#[tokio::test] +async fn counter_rates_over_a_shared_query_time_chain_answer_as_installed() { + let plan = plan_counter_queries( + &[ + "rate(requests_total[5m] offset 1m)", + "rate(requests_total[5m])", + ], + 60_000, + true, + ) + .await; + assert!( + plan.dag.nodes.iter().any(|node| matches!( + node.payload, + asap_types::ir::Operator::ASAP( + asap_types::ir::ASAPOp::SummaryTumblingWindowChain { .. } + ) + ) && node.output_state.timing + == asap_types::ir::properties::ExecutionTiming::QueryTime), + "the plan builds a chain at query time: {:#?}", + plan.dag.nodes + ); + let rates: Vec = (1..=4).map(|i| i as f64 * 20.0 / 300.0).collect(); + for answered in installed_answers(&plan, &counter_samples()) { + let values: Vec = answered.iter().map(|(_, value)| *value).collect(); + assert_eq!(values.len(), rates.len(), "{answered:?}"); + for (value, rate) in values.iter().zip(&rates) { + assert!((value - rate).abs() <= 1e-9 * rate, "{answered:?}"); + } + } +}