From f72b9f12a0a41c412be4690bd45ec00e41b96292 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 9 Nov 2026 09:00:46 +0000 Subject: [PATCH 01/31] refactor(stage3): baseline selection MILP and the plan it builds; native tumbling-window chains and a time-indexed store in the executor (#1207 PR 6) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Stage 3 costs each physical candidate (w_v) and each selectable edge (c_{u,c,i}) with asap-cost, solves the baseline MILP over x, y, z, t (constraints (A)-(F), (E) only inside strongly connected components, a query-time result that is not materialized read by at most one consumer), and builds the PhysicalASAPDAG of the optimum directly, each node with its timing, lifetime, θ, retention and, materialized, its view coverage (asap_types::plan::view_coverage of its memo group). asap-cost sizes summaries from the node's θ and prices chains and raw data by the node's retention_ms. The executor builds and runs summaries with θ, runs tumbling-window chains natively (TimeRange/TimeShift over a chain select windows), keeps materialized rows in a time-indexed store with retention, and evaluates TimeShift{at} once per request. Installation no longer expands chains. --- crates/cost/src/lib.rs | 12 +- crates/cost/src/node/state_bytes/mod.rs | 23 +- crates/cost/src/node/state_bytes/tests.rs | 3 +- .../cost/src/node/{usage.rs => usage/mod.rs} | 343 ++++++++--- crates/cost/src/node/usage/tests.rs | 205 +++++++ crates/cost/src/plan/cost.rs | 221 ++++--- crates/cost/src/plan/mod.rs | 7 +- crates/cost/src/plan/read_back.rs | 66 +++ crates/cost/src/plan/retention.rs | 134 ----- crates/devtools/src/bin/asap_planner.rs | 45 +- crates/devtools/src/planner_fuzz/mod.rs | 86 ++- crates/devtools/src/planner_fuzz/relations.rs | 6 +- crates/devtools/tests/asap_planner.rs | 6 +- crates/executor/src/capability/mod.rs | 219 +++++-- crates/executor/src/capability/tests.rs | 22 + crates/executor/src/operators/mod.rs | 44 +- crates/executor/src/operators/summary/mod.rs | 176 +++++- crates/executor/src/operators/time_range.rs | 69 ++- crates/executor/src/operators/unchecked.rs | 30 +- .../executor/src/physical_planner/compiled.rs | 26 +- crates/executor/src/physical_planner/mod.rs | 435 +++++++++++--- crates/executor/src/plan/mod.rs | 6 + crates/executor/src/runtime/context.rs | 8 + crates/executor/src/runtime/mod.rs | 20 +- crates/executor/src/sources/data_store.rs | 34 +- crates/executor/src/sources/memory.rs | 89 ++- crates/executor/src/sources/mod.rs | 90 +-- crates/executor/src/sources/window.rs | 195 ------ .../src/summary_kernels/factory/mod.rs | 17 +- .../src/summary_kernels/factory/tests.rs | 49 ++ .../summary_kernels/state_column_updater.rs | 77 ++- crates/executor/src/supported_summaries.rs | 14 +- crates/executor/src/values/batch.rs | 28 +- crates/executor/src/values/mod.rs | 56 +- crates/executor/tests/blocking_resources.rs | 7 +- crates/executor/tests/current_series_heap.rs | 3 +- crates/executor/tests/deployment.rs | 79 +-- crates/executor/tests/exact_state_columns.rs | 3 +- crates/executor/tests/hydra_cms_execution.rs | 25 +- .../tests/keyed_summary_row_weight.rs | 18 +- crates/executor/tests/main.rs | 1 + crates/executor/tests/physical_dag.rs | 32 +- crates/executor/tests/physical_semantics.rs | 34 +- crates/executor/tests/plan_properties.rs | 46 +- .../executor/tests/precompute_population.rs | 17 +- crates/executor/tests/promql_binary.rs | 5 +- crates/executor/tests/raw_scan.rs | 3 +- crates/executor/tests/state_column_updater.rs | 298 +++++++++- .../stored_tumbling_window_memory_limit.rs | 5 +- .../tests/summary_build_memory_limit.rs | 12 +- crates/executor/tests/summary_projection.rs | 6 +- .../executor/tests/tumbling_window_chain.rs | 373 ++++++++++++ crates/executor/tests/univmon_execution.rs | 16 +- crates/integration-tests/src/lib.rs | 45 +- .../tests/filtered_aggregates.rs | 4 +- .../tests/joint_search_equivalence.rs | 2 + .../tests/kll_tumbling_window_execution.rs | 92 ++- crates/planner/src/installable.rs | 334 +++++++---- crates/planner/tests/runtime_install.rs | 61 +- crates/planner/tests/sql_table_scans.rs | 124 ++-- crates/stage3-plan-selection/Cargo.toml | 9 +- crates/stage3-plan-selection/src/cost.rs | 525 ++++++++++++++++ crates/stage3-plan-selection/src/lib.rs | 253 +++++++- .../stage3-plan-selection/src/milp/model.rs | 454 ++++++++++++-- .../src/milp/model/tests.rs | 283 +++++++++ crates/stage3-plan-selection/src/plan.rs | 132 +++++ .../src/ir/properties/execution_data_state.rs | 20 + crates/types/src/plan/materialized_view.rs | 561 +++++++++++------- crates/types/src/plan/physical_asap_dag.rs | 125 ++-- .../stages/physical/memo_candidates/mod.rs | 5 + crates/types/src/test_support/forwarders.rs | 6 - crates/types/tests/physical_asap_dag.rs | 6 +- .../physical-planning-and-deployment.md | 51 +- .../design_docs/proposals/planner-layering.md | 15 +- .../develop_docs/extend-asap-aware-mapping.md | 2 +- 75 files changed, 5250 insertions(+), 1703 deletions(-) rename crates/cost/src/node/{usage.rs => usage/mod.rs} (73%) create mode 100644 crates/cost/src/node/usage/tests.rs create mode 100644 crates/cost/src/plan/read_back.rs delete mode 100644 crates/cost/src/plan/retention.rs delete mode 100644 crates/executor/src/sources/window.rs create mode 100644 crates/executor/tests/tumbling_window_chain.rs create mode 100644 crates/stage3-plan-selection/src/cost.rs create mode 100644 crates/stage3-plan-selection/src/milp/model/tests.rs create mode 100644 crates/stage3-plan-selection/src/plan.rs diff --git a/crates/cost/src/lib.rs b/crates/cost/src/lib.rs index a9c2ac922..a2c1d3bcf 100644 --- a/crates/cost/src/lib.rs +++ b/crates/cost/src/lib.rs @@ -14,8 +14,8 @@ //! [`ResourceSpeed`], [`CostModel`] ([`ClusterLimits`], //! [`ClusterObjective`]) and the deployment's [`CostInputs`], with the //! per-node and per-plan costs ([`NodeCost`], [`PlanCost`]). -//! - Costing a physical plan DAG: [`node_usage`] gives one node's -//! [`NodeUsage`]; [`plan_cost`] the whole plan's [`PlanCost`]. Stage 3 +//! - Costing a physical plan DAG: [`node_resource_usage_and_cost`] gives one node's +//! [`NodeResourceUsageAndCost`]; [`plan_cost`] the whole plan's [`PlanCost`]. Stage 3 //! computes no cost of its own. //! //! Plan selection (Stage 3, `asap-stage3-plan-selection`) documents how it prices @@ -37,12 +37,10 @@ pub use model::{ }; pub use node::operator_statistics::EdgeStatistics; pub use node::usage::{ - node_map, node_usage, summary_type_name, NodeCost, NodeMap, NodeUsage, ProducerOutput, + node_resource_usage_and_cost, summary_type_name, NodeCost, NodeResourceUsageAndCost, + ProducerOutput, }; pub use plan::cost::{plan_cost, PlanCost, QueryLatency}; pub use plan::ranking::{cheaper, worst_latency_ms, PlanRanking}; -pub use plan::retention::{ - maintained_window_ms, query_reach, retained_tumbling_windows, tumbling_window_retention_ms, - tumbling_window_retention_ms_of_plan, tumbling_windows_of, -}; +pub use plan::read_back::read_back_ms; pub use resources::ResourceUsage; diff --git a/crates/cost/src/node/state_bytes/mod.rs b/crates/cost/src/node/state_bytes/mod.rs index c0ca6c042..6f039cfea 100644 --- a/crates/cost/src/node/state_bytes/mod.rs +++ b/crates/cost/src/node/state_bytes/mod.rs @@ -1,6 +1,7 @@ //! The bytes one summary or exact state holds and its update operations //! per input row, from its parameters and the rows it folds. +use crate::node::analytical::AnalyticalCostError; use asap_types::ir::operator::AggIntent; use asap_types::ir::schema::{DataType, FieldDataType, Schema}; use asap_types::summary_type::KLL_MAX_K; @@ -57,15 +58,21 @@ pub fn keeps_every_value(measure: &AggIntent) -> bool { /// Count-Min and Count-Sketch, plus one heap update for their top-k variants; /// for UnivMon, the same per layer it updates (`univmon_expected_layers`); /// 1 for every other sketch. -pub fn summary_shape(family: &FieldDataType, input_rows: StateInputRows) -> (f64, u64) { - match family { - FieldDataType::Sketch(..) => { - let _ = input_rows; - todo!("#1207 PR 6: the state shape of θ from the candidate's LogicalProperties::parameters") - } - FieldDataType::ExactAggregate(..) => (1.0, EXACT_ACCUMULATOR_STATE_BYTES), +/// +/// `parameters` is the build's θ (`PhysicalASAPDAGNode::parameters`). Err +/// [`AnalyticalCostError::UnsupportedCandidate`] for a sketch without θ: a +/// planner bug, since every sketch build carries its θ. +pub fn summary_shape( + family: &FieldDataType, + parameters: Option<&SketchParams>, + input_rows: StateInputRows, +) -> Result<(f64, u64), AnalyticalCostError> { + Ok(match (family, parameters) { + (FieldDataType::Sketch(..), Some(parameters)) => sketch_shape(parameters, input_rows), + (FieldDataType::Sketch(..), None) => return Err(AnalyticalCostError::UnsupportedCandidate), + (FieldDataType::ExactAggregate(..), _) => (1.0, EXACT_ACCUMULATOR_STATE_BYTES), _ => (1.0, 8), - } + }) } /// What one summary state's bytes depend on besides its parameters (only diff --git a/crates/cost/src/node/state_bytes/tests.rs b/crates/cost/src/node/state_bytes/tests.rs index 131a9f26f..3e30a6bb2 100644 --- a/crates/cost/src/node/state_bytes/tests.rs +++ b/crates/cost/src/node/state_bytes/tests.rs @@ -15,9 +15,10 @@ fn exact_state_is_priced_at_the_executors_state_size() { assert_eq!( summary_shape( &FieldDataType::ExactAggregate(kind, params), + None, StateInputRows::UNKNOWN ), - (1.0, 56) + Ok((1.0, 56)) ); } assert_eq!(asap_types::summary_type::EXACT_ACCUMULATOR_STATE_BYTES, 56); diff --git a/crates/cost/src/node/usage.rs b/crates/cost/src/node/usage/mod.rs similarity index 73% rename from crates/cost/src/node/usage.rs rename to crates/cost/src/node/usage/mod.rs index ef3286ac3..1cdff4e30 100644 --- a/crates/cost/src/node/usage.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -1,5 +1,5 @@ -//! One node of a physical plan DAG: its resource usage ([`node_usage`]) -//! and cost ([`NodeCost`]). +//! One node of a physical plan DAG: its resource usage and cost +//! ([`node_resource_usage_and_cost`], [`NodeCost`]). use std::collections::{BTreeMap, BTreeSet, HashMap}; @@ -12,7 +12,6 @@ use crate::node::operator_statistics::{ use crate::node::state_bytes::{ keeps_every_value, row_bytes, sketch_shape, summary_shape, StateInputRows, }; -use crate::tumbling_windows_of; use crate::{aggregate_lookup, lookup, StateFamily}; use crate::{ CalibrationFit, CostInputs, CostModel, NodeCostSource, OperatorKind, COST_SOURCE, @@ -65,15 +64,17 @@ pub struct NodeCost { pub cost_source: NodeCostSource, } -/// The state family of a node with `payload` whose first producer has -/// `producer_family`: a build's own (one sketch instance per group; a -/// shared Hydra structure is not that sketch), inherited by the merges, -/// estimates and finalizations over it, none otherwise. +/// The state family of `node` whose first producer has `producer_family`: +/// a build's own, its θ (`node.parameters`; one sketch instance per group; +/// a shared Hydra structure is not that sketch), inherited by the merges, +/// estimates and finalizations over it, none otherwise. Err +/// [`AnalyticalCostError::UnsupportedCandidate`] for a sketch build without +/// θ, a planner bug. fn state_family<'a>( - payload: &'a Payload, + node: &'a asap_types::plan::physical_asap_dag::PhysicalASAPDAGNode, producer_family: Option>, -) -> StateFamily<'a> { - match payload { +) -> Result, AnalyticalCostError> { + Ok(match &node.payload { Payload::ASAP( ASAPOp::SummaryAgg { summary_type: family, @@ -84,9 +85,11 @@ fn state_family<'a>( .. }, ) => match family { - FieldDataType::Sketch(..) => { - todo!("#1207 PR 6: the state family of θ from the candidate's LogicalProperties::parameters") - } + FieldDataType::Sketch(..) => node + .parameters + .as_ref() + .map(StateFamily::Sketch) + .ok_or(AnalyticalCostError::UnsupportedCandidate)?, FieldDataType::ExactAggregate(kind, _) => StateFamily::Exact(kind), _ => StateFamily::None, }, @@ -96,10 +99,10 @@ fn state_family<'a>( | ASAPOp::FinalizeExactAccumulator { .. }, ) => producer_family.unwrap_or(StateFamily::None), _ => StateFamily::None, - } + }) } -/// One producer of a node being priced ([`node_usage`]): its payload and +/// One producer of a node being priced ([`node_resource_usage_and_cost`]): its payload and /// schema, its output statistics, the sources it reads and its state /// family. #[derive(Debug, Clone)] @@ -112,33 +115,34 @@ pub struct ProducerOutput<'a> { /// Its output columns that still hold one source row's label (or /// column) values unchanged ([`unchanged_source_columns`]). pub unchanged_columns: BTreeSet<&'a str>, + /// The tumbling windows its rows span: those a `TimeRange` selects + /// from a chain, 1 for any other node. + pub tumbling_windows: u64, } -/// What one node uses and outputs ([`node_usage`]): its output statistics, sources and state family for its +/// What one node uses and outputs ([`node_resource_usage_and_cost`]): its output statistics, sources and state family for its /// consumers; its work for one computation (one execution) in ms on one -/// core; its usage per second; what is charged -/// once the whole plan is known (memory held for a query's evaluation, an -/// in-memory ingestion window's partial copies, the raw data a query-time -/// scan keeps on disk); its calibration fit and its [`NodeCost`]. +/// core; its usage per second (query-time memory held while it computes +/// included); what is charged once the whole plan is known (an in-memory +/// ingestion window's partial copies, the raw data a query-time scan keeps +/// on disk); its calibration fit and its [`NodeCost`]. #[derive(Debug, Clone)] -pub struct NodeUsage<'a> { +pub struct NodeResourceUsageAndCost<'a> { pub output: EdgeStatistics, pub sources: BTreeSet<&'a str>, pub family: StateFamily<'a>, /// Its output columns that still hold one source row's label (or /// column) values unchanged ([`unchanged_source_columns`]). pub unchanged_columns: BTreeSet<&'a str>, + /// The tumbling windows its rows span: those a `TimeRange` selects + /// from a chain, 1 for any other node. + pub tumbling_windows: u64, pub work_ms_per_execution: f64, pub cpu_ops_per_second: f64, pub scanned_bytes_per_second: f64, pub average_memory_bytes: f64, pub peak_memory_bytes: u64, pub disk_bytes: u64, - /// Query-time memory held while an evaluation runs: (bytes, computations - /// per second, whether each consumer computes its own copy, held while - /// that consumer's evaluation runs ([`plan_cost`](crate::plan_cost)), rather than one copy - /// held until the longest query reading the node finishes). - pub held: Option<(u64, f64, bool)>, /// An in-memory ingestion-time state's bytes per window. pub maintained: Option, /// A query-time scan's source and the raw bytes it reads. @@ -148,22 +152,25 @@ pub struct NodeUsage<'a> { } /// Measure and price one node of a physical DAG (one step of -/// [`plan_cost`](crate::plan_cost)): `producers` are its producers in edge order, -/// `rate` its `(multiplier, per_second)` ([`computations`](crate::plan::execution_demand::computations)), -/// `retained_tumbling_window_count` the tumbling windows a tumbling-window chain keeps -/// completed (`retained_tumbling_windows`), and `ingestion_window_ms` the window an -/// ingestion-time node maintains (`maintained_window_ms`). +/// [`plan_cost`]): `producers` are its producers in edge order, +/// `rate` its `(multiplier, per_second)` ([`computations`]), and +/// `window_ms` the time window one execution covers: the window an +/// ingestion-time node maintains, or a query-time `Scan`'s read, the most +/// offset + range its consumers read ([`read_back_ms`] without a span; +/// `None`: it reads what it retains) +/// (`maintained_window_ms`). A materialized node keeps what its +/// `retention_ms` ($h_v$) covers: a tumbling-window chain its windows in +/// it, a query-time `Scan` its raw rows in it. #[allow(clippy::too_many_arguments)] -pub fn node_usage<'a>( +pub fn node_resource_usage_and_cost<'a>( node: &'a asap_types::plan::physical_asap_dag::PhysicalASAPDAGNode, producers: &[ProducerOutput<'a>], rate: (u64, f64), - retained_tumbling_window_count: u64, - ingestion_window_ms: Option, + window_ms: Option, data: &DataWorkload, settings: &'a CostInputs, formula_scale: f64, -) -> Result, AnalyticalCostError> { +) -> Result, AnalyticalCostError> { let table = &settings.measured_operation_costs; let speed = settings.resource_speed; let usage_prices = match &settings.cost_model { @@ -173,12 +180,27 @@ pub fn node_usage<'a>( let sketch_size = |params: &SketchParams| sketch_shape(params, StateInputRows::UNKNOWN).1 as f64; let mut fit = CalibrationFit::default(); - let mut held = None; + // Query-time memory held while the node computes. + let mut held_bytes = None; let mut maintained = None; let mut raw_scan = None; // A query-time scan's raw rows retained. let mut raw_retained_rows = None; let ingestion = node.output_state.timing == ExecutionTiming::IngestionTime; + // The completed tumbling windows a chain retains: its `retention_ms` + // over its window width, at least one. + let retained_tumbling_window_count = match &node.payload { + Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { + tumbling_window_width_ms, + .. + }) => node + .retention_ms + .map_or(1, |retention_ms| { + retention_ms.div_ceil((*tumbling_window_width_ms).max(1)) + }) + .max(1), + _ => 1, + }; let inputs: Vec = producers.iter().map(|p| p.output).collect(); let sources: BTreeSet<&str> = match &node.payload { Payload::NonASAP(NonASAPOp::Scan { source, .. }) => [source_name(source)].into(), @@ -198,7 +220,7 @@ pub fn node_usage<'a>( missing: "an arrival rate (the arrival's rows_per_second, or the \ source's series_count with sample_interval)", }; - let family = state_family(&node.payload, producers.first().map(|p| p.family)); + let family = state_family(node, producers.first().map(|p| p.family))?; let unchanged_columns = unchanged_source_columns( node, &producers @@ -265,8 +287,7 @@ pub fn node_usage<'a>( let rows_read = || match ingestion { false => Ok(input.rows.max(1)), true => { - let window_ms = - ingestion_window_ms.ok_or(AnalyticalCostError::IngestionGroupingWithoutWindow)?; + let window_ms = window_ms.ok_or(AnalyticalCostError::IngestionGroupingWithoutWindow)?; // Name a source with no rate of its own. let rows_per_ms = rows_per_ms.ok_or_else(|| { let unrated = sources @@ -314,7 +335,11 @@ pub fn node_usage<'a>( let (window_width_ms, retained_lookback_ms): (Option, Option) = match ingestion { true => (None, None), - false => todo!("#1207 PR 7: a query-time Scan's read per edge (the TimeRange above it) and its retained rows from retention_ms (h_v)"), + // One execution reads the most its consumers read + // (`window_ms`), else what the scan retains (its + // `retention_ms`, $h_v$); `None` reads every row + // held. It keeps `retention_ms`. + false => (window_ms.or(node.retention_ms), node.retention_ms), }; let lookback = match ingestion { true => Some(1_000), @@ -328,7 +353,13 @@ pub fn node_usage<'a>( // The raw rows retained for a query-time scan: back to the // oldest row it reads, `end_offset + width` (an offset // window keeps more than it reads), at most those held. - if !ingestion { + // So are those an ingestion-time scan materialized across + // evaluations keeps: back `retention_ms` ($h_v$). + let retained_lookback_ms = match ingestion { + true => node.retention_ms, + false => retained_lookback_ms, + }; + if !ingestion || matches!(node.lifetime, Lifetime::AcrossEvaluations(_)) { let retained_rows = match (by_rate(retained_lookback_ms), rows_held) { (Some(by_rate), Some(rows_held)) => Some(by_rate.min(rows_held)), (by_rate, rows_held) => by_rate.or(rows_held), @@ -365,6 +396,17 @@ pub fn node_usage<'a>( source_read_bytes: out.bytes, }, ); + // Raw data materialized at ingestion time: its consumers + // read the rows it retains, not one second of them. + let out = match (ingestion, node.lifetime) { + (true, Lifetime::AcrossEvaluations(_)) => edge(raw_retained_rows.ok_or( + AnalyticalCostError::MissingSourceFact { + source_name: source.to_string(), + missing: "its rows_held (raw data retained without a bound)", + }, + )?)?, + _ => out, + }; (out, estimate, format!("scan {} samples", out.rows)) } NonASAPOp::Aggregate { @@ -466,6 +508,37 @@ pub fn node_usage<'a>( ), format!("time shift: pass {} rows, free", input.rows), ), + NonASAPOp::TimeRange { range, .. } if chain_width_ms(producers).is_some() => { + // Over a chain, the windows inside the range: one + // window's states per window. + let windows = selected_tumbling_windows(range, producers)?; + let out = EdgeStatistics { + rows: input + .rows + .checked_mul(windows) + .ok_or(AnalyticalCostError::Overflow)?, + bytes: input + .bytes + .checked_mul(windows) + .ok_or(AnalyticalCostError::Overflow)?, + }; + // It reads those windows' states from the chain's store. + let estimate = estimate_operator( + PhysicalOperator::TimeRange, + OperatorStatistics::PassThrough { + edges: UnaryEdgeStatistics { + input: out, + output: out, + promql: None, + }, + }, + ); + ( + out, + estimate, + format!("time range {range:?}: {windows} tumbling windows of states"), + ) + } NonASAPOp::TimeRange { range, kind, .. } => { // A range keeps its window's samples; an instant // selector the latest sample of each series. @@ -521,7 +594,7 @@ pub fn node_usage<'a>( rows_per_state: (!ingestion).then(|| input.rows.div_ceil(group_count.max(1))), non_negative: raw_data_samples_non_negative(), }; - let (depth, state_bytes) = summary_shape(family, input_rows); + let (depth, state_bytes) = summary_shape(family, node.parameters.as_ref(), input_rows)?; let state_bytes = measured_state_bytes.unwrap_or(state_bytes); let out = EdgeStatistics { rows: group_count, @@ -543,22 +616,66 @@ pub fn node_usage<'a>( ), ) } - Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) => { - todo!("#1207 PR 7: a chain's states and memory over the windows it retains (h_v)") + Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { + summary_type: family, + reduction, + tumbling_window_width_ms, + .. + }) => { + // One window's states: each input row updates the window its + // time falls in. At ingestion time the rows of one window + // arrive at the rate of one second's `input.rows`; at query + // time its input holds the rows of every retained window. + let group_count = groups(reduction)?; + let rows_per_tumbling_window = match ingestion { + true => u64::try_from( + (u128::from(input.rows) * u128::from(*tumbling_window_width_ms)) + .div_ceil(1_000), + ) + .map_err(|_| AnalyticalCostError::Overflow)?, + false => input.rows.div_ceil(retained_tumbling_window_count), + }; + let input_rows = StateInputRows { + rows_per_state: Some(rows_per_tumbling_window.div_ceil(group_count.max(1))), + non_negative: raw_data_samples_non_negative(), + }; + let (depth, state_bytes) = summary_shape(family, node.parameters.as_ref(), input_rows)?; + let state_bytes = measured_state_bytes.unwrap_or(state_bytes); + let out = EdgeStatistics { + rows: group_count, + bytes: group_count + .checked_mul(state_bytes) + .ok_or(AnalyticalCostError::Overflow)?, + }; + ( + out, + Ok(ResourceEstimate::new( + input.rows as f64 * (depth + 1.0), + out.bytes, + 0, + )), + format!( + "build {} in tumbling windows of {tumbling_window_width_ms} ms for \ + {group_count} groups: {} rows x (1 + depth {depth})", + summary_type_name(family), + input.rows + ), + ) } Payload::ASAP(ASAPOp::SummaryMerge { group_by, .. }) => { // One operation per input state read; one state per group - // out. A rollup's groups are those of its keys, at most its - // input groups. + // out: an input's groups are its states of one window (all + // its states unless it selects a chain's windows). A + // rollup's groups are those of its keys, at most its input + // groups. let mut group_count = 0; for producer in producers { - if matches!( - producer.payload, - Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) - ) { - todo!("#1207 PR 7: the states a merge reads from a chain's windows") - } - group_count = group_count.max(producer.output.rows); + group_count = group_count.max( + producer + .output + .rows + .div_ceil(producer.tumbling_windows.max(1)), + ); } let states = merged_states(producers)?; if let Some(keys) = group_by { @@ -661,52 +778,38 @@ pub fn node_usage<'a>( // State retained between computations: at ingestion time, the window // being built and the completed ones queries read (a chain of N // tumbling windows keeps N + 1), every second. At query time the output is - // reused only within one evaluation, so it is held only while that - // evaluation runs (counted below, once latencies are known). The - // state is what the node holds: its output, or the values an exact - // aggregate keeps per group when that is more. A computation that - // keeps nothing holds its working memory while it runs, once per - // consumer that recomputes it; at query time, that is held for the - // evaluation too (counted in [`plan_cost`](crate::plan_cost), per consumer: a query's - // latency, or a materialized consumer's longest query's, as a - // OneEvaluation state is). + // held only while the node computes (below). The state is what the + // node holds: its output, or the values an exact aggregate keeps per + // group when that is more. A computation that keeps nothing holds its + // working memory while it runs. let state = out.bytes.max(estimate.peak_memory_bytes()); let (materialized_bytes, medium) = match (ingestion, node.lifetime, &node.payload) { (_, Lifetime::None, _) => { let working_memory_bytes = estimate.peak_memory_bytes(); peak_memory_bytes = working_memory_bytes.saturating_mul(multiplier); if !ingestion && working_memory_bytes > 0 { - held = Some((working_memory_bytes, per_second, true)); + held_bytes = Some(working_memory_bytes); } (0, StorageMedium::Memory) } - // A tumbling-window chain materialized across evaluations holds the tumbling windows its - // retention covers ([`tumbling_window_retention_ms`]: N, plus one per tumbling window - // width of the queries' range-evaluation span), all but the - // newest between evaluations and all while one runs. + // A tumbling-window chain materialized across evaluations holds the + // tumbling windows its retention covers, all but the newest between + // evaluations and all while one runs. `state` is one window's. ( false, Lifetime::AcrossEvaluations(medium), Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }), ) => { - let tumbling_windows = tumbling_windows_of(node); let retained = retained_tumbling_window_count; - let between = (state / tumbling_windows) - .checked_mul( - retained - .checked_sub(1) - .ok_or(AnalyticalCostError::Overflow)?, - ) + let between = state + .checked_mul(retained - 1) .ok_or(AnalyticalCostError::Overflow)?; match medium { StorageMedium::Memory => { average_memory_bytes = between as f64; - peak_memory_bytes = match retained == tumbling_windows { - true => state, - false => (state / tumbling_windows) - .checked_mul(retained) - .ok_or(AnalyticalCostError::Overflow)?, - }; + peak_memory_bytes = state + .checked_mul(retained) + .ok_or(AnalyticalCostError::Overflow)?; } StorageMedium::Disk => disk_bytes = between, } @@ -716,7 +819,7 @@ pub fn node_usage<'a>( (_, Lifetime::OneEvaluation(medium), _) => { match medium { StorageMedium::Memory => { - held = Some((state, per_second, false)); + held_bytes = Some(state); peak_memory_bytes = state; } StorageMedium::Disk => disk_bytes = state, @@ -728,18 +831,19 @@ pub fn node_usage<'a>( true, Lifetime::AcrossEvaluations(medium), Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }), - ) => { - let tumbling_windows = tumbling_windows_of(node); - ( - (state / tumbling_windows) - .checked_mul( - retained_tumbling_window_count - .checked_add(1) - .ok_or(AnalyticalCostError::Overflow)?, - ) - .ok_or(AnalyticalCostError::Overflow)?, - medium, - ) + ) => ( + state + .checked_mul( + retained_tumbling_window_count + .checked_add(1) + .ok_or(AnalyticalCostError::Overflow)?, + ) + .ok_or(AnalyticalCostError::Overflow)?, + medium, + ), + // Raw data materialized at ingestion time: the rows it retains. + (true, Lifetime::AcrossEvaluations(medium), Payload::NonASAP(NonASAPOp::Scan { .. })) => { + (edge(raw_retained_rows.unwrap_or(out.rows))?.bytes, medium) } // A whole state materialized across evaluations: it and the one being built. (_, Lifetime::AcrossEvaluations(medium), _) => ( @@ -748,7 +852,7 @@ pub fn node_usage<'a>( ), }; if ingestion && node.lifetime == Lifetime::AcrossEvaluations(StorageMedium::Memory) { - maintained = Some(state / tumbling_windows_of(node)); + maintained = Some(state); } if materialized_bytes > 0 { match medium { @@ -769,6 +873,14 @@ pub fn node_usage<'a>( raw_scan = Some((source, retained)); } } + // Query-time memory is held only while the node computes (concepts doc, + // section 5, "Memory held while computing"): its bytes for its work per + // execution, at its executions per second. Query latency does not + // enter it. + if let Some(bytes) = held_bytes { + average_memory_bytes += bytes as f64 * node_ms / 1_000.0 * per_second; + detail += &format!("; holds {bytes} bytes for {node_ms:.3} ms"); + } // One computation's CPU and scan price times its rate, then the // materialized state's. let cost = usage_prices.map(|prices| { @@ -781,18 +893,26 @@ pub fn node_usage<'a>( if cost.is_some_and(|cost| !cost.is_finite()) { return Err(AnalyticalCostError::Overflow); } - Ok(NodeUsage { + let tumbling_windows = match &node.payload { + Payload::NonASAP(NonASAPOp::TimeRange { range, .. }) + if chain_width_ms(producers).is_some() => + { + selected_tumbling_windows(range, producers)? + } + _ => 1, + }; + Ok(NodeResourceUsageAndCost { output: out, sources, family, unchanged_columns, + tumbling_windows, work_ms_per_execution: node_ms, cpu_ops_per_second: estimate.cpu_ops() * per_second, scanned_bytes_per_second: estimate.scan_bytes() as f64 * per_second, average_memory_bytes, peak_memory_bytes, disk_bytes, - held, maintained, raw_scan, fit, @@ -820,6 +940,30 @@ pub(crate) fn cost_source(per_node: &BTreeMap) -> } } +/// The tumbling-window width of the chain `producers` starts with, if it +/// is one: a `TimeRange` over it selects windows. +fn chain_width_ms(producers: &[ProducerOutput<'_>]) -> Option { + match producers.first()?.payload { + Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { + tumbling_window_width_ms, + .. + }) => Some(*tumbling_window_width_ms), + _ => None, + } +} + +/// The windows a `TimeRange` of `range` selects from the chain +/// `producers` starts with: those inside the range, at least one. +fn selected_tumbling_windows( + range: &std::time::Duration, + producers: &[ProducerOutput<'_>], +) -> Result { + let width_ms = chain_width_ms(producers).unwrap_or(1).max(1); + Ok(u64::try_from(range.as_millis() / u128::from(width_ms)) + .map_err(|_| AnalyticalCostError::Overflow)? + .max(1)) +} + /// The input states a `SummaryMerge` over `producers` reads: every row of /// each. fn merged_states(producers: &[ProducerOutput<'_>]) -> Result { @@ -831,12 +975,12 @@ fn merged_states(producers: &[ProducerOutput<'_>]) -> Result NodeMap<'_> { +pub(crate) fn node_map(dag: &PhysicalASAPDAG) -> NodeMap<'_> { dag.nodes.iter().map(|n| (n.id, n)).collect() } /// The nodes of a physical DAG by id. -pub type NodeMap<'a> = +pub(crate) type NodeMap<'a> = HashMap; /// A summary type's name in reports: a sketch by its algorithm, e.g. @@ -861,3 +1005,6 @@ fn split(input: EdgeStatistics, partitions: u64) -> PartitionStatistics { .collect(), } } + +#[cfg(test)] +mod tests; diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs new file mode 100644 index 000000000..a65166844 --- /dev/null +++ b/crates/cost/src/node/usage/tests.rs @@ -0,0 +1,205 @@ +//! Tests of [`super::node_resource_usage_and_cost`]. + +use std::time::Duration; + +use asap_types::ir::properties::ExecutionDataState; +use asap_types::ir::scalar::ColumnRef; +use asap_types::plan::physical_asap_dag::PhysicalASAPDAGNode; +use asap_types::summary_type::{ExactKind, ExactParams, SummaryUpdate}; + +use super::*; +use crate::read_back_ms; + +fn node(payload: Payload) -> PhysicalASAPDAGNode { + PhysicalASAPDAGNode { + id: 0, + payload, + output_state: ExecutionDataState::QUERY_ROWS, + lifetime: Lifetime::None, + parameters: None, + output_schema: Schema::default(), + retention_ms: None, + selected_physical_candidate: None, + view_coverage: None, + } +} + +fn produced<'a>( + node: &'a PhysicalASAPDAGNode, + usage: &NodeResourceUsageAndCost<'a>, +) -> ProducerOutput<'a> { + ProducerOutput { + payload: &node.payload, + output_schema: &node.output_schema, + output: usage.output, + sources: usage.sources.clone(), + family: usage.family, + unchanged_columns: usage.unchanged_columns.clone(), + tumbling_windows: usage.tumbling_windows, + } +} + +/// A merge of the five 60 s windows a 5m `TimeRange` selects from a +/// chain of 1000 states per window reads 5000 states and outputs one +/// state per group, 1000, not one per state read. +#[test] +fn a_merge_over_a_chain_s_windows_outputs_one_state_per_group() { + let settings = CostInputs::default(); + let data = DataWorkload::default(); + let chain = node(Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { + child: 0, + summary_type: FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::PerEntity, + filter: None, + tumbling_window_width_ms: 60_000, + })); + let chain_output = ProducerOutput { + payload: &chain.payload, + output_schema: &chain.output_schema, + output: EdgeStatistics { + rows: 1_000, + bytes: 16_000, + }, + sources: BTreeSet::new(), + family: StateFamily::Exact(&ExactKind::Sum), + unchanged_columns: BTreeSet::new(), + tumbling_windows: 1, + }; + let windows = node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(300), + kind: TimeRangeKind::Range, + child: 0, + })); + let windows_usage = node_resource_usage_and_cost( + &windows, + &[chain_output], + (1, 1.0), + None, + &data, + &settings, + 1.0, + ) + .unwrap(); + assert_eq!(windows_usage.output.rows, 5_000); + assert_eq!(windows_usage.tumbling_windows, 5); + let merge = node(Payload::ASAP(ASAPOp::SummaryMerge { + children: vec![0], + group_by: None, + })); + let merge_usage = node_resource_usage_and_cost( + &merge, + &[produced(&windows, &windows_usage)], + (1, 1.0), + None, + &data, + &settings, + 1.0, + ) + .unwrap(); + assert_eq!(merge_usage.output.rows, 1_000); + assert_eq!(merge_usage.output.bytes, 16_000); + assert!( + merge_usage + .cost + .detail + .starts_with("merge 5000 states into 1000;"), + "{}", + merge_usage.cost.detail + ); +} + +/// A query-time node holds its state only +/// while it computes: bytes × its work per execution × its executions +/// per second, with no query latency. A KLL state of 2 MB whose own +/// work takes 3 ms, run 0.1 times per second, holds 2,000,000 × 0.003 × +/// 0.1 = 600 byte·s per second; here, a `OneEvaluation` merge run 0.1 +/// times per second. +#[test] +fn query_time_memory_is_held_only_while_the_node_computes() { + let settings = CostInputs::default(); + let data = DataWorkload::default(); + let input = node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(300), + kind: TimeRangeKind::Range, + child: 0, + })); + let states = ProducerOutput { + payload: &input.payload, + output_schema: &input.output_schema, + output: EdgeStatistics { + rows: 5_000, + bytes: 80_000, + }, + sources: BTreeSet::new(), + family: StateFamily::Exact(&ExactKind::Sum), + unchanged_columns: BTreeSet::new(), + tumbling_windows: 1, + }; + let merge = PhysicalASAPDAGNode { + lifetime: Lifetime::OneEvaluation(StorageMedium::Memory), + ..node(Payload::ASAP(ASAPOp::SummaryMerge { + children: vec![0], + group_by: None, + })) + }; + let usage = + node_resource_usage_and_cost(&merge, &[states], (1, 0.1), None, &data, &settings, 1.0) + .unwrap(); + let held: u64 = usage + .cost + .detail + .split("holds ") + .nth(1) + .and_then(|rest| rest.split(' ').next()) + .and_then(|bytes| bytes.parse().ok()) + .expect("the held bytes are in the detail"); + assert!(held > 0); + let expected = held as f64 * usage.work_ms_per_execution / 1_000.0 * 0.1; + assert!( + (usage.average_memory_bytes - expected).abs() <= 1e-9 * expected, + "{} vs {expected}", + usage.average_memory_bytes + ); +} + +/// A query-time `Scan` keeps what its +/// `retention_ms` covers but reads, per execution, only the most its +/// consumers read (no range-evaluation span): under a `TimeRange` of 1 s +/// over 1,000 rows per second it reads 1,000 rows while it keeps +/// 3,601,000 (a 1 h range query with step 1 s over an instant +/// selector). +#[test] +fn a_query_time_scan_reads_what_its_consumers_read_and_keeps_its_retention() { + use asap_types::ir::schema::{DataType, Field}; + use asap_types::workload::DurationMs; + let settings = CostInputs::default(); + let mut data = DataWorkload::sampled_every(["x".to_string()], DurationMs(1_000)); + for facts in data.sources.values_mut() { + facts.series_count = Some(1_000); + } + let scan = PhysicalASAPDAGNode { + retention_ms: Some(3_601_000), + output_schema: Schema::new(vec![ + Field::plain("ts", DataType::Timestamp, false), + Field::plain("value", DataType::Float64, false), + ]), + ..node(Payload::NonASAP(NonASAPOp::Scan { + source: Source::TimeSeries { metric: "x".into() }, + predicates: vec![], + schema: Schema::default(), + })) + }; + let instant = node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(1), + kind: TimeRangeKind::Range, + child: 0, + })); + let read_ms = read_back_ms([1], |_| &instant.payload, |_| vec![], |_| 0, true); + assert_eq!(read_ms, Some(1_000)); + let usage = + node_resource_usage_and_cost(&scan, &[], (1, 1.0), read_ms, &data, &settings, 1.0).unwrap(); + assert_eq!(usage.output.rows, 1_000); + let (_, kept_bytes) = usage.raw_scan.expect("a query-time scan keeps raw data"); + assert_eq!(kept_bytes, usage.output.bytes * 3_601); +} diff --git a/crates/cost/src/plan/cost.rs b/crates/cost/src/plan/cost.rs index fe385145b..dbe4e3829 100644 --- a/crates/cost/src/plan/cost.rs +++ b/crates/cost/src/plan/cost.rs @@ -1,32 +1,32 @@ //! Costing a physical plan DAG node by node: each node's resource usage -//! ([`node_usage`]), then the plan's usage, latency, retention, retained -//! tumbling window chains and cost ([`plan_cost`]). Moved from Stage 3 -//! (`asap-stage3-plan-selection`, whose module doc, "Cost model", describes the -//! model), so Stage 3 computes no cost of its own. +//! ([`node_resource_usage_and_cost`]), then the plan's usage, latency and +//! cost ([`plan_cost`]). Moved from Stage 3 (`asap-stage3-plan-selection`, +//! whose module doc, "Cost model", describes the model), so Stage 3 computes +//! no cost of its own. use std::collections::{BTreeMap, HashMap, HashSet}; use crate::node::analytical::AnalyticalCostError; use crate::node::operator_statistics::EdgeStatistics; use crate::node::state_bytes::{sketch_shape, StateInputRows}; -use crate::node::usage::cost_source; -use crate::plan::execution_demand::{ - computations, consumers, evaluation_rate, query_starts, Consumer, -}; +use crate::node::usage::{cost_source, node_map, NodeMap}; +use crate::plan::execution_demand::{computations, evaluation_rate, query_starts}; +use crate::plan::read_back::read_back_ms; use crate::{lookup, StateFamily}; -use crate::{ - maintained_window_ms, node_usage, query_reach, retained_tumbling_windows, - tumbling_window_retention_ms, tumbling_windows_of, NodeMap, NodeUsage, ProducerOutput, -}; +use crate::{node_resource_usage_and_cost, NodeResourceUsageAndCost, ProducerOutput}; use crate::{ CalibrationFit, CostInputs, CostModel, NodeCost, NodeCostSource, OperatorKind, ResourceUsage, MEASURED_COST_SOURCE, MIXED_COST_SOURCE, }; use asap_types::ir::operator::Source; use asap_types::ir::properties::ExecutionTiming; -use asap_types::plan::physical_asap_dag::{PhysicalASAPDAG, PhysicalASAPNodeId}; +use asap_types::ir::{ASAPOp, NonASAPOp}; +use asap_types::plan::physical_asap_dag::{ + PhysicalASAPDAG, PhysicalASAPDAGNode, PhysicalASAPNodeId, + PhysicalASAPOperatorPayload as Payload, +}; use asap_types::summary_type::SketchParams; -use asap_types::workload::{DataWorkload, QueryRecurrence, RangeEvaluation}; +use asap_types::workload::{DataWorkload, QueryRecurrence, QueryWorkloadEntry, RangeEvaluation}; /// One candidate's usage and price over the whole workload: `usage` is /// the whole candidate's, `per_node` one price per operator node of its @@ -77,20 +77,25 @@ impl QueryLatency { /// ([`computations`]); an ingestion-time node over one second of arriving /// rows. A materialized node's state is counted in byte-seconds per /// second; a tumbling-window chain materialized beyond one evaluation keeps the tumbling windows its -/// retention covers ([`tumbling_window_retention_ms`]). `nodes` is `dag`'s -/// ([`node_map`](crate::node_map)); `recurrence[q]` and -/// `range_evaluations[q]` say how often query `q` runs and how many steps -/// each run evaluates (one when missing). -#[allow(clippy::too_many_arguments)] +/// `retention_ms` ($h_v$) covers. `entries[q]` says how often query `q` +/// runs and how many steps each run evaluates; the formulas are scaled by +/// `settings.frozen_calibration_factor` (1 when unset). pub fn plan_cost( dag: &PhysicalASAPDAG, - nodes: &NodeMap<'_>, - recurrence: &[QueryRecurrence], - range_evaluations: &[Option], + entries: &[QueryWorkloadEntry], data: &DataWorkload, settings: &CostInputs, - formula_scale: f64, ) -> Result<(PlanCost, CalibrationFit), (PhysicalASAPNodeId, AnalyticalCostError)> { + let nodes = &node_map(dag); + let recurrence: &[QueryRecurrence] = &entries + .iter() + .map(|entry| entry.recurrence.clone()) + .collect::>(); + let range_evaluations: &[Option] = &entries + .iter() + .map(|entry| entry.range_evaluation) + .collect::>(); + let formula_scale = settings.frozen_calibration_factor.unwrap_or(1.0); let first = dag.nodes.first().map(|n| n.id).unwrap_or_default(); let table = &settings.measured_operation_costs; let mut fit = CalibrationFit::default(); @@ -100,11 +105,10 @@ pub fn plan_cost( CostModel::UsageOverTime(prices) => Some(prices), _ => None, }; - let EachNodeUsage { + let EachNodeResourceUsageAndCost { priced, multipliers, - reach, - } = each_node_usage( + } = each_node_resource_usage_and_cost( dag, nodes, recurrence, @@ -122,12 +126,6 @@ pub fn plan_cost( // Each node's work in ms on one core: one computation at query time, // one second of arriving rows at ingestion time. let mut work_ms: HashMap = HashMap::new(); - // Query-time memory held only while one evaluation runs, a OneEvaluation - // state in memory or a not-materialized node's working memory: (bytes, - // per second, whether each consumer computes its own copy), - // counted once the evaluations' latencies are known, in node order so - // the sum is the same in every run. - let mut held: BTreeMap = BTreeMap::new(); // The raw data query-time scans read, retained on disk: per source, the // largest scan's (bytes, node). let mut raw_data: HashMap<&Source, (u64, PhysicalASAPNodeId)> = HashMap::new(); @@ -156,9 +154,6 @@ pub fn plan_cost( if node_priced.family != StateFamily::None { family_of.insert(node.id, node_priced.family); } - if let Some(entry) = node_priced.held { - held.insert(node.id, entry); - } if let Some(window) = node_priced.maintained { maintained.push((node.id, window)); } @@ -274,65 +269,6 @@ pub fn plan_cost( for (latency, evaluation_ms) in latency.iter_mut().zip(evaluation_ms_per_query) { latency.query_ms += evaluation_ms; } - // Query-time memory is held for one evaluation: a `OneEvaluation` state from - // its build until the longest query reading it finishes, about that - // query's latency, once per execution. A not-materialized node's working - // memory is computed once per consumer ([`consumers`]), and each copy is - // held while its consumer's evaluation runs: a query's for that query's - // latency, at its rate; a materialized node's until the longest query - // reading that node finishes, at the node's rate, as its `OneEvaluation` - // state is. - let longest_reader_ms = |id: PhysicalASAPNodeId| { - reach - .iter() - .zip(&latency) - .filter(|(reached, _)| reached.contains(&id)) - .map(|(_, l)| l.total_ms()) - .fold(0.0, f64::max) - }; - let consumers_of = consumers(dag); - for (id, (state, per_second, computed_per_consumer)) in held { - // The milliseconds it is held per second: each execution's held - // time times its executions per second, summed. - let held_ms_per_second = match computed_per_consumer { - false => per_second * longest_reader_ms(id), - true => { - let mut sum = 0.0; - for &consumer in consumers_of.get(&id).into_iter().flatten() { - sum += match consumer { - Consumer::Query(query) => { - let rate = evaluation_rate( - recurrence - .get(query) - .map(|r| (r, range_evaluations.get(query).copied().flatten())), - settings.non_repeating_query_horizon_seconds, - ) - .map_err(|error| (id, error))?; - rate * latency.get(query).map_or(0.0, QueryLatency::total_ms) - } - Consumer::Materialized(node) => { - multipliers[&node].1 * longest_reader_ms(node) - } - }; - } - sum - } - }; - let evaluation_ms = match per_second > 0.0 { - true => held_ms_per_second / per_second, - false => 0.0, - }; - let byte_seconds = state as f64 * held_ms_per_second / 1_000.0; - let node = per_node.get_mut(&id).expect("every node is measured"); - usage.average_memory_bytes += byte_seconds; - node.detail += &format!("; holds {state} bytes for {evaluation_ms:.1} ms"); - if let (Some(cost), Some(prices)) = (&mut node.cost_per_second, usage_prices) { - *cost += prices.cost_per_memory_byte_second * byte_seconds; - if !cost.is_finite() { - return Err((id, AnalyticalCostError::Overflow)); - } - } - } usage.peak_cpu_cores = peak_cpu_cores( dag, nodes, @@ -385,21 +321,19 @@ pub fn plan_cost( )) } -/// Every node of a physical DAG priced ([`each_node_usage`]), with each -/// node's `(multiplier, per_second)` ([`computations`]) and the nodes each -/// query reaches at query time ([`query_reach`]). -struct EachNodeUsage<'a> { - priced: HashMap>, +/// Every node of a physical DAG priced ([`each_node_resource_usage_and_cost`]), with each +/// node's `(multiplier, per_second)` ([`computations`]). +struct EachNodeResourceUsageAndCost<'a> { + priced: HashMap>, multipliers: HashMap, - reach: Vec>, } -/// [`node_usage`] over every node of `dag`, children first, with the +/// [`node_resource_usage_and_cost`] over every node of `dag`, children first, with the /// arguments of [`plan_cost`]. `root_rates`, when given, are /// the computations per second of each root's consumer in place of its /// query's own rate ([`computations`]). #[allow(clippy::too_many_arguments)] -fn each_node_usage<'a>( +fn each_node_resource_usage_and_cost<'a>( dag: &'a PhysicalASAPDAG, nodes: &NodeMap<'a>, recurrence: &[QueryRecurrence], @@ -408,7 +342,7 @@ fn each_node_usage<'a>( settings: &'a CostInputs, formula_scale: f64, root_rates: Option<&[f64]>, -) -> Result, (PhysicalASAPNodeId, AnalyticalCostError)> { +) -> Result, (PhysicalASAPNodeId, AnalyticalCostError)> { let first = dag.nodes.first().map(|n| n.id).unwrap_or_default(); let multipliers = computations( dag, @@ -418,20 +352,20 @@ fn each_node_usage<'a>( root_rates, ) .map_err(|error| (first, error))?; - let reach = query_reach(dag, nodes); - let retention_ms = tumbling_window_retention_ms(dag, &reach, range_evaluations); let mut producers_of: HashMap> = HashMap::new(); + let mut consumers_of: HashMap> = HashMap::new(); for e in &dag.edges { producers_of.entry(e.consumer).or_default().push(e.producer); + consumers_of.entry(e.producer).or_default().push(e.consumer); } - let mut priced: HashMap> = HashMap::new(); + let mut priced: HashMap> = HashMap::new(); for node in &dag.nodes { let views: Vec> = producers_of .get(&node.id) .map_or(&[][..], Vec::as_slice) .iter() .map(|p| { - let produced: &NodeUsage<'a> = &priced[p]; + let produced: &NodeResourceUsageAndCost<'a> = &priced[p]; ProducerOutput { payload: &nodes[p].payload, output_schema: &nodes[p].output_schema, @@ -439,17 +373,19 @@ fn each_node_usage<'a>( sources: produced.sources.clone(), family: produced.family, unchanged_columns: produced.unchanged_columns.clone(), + tumbling_windows: produced.tumbling_windows, } }) .collect(); - let node_priced = node_usage( + let window_ms = match node.output_state.timing { + ExecutionTiming::IngestionTime => maintained_window_ms(node.id, nodes, &producers_of), + ExecutionTiming::QueryTime => scan_read_ms(node, nodes, &consumers_of), + }; + let node_priced = node_resource_usage_and_cost( node, &views, multipliers[&node.id], - retained_tumbling_windows(node, &retention_ms), - (node.output_state.timing == ExecutionTiming::IngestionTime) - .then(|| maintained_window_ms(node.id, nodes, &producers_of)) - .flatten(), + window_ms, data, settings, formula_scale, @@ -457,20 +393,41 @@ fn each_node_usage<'a>( .map_err(|error| (node.id, error))?; priced.insert(node.id, node_priced); } - Ok(EachNodeUsage { + Ok(EachNodeResourceUsageAndCost { priced, multipliers, - reach, }) } +/// The window a query-time `Scan` `node` reads per execution: the most +/// offset + range its consumers read ([`read_back_ms`], no range-evaluation +/// span), once however many consumers read it. `None` for any other node, +/// or when a consumer reads it whole. +fn scan_read_ms( + node: &PhysicalASAPDAGNode, + nodes: &NodeMap<'_>, + consumers_of: &HashMap>, +) -> Option { + if !matches!(node.payload, Payload::NonASAP(NonASAPOp::Scan { .. })) { + return None; + } + let readers = |id: PhysicalASAPNodeId| consumers_of.get(&id).cloned().unwrap_or_default(); + read_back_ms( + readers(node.id), + |id| &nodes[&id].payload, + readers, + |_| 0, + true, + ) +} + /// The CPU cores a candidate needs at its peak: enough for its /// ingestion-time work every second (none without any), plus one per /// query with query-time work, all evaluating at once; at least the cores /// its average query-time work fills. fn peak_cpu_cores( dag: &PhysicalASAPDAG, - nodes: &HashMap, + nodes: &HashMap, work_ms: &HashMap, multipliers: &HashMap, latency: &[QueryLatency], @@ -503,7 +460,7 @@ fn peak_cpu_cores( /// one, and one operation otherwise. fn query_latencies( dag: &PhysicalASAPDAG, - nodes: &HashMap, + nodes: &HashMap, output: &HashMap, work_ms: &HashMap, merge_ms: &HashMap, @@ -519,7 +476,7 @@ fn query_latencies( |id: PhysicalASAPNodeId| nodes[&id].output_state.timing == ExecutionTiming::IngestionTime; let compaction_ms = |state: PhysicalASAPNodeId| { let workers = ingest_workers(dag, work_ms, state) as f64; - let states = (output[&state].rows / tumbling_windows_of(nodes[&state])) as f64; + let states = output[&state].rows as f64; let per_merge_ms = merge_ms.get(&state).copied().unwrap_or(formula_merge_ms); (workers - 1.0) * states * per_merge_ms }; @@ -571,3 +528,33 @@ fn ingest_workers( } (ingest_ms_per_s / 1_000.0).ceil().max(1.0) as u64 } + +/// The window an ingestion-time grouping `node` maintains: a tumbling-window chain's +/// tumbling windows, or the time range below it (through filters, projections and +/// time shifts). `None` when there is none. +fn maintained_window_ms( + node: PhysicalASAPNodeId, + nodes: &NodeMap<'_>, + producers_of: &HashMap>, +) -> Option { + match &nodes[&node].payload { + Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { + tumbling_window_width_ms, + .. + }) => return Some(*tumbling_window_width_ms), + Payload::NonASAP(NonASAPOp::TimeRange { range, .. }) => { + return Some(range.as_millis() as u64) + } + _ => {} + } + let producer = *producers_of.get(&node)?.first()?; + match &nodes[&producer].payload { + Payload::NonASAP( + NonASAPOp::TimeRange { .. } + | NonASAPOp::Filter { .. } + | NonASAPOp::Project { .. } + | NonASAPOp::TimeShift { .. }, + ) => maintained_window_ms(producer, nodes, producers_of), + _ => None, + } +} diff --git a/crates/cost/src/plan/mod.rs b/crates/cost/src/plan/mod.rs index 4f8b258d7..cbe5e4c4a 100644 --- a/crates/cost/src/plan/mod.rs +++ b/crates/cost/src/plan/mod.rs @@ -1,8 +1,9 @@ //! The whole physical plan DAG: how often each node is computed -//! ([`execution_demand`]), the plan's cost (`cost`), the tumbling windows it -//! retains (`retention`) and how plans rank (`ranking`). +//! ([`execution_demand`]), the plan's cost (`cost`), how far back the +//! consumers of a time-indexed producer read (`read_back`) and how plans +//! rank (`ranking`). pub(crate) mod cost; pub mod execution_demand; pub(crate) mod ranking; -pub(crate) mod retention; +pub(crate) mod read_back; diff --git a/crates/cost/src/plan/read_back.rs b/crates/cost/src/plan/read_back.rs new file mode 100644 index 000000000..06c09f5ca --- /dev/null +++ b/crates/cost/src/plan/read_back.rs @@ -0,0 +1,66 @@ +//! How far back from the evaluation time the consumers of a time-indexed +//! producer (raw data, a chain, results stored per evaluation time) read its +//! rows: one walk, shared by Stage 3 (over candidates) and [`plan_cost`] +//! (over plan nodes) for retention and for a query-time `Scan`'s read +//! window (concepts doc, section 5, "Cost and retention"). +//! +//! [`plan_cost`]: crate::plan_cost + +use std::collections::BTreeSet; + +use asap_types::ir::{ASAPOp, NonASAPOp, Operator}; + +/// Over the paths from `consumers` (each a reader of the producer) up +/// through `TimeShift`s without `@` (`consumers_of` gives a node's own +/// readers), the most of the shifts' offsets plus the `TimeRange`'s range +/// plus `span_ms` of that `TimeRange` (a range query's span, for retention; +/// 0 for one execution's read). A tumbling-window chain reads its newest +/// window back. Any other reader reads at the evaluation time (its +/// `span_ms`), except over raw data (`reads_raw_data`), which it reads +/// whole: `None`, unbounded, as under a `TimeShift` anchored with `@`. +/// +/// Example: `sum_over_time(m[5m] offset 1h)` reads `m` back 1h + 5m. +pub fn read_back_ms<'a, N: Copy + Ord, C: 'a>( + consumers: impl IntoIterator, + operator_of: impl Fn(N) -> &'a Operator, + consumers_of: impl Fn(N) -> Vec, + span_ms: impl Fn(N) -> i64, + reads_raw_data: bool, +) -> Option { + let mut need_ms: i64 = 0; + // (reader, offset of the shifts below it), walked up through shifts. + let mut pending: Vec<(N, i64)> = consumers.into_iter().map(|c| (c, 0)).collect(); + let mut seen = BTreeSet::new(); + while let Some((consumer, offset_ms)) = pending.pop() { + if !seen.insert((consumer, offset_ms)) { + continue; + } + let read_ms = match operator_of(consumer) { + Operator::NonASAP(NonASAPOp::TimeRange { range, .. }) => { + let range_ms = i64::try_from(range.as_millis()).ok()?; + offset_ms + .checked_add(range_ms)? + .checked_add(span_ms(consumer))? + } + Operator::NonASAP(NonASAPOp::TimeShift { shift, .. }) if shift.at.is_none() => { + let offset_ms = offset_ms.checked_add(shift.offset_ms)?; + pending.extend( + consumers_of(consumer) + .into_iter() + .map(|reader| (reader, offset_ms)), + ); + continue; + } + Operator::NonASAP(NonASAPOp::TimeShift { .. }) => return None, + // A chain builds its newest window from it. + Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { + tumbling_window_width_ms, + .. + }) => offset_ms.checked_add(i64::try_from(*tumbling_window_width_ms).ok()?)?, + _ if reads_raw_data => return None, + _ => offset_ms.checked_add(span_ms(consumer))?, + }; + need_ms = need_ms.max(read_ms); + } + u64::try_from(need_ms).ok() +} diff --git a/crates/cost/src/plan/retention.rs b/crates/cost/src/plan/retention.rs deleted file mode 100644 index 336358152..000000000 --- a/crates/cost/src/plan/retention.rs +++ /dev/null @@ -1,134 +0,0 @@ -//! How long a plan keeps each tumbling window chain and which windows it -//! retains, read from the queries each node reaches. - -use std::collections::{BTreeMap, HashMap, HashSet}; - -use crate::plan::execution_demand::query_starts; -use crate::{node_map, NodeMap}; -use asap_types::ir::properties::ExecutionTiming; -use asap_types::ir::{ASAPOp, NonASAPOp}; -use asap_types::plan::physical_asap_dag::{ - PhysicalASAPDAG, PhysicalASAPNodeId, PhysicalASAPOperatorPayload as Payload, -}; -use asap_types::stages::Lifetime; -use asap_types::workload::RangeEvaluation; - -/// For each tumbling-window chain of `dag` materialized across -/// evaluations, how far back from the newest evaluation its completed -/// windows must be retained, in milliseconds. `reach` is [`query_reach`] of -/// `dag`; `range_evaluations[q]` is query `q`'s (none when missing). -pub fn tumbling_window_retention_ms( - dag: &PhysicalASAPDAG, - reach: &[HashSet], - range_evaluations: &[Option], -) -> BTreeMap { - let nodes = node_map(dag); - dag.nodes - .iter() - .filter_map(|node| match (&node.payload, node.lifetime) { - ( - Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }), - Lifetime::AcrossEvaluations(_), - ) => { - let _ = (&nodes, reach, range_evaluations); - todo!("#1207 PR 7: a chain's retention, max(offset + range) over the paths that read it") - } - _ => None, - }) - .collect() -} - -/// The tumbling-window retention of every materialized tumbling-window chain of `dag` -/// ([`tumbling_window_retention_ms`] over [`query_reach`]), for a plan whose query -/// `q` has range evaluation `range_evaluations[q]` (none when missing): -/// what Stage 2's realizer records for a realized plan. -pub fn tumbling_window_retention_ms_of_plan( - dag: &PhysicalASAPDAG, - range_evaluations: &[Option], -) -> BTreeMap { - tumbling_window_retention_ms(dag, &query_reach(dag, &node_map(dag)), range_evaluations) -} - -/// The panes a chain in `retention_ms` ([`tumbling_window_retention_ms`]) keeps -/// completed: its retention over its pane width, at least its pane count. -pub fn retained_tumbling_windows( - node: &asap_types::plan::physical_asap_dag::PhysicalASAPDAGNode, - retention_ms: &BTreeMap, -) -> u64 { - match (&node.payload, retention_ms.get(&node.id)) { - ( - Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { - tumbling_window_width_ms, - .. - }), - Some(&retention), - ) => (retention / tumbling_window_width_ms.max(&1)).max(tumbling_windows_of(node)), - _ => tumbling_windows_of(node), - } -} - -/// A node's tumbling windows: a tumbling-window chain's count, 1 for any other node. -pub fn tumbling_windows_of(node: &asap_types::plan::physical_asap_dag::PhysicalASAPDAGNode) -> u64 { - match &node.payload { - Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) => { - todo!("#1207 PR 7: the windows a chain retains (h_v)") - } - _ => 1, - } -} - -/// The nodes each query reaches at query time, in workload order: its walk -/// from its roots, stopping at (and including) ingestion-time nodes. -pub fn query_reach( - dag: &PhysicalASAPDAG, - nodes: &HashMap, -) -> Vec> { - query_starts(dag) - .into_iter() - .map(|mut stack| { - let mut seen = HashSet::new(); - while let Some(id) = stack.pop() { - if seen.insert(id) - && nodes[&id].output_state.timing != ExecutionTiming::IngestionTime - { - stack.extend( - dag.edges - .iter() - .filter(|e| e.consumer == id) - .map(|e| e.producer), - ); - } - } - seen - }) - .collect() -} - -/// The window an ingestion-time grouping `node` maintains: a tumbling-window chain's -/// tumbling windows, or the time range below it (through filters, projections and -/// time shifts). `None` when there is none. -pub fn maintained_window_ms( - node: PhysicalASAPNodeId, - nodes: &NodeMap<'_>, - producers_of: &HashMap>, -) -> Option { - match &nodes[&node].payload { - Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) => { - todo!("#1207 PR 7: the window a chain maintains, its retention (h_v)") - } - Payload::NonASAP(NonASAPOp::TimeRange { range, .. }) => { - return Some(range.as_millis() as u64) - } - _ => {} - } - let producer = *producers_of.get(&node)?.first()?; - match &nodes[&producer].payload { - Payload::NonASAP( - NonASAPOp::TimeRange { .. } - | NonASAPOp::Filter { .. } - | NonASAPOp::Project { .. } - | NonASAPOp::TimeShift { .. }, - ) => maintained_window_ms(producer, nodes, producers_of), - _ => None, - } -} diff --git a/crates/devtools/src/bin/asap_planner.rs b/crates/devtools/src/bin/asap_planner.rs index 7399f634e..2230c8df5 100644 --- a/crates/devtools/src/bin/asap_planner.rs +++ b/crates/devtools/src/bin/asap_planner.rs @@ -65,11 +65,8 @@ // `milp_construction_ms` and `milp_solve_ms`; `error` when no plan was // selected. // `materialized_views` are the plan's materialized views as -// `plan_installable` derives them, each with the node of `P1` it is -// installed from (`stage2_node_id`), its position among that node's -// installed nodes (`installed_position`) and their number -// (`installed_count`); `materialized_views_error` when the plan is not -// installable. `warnings`: a PromQL query applying `rate`, `increase` +// `plan_installable` derives them; `materialized_views_error` when the +// plan is not installable. `warnings`: a PromQL query applying `rate`, `increase` // or `irate` to a declared gauge or combining a declared summary's // quantile series across series (`metric_type_warnings`), and a peak memory over 10^12 bytes // when the cost model declares no memory capacity; @@ -231,9 +228,8 @@ fn run(args: Vec) -> Result<(), Failure> { .as_ref() .ok() .and_then(|run| run.run.selection.as_ref().ok()) - .map(|(_plan, _selection)| -> u64 { - todo!("#1207 PR 6: the selected plan's peak memory, from plan_cost(&plan)") - }) + .and_then(|(plan, _)| asap_cost::plan_cost(plan, &entries, &data, &cost_inputs).ok()) + .map(|(cost, _)| cost.usage.peak_memory_bytes) .and_then(|peak_memory_bytes| { large_peak_memory_warning(peak_memory_bytes, &cost_inputs.cost_model) }), @@ -568,43 +564,16 @@ fn arrival_together(earlier: DataArrival, later: DataArrival) -> Option Result, String> { - let (plan, report) = installable_plan_of(plan, None, entries).map_err(|e| e.to_string())?; + let (plan, _) = installable_plan_of(plan, None, entries).map_err(|e| e.to_string())?; let views = plan.materialized_views().map_err(|e| e.to_string())?; - let mut stage2_of = std::collections::HashMap::new(); - for node in report - .iter() - .flat_map(|report| &report.predicted_usage.per_node) - { - for (position, installed) in node.installed_node_ids.iter().enumerate() { - stage2_of.insert( - *installed, - (node.stage3_node_id, position, node.installed_node_ids.len()), - ); - } - } views .iter() - .map(|view| { - let mut value = serde_json::to_value(view).map_err(|e| e.to_string())?; - let (stage2_node_id, position, count) = match stage2_of.get(&view.node_id) { - Some((id, position, count)) => (json!(id), json!(position), json!(count)), - None => (Value::Null, Value::Null, Value::Null), - }; - value["stage2_node_id"] = stage2_node_id; - value["installed_position"] = position; - value["installed_count"] = count; - Ok(value) - }) + .map(|view| serde_json::to_value(view).map_err(|e| e.to_string())) .collect() } diff --git a/crates/devtools/src/planner_fuzz/mod.rs b/crates/devtools/src/planner_fuzz/mod.rs index a444c276f..11e210527 100644 --- a/crates/devtools/src/planner_fuzz/mod.rs +++ b/crates/devtools/src/planner_fuzz/mod.rs @@ -34,7 +34,7 @@ pub mod relations; pub mod report; pub mod shrink; -use std::collections::HashMap; +use std::collections::{BTreeMap, BTreeSet, HashMap}; use std::rc::Rc; use serde::{Deserialize, Serialize}; @@ -445,25 +445,69 @@ impl Planner { todo!("#1207 PR 9: the selected plan's cost (plan_cost) and roots from its PhysicalASAPDAG") } - /// Stage 2 and Stage 3 over the logical plan `roots` of `workload`'s - /// queries (one root per query, in order): every materialization - /// assignment implemented and priced, the cheapest selected, with the - /// same models as [`plan`](Self::plan). Guarantees are composed again - /// first, so a derived node carries the guarantee of what it reads, and - /// summaries are sized for their consumers as Pass 1 sizes them. - /// With `last_root_is_key_filter`, the last root is a `Filter` on - /// group-key columns only and keeps its input's guarantee (whole groups - /// selected by an exact key keep each remaining row's bound), which - /// Stage 1's composition, giving no guarantee to any filter over - /// approximate rows, does not know. - pub fn price( - &self, - workload: &FuzzWorkload, - roots: &[QueryRoot], - last_root_is_key_filter: bool, - ) -> Result { - let _ = (workload, roots, last_root_is_key_filter); - todo!("#1207 PR 6: price a witness plan through the new Stage 2 and Stage 3") + /// The planning stages over the logical plan `roots` of `workload`'s + /// queries (one root per query, in order), with the same models as + /// [`plan`](Self::plan): the selected plan's cost (`plan_cost`) and the + /// cost of the nodes only its last root reads. + pub fn price(&self, workload: &FuzzWorkload, roots: &[QueryRoot]) -> Result { + let planning = workload.planning_workload(); + let entries: Vec<_> = planning.query_workload.entries().collect(); + let cost_inputs = self.cost_inputs(); + let deployment = workload.deployment(); + let models = PlanningModels::builtin() + .with_cost_inputs(&cost_inputs) + .with_deployment_capabilities(&deployment) + .with_ignore_accuracy_guarantees(!self.settings.enforce_accuracy_guarantees); + let run = run_search_over( + roots.to_vec(), + &entries, + &workload.data, + models, + PlanSearchSettings::default(), + ) + .map_err(|e| format!("witness: {e}"))?; + let (plan, _) = run + .run + .selection + .as_ref() + .map_err(|e| format!("witness selection: {e}"))?; + let (cost, _) = asap_cost::plan_cost(plan, &entries, &workload.data, &cost_inputs) + .map_err(|(node, error)| format!("witness cost, node {node}: {error}"))?; + let cost_per_second = cost + .cost_per_second + .ok_or("no cost per second (cost model 3)")?; + let mut inputs: BTreeMap<_, Vec<_>> = BTreeMap::new(); + for edge in &plan.edges { + inputs.entry(edge.consumer).or_default().push(edge.producer); + } + let reached = |root| { + let mut seen = BTreeSet::new(); + let mut stack = vec![root]; + while let Some(node) = stack.pop() { + if seen.insert(node) { + stack.extend(inputs.get(&node).into_iter().flatten().copied()); + } + } + seen + }; + let reached_by_root: Vec> = plan.roots.iter().map(|r| reached(*r)).collect(); + let (last, others) = reached_by_root.split_last().ok_or("no roots")?; + let private: Vec<_> = last + .iter() + .filter(|node| others.iter().all(|set| !set.contains(node))) + .filter_map(|node| cost.per_node.get(node)) + .collect(); + Ok(Priced { + selected: "selected".into(), + cost_per_second, + derived_root: PrivateNodes { + cost_per_second: private.iter().filter_map(|n| n.cost_per_second).sum(), + operators: private + .iter() + .map(|n| format!("{:?}", n.operator)) + .collect(), + }, + }) } /// [`price`](Self::price) of `workload`'s selected logical plan @@ -478,7 +522,7 @@ impl Planner { if let Some(priced) = self.reference_prices.get(&key) { return priced.clone(); } - let priced = self.price(workload, roots, false); + let priced = self.price(workload, roots); self.reference_prices.insert(key, priced.clone()); priced } diff --git a/crates/devtools/src/planner_fuzz/relations.rs b/crates/devtools/src/planner_fuzz/relations.rs index e5ccf73de..4054736ae 100644 --- a/crates/devtools/src/planner_fuzz/relations.rs +++ b/crates/devtools/src/planner_fuzz/relations.rs @@ -1489,7 +1489,7 @@ fn confirming( roots: Option<&Vec>, keep: usize, ) -> Option { - let priced = planner.price(workload, &roots?[..keep], false).ok()?; + let priced = planner.price(workload, &roots?[..keep]).ok()?; Some(WitnessReport { derivation: None, cost_per_second: priced.cost_per_second, @@ -1602,9 +1602,7 @@ fn evaluate_marginal( &base_roots, check.target, ) - .and_then(|roots| { - planner.price(related, &roots, matches!(witness, Witness::Filter { .. })) - }); + .and_then(|roots| planner.price(related, &roots)); match priced { Ok(priced) if best diff --git a/crates/devtools/tests/asap_planner.rs b/crates/devtools/tests/asap_planner.rs index 5244fb941..fc4060de4 100644 --- a/crates/devtools/tests/asap_planner.rs +++ b/crates/devtools/tests/asap_planner.rs @@ -1247,11 +1247,7 @@ fn check_workload_example(root: &Path, example: &Value) { ); let nodes: HashSet<_> = dag.nodes.iter().map(|node| node.id as u64).collect(); for view in selection["materialized_views"].as_array().unwrap() { - assert!(nodes.contains(&view["stage2_node_id"].as_u64().unwrap())); - assert!( - view["installed_position"].as_u64().unwrap() - < view["installed_count"].as_u64().unwrap() - ); + assert!(nodes.contains(&view["node_id"].as_u64().unwrap())); } assert!( selection["search_report"].is_object(), diff --git a/crates/executor/src/capability/mod.rs b/crates/executor/src/capability/mod.rs index 83ce44d83..9f09a636e 100644 --- a/crates/executor/src/capability/mod.rs +++ b/crates/executor/src/capability/mod.rs @@ -9,41 +9,44 @@ //! Stored-state encodings belong to deployments. Full plan acceptance is //! owned by `binding`, which also validates schemas, expressions and inputs. +use crate::summary_kernels::hll_sketch::HLL_PRECISIONS; +use crate::summary_kernels::weighted_frequency::{self, FrequencyAlgorithm}; use crate::Error; use planner_types::ir::schema::FieldDataType as SummaryFamilyType; use planner_types::summary_type::{ - ExactKind, ExactParams, SketchAlgorithm, SketchStatistic, StatisticKind, SummaryTypeCapability, - SummaryUpdate, + ExactKind, ExactParams, SketchAlgorithm, SketchParams, SketchStatistic, StatisticKind, + SummaryTypeCapability, SummaryUpdate, }; /// Check the same contract used by `create_planner_accumulator` before a plan /// is accepted. Execution timing is deliberately not a kernel property. +/// +/// `parameters` is θ, which the exported build node carries +/// (`PhysicalASAPDAGNode::parameters`): required for a sketch family, +/// `None` for an exact one. pub fn validate_summary_kernel( family: &SummaryFamilyType, + parameters: Option<&SketchParams>, input: &SummaryUpdate, ) -> Result<(), String> { if SummaryTypeCapability::shares_one_state_across_groups(family) { - return validate_hydra_cms(family, input); + return validate_hydra_cms(family, parameters, input); } let keyed = match family { - SummaryFamilyType::ExactAggregate(kind, params) => { - use ExactKind as K; - use ExactParams as P; - if !matches!( - (kind, params), - (K::Sum, P::Sum) - | (K::Count, P::Count) - | (K::Min, P::Min) - | (K::Max, P::Max) - | (K::Rate, P::Rate) - | (K::Increase, P::Increase) - ) { + SummaryFamilyType::ExactAggregate(..) => { + if !exact_kernel(family) { return Err(format!("unsupported exact kernel {family:?}")); } + if parameters.is_some() { + return Err("an exact summary has no parameters θ".into()); + } input.item.is_some() } - SummaryFamilyType::Sketch(_) => { - todo!("#1207 PR 6: check θ, which the exported node carries") + SummaryFamilyType::Sketch(algorithm) => { + let Some(parameters) = parameters else { + return Err(format!("a {algorithm:?} summary needs its parameters θ")); + }; + validate_sketch_parameters(algorithm, parameters)? } _ => return Err(format!("unsupported summary kernel {family:?}")), }; @@ -53,15 +56,98 @@ pub fn validate_summary_kernel( Ok(()) } +/// Whether an exact family has a kernel. +fn exact_kernel(family: &SummaryFamilyType) -> bool { + use ExactKind as K; + use ExactParams as P; + matches!( + family, + SummaryFamilyType::ExactAggregate(K::Sum, P::Sum) + | SummaryFamilyType::ExactAggregate(K::Count, P::Count) + | SummaryFamilyType::ExactAggregate(K::Min, P::Min) + | SummaryFamilyType::ExactAggregate(K::Max, P::Max) + | SummaryFamilyType::ExactAggregate(K::Rate, P::Rate) + | SummaryFamilyType::ExactAggregate(K::Increase, P::Increase) + ) +} + +/// Check θ of a per-instance sketch of `algorithm` against what its kernel +/// builds; whether the kernel is keyed (its updates name an item). +fn validate_sketch_parameters( + algorithm: &SketchAlgorithm, + parameters: &SketchParams, +) -> Result { + use SketchAlgorithm as A; + use SketchParams as P; + let keyed = match (algorithm, parameters) { + // `asap_sketchlib` silently caps a larger `k`. + (A::Kll, P::Kll { k }) if (8..=planner_types::summary_type::KLL_MAX_K).contains(k) => false, + (A::DDSketch, P::DDSketch { alpha }) + if alpha.is_finite() && *alpha > 0.0 && *alpha < 1.0 => + { + false + } + (A::Hll, P::Hll { precision }) if HLL_PRECISIONS.contains(precision) => false, + (A::Kmv, P::Kmv { k }) if *k >= crate::summary_kernels::kmv::KMV_MIN_K => false, + (A::Cms, P::Cms { width, depth }) if valid_matrix(*width, *depth) => true, + // The kernel's shape rules (an odd depth, for the median). + (A::CountSketch, P::CountSketch { width, depth }) + if valid_matrix(*width, *depth) + && weighted_frequency::check_shape( + FrequencyAlgorithm::CountSketch, + *width as usize, + *depth as usize, + ) + .is_ok() => + { + true + } + (A::CmsWithHeap | A::CountSketchWithHeap, _) + if weighted_frequency::WeightedFrequency::configuration(algorithm, parameters) + .is_ok_and(|(_, width, depth, _)| valid_matrix(width as u32, depth as u32)) => + { + true + } + ( + A::UnivMon, + P::UnivMon { + heap_size, + sketch_rows, + sketch_cols, + layers, + }, + ) if crate::summary_kernels::univmon::check_shape( + *heap_size as usize, + *sketch_rows as usize, + *sketch_cols as usize, + usize::from(*layers), + ) + .is_ok() => + { + false + } + _ => { + return Err(format!( + "unsupported kernel or invalid parameters: {algorithm:?} {parameters:?}" + )) + } + }; + Ok(keyed) +} + /// The only Hydra algorithm with a kernel: Hydra over Count-Min. Each update /// adds one item column's value to its group, with weight 1 (a row count), a /// positive integer constant or a weight column. The planner offers a /// Count-Min build only over weights proven non-negative (a Count-Min point /// count only overestimates when no weight is negative), and the build /// rejects a negative weight value. -fn validate_hydra_cms(family: &SummaryFamilyType, input: &SummaryUpdate) -> Result<(), String> { +fn validate_hydra_cms( + family: &SummaryFamilyType, + parameters: Option<&SketchParams>, + input: &SummaryUpdate, +) -> Result<(), String> { use planner_types::summary_type::SummaryInputExpr; - hydra_cms_shape(family)?; + hydra_cms_shape(family, parameters)?; if !matches!(input.item, Some(SummaryInputExpr::Column(_))) { return Err("HydraCms requires one item column".into()); } @@ -97,14 +183,44 @@ pub(crate) fn constant_row_weight(weight: f64) -> Result Result<(), String> { + match family { + SummaryFamilyType::Sketch(SketchAlgorithm::HydraCms) => Ok(()), + _ => Err("a Hydra algorithm other than HydraCms has no registered kernel".into()), + } +} + +/// `(shared_rows, shared_columns, width, depth)` of a HydraCms state whose +/// θ is `parameters` (`SketchParams::Hydra(HydraParams::HydraCms { .. })`): +/// the grid's own rows and columns, and the width and depth of the +/// per-group Count-Min it emulates. pub(crate) fn hydra_cms_shape( family: &SummaryFamilyType, + parameters: Option<&SketchParams>, ) -> Result<(usize, usize, usize, usize), String> { - let SummaryFamilyType::Sketch(SketchAlgorithm::HydraCms) = family else { - return Err("a Hydra algorithm other than HydraCms has no registered kernel".into()); + use planner_types::summary_type::HydraParams; + validate_hydra_cms_family(family)?; + let Some(SketchParams::Hydra(HydraParams::HydraCms { + width, + depth, + shared_rows, + shared_columns, + })) = parameters + else { + return Err(format!( + "a HydraCms summary needs HydraCms parameters θ, got {parameters:?}" + )); }; - todo!("#1207 PR 6: the Hydra grid's sizes are θ, which the exported node carries") + if !valid_matrix(*width, *depth) || !valid_matrix(*shared_columns, *shared_rows) { + return Err("invalid HydraCms dimensions".into()); + } + Ok(( + *shared_rows as usize, + *shared_columns as usize, + *width as usize, + *depth as usize, + )) } /// A filtered `SummaryAgg` never runs at ingestion time. An ingestion-time @@ -124,7 +240,6 @@ pub fn validate_ingestion_time_summary( } } -#[allow(dead_code)] // Used again once θ comes from the exported node. fn valid_matrix(width: u32, depth: u32) -> bool { // Construction uses the kernel's native row hashing, so no encoded-size // limit applies here. @@ -153,38 +268,36 @@ pub(crate) fn unit_frequency_column( } } +/// Whether the executor holds, merges and reads states of `family` natively. +/// θ is not part of a family: each kernel checks it when building a state, +/// and merges check that their states have one shape. pub fn validate_native_family(family: &SummaryFamilyType) -> Result<(), Error> { use planner_types::summary_type::SketchAlgorithm as A; - if let SummaryFamilyType::Sketch(kind) = family { - // Plain Count-Min is native as stored state only: it merges and reads - // its bare count, but the DAG does not build it from rows. HydraCms - // is built by the all-groups summary build. - if let A::HydraCms = kind { - return hydra_cms_shape(family).map(|_| ()).map_err(Error::Invalid); - } - if let A::Cms = kind { - todo!("#1207 PR 6: check the Count-Min θ, which the exported node carries") - } - if matches!(kind, A::CmsWithHeap | A::CountSketchWithHeap) { - todo!("#1207 PR 6: check the weighted-frequency θ, which the exported node carries") - } - } - match family { - SummaryFamilyType::ExactAggregate(..) => {} - SummaryFamilyType::Sketch(A::Kll | A::DDSketch | A::Hll | A::Kmv | A::UnivMon) => {} - _ => { - return Err(Error::Invalid( - "summary family has no native DAG state implementation".into(), - )) - } + let native = match family { + SummaryFamilyType::ExactAggregate(..) => exact_kernel(family), + // HydraCms is built by the all-groups summary build. Plain Count-Min + // is native as stored state only: it merges and reads its bare + // count, but the DAG does not build it from rows. + SummaryFamilyType::Sketch( + A::HydraCms + | A::Cms + | A::CmsWithHeap + | A::CountSketchWithHeap + | A::Kll + | A::DDSketch + | A::Hll + | A::Kmv + | A::UnivMon, + ) => true, + _ => false, + }; + if native { + Ok(()) + } else { + Err(Error::Invalid(format!( + "summary family {family:?} has no native DAG state implementation" + ))) } - crate::capability::validate_summary_kernel( - family, - &planner_types::summary_type::SummaryUpdate::column( - planner_types::ir::scalar::ColumnRef::SampleValue, - ), - ) - .map_err(Error::Invalid) } /// What the executor evaluates from a per-instance state of each family it diff --git a/crates/executor/src/capability/tests.rs b/crates/executor/src/capability/tests.rs index eb54f5975..16e7e54d4 100644 --- a/crates/executor/src/capability/tests.rs +++ b/crates/executor/src/capability/tests.rs @@ -9,3 +9,25 @@ fn filtered_summary_runs_only_at_query_time() { assert!(validate_ingestion_time_summary(Some(&filter)).is_err()); assert!(validate_ingestion_time_summary::(None).is_ok()); } + +// θ is checked with the kernel, not with the family: a Count Sketch with +// heap of even depth is refused (its estimate is a median of rows), +// while its family stays native. +#[test] +fn weighted_frequency_parameters_are_checked_with_the_kernel() { + let family = SummaryFamilyType::Sketch(SketchAlgorithm::CountSketchWithHeap); + let mut update = SummaryUpdate::column(planner_types::ir::scalar::ColumnRef::SampleValue); + update.item = Some(planner_types::summary_type::SummaryInputExpr::Column( + planner_types::ir::scalar::ColumnRef::Named("host".into()), + )); + let theta = |depth, heap_size| SketchParams::CountSketchWithHeap { + width: 32, + depth, + heap_size, + }; + assert!(validate_native_family(&family).is_ok()); + assert!(validate_summary_kernel(&family, Some(&theta(5, 8)), &update).is_ok()); + assert!(validate_summary_kernel(&family, Some(&theta(4, 8)), &update).is_err()); + assert!(validate_summary_kernel(&family, Some(&theta(5, 0)), &update).is_err()); + assert!(validate_summary_kernel(&family, None, &update).is_err()); +} diff --git a/crates/executor/src/operators/mod.rs b/crates/executor/src/operators/mod.rs index cfdca4446..ee8b917b1 100644 --- a/crates/executor/src/operators/mod.rs +++ b/crates/executor/src/operators/mod.rs @@ -11,7 +11,7 @@ use planner_types::ir::scalar::ColumnRef; use planner_types::ir::schema::{ DataType, Field as SummaryField, FieldDataType as SummaryFamilyType, Schema, }; -use planner_types::summary_type::SummaryUpdate; +use planner_types::summary_type::{SketchParams, SummaryUpdate}; use std::{collections::BTreeMap, sync::Arc}; pub(crate) mod common; use crate::expressions::ordered; @@ -112,7 +112,12 @@ enum Kind { }, Project(Vec), Filter(Expression), - /// A `TimeRange` over a `Scan` ([`Operator::time_range`]). + /// A `TimeShift` with an `@` anchor ([`Operator::evaluate_at`]). + EvaluateAt { + at: planner_types::ir::operator::AtModifier, + offset_ms: i64, + }, + /// A `TimeRange` over a `Scan` or a chain ([`Operator::time_range`]). TimeRange { time: usize, end_offset_ms: i64, @@ -157,6 +162,10 @@ enum Kind { /// (built by [`Operator::per_group_summary_build`]). PerGroupSummaryBuild { family: SummaryFamilyType, + /// θ of the states: the exported build node's `parameters`; `None` + /// for an exact family. + #[serde(default, skip_serializing_if = "Option::is_none")] + parameters: Option, value: usize, time: Option, groups: Vec, @@ -168,6 +177,10 @@ enum Kind { /// with heap (built by [`Operator::per_group_keyed_summary_build`]). PerGroupKeyedSummaryBuild { family: SummaryFamilyType, + /// θ of the states: the exported build node's `parameters`; `None` + /// for an exact family. + #[serde(default, skip_serializing_if = "Option::is_none")] + parameters: Option, items: Vec, /// What each row adds to its item's count. row_weight: RowWeight, @@ -180,6 +193,10 @@ enum Kind { /// HydraCms grid (built by [`Operator::all_groups_summary_build`]). AllGroupsSummaryBuild { family: SummaryFamilyType, + /// θ of the states: the exported build node's `parameters`; `None` + /// for an exact family. + #[serde(default, skip_serializing_if = "Option::is_none")] + parameters: Option, item: usize, /// What each row adds to its item's count. row_weight: RowWeight, @@ -200,6 +217,11 @@ enum Kind { /// with no groups; `false` (no output row) by default. #[serde(default, skip_serializing_if = "std::ops::Not::not")] emit_empty_global: bool, + /// θ of the merged states, which the empty global state takes: the + /// `parameters` of the build node that produced them. `None` for an + /// exact family, and without `emit_empty_global`. + #[serde(default, skip_serializing_if = "Option::is_none")] + parameters: Option, }, Evaluation { state: usize, @@ -217,9 +239,11 @@ pub struct Operator { impl Operator { pub(crate) fn row_preserving_input(&self) -> Option { match self.kind { - Kind::Filter(_) | Kind::Sort { .. } | Kind::Limit { .. } | Kind::SemiJoin { .. } => { - Some(0) - } + Kind::Filter(_) + | Kind::Sort { .. } + | Kind::Limit { .. } + | Kind::SemiJoin { .. } + | Kind::EvaluateAt { .. } => Some(0), _ => None, } } @@ -395,6 +419,7 @@ impl PhysicalOperator for Operator { Kind::Project(_) => "Project", Kind::Filter(_) => "Filter", Kind::TimeRange { .. } => "TimeRange", + Kind::EvaluateAt { .. } => "TimeShift", Kind::Limit { .. } => "Limit", Kind::Sort { .. } => "Sort", Kind::Aggregate { .. } => "Aggregate", @@ -414,6 +439,14 @@ impl PhysicalOperator for Operator { series_window::validate_context(self, context)?; self.evaluation_range(context).map(|_| ()) } + fn input_context(&self, context: &RunContext) -> Result { + match self.kind { + Kind::EvaluateAt { at, offset_ms } => { + time_range::anchored_context(at, offset_ms, context) + } + _ => Ok(context.clone()), + } + } fn input_schemas(&self) -> Vec { self.inputs.clone() } @@ -450,6 +483,7 @@ impl PhysicalOperator for Operator { | Kind::SeriesRelabel { .. } => series_labels::execute(self, inputs, context), Kind::Filter(_) => filter::execute(self, inputs, context), Kind::TimeRange { .. } => time_range::execute(self, inputs, context), + Kind::EvaluateAt { .. } => time_range::execute_evaluate_at(inputs, context), Kind::Limit { .. } => limit::execute(self, inputs, context), Kind::Sort { .. } => sort::execute(self, inputs, context), Kind::Window { .. } | Kind::Aggregate { .. } => { diff --git a/crates/executor/src/operators/summary/mod.rs b/crates/executor/src/operators/summary/mod.rs index d7b94f9ac..7faab29cd 100644 --- a/crates/executor/src/operators/summary/mod.rs +++ b/crates/executor/src/operators/summary/mod.rs @@ -29,34 +29,95 @@ pub enum SummaryEvaluation { } impl Operator { - /// One keyed summary state per group (a CMS or Count Sketch with heap): - /// each row adds its `row_weight` to the tuple of its `items` values, in - /// its group's state. A weight column must be Float64 or Int64, the - /// types the kernel's `f64` weights hold; a NULL weight adds nothing. + /// One keyed summary state per group (a CMS or Count Sketch with heap) + /// of θ `parameters`: each row adds its `row_weight` to the tuple of its + /// `items` values, in its group's state. A weight column must be Float64 + /// or Int64, the types the kernel's `f64` weights hold; a NULL weight + /// adds nothing. pub fn per_group_keyed_summary_build( input: SchemaRef, family: SummaryFamilyType, + parameters: Option, items: Vec, row_weight: RowWeight, groups: Vec, ) -> Result { - let _ = (input, family, items, row_weight, groups); - todo!("#1207 PR 6: the weighted-frequency θ, which the exported node carries") + use crate::summary_kernels::weighted_frequency::WeightedFrequency; + use planner_types::summary_type::SketchAlgorithm as A; + crate::values::validate_family(&family)?; + let SummaryFamilyType::Sketch(algorithm @ (A::CmsWithHeap | A::CountSketchWithHeap)) = + &family + else { + return Err(invalid("keyed sketch required")); + }; + let Some(theta) = ¶meters else { + return Err(invalid("a keyed sketch needs its parameters θ")); + }; + WeightedFrequency::configuration(algorithm, theta)?; + validate_groups(&input, &groups)?; + if items.is_empty() { + return Err(invalid("keyed summary requires item columns")); + } + if let RowWeight::Column(weight) = row_weight { + if !matches!( + plain(&input, weight)?.0, + DataType::Float64 | DataType::Int64 + ) { + return Err(invalid("keyed summary weights must be Float64 or Int64")); + } + } + for &item in &items { + if !matches!( + plain(&input, item)?.0, + DataType::Utf8 + | DataType::Timestamp + | DataType::Int64 + | DataType::Float64 + | DataType::Bool + | DataType::Null + ) { + return Err(invalid("unsupported keyed summary identity type")); + } + } + let mut fields = groups + .iter() + .map(|&i| input.fields[i].clone()) + .collect::>(); + fields.push(SummaryField { + name: "state".into(), + dtype: family.clone(), + nullable: false, + }); + Ok(Self { + kind: Kind::PerGroupKeyedSummaryBuild { + family, + parameters, + items, + row_weight, + groups, + filter: None, + }, + inputs: vec![input], + output: schema(fields), + }) } - /// One summary state shared by all groups (a HydraCms grid), emitted as - /// one row per group that reads its own part of it. Each row adds its + /// One summary state shared by all groups (a HydraCms grid sized by θ + /// `parameters`, `SketchParams::Hydra(HydraParams::HydraCms { .. })`), + /// emitted as one row per group that reads its own part of it. Each row adds its /// `row_weight` to its `item` value in its group; a NULL weight adds /// nothing. A constant weight is at most `i32::MAX` (asap_sketchlib's /// Hydra adds `i32` weights). pub fn all_groups_summary_build( input: SchemaRef, family: SummaryFamilyType, + parameters: Option, item: usize, row_weight: RowWeight, groups: Vec, ) -> Result { crate::values::validate_family(&family)?; - crate::factory::create_hydra_cms(&family).map_err(Error::Invalid)?; + // The grid's shape only: the grid itself is allocated when it runs. + crate::capability::hydra_cms_shape(&family, parameters.as_ref()).map_err(Error::Invalid)?; validate_groups(&input, &groups)?; if !matches!( plain(&input, item)?, @@ -96,6 +157,7 @@ impl Operator { Ok(Self { kind: Kind::AllGroupsSummaryBuild { family, + parameters, item, row_weight, groups, @@ -120,22 +182,52 @@ impl Operator { } Ok(self) } + /// The `k` heaviest items of each input row's keyed state (a CMS or + /// Count Sketch with heap), one output row per item. The state's heap + /// capacity is its θ, which the state carries: a state whose heap holds + /// fewer than `k` items is refused when it is read. pub fn keyed_evaluation( input: SchemaRef, state: usize, k: usize, output: SchemaRef, ) -> Result { - let _ = (input, state, k, output); - todo!("#1207 PR 6: the weighted-frequency capacity of θ, which the exported node carries") + use planner_types::summary_type::SketchAlgorithm as A; + let family = &field(&input, state)?.dtype; + crate::values::validate_family(family)?; + if !matches!( + family, + SummaryFamilyType::Sketch(A::CmsWithHeap | A::CountSketchWithHeap) + ) { + return Err(invalid("keyed evaluation requires a keyed summary state")); + } + if output.fields.len() <= input.fields.len() { + return Err(invalid("invalid keyed evaluation shape")); + } + if state + 1 != input.fields.len() + || output.fields[..state] != input.fields[..state] + || output.fields.last().unwrap().dtype != SummaryFamilyType::Plain(DataType::Float64) + { + return Err(invalid( + "keyed evaluation must preserve partitions and return a Float64 score", + )); + } + crate::values::validate_schema(&output)?; + Ok(Self { + kind: Kind::KeyedEvaluation { state, k }, + inputs: vec![input], + output, + }) } /// One summary state per group, updated from the value of one input /// column per row (`time` orders counter samples): an exact aggregate, /// a quantile/distinct sketch, or an unkeyed frequency sketch (UnivMon) - /// over that column's values. + /// over that column's values. `parameters` is the sketch's θ (`None` + /// for an exact aggregate). pub fn per_group_summary_build( input: SchemaRef, family: SummaryFamilyType, + parameters: Option, value: usize, time: Option, groups: Vec, @@ -164,6 +256,7 @@ impl Operator { } crate::capability::validate_summary_kernel( &family, + parameters.as_ref(), &SummaryUpdate::column(ColumnRef::SampleValue), ) .map_err(Error::Invalid)?; @@ -179,6 +272,7 @@ impl Operator { Ok(Self { kind: Kind::PerGroupSummaryBuild { family, + parameters, value, time, groups, @@ -208,6 +302,7 @@ impl Operator { state, groups, emit_empty_global: false, + parameters: None, }, inputs: vec![input], output: schema(fields), @@ -216,14 +311,25 @@ impl Operator { /// A merge into no groups that outputs one empty state when it has no /// input state: the global SQL aggregate a rollup of a relation's /// grouped states to no keys stands for, whose one row is NULL over no - /// input rows. - pub fn with_emit_empty_global(mut self) -> Result { + /// input rows. `parameters` is the merged states' θ, which the empty + /// state takes: the exported build node's `parameters` (`None` for an + /// exact family). + pub fn with_emit_empty_global( + mut self, + parameters: Option, + ) -> Result { match &mut self.kind { Kind::SummaryMerge { + state, groups, emit_empty_global, - .. - } if groups.is_empty() => *emit_empty_global = true, + parameters: slot, + } if groups.is_empty() => { + // An empty state of this family and θ can be built. + StateColumnUpdater::new(&self.inputs[0].fields[*state].dtype, parameters.as_ref())?; + *emit_empty_global = true; + *slot = parameters; + } _ => { return Err(invalid( "an empty global state needs a merge into no groups", @@ -348,6 +454,7 @@ pub(super) fn execute<'a>( match &operator.kind { Kind::PerGroupSummaryBuild { family, + parameters, value, time, groups, @@ -357,6 +464,7 @@ pub(super) fn execute<'a>( input, PerGroupBuild { family, + parameters: parameters.as_ref(), value: *value, time: *time, groups, @@ -372,6 +480,7 @@ pub(super) fn execute<'a>( .boxed_local()), Kind::PerGroupKeyedSummaryBuild { family, + parameters, items, row_weight, groups, @@ -381,6 +490,7 @@ pub(super) fn execute<'a>( input, KeyedBuild { family, + parameters: parameters.as_ref(), items, row_weight: *row_weight, groups, @@ -395,6 +505,7 @@ pub(super) fn execute<'a>( .boxed_local()), Kind::AllGroupsSummaryBuild { family, + parameters, item, row_weight, groups, @@ -404,6 +515,7 @@ pub(super) fn execute<'a>( input, AllGroupsBuild { family, + parameters: parameters.as_ref(), item: *item, row_weight: *row_weight, groups, @@ -438,6 +550,7 @@ pub(super) fn execute_merge<'a>( state, groups, emit_empty_global, + parameters, } = &operator.kind else { unreachable!() @@ -446,7 +559,7 @@ pub(super) fn execute_merge<'a>( input, *state, groups, - *emit_empty_global, + emit_empty_global.then_some(parameters.as_ref()), &input_schema, &output, &context, @@ -494,6 +607,8 @@ fn grouped_states( /// A `PerGroupSummaryBuild`'s parameters. struct PerGroupBuild<'a> { family: &'a SummaryFamilyType, + /// θ (`None` for an exact family). + parameters: Option<&'a SketchParams>, value: usize, time: Option, groups: &'a [usize], @@ -516,7 +631,7 @@ async fn build_per_group_summary( .filter .map(|filter| filter.at_evaluation_time(context)); let mut table = GroupTable::new(build.groups.to_vec()); - let mut updater = StateColumnUpdater::new(build.family)?; + let mut updater = StateColumnUpdater::new(build.family, build.parameters)?; let mut work = Cooperative::new(context); let mut workspace = Workspace::new(context)?; // PromQL aggregation of an empty vector produces no sample. @@ -591,6 +706,8 @@ async fn build_per_group_summary( /// A `PerGroupKeyedSummaryBuild`'s parameters. struct KeyedBuild<'a> { family: &'a SummaryFamilyType, + /// θ. + parameters: Option<&'a SketchParams>, items: &'a [usize], row_weight: RowWeight, groups: &'a [usize], @@ -654,7 +771,7 @@ async fn build_per_group_keyed_summary( .filter .map(|filter| filter.at_evaluation_time(context)); let mut table = GroupTable::new(build.groups.to_vec()); - let mut updater = StateColumnUpdater::new(build.family)?; + let mut updater = StateColumnUpdater::new(build.family, build.parameters)?; let mut work = Cooperative::new(context); let mut workspace = Workspace::new(context)?; let mut items = Vec::with_capacity(build.items.len()); @@ -696,6 +813,8 @@ async fn build_per_group_keyed_summary( /// An `AllGroupsSummaryBuild`'s parameters. struct AllGroupsBuild<'a> { family: &'a SummaryFamilyType, + /// θ: the Hydra grid's sizes. + parameters: Option<&'a SketchParams>, item: usize, row_weight: RowWeight, groups: &'a [usize], @@ -717,7 +836,8 @@ async fn build_all_groups_summary( .map(|filter| filter.at_evaluation_time(context)); use crate::summary_kernels::HydraCmsGroup; use crate::AggregateCore; - let mut grid = crate::factory::create_hydra_cms(build.family).map_err(Error::Invalid)?; + let mut grid = + crate::factory::create_hydra_cms(build.family, build.parameters).map_err(Error::Invalid)?; let grid_bytes = grid.approx_memory_bytes(); let mut workspace = Workspace::new(context)?; workspace.set_bytes(grid_bytes)?; @@ -794,12 +914,13 @@ fn state_indices(batch: &Batch, state: usize) -> Result<(&StateColumn, &UInt32Ar } /// Merge the states of each group across all input rows; with -/// `emit_empty_global`, one empty state when there is no input state. +/// `empty_global` (`Some` of the states' θ), one empty state when there is +/// no input state. async fn merge_summary( mut input: Input<'_, Batch>, state_column: usize, groups: &[usize], - emit_empty_global: bool, + empty_global: Option>, input_schema: &SchemaRef, output: &SchemaRef, context: &RunContext, @@ -827,9 +948,9 @@ async fn merge_summary( ); } // A global SQL aggregate's one row over no input rows: one empty state. - if entries.is_empty() && emit_empty_global { + if let (true, Some(parameters)) = (entries.is_empty(), empty_global) { table.add_empty_group()?; - let mut updater = StateColumnUpdater::new(family)?; + let mut updater = StateColumnUpdater::new(family, parameters)?; updater.add_group()?; return grouped_states(&table, updater.seal(), input_schema, output); } @@ -1198,6 +1319,13 @@ fn keyed_evaluation( .as_any() .downcast_ref::() .ok_or_else(|| invalid("weighted frequency typed state required"))?; + // The heap capacity is the state's θ. + let (_, _, capacity) = summary.shape(); + if k > capacity { + return Err(Error::Invalid(format!( + "top-{k} exceeds the state's heap capacity {capacity}" + ))); + } for items in summary.rows(k) { source_rows.push(row as u32); for ((column, value), field) in item_columns diff --git a/crates/executor/src/operators/time_range.rs b/crates/executor/src/operators/time_range.rs index 9e0540b62..f7f044f55 100644 --- a/crates/executor/src/operators/time_range.rs +++ b/crates/executor/src/operators/time_range.rs @@ -1,8 +1,11 @@ -//! A `TimeRange` over a `Scan`: the rows of one window on the time column, -//! for a SQL table and a time series alike. A time series' stale markers -//! (the Prometheus end-of-series NaN) are never rows; an ordinary NaN is. +//! A `TimeRange` over a `Scan` or a tumbling-window chain: the rows of one +//! window on the time column, for a SQL table, a time series and a chain's +//! `window_end` alike. A time series' stale markers (the Prometheus +//! end-of-series NaN) are never rows; an ordinary NaN is. And a `TimeShift` +//! with an `@` anchor, which evaluates its input at the anchor's time. use super::*; use arrow::array::Array; +use planner_types::ir::operator::AtModifier; impl Operator { /// The rows of `input` whose `time` lies in the window of a @@ -50,6 +53,66 @@ impl Operator { } } +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` + /// ([`anchored_context`]). + pub fn evaluate_at(input: SchemaRef, at: AtModifier, offset_ms: i64) -> Result { + Ok(Self { + kind: Kind::EvaluateAt { at, offset_ms }, + inputs: vec![input.clone()], + output: input, + }) + } +} + +/// The context an `@`-anchored input runs in: a query run evaluated at the +/// anchor's time minus `offset_ms`. `@ ` pins `t`; `@ start()` and `@ +/// end()` the request's bounds ([`RunContext::with_query_range`]), which an +/// instant query (a run without them) has both at its evaluation time, as +/// in Prometheus. An ingestion run has no anchor time. +pub(super) fn anchored_context( + at: AtModifier, + offset_ms: i64, + context: &RunContext, +) -> Result { + let crate::runtime::Scope::Query { evaluation_time_ms } = context.scope else { + return Err(invalid( + "an @-anchored node is evaluated only at query time", + )); + }; + let (start, end) = context + .query_range() + .unwrap_or((evaluation_time_ms, evaluation_time_ms)); + let anchor = match at { + AtModifier::Timestamp(ms) => ms, + AtModifier::Start => start, + AtModifier::End => end, + }; + let time = anchor + .checked_sub(offset_ms) + .ok_or_else(|| invalid("anchored time overflows Int64"))?; + Ok(context.at_evaluation_time(time)) +} + +/// An `@`-anchored node's rows: its input's, unchanged. +pub(super) fn execute_evaluate_at<'a>( + mut inputs: Vec>, + context: RunContext, +) -> Result, Error> { + let input = inputs + .pop() + .ok_or_else(|| invalid("anchored input missing"))?; + Ok(input + .map(move |batch| { + if context.is_cancelled() { + return Err(Error::Cancelled); + } + Ok((*batch?).clone()) + }) + .boxed_local()) +} + /// `(start, end]` of a window `width_ms` wide in this run: ending /// `end_offset_ms` before the evaluation time `T` in a query run; ending at /// the run's `window_end_ms` in an ingestion run, which builds the window diff --git a/crates/executor/src/operators/unchecked.rs b/crates/executor/src/operators/unchecked.rs index 173dce2d1..3ab8bf172 100644 --- a/crates/executor/src/operators/unchecked.rs +++ b/crates/executor/src/operators/unchecked.rs @@ -108,6 +108,7 @@ impl TryFrom for Operator { )? } Kind::Filter(expression) => Operator::filter(input(0)?, expression)?, + Kind::EvaluateAt { at, offset_ms } => Operator::evaluate_at(input(0)?, at, offset_ms)?, Kind::TimeRange { time, end_offset_ms, @@ -168,16 +169,25 @@ impl TryFrom for Operator { )?, Kind::PerGroupSummaryBuild { family, + parameters, value, time, groups, filter, } => with_filter( - Operator::per_group_summary_build(input(0)?, family, value, time, groups)?, + Operator::per_group_summary_build( + input(0)?, + family, + parameters, + value, + time, + groups, + )?, filter, )?, Kind::PerGroupKeyedSummaryBuild { family, + parameters, items, row_weight, groups, @@ -186,6 +196,7 @@ impl TryFrom for Operator { Operator::per_group_keyed_summary_build( input(0)?, family, + parameters, items, row_weight, groups, @@ -194,12 +205,20 @@ impl TryFrom for Operator { )?, Kind::AllGroupsSummaryBuild { family, + parameters, item, row_weight, groups, filter, } => with_filter( - Operator::all_groups_summary_build(input(0)?, family, item, row_weight, groups)?, + Operator::all_groups_summary_build( + input(0)?, + family, + parameters, + item, + row_weight, + groups, + )?, filter, )?, Kind::KeyedEvaluation { state, k } => { @@ -209,10 +228,15 @@ impl TryFrom for Operator { state, groups, emit_empty_global, + parameters, } => { let merge = Operator::summary_merge(input(0)?, state, groups)?; if emit_empty_global { - merge.with_emit_empty_global()? + merge.with_emit_empty_global(parameters)? + } else if parameters.is_some() { + return Err(invalid( + "only a merge that outputs an empty global state takes parameters θ", + )); } else { merge } diff --git a/crates/executor/src/physical_planner/compiled.rs b/crates/executor/src/physical_planner/compiled.rs index 0744c4ba2..94e4c2fa5 100644 --- a/crates/executor/src/physical_planner/compiled.rs +++ b/crates/executor/src/physical_planner/compiled.rs @@ -24,8 +24,10 @@ pub(super) struct MaterializedViewSlot { pub(super) view: MaterializedView, /// The view node's schema. pub(super) schema: SchemaRef, - /// The data its state summarizes (`crate::sources::view_windows`). - pub(super) window: ScanWindow, + /// The rows a read selects, `(end_offset_ms, width_ms)` back from the + /// evaluation time: for a chain, the windows the `TimeRange`s above it + /// select; for any other view, the data its state summarizes. + pub(super) read_window_ms: (i64, i64), } /// A `Scan` leaf, computed once at compile time: what [`CompiledPhysicalDAG::instantiate`] @@ -37,8 +39,9 @@ pub(super) struct RawDataSlot { pub(super) schema: SchemaRef, /// The `Scan`'s predicates, compiled over `schema`. pub(super) predicates: Vec, - /// The rows its readers read (`crate::sources::scan_windows`). - pub(super) window: ScanWindow, + /// The rows its consumers read, `(end_offset_ms, width_ms)` back from + /// the evaluation time; `None` when one reads every row. + pub(super) read_window_ms: Option<(i64, i64)>, } /// Selected native operators and input slots. Rebinding never repeats lowering. @@ -183,9 +186,8 @@ impl CompiledPhysicalDAG { store.clone(), slot.view.clone(), slot.schema.clone(), - slot.window, - ) - .map_err(|error| at_slot(id, error))?; + slot.read_window_ms, + ); dag.add(id, vec![], scan)?; } Node::RawData(slot) => { @@ -194,7 +196,7 @@ impl CompiledPhysicalDAG { slot.source.clone(), slot.schema.clone(), slot.predicates.clone(), - slot.window, + slot.read_window_ms, ); dag.add(id, vec![], scan)?; } @@ -207,14 +209,6 @@ impl CompiledPhysicalDAG { Ok(dag) } } -/// `error` binding the slot `id`. -fn at_slot(id: NodeId, error: Error) -> Error { - match error { - Error::Invalid(message) => invalid(format!("physical input {id}: {message}")), - other => other, - } -} - /// Stands in for an input slot when validating or deriving properties. struct UnboundInput(SchemaRef); impl PhysicalOperator for UnboundInput { diff --git a/crates/executor/src/physical_planner/mod.rs b/crates/executor/src/physical_planner/mod.rs index 31a53f9fd..9943f6e43 100644 --- a/crates/executor/src/physical_planner/mod.rs +++ b/crates/executor/src/physical_planner/mod.rs @@ -3,7 +3,6 @@ //! A node without a native lowering fails compilation; the deployment runtime //! must execute it. use crate::operators::{RowWeight, SummaryEvaluation}; -use crate::sources::{scan_windows, time_range_window, view_windows, ScanWindow}; use crate::summary_kernels::exact::ExactEvaluation; use crate::{ operators::{Expression, Operator, Reduction, SortKey}, @@ -21,7 +20,9 @@ use planner_types::plan::physical_asap_dag::{ PhysicalASAPDAG, PhysicalASAPDAGNode, PhysicalASAPNodeId, PhysicalASAPOperatorPayload as Payload, }; -use planner_types::summary_type::{SketchStatistic, SummaryInputExpr, SummaryTypeCapability}; +use planner_types::summary_type::{ + SketchParams, SketchStatistic, SummaryInputExpr, SummaryTypeCapability, +}; // `promql_rows` reads `OperatorNode` through `super::*` only in its // test-support helpers. #[cfg_attr(not(any(test, feature = "test-support")), allow(unused_imports))] @@ -87,14 +88,6 @@ fn compile_internal( roots: &[NodeId], ) -> Result { preflight_depth(dag)?; - if dag.nodes.iter().any(|n| { - matches!( - n.payload, - Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) - ) - }) { - todo!("#1207 PR 6: run a chain, and a TimeRange or TimeShift over it by window index") - } // Helper IDs lie above the u32 range; a larger Planner ID could collide. if let Some(node) = dag.nodes.iter().find(|n| n.id as u64 > u64::from(u32::MAX)) { return Err(invalid(format!("Planner node ID {} exceeds u32", node.id))); @@ -157,16 +150,15 @@ fn compile_internal( } } } - // Each view's and each `Scan`'s window, the DAG indexed once for all: - // a materialized tumbling-window chain has a view per tumbling window. - let view_windows = view_windows(dag, views.iter().map(|view| view.node_id)); - let scan_windows = scan_windows( - dag, - dag.nodes - .iter() - .filter(|node| matches!(node.payload, Payload::NonASAP(NonASAPOp::Scan { .. }))) - .map(|node| node.id), - ); + // The consumers of each compiled node, for the windows its reads select. + let mut consumers = BTreeMap::>::new(); + for (&consumer, producers) in &dependencies { + if seen.contains(&consumer) && !views_by_node.contains_key(&consumer) { + for &producer in producers { + consumers.entry(producer).or_default().push(consumer); + } + } + } let compile_roots: BTreeSet = roots.iter().copied().collect(); let mut physical_dag = CompiledPhysicalDAG::new(roots.to_vec()); for id in ordered { @@ -175,20 +167,22 @@ fn compile_internal( let output = Arc::new(node.output_schema.clone()); crate::values::validate_schema(&output)?; if let Some(view) = views_by_node.get(&id) { - // A view read is exact for the window its state summarizes, so - // that window must be bounded. - let window = view_windows[&view.node_id]; - if window == ScanWindow::Unbounded { - return Err(invalid(format!( - "materialized view {id} summarizes an unbounded window" - ))); + // A chain's rows are read through the `TimeRange`s above it, + // which select its windows; any other view's state is read for + // the window of data it summarizes. Either is exact, so bounded. + let read_window_ms = match node.payload { + Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) => { + window_read_above(id, &compile_roots, &consumers, &nodes, &dependencies) + } + _ => window_below(id, &nodes, &dependencies), } + .ok_or_else(|| invalid(format!("materialized view {id} is read unbounded")))?; physical_dag.add_materialized_view( id, compiled::MaterializedViewSlot { view: (*view).clone(), schema: output, - window, + read_window_ms, }, )?; } else if let Payload::NonASAP(NonASAPOp::Scan { @@ -213,20 +207,17 @@ fn compile_internal( }) .collect::, _>>() .map_err(|error| invalid(format!("scan {id}: {error}")))?; - // The rows its readers read; a compile root reads them all. - let window = match output.time_index { - Some(_) if !compile_roots.contains(&id) => { - scan_windows[&(id as PhysicalASAPNodeId)] - } - _ => ScanWindow::Unbounded, - }; + // The rows its consumers read; a compile root reads them all. + let read_window_ms = output.time_index.and_then(|_| { + window_read_above(id, &compile_roots, &consumers, &nodes, &dependencies) + }); physical_dag.add_raw_data( id, compiled::RawDataSlot { source: source.clone(), schema: output, predicates, - window, + read_window_ms, }, )?; } else { @@ -445,6 +436,111 @@ 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. + if let Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { + summary_type, + input: update, + reduction, + filter, + tumbling_window_width_ms, + .. + }) = &node.payload + { + if node.output_state.timing + != planner_types::ir::properties::ExecutionTiming::IngestionTime + { + return Err(invalid(format!( + "node {id}: a tumbling-window chain is built in ingestion runs" + ))); + } + crate::capability::validate_ingestion_time_summary(filter.as_ref()) + .map_err(Error::Invalid)?; + let [input] = schemas.as_slice() else { + return Err(invalid("a tumbling-window chain reads one input")); + }; + let time = input + .time_index + .ok_or_else(|| invalid("a tumbling-window chain reads a time column"))?; + let width_ms = i64::try_from(*tumbling_window_width_ms) + .map_err(|_| invalid("tumbling-window width exceeds Int64"))?; + // A time series' stale markers are not samples. + let below = + node_below_shifts(first_input(id, &dependencies), &nodes, &dependencies); + let sample_value = match below.map(|below| &nodes[&below].payload) { + Some(Payload::NonASAP(NonASAPOp::Scan { + source: planner_types::ir::operator::Source::TimeSeries { .. }, + .. + })) => Some(named_column(input, &ColumnRef::SampleValue)?), + _ => None, + }; + let window = + Operator::time_range(input.clone(), time, 0, width_ms, sample_value, None)?; + let build = match reduction { + PlannerReduction::PerEntity => { + let SummaryInputExpr::Column(value) = &update.weight else { + return Err(invalid( + "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) + .collect(); + Operator::per_group_summary_build( + input.clone(), + summary_type.clone(), + node.parameters.clone(), + value, + Some(time), + groups, + )? + } + _ => summary_agg_build( + input, + summary_type, + node.parameters.as_ref(), + update, + reduction, + )?, + }; + let build = match filter { + Some(filter) => build.with_row_filter(expression(&filter.0, input)?)?, + None => build, + }; + let compact = build.schema(); + physical_dag.add(auxiliary, inputs, window)?; + physical_dag.add(helper_id(id, 1)?, vec![auxiliary], build)?; + physical_dag.add( + id, + vec![helper_id(id, 1)?], + Operator::scope_timestamp(compact, output)?, + )?; + continue; + } + // A `TimeShift` with an `@` anchor evaluates its input once, at + // the anchor's time minus its offset. + if let Payload::NonASAP(NonASAPOp::TimeShift { + shift: + planner_types::ir::operator::TimeShift { + offset_ms, + at: Some(at), + }, + .. + }) = &node.payload + { + let [input] = schemas.as_slice() else { + return Err(invalid("a time shift reads one input")); + }; + physical_dag.add( + id, + inputs, + Operator::evaluate_at(input.clone(), *at, *offset_ms)? + .with_output_schema(output)?, + )?; + continue; + } // A closed row must include either all source labels or the explicit // complete-label identity. Projected labels alone are insufficient. if let Payload::ASAP(ASAPOp::SummaryAgg { @@ -478,8 +574,12 @@ fn compile_internal( "per-entity summary requires complete source identity", )); } - crate::capability::validate_summary_kernel(family, update) - .map_err(Error::Invalid)?; + crate::capability::validate_summary_kernel( + family, + node.parameters.as_ref(), + update, + ) + .map_err(Error::Invalid)?; let value = match (frequency_item, &update.weight) { (Some(item), _) => item, (None, SummaryInputExpr::Column(value)) => value, @@ -500,6 +600,7 @@ fn compile_internal( let build = Operator::per_group_summary_build( input, family.clone(), + node.parameters.clone(), value, Some(coordinate), groups, @@ -617,8 +718,10 @@ fn compile_internal( }) = &node.payload { if keys.is_empty() && built_from_relation(&restored[&id]) { + // The merge carries the θ of the states it reads. + let parameters = node.parameters.clone(); let operator = compile_node(node, &schemas) - .and_then(Operator::with_emit_empty_global) + .and_then(|merge| merge.with_emit_empty_global(parameters)) .map_err(|error| invalid(format!("node {id}: {error}")))?; physical_dag.add(id, inputs, operator)?; continue; @@ -667,21 +770,21 @@ fn built_from_relation(state: &std::rc::Rc) -> bool { } /// The native operator of `id` when it is a `TimeRange` or a `TimeShift` -/// over a `Scan` (below any stacked `TimeShift`s), of a SQL table or a time -/// series; `None` for any other node. +/// without an `@` anchor over a `Scan` of a SQL table or a time series, or +/// over a tumbling-window chain (below any stacked `TimeShift`s); `None` +/// for any other node. /// /// A `TimeRange(w)` over shifts summing to `s` keeps the rows whose time /// column lies in `(T - s - w, T - s]` at evaluation time `T` -/// ([`time_range_window`]), the window a PromQL selector -/// and a SQL filter against `now()` read; an ingestion run builds the -/// window ending at its `window_end_ms` ([`Operator::time_range`]). Over a -/// time series a stale marker is not a row, and an instant window keeps -/// each series' (each label set's) latest row. A `TimeShift` only moves -/// the window its `TimeRange` reads (the range above applies its offset), -/// so it passes the rows through; a shift is meaningful only under a -/// range, the shape the frontends and Pass 2's tumbling windows build. An -/// `@` anchor has no lowering, so a range or shift over one gets `None` and -/// the node fails compilation; an instant window over a table is refused. +/// ([`time_range_window`]), the window a PromQL selector and a SQL filter +/// against `now()` read; an ingestion run keeps the window ending at its +/// `window_end_ms` ([`Operator::time_range`]). Over a time series a stale +/// marker is not a row, and an instant window keeps each series' (each +/// label set's) latest row. Over a chain of width `c`, it keeps the windows +/// whose `window_end` lies there, `w / c` of them: `c` must divide `w` and +/// `s`, and the window must be a range. A `TimeShift` only moves the window +/// its `TimeRange` reads (the range above applies its offset), so it passes +/// the rows through. An instant window over a table is refused. fn time_window_operator( id: NodeId, nodes: &BTreeMap, @@ -696,11 +799,19 @@ fn time_window_operator( let Some(below) = node_below_shifts(below, nodes, dependencies) else { return Ok(None); }; - let Payload::NonASAP(NonASAPOp::Scan { source, .. }) = &nodes[&below].payload else { - return Ok(None); + let chain_width_ms = match &nodes[&below].payload { + Payload::NonASAP(NonASAPOp::Scan { .. }) => None, + Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { + tumbling_window_width_ms, + .. + }) => Some( + i64::try_from(*tumbling_window_width_ms) + .map_err(|_| invalid("tumbling-window width exceeds Int64"))?, + ), + _ => return Ok(None), }; let [input] = input_schemas else { - return Err(invalid("a time range or shift over a Scan reads one input")); + return Err(invalid("a time range or shift reads one input")); }; let Payload::NonASAP(NonASAPOp::TimeRange { kind, .. }) = &nodes[&id].payload else { // A shift passes its rows through. @@ -712,24 +823,41 @@ fn time_window_operator( .collect(); return Operator::project(input.clone(), columns).map(Some); }; - let ScanWindow::Within { - end_offset_ms, - width_ms, - } = time_range_window(id as PhysicalASAPNodeId, |id| { - nodes.get(&(id as NodeId)).map(|node| &node.payload) - }) - else { + let Some((end_offset_ms, width_ms)) = time_range_window(id, nodes, dependencies) else { return Ok(None); }; let time = input .time_index - .ok_or_else(|| invalid("a time range needs its source's time column"))?; - let (sample_value, latest_per_series) = match (source, kind) { - (planner_types::ir::operator::Source::Table { .. }, TimeRangeKind::Range) => (None, None), - (planner_types::ir::operator::Source::Table { .. }, TimeRangeKind::Instant) => { - return Err(invalid("an instant window over a SQL table is unsupported")) + .ok_or_else(|| invalid("a time range needs its input's time column"))?; + let (sample_value, latest_per_series) = match (&nodes[&below].payload, kind) { + (_, TimeRangeKind::Instant) if chain_width_ms.is_some() => { + return Err(invalid("an instant window over a tumbling-window chain")) } - (planner_types::ir::operator::Source::TimeSeries { .. }, kind) => { + (Payload::ASAP(_), TimeRangeKind::Range) => { + let chain_width_ms = chain_width_ms.expect("a chain"); + if width_ms % chain_width_ms != 0 || end_offset_ms % chain_width_ms != 0 { + return Err(invalid(format!( + "a window ({end_offset_ms} ms back, {width_ms} ms wide) is not whole \ + tumbling windows of {chain_width_ms} ms" + ))); + } + (None, None) + } + ( + Payload::NonASAP(NonASAPOp::Scan { + source: planner_types::ir::operator::Source::Table { .. }, + .. + }), + TimeRangeKind::Range, + ) => (None, None), + ( + Payload::NonASAP(NonASAPOp::Scan { + source: planner_types::ir::operator::Source::Table { .. }, + .. + }), + TimeRangeKind::Instant, + ) => return Err(invalid("an instant window over a SQL table is unsupported")), + (_, kind) => { let value = named_column(input, &ColumnRef::SampleValue)?; // A series is its label set: every column but its time and value. let series = (*kind == TimeRangeKind::Instant).then(|| { @@ -751,6 +879,109 @@ fn time_window_operator( .map(Some) } +/// `(s, w)` of the `TimeRange(w)` `id` over `TimeShift`s summing to `s`: it +/// reads `(T - s - w, T - s]` at evaluation time `T`. `None` when `id` is not +/// a `TimeRange` or a shift below it has an `@` anchor. +fn time_range_window( + id: NodeId, + nodes: &BTreeMap, + dependencies: &BTreeMap>, +) -> Option<(i64, i64)> { + let Payload::NonASAP(NonASAPOp::TimeRange { range, .. }) = &nodes.get(&id)?.payload else { + return None; + }; + let mut end_offset_ms = 0i64; + let mut below = first_input(id, dependencies); + while let Some(Payload::NonASAP(NonASAPOp::TimeShift { shift, .. })) = below + .and_then(|below| nodes.get(&below)) + .map(|node| &node.payload) + { + if shift.at.is_some() { + return None; + } + end_offset_ms = end_offset_ms.saturating_add(shift.offset_ms); + below = below.and_then(|below| first_input(below, dependencies)); + } + Some((end_offset_ms, i64::try_from(range.as_millis()).ok()?)) +} + +/// The smallest window `(end_offset_ms, width_ms)` holding both. +fn window_union(a: (i64, i64), b: (i64, i64)) -> (i64, i64) { + let end_offset_ms = a.0.min(b.0); + let start_back_ms = a.0.saturating_add(a.1).max(b.0.saturating_add(b.1)); + (end_offset_ms, start_back_ms.saturating_sub(end_offset_ms)) +} + +/// The window `(end_offset_ms, width_ms)` back from the evaluation time that +/// the compiled consumers of `id` read through the `TimeShift`s (without an +/// `@` anchor) above it: a `TimeRange(w)`, or a chain of width `w` (its +/// window ending at the evaluation time), over shifts summing to `s` reads +/// `(s, w)`; several read the smallest window holding all of theirs. +/// `None` when `id` is a compile root, a root or another consumer reads it +/// outside a window, or nothing reads it. +fn window_read_above( + id: NodeId, + roots: &BTreeSet, + consumers: &BTreeMap>, + nodes: &BTreeMap, + dependencies: &BTreeMap>, +) -> Option<(i64, i64)> { + let mut window: Option<(i64, i64)> = None; + let mut pending = vec![(id, 0i64)]; + while let Some((node, end_offset_ms)) = pending.pop() { + if roots.contains(&node) { + return None; + } + for &consumer in consumers.get(&node).into_iter().flatten() { + let read = match &nodes[&consumer].payload { + Payload::NonASAP(NonASAPOp::TimeShift { shift, .. }) if shift.at.is_none() => { + pending.push((consumer, end_offset_ms.saturating_add(shift.offset_ms))); + continue; + } + Payload::NonASAP(NonASAPOp::TimeRange { .. }) => { + time_range_window(consumer, nodes, dependencies)? + } + Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { + tumbling_window_width_ms, + .. + }) => ( + end_offset_ms, + i64::try_from(*tumbling_window_width_ms).ok()?, + ), + _ => return None, + }; + window = Some(window.map_or(read, |window| window_union(window, read))); + } + } + window +} + +/// The window `(end_offset_ms, width_ms)` back from the evaluation time of +/// the data `id`'s state summarizes: the smallest window holding every +/// `TimeRange` below it. `None` when none is below it, or one is read +/// through an `@` anchor. +fn window_below( + id: NodeId, + nodes: &BTreeMap, + dependencies: &BTreeMap>, +) -> Option<(i64, i64)> { + let mut seen = BTreeSet::new(); + let mut pending = vec![id]; + let mut window: Option<(i64, i64)> = None; + while let Some(node) = pending.pop() { + if !seen.insert(node) { + continue; + } + if let Payload::NonASAP(NonASAPOp::TimeRange { .. }) = &nodes[&node].payload { + let read = time_range_window(node, nodes, dependencies)?; + window = Some(window.map_or(read, |window| window_union(window, read))); + } else { + pending.extend(dependencies.get(&node).into_iter().flatten().copied()); + } + } + window +} + fn first_input(id: NodeId, dependencies: &BTreeMap>) -> Option { dependencies.get(&id).and_then(|d| d.first()).copied() } @@ -795,7 +1026,7 @@ pub(crate) fn below_time_shifts<'a, N, C: 'a>( /// lookback)`: it ends `offset` ms before the evaluation time and spans /// `lookback` ms. It is the union of the range windows the evaluation /// reads, each moved back by the time shifts below its range: one range for -/// a single build, back-to-back tumbling windows for merged tumbling windows. `None` +/// a single build or for the windows of a chain it merges. `None` /// without a range; an error when the ranges do not form one window. fn counter_window( id: NodeId, @@ -1211,7 +1442,8 @@ fn bind_operation(node: &PhysicalASAPDAGNode, inputs: &[SchemaRef]) -> Result build.with_row_filter(expression(&filter.0, input)?), None => Ok(build), @@ -1278,6 +1510,7 @@ fn bind_operation(node: &PhysicalASAPDAGNode, inputs: &[SchemaRef]) -> Result, update: &planner_types::summary_type::SummaryUpdate, reduction: &PlannerReduction, ) -> Result { @@ -1285,7 +1518,8 @@ fn summary_agg_build( family, FieldDataType::Sketch(planner_types::summary_type::SketchAlgorithm::HydraCms) ) { - crate::capability::validate_summary_kernel(family, update).map_err(Error::Invalid)?; + crate::capability::validate_summary_kernel(family, parameters, update) + .map_err(Error::Invalid)?; let PlannerReduction::Reduce(keys) = reduction else { return Err(invalid("an all-groups summary requires explicit groups")); }; @@ -1297,6 +1531,7 @@ fn summary_agg_build( return Operator::all_groups_summary_build( input.clone(), family.clone(), + parameters.cloned(), named_column(input, item)?, row_weight(input, &update.weight)?, groups(input, keys)?, @@ -1308,7 +1543,8 @@ fn summary_agg_build( crate::capability::unit_frequency_column(update), unkeyed_frequency_family(family), ) { - crate::capability::validate_summary_kernel(family, update).map_err(Error::Invalid)?; + crate::capability::validate_summary_kernel(family, parameters, update) + .map_err(Error::Invalid)?; let PlannerReduction::Reduce(keys) = reduction else { return Err(invalid( "summary construction requires explicit grouping columns", @@ -1317,6 +1553,7 @@ fn summary_agg_build( return Operator::per_group_summary_build( input.clone(), family.clone(), + parameters.cloned(), named_column(input, item)?, input.time_index, groups(input, keys)?, @@ -1347,12 +1584,14 @@ fn summary_agg_build( return Operator::per_group_keyed_summary_build( input.clone(), family.clone(), + parameters.cloned(), items, row_weight(input, &update.weight)?, groups(input, keys)?, ); } - crate::capability::validate_summary_kernel(family, update).map_err(Error::Invalid)?; + crate::capability::validate_summary_kernel(family, parameters, update) + .map_err(Error::Invalid)?; let SummaryInputExpr::Column(column) = &update.weight else { return Err(invalid( "summary update expression must be projected to a column", @@ -1366,6 +1605,7 @@ fn summary_agg_build( Operator::per_group_summary_build( input.clone(), family.clone(), + parameters.cloned(), named_column(input, column)?, input.time_index, groups(input, keys)?, @@ -1588,12 +1828,42 @@ pub mod test_support { use planner_types::stages::{Lifetime, StorageMedium}; use std::collections::HashMap; + /// Set θ on every sketch build among `nodes` (`SummaryAgg`, + /// `SummaryTumblingWindowChain` of a sketch family): `parameters` when + /// given, else the first θ its algorithm lists (`parameter_list`). + /// Exact builds keep `None`. For tests of hand-built plans, whose nodes + /// carry no θ: pass `&mut dag.nodes`, or `[&mut node]`. + pub fn set_build_parameters<'a>( + nodes: impl IntoIterator, + parameters: Option<&SketchParams>, + ) { + for node in nodes { + if let Payload::ASAP( + ASAPOp::SummaryAgg { + summary_type: FieldDataType::Sketch(algorithm), + .. + } + | ASAPOp::SummaryTumblingWindowChain { + summary_type: FieldDataType::Sketch(algorithm), + .. + }, + ) = &node.payload + { + node.parameters = parameters.cloned().or_else(|| { + planner_types::summary_type::parameter_list(*algorithm) + .first() + .cloned() + }); + } + } + } + /// The materialized views at `nodes`, in that order: each node that is - /// not a view is marked materialized at ingestion time, and it and every node below it - /// run at ingestion time (their outputs and outgoing edges); then every - /// view's `retention_ms` is set ([`PhysicalASAPDAG::set_retention`]) - /// with no tumbling window or range-evaluation span. For tests that bind states at - /// those nodes, as a deployment binds its views. + /// not a view is marked materialized at ingestion time, and it and + /// every node below it run at ingestion time (their outputs and + /// outgoing edges); every view without a retention or coverage is + /// retained for ever with a coverage of its own. For tests that bind + /// states at those nodes, as a deployment binds its views. pub fn views_at( dag: &mut PhysicalASAPDAG, nodes: &[NodeId], @@ -1630,6 +1900,15 @@ pub mod test_support { if marked.contains(&id) { node.lifetime = Lifetime::AcrossEvaluations(StorageMedium::Memory); } + if matches!(node.lifetime, Lifetime::AcrossEvaluations(_)) { + node.retention_ms.get_or_insert(i64::MAX as u64); + node.view_coverage.get_or_insert_with(|| { + planner_types::plan::physical_asap_dag::ViewCoverage::new( + format!("node {id}"), + vec![], + ) + }); + } if at_ingestion.contains(&id) { node.output_state.timing = ExecutionTiming::IngestionTime; } @@ -1640,7 +1919,7 @@ pub mod test_support { } } let views = dag - .set_retention(&HashMap::new(), &HashMap::new()) + .materialized_views() .map_err(|error| invalid(error.to_string()))?; nodes .iter() diff --git a/crates/executor/src/plan/mod.rs b/crates/executor/src/plan/mod.rs index e54688a9e..1b87df0af 100644 --- a/crates/executor/src/plan/mod.rs +++ b/crates/executor/src/plan/mod.rs @@ -29,6 +29,12 @@ pub trait PhysicalOperator { fn validate_context(&self, _context: &RunContext) -> Result<(), Error> { Ok(()) } + /// The run context its inputs run in: this run's, unless the operator + /// evaluates its inputs at another time (a `TimeShift` with an `@` + /// anchor). + fn input_context(&self, context: &RunContext) -> Result { + Ok(context.clone()) + } fn input_schemas(&self) -> Vec; fn output_schema(&self) -> S; fn start<'a>( diff --git a/crates/executor/src/runtime/context.rs b/crates/executor/src/runtime/context.rs index 9714bda91..e20b7307c 100644 --- a/crates/executor/src/runtime/context.rs +++ b/crates/executor/src/runtime/context.rs @@ -79,6 +79,14 @@ impl RunContext { pub(crate) fn query_range(&self) -> Option<(i64, i64)> { self.query_range } + /// This run's context, evaluated at `evaluation_time_ms` instead: the + /// same limits, cancellation and query range. + pub(crate) fn at_evaluation_time(&self, evaluation_time_ms: i64) -> Self { + Self { + scope: Scope::Query { evaluation_time_ms }, + ..self.clone() + } + } pub fn cancel(&self) { self.control.cancelled.set(true); diff --git a/crates/executor/src/runtime/mod.rs b/crates/executor/src/runtime/mod.rs index 986b95484..89039cffc 100644 --- a/crates/executor/src/runtime/mod.rs +++ b/crates/executor/src/runtime/mod.rs @@ -59,19 +59,31 @@ pub(crate) fn execute<'r, V: 'r, S: Clone + PartialEq + Debug + 'r>( pending.extend(node.inputs.iter().copied()); } } + // A node runs once per time it is evaluated at: an `@` anchor above it + // evaluates it at another time than the run's. + type ProducerKey = (NodeId, Option); fn build<'r, V: 'r, S: 'r>( dag: &'r PhysicalDAG<'_, V, S>, id: NodeId, context: &RunContext, - states: &mut BTreeMap>>>, + states: &mut BTreeMap>>>, ) -> Result>>, Error> { - if let Some(state) = states.get(&id) { + let key = (id, context.evaluation_time_ms()); + if let Some(state) = states.get(&key) { return Ok(Rc::clone(state)); } let node = &dag.nodes[&id]; + let input_context = + node.operator + .input_context(context) + .map_err(|source| Error::AtNode { + node: id, + operation: node.operator.name().into(), + source: Box::new(source), + })?; let mut inputs = Vec::new(); for &child in &node.inputs { - inputs.push(Input::subscribe(build(dag, child, context, states)?)); + inputs.push(Input::subscribe(build(dag, child, &input_context, states)?)); } let stream = node .operator @@ -97,7 +109,7 @@ pub(crate) fn execute<'r, V: 'r, S: Clone + PartialEq + Debug + 'r>( finished: false, failure: None, })); - states.insert(id, Rc::clone(&state)); + states.insert(key, Rc::clone(&state)); Ok(state) } let mut states = BTreeMap::new(); diff --git a/crates/executor/src/sources/data_store.rs b/crates/executor/src/sources/data_store.rs index 62003366f..873915f95 100644 --- a/crates/executor/src/sources/data_store.rs +++ b/crates/executor/src/sources/data_store.rs @@ -28,9 +28,10 @@ pub trait DataStore { context: RunContext, ) -> Result, Error>; - /// The stored states of `view` whose intervals tile - /// `request.time_range_ms` exactly, in `request.schema` (group columns - /// and one state column). Exact: the executor never re-filters them, + /// The stored rows of `view` whose time ranges tile + /// `request.time_range_ms` exactly, in `request.schema` (a + /// `SummaryTumblingWindowChain`'s windows, each `(window_end, keys, + /// state)`; any other view's group columns and one state column). Exact: the executor never re-filters them, /// so the store never counts an observation twice, never returns /// another definition's state, and never treats missing state as /// empty; it returns an error instead. The states are the store's: a @@ -49,13 +50,17 @@ pub struct RawDataRequest<'a> { /// `(start, end]` in milliseconds, on the slot's time column. /// /// - Query run at evaluation time `T`: the smallest range covering - /// every window the `Scan`'s readers read - /// ([`PhysicalASAPDAG::scan_window`](planner_types::plan::physical_asap_dag::PhysicalASAPDAG::scan_window)); - /// `None` when a reader reads the `Scan` outside a window. + /// every window the `Scan`'s consumers read, from the `TimeRange`, + /// `TimeShift` and `SummaryTumblingWindowChain` nodes above it + /// (`TimeRange(w)` over shifts summing to `s` reads `(T - s - w, T - + /// s]`; a chain of width `w` its window ending at `T - s`); `None` + /// when a consumer reads the `Scan` outside a window. Under a + /// `TimeShift` with an `@` anchor, `T` is the anchor's time. /// - Ingestion run: the run's `(window_start_ms, window_end_ms]` when /// the `Scan` is read through windows, else `None`. Each /// `TimeRange(w)` then keeps `(window_end_ms - w, window_end_ms]`, - /// whatever the shifts below it. + /// whatever the shifts below it, and a chain builds its window ending + /// at `window_end_ms`. pub time_range_ms: Option<(i64, i64)>, /// The slot's columns, the `Scan`'s schema. The store returns rows with /// these columns (name, type and nullability), in this order. @@ -68,14 +73,15 @@ pub struct RawDataRequest<'a> { /// What a materialized view slot reads in one run. #[derive(Clone, Copy, Debug, PartialEq)] pub struct MaterializedViewRequest<'a> { - /// `(start, end]` in milliseconds: the interval of data the view's state - /// summarizes in this run - /// ([`PhysicalASAPDAG::view_window`](planner_types::plan::physical_asap_dag::PhysicalASAPDAG::view_window)). - /// In a query run at `T` with view window `(T - o - w, T - o]`, it is - /// that range; in an ingestion run, `(window_end_ms - w, window_end_ms]`. + /// `(start, end]` in milliseconds: the rows the read selects. For a + /// chain, the windows the `TimeRange` and `TimeShift` nodes above it + /// select (`TimeRange(W)` over shifts summing to `o` selects the windows + /// ending in `(T - o - W, T - o]`); for any other view, the interval of + /// data its state summarizes, from the `TimeRange`s below it. With that + /// window `(T - o - w, T - o]`, a query run at `T` reads that range; an + /// ingestion run `(window_end_ms - w, window_end_ms]`. pub time_range_ms: (i64, i64), - /// The slot's columns, the view node's schema: group columns and one - /// state column. The store returns rows with these columns, in this + /// The slot's columns, the view node's schema. The store returns rows with these columns, in this /// order. pub schema: &'a SchemaRef, } diff --git a/crates/executor/src/sources/memory.rs b/crates/executor/src/sources/memory.rs index 8f362b458..ae4196444 100644 --- a/crates/executor/src/sources/memory.rs +++ b/crates/executor/src/sources/memory.rs @@ -2,11 +2,15 @@ use super::*; use planner_types::plan::physical_asap_dag::PhysicalASAPNodeId; use std::collections::BTreeMap; -/// An immutable in-memory [`DataStore`], for tests and examples: raw tables -/// by `Source`, and each materialized view's states by its node id. A -/// deployment's store keys views by their coverage and selects their states -/// by time range; this one returns every state bound to the node, and every -/// row of a table (the executor re-applies the raw data request). +/// An in-memory [`DataStore`], for tests and examples: raw tables by +/// `Source`, and each materialized view's rows by its node id, time-indexed +/// and append-only. A run's output for a view is appended with the time +/// range its rows cover: a chain's window `(window_end - w, window_end]`, +/// or the range of data another view's state summarizes. Appending drops the +/// rows older than the view's `retention_ms` before the newest appended +/// end. A read returns the rows whose time ranges tile the requested range +/// exactly, and fails when one is missing. Raw tables return every row (the +/// executor re-applies the raw data request). /// /// The store owns the resident batches (a run is not charged for their /// states again); each cursor clones only the next batch, not the entire @@ -14,8 +18,12 @@ use std::collections::BTreeMap; #[derive(Default)] pub struct InMemoryDataStore { raw_data: Vec<(Source, SchemaRef, Vec)>, - materialized_views: BTreeMap>, + /// Each view's appended rows, oldest first. + materialized_views: BTreeMap>, } + +/// Rows appended for a view: the `(start, end]` they cover and their batches. +type AppendedRows = ((i64, i64), Vec); impl InMemoryDataStore { /// Bind `batches`, all in `schema`, as the rows of `source`. Bad input /// when `source` is already bound or a batch is in another schema. @@ -38,21 +46,35 @@ impl InMemoryDataStore { self.raw_data.push((source, schema, batches)); Ok(self) } - /// Bind `batches` as the states of the materialized view at node - /// `view`. Bad input when the node is already bound. - pub fn with_materialized_view( - mut self, - view: PhysicalASAPNodeId, + /// Append `batches` as the rows of `view` covering `time_range_ms` + /// (`(start, end]`), then drop its rows ending `view.retention_ms()` or + /// more before `end` (the newest rows are always kept). Bad input when + /// the range is empty or starts before the end of the rows appended + /// last (the store is append-only). + pub fn append_materialized_view( + &mut self, + view: &MaterializedView, + time_range_ms: (i64, i64), batches: Vec, - ) -> Result { - if self.materialized_views.contains_key(&view) { + ) -> Result<(), Error> { + let (start, end) = time_range_ms; + let rows = self.materialized_views.entry(view.node_id).or_default(); + if start >= end + || rows + .last() + .is_some_and(|((_, last_end), _)| start < *last_end) + { return Err(Error::Invalid(format!( - "materialized view {view} is bound twice" + "materialized view {}: appended rows ({start}, {end}] are empty or not after the stored ones", + view.node_id ))); } held_by_store(&batches); - self.materialized_views.insert(view, batches); - Ok(self) + rows.push((time_range_ms, batches)); + let oldest_kept_end = + end.saturating_sub(i64::try_from(view.retention_ms()).unwrap_or(i64::MAX)); + rows.retain(|((_, row_end), _)| *row_end > oldest_kept_end || *row_end == end); + Ok(()) } } @@ -103,14 +125,41 @@ impl DataStore for InMemoryDataStore { fn read_materialized_view( &self, view: &MaterializedView, - _: &MaterializedViewRequest, + request: &MaterializedViewRequest, context: RunContext, ) -> Result, Error> { - let batches = self + let (start, end) = request.time_range_ms; + let missing = || { + Error::Invalid(format!( + "materialized view {} has no rows tiling ({start}, {end}]", + view.node_id + )) + }; + let rows = self .materialized_views .get(&view.node_id) - .ok_or_else(|| Error::Invalid(format!("unbound materialized view {}", view.node_id)))?; - Ok(stream_batches(batches.iter().cloned().map(Ok), context)) + .ok_or_else(missing)?; + let selected: Vec<&AppendedRows> = rows + .iter() + .filter(|((row_start, row_end), _)| *row_start >= start && *row_end <= end) + .collect(); + // The selected ranges, oldest first, must tile `(start, end]`. + let mut covered_to = start; + for ((row_start, row_end), _) in &selected { + if *row_start != covered_to { + return Err(missing()); + } + covered_to = *row_end; + } + if covered_to != end { + return Err(missing()); + } + Ok(stream_batches( + selected + .into_iter() + .flat_map(|(_, batches)| batches.iter().cloned().map(Ok)), + context, + )) } } diff --git a/crates/executor/src/sources/mod.rs b/crates/executor/src/sources/mod.rs index 290fc40c3..6cdcdcf34 100644 --- a/crates/executor/src/sources/mod.rs +++ b/crates/executor/src/sources/mod.rs @@ -18,8 +18,6 @@ use std::sync::Arc; mod data_store; pub use data_store::{DataStore, MaterializedViewRequest, RawDataRequest}; -mod window; -pub(crate) use window::{scan_windows, time_range_window, view_windows, ScanWindow}; mod memory; pub use memory::InMemoryDataStore; @@ -45,16 +43,18 @@ enum ScanInput { predicates: Vec, /// `predicates` as the store receives them. predicate_hints: Vec>, - /// The rows its readers read. - window: ScanWindow, + /// The rows its consumers read, `(end_offset_ms, width_ms)` back + /// from the time the run evaluates it at + /// ([`window_bounds_ms`]); `None` when one reads every row. + read_window_ms: Option<(i64, i64)>, }, /// A materialized view slot: exact, never re-filtered; its states are /// the store's. MaterializedView { view: MaterializedView, - /// The data its state summarizes, back from the evaluation time; - /// always `Within`. - window: ScanWindow, + /// The rows a read selects, `(end_offset_ms, width_ms)` back from + /// the evaluation time ([`window_bounds_ms`]). + read_window_ms: (i64, i64), }, } @@ -72,7 +72,7 @@ impl Scan { source: Source, output: SchemaRef, predicates: Vec, - window: ScanWindow, + read_window_ms: Option<(i64, i64)>, ) -> Self { let predicate_hints = predicates .iter() @@ -85,29 +85,26 @@ impl Scan { source, predicates, predicate_hints, - window, + read_window_ms, }, } } - /// A materialized view slot reading `view`, whose state summarizes - /// `window`; bad input when the window is unbounded. + /// A materialized view slot reading the rows of `view` that + /// `read_window_ms` selects. pub(crate) fn materialized_view( store: Arc, view: MaterializedView, output: SchemaRef, - window: ScanWindow, - ) -> Result { - if window == ScanWindow::Unbounded { - return Err(Error::Invalid(format!( - "materialized view {} summarizes an unbounded window", - view.node_id - ))); - } - Ok(Self { + read_window_ms: (i64, i64), + ) -> Self { + Self { store, output, - input: ScanInput::MaterializedView { view, window }, - }) + input: ScanInput::MaterializedView { + view, + read_window_ms, + }, + } } /// `batch` in the slot's schema; bad input when its columns differ. fn to_output(&self, batch: Batch) -> Result { @@ -168,11 +165,18 @@ impl PhysicalOperator for Scan { source, predicates, predicate_hints, - window, - } => self.start_raw_data(source, predicates, predicate_hints, *window, context), - ScanInput::MaterializedView { view, window } => { - self.start_materialized_view(view, *window, context) - } + read_window_ms, + } => self.start_raw_data( + source, + predicates, + predicate_hints, + *read_window_ms, + context, + ), + ScanInput::MaterializedView { + view, + read_window_ms, + } => self.start_materialized_view(view, *read_window_ms, context), } } } @@ -180,23 +184,18 @@ impl Scan { fn start_materialized_view<'a>( &'a self, view: &'a MaterializedView, - window: ScanWindow, + read_window_ms: (i64, i64), context: RunContext, ) -> Result, Error> { // A query run reads the window back from its evaluation time; an - // ingestion run builds the windows ending at its end. + // ingestion run reads the window ending at its end, whatever the + // offset. + let (end_offset_ms, _) = read_window_ms; let at_ms = match context.scope { Scope::Query { evaluation_time_ms } => evaluation_time_ms, - Scope::Ingestion { window_end_ms, .. } => match window { - ScanWindow::Within { end_offset_ms, .. } => { - window_end_ms.saturating_add(end_offset_ms) - } - ScanWindow::Unbounded => window_end_ms, - }, + Scope::Ingestion { window_end_ms, .. } => window_end_ms.saturating_add(end_offset_ms), }; - let time_range_ms = window - .bounds_ms(at_ms) - .ok_or_else(|| Error::Invalid("unbounded materialized view window".into()))?; + let time_range_ms = window_bounds_ms(read_window_ms, at_ms); let opening = context.clone(); let stream = stream::once(async move { if opening.is_cancelled() { @@ -227,7 +226,7 @@ impl Scan { source: &'a Source, predicates: &'a [CompiledExpression], predicate_hints: &'a [ScalarExpr<()>], - window: ScanWindow, + read_window_ms: Option<(i64, i64)>, context: RunContext, ) -> Result, Error> { // `now()` in the predicates is this run's evaluation time. @@ -239,13 +238,15 @@ impl Scan { }) .collect(); let time_range_ms = match context.scope { - Scope::Query { evaluation_time_ms } => window.bounds_ms(evaluation_time_ms), + Scope::Query { evaluation_time_ms } => { + read_window_ms.map(|window| window_bounds_ms(window, evaluation_time_ms)) + } // A slot read through windows reads the rows of the window the // run builds. Scope::Ingestion { window_start_ms, window_end_ms, - } => (window != ScanWindow::Unbounded).then_some((window_start_ms, window_end_ms)), + } => read_window_ms.map(|_| (window_start_ms, window_end_ms)), }; let time_filter = match time_range_ms { Some(range) => Some(( @@ -325,6 +326,13 @@ impl Scan { } } +/// `(start, end]` of the window `(end_offset_ms, width_ms)` back from +/// `at_ms`: `(at_ms - end_offset_ms - width_ms, at_ms - end_offset_ms]`. +pub(crate) fn window_bounds_ms((end_offset_ms, width_ms): (i64, i64), at_ms: i64) -> (i64, i64) { + let end = at_ms.saturating_sub(end_offset_ms); + (end.saturating_sub(width_ms), end) +} + /// The rows of `batch` that `keep` keeps; `batch` itself, uncopied, when it /// keeps every row. fn keep_rows(batch: Batch, keep: &arrow::array::BooleanArray) -> Result { diff --git a/crates/executor/src/sources/window.rs b/crates/executor/src/sources/window.rs deleted file mode 100644 index 191701007..000000000 --- a/crates/executor/src/sources/window.rs +++ /dev/null @@ -1,195 +0,0 @@ -//! The rows a slot of a compiled DAG reads, back from the evaluation time: -//! the executor derives each `Scan`'s and each materialized view's window -//! from the `TimeRange` and `TimeShift` nodes above or below it, and pushes -//! it into the data-store read. - -use std::collections::{HashMap, HashSet}; - -use planner_types::ir::operator::asap::ASAPOp; -use planner_types::ir::operator::node::Operator; -use planner_types::ir::operator::non_asap::NonASAPOp; -use planner_types::plan::physical_asap_dag::{ - PhysicalASAPDAG, PhysicalASAPNodeId, PhysicalASAPOperatorPayload, -}; - -/// Which rows a slot reads, back from an evaluation time `T`. -#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)] -pub(crate) enum ScanWindow { - /// The rows with time in `(T - end_offset_ms - width_ms, T - - /// end_offset_ms]`. - Within { end_offset_ms: i64, width_ms: i64 }, - /// Rows at any time. - Unbounded, -} - -impl ScanWindow { - /// `(start, end]` at evaluation time `evaluation_time_ms`; `None` when - /// unbounded. - pub(crate) fn bounds_ms(self, evaluation_time_ms: i64) -> Option<(i64, i64)> { - match self { - Self::Within { - end_offset_ms, - width_ms, - } => { - let end = evaluation_time_ms.saturating_sub(end_offset_ms); - Some((end.saturating_sub(width_ms), end)) - } - Self::Unbounded => None, - } - } - - /// The smallest window holding both. - fn union(self, other: Self) -> Self { - let ( - Self::Within { - end_offset_ms: a_end, - width_ms: a_width, - }, - Self::Within { - end_offset_ms: b_end, - width_ms: b_width, - }, - ) = (self, other) - else { - return Self::Unbounded; - }; - let end_offset_ms = a_end.min(b_end); - let start_back_ms = a_end - .saturating_add(a_width) - .max(b_end.saturating_add(b_width)); - Self::Within { - end_offset_ms, - width_ms: start_back_ms.saturating_sub(end_offset_ms), - } - } -} - -/// The rows the `TimeRange` `time_range` keeps, back from the evaluation -/// time `T`: `(T - s - w, T - s]` for a `TimeRange(w)` over `TimeShift`s -/// whose offsets sum to `s`. `Unbounded` when one of those shifts has an `@` -/// anchor, or `time_range` is not a `TimeRange`. -pub(crate) fn time_range_window<'a>( - time_range: PhysicalASAPNodeId, - payload_of: impl Fn(PhysicalASAPNodeId) -> Option<&'a PhysicalASAPOperatorPayload>, -) -> ScanWindow { - let Some(Operator::NonASAP(NonASAPOp::TimeRange { range, child, .. })) = payload_of(time_range) - else { - return ScanWindow::Unbounded; - }; - let mut end_offset_ms = 0i64; - let mut below = payload_of(*child); - while let Some(Operator::NonASAP(NonASAPOp::TimeShift { shift, child })) = below { - if shift.at.is_some() { - return ScanWindow::Unbounded; - } - end_offset_ms = end_offset_ms.saturating_add(shift.offset_ms); - below = payload_of(*child); - } - ScanWindow::Within { - end_offset_ms, - width_ms: i64::try_from(range.as_millis()).unwrap_or(i64::MAX), - } -} - -fn payloads_by_id( - dag: &PhysicalASAPDAG, -) -> HashMap { - dag.nodes - .iter() - .map(|node| (node.id, &node.payload)) - .collect() -} - -/// Each of `scans`' window, by id: the union of the windows its consumers -/// read through `TimeShift`s without an `@` anchor, each a `TimeRange(w)` -/// (`(T - s - w, T - s]`). `Unbounded` when a root, any other consumer, or -/// a shift with an `@` anchor reads the `Scan`, or nothing reads it. -pub(crate) fn scan_windows( - dag: &PhysicalASAPDAG, - scans: impl IntoIterator, -) -> HashMap { - let payloads = payloads_by_id(dag); - let mut consumers: HashMap> = HashMap::new(); - for edge in &dag.edges { - consumers - .entry(edge.producer) - .or_default() - .push(edge.consumer); - } - let roots: HashSet = dag.roots.iter().copied().collect(); - let window_of = |scan: PhysicalASAPNodeId| { - let mut window: Option = None; - let mut pending = vec![(scan, 0i64)]; - while let Some((id, end_offset_ms)) = pending.pop() { - if roots.contains(&id) { - return ScanWindow::Unbounded; - } - for &consumer in consumers.get(&id).into_iter().flatten() { - let width_ms = match payloads.get(&consumer) { - Some(Operator::NonASAP(NonASAPOp::TimeShift { shift, .. })) - if shift.at.is_none() => - { - pending.push((consumer, end_offset_ms.saturating_add(shift.offset_ms))); - continue; - } - Some(Operator::NonASAP(NonASAPOp::TimeRange { range, .. })) => { - range.as_millis() - } - Some(Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { .. })) => todo!( - "#1207 PR 6: the scan window of a chain read through a TimeRange over it" - ), - _ => return ScanWindow::Unbounded, - }; - let read = ScanWindow::Within { - end_offset_ms, - width_ms: i64::try_from(width_ms).unwrap_or(i64::MAX), - }; - window = Some(window.map_or(read, |window| window.union(read))); - } - } - window.unwrap_or(ScanWindow::Unbounded) - }; - scans - .into_iter() - .map(|scan| (scan, window_of(scan))) - .collect() -} - -/// Each of `nodes`' window, by id: the union of the windows of every -/// `TimeRange` below it. `Unbounded` when no `TimeRange` is below it or one -/// of them is unbounded (a whole SQL table, an `@` anchor). -pub(crate) fn view_windows( - dag: &PhysicalASAPDAG, - nodes: impl IntoIterator, -) -> HashMap { - let payloads = payloads_by_id(dag); - let window_of = |node: PhysicalASAPNodeId| { - let mut seen = HashSet::new(); - let mut pending = vec![node]; - let mut window: Option = None; - while let Some(id) = pending.pop() { - if !seen.insert(id) { - continue; - } - let read = match payloads.get(&id) { - Some(Operator::NonASAP(NonASAPOp::TimeRange { .. })) => { - time_range_window(id, |id| payloads.get(&id).copied()) - } - Some(Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { .. })) => todo!( - "#1207 PR 6: the window a chain read through a TimeRange over it summarizes" - ), - Some(payload) => { - pending.extend(payload.children().into_iter().copied()); - continue; - } - None => continue, - }; - window = Some(window.map_or(read, |window| window.union(read))); - } - window.unwrap_or(ScanWindow::Unbounded) - }; - nodes - .into_iter() - .map(|node| (node, window_of(node))) - .collect() -} diff --git a/crates/executor/src/summary_kernels/factory/mod.rs b/crates/executor/src/summary_kernels/factory/mod.rs index 30c087e2e..222935169 100644 --- a/crates/executor/src/summary_kernels/factory/mod.rs +++ b/crates/executor/src/summary_kernels/factory/mod.rs @@ -469,7 +469,7 @@ pub fn create_planner_accumulator( parameters: Option<&SketchParams>, input: &planner_types::summary_type::SummaryUpdate, ) -> Result, String> { - crate::capability::validate_summary_kernel(family, input)?; + crate::capability::validate_summary_kernel(family, parameters, input)?; if matches!(family, SummaryFamilyType::Sketch(SketchAlgorithm::HydraCms)) { return Err( "HydraCms has one state for all groups; build it with `create_hydra_cms`".into(), @@ -558,11 +558,16 @@ pub fn create_planner_accumulator( Ok(updater) } -/// Construct the one state a `SharedMultiSubpopulation { kind: HydraCms }` -/// family shares across its groups. The update contract is checked with the -/// plan by `validate_summary_kernel`. -pub fn create_hydra_cms(family: &SummaryFamilyType) -> Result { - let (shared_rows, shared_columns, width, depth) = crate::capability::hydra_cms_shape(family)?; +/// Construct the one state a HydraCms family shares across its groups, +/// sized by its θ `parameters` (`SketchParams::Hydra(HydraParams::HydraCms +/// { .. })`). The update contract is checked with the plan by +/// `validate_summary_kernel`. +pub fn create_hydra_cms( + family: &SummaryFamilyType, + parameters: Option<&SketchParams>, +) -> Result { + let (shared_rows, shared_columns, width, depth) = + crate::capability::hydra_cms_shape(family, parameters)?; HydraCms::new(shared_rows, shared_columns, width, depth).map_err(|e| e.to_string()) } diff --git a/crates/executor/src/summary_kernels/factory/tests.rs b/crates/executor/src/summary_kernels/factory/tests.rs index cde466f27..1bf70defd 100644 --- a/crates/executor/src/summary_kernels/factory/tests.rs +++ b/crates/executor/src/summary_kernels/factory/tests.rs @@ -1,5 +1,54 @@ use super::*; +// A sketch kernel needs θ of its own algorithm; an exact one takes none. +#[test] +fn sketch_kernels_need_their_own_parameters() { + use planner_types::summary_type::{ExactKind, ExactParams, SummaryUpdate}; + let update = SummaryUpdate::column(planner_types::ir::scalar::ColumnRef::SampleValue); + let kll = SummaryFamilyType::Sketch(SketchAlgorithm::Kll); + let k200 = SketchParams::Kll { k: 200 }; + assert!(create_planner_accumulator(&kll, Some(&k200), &update).is_ok()); + assert!(create_planner_accumulator(&kll, None, &update).is_err()); + let dd = SketchParams::DDSketch { alpha: 0.01 }; + assert!(create_planner_accumulator(&kll, Some(&dd), &update).is_err()); + let sum = SummaryFamilyType::ExactAggregate(ExactKind::Sum, ExactParams::Sum); + assert!(create_planner_accumulator(&sum, None, &update).is_ok()); + assert!(create_planner_accumulator(&sum, Some(&k200), &update).is_err()); +} + +// HydraCms builds one shared grid shaped by its Hydra θ, never a +// per-group updater. +#[test] +fn hydra_cms_builds_one_shared_grid() { + use planner_types::summary_type::{HydraParams, SummaryInputExpr, SummaryUpdate}; + let family = SummaryFamilyType::Sketch(SketchAlgorithm::HydraCms); + let hydra = SketchParams::Hydra(HydraParams::HydraCms { + width: 128, + depth: 3, + shared_rows: 5, + shared_columns: 32, + }); + assert_eq!( + create_hydra_cms(&family, Some(&hydra)).unwrap().shape(), + (5, 32, 128, 3) + ); + // θ of the per-group Count-Min, not the Hydra grid's. + let cms = SketchParams::Cms { + width: 128, + depth: 3, + }; + assert!(create_hydra_cms(&family, Some(&cms)).is_err()); + assert!(create_hydra_cms(&family, None).is_err()); + let update = SummaryUpdate { + item: Some(SummaryInputExpr::Column( + planner_types::ir::scalar::ColumnRef::Named("host".into()), + )), + weight: SummaryInputExpr::Constant(1.0), + }; + assert!(crate::capability::validate_summary_kernel(&family, Some(&hydra), &update).is_ok()); + assert!(create_planner_accumulator(&family, Some(&hydra), &update).is_err()); +} + /// A DDSketch updater reports sketchlib's count of the bins its stores /// allocate: none before a sample, one 128-bin chunk of the positive /// store after a few positive samples. diff --git a/crates/executor/src/summary_kernels/state_column_updater.rs b/crates/executor/src/summary_kernels/state_column_updater.rs index 3034fafe1..7768c5b21 100644 --- a/crates/executor/src/summary_kernels/state_column_updater.rs +++ b/crates/executor/src/summary_kernels/state_column_updater.rs @@ -2,26 +2,30 @@ //! group: the code the `SummaryAgg` build runs row by row, exposed so a //! runtime that ingests sample by sample builds the same states. use super::exact::ScalarState; -use super::factory::{create_hydra_cms, AccumulatorUpdater}; +use super::factory::{create_hydra_cms, create_planner_accumulator, AccumulatorUpdater}; use super::hydra_cms::{hydra_group_name, hydra_item, hydra_weight, HydraCms, HydraCmsGroup}; use super::weighted_frequency::WeightedFrequency; use crate::values::{StateColumn, Value}; use crate::{AggregateCore, Error}; +use planner_types::ir::scalar::ColumnRef; use planner_types::ir::schema::FieldDataType; -use planner_types::summary_type::ExactKind; +use planner_types::summary_type::{ExactKind, SketchParams, SummaryUpdate}; use std::sync::Arc; /// The open summary states of one family, one per group, updated one sample /// at a time; sealed into an immutable [`StateColumn`]. /// -/// Built from the plan's family and parameters with [`Self::new`]. Groups are +/// Built from the plan's family and θ with [`Self::new`]. Groups are /// numbered `0..group_count()` in the order [`Self::add_group`] (or /// [`Self::add_group_with_key`]) adds them; the caller keeps its own map from /// a group's labels to its number. pub struct StateColumnUpdater { - /// The family and parameters of every state. + /// The family of every state. family: FieldDataType, + /// θ of every state: the `parameters` of the exported build node + /// (`PhysicalASAPDAGNode::parameters`); `None` for an exact family. + parameters: Option, /// The states, by group. states: GroupStates, /// For a counter (rate, increase), each group's latest sample time: @@ -48,7 +52,6 @@ enum GroupStates { Sketches(Vec>), /// A keyed frequency sketch (Count-Min or Count Sketch with a heap) per /// group. - #[allow(dead_code)] // Used again once θ comes from the exported node. Keyed { /// An empty state of the family's shape, cloned for each new group /// (boxed: much larger than the other kinds' fields). @@ -71,33 +74,49 @@ fn operator_error(message: impl ToString) -> Error { } impl StateColumnUpdater { - /// An updater with no group for `family`, the family and parameters of a - /// `SummaryAgg` the plan builds one state per group of: an exact - /// accumulator, a value sketch (KLL, DDSketch, HLL, KMV, UnivMon), a - /// keyed frequency sketch with a heap, or a HydraCms grid shared by all - /// groups. Bad input for any other summary type. - pub fn new(family: &FieldDataType) -> Result { + /// An updater with no group for `family` and θ `parameters`, the family + /// and the exported node's `parameters` of a `SummaryAgg` the plan + /// builds one state per group of: an exact accumulator (no θ), a value + /// sketch (KLL, DDSketch, HLL, KMV, UnivMon), a keyed frequency sketch + /// with a heap, or a HydraCms grid shared by all groups. Bad input for + /// any other summary type, or θ its kernel does not build. + pub fn new(family: &FieldDataType, parameters: Option<&SketchParams>) -> Result { crate::values::validate_family(family)?; let states = match family { FieldDataType::Sketch(planner_types::summary_type::SketchAlgorithm::HydraCms) => { GroupStates::Hydra { - grid: Box::new(create_hydra_cms(family).map_err(Error::Invalid)?), + grid: Box::new(create_hydra_cms(family, parameters).map_err(Error::Invalid)?), names: Vec::new(), } } + FieldDataType::ExactAggregate(..) if parameters.is_some() => { + return Err(Error::Invalid( + "an exact summary has no parameters θ".into(), + )) + } FieldDataType::ExactAggregate(..) => GroupStates::Exact { empty: ScalarState::empty(family).map_err(Error::Invalid)?, states: Vec::new(), }, FieldDataType::Sketch( - planner_types::summary_type::SketchAlgorithm::CmsWithHeap - | planner_types::summary_type::SketchAlgorithm::CountSketchWithHeap, + algorithm @ (planner_types::summary_type::SketchAlgorithm::CmsWithHeap + | planner_types::summary_type::SketchAlgorithm::CountSketchWithHeap), ) => { - todo!("#1207 PR 6: the keyed frequency state of θ, which the exported node carries") + let Some(parameters) = parameters else { + return Err(Error::Invalid(format!( + "a {algorithm:?} summary needs its parameters θ" + ))); + }; + let (algorithm, width, depth, capacity) = + WeightedFrequency::configuration(algorithm, parameters)?; + GroupStates::Keyed { + empty: Box::new(WeightedFrequency::new(algorithm, width, depth, capacity)?), + states: Vec::new(), + } } FieldDataType::Sketch(..) => { // Fails for a family with no per-group value kernel. - Self::sketch_updater(family)?; + Self::sketch_updater(family, parameters)?; GroupStates::Sketches(Vec::new()) } _ => return Err(Error::Invalid("unsupported summary family".into())), @@ -113,6 +132,7 @@ impl StateColumnUpdater { }; Ok(Self { family: family.clone(), + parameters: parameters.cloned(), states, latest_sample_ms: counter.then(Vec::new), memory_bytes, @@ -120,9 +140,16 @@ impl StateColumnUpdater { }) } - fn sketch_updater(family: &FieldDataType) -> Result, Error> { - let _ = family; - todo!("#1207 PR 6: build it with `create_planner_accumulator` and θ from the exported node's `parameters`") + fn sketch_updater( + family: &FieldDataType, + parameters: Option<&SketchParams>, + ) -> Result, Error> { + create_planner_accumulator( + family, + parameters, + &SummaryUpdate::column(ColumnRef::SampleValue), + ) + .map_err(Error::Invalid) } /// The number of groups. @@ -220,7 +247,7 @@ impl StateColumnUpdater { (states.capacity() - before) * std::mem::size_of::() } GroupStates::Sketches(states) => { - let state = Self::sketch_updater(&self.family)?; + let state = Self::sketch_updater(&self.family, self.parameters.as_ref())?; let bytes = state.memory_usage_bytes(); states.push(state); bytes @@ -349,12 +376,12 @@ impl StateColumnUpdater { /// shards must be disjoint in time: one shard's last sample before the /// other's first. Overlapping shards would count the increase of their /// overlap twice, so they are refused as bad input, as are different - /// families. An operator error when a kernel refuses the merge. On an - /// error this updater may hold some groups already merged. + /// families or θ. An operator error when a kernel refuses the merge. On + /// an error this updater may hold some groups already merged. pub fn merge(&mut self, other: Self) -> Result<(), Error> { - if self.family != other.family { + if self.family != other.family || self.parameters != other.parameters { return Err(Error::Invalid( - "cannot merge different summary families".into(), + "cannot merge different summary families or parameters".into(), )); } if let ( @@ -483,7 +510,7 @@ mod tests { fn exact_memory_is_the_buffer_capacity() { let family = FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum); for groups in [1, 5, 17] { - let mut updater = StateColumnUpdater::new(&family).unwrap(); + let mut updater = StateColumnUpdater::new(&family, None).unwrap(); updater.ensure_groups(groups).unwrap(); let charged = updater.memory_bytes(); let GroupStates::Exact { states, .. } = &updater.states else { diff --git a/crates/executor/src/supported_summaries.rs b/crates/executor/src/supported_summaries.rs index c04acc01d..185edfb08 100644 --- a/crates/executor/src/supported_summaries.rs +++ b/crates/executor/src/supported_summaries.rs @@ -85,16 +85,17 @@ mod tests { /// Whether the factory builds the per-instance `family` from rows, with /// a value column or keyed items. fn factory_builds(family: &FieldDataType) -> bool { - let FieldDataType::Sketch(_) = family else { + let FieldDataType::Sketch(algorithm) = family else { return false; }; + let theta = planner_types::summary_type::parameter_list(*algorithm).first(); let value = SummaryUpdate::column(ColumnRef::SampleValue); let keyed = SummaryUpdate { item: Some(SummaryInputExpr::Column(ColumnRef::Named("item".into()))), ..value.clone() }; - create_planner_accumulator(family, None, &value).is_ok() - || create_planner_accumulator(family, None, &keyed).is_ok() + create_planner_accumulator(family, theta, &value).is_ok() + || create_planner_accumulator(family, theta, &keyed).is_ok() } /// Whether the executor answers `readout` from `family`'s state: top-k @@ -120,16 +121,17 @@ mod tests { /// Whether the kernel of a per-instance `family` state built from one /// update reads `readout`. fn kernel_answers(family: &FieldDataType, readout: StatisticKind) -> bool { - let FieldDataType::Sketch(_) = family else { + let FieldDataType::Sketch(algorithm) = family else { return false; }; + let theta = planner_types::summary_type::parameter_list(*algorithm).first(); let value = SummaryUpdate::column(ColumnRef::SampleValue); let keyed = SummaryUpdate { item: Some(SummaryInputExpr::Column(ColumnRef::Named("item".into()))), ..value.clone() }; - let Ok(mut updater) = create_planner_accumulator(family, None, &value) - .or_else(|_| create_planner_accumulator(family, None, &keyed)) + let Ok(mut updater) = create_planner_accumulator(family, theta, &value) + .or_else(|_| create_planner_accumulator(family, theta, &keyed)) else { return false; }; diff --git a/crates/executor/src/values/batch.rs b/crates/executor/src/values/batch.rs index 25d221e61..c125625e7 100644 --- a/crates/executor/src/values/batch.rs +++ b/crates/executor/src/values/batch.rs @@ -852,7 +852,7 @@ mod tests { use crate::StateColumnUpdater; use planner_types::summary_type::{ExactKind, ExactParams}; let family = FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum); - let mut updater = StateColumnUpdater::new(&family).unwrap(); + let mut updater = StateColumnUpdater::new(&family, None).unwrap(); updater.ensure_groups(2).unwrap(); updater.update(0, 1.5, 0).unwrap(); updater.update(1, 4.0, 0).unwrap(); @@ -913,10 +913,10 @@ mod tests { states: states.clone(), }; let other_family = Arc::new( - StateColumnUpdater::new(&FieldDataType::ExactAggregate( - ExactKind::Count, - ExactParams::Count, - )) + StateColumnUpdater::new( + &FieldDataType::ExactAggregate(ExactKind::Count, ExactParams::Count), + None, + ) .unwrap() .seal(), ); @@ -994,7 +994,7 @@ mod tests { use crate::StateColumnUpdater; use planner_types::summary_type::{ExactKind, ExactParams}; let family = FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum); - let mut updater = StateColumnUpdater::new(&family).unwrap(); + let mut updater = StateColumnUpdater::new(&family, None).unwrap(); updater.ensure_groups(10).unwrap(); for group in 0..10 { updater.update(group, f64::from(group), 0).unwrap(); @@ -1066,7 +1066,7 @@ mod tests { // States [10, 20, 30] referenced as rows [2, 0, 2]; states [1, 2] // referenced as rows [1]. let batch = |sums: &[f64], rows: Vec| { - let mut updater = StateColumnUpdater::new(&family).unwrap(); + let mut updater = StateColumnUpdater::new(&family, None).unwrap(); updater.ensure_groups(sums.len()).unwrap(); for (group, &sum) in sums.iter().enumerate() { updater.update(group as u32, sum, 0).unwrap(); @@ -1121,12 +1121,18 @@ mod tests { fn state_batch(count: u32, exact: bool) -> Batch { use crate::StateColumnUpdater; use planner_types::summary_type::{ExactKind, ExactParams, SketchAlgorithm}; - let family = if exact { - FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum) + let (family, parameters) = if exact { + ( + FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum), + None, + ) } else { - FieldDataType::Sketch(SketchAlgorithm::Kll) + ( + FieldDataType::Sketch(SketchAlgorithm::Kll), + Some(planner_types::summary_type::SketchParams::Kll { k: 200 }), + ) }; - let mut updater = StateColumnUpdater::new(&family).unwrap(); + let mut updater = StateColumnUpdater::new(&family, parameters.as_ref()).unwrap(); updater.ensure_groups(count as usize).unwrap(); for group in 0..count { updater.update(group, f64::from(group), 0).unwrap(); diff --git a/crates/executor/src/values/mod.rs b/crates/executor/src/values/mod.rs index 6215fd15f..7678541e0 100644 --- a/crates/executor/src/values/mod.rs +++ b/crates/executor/src/values/mod.rs @@ -206,35 +206,50 @@ impl Value { pub(crate) use crate::capability::validate_native_family as validate_family; +/// Whether `state` is a state of `family`: the kernel's state type of the +/// family's algorithm. θ is not part of a family, so a state's shape is its +/// own; a merge refuses states of different shapes. pub(super) fn validate_state( family: &SummaryFamilyType, state: &dyn AggregateCore, ) -> Result<(), Error> { - use crate::summary_kernels::exact::ExactAccumulator; - + use crate::summary_kernels::weighted_frequency::{FrequencyAlgorithm, WeightedFrequency}; + use crate::summary_kernels::{ + count_min_sketch::CountMinSketchAccumulator, datasketches_kll::DatasketchesKLLAccumulator, + dd_sketch::DDSketchAccumulator, exact::ExactAccumulator, hll_sketch::HllSketchAccumulator, + kmv::KmvAccumulator, univmon::UnivMonAccumulator, HydraCmsGroup, + }; + use planner_types::summary_type::SketchAlgorithm as A; validate_family(family)?; + let state = state.as_any(); + let frequency = |algorithm| { + state + .downcast_ref::() + .is_some_and(|s| s.algorithm() == algorithm && s.shape().2 > 0) + }; let valid = match family { - SummaryFamilyType::Sketch(planner_types::summary_type::SketchAlgorithm::HydraCms) => { - let shape = crate::capability::hydra_cms_shape(family).map_err(Error::Invalid)?; - state - .as_any() - .downcast_ref::() - .is_some_and(|s| s.grid.shape() == shape) - } SummaryFamilyType::ExactAggregate(..) => state - .as_any() .downcast_ref::() .is_some_and(|s| s.family() == family && !s.is_keyed()), - SummaryFamilyType::Sketch(..) => { - todo!("#1207 PR 6: compare the state with θ, which the exported node carries") - } + SummaryFamilyType::Sketch(algorithm) => match algorithm { + A::HydraCms => state.is::(), + A::CmsWithHeap => frequency(FrequencyAlgorithm::Cms), + A::CountSketchWithHeap => frequency(FrequencyAlgorithm::CountSketch), + A::Kll => state.is::(), + A::DDSketch => state.is::(), + A::Hll => state.is::(), + A::Kmv => state.is::(), + A::UnivMon => state.is::(), + A::Cms => state.is::(), + _ => false, + }, _ => false, }; if valid { Ok(()) } else { Err(Error::Invalid( - "state payload differs from declared family, parameters or population layout".into(), + "state payload differs from declared family".into(), )) } } @@ -283,18 +298,21 @@ mod weighted_state_tests { use crate::summary_kernels::weighted_frequency::{FrequencyAlgorithm, WeightedFrequency}; use planner_types::summary_type::SketchAlgorithm; - // A state cannot acquire a different family or shape merely by relabeling its batch. + // A state cannot acquire a different family merely by relabeling its + // batch; states of different shapes (θ) do not merge. #[test] - fn weighted_state_family_and_shape_must_match() { + fn weighted_state_family_must_match_and_shapes_must_merge() { let cms = SummaryFamilyType::Sketch(SketchAlgorithm::CmsWithHeap); let cs = SummaryFamilyType::Sketch(SketchAlgorithm::CountSketchWithHeap); let state = WeightedFrequency::new(FrequencyAlgorithm::CountSketch, 32, 5, 8).unwrap(); assert!(validate_state(&cs, &state).is_ok()); assert!(validate_state(&cms, &state).is_err()); + // A Count Sketch without a heap is not a heap family's state. + let no_heap = WeightedFrequency::new(FrequencyAlgorithm::CountSketch, 32, 5, 0).unwrap(); + assert!(validate_state(&cs, &no_heap).is_err()); let wrong_shape = WeightedFrequency::new(FrequencyAlgorithm::CountSketch, 64, 5, 8).unwrap(); - assert!(validate_state(&cs, &wrong_shape).is_err()); - let even_depth = SummaryFamilyType::Sketch(SketchAlgorithm::CountSketchWithHeap); - assert!(validate_family(&even_depth).is_err()); + assert!(validate_state(&cs, &wrong_shape).is_ok()); + assert!(state.merge(&wrong_shape).is_err()); } } diff --git a/crates/executor/tests/blocking_resources.rs b/crates/executor/tests/blocking_resources.rs index e040021f9..ac068af60 100644 --- a/crates/executor/tests/blocking_resources.rs +++ b/crates/executor/tests/blocking_resources.rs @@ -185,7 +185,7 @@ fn cooperative_sort_preserves_ties_across_chunks() { // The integrated weighted-summary path obeys the same cooperative cancellation contract. #[test] fn weighted_summary_build_yields_within_a_batch() { - use planner_types::summary_type::SketchAlgorithm; + use planner_types::summary_type::{SketchAlgorithm, SketchParams}; let input = Arc::new(planner_types::ir::schema::Schema { unique_keys: vec![], @@ -223,6 +223,11 @@ fn weighted_summary_build_yields_within_a_batch() { let operator = Operator::per_group_keyed_summary_build( input, family, + Some(SketchParams::CmsWithHeap { + width: 64, + depth: 3, + heap_size: 8, + }), vec![0], asap_executor::operators::RowWeight::Column(1), vec![], diff --git a/crates/executor/tests/current_series_heap.rs b/crates/executor/tests/current_series_heap.rs index 01893a272..58988fe00 100644 --- a/crates/executor/tests/current_series_heap.rs +++ b/crates/executor/tests/current_series_heap.rs @@ -160,7 +160,7 @@ fn spatial_heap_ranks_latest_values_in_independent_runs() { SketchAlgorithm::CmsWithHeap, SketchAlgorithm::CountSketchWithHeap, ] { - let _params = match algorithm { + let params = match algorithm { SketchAlgorithm::CmsWithHeap => SketchParams::CmsWithHeap { width: 2048, depth: 5, @@ -176,6 +176,7 @@ fn spatial_heap_ranks_latest_values_in_independent_runs() { let build = Operator::per_group_keyed_summary_build( schema(), family, + Some(params), vec![3], asap_executor::operators::RowWeight::Column(1), vec![2], diff --git a/crates/executor/tests/deployment.rs b/crates/executor/tests/deployment.rs index f158126be..5dc88278b 100644 --- a/crates/executor/tests/deployment.rs +++ b/crates/executor/tests/deployment.rs @@ -4,16 +4,26 @@ use asap_executor::planner::ir::schema::FieldDataType; use asap_executor::planner::summary_type::{SketchAlgorithm, SketchParams, SummaryUpdate}; use asap_executor::{factory::create_planner_accumulator, AggregateCore}; -fn family(_k: u32) -> FieldDataType { - FieldDataType::Sketch(SketchAlgorithm::Kll) +/// A sketch family and its θ, as a build node carries them. +type Family = (FieldDataType, SketchParams); + +/// The kernel of `family` for `update`. +fn accumulator( + family: &Family, + update: &SummaryUpdate, +) -> Result, String> { + create_planner_accumulator(&family.0, Some(&family.1), update) } -fn build(values: &[f64]) -> Box { - let mut operator = create_planner_accumulator( - &family(512), - None, - &SummaryUpdate::column(ColumnRef::SampleValue), + +fn family(k: u32) -> Family { + ( + FieldDataType::Sketch(SketchAlgorithm::Kll), + SketchParams::Kll { k }, ) - .unwrap(); +} +fn build(values: &[f64]) -> Box { + let mut operator = + accumulator(&family(512), &SummaryUpdate::column(ColumnRef::SampleValue)).unwrap(); for (at, value) in values.iter().enumerate() { operator.validate_single_input(*value).unwrap(); operator.update_single(*value, at as i64).unwrap(); @@ -44,11 +54,7 @@ fn raw_partial_and_fully_precomputed_use_the_same_kernels() { // A compiler must reject invalid physical parameters before starting execution. #[test] fn invalid_kll_parameters_are_rejected_at_binding() { - let result = create_planner_accumulator( - &family(0), - None, - &SummaryUpdate::column(ColumnRef::SampleValue), - ); + let result = accumulator(&family(0), &SummaryUpdate::column(ColumnRef::SampleValue)); assert!(result.is_err()); } @@ -57,10 +63,17 @@ fn invalid_kll_parameters_are_rejected_at_binding() { #[test] fn native_count_sketch_dimensions_are_not_packed_wire_dimensions() { use asap_executor::planner::summary_type::SummaryInputExpr; - let family = FieldDataType::Sketch(SketchAlgorithm::CountSketchWithHeap); + let family = ( + FieldDataType::Sketch(SketchAlgorithm::CountSketchWithHeap), + SketchParams::CountSketchWithHeap { + width: 1200, + depth: 55, + heap_size: 3, + }, + ); let mut update = SummaryUpdate::column(ColumnRef::SampleValue); update.item = Some(SummaryInputExpr::Column(ColumnRef::Named("host".into()))); - let mut operator = create_planner_accumulator(&family, None, &update).unwrap(); + let mut operator = accumulator(&family, &update).unwrap(); operator .update_keyed(&["a".to_string()], 7.0, 1000) .unwrap(); @@ -77,8 +90,11 @@ fn native_count_sketch_dimensions_are_not_packed_wire_dimensions() { ); } -fn plain_count_sketch(_width: u32, _depth: u32) -> FieldDataType { - FieldDataType::Sketch(SketchAlgorithm::CountSketch) +fn plain_count_sketch(width: u32, depth: u32) -> Family { + ( + FieldDataType::Sketch(SketchAlgorithm::CountSketch), + SketchParams::CountSketch { width, depth }, + ) } fn host(i: usize) -> [String; 1] { @@ -87,7 +103,7 @@ fn host(i: usize) -> [String; 1] { /// Build a plain CountSketch over unit events, one per key index. fn build_plain_count_sketch( - family: &FieldDataType, + family: &Family, events: impl IntoIterator, ) -> Box { build_weighted_frequency(family, events.into_iter().map(|i| (i, 1.0))) @@ -95,13 +111,13 @@ fn build_plain_count_sketch( /// Build a keyed frequency summary over `(key index, weight)` updates. fn build_weighted_frequency( - family: &FieldDataType, + family: &Family, updates: impl IntoIterator, ) -> Box { use asap_executor::planner::summary_type::SummaryInputExpr; let mut update = SummaryUpdate::column(ColumnRef::SampleValue); update.item = Some(SummaryInputExpr::Column(ColumnRef::Named("host".into()))); - let mut operator = create_planner_accumulator(family, None, &update).unwrap(); + let mut operator = accumulator(family, &update).unwrap(); for (at, (i, weight)) in updates.into_iter().enumerate() { operator.validate_single_input(weight).unwrap(); operator.update_keyed(&host(i), weight, at as i64).unwrap(); @@ -129,7 +145,7 @@ fn zipf_events(keys: usize) -> Vec { /// Decode, for `keys` keys, the column each row of `family` (a keyed /// frequency sketch of `width` x `depth`) puts the key in, and assert every /// pair of rows shares a column for few keys, as independent rows do. -fn assert_rows_hash_independently(family: &FieldDataType, width: usize, depth: usize, keys: usize) { +fn assert_rows_hash_independently(family: &Family, width: usize, depth: usize, keys: usize) { // Key `base + p` of a batch carries weight 3^p, so a cell holds a sum // whose balanced-ternary digits name the keys that hit it (CountSketch // signs give digits -1 or 1, Count-Min only 1). @@ -187,13 +203,8 @@ fn deep_plain_count_sketch_rows_hash_independently() { assert_rows_hash_independently(&plain_count_sketch(30_000, 15), 30_000, 15, 2_000); } -fn heap_sketch( - algorithm: SketchAlgorithm, - width: u32, - depth: u32, - heap_size: u32, -) -> FieldDataType { - let _params = match algorithm { +fn heap_sketch(algorithm: SketchAlgorithm, width: u32, depth: u32, heap_size: u32) -> Family { + let params = match algorithm { SketchAlgorithm::CmsWithHeap => SketchParams::CmsWithHeap { width, depth, @@ -205,7 +216,7 @@ fn heap_sketch( heap_size, }, }; - FieldDataType::Sketch(algorithm) + (FieldDataType::Sketch(algorithm), params) } // The factory's top-k sketches use the same per-row-seeded kernel: past 20 @@ -245,7 +256,7 @@ fn heap_sketch_states_load_as_dag_summary_values() { let schema = std::sync::Arc::new(Schema { fields: vec![Field { name: "state".into(), - dtype: family.clone(), + dtype: family.0.clone(), nullable: false, }], unique_keys: vec![], @@ -253,7 +264,7 @@ fn heap_sketch_states_load_as_dag_summary_values() { time_index: None, }); let row = vec![Value::Summary { - family, + family: family.0, state: std::sync::Arc::from(state), }]; assert!(Batch::try_new(schema, vec![row]).is_ok()); @@ -310,7 +321,7 @@ fn count_min_heap_rejects_negative_inputs() { let mut update = SummaryUpdate::column(ColumnRef::SampleValue); update.item = Some(SummaryInputExpr::Column(ColumnRef::Named("host".into()))); let family = heap_sketch(SketchAlgorithm::CmsWithHeap, 64, 3, 4); - let operator = create_planner_accumulator(&family, None, &update).unwrap(); + let operator = accumulator(&family, &update).unwrap(); assert!(operator.validate_single_input(-1.0).is_err()); assert!(operator.validate_single_input(0.5).is_ok()); } @@ -358,7 +369,7 @@ fn plain_count_sketch_merge_equals_union_build() { for i in 0..500 { assert_eq!(plain_estimate(&*merged, i), plain_estimate(&*union, i)); } - // A different shape is a different family and does not merge. + // A different shape (θ) does not merge. assert!(merged .merge_with(&*build_plain_count_sketch( &plain_count_sketch(1_001, 5), @@ -376,7 +387,7 @@ fn keyed_update_overflow_is_an_error_not_a_panic() { let mut update = SummaryUpdate::column(ColumnRef::SampleValue); update.item = Some(SummaryInputExpr::Column(ColumnRef::Named("host".into()))); let family = plain_count_sketch(64, 3); - let mut operator = create_planner_accumulator(&family, None, &update).unwrap(); + let mut operator = accumulator(&family, &update).unwrap(); operator.validate_single_input(f64::MAX).unwrap(); operator.update_keyed(&host(0), f64::MAX, 0).unwrap(); let error = operator.update_keyed(&host(0), f64::MAX, 1).unwrap_err(); diff --git a/crates/executor/tests/exact_state_columns.rs b/crates/executor/tests/exact_state_columns.rs index ce1bcfa61..f8cae953a 100644 --- a/crates/executor/tests/exact_state_columns.rs +++ b/crates/executor/tests/exact_state_columns.rs @@ -94,7 +94,8 @@ fn exact_records_merge_and_evaluate_as_kernel_objects_do() { ] { let family = FieldDataType::ExactAggregate(kind, params); let build = || { - Operator::per_group_summary_build(input(), family.clone(), 2, Some(1), vec![0]).unwrap() + Operator::per_group_summary_build(input(), family.clone(), None, 2, Some(1), vec![0]) + .unwrap() }; let tumbling_windows = [0, 1_000].map(|start| run(build(), vec![tumbling_window_input(start)]).remove(0)); diff --git a/crates/executor/tests/hydra_cms_execution.rs b/crates/executor/tests/hydra_cms_execution.rs index 42df794de..bc889dd24 100644 --- a/crates/executor/tests/hydra_cms_execution.rs +++ b/crates/executor/tests/hydra_cms_execution.rs @@ -37,6 +37,16 @@ const ROWS: [(&str, &str, f64); 9] = [ ("db", "vacuum", 2.), ]; +/// θ of the HydraCms grid. +fn hydra_params() -> SketchParams { + SketchParams::Hydra(HydraParams::HydraCms { + width: WIDTH, + depth: 3, + shared_rows: 3, + shared_columns: SHARED_COLUMNS, + }) +} + fn family() -> FieldDataType { FieldDataType::Sketch(SketchAlgorithm::HydraCms) } @@ -98,7 +108,7 @@ fn unit_count() -> SummaryUpdate { /// The planner contract: item = the counted column, weight = unit count, /// reduction = the group-by keys. fn summary_agg(update: SummaryUpdate, filter: Option>) -> PhysicalASAPDAGNode { - node( + let mut build = node( 1, Payload::ASAP(ASAPOp::SummaryAgg { child: 0, @@ -115,7 +125,9 @@ fn summary_agg(update: SummaryUpdate, filter: Option>) -> Physi nullable: false, }, ]), - ) + ); + build.parameters = Some(hydra_params()); + build } fn estimate(id: usize, query: SketchStatistic, dtype: DataType) -> PhysicalASAPDAGNode { @@ -349,6 +361,7 @@ fn row_weight_serializes_as_one_field_and_rejects_constants_above_i32() { Operator::all_groups_summary_build( input_schema(), family(), + Some(hydra_params()), 1, RowWeight::Constant(std::num::NonZeroU32::new(weight).unwrap()), vec![0], @@ -376,8 +389,12 @@ fn constant_weight_above_i32_is_refused_at_validation() { weight: SummaryInputExpr::Constant(2_147_483_648.0), ..unit_count() }; - let error = asap_executor::capability::validate_summary_kernel(&family(), &update) - .expect_err("validation accepted a constant above i32::MAX"); + let error = asap_executor::capability::validate_summary_kernel( + &family(), + Some(&hydra_params()), + &update, + ) + .expect_err("validation accepted a constant above i32::MAX"); assert!( error.contains("HydraCms") && error.contains("i32::MAX"), "{error}" diff --git a/crates/executor/tests/keyed_summary_row_weight.rs b/crates/executor/tests/keyed_summary_row_weight.rs index ba524ae1d..cdf195bb8 100644 --- a/crates/executor/tests/keyed_summary_row_weight.rs +++ b/crates/executor/tests/keyed_summary_row_weight.rs @@ -31,6 +31,15 @@ const ROWS: [(&str, &str); 6] = [ ("batch", "export"), ]; +/// θ of `heap_family`. +fn heap_params() -> Option { + Some(SketchParams::CmsWithHeap { + width: 1024, + depth: 3, + heap_size: 8, + }) +} + fn heap_family() -> FieldDataType { FieldDataType::Sketch(SketchAlgorithm::CmsWithHeap) } @@ -147,7 +156,7 @@ fn query_time_count_heap_binds_a_constant_row_weight() { }), output_state: ExecutionDataState::QUERY_ROWS, lifetime: Lifetime::None, - parameters: None, + parameters: heap_params(), output_schema: (*schema(vec![ Field::plain("job", DataType::Utf8, false), Field { @@ -179,6 +188,7 @@ fn keyed_build(input: &SchemaRef, row_weight: RowWeight) -> Operator { Operator::per_group_keyed_summary_build( input.clone(), heap_family(), + heap_params(), vec![1], row_weight, vec![0], @@ -239,6 +249,11 @@ fn int64_weight_column_equals_float64_column() { let signed = Operator::per_group_keyed_summary_build( int64.clone(), count_sketch, + Some(SketchParams::CountSketchWithHeap { + width: 1024, + depth: 3, + heap_size: 8, + }), vec![1], RowWeight::Column(2), vec![0], @@ -296,6 +311,7 @@ fn int64_weight_column_equals_float64_column() { let error = Operator::per_group_keyed_summary_build( text, heap_family(), + heap_params(), vec![1], RowWeight::Column(2), vec![0], diff --git a/crates/executor/tests/main.rs b/crates/executor/tests/main.rs index 3aad7004d..da826617d 100644 --- a/crates/executor/tests/main.rs +++ b/crates/executor/tests/main.rs @@ -27,4 +27,5 @@ mod summary_projection; mod text_extremum_memory; mod thread_safety; mod time_series_window; +mod tumbling_window_chain; mod univmon_execution; diff --git a/crates/executor/tests/physical_dag.rs b/crates/executor/tests/physical_dag.rs index ec9432e08..e1b2ca278 100644 --- a/crates/executor/tests/physical_dag.rs +++ b/crates/executor/tests/physical_dag.rs @@ -164,7 +164,8 @@ fn summary_construction_merge_and_evaluation_at_both_phases() { .map(|v| Batch::try_new(schema.clone(), vec![vec![Value::Float64(v as f64)]]).unwrap()) .collect(); let family = FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum); - let build = Operator::per_group_summary_build(schema.clone(), family, 0, None, vec![]).unwrap(); + let build = + Operator::per_group_summary_build(schema.clone(), family, None, 0, None, vec![]).unwrap(); let state = build.schema(); let mut dag = PhysicalDAG::default(); dag.add(0, vec![], Operator::source(schema, batches).unwrap()) @@ -323,6 +324,7 @@ fn binding_rejects_unsupported_operations() { assert!(Operator::per_group_summary_build( schema.clone(), FieldDataType::ExactAggregate(ExactKind::Rate, ExactParams::Rate), + None, 0, None, vec![] @@ -331,6 +333,7 @@ fn binding_rejects_unsupported_operations() { let sum = Operator::per_group_summary_build( schema.clone(), FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum), + None, 0, None, vec![], @@ -354,7 +357,15 @@ fn kll_raw_partial_and_precomputed_are_native_dags() { let input = schema(&[("value", DataType::Float64, false)]); let family = FieldDataType::Sketch(SketchAlgorithm::Kll); - let build = Operator::per_group_summary_build(input.clone(), family, 0, None, vec![]).unwrap(); + let build = Operator::per_group_summary_build( + input.clone(), + family, + Some(planner_types::summary_type::SketchParams::Kll { k: 200 }), + 0, + None, + vec![], + ) + .unwrap(); let state = build.schema(); let build_range = |start: u32, end: u32| { let mut dag = PhysicalDAG::default(); @@ -609,6 +620,7 @@ fn empty_exact_count_is_an_integer_state_evaluation() { let build = Operator::per_group_summary_build( input.clone(), FieldDataType::ExactAggregate(ExactKind::Count, ExactParams::Count), + None, 0, None, vec![], @@ -968,7 +980,7 @@ fn weighted_rate_topk_preserves_partitions_fractional_scores_and_evaluation_scop } } fn assert_weighted_rate_topk(count_sketch: bool) { - use planner_types::summary_type::SketchAlgorithm; + use planner_types::summary_type::{SketchAlgorithm, SketchParams}; let raw = schema(&[ ("service", DataType::Utf8, false), @@ -1012,9 +1024,23 @@ fn assert_weighted_rate_topk(count_sketch: bool) { } else { SketchAlgorithm::CmsWithHeap }); + let parameters = if count_sketch { + SketchParams::CountSketchWithHeap { + width: 1024, + depth: 3, + heap_size: 8, + } + } else { + SketchParams::CmsWithHeap { + width: 1024, + depth: 3, + heap_size: 8, + } + }; let build = Operator::per_group_keyed_summary_build( rates.schema(), family, + Some(parameters), vec![0], asap_executor::operators::RowWeight::Column(3), vec![1], diff --git a/crates/executor/tests/physical_semantics.rs b/crates/executor/tests/physical_semantics.rs index 79be1b280..043d86c3a 100644 --- a/crates/executor/tests/physical_semantics.rs +++ b/crates/executor/tests/physical_semantics.rs @@ -552,6 +552,11 @@ fn boolean_truth_tables_agree_between_expression_paths() { } } +/// θ of a KLL with k = 512. +fn kll_512() -> planner_types::summary_type::SketchParams { + planner_types::summary_type::SketchParams::Kll { k: 512 } +} + // Partial/final execution must agree with one build for an uncompacted KLL population. #[test] fn kll_partial_merge_and_multiple_evaluations_preserve_population() { @@ -575,12 +580,19 @@ fn kll_partial_merge_and_multiple_evaluations_preserve_population() { dag.add( id + 3, vec![id], - Operator::per_group_summary_build(input.clone(), family.clone(), 0, None, vec![]) - .unwrap(), + Operator::per_group_summary_build( + input.clone(), + family.clone(), + Some(kll_512()), + 0, + None, + vec![], + ) + .unwrap(), ) .unwrap(); } - let state = Operator::per_group_summary_build(input, family, 0, None, vec![]) + let state = Operator::per_group_summary_build(input, family, Some(kll_512()), 0, None, vec![]) .unwrap() .schema(); dag.add(6, vec![3, 4], Operator::union(state.clone(), 2).unwrap()) @@ -653,7 +665,15 @@ fn compacted_kll_tumbling_windows_merge_within_the_calibrated_rank_error() { let input = schema(&[("v", DataType::Float64, false)]); let family = FieldDataType::Sketch(SketchAlgorithm::Kll); let build = || { - Operator::per_group_summary_build(input.clone(), family.clone(), 0, None, vec![]).unwrap() + Operator::per_group_summary_build( + input.clone(), + family.clone(), + Some(planner_types::summary_type::SketchParams::Kll { k: K }), + 0, + None, + vec![], + ) + .unwrap() }; let state = build().schema(); let mut dag = PhysicalDAG::default(); @@ -737,7 +757,7 @@ fn counter_tumbling_windows_merge_in_any_order() { ]); let family = FieldDataType::ExactAggregate(ExactKind::Increase, ExactParams::Increase); let build = || { - Operator::per_group_summary_build(input.clone(), family.clone(), 0, Some(1), vec![]) + Operator::per_group_summary_build(input.clone(), family.clone(), None, 0, Some(1), vec![]) .unwrap() }; let state = build().schema(); @@ -827,6 +847,7 @@ fn empty_exact_summary_extrema_agree_with_ordinary_aggregation() { let build = Operator::per_group_summary_build( input.clone(), FieldDataType::ExactAggregate(kind, params), + None, 0, None, vec![], @@ -1036,7 +1057,7 @@ fn filtered_summary_build_keeps_empty_groups() { ), ]; for (family, read, (result, nullable), expected) in cases { - let build = planner_node( + let mut build = planner_node( PhysicalASAPOperatorPayload::ASAP(ASAPOp::SummaryAgg { child: 0, summary_type: family.clone(), @@ -1049,6 +1070,7 @@ fn filtered_summary_build_keeps_empty_groups() { ("state", family, false), ], ); + asap_executor::physical_planner::test_support::set_build_parameters([&mut build], None); let build = round_trip(compile_node(&build, std::slice::from_ref(&input)).unwrap()); let read = planner_node( PhysicalASAPOperatorPayload::ASAP(read), diff --git a/crates/executor/tests/plan_properties.rs b/crates/executor/tests/plan_properties.rs index d35156b61..5623fe603 100644 --- a/crates/executor/tests/plan_properties.rs +++ b/crates/executor/tests/plan_properties.rs @@ -130,6 +130,44 @@ fn blocking_inputs_over_a_raw_data_slot_are_finite() { assert_eq!(opens.load(Ordering::SeqCst), 1); } +/// A KLL kernel accepts `k` only up to the 26 602 `asap_sketchlib` caps it at. +#[test] +fn kll_kernel_rejects_a_k_above_the_library_cap() { + use asap_executor::capability::validate_summary_kernel; + use planner_types::ir::scalar::ColumnRef; + use planner_types::summary_type::{SketchAlgorithm, SketchParams, SummaryUpdate}; + let update = SummaryUpdate::column(ColumnRef::SampleValue); + let kll = FieldDataType::Sketch(SketchAlgorithm::Kll); + let params = |k| SketchParams::Kll { k }; + assert!(validate_summary_kernel(&kll, Some(¶ms(26_602)), &update).is_ok()); + assert!(validate_summary_kernel(&kll, Some(¶ms(26_603)), &update).is_err()); +} + +/// A plain CountSketch reads the median of its rows, so plan validation and +/// kernel construction both require a positive odd depth. +#[test] +fn count_sketch_kernel_requires_an_odd_depth() { + use asap_executor::{capability::validate_summary_kernel, factory::create_planner_accumulator}; + use planner_types::ir::scalar::ColumnRef; + use planner_types::summary_type::{ + SketchAlgorithm, SketchParams, SummaryInputExpr, SummaryUpdate, + }; + let mut update = SummaryUpdate::column(ColumnRef::SampleValue); + update.item = Some(SummaryInputExpr::Column(ColumnRef::Named("host".into()))); + let family = FieldDataType::Sketch(SketchAlgorithm::CountSketch); + for (depth, valid) in [(5, true), (4, false)] { + let params = SketchParams::CountSketch { width: 64, depth }; + assert_eq!( + validate_summary_kernel(&family, Some(¶ms), &update).is_ok(), + valid + ); + assert_eq!( + create_planner_accumulator(&family, Some(¶ms), &update).is_ok(), + valid + ); + } +} + // Kernel support must not be mistaken for executable native state/evaluation support. #[test] fn summary_capability_levels_are_distinct() { @@ -146,7 +184,13 @@ fn summary_capability_levels_are_distinct() { )), weight: planner_types::summary_type::SummaryInputExpr::Constant(1.0), }; - assert!(validate_summary_kernel(&cms, &update).is_ok()); + let cms_params = planner_types::summary_type::SketchParams::Cms { + width: 64, + depth: 4, + }; + assert!(validate_summary_kernel(&cms, Some(&cms_params), &update).is_ok()); + // A sketch kernel needs its θ. + assert!(validate_summary_kernel(&cms, None, &update).is_err()); // Stored Count-Min state reads only its bare count natively. assert!(validate_native_family(&cms).is_ok()); let bare_count = SketchStatistic::PointCount { diff --git a/crates/executor/tests/precompute_population.rs b/crates/executor/tests/precompute_population.rs index 39486bb2d..7d908bb4e 100644 --- a/crates/executor/tests/precompute_population.rs +++ b/crates/executor/tests/precompute_population.rs @@ -11,6 +11,7 @@ use planner_types::ir::properties::*; use planner_types::ir::scalar::ColumnRef; use planner_types::ir::schema::{DataType, *}; use planner_types::ir::ASAPOp; +use planner_types::plan::materialized_view::MaterializedView; use planner_types::plan::physical_asap_dag::{ EdgeRole, GroupingEdgeCompatibility, PhysicalASAPDAG, PhysicalASAPDAGEdge, PhysicalASAPDAGNode, PhysicalASAPOperatorPayload, WindowEdgeCompatibility, @@ -113,14 +114,14 @@ fn state_dag( family: FieldDataType, target: Option, merge: bool, -) -> CompiledPhysicalDAG { +) -> (CompiledPhysicalDAG, MaterializedView) { try_state_dag(family, target, merge).unwrap() } fn try_state_dag( family: FieldDataType, target: Option, merge: bool, -) -> Result { +) -> Result<(CompiledPhysicalDAG, MaterializedView), asap_executor::Error> { let scan = 2 + usize::from(merge) + usize::from(target.is_some()); let [view, scan_node, range_node] = view_over_samples(family.clone(), scan); let mut nodes = vec![view]; @@ -190,16 +191,17 @@ fn try_state_dag( roots: vec![root], scalar_roots: vec![], }; + asap_executor::physical_planner::test_support::set_build_parameters(&mut dag.nodes, None); let views = views_at(&mut dag, &[0])?; - compile(&dag, &views, &[root as u64]) + Ok((compile(&dag, &views, &[root as u64])?, views[0].clone())) } fn native_run( - program: &CompiledPhysicalDAG, + (program, view): &(CompiledPhysicalDAG, MaterializedView), family: FieldDataType, states: Vec>, context: RunContext, ) -> Result>, asap_executor::Error> { - let (view, schema) = program.input_slots().next().unwrap(); + let (_, schema) = program.input_slots().next().unwrap(); let rows = states .into_iter() .map(|state| { @@ -210,7 +212,10 @@ fn native_run( }) .collect(); let input = Batch::try_new(schema.clone(), rows)?; - let store = InMemoryDataStore::default().with_materialized_view(view as usize, vec![input])?; + let mut store = InMemoryDataStore::default(); + // Every run reads the view's one-minute window ending at the ingestion + // run's end. + store.append_materialized_view(view, (2000 - 60_000, 2000), vec![input])?; let physical_dag = program.instantiate(Arc::new(store))?; block_on(async { let mut rows = Vec::new(); diff --git a/crates/executor/tests/promql_binary.rs b/crates/executor/tests/promql_binary.rs index fb36382d1..825f9f953 100644 --- a/crates/executor/tests/promql_binary.rs +++ b/crates/executor/tests/promql_binary.rs @@ -480,7 +480,10 @@ fn stored_series_evaluations_support_filters_and_sets() { ]], ) .unwrap(); - store = store.with_materialized_view(id, vec![batch]).unwrap(); + // Each view is read for its one-minute window ending at T = 1. + store + .append_materialized_view(&views[id], (1 - 60_000, 1), vec![batch]) + .unwrap(); } let bound = physical_dag.instantiate(Arc::new(store)).unwrap(); let context = RunContext::new( diff --git a/crates/executor/tests/raw_scan.rs b/crates/executor/tests/raw_scan.rs index 033118a69..28408ed3a 100644 --- a/crates/executor/tests/raw_scan.rs +++ b/crates/executor/tests/raw_scan.rs @@ -918,8 +918,7 @@ impl DataStore for RecordingViewStore { } } -// A materialized view slot (the count over the range, as an expanded tumbling window -// over its range) is asked for its window, `(T - 5m, T]` or +// A materialized view slot (the count over the range) is asked for its window, `(T - 5m, T]` or // `(T - 15m, T - 10m]` under a 10-minute shift, and its state is the // answer's. #[test] diff --git a/crates/executor/tests/state_column_updater.rs b/crates/executor/tests/state_column_updater.rs index 076b8633d..bcfd31660 100644 --- a/crates/executor/tests/state_column_updater.rs +++ b/crates/executor/tests/state_column_updater.rs @@ -1,13 +1,14 @@ //! The per-sample `StateColumnUpdater` builds the same states as the //! batch `SummaryAgg` build and as each family's kernel updated row by row, //! for every family a per-group build supports. - +use asap_executor::factory::create_planner_accumulator; use asap_executor::operators::{Operator, RowWeight}; use asap_executor::plan::PhysicalDAG; use asap_executor::planner::ir::scalar::ColumnRef; use asap_executor::planner::ir::schema::{DataType, Field, FieldDataType, Schema}; use asap_executor::planner::summary_type::{ - ExactKind, ExactParams, SketchAlgorithm, SketchParams, SketchStatistic, + ExactKind, ExactParams, HydraParams, SketchAlgorithm, SketchParams, SketchStatistic, + SummaryUpdate, }; use asap_executor::runtime::{Limits, RunContext, Scope}; use asap_executor::summary_kernels::weighted_frequency::WeightedFrequency; @@ -90,16 +91,55 @@ fn input_batch() -> Batch { .unwrap() } -fn sketch(algorithm: SketchAlgorithm, _params: SketchParams) -> FieldDataType { - FieldDataType::Sketch(algorithm) +/// A family and its θ (`None` for an exact family), as a build node +/// carries them. +type Family = (FieldDataType, Option); + +fn sketch(algorithm: SketchAlgorithm, params: SketchParams) -> Family { + (FieldDataType::Sketch(algorithm), Some(params)) } -fn exact(kind: ExactKind, params: ExactParams) -> FieldDataType { - FieldDataType::ExactAggregate(kind, params) +fn exact(kind: ExactKind, params: ExactParams) -> Family { + (FieldDataType::ExactAggregate(kind, params), None) +} + +/// An updater of `family` with no group. +fn updater(family: &Family) -> StateColumnUpdater { + StateColumnUpdater::new(&family.0, family.1.as_ref()).unwrap() +} + +/// Every family a per-group value build supports. +fn value_families() -> Vec { + vec![ + exact(ExactKind::Sum, ExactParams::Sum), + exact(ExactKind::Count, ExactParams::Count), + exact(ExactKind::Min, ExactParams::Min), + exact(ExactKind::Max, ExactParams::Max), + exact(ExactKind::Rate, ExactParams::Rate), + exact(ExactKind::Increase, ExactParams::Increase), + // `k` above any group's sample count (at most 600): no compaction, + // so the random compaction coin never flips. + sketch(SketchAlgorithm::Kll, SketchParams::Kll { k: 1024 }), + sketch( + SketchAlgorithm::DDSketch, + SketchParams::DDSketch { alpha: 0.01 }, + ), + sketch(SketchAlgorithm::Hll, SketchParams::Hll { precision: 10 }), + sketch(SketchAlgorithm::Kmv, SketchParams::Kmv { k: 32 }), + sketch( + SketchAlgorithm::UnivMon, + SketchParams::UnivMon { + heap_size: 64, + sketch_rows: 5, + sketch_cols: 128, + layers: 4, + }, + ), + ] } /// Every keyed family a per-group keyed build supports. -fn keyed_families() -> Vec { +fn keyed_families() -> Vec { vec![ sketch( SketchAlgorithm::CmsWithHeap, @@ -231,22 +271,227 @@ fn batch_build(build: Operator) -> BTreeMap { /// One test sample: series, timestamp, value, item. type TestSample = (String, i64, f64, String); +/// How a test feeds one sample to an updater. +type FeedSample<'a> = dyn FnMut(&mut StateColumnUpdater, u32, &TestSample) + 'a; + +/// The per-sample build: one updater, groups numbered as series appear. +/// With `sharded`, the first half of the samples go to one updater and the +/// rest to another, merged at the end as a runtime's shards are. +fn per_sample_build( + family: &Family, + sharded: bool, + mut update: impl FnMut(&mut StateColumnUpdater, u32, &TestSample), +) -> BTreeMap { + let samples = samples(); + let mut groups: BTreeMap = BTreeMap::new(); + for sample in &samples { + let next = groups.len() as u32; + groups.entry(sample.0.clone()).or_insert(next); + } + let shard = |part: &[TestSample], update: &mut FeedSample<'_>| { + let mut updater = updater(family); + updater.ensure_groups(groups.len()).unwrap(); + for sample in part { + update(&mut updater, groups[&sample.0], sample); + } + updater + }; + let split = if sharded { + samples.len() / 2 + } else { + samples.len() + }; + let (first, second) = samples.split_at(split); + let mut updater = shard(first, &mut update); + if sharded { + updater.merge(shard(second, &mut update)).unwrap(); + } + assert_eq!(updater.group_count(), groups.len()); + let states = updater.seal(); + groups + .into_iter() + .map(|(series, group)| (series, fingerprint(states.state(group as usize).as_ref()))) + .collect() +} + +/// The reference: each family's kernel, one per series, updated row by +/// row. With `sharded`, two halves merged through the kernel's own merge. +fn kernel_build(family: &Family, keyed: bool, sharded: bool) -> BTreeMap { + use asap_executor::planner::summary_type::SummaryInputExpr; + use asap_executor::summary_kernels::weighted_frequency::FrequencyAlgorithm; + let update = if keyed { + SummaryUpdate { + item: Some(SummaryInputExpr::Column(ColumnRef::Named("item".into()))), + weight: SummaryInputExpr::Constant(1.0), + } + } else { + SummaryUpdate::column(ColumnRef::SampleValue) + }; + let samples = samples(); + let split = if sharded { + samples.len() / 2 + } else { + samples.len() + }; + let (first, second) = samples.split_at(split); + let half = |part: &[TestSample]| { + let mut states = BTreeMap::new(); + for (series, ts, value, item) in part { + let state = states.entry(series.clone()).or_insert_with(|| { + if keyed { + let (algorithm, width, depth, capacity) = match family.1.as_ref().unwrap() { + SketchParams::CmsWithHeap { + width, + depth, + heap_size, + } => (FrequencyAlgorithm::Cms, width, depth, heap_size), + SketchParams::CountSketchWithHeap { + width, + depth, + heap_size, + } => (FrequencyAlgorithm::CountSketch, width, depth, heap_size), + _ => unreachable!(), + }; + Err(Box::new( + WeightedFrequency::new( + algorithm, + *width as usize, + *depth as usize, + *capacity as usize, + ) + .unwrap(), + )) + } else { + Ok(create_planner_accumulator(&family.0, family.1.as_ref(), &update).unwrap()) + } + }); + match state { + Ok(updater) => updater.update_value(&Value::Float64(*value), *ts).unwrap(), + Err(keyed) => keyed + .update(&[Value::Utf8(item.as_str().into())], 1.0) + .unwrap(), + } + } + states + .into_iter() + .map(|(series, state)| { + ( + series, + match state { + Ok(updater) => Arc::::from(updater.into_accumulator()), + Err(keyed) => Arc::new(*keyed) as Arc, + }, + ) + }) + .collect::>() + }; + let (mut merged, second) = (half(first), half(second)); + for (series, state) in second { + let combined = match merged.get(&series) { + Some(existing) => Arc::from(existing.merge_with(state.as_ref()).unwrap()), + None => state, + }; + merged.insert(series, combined); + } + merged + .into_iter() + .map(|(series, state)| (series, fingerprint(state.as_ref()))) + .collect() +} + +// For every value family: the per-sample updater, the batch `SummaryAgg` +// build and the family's own kernel build the same state for every group, +// byte for byte; two updater shards merged equal the kernel's merge of the +// same two halves. +#[test] +fn per_sample_updates_build_the_batch_states_for_every_value_family() { + for family in value_families() { + let batch = batch_build( + Operator::per_group_summary_build( + input_schema(), + family.0.clone(), + family.1.clone(), + 2, + Some(1), + vec![0], + ) + .unwrap(), + ); + let update = |updater: &mut StateColumnUpdater, group, sample: &TestSample| { + updater.update(group, sample.2, sample.1).unwrap() + }; + assert_eq!(batch.len(), 7, "{family:?}"); + assert_eq!( + per_sample_build(&family, false, update), + batch, + "{family:?}: updater vs batch build" + ); + assert_eq!( + kernel_build(&family, false, false), + batch, + "{family:?}: kernel vs batch build" + ); + assert_eq!( + per_sample_build(&family, true, update), + kernel_build(&family, false, true), + "{family:?}: merged shards vs kernel merge" + ); + } +} + +// The same for the keyed families (an item and a weight per sample). +#[test] +fn per_sample_updates_build_the_batch_states_for_every_keyed_family() { + for family in keyed_families() { + let batch = batch_build( + Operator::per_group_keyed_summary_build( + input_schema(), + family.0.clone(), + family.1.clone(), + vec![3], + RowWeight::Constant(NonZeroU32::new(1).unwrap()), + vec![0], + ) + .unwrap(), + ); + let update = |updater: &mut StateColumnUpdater, group, sample: &TestSample| { + updater + .update_item(group, &[Value::Utf8(sample.3.as_str().into())], 1.0) + .unwrap() + }; + assert_eq!(batch.len(), 7, "{family:?}"); + assert_eq!( + per_sample_build(&family, false, update), + batch, + "{family:?}: updater vs batch build" + ); + assert_eq!( + kernel_build(&family, true, false), + batch, + "{family:?}: kernel vs batch build" + ); + assert_eq!( + per_sample_build(&family, true, update), + kernel_build(&family, true, true), + "{family:?}: merged shards vs kernel merge" + ); + } +} // A counter refuses a sample that is not after its group's previous one, // and a value outside the kernel's domain, leaving the state unchanged. #[test] fn per_sample_updates_keep_each_kernels_input_contract() { - let mut rate = StateColumnUpdater::new(&exact(ExactKind::Rate, ExactParams::Rate)).unwrap(); + let mut rate = updater(&exact(ExactKind::Rate, ExactParams::Rate)); let group = rate.add_group().unwrap(); rate.update(group, 1.0, 100).unwrap(); assert!(rate.update(group, 2.0, 100).is_err()); assert!(rate.update(group, f64::INFINITY, 200).is_err()); rate.update(group, 3.0, 200).unwrap(); - let mut dd = StateColumnUpdater::new(&sketch( + let mut dd = updater(&sketch( SketchAlgorithm::DDSketch, SketchParams::DDSketch { alpha: 0.01 }, - )) - .unwrap(); + )); let group = dd.add_group().unwrap(); // A DDSketch takes zero and negative values, not NaN or ±Inf. dd.update(group, -1.0, 0).unwrap(); @@ -254,7 +499,7 @@ fn per_sample_updates_keep_each_kernels_input_contract() { assert!(dd.update(group, f64::NAN, 0).is_err()); assert!(dd.update(group, f64::INFINITY, 0).is_err()); // A keyed family takes items, a value family takes values. - let mut cms = StateColumnUpdater::new(&keyed_families()[0]).unwrap(); + let mut cms = updater(&keyed_families()[0]); let group = cms.add_group().unwrap(); assert!(cms.update(group, 1.0, 0).is_err()); assert!(dd @@ -272,7 +517,7 @@ fn merging_counter_shards_that_overlap_in_time_is_refused() { exact(ExactKind::Increase, ExactParams::Increase), ] { let shard = |samples: &[(i64, f64)]| { - let mut updater = StateColumnUpdater::new(&family).unwrap(); + let mut updater = updater(&family); let group = updater.add_group().unwrap(); for &(ts, value) in samples { updater.update(group, value, ts).unwrap(); @@ -308,13 +553,13 @@ fn merging_counter_shards_that_overlap_in_time_is_refused() { // A group number the updater never added is bad input, not a panic. #[test] fn an_unknown_group_is_bad_input() { - let mut sum = StateColumnUpdater::new(&exact(ExactKind::Sum, ExactParams::Sum)).unwrap(); + let mut sum = updater(&exact(ExactKind::Sum, ExactParams::Sum)); sum.add_group().unwrap(); assert!(matches!( sum.update(1, 1.0, 0), Err(asap_executor::Error::Invalid(_)) )); - let mut cms = StateColumnUpdater::new(&keyed_families()[0]).unwrap(); + let mut cms = updater(&keyed_families()[0]); assert!(matches!( cms.update_item(0, &[Value::Utf8("a".into())], 1.0), Err(asap_executor::Error::Invalid(_)) @@ -325,8 +570,16 @@ fn an_unknown_group_is_bad_input() { } /// A HydraCms family: one shared grid for all groups. -fn hydra_family() -> FieldDataType { - FieldDataType::Sketch(SketchAlgorithm::HydraCms) +fn hydra_family() -> Family { + ( + FieldDataType::Sketch(SketchAlgorithm::HydraCms), + Some(SketchParams::Hydra(HydraParams::HydraCms { + width: 64, + depth: 3, + shared_rows: 3, + shared_columns: 16, + })), + ) } /// The per-sample HydraCms build: groups added with their key as series @@ -335,7 +588,7 @@ fn hydra_per_sample_build(sharded: bool) -> BTreeMap { let family = hydra_family(); let samples = samples(); let shard = |part: &[TestSample]| { - let mut updater = StateColumnUpdater::new(&family).unwrap(); + let mut updater = updater(&family); let mut groups = BTreeMap::::new(); for sample in part { let group = match groups.get(&sample.0) { @@ -358,7 +611,7 @@ fn hydra_per_sample_build(sharded: bool) -> BTreeMap { if sharded { // The second shard numbers groups as the first does: the runtime's // shared map from labels to group numbers. - let mut other = StateColumnUpdater::new(&family).unwrap(); + let mut other = self::updater(&family); let mut by_number = groups .iter() .map(|(series, &group)| (group, series.clone())) @@ -402,7 +655,8 @@ fn per_sample_updates_build_the_batch_hydra_grid() { let batch = batch_build( Operator::all_groups_summary_build( input_schema(), - hydra_family(), + hydra_family().0, + hydra_family().1, 3, RowWeight::Constant(NonZeroU32::new(1).unwrap()), vec![0], @@ -428,7 +682,7 @@ fn per_sample_updates_build_the_batch_hydra_grid() { #[test] fn hydra_updates_keep_the_kernel_input_contract() { let family = hydra_family(); - let mut updater = StateColumnUpdater::new(&family).unwrap(); + let mut updater = updater(&family); assert!(matches!( updater.add_group(), Err(asap_executor::Error::Invalid(_)) @@ -465,7 +719,7 @@ fn hydra_updates_keep_the_kernel_input_contract() { assert_eq!(updater.group_count(), 1); updater.add_group_with_key(&[Value::Null]).unwrap(); assert_eq!(updater.group_count(), 2); - let mut other = StateColumnUpdater::new(&family).unwrap(); + let mut other = self::updater(&family); other .add_group_with_key(&[Value::Utf8("web".into())]) .unwrap(); diff --git a/crates/executor/tests/stored_tumbling_window_memory_limit.rs b/crates/executor/tests/stored_tumbling_window_memory_limit.rs index 9570e9fcb..af389cc18 100644 --- a/crates/executor/tests/stored_tumbling_window_memory_limit.rs +++ b/crates/executor/tests/stored_tumbling_window_memory_limit.rs @@ -6,7 +6,7 @@ use asap_executor::arrow::datatypes::UInt32Type; use asap_executor::operators::{Operator, SortKey}; use asap_executor::plan::PhysicalDAG; use asap_executor::planner::ir::schema::{DataType, Field, FieldDataType, Schema}; -use asap_executor::planner::summary_type::SketchAlgorithm; +use asap_executor::planner::summary_type::{SketchAlgorithm, SketchParams}; use asap_executor::runtime::{Limits, RunContext, Scope}; use asap_executor::values::{Batch, BatchColumn}; use asap_executor::{Error, StateColumnUpdater}; @@ -44,7 +44,8 @@ fn tumbling_window_of(series_count: u32) -> (Arc, Batch) { unique_keys: vec![], closed: false, }); - let mut updater = StateColumnUpdater::new(&family).unwrap(); + let mut updater = + StateColumnUpdater::new(&family, Some(&SketchParams::Kll { k: 200 })).unwrap(); updater.ensure_groups(series_count as usize).unwrap(); for series in 0..series_count { for sample in 0..SAMPLES { diff --git a/crates/executor/tests/summary_build_memory_limit.rs b/crates/executor/tests/summary_build_memory_limit.rs index e08176203..ea30af325 100644 --- a/crates/executor/tests/summary_build_memory_limit.rs +++ b/crates/executor/tests/summary_build_memory_limit.rs @@ -4,7 +4,7 @@ use asap_executor::operators::Operator; use asap_executor::plan::PhysicalDAG; use asap_executor::planner::ir::schema::{DataType, Field, FieldDataType, Schema}; -use asap_executor::planner::summary_type::SketchAlgorithm; +use asap_executor::planner::summary_type::{SketchAlgorithm, SketchParams}; use asap_executor::runtime::{Limits, RunContext, Scope}; use asap_executor::values::{Batch, Value}; use asap_executor::Error; @@ -66,7 +66,15 @@ fn one_large_batch_fails_near_the_memory_limit() { dag.add( 1, vec![0], - Operator::per_group_summary_build(input, family, 1, None, vec![0]).unwrap(), + Operator::per_group_summary_build( + input, + family, + Some(SketchParams::Kll { k: 200 }), + 1, + None, + vec![0], + ) + .unwrap(), ) .unwrap(); let context = RunContext::new( diff --git a/crates/executor/tests/summary_projection.rs b/crates/executor/tests/summary_projection.rs index 48b458694..6b4ecc950 100644 --- a/crates/executor/tests/summary_projection.rs +++ b/crates/executor/tests/summary_projection.rs @@ -211,8 +211,10 @@ fn summary_projection_survives_recovery() { ]], ) .unwrap(); - let store = InMemoryDataStore::default() - .with_materialized_view(0, vec![batch]) + let mut store = InMemoryDataStore::default(); + // The view is read for its one-minute window ending at T = 1. + store + .append_materialized_view(&views[0], (1 - 60_000, 1), vec![batch]) .unwrap(); let physical_dag = program.instantiate(Arc::new(store)).unwrap(); let context = RunContext::new( diff --git a/crates/executor/tests/tumbling_window_chain.rs b/crates/executor/tests/tumbling_window_chain.rs new file mode 100644 index 000000000..6745fe07e --- /dev/null +++ b/crates/executor/tests/tumbling_window_chain.rs @@ -0,0 +1,373 @@ +//! A tumbling-window chain runs natively: each ingestion run builds the +//! chain's window ending at the run's end, the runtime appends it to its +//! time-indexed store, and a query run merges the windows the `TimeRange` +//! (and `TimeShift`) above the chain select. And a `TimeShift` with an `@` +//! anchor evaluates its input at the anchor's time. +use asap_executor::physical_planner::{compile, test_support::views_at}; +use asap_executor::runtime::{Limits, RunContext, Scope}; +use asap_executor::sources::InMemoryDataStore; +use asap_executor::values::{Batch, Value}; +use futures::{executor::block_on, StreamExt}; +use planner_types::ir::operator::{AtModifier, Reduction, Source, TimeShift}; +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::summary_type::{ExactKind, ExactParams, SummaryUpdate}; +use std::collections::BTreeMap; +use std::rc::Rc; +use std::sync::Arc; +use std::time::Duration; + +const MINUTE_MS: i64 = 60_000; +const WIDTH_MS: i64 = 10 * MINUTE_MS; +const HOUR_MS: i64 = 60 * MINUTE_MS; + +fn samples_schema() -> Schema { + Schema::with_time_index( + vec![ + Field::plain("job", DataType::Utf8, false), + Field::plain("ts", DataType::Timestamp, false), + Field::plain("value", DataType::Float64, false), + ], + 1, + vec![], + ) +} + +fn source() -> Source { + Source::TimeSeries { + metric: "latency".into(), + } +} + +fn node(operator: Operator) -> Rc { + OperatorNode::new_shared(operator).unwrap() +} + +fn scan() -> Rc { + node(Operator::NonASAP(NonASAPOp::Scan { + source: source(), + predicates: vec![], + schema: samples_schema(), + })) +} + +/// The exact per-`job` sum of `value`. +fn sum() -> FieldDataType { + FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum) +} + +fn range(width_ms: i64, child: Rc) -> Rc { + node(Operator::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_millis(width_ms as u64), + kind: TimeRangeKind::Range, + child, + })) +} + +fn shift(offset_ms: i64, at: Option, child: Rc) -> Rc { + node(Operator::NonASAP(NonASAPOp::TimeShift { + shift: TimeShift { offset_ms, at }, + child, + })) +} + +/// The sum per job, finalized, of the states `states` gives. +fn finalized_sum(states: Rc) -> Rc { + node(Operator::ASAP(ASAPOp::FinalizeExactAccumulator { + child: states, + })) +} + +/// Two series, `a` and `b`, one sample per minute over `(0, 4h]`: `a` is +/// worth `t` minutes, `b` twice that. +fn samples() -> Vec<(&'static str, i64, f64)> { + (1..=4 * 60) + .flat_map(|minute| { + let t = minute * MINUTE_MS; + [("a", t, minute as f64), ("b", t, 2.0 * minute as f64)] + }) + .collect() +} + +/// The sum per job of the samples in `(start, end]`, computed directly. +fn direct_sums(start: i64, end: i64) -> BTreeMap { + let mut sums = BTreeMap::new(); + for (job, t, value) in samples() { + if start < t && t <= end { + *sums.entry(job.to_string()).or_insert(0.0) += value; + } + } + sums +} + +fn store_with_samples() -> InMemoryDataStore { + let schema = Arc::new(samples_schema()); + let rows = samples() + .into_iter() + .map(|(job, t, value)| { + vec![ + Value::Utf8(job.into()), + Value::Timestamp(t), + Value::Float64(value), + ] + }) + .collect(); + let batch = Batch::try_new(schema.clone(), rows).unwrap(); + InMemoryDataStore::default() + .with_raw_data(source(), schema, vec![batch]) + .unwrap() +} + +/// `root`'s rows as a sum per job: its `job` and `Float64` columns. +fn sums_of(rows: Vec>) -> BTreeMap { + rows.into_iter() + .map(|row| { + let job = row.iter().find_map(|value| match value { + Value::Utf8(job) => Some(job.to_string()), + _ => None, + }); + let sum = row.iter().find_map(|value| match value { + Value::Float64(sum) => Some(*sum), + _ => None, + }); + (job.unwrap(), sum.unwrap()) + }) + .collect() +} + +fn run( + dag: &asap_executor::plan::PhysicalDAG<'static, Batch, asap_executor::values::SchemaRef>, + roots: &[u64], + context: RunContext, +) -> Vec>> { + // The roots share producers, so they are read together. + block_on(futures::future::join_all( + dag.execute(roots, context) + .unwrap() + .into_iter() + .map(|mut output| async move { + let mut rows = Vec::new(); + while let Some(batch) = output.next().await { + rows.extend(batch.unwrap().to_rows()); + } + rows + }), + )) +} + +/// The id of the node of `dag` that is `operator`'s node in `nodes`. +fn id_of(nodes: &[Rc], operator: &Rc) -> PhysicalASAPNodeId { + nodes + .iter() + .position(|node| Rc::ptr_eq(node, operator)) + .unwrap() +} + +/// `quantile`-style panel over a chain, with exact sums: the chain of +/// 10-minute windows over `latency`, built at ingestion and retained for +/// 2 h; query `now` merges the windows in `(T - 1h, T]`, query `shifted` +/// (`offset 1h`) those in `(T - 2h, T - 1h]`. +fn chain_plan() -> (PhysicalASAPDAG, PhysicalASAPNodeId, [PhysicalASAPNodeId; 2]) { + 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: WIDTH_MS as u64, + })); + let merge = |windows| { + node(Operator::ASAP(ASAPOp::SummaryMerge { + children: vec![windows], + group_by: None, + })) + }; + let now = finalized_sum(merge(range(HOUR_MS, chain.clone()))); + let shifted = finalized_sum(merge(range(HOUR_MS, shift(HOUR_MS, None, chain.clone())))); + let (mut dag, nodes) = + planner_types::test_support::forwarders::physical_asap_dag_at_query_time_with_node_ids(&[ + QueryRoot::Operator(now.clone()), + QueryRoot::Operator(shifted.clone()), + ]) + .unwrap(); + let chain_id = id_of(&nodes, &chain); + // Stage 3 sets the chain's retention: the shifted query reads 2 h back. + dag.nodes + .iter_mut() + .find(|node| node.id == chain_id) + .unwrap() + .retention_ms = Some(2 * HOUR_MS as u64); + ( + dag, + chain_id, + [id_of(&nodes, &now), id_of(&nodes, &shifted)], + ) +} + +// Ingestion runs build each 10-minute window of the chain into the store; +// at T = 4h the query merges the six windows of the last hour, and the +// shifted one the six of the hour before, each equal to the sum computed +// directly over the raw samples. Windows older than the retention are +// dropped, so a range reaching past it is refused. +#[test] +fn a_chain_built_at_ingestion_answers_queries_through_its_time_ranges() { + let (mut dag, chain, [now, shifted]) = chain_plan(); + let views = views_at(&mut dag, &[chain as u64]).unwrap(); + assert_eq!(views[0].retention_ms(), 2 * HOUR_MS as u64); + dag.materialized_views().unwrap(); + assert_eq!( + dag.refresh_window(chain), + planner_types::plan::materialized_view::RefreshWindow::of_window(0, WIDTH_MS) + ); + let refresh = compile(&dag, &[], &[chain as u64]).unwrap(); + let query = compile(&dag, &views, &[now as u64, shifted as u64]).unwrap(); + let mut store = Arc::new(store_with_samples()); + let mut window_end = WIDTH_MS; + while window_end <= 4 * HOUR_MS { + let context = RunContext::new( + Scope::Ingestion { + window_start_ms: window_end - WIDTH_MS, + window_end_ms: window_end, + }, + Limits::default(), + ) + .unwrap(); + let rows = { + let refresh = refresh.instantiate(store.clone()).unwrap(); + run(&refresh, &[chain as u64], context).remove(0) + }; + // One row per job, stamped with the window's end. + assert_eq!(rows.len(), 2); + assert!(rows + .iter() + .all(|row| matches!(row[0], Value::Timestamp(t) if t == window_end))); + let schema = Arc::new( + dag.nodes + .iter() + .find(|node| node.id == chain) + .unwrap() + .output_schema + .clone(), + ); + let batch = Batch::try_new(schema, rows).unwrap(); + Arc::get_mut(&mut store) + .unwrap() + .append_materialized_view(&views[0], (window_end - WIDTH_MS, window_end), vec![batch]) + .unwrap(); + window_end += WIDTH_MS; + } + let evaluation_time_ms = 4 * HOUR_MS; + let context = RunContext::new( + Scope::Query { evaluation_time_ms }, + Limits { + ..Limits::default() + }, + ) + .unwrap(); + let answers = { + let query = query.instantiate(store.clone()).unwrap(); + run(&query, &[now as u64, shifted as u64], context) + }; + assert_eq!( + sums_of(answers[0].clone()), + direct_sums(evaluation_time_ms - HOUR_MS, evaluation_time_ms) + ); + assert_eq!( + sums_of(answers[1].clone()), + direct_sums( + evaluation_time_ms - 2 * HOUR_MS, + evaluation_time_ms - HOUR_MS + ) + ); + // At T = 3h the shifted query reads (1h, 2h], whose windows ended more + // than 2 h before the newest one (4h) and are gone. + let context = RunContext::new( + Scope::Query { + evaluation_time_ms: 3 * HOUR_MS, + }, + Limits::default(), + ) + .unwrap(); + let query = query.instantiate(store).unwrap(); + let error = block_on(async { + let mut outputs = query.execute(&[shifted as u64], context).unwrap(); + let mut error = None; + while let Some(batch) = outputs[0].next().await { + if let Err(e) = batch { + error = Some(e); + } + } + error + }); + assert!(error.is_some_and(|error| error.to_string().contains("no rows tiling"))); +} + +// A query run never builds a chain: it is built one window per ingestion +// run. +#[test] +fn a_chain_is_built_only_at_ingestion() { + let (mut dag, chain, _) = chain_plan(); + // At query time the chain is not materialized, so it has no retention. + dag.nodes + .iter_mut() + .find(|node| node.id == chain) + .unwrap() + .retention_ms = None; + assert!(compile(&dag, &[], &[chain as u64]) + .is_err_and(|error| error.to_string().contains("built in ingestion runs"))); +} + +// `sum_over_time(latency[5m] @ end())` in a range request over (1h, 3h]: +// every step's run evaluates the sum over the request's last five minutes, +// `(3h - 5m, 3h]`; an instant query (no request bounds) over its own last +// five minutes. +#[test] +fn a_time_shift_at_end_evaluates_its_input_at_the_requests_end() { + let per_job = node(Operator::ASAP(ASAPOp::SummaryAgg { + child: range(5 * MINUTE_MS, scan()), + summary_type: sum(), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::by(vec![0]), + filter: None, + })); + let anchored = shift(0, Some(AtModifier::End), finalized_sum(per_job)); + let (dag, nodes) = + planner_types::test_support::forwarders::physical_asap_dag_at_query_time_with_node_ids(&[ + QueryRoot::Operator(anchored.clone()), + ]) + .unwrap(); + let root = id_of(&nodes, &anchored) as u64; + let query = compile(&dag, &[], &[root]).unwrap(); + assert_eq!(query.operator_name(root), Some("TimeShift")); + let query = query.instantiate(Arc::new(store_with_samples())).unwrap(); + let (start, end) = (HOUR_MS, 3 * HOUR_MS); + for step in [start, 2 * HOUR_MS, end] { + let context = RunContext::new( + Scope::Query { + evaluation_time_ms: step, + }, + Limits::default(), + ) + .unwrap() + .with_query_range(start, end) + .unwrap(); + assert_eq!( + sums_of(run(&query, &[root], context).remove(0)), + direct_sums(end - 5 * MINUTE_MS, end), + "step {step}" + ); + } + let instant = RunContext::new( + Scope::Query { + evaluation_time_ms: 2 * HOUR_MS, + }, + Limits::default(), + ) + .unwrap(); + assert_eq!( + sums_of(run(&query, &[root], instant).remove(0)), + direct_sums(2 * HOUR_MS - 5 * MINUTE_MS, 2 * HOUR_MS) + ); +} diff --git a/crates/executor/tests/univmon_execution.rs b/crates/executor/tests/univmon_execution.rs index 2bfdc4135..c6fa4aff3 100644 --- a/crates/executor/tests/univmon_execution.rs +++ b/crates/executor/tests/univmon_execution.rs @@ -21,6 +21,16 @@ use planner_types::summary_type::{ }; use std::sync::Arc; +/// θ of `family`. +fn params() -> Option { + Some(planner_types::summary_type::SketchParams::UnivMon { + heap_size: 64, + sketch_rows: 5, + sketch_cols: 128, + layers: 4, + }) +} + fn family() -> FieldDataType { FieldDataType::Sketch(SketchAlgorithm::UnivMon) } @@ -92,7 +102,7 @@ fn one_univmon_state_answers_distinct_l2_and_entropy() -> Result<(), Error> { .map(|value| vec![Value::Float64(value)]) .collect(), )?; - let build = Operator::per_group_summary_build(input, family(), 0, None, vec![])?; + let build = Operator::per_group_summary_build(input, family(), params(), 0, None, vec![])?; let [distinct, l2, entropy] = three_statistics(batch, build)?[..] else { unreachable!() }; @@ -135,7 +145,7 @@ fn typed_frequency_keys_preserve_identity() -> Result<(), Error> { vec![Value::Null], ], )?; - let build = Operator::per_group_summary_build(input, family(), 0, None, vec![])?; + let build = Operator::per_group_summary_build(input, family(), params(), 0, None, vec![])?; for (found, expected) in three_statistics(batch, build)? .into_iter() @@ -175,7 +185,7 @@ fn planner_unit_frequency_update_binds_as_a_value_frequency_build() -> Result<() }), output_state: ExecutionDataState::QUERY_ROWS, lifetime: Default::default(), - parameters: None, + parameters: params(), output_schema: (*state).clone(), retention_ms: None, selected_physical_candidate: None, diff --git a/crates/integration-tests/src/lib.rs b/crates/integration-tests/src/lib.rs index 4deb6e593..813deaa4e 100644 --- a/crates/integration-tests/src/lib.rs +++ b/crates/integration-tests/src/lib.rs @@ -153,6 +153,23 @@ pub mod planning { } } + /// The workload entry of the single query these tests plan: `accuracy`, + /// everything else the default. + pub fn single_query_entry( + accuracy: AccuracyTarget, + ) -> asap_types::workload::QueryWorkloadEntry { + asap_types::workload::QueryWorkloadEntry { + query: Default::default(), + requirements: asap_types::workload::QueryRequirements { + accuracy, + max_latency_ms: None, + }, + predictability: Default::default(), + recurrence: Default::default(), + range_evaluation: None, + } + } + /// Every planning stage's output for the single query `root`. pub fn run_all_stages( root: Rc, @@ -170,16 +187,7 @@ pub mod planning { let data = declared_data(&root); asap_devtools::planning_stages::run_planning_stages_over( vec![QueryRoot::Operator(root)], - &[asap_types::workload::QueryWorkloadEntry { - query: Default::default(), - requirements: asap_types::workload::QueryRequirements { - accuracy, - max_latency_ms: None, - }, - predictability: Default::default(), - recurrence: Default::default(), - range_evaluation: None, - }], + &[single_query_entry(accuracy)], &data, models, ) @@ -203,22 +211,7 @@ pub mod planning { data: &DataWorkload, settings: &asap_cost::CostInputs, ) -> f64 { - let recurrence: Vec<_> = entries - .iter() - .map(|entry| entry.recurrence.clone()) - .collect(); - let range_evaluations: Vec<_> = - entries.iter().map(|entry| entry.range_evaluation).collect(); - let (cost, _) = asap_cost::plan_cost( - plan, - &asap_cost::node_map(plan), - &recurrence, - &range_evaluations, - data, - settings, - 1.0, - ) - .unwrap(); + let (cost, _) = asap_cost::plan_cost(plan, entries, data, settings).unwrap(); cost.cost_per_second.expect("cost model 1 or 2") } diff --git a/crates/integration-tests/tests/filtered_aggregates.rs b/crates/integration-tests/tests/filtered_aggregates.rs index 970abf26a..eaa96aa4f 100644 --- a/crates/integration-tests/tests/filtered_aggregates.rs +++ b/crates/integration-tests/tests/filtered_aggregates.rs @@ -57,7 +57,9 @@ fn rows() -> Vec> { /// Run the logical DAG `root` over [`rows`]; rows come back shown and sorted. fn execute(root: &Rc) -> Vec { - let dag = asap_types::test_support::forwarders::physical_asap_dag_at_query_time(root).unwrap(); + let mut dag = + asap_types::test_support::forwarders::physical_asap_dag_at_query_time(root).unwrap(); + asap_executor::physical_planner::test_support::set_build_parameters(&mut dag.nodes, None); let scan = dag .nodes .iter() diff --git a/crates/integration-tests/tests/joint_search_equivalence.rs b/crates/integration-tests/tests/joint_search_equivalence.rs index b2ef58aab..d7009d822 100644 --- a/crates/integration-tests/tests/joint_search_equivalence.rs +++ b/crates/integration-tests/tests/joint_search_equivalence.rs @@ -29,6 +29,8 @@ fn workload_of(queries: &[(&str, u32)], accuracy: AccuracyTarget) -> PlanningWor .iter() .map(|&(query, interval)| RepeatingEntry { query: Query(query.into()), + // Evaluated at multiples of the interval: a known grid, + // which a chain delivers on. demand: RepeatedDemand::FixedIntervalAt { interval: RepetitionInterval(interval), evaluation_phase: TimestampMs(0), diff --git a/crates/integration-tests/tests/kll_tumbling_window_execution.rs b/crates/integration-tests/tests/kll_tumbling_window_execution.rs index c49abdef5..714b69c8c 100644 --- a/crates/integration-tests/tests/kll_tumbling_window_execution.rs +++ b/crates/integration-tests/tests/kll_tumbling_window_execution.rs @@ -17,6 +17,7 @@ use asap_types::ir::schema::{DataType, Field, FieldDataType, Schema}; use asap_types::ir::{ASAPOp, NonASAPOp, OperatorNode, QueryRoot, TimeRangeKind}; use asap_types::plan::materialized_view::MaterializedView; use asap_types::plan::physical_asap_dag::PhysicalASAPDAG; +use asap_types::plan::physical_asap_dag::ViewCoverage; use asap_types::summary_type::{SketchAlgorithm, SketchStatistic, SummaryUpdate}; use futures::{executor::block_on, StreamExt}; use std::{ @@ -29,7 +30,7 @@ use std::{ const TUMBLING_WINDOW_WIDTH_MS: i64 = 60_000; -fn family(_k: u32) -> FieldDataType { +fn kll() -> FieldDataType { FieldDataType::Sketch(SketchAlgorithm::Kll) } /// `t(ts, value)`, time index `ts`. @@ -85,7 +86,7 @@ fn fixture() -> Fixture { })); new(asap_types::ir::Operator::ASAP(ASAPOp::SummaryAgg { child: range, - summary_type: family(200), + summary_type: kll(), input: SummaryUpdate::column(ColumnRef::Named("value".into())), reduction: Reduction::Reduce(GroupKeys::default()), filter: None, @@ -106,9 +107,13 @@ fn fixture() -> Fixture { QueryRoot::Operator(estimate(0.5)), QueryRoot::Operator(estimate(0.99)), ]; - let (dag, operator_node_of_id) = + let (mut dag, operator_node_of_id) = asap_types::test_support::forwarders::physical_asap_dag_at_query_time_with_node_ids(&roots) .unwrap(); + asap_executor::physical_planner::test_support::set_build_parameters( + &mut dag.nodes, + Some(&asap_types::summary_type::SketchParams::Kll { k: 200 }), + ); let mut tumbling_window_ids = vec![0; 5]; let (mut merge, mut p50, mut p99) = (0, 0, 0); for node in &dag.nodes { @@ -132,7 +137,14 @@ fn fixture() -> Fixture { _ => {} } } - let mut dag = dag; + // The five tumbling windows compute one thing: one definition. + for node in dag + .nodes + .iter_mut() + .filter(|node| tumbling_window_ids.contains(&(node.id as u64))) + { + node.view_coverage = Some(ViewCoverage::new("kll of t.value".into(), vec![])); + } let views = views_at(&mut dag, &tumbling_window_ids).unwrap(); Fixture { dag, @@ -222,7 +234,7 @@ fn restore(schema: SchemaRef, states: &[Arc]) -> Batch { .iter() .map(|state| { vec![Value::Summary { - family: family(200), + family: kll(), state: state.clone(), }] }) @@ -300,27 +312,16 @@ fn five_tumbling_windows_roundtrip_and_shared_merge_runs_once() { let evaluation_time_ms = (5 + offset as i64) * 60_000; // View `i` holds the tumbling window ending `i` minutes before the evaluation time. let bind = |count: usize| { - fixture.tumbling_windows[..count].iter().enumerate().fold( - InMemoryDataStore::default(), - |store, (i, &view)| { - store - .with_materialized_view( - view as usize, - vec![tumbling_windows[offset + 4 - i].clone()], - ) - .unwrap() - }, - ) + store_at(&fixture.views[..count], evaluation_time_ms, |i| { + tumbling_windows[offset + 4 - i].clone() + }) }; let scope = Scope::Query { evaluation_time_ms }; // A five-tumbling-window deployment cannot bind only four materialized views. let missing = try_execute(&compiled, bind(4), scope.clone()) .err() .unwrap(); - assert!( - missing.to_string().contains("unbound materialized view"), - "{missing}" - ); + assert!(missing.to_string().contains("no rows tiling"), "{missing}"); let result = physical_common::execute(&compiled, bind(5), scope); let kll_state = summary(&result[0][0]); let kll = kll_state @@ -376,37 +377,37 @@ fn five_tumbling_windows_roundtrip_and_shared_merge_runs_once() { } } -/// Relabelled KLL parameters, a view bound in another schema and a missing -/// view fail explicitly. +/// A store holding, for each of `views`, its state `state(i)` for the +/// window view `i` reads at `evaluation_time_ms`: the minute ending `i` +/// minutes before it. +fn store_at( + views: &[MaterializedView], + evaluation_time_ms: i64, + state: impl Fn(usize) -> Batch, +) -> InMemoryDataStore { + let mut store = InMemoryDataStore::default(); + for (i, view) in views.iter().enumerate() { + let end = evaluation_time_ms - i as i64 * TUMBLING_WINDOW_WIDTH_MS; + store + .append_materialized_view(view, (end - TUMBLING_WINDOW_WIDTH_MS, end), vec![state(i)]) + .unwrap(); + } + store +} + +/// A view bound in another schema and a missing view fail explicitly. #[test] fn tumbling_windows_reject_parameters_schema_and_missing_binding() { let fixture = fixture(); - let schema = schema_of(&fixture.dag, fixture.tumbling_windows[0]); - let wrong = Value::Summary { - family: family(200), - state: Arc::new(DatasketchesKLLAccumulator::new(128)), - }; - assert!(Batch::try_new(schema, vec![vec![wrong]]).is_err()); let compiled = compile(&fixture.dag, &fixture.views, &[fixture.merge]).unwrap(); let missing = try_execute(&compiled, InMemoryDataStore::default(), query_scope()) .err() .unwrap(); - assert!( - missing.to_string().contains("unbound materialized view"), - "{missing}" - ); + assert!(missing.to_string().contains("no rows tiling"), "{missing}"); let raw = Arc::new(table_schema()); let raw_rows = Batch::try_new(raw, vec![vec![Value::Timestamp(1), Value::Float64(1.)]]).unwrap(); - let store = - fixture - .tumbling_windows - .iter() - .fold(InMemoryDataStore::default(), |store, &view| { - store - .with_materialized_view(view as usize, vec![raw_rows.clone()]) - .unwrap() - }); + let store = store_at(&fixture.views, 300_000, |_| raw_rows.clone()); let relabelled = try_execute(&compiled, store, query_scope()).err().unwrap(); assert!( relabelled.to_string().contains("another schema"), @@ -522,14 +523,7 @@ fn a_store_keyed_by_definition_and_time_range_answers_the_tumbling_windows() { .collect::>() ); // View `i` holds the tumbling window ending `i` minutes before the evaluation time. - let by_node = fixture.tumbling_windows.iter().enumerate().fold( - InMemoryDataStore::default(), - |store, (i, &view)| { - store - .with_materialized_view(view as usize, vec![tumbling_windows[4 - i].clone()]) - .unwrap() - }, - ); + let by_node = store_at(&fixture.views, 300_000, |i| tumbling_windows[4 - i].clone()); let by_node: Vec>> = physical_common::execute(&compiled, by_node, scope) .iter() .map(|batches| batches.iter().flat_map(Batch::to_rows).collect()) diff --git a/crates/planner/src/installable.rs b/crates/planner/src/installable.rs index 5cd327d7e..974680d4e 100644 --- a/crates/planner/src/installable.rs +++ b/crates/planner/src/installable.rs @@ -18,10 +18,10 @@ //! data (a time series is a table `(ts, value, *labels)` over the samples it //! buffers; a SQL table is read as is) and its stored views, and the //! DAG's own `TimeRange`s apply each window, an instant selector's lookback -//! and Prometheus stale markers. How the Planner represents the plan -//! internally (e.g. a tumbling-window chain, which the stage export keeps for -//! costing and display) is not the runtime's concern: the installable DAG -//! has every chain expanded into the tumbling windows the executor runs. +//! and Prometheus stale markers. A tumbling-window chain runs as it is: each +//! ingestion run builds its newest window, the runtime appends it to its +//! store, and the `TimeRange`s above the chain select the windows a query +//! merges. use std::collections::{HashMap, HashSet}; @@ -54,9 +54,8 @@ pub struct InstallablePlan { /// [`INSTALLABLE_PLAN_WIRE_VERSION`] of the planner that wrote it. pub wire_version: u32, /// The selected physical ASAP DAG with Stage 3's timings and - /// materialization, every tumbling-window chain expanded into its tumbling windows, each - /// materialized view and query-time `Scan` carrying its `retention_ms`; - /// the executor compiles it as is. + /// materialization, each materialized view and query-time `Scan` + /// carrying its `retention_ms`; the executor compiles it as is. pub dag: PhysicalASAPDAG, /// One per workload query, in `QueryWorkload::entries()` order, as the /// workload gave it: query `i` is answered by `dag.roots[i]`. @@ -100,20 +99,12 @@ pub struct PredictedUsage { pub per_query: Vec, } -/// One operator node's predicted usage. The node is one of the physical -/// DAG Stage 3 priced, where a tumbling-window chain is one node; `dag` has -/// it expanded into its tumbling windows, so `installed_node_ids` says which of -/// `dag`'s nodes run it. +/// One operator node's predicted usage. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct PredictedNodeUsage { - /// Its id in Stage 3's physical DAG of the selected candidate. - pub stage3_node_id: PhysicalASAPNodeId, - /// The ids in `dag` that run it: a tumbling-window chain's tumbling windows, newest - /// first; for every other node, the one node it is installed as. Empty - /// for a node `dag` has no copy of: one the expansion rebuilds once per - /// tumbling window below a chain (e.g. the chain's input window). - pub installed_node_ids: Vec, + /// Its id in the installed DAG. + pub node_id: PhysicalASAPNodeId, /// Its operator, as the measured cost table names it. pub operator: OperatorKind, /// When it runs. @@ -179,6 +170,13 @@ pub enum InstallablePlanError { /// (`reason`). #[error("the executor cannot compile the plan's query run: {reason}")] QueryRunNotExecutable { reason: String }, + /// Internal bug: the cost of the selected plan cannot be computed at + /// node `node_id` (`reason`). + #[error("the selected plan's cost at node {node_id}: {reason}")] + PlanCostFailed { + node_id: PhysicalASAPNodeId, + reason: String, + }, } impl From for InstallablePlanError { @@ -213,9 +211,18 @@ pub async fn plan_installable( models, pass: input.pass, }; + let data = input.workload.data_workload.clone().unwrap_or_default(); + let cost_inputs = input.models.cost_inputs; let output = e2e_plan(input).await?; - let _ = (output, entries); - todo!("#1207 PR 6: install the plan Stage 3 built, with its plan_cost report") + let (cost, _) = asap_cost::plan_cost(&output.plan, &entries, &data, cost_inputs).map_err( + |(node_id, error)| { + PlanError::Installable(InstallablePlanError::PlanCostFailed { + node_id, + reason: error.to_string(), + }) + }, + )?; + installable_plan_of(&output.plan, Some(&cost), &entries).map_err(PlanError::Installable) } /// The installable plan of `plan`, the plan Stage 3 built for a workload @@ -229,23 +236,9 @@ pub fn installable_plan_of( cost: Option<&PlanCost>, entries: &[QueryWorkloadEntry], ) -> Result<(InstallablePlan, Option), InstallablePlanError> { - // Each query's longest range-evaluation span, which the views it - // reads must stay available for. - let range_spans_ms: Vec = entries - .iter() - .map(|entry| entry.range_evaluation.map_or(0, |range| range.span_ms)) - .collect(); let query_texts: Vec = entries.iter().map(|entry| entry.query.0.clone()).collect(); - let expanded = tumbling_window_retention_of_expanded_tumbling_windows(plan)?; - let report = cost.map(|cost| report_of(cost, &expanded.installed_node_ids)); - let reader_span_ms_by_node = reader_span_ms_by_node(&expanded.dag, &range_spans_ms); - let plan = InstallablePlan::of_dag( - expanded.dag, - query_texts, - &expanded.retention_ms_by_node, - &reader_span_ms_by_node, - )?; - Ok((plan, report)) + let installable = InstallablePlan::of_dag(plan.clone(), query_texts)?; + Ok((installable, cost.map(report_of))) } impl InstallablePlan { @@ -308,17 +301,16 @@ impl InstallablePlan { } /// The plan of `dag`, with each view's and query-time scan's retention - /// set ([`PhysicalASAPDAG::set_retention`]), each view's refresh and the - /// query run compiled ([`executor_compiles`]): a plan the executor - /// cannot run is refused here, not installed. + /// (which Stage 3 set) checked ([`PhysicalASAPDAG::materialized_views`], + /// [`PhysicalASAPDAG::raw_data_retention`]), and each view's refresh + /// and the query run compiled ([`executor_compiles`]): a plan the + /// executor cannot run is refused here, not installed. pub(crate) fn of_dag( - mut dag: PhysicalASAPDAG, + dag: PhysicalASAPDAG, query_texts: Vec, - tumbling_window_retention_ms_by_node: &HashMap, - reader_span_ms_by_node: &HashMap, ) -> Result { - let views = - dag.set_retention(tumbling_window_retention_ms_by_node, reader_span_ms_by_node)?; + let views = dag.materialized_views()?; + dag.raw_data_retention()?; check_queries(&dag, &query_texts)?; executor_compiles(&dag, &views)?; let plan = InstallablePlan { @@ -441,36 +433,6 @@ impl<'a> IndexedDag<'a> { } } -/// For every node of `dag`, the longest range-evaluation span among the -/// queries that read it (`range_spans_ms[i]` for root `i`); a node no -/// range-evaluated query reads is absent. One walk per root: -/// O(roots · (nodes + edges)). -fn reader_span_ms_by_node( - dag: &PhysicalASAPDAG, - range_spans_ms: &[u64], -) -> HashMap { - let mut inputs: HashMap> = HashMap::new(); - for edge in &dag.edges { - inputs.entry(edge.consumer).or_default().push(edge.producer); - } - let mut spans = HashMap::new(); - for (&root, &span_ms) in dag.roots.iter().zip(range_spans_ms) { - if span_ms == 0 { - continue; - } - let mut seen = HashSet::new(); - let mut pending = vec![root]; - while let Some(id) = pending.pop() { - if seen.insert(id) { - let span = spans.entry(id).or_insert(0); - *span = (*span).max(span_ms); - pending.extend(inputs.get(&id).into_iter().flatten().copied()); - } - } - } - spans -} - /// Whether the executor compiles `dag` as a runtime runs it: each refresh /// ([`executor_refreshes`]): one per refresh group of the views refreshed /// at ingestion ([`PhysicalASAPDAG::ingestion_refresh_groups_of`] `views`) @@ -585,31 +547,8 @@ fn executor_refreshes( ) } -/// The installable DAG of the timed selected plan, with each expanded -/// tumbling window's retention by node id. -struct ExpandedWithRetention { - dag: PhysicalASAPDAG, - retention_ms_by_node: HashMap, - /// Each node id of the selected candidate's physical DAG (its roots - /// exported, Stage 3's ids) with the ids in `dag` that run it - /// ([`PredictedNodeUsage::installed_node_ids`]). - installed_node_ids: HashMap>, -} - -/// The plan's DAG with its tumbling-window chains expanded; each tumbling -/// window takes its chain's retention (`retention_ms`, Stage 3's $h_v$). -fn tumbling_window_retention_of_expanded_tumbling_windows( - plan: &PhysicalASAPDAG, -) -> Result { - let _ = plan; - todo!("#1207 PR 6: expand the plan's tumbling-window chains") -} - /// The report of the selected plan's `cost`. -fn report_of( - cost: &PlanCost, - installed_node_ids: &HashMap>, -) -> SelectedPlanReport { +fn report_of(cost: &PlanCost) -> SelectedPlanReport { SelectedPlanReport { cost_per_second: cost.cost_per_second, predicted_usage: PredictedUsage { @@ -627,8 +566,7 @@ fn report_of( .per_node .iter() .map(|(&id, node)| PredictedNodeUsage { - stage3_node_id: id, - installed_node_ids: installed_node_ids.get(&id).cloned().unwrap_or_default(), + node_id: id, operator: node.operator, timing: node.timing, cpu_core_seconds_per_second: node.cpu_core_seconds_per_second, @@ -653,6 +591,7 @@ fn report_of( mod tests { use super::*; use asap_types::summary_type::{SketchAlgorithm, StatisticKind, SupportedSummary}; + use std::rc::Rc; thread_local! { /// The plan nodes handed to the executor's compiler by the views' @@ -661,15 +600,13 @@ mod tests { pub(super) static COMPILED_NODES: std::cell::Cell = const { std::cell::Cell::new(0) }; } - /// A p99 panel over a 300 s window every second, with no raw data retained: - /// one DDSketch per series in each of 300 one-second tumbling windows built at - /// ingestion, about 300 materialized views in a DAG of about 900 nodes. - /// Each refresh (here one refresh group of all the tumbling windows) is compiled + /// A p99 panel over a 300 s window every second, with no raw data + /// retained: one DDSketch per series in a chain of one-second windows + /// built at ingestion. Each refresh (here one refresh group) is compiled /// over the nodes below its views, and the whole plan's checks run once, /// so the nodes handed to the executor grow with the plan, not with the - /// plan times its views (before, every refresh compiled the whole - /// plan). The count - /// covers both `plan_installable`'s build and `check_compiles`. + /// plan times its views. The count covers both an installation and + /// `check_compiles`. #[tokio::test] async fn view_refreshes_compile_only_the_nodes_below_them() { let workload: crate::PlanningWorkload = serde_json::from_str( @@ -698,17 +635,10 @@ mod tests { .expect("a plan"); let nodes = plan.dag.nodes.len(); let views = plan.materialized_views().unwrap().len(); - assert!(views >= 300, "{views} materialized views"); // Measure one installation and one `check_compiles`, independently of // how many candidates the optimizer explored before selecting this DAG. COMPILED_NODES.with(|count| count.set(0)); - let rebuilt = InstallablePlan::of_dag( - plan.dag.clone(), - plan.query_texts.clone(), - &HashMap::new(), - &HashMap::new(), - ) - .unwrap(); + let rebuilt = InstallablePlan::of_dag(plan.dag.clone(), plan.query_texts.clone()).unwrap(); assert_eq!(rebuilt.materialized_views().unwrap().len(), views); plan.check_compiles().unwrap(); let compiled = COMPILED_NODES.with(std::cell::Cell::get); @@ -718,6 +648,180 @@ mod tests { ); } + /// The plan of the `sum_over_time(latency[1h])` panel as Stage 3 builds + /// it: a chain of 10-minute windows of exact sums per `job` over + /// `latency`, built at ingestion and retained for `chain_retention_ms`, + /// read through `TimeRange(1h)` by a merge and its finalization at query + /// time; and the chain's node id. + fn chain_plan(chain_retention_ms: Option) -> (PhysicalASAPDAG, PhysicalASAPNodeId) { + use asap_types::ir::operator::{Reduction, Source}; + use asap_types::ir::scalar::ColumnRef; + use asap_types::ir::schema::{DataType, Field, FieldDataType, Schema}; + use asap_types::ir::{ASAPOp, NonASAPOp, Operator, OperatorNode, QueryRoot, TimeRangeKind}; + use asap_types::plan::physical_asap_dag::ViewCoverage; + use asap_types::stages::{Lifetime, StorageMedium}; + use asap_types::summary_type::{ExactKind, ExactParams, SummaryUpdate}; + let node = |operator| OperatorNode::new_shared(operator).unwrap(); + let scan = node(Operator::NonASAP(NonASAPOp::Scan { + source: Source::TimeSeries { + metric: "latency".into(), + }, + predicates: vec![], + schema: Schema::with_time_index( + vec![ + Field::plain("job", DataType::Utf8, false), + Field::plain("ts", DataType::Timestamp, false), + Field::plain("value", DataType::Float64, false), + ], + 1, + vec![], + ), + })); + let chain = node(Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { + child: scan, + summary_type: FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::by(vec![0]), + filter: None, + tumbling_window_width_ms: 600_000, + })); + let windows = node(Operator::NonASAP(NonASAPOp::TimeRange { + range: std::time::Duration::from_secs(3_600), + kind: TimeRangeKind::Range, + child: chain.clone(), + })); + let merged = node(Operator::ASAP(ASAPOp::SummaryMerge { + children: vec![windows], + group_by: None, + })); + let root = node(Operator::ASAP(ASAPOp::FinalizeExactAccumulator { + child: merged, + })); + let (mut dag, nodes) = + asap_types::test_support::forwarders::physical_asap_dag_at_query_time_with_node_ids(&[ + QueryRoot::Operator(root), + ]) + .unwrap(); + let chain_id = nodes.iter().position(|n| Rc::ptr_eq(n, &chain)).unwrap(); + let scan_id = producer_of(&dag, chain_id); + for node in &mut dag.nodes { + if node.id == chain_id { + node.lifetime = Lifetime::AcrossEvaluations(StorageMedium::Memory); + node.retention_ms = chain_retention_ms; + node.view_coverage = + Some(ViewCoverage::new("sum of latency by job".into(), vec![])); + } + if node.id == chain_id || node.id == scan_id { + node.output_state.timing = ExecutionTiming::IngestionTime; + } + } + for edge in &mut dag.edges { + if edge.producer == chain_id || edge.producer == scan_id { + edge.data_state_mut().timing = ExecutionTiming::IngestionTime; + } + } + (dag, chain_id) + } + + /// One exact query's entry. + fn entry() -> QueryWorkloadEntry { + QueryWorkloadEntry { + query: Default::default(), + requirements: asap_types::workload::QueryRequirements { + accuracy: asap_types::accuracy::AccuracyTarget::exact(), + max_latency_ms: None, + }, + predictability: Default::default(), + recurrence: Default::default(), + range_evaluation: None, + } + } + + /// The input of `consumer`, which reads one. + fn producer_of(dag: &PhysicalASAPDAG, consumer: PhysicalASAPNodeId) -> PhysicalASAPNodeId { + dag.edges + .iter() + .find(|edge| edge.consumer == consumer) + .unwrap() + .producer + } + + /// The installable plan of a DAG with a chain is the DAG as Stage 3 + /// built it: the chain is one materialized view, refreshed at ingestion + /// one 10-minute window per run, and no raw data is kept for queries. + #[test] + fn a_chain_is_installed_as_one_view() { + let (dag, chain) = chain_plan(Some(3_600_000)); + let (plan, report) = installable_plan_of(&dag, None, &[entry()]).unwrap(); + assert_eq!(plan.dag, dag); + assert!(report.is_none()); + let views = plan.materialized_views().unwrap(); + assert_eq!(views.len(), 1); + assert_eq!(views[0].node_id, chain); + assert_eq!(views[0].retention_ms(), 3_600_000); + let groups = plan.ingestion_refresh_groups().unwrap(); + assert_eq!( + groups[0].refresh_window, + Some(asap_types::plan::materialized_view::RefreshWindow { + width_ms: 600_000, + phase_ms: 0 + }) + ); + assert!(plan.raw_data_retention().unwrap().is_empty()); + plan.check_compiles().unwrap(); + } + + /// Stage 3 sets each view's retention; installation checks it: a view + /// without one, or retained for less than the hour its query reads back, + /// is refused. + #[test] + fn installation_checks_the_retention_stage3_set() { + let entries = [entry()]; + assert!(matches!( + installable_plan_of(&chain_plan(None).0, None, &entries), + Err(InstallablePlanError::Uninstallable( + InstallableDagError::ViewWithoutRetention { .. } + )) + )); + assert!(matches!( + installable_plan_of(&chain_plan(Some(3_000_000)).0, None, &entries), + Err(InstallablePlanError::Uninstallable( + InstallableDagError::ViewRetentionTooShort { .. } + )) + )); + } + + /// A query-time `Scan` read through a window carries its retention; + /// one without is refused, and one with it is the raw data kept for its + /// source. + #[test] + fn a_query_time_scan_carries_its_retention() { + let (mut dag, chain) = chain_plan(Some(3_600_000)); + let scan = producer_of(&dag, chain); + for node in &mut dag.nodes { + if node.id == chain || node.id == scan { + node.output_state.timing = ExecutionTiming::QueryTime; + } + } + for edge in &mut dag.edges { + if edge.producer == chain || edge.producer == scan { + edge.data_state_mut().timing = ExecutionTiming::QueryTime; + } + } + assert!(matches!( + dag.raw_data_retention(), + Err(InstallableDagError::ScanWithoutRetention { .. }) + )); + dag.nodes + .iter_mut() + .find(|n| n.id == scan) + .unwrap() + .retention_ms = Some(600_000); + let retention = dag.raw_data_retention().unwrap(); + assert_eq!(retention.len(), 1); + assert_eq!(retention[0].lookback_ms(), Some(600_000)); + } + /// Unrestricted summaries become the executor's families; a caller's /// own list and its retention flags are kept. #[test] diff --git a/crates/planner/tests/runtime_install.rs b/crates/planner/tests/runtime_install.rs index db35d35a6..2fdff66df 100644 --- a/crates/planner/tests/runtime_install.rs +++ b/crates/planner/tests/runtime_install.rs @@ -96,36 +96,19 @@ impl DataStore for CountingStore { } } -/// Compile `ids` of `dag` over the stored states `views` (by node id) and -/// run them in `scope` over the raw `samples`, as a runtime does with the -/// executor: each id's output batches. +/// Compile `ids` of `dag` and run them in `scope` over the raw `samples`, +/// as a runtime does with the executor: each id's output batches. fn run( dag: &PhysicalASAPDAG, - views: BTreeMap, samples: &[RawDataSample], ids: &[u64], scope: Scope, ) -> Vec> { - // The materialized views the run reads; each one's schema is its DAG node's. - let read: Vec = if views.is_empty() { - Vec::new() - } else { - dag.materialized_views() - .unwrap() - .into_iter() - .filter(|view| views.contains_key(&(view.node_id as u64))) - .collect() - }; - assert_eq!(read.len(), views.len(), "every bound state is a view's"); - // The executor's plan for `ids`, reading the views; every `Scan` it - // reaches is a raw data slot. - let compiled = compile(dag, &read, ids).expect("the executor compiles the plan"); - // The plan's data in one store: raw samples at the `Scan`s by metric, - // and each view's states by node id. - let mut store = time_series_tables(dag, samples); - for (id, batch) in views { - store = store.with_materialized_view(id as _, vec![batch]).unwrap(); - } + // The executor's plan for `ids`; every `Scan` it reaches is a raw data + // slot. + let compiled = compile(dag, &[], ids).expect("the executor compiles the plan"); + // The plan's raw samples at the `Scan`s by metric. + let store = time_series_tables(dag, samples); let instance = compiled .instantiate(Arc::new(CountingStore(store))) .unwrap(); @@ -249,33 +232,21 @@ async fn the_plan_carries_stage3s_predicted_usage() { && node.timing == asap_planner::ExecutionTiming::IngestionTime)); assert_eq!(usage.per_query.len(), 1); assert!(usage.peak_memory_bytes > 0); - // Each prediction names the installed nodes that run it: the tumbling window - // chain its selected tumbling windows, the query's root its own node. + // Each prediction names its node of the installed DAG: the chain is + // one materialized view, and the query's root has its own. let installed: BTreeSet<_> = plan.dag.nodes.iter().map(|node| node.id).collect(); - for node in &usage.per_node { - assert!( - node.installed_node_ids - .iter() - .all(|id| installed.contains(id)), - "{node:?}" - ); - } + assert!(usage + .per_node + .iter() + .all(|node| installed.contains(&node.node_id))); let chain = usage .per_node .iter() .find(|node| node.operator == OperatorKind::SummaryTumblingWindowChain) .unwrap(); - assert_eq!(chain.installed_node_ids.len(), views.len(), "{chain:?}"); - let view_ids: BTreeSet<_> = views.iter().map(|v| v.node_id).collect(); - assert!(chain - .installed_node_ids - .iter() - .all(|id| view_ids.contains(id))); + assert!(views.iter().any(|view| view.node_id == chain.node_id)); let root = plan.dag.roots[0]; - assert!(usage - .per_node - .iter() - .any(|node| node.installed_node_ids == [root])); + assert!(usage.per_node.iter().any(|node| node.node_id == root)); } /// `sum(latency_ms)` reads an instant selector: per series, only the latest @@ -309,7 +280,7 @@ async fn an_instant_selector_reads_the_latest_sample_per_series() { let query = Scope::Query { evaluation_time_ms: EVALUATION_MS, }; - let answered = answers(&run(dag, BTreeMap::new(), &samples, &query_roots(&plan), query)[0]); + let answered = answers(&run(dag, &samples, &query_roots(&plan), query)[0]); assert_eq!(answered.len(), 1); assert_eq!(answered[0].1, 110.0); } diff --git a/crates/planner/tests/sql_table_scans.rs b/crates/planner/tests/sql_table_scans.rs index 0821d5ab5..c078dbe2f 100644 --- a/crates/planner/tests/sql_table_scans.rs +++ b/crates/planner/tests/sql_table_scans.rs @@ -21,7 +21,7 @@ use asap_planner::{ use asap_types::ir::operator::Source; use asap_types::ir::schema::{DataType, Field, Schema}; -use asap_types::ir::NonASAPOp; +use asap_types::ir::{ASAPOp, NonASAPOp}; use asap_types::plan::physical_asap_dag::PhysicalASAPOperatorPayload; use futures::{executor::block_on, StreamExt}; @@ -51,7 +51,7 @@ fn workload(queries: &[&str], accuracy: &str, interval_ms: u64) -> PlanningWorkl .iter() .map(|query| { format!( - r#"{{"query": {query:?}, "demand": {{"fixed_interval_at": {{"interval": {interval_ms}, "evaluation_phase": 0}}}}}}, + r#"{{"query": {query:?}, "demand": {{"fixed_interval_at": {{"interval": {interval_ms}, "evaluation_phase": 0}}}}, "requirements": {{"accuracy": {accuracy}}}, "predictability": {{"predictable": {{"known_at": null}}}}}}"# ) @@ -163,12 +163,15 @@ fn expected_distinct_users() -> BTreeMap { .collect() } -/// Runs `ids` of `dag` in `scope` with each of `inputs` bound to the node -/// its key names and `table` bound as `http`: the batches of each id's -/// output. +/// Runs `ids` of `dag` in `scope` with each of `inputs` (a state and the +/// span of data it summarizes) stored for the node its key names and +/// `table` bound as `http`: the batches of each id's output. +/// One stored state of a view and the `(start, end]` of the data it holds. +type WindowState = (Batch, (i64, i64)); + fn run_bound( dag: &asap_types::plan::physical_asap_dag::PhysicalASAPDAG, - inputs: BTreeMap, + inputs: BTreeMap>, table: &Batch, ids: &[u64], scope: Scope, @@ -194,8 +197,12 @@ fn run_bound( vec![table.clone()], ) .unwrap(); - for (id, batch) in inputs { - store = store.with_materialized_view(id as _, vec![batch]).unwrap(); + for view in &views { + for (batch, span) in &inputs[&(view.node_id as u64)] { + store + .append_materialized_view(view, *span, vec![batch.clone()]) + .unwrap(); + } } let instance = compiled.instantiate(Arc::new(store)).unwrap(); block_on(async { @@ -233,39 +240,71 @@ fn run_data_plane(plan: &InstallablePlan) -> Vec>> { }) .expect("the plan scans the table"); let table = Batch::try_new(schema, rows()).unwrap(); - // Each view's state, by node id, with its data's span. - let mut built: BTreeMap = BTreeMap::new(); + // Each view's states, by node id, each with its data's span: a chain's + // one per tumbling window its retention keeps, newest last. + let mut built: BTreeMap> = BTreeMap::new(); for view in &views { - let span = view - .inputs - .raw_data_windows() + let width_ms = match &dag + .nodes .iter() - .map(|window| window.bounds_ms(EVALUATION_MS)) - .chain( - view.inputs - .materialized_views - .iter() - .map(|id| built[&(*id as u64)].1), - ) - .reduce(|(a, b), (c, d)| (a.min(c), b.max(d))) - .expect("a materialized view reads something"); - let mut sources = BTreeMap::new(); - for id in &view.inputs.materialized_views { - sources.insert(*id as u64, built[&(*id as u64)].0.clone()); - } - let scope = match view.refresh() { - MaterializedViewRefresh::AtIngestion => Scope::Ingestion { - window_start_ms: span.0, - window_end_ms: span.1, - }, - MaterializedViewRefresh::OnFirstRead => Scope::Query { - evaluation_time_ms: EVALUATION_MS, - }, + .find(|n| n.id == view.node_id) + .unwrap() + .payload + { + PhysicalASAPOperatorPayload::ASAP(ASAPOp::SummaryTumblingWindowChain { + tumbling_window_width_ms, + .. + }) => Some(*tumbling_window_width_ms as i64), + _ => None, + }; + let spans: Vec<(i64, i64)> = match width_ms { + Some(width_ms) => { + let windows = (view.retention_ms() as i64 / width_ms).max(1); + (0..windows) + .rev() + .map(|k| { + let end = EVALUATION_MS - k * width_ms; + (end - width_ms, end) + }) + .collect() + } + None => vec![view + .inputs + .raw_data_windows() + .iter() + .map(|window| window.bounds_ms(EVALUATION_MS)) + .chain( + view.inputs + .materialized_views + .iter() + .flat_map(|id| built[&(*id as u64)].iter().map(|(_, span)| *span)), + ) + .reduce(|(a, b), (c, d)| (a.min(c), b.max(d))) + .expect("a materialized view reads something")], }; - let batches = run_bound(dag, sources, &table, &[view.node_id as u64], scope).remove(0); - let state_rows = batches.iter().flat_map(|b| b.to_rows()).collect(); - let state = Batch::try_new(batches[0].schema().clone(), state_rows).unwrap(); - built.insert(view.node_id as u64, (state, span)); + let mut states = Vec::new(); + for span in spans { + let mut sources = BTreeMap::new(); + for id in &view.inputs.materialized_views { + sources.insert(*id as u64, built[&(*id as u64)].clone()); + } + let scope = match view.refresh() { + MaterializedViewRefresh::AtIngestion => Scope::Ingestion { + window_start_ms: span.0, + window_end_ms: span.1, + }, + MaterializedViewRefresh::OnFirstRead => Scope::Query { + evaluation_time_ms: EVALUATION_MS, + }, + }; + let batches = run_bound(dag, sources, &table, &[view.node_id as u64], scope).remove(0); + let state_rows = batches.iter().flat_map(|b| b.to_rows()).collect(); + states.push(( + Batch::try_new(batches[0].schema().clone(), state_rows).unwrap(), + span, + )); + } + built.insert(view.node_id as u64, states); } // The query run reads the views it reaches without passing another // one, and the windows it reads at query time. @@ -278,8 +317,8 @@ fn run_data_plane(plan: &InstallablePlan) -> Vec>> { continue; } match built.get(&id) { - Some((state, _)) => { - inputs.insert(id, state.clone()); + Some(built) => { + inputs.insert(id, built.clone()); } None => pending.extend( dag.edges @@ -315,11 +354,6 @@ async fn ingestion_time_tumbling_windows_built_from_table_rows_answer_exactly() "tumbling windows built at ingestion time from table rows: {:?}", views ); - // Older tumbling windows read windows that end before the evaluation. - assert!(views - .iter() - .flat_map(|v| v.inputs.raw_data_windows()) - .any(|w| w.end_offset_ms() > 0)); let answers = run_data_plane(&plan); assert_eq!(by_key(answers[0].clone()), expected_distinct_users()); let expected: BTreeMap = (0..3) diff --git a/crates/stage3-plan-selection/Cargo.toml b/crates/stage3-plan-selection/Cargo.toml index 22f140ef0..948673148 100644 --- a/crates/stage3-plan-selection/Cargo.toml +++ b/crates/stage3-plan-selection/Cargo.toml @@ -5,9 +5,9 @@ edition = "2021" # #509 Stage 3, plan selection: the only stage that prices plans, with the # planning models (accuracy from `asap-accuracy`; cost from `asap-cost`) -# Reads only Stage 2's output (`asap_types::stages`) and calls Stage 2 through -# the facade's `AssignmentRealizer`: no dependency on Stages 1 or 2, nor on -# the executor (#1054 X1, #1047 P7). Tests build their inputs with them. +# Reads only Stage 2's output (`asap_types::stages`) and builds the plan +# itself: no dependency on Stages 1 or 2, nor on the executor (#1054 X1, +# #1047 P7). [dependencies] asap-types = { path = "../types" } asap-accuracy = { path = "../accuracy" } @@ -18,3 +18,6 @@ serde_json = "1" good_lp = { version = "=1.15.3", default-features = false, features = ["highs"] } highs = "=2.4.0" + +[dev-dependencies] +asap-types = { path = "../types", features = ["test-support"] } diff --git a/crates/stage3-plan-selection/src/cost.rs b/crates/stage3-plan-selection/src/cost.rs new file mode 100644 index 000000000..40ce63de4 --- /dev/null +++ b/crates/stage3-plan-selection/src/cost.rs @@ -0,0 +1,525 @@ +//! The baseline objective's coefficients (concepts doc, section 3, "Stage +//! 3: baseline selection and plan output"): $w_v$ of every physical +//! candidate and $c_{u,c,i}$ of every edge a plan may select, from +//! `asap-cost` under cost model 1. +//! +//! Each candidate is costed as one plan node ([`candidate_node`], +//! [`asap_cost::node_resource_usage_and_cost`]) over the logical output of each input: the +//! most any costed candidate it may read there (`Memo::producers`) +//! outputs (rows, bytes and, for a state, its family). That is $w_v$, which +//! does not depend on the producers chosen. The part of a consumer's cost +//! that changes with the producer read is on the edge: $c_{u,c,i}$ is the +//! consumer's cost with input $i$ read from $u$'s own output, minus $w_c$. +//! It is exact when a consumer's cost changes with one input's producer +//! only (every consumer of a state reads one state input). Every state +//! candidate has its own θ (a merge, a `TimeRange` over a chain carry the +//! θ of the states they read, and read only producers with it), so a +//! consumer above a sketch build is costed at that build's θ. +//! +//! How often a candidate runs is the baseline's fixed rate: at ingestion +//! time, one second of arriving rows per second; at query time, once per +//! evaluation of the queries reaching its group (once per request when it +//! is materialized across evaluations). A materialized candidate and a +//! query-time `Scan` retain the most their selectable consumers read back +//! ([`read_back_ms`]). Execution contexts make both per context. + +use std::collections::HashMap; + +use asap_cost::plan::execution_demand::evaluation_rate; +use asap_cost::{ + node_resource_usage_and_cost, CostInputs, CostModel, NodeResourceUsageAndCost, ProducerOutput, +}; +use asap_types::ir::properties::ExecutionTiming; +use asap_types::ir::properties::{DataPrimitive, ExecutionDataState}; +use asap_types::ir::{ASAPOp, NonASAPOp, Operator}; +use asap_types::plan::physical_asap_dag::{ + PhysicalASAPDAGNode, PhysicalASAPNodeId, PhysicalASAPOperatorPayload, +}; +use asap_types::plan::view_coverage; +use asap_types::stages::{ + Lifetime, Memo, MemoGroupId, PhysicalASAPMemoCandidates, PhysicalCandidateId, PhysicalEdgeId, +}; +use asap_types::workload::{DataWorkload, QueryWorkloadEntry}; + +/// $w_v$ and $c_{u,c,i}$. +pub(crate) struct SelectionCosts { + /// $w_v$ per second, by [`PhysicalCandidateId`], or why `asap-cost` + /// could not cost it (the MILP leaves such a candidate out). + pub(crate) candidate: Vec>, + /// $c_{u,c,i}$ per second of each selectable edge whose producer and + /// consumer are costable; an edge missing here cannot be selected. + pub(crate) edge: HashMap, +} + +/// Candidate `v` as a plan node with id `id`, its input `i` read from node +/// `input_ids[i]`: its operator, timing, lifetime and θ, its group's schema +/// and, materialized across evaluations, its group's coverage. +/// `retention_ms` is left `None` for the caller to set. +pub(crate) fn candidate_node( + physical: &PhysicalASAPMemoCandidates, + v: PhysicalCandidateId, + id: PhysicalASAPNodeId, + input_ids: &[PhysicalASAPNodeId], +) -> PhysicalASAPDAGNode { + let memo = &physical.logical_candidates.memo; + let candidate = &physical.physical_candidates[v.0]; + let logical = memo.candidate(candidate.logical_candidate_id); + let group = &memo.groups()[candidate.logical_candidate_id.group.0 as usize].group_properties; + let mut inputs = input_ids.iter(); + let payload = logical + .expression + .operator + .map_children(|_| *inputs.next().expect("one node per input")); + let lifetime = candidate.physical_properties.lifetime(); + PhysicalASAPDAGNode { + id, + output_state: ExecutionDataState { + timing: candidate.physical_properties.timing(), + primitive: DataPrimitive::of(&payload, group.result_kind), + }, + payload, + lifetime, + parameters: logical.logical_properties.parameters().cloned(), + output_schema: group.schema.clone(), + retention_ms: None, + selected_physical_candidate: Some(v), + view_coverage: matches!(lifetime, Lifetime::AcrossEvaluations(_)) + .then(|| view_coverage(memo, candidate.logical_candidate_id.group)) + .flatten(), + } +} + +/// The number of inputs of candidate `v`. +pub(crate) fn input_count(physical: &PhysicalASAPMemoCandidates, v: PhysicalCandidateId) -> usize { + physical + .logical_candidates + .memo + .candidate(physical.physical_candidates[v.0].logical_candidate_id) + .expression + .operator + .children() + .len() +} + +/// Whether candidate `v` keeps time-indexed rows for its consumers to read +/// back: materialized across evaluations, or a `Scan` at query time (raw +/// data). Its node carries `retention_ms`. +pub(crate) fn retains_rows(physical: &PhysicalASAPMemoCandidates, v: PhysicalCandidateId) -> bool { + let candidate = &physical.physical_candidates[v.0]; + let scan = matches!( + physical + .logical_candidates + .memo + .candidate(candidate.logical_candidate_id) + .expression + .operator, + Operator::NonASAP(NonASAPOp::Scan { .. }) + ); + matches!( + candidate.physical_properties.lifetime(), + Lifetime::AcrossEvaluations(_) + ) || (scan && candidate.physical_properties.timing() == ExecutionTiming::QueryTime) +} + +/// How far back from the evaluation time `producer`'s rows are read +/// through `edges` (its edges to consumers; `consumers_of` gives each +/// candidate's edges to its own consumers): [`asap_cost::read_back_ms`] +/// with the longest range-evaluation span of the queries reaching each +/// `TimeRange` when `with_span` (retention), without it (one execution's +/// read of a query-time `Scan`). +pub(crate) fn read_back_ms( + physical: &PhysicalASAPMemoCandidates, + entries: &[QueryWorkloadEntry], + producer: PhysicalCandidateId, + edges: &[PhysicalEdgeId], + consumers_of: &dyn Fn(PhysicalCandidateId) -> Vec, + with_span: bool, +) -> Option { + let memo = &physical.logical_candidates.memo; + let operator_of = |v: PhysicalCandidateId| { + &memo + .candidate(physical.physical_candidates[v.0].logical_candidate_id) + .expression + .operator + }; + let span_ms = |v: PhysicalCandidateId| { + if !with_span { + return 0; + } + let group = physical.physical_candidates[v.0].logical_candidate_id.group; + memo.groups()[group.0 as usize] + .reached_by_queries() + .iter() + .filter_map(|&query| entries.get(query)?.range_evaluation) + .map(|range| i64::try_from(range.span_ms).unwrap_or(i64::MAX)) + .max() + .unwrap_or(0) + }; + let readers = |edges: Vec| -> Vec { + edges + .into_iter() + .map(|edge| physical.edge(edge).consumer_candidate_id) + .collect() + }; + asap_cost::read_back_ms( + readers(edges.to_vec()), + operator_of, + |v| readers(consumers_of(v)), + span_ms, + matches!( + operator_of(producer), + Operator::NonASAP(NonASAPOp::Scan { .. }) + ), + ) +} + +/// [`SelectionCosts`] over `selectable` (each admissible edge a plan may +/// select, by consumer and input). Errs on nothing: a candidate +/// `asap-cost` cannot cost is reported in [`SelectionCosts::candidate`]. +pub(crate) fn selection_costs( + physical: &PhysicalASAPMemoCandidates, + selectable: &[PhysicalEdgeId], + entries: &[QueryWorkloadEntry], + data: &DataWorkload, + cost_inputs: &CostInputs, + formula_scale: f64, +) -> SelectionCosts { + let memo = &physical.logical_candidates.memo; + let count = physical.physical_candidates.len(); + let mut out_edges: Vec> = vec![Vec::new(); count]; + for &edge in selectable { + out_edges[physical.edge(edge).producer_candidate_id.0].push(edge); + } + // Each candidate's node, its inputs numbered 0.. (the ids do not + // matter to `node_resource_usage_and_cost`), with the retention it would need. + let nodes: Vec = (0..count) + .map(PhysicalCandidateId) + .map(|v| { + let inputs: Vec = (0..input_count(physical, v)).collect(); + let mut node = candidate_node(physical, v, v.0, &inputs); + if retains_rows(physical, v) { + node.retention_ms = read_back_ms( + physical, + entries, + v, + &out_edges[v.0], + &|consumer| out_edges[consumer.0].clone(), + true, + ); + } + node + }) + .collect(); + // Each query-time `Scan`'s read per execution: the most any selectable + // consumer reads (no range-evaluation span), charged once. + let scan_read: Vec> = (0..count) + .map(PhysicalCandidateId) + .map(|v| { + let candidate = &physical.physical_candidates[v.0]; + let scan = matches!( + memo.candidate(candidate.logical_candidate_id) + .expression + .operator, + Operator::NonASAP(NonASAPOp::Scan { .. }) + ); + (scan && candidate.physical_properties.timing() == ExecutionTiming::QueryTime) + .then(|| { + read_back_ms( + physical, + entries, + v, + &out_edges[v.0], + &|consumer| out_edges[consumer.0].clone(), + false, + ) + }) + .flatten() + }) + .collect(); + // Each group's expression as a payload, for its logical output. + let group_payloads: Vec = memo + .groups() + .iter() + .map(|group| group.expression.map_children(|input| input.0 as usize)) + .collect(); + let mut candidates_of_group: Vec> = + vec![Vec::new(); memo.groups().len()]; + for (v, candidate) in physical.physical_candidates.iter().enumerate() { + candidates_of_group[candidate.logical_candidate_id.group.0 as usize] + .push(PhysicalCandidateId(v)); + } + let order = memo + .topological_order() + .unwrap_or_else(|| (0..memo.groups().len() as u32).map(MemoGroupId).collect()); + let mut usage: Vec, String>>> = + (0..count).map(|_| None).collect(); + for group in order { + for &v in &candidates_of_group[group.0 as usize] { + let inputs = logical_inputs(physical, &group_payloads, &candidates_of_group, &usage, v); + usage[v.0] = Some(match inputs { + Some(inputs) => cost_node( + physical, + v, + &nodes[v.0], + &inputs, + entries, + data, + cost_inputs, + formula_scale, + scan_read[v.0], + ), + None => Err("an input group has no costable candidate".to_string()), + }); + } + } + let candidate: Vec> = usage + .iter() + .map(|usage| match usage { + Some(Ok(usage)) => cost_per_second(usage, cost_inputs), + Some(Err(error)) => Err(error.clone()), + None => Err("it reads a group on a cycle of candidates".to_string()), + }) + .collect(); + let mut edge = HashMap::new(); + for &id in selectable { + let e = physical.edge(id); + let (u, c) = (e.producer_candidate_id, e.consumer_candidate_id); + let (Some(Ok(produced)), Ok(own)) = (&usage[u.0], &candidate[c.0]) else { + continue; + }; + let Some(mut inputs) = + logical_inputs(physical, &group_payloads, &candidates_of_group, &usage, c) + else { + continue; + }; + inputs[e.input_index_of_consumer] = producer_output(&nodes[u.0], produced); + let with_u = cost_node( + physical, + c, + &nodes[c.0], + &inputs, + entries, + data, + cost_inputs, + formula_scale, + scan_read[c.0], + ) + .and_then(|usage| cost_per_second(&usage, cost_inputs)); + if let Ok(with_u) = with_u { + edge.insert(id, with_u - own); + } + } + SelectionCosts { candidate, edge } +} + +/// The logical output of each input of `v` over the costed candidates +/// (`usage`) it may read there (`Memo::producers`); `None` when one has +/// none. +fn logical_inputs<'a>( + physical: &'a PhysicalASAPMemoCandidates, + group_payloads: &'a [PhysicalASAPOperatorPayload], + candidates_of_group: &[Vec], + usage: &[Option, String>>], + v: PhysicalCandidateId, +) -> Option>> { + let memo = &physical.logical_candidates.memo; + let consumer = physical.physical_candidates[v.0].logical_candidate_id; + input_groups(memo, physical, v) + .into_iter() + .enumerate() + .map(|(input, group)| { + let readable = memo.producers(consumer, input); + logical_output( + &group_payloads[group.0 as usize], + &memo.groups()[group.0 as usize].group_properties.schema, + candidates_of_group[group.0 as usize] + .iter() + .filter(|u| { + readable.contains(&physical.physical_candidates[u.0].logical_candidate_id) + }) + .filter_map(|u| usage[u.0].as_ref()?.as_ref().ok()), + ) + }) + .collect() +} + +/// The input groups of candidate `v`, by input index. +fn input_groups( + memo: &Memo, + physical: &PhysicalASAPMemoCandidates, + v: PhysicalCandidateId, +) -> Vec { + memo.candidate(physical.physical_candidates[v.0].logical_candidate_id) + .expression + .operator + .children() + .into_iter() + .copied() + .collect() +} + +/// What a consumer reads of candidate `node` with `usage`. +fn producer_output<'a>( + node: &'a PhysicalASAPDAGNode, + usage: &NodeResourceUsageAndCost<'a>, +) -> ProducerOutput<'a> { + ProducerOutput { + payload: &node.payload, + output_schema: &node.output_schema, + output: usage.output, + sources: usage.sources.clone(), + family: usage.family, + unchanged_columns: usage.unchanged_columns.clone(), + tumbling_windows: usage.tumbling_windows, + } +} + +/// An input's logical output over its readable costed candidates' `usages`: the most +/// rows and bytes any outputs, the state family of the one with the most +/// bytes, the sources any reads and the columns every one leaves unchanged; +/// `payload` and `schema` the group's own. `None` without a costed +/// candidate. +fn logical_output<'a: 'b, 'b>( + payload: &'a PhysicalASAPOperatorPayload, + schema: &'a asap_types::ir::schema::Schema, + usages: impl Iterator>, +) -> Option> { + let mut output: Option> = None; + for usage in usages { + let Some(known) = output.as_mut() else { + output = Some(ProducerOutput { + payload, + output_schema: schema, + output: usage.output, + sources: usage.sources.clone(), + family: usage.family, + unchanged_columns: usage.unchanged_columns.clone(), + tumbling_windows: usage.tumbling_windows, + }); + continue; + }; + if usage.output.bytes > known.output.bytes { + known.family = usage.family; + } + known.output.rows = known.output.rows.max(usage.output.rows); + known.output.bytes = known.output.bytes.max(usage.output.bytes); + known.sources.extend(usage.sources.iter().copied()); + known + .unchanged_columns + .retain(|column| usage.unchanged_columns.contains(column)); + } + output +} + +/// Candidate `v`'s usage as `node` over `inputs`, at its rate (module doc); +/// a query-time `Scan` reads `scan_read_ms` per execution. +#[allow(clippy::too_many_arguments)] +fn cost_node<'a>( + physical: &PhysicalASAPMemoCandidates, + v: PhysicalCandidateId, + node: &'a PhysicalASAPDAGNode, + inputs: &[ProducerOutput<'a>], + entries: &[QueryWorkloadEntry], + data: &DataWorkload, + cost_inputs: &'a CostInputs, + formula_scale: f64, + scan_read_ms: Option, +) -> Result, String> { + let memo = &physical.logical_candidates.memo; + let candidate = &physical.physical_candidates[v.0]; + let group = candidate.logical_candidate_id.group; + let ingestion = candidate.physical_properties.timing() == ExecutionTiming::IngestionTime; + let per_second = match ingestion { + true => 1.0, + false => { + let per_request = matches!( + candidate.physical_properties.lifetime(), + Lifetime::AcrossEvaluations(_) + ); + evaluation_rate( + memo.groups()[group.0 as usize] + .reached_by_queries() + .iter() + .filter_map(|&query| entries.get(query)) + .map(|entry| { + ( + &entry.recurrence, + (!per_request).then_some(entry.range_evaluation).flatten(), + ) + }), + cost_inputs.non_repeating_query_horizon_seconds, + ) + .map_err(|error| error.to_string())? + } + }; + let window_ms = match (&node.payload, ingestion) { + (_, false) => scan_read_ms, + ( + Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { + tumbling_window_width_ms, + .. + }), + true, + ) => Some(*tumbling_window_width_ms), + (_, true) => window_ms_below(memo, group), + }; + node_resource_usage_and_cost( + node, + inputs, + (1, per_second), + window_ms, + data, + cost_inputs, + formula_scale, + ) + .map_err(|error| error.to_string()) +} + +/// $w_v$ from a candidate's usage: its cost per second under cost model 1 +/// plus the raw data a query-time scan retains on disk. +fn cost_per_second( + usage: &NodeResourceUsageAndCost<'_>, + cost_inputs: &CostInputs, +) -> Result { + let CostModel::UsageOverTime(unit_cost) = &cost_inputs.cost_model else { + return Err("the selection MILP costs under cost model 1 only".into()); + }; + let own = usage + .cost + .cost_per_second + .ok_or("cost model 1 gives every node a cost per second")?; + let raw_data = usage.raw_scan.map_or(0.0, |(_, bytes)| { + unit_cost.cost_per_disk_byte_second * bytes as f64 + }); + let cost = own + raw_data; + match cost.is_finite() && cost >= 0.0 { + true => Ok(cost), + false => Err(format!( + "its cost per second {cost} is not a finite non-negative number" + )), + } +} + +/// The window, in ms, of the first range below `group`'s operator, +/// through filters, projections and time shifts: what an ingestion-time +/// grouping maintains (what asap-cost reads below a plan node, over the memo's +/// groups, since a candidate is costed before any plan exists). +fn window_ms_below(memo: &Memo, group: MemoGroupId) -> Option { + let range_ms = |range: &std::time::Duration| u64::try_from(range.as_millis()).ok(); + let expression = &memo.groups()[group.0 as usize].expression; + if let Operator::NonASAP(NonASAPOp::TimeRange { range, .. }) = expression { + return range_ms(range); + } + let mut below = **expression.children().first()?; + for _ in 0..memo.groups().len() { + let expression = &memo.groups()[below.0 as usize].expression; + match expression { + Operator::NonASAP(NonASAPOp::TimeRange { range, .. }) => return range_ms(range), + Operator::NonASAP( + NonASAPOp::Filter { .. } | NonASAPOp::Project { .. } | NonASAPOp::TimeShift { .. }, + ) => below = **expression.children().first()?, + _ => return None, + } + } + None +} diff --git a/crates/stage3-plan-selection/src/lib.rs b/crates/stage3-plan-selection/src/lib.rs index 978ca7c0e..0bfd35a58 100644 --- a/crates/stage3-plan-selection/src/lib.rs +++ b/crates/stage3-plan-selection/src/lib.rs @@ -13,14 +13,30 @@ //! per-candidate error bounds $E_c$ and $D_c$ (section 4). Costs come from //! `asap-cost`. +mod cost; mod milp; +mod plan; mod report; -use asap_cost::CostInputs; -use asap_types::plan::physical_asap_dag::PhysicalASAPDAG; -use asap_types::stages::PhysicalASAPMemoCandidates; +use std::collections::BTreeSet; + +use asap_cost::{summary_type_name, CostInputs, CostModel}; +use asap_types::ir::properties::ExecutionTiming; +use asap_types::ir::schema::FieldDataType; +use asap_types::ir::{ASAPOp, Operator, QueryRoot}; +use asap_types::plan::physical_asap_dag::{PhysicalASAPDAG, PhysicalASAPNodeId}; +use asap_types::stages::{ + output_references, producer_satisfies_physical_requirements, EvaluationGrid, ExecutionContext, + MemoGroupId, PhysicalASAPMemoCandidates, PhysicalCandidate, PhysicalCandidateId, PhysicalEdge, + PhysicalEdgeId, PruneReason, PrunedCandidate, +}; use asap_types::workload::{DataWorkload, QueryWorkloadEntry}; +use crate::cost::{selection_costs, SelectionCosts}; +use crate::milp::model::{solve_selection, QueryId, SelectionInputs, SelectionMilpError}; +use crate::plan::plan_of; +use crate::report::Rejection; + pub use report::{ large_peak_memory_warning, AcceptedAccuracy, ErrorSelectingPlan, RejectionCause, Selection, }; @@ -45,6 +61,233 @@ pub fn select_plan( cost_inputs .validate() .map_err(ErrorSelectingPlan::InvalidSettings)?; - let _ = (physical, entries, data); - todo!("#1207 PR 6: the baseline MILP over x, y, z, t, and the PhysicalASAPDAG of its solution, each materialized view node's view_coverage = asap_types::plan::view_coverage(memo, its group)") + if !matches!(cost_inputs.cost_model, CostModel::UsageOverTime(_)) { + return Err(ErrorSelectingPlan::UnsupportedCostModel( + "cost models 2 and 3 (the selection MILP implements cost model 1 only)".into(), + )); + } + let queries = physical.logical_candidates.memo.queries(); + if entries.len() != queries.len() { + return Err(ErrorSelectingPlan::EntryCount { + candidate: "the physical memo".into(), + roots: queries.len(), + entries: entries.len(), + }); + } + let formula_scale = cost_inputs.frozen_calibration_factor.unwrap_or(1.0); + let selectable: Vec = physical + .edges() + .filter(|(_, edge)| { + edge.is_logically_and_physically_admissible() + && delivers_to_consumer(physical, entries, edge) + }) + .map(|(id, _)| id) + .collect(); + let costs = selection_costs( + physical, + &selectable, + entries, + data, + cost_inputs, + formula_scale, + ); + let roots: Vec> = + queries.iter().map(|query| query.root.clone()).collect(); + let outputs: Vec<(QueryId, MemoGroupId)> = output_references(&roots) + .into_iter() + .map(|(query, group)| (QueryId(query), group)) + .collect::>() + .into_iter() + .collect(); + let query_outputs: Vec<(QueryId, Vec)> = outputs + .iter() + .map(|&(query, group)| { + let candidates = physical + .physical_candidates + .iter() + .enumerate() + .filter(|(v, candidate)| { + candidate.logical_candidate_id.group == group + && costs.candidate[*v].is_ok() + && producer_satisfies_physical_requirements( + candidate, + ExecutionTiming::QueryTime, + ) + && candidate.delivers_on(physical, &evaluation_context(&entries[query.0])) + }) + .map(|(v, _)| PhysicalCandidateId(v)) + .collect(); + (query, candidates) + }) + .collect(); + if query_outputs + .iter() + .any(|(_, candidates)| candidates.is_empty()) + { + return Err(infeasible(&costs)); + } + let solved = solve_selection(&SelectionInputs { + physical, + costs: &costs, + query_outputs: &query_outputs, + }) + .map_err(|error| match error { + SelectionMilpError::Infeasible => infeasible(&costs), + SelectionMilpError::Solver(reason) => ErrorSelectingPlan::Solver(reason), + })?; + let plan = plan_of(physical, entries, &solved, &outputs).map_err(|error| { + ErrorSelectingPlan::Solver(format!("the selected plan is invalid: {error}")) + })?; + let selection = Selection { + accepted_accuracy_per_query: accepted_accuracy(physical, &plan), + pruned_candidates: costs + .candidate + .iter() + .zip(&physical.physical_candidates) + .filter(|(cost, _)| cost.is_err()) + .map(|(_, &candidate)| PrunedCandidate { + candidate, + reason: PruneReason::UsageNotCostable, + }) + .collect(), + milp_variables: solved.variables, + milp_constraints: solved.constraints, + milp_construction_ms: solved.construction_ms, + milp_solve_ms: solved.solve_ms, + }; + Ok((plan, selection)) +} + +/// The evaluation context of `entry`'s query: the grid its evaluations +/// fall on (`QueryRecurrence::evaluation_grid_ms`), anchored at the epoch. +/// Times that are not known may fall on any millisecond: a 1 ms grid, on +/// which only producers stored at every time deliver. +fn evaluation_context(entry: &QueryWorkloadEntry) -> ExecutionContext { + ExecutionContext::Evaluation { + grid: EvaluationGrid { + period_ms: entry + .recurrence + .evaluation_grid_ms(entry.range_evaluation) + .unwrap_or(1), + phase_ms: 0, + }, + } +} + +/// Whether `edge`'s producer delivers on every evaluation its query-time +/// consumer serves: on the evaluation context of each query reaching the +/// consumer's group ([`PhysicalCandidate::delivers_on`]). The baseline has +/// no other context: an ingestion-time consumer's (its pipeline) is checked +/// with execution contexts. +fn delivers_to_consumer( + physical: &PhysicalASAPMemoCandidates, + entries: &[QueryWorkloadEntry], + edge: &PhysicalEdge, +) -> bool { + let producer: &PhysicalCandidate = &physical.physical_candidates[edge.producer_candidate_id.0]; + let consumer = &physical.physical_candidates[edge.consumer_candidate_id.0]; + if consumer.physical_properties.timing() != ExecutionTiming::QueryTime { + return true; + } + physical.logical_candidates.memo.groups()[consumer.logical_candidate_id.group.0 as usize] + .reached_by_queries() + .iter() + .filter_map(|&query| entries.get(query)) + .all(|entry| producer.delivers_on(physical, &evaluation_context(entry))) +} + +/// The selection MILP is infeasible, with the reasons candidates could not +/// be costed. +fn infeasible(costs: &SelectionCosts) -> ErrorSelectingPlan { + let mut reasons: Vec<&str> = Vec::new(); + for reason in costs + .candidate + .iter() + .filter_map(|cost| cost.as_ref().err()) + { + if !reasons.contains(&reason.as_str()) { + reasons.push(reason); + } + } + let mut reason = "no plan gives every query output a candidate whose every input has an \ + admissible producer (the selection MILP is infeasible)" + .to_string(); + if !reasons.is_empty() { + reason += &format!( + "; candidates left out because they could not be costed: {}", + reasons.join("; ") + ); + } + ErrorSelectingPlan::NoValidCandidate(vec![Rejection { + id: "workload".into(), + cause: RejectionCause::Infeasible, + reason, + }]) +} + +/// Per query, the sketches its output reads in `plan` (each sketch build +/// below its root, in node order) and, when it reads one, its target's +/// metric. +fn accepted_accuracy( + physical: &PhysicalASAPMemoCandidates, + plan: &PhysicalASAPDAG, +) -> Vec { + let mut operator_roots = plan.roots.iter(); + physical + .logical_candidates + .memo + .queries() + .iter() + .enumerate() + .map(|(query, memo_query)| { + let starts: Vec = match plan + .scalar_roots + .iter() + .find(|(position, _)| *position == query) + { + Some((_, expression)) => expression.operator_refs().into_iter().copied().collect(), + None => vec![*operator_roots.next().expect("one operator root per query")], + }; + let sketches = sketches_below(plan, starts); + AcceptedAccuracy { + accepted_metric: (!sketches.is_empty()) + .then(|| memo_query.accuracy_target.as_ref()?.accuracy_metric) + .flatten(), + sketches, + } + }) + .collect() +} + +/// The summary type names of the sketch builds at or below `starts` in +/// `plan`, in node order, each once. +fn sketches_below(plan: &PhysicalASAPDAG, starts: Vec) -> Vec { + let mut below = BTreeSet::new(); + let mut pending = starts; + while let Some(id) = pending.pop() { + if below.insert(id) { + pending.extend( + plan.edges + .iter() + .filter(|e| e.consumer == id) + .map(|e| e.producer), + ); + } + } + let mut sketches: Vec = Vec::new(); + for node in plan.nodes.iter().filter(|node| below.contains(&node.id)) { + if let Operator::ASAP( + ASAPOp::SummaryAgg { summary_type, .. } + | ASAPOp::SummaryTumblingWindowChain { summary_type, .. }, + ) = &node.payload + { + if matches!(summary_type, FieldDataType::Sketch(_)) { + let name = summary_type_name(summary_type); + if !sketches.contains(&name) { + sketches.push(name); + } + } + } + } + sketches } diff --git a/crates/stage3-plan-selection/src/milp/model.rs b/crates/stage3-plan-selection/src/milp/model.rs index 5af066859..6b7382e83 100644 --- a/crates/stage3-plan-selection/src/milp/model.rs +++ b/crates/stage3-plan-selection/src/milp/model.rs @@ -3,30 +3,49 @@ //! //! | Family | Variables | Constraints | //! |---|---|---| -//! | baseline (section 3) | $x_v$, $y_{u,c,i}$, $z_{q,v}$, $t_v$ ([`SelectionVariables`]) | (A) [`input_satisfaction_constraints`], (B) [`producer_selection_constraints`], (C)/(D) [`query_coverage_constraints`], (E) [`acyclicity_constraints`], (F) [`utilization_constraints`]; (G), (H): $y$ exists only on admissible edges | +//! | baseline (section 3) | $x_v$, $y_{u,c,i}$, $z_{q,v}$, $t_v$ ([`SelectionVariables`]) | (A) [`input_satisfaction_constraints`], (B) [`producer_selection_constraints`], (C)/(D) [`query_coverage_constraints`], (E) [`acyclicity_constraints`], (F) [`utilization_constraints`], one consumer of a query-time result that is not materialized [`unshared_result_constraints`]; (G), (H): $y$ exists only on admissible edges | //! | execution contexts (section 5) | $x^{\mathrm{mat}}_v$, $x_{\kappa,v}$, $y_{\kappa,u,c,i}$, $h_v$ ([`ExecutionContextVariables`]) | (R) [`retention_constraints`] | //! | accuracy (section 4) | $E_{c,m}$, $F_{c,i,m}$, $D_c$, $H_{c,i}$ ([`AccuracyVariables`]) | (K) [`accuracy_constraints`] | //! -//! Every function body is `todo!()` until #1207 PRs 6 (baseline), 7 -//! (contexts) and 8 (accuracy) fill them in; until then nothing calls -//! them, hence the `dead_code` allowance below. -#![allow(dead_code)] +//! The baseline is solved by [`solve_selection`]; the extensions' bodies +//! are `todo!()` until execution contexts and constraint (K) are added. use std::collections::HashMap; +use std::time::Instant; -use good_lp::{Constraint, Variable}; +use good_lp::solvers::highs::highs; +use good_lp::{ + constraint, variable, Constraint, Expression, ProblemVariables, SolverModel, Variable, +}; use asap_types::accuracy::AccuracyMetric; +use asap_types::ir::properties::ExecutionTiming; +use asap_types::stages::Lifetime; use asap_types::stages::{ ExecutionContextId, PhysicalASAPMemoCandidates, PhysicalCandidateId, PhysicalEdgeId, }; +use crate::cost::{input_count, SelectionCosts}; + /// A query, by index in the workload. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct QueryId(pub usize); +/// What the baseline MILP is built over. +pub(crate) struct SelectionInputs<'a> { + pub(crate) physical: &'a PhysicalASAPMemoCandidates, + /// $w_v$ and $c_{u,c,i}$: $y$ exists only on the edges + /// [`SelectionCosts::edge`] costs, $x_v$ is 0 for a candidate it could + /// not cost. + pub(crate) costs: &'a SelectionCosts, + /// Each query output reference (a query, or one operator a scalar query + /// reads), with the candidates that may answer it: $z_{q,v}$ exists + /// only for them. + pub(crate) query_outputs: &'a [(QueryId, Vec)], +} + /// The baseline variables (section 3). -struct SelectionVariables { +pub(crate) struct SelectionVariables { /// $x_v$, by [`PhysicalCandidateId`]. x: Vec, /// $y_{u,c,i}$, on admissible edges only. @@ -38,76 +57,414 @@ struct SelectionVariables { t: Vec>, } -/// The execution-context variables (section 5), which replace $x$ and the -/// unindexed $y$ of the baseline. -struct ExecutionContextVariables { - /// $x^{\mathrm{mat}}_v$, for an `AcrossEvaluations` candidate. - x_materialized: HashMap, - /// $x_{\kappa,v}$, for a candidate with $\ell$ = None or `OneEvaluation`. - x_in_execution_context: HashMap<(ExecutionContextId, PhysicalCandidateId), Variable>, - /// $y_{\kappa,u,c,i}$, indexed by the consumer's context. - y_in_execution_context: HashMap<(ExecutionContextId, PhysicalEdgeId), Variable>, - /// $h_v$, for an `AcrossEvaluations` producer. - retention_amount: HashMap, -} - -/// The accuracy variables of constraint (K) (section 4), all continuous. -struct AccuracyVariables { - /// $E_{c,m}$: candidate $c$'s error bound in metric $m$. - error_bound: HashMap<(PhysicalCandidateId, AccuracyMetric), Variable>, - /// $F_{c,i,m}$: the chosen producer's error bound in metric $m$ on input - /// $i$ of $c$. - error_bound_of_input: HashMap<(PhysicalCandidateId, usize, AccuracyMetric), Variable>, - /// $D_c$, by [`PhysicalCandidateId`]. - failure_probability: Vec, - /// $H_{c,i}$: the chosen producer's failure probability on input $i$ of $c$. - failure_probability_of_input: HashMap<(PhysicalCandidateId, usize), Variable>, +/// $x$, $y$, $z$ and $t$ over `inputs`, added to `problem`. +fn selection_variables( + inputs: &SelectionInputs<'_>, + problem: &mut ProblemVariables, +) -> SelectionVariables { + let count = inputs.physical.physical_candidates.len(); + let x = (0..count) + .map(|v| match inputs.costs.candidate[v].is_ok() { + true => problem.add(variable().binary()), + false => problem.add(variable().min(0).max(0)), + }) + .collect(); + let mut edges: Vec = inputs.costs.edge.keys().copied().collect(); + edges.sort_unstable(); + let y = edges + .into_iter() + .map(|edge| (edge, problem.add(variable().binary()))) + .collect(); + let mut z = HashMap::new(); + for (query, candidates) in inputs.query_outputs { + for &v in candidates { + z.entry((*query, v)) + .or_insert_with(|| problem.add(variable().binary())); + } + } + // t_v only inside a strongly connected component of the selectable + // edges, bounded by its size. + let t = component_sizes(inputs) + .into_iter() + .map(|size| (size > 1).then(|| problem.add(variable().min(0).max((size - 1) as f64)))) + .collect(); + SelectionVariables { x, y, z, t } } /// (A) $\sum_{u \in P_{c,i}} y_{u,c,i} = x_c$. fn input_satisfaction_constraints( - physical: &PhysicalASAPMemoCandidates, + inputs: &SelectionInputs<'_>, variables: &SelectionVariables, ) -> Vec { - let _ = (physical, variables); - todo!("#1207 PR 6: constraint (A)") + let physical = inputs.physical; + let mut constraints = Vec::new(); + for c in (0..physical.physical_candidates.len()).map(PhysicalCandidateId) { + for i in 0..input_count(physical, c) { + let producers: Expression = physical + .admissible_edges_into(c, i) + .iter() + .filter_map(|edge| variables.y.get(edge)) + .sum(); + constraints.push(constraint!(producers == variables.x[c.0])); + } + } + constraints } /// (B) $y_{u,c,i} \le x_u$. fn producer_selection_constraints( - physical: &PhysicalASAPMemoCandidates, + inputs: &SelectionInputs<'_>, variables: &SelectionVariables, ) -> Vec { - let _ = (physical, variables); - todo!("#1207 PR 6: constraint (B)") + sorted(&variables.y) + .map(|(edge, y)| { + let producer = inputs.physical.edge(edge).producer_candidate_id; + constraint!(y <= variables.x[producer.0]) + }) + .collect() } /// (C) $\sum_{v \in G_q} z_{q,v} = 1$; (D) $z_{q,v} \le x_v$. fn query_coverage_constraints( - physical: &PhysicalASAPMemoCandidates, + inputs: &SelectionInputs<'_>, variables: &SelectionVariables, ) -> Vec { - let _ = (physical, variables); - todo!("#1207 PR 6: constraints (C) and (D)") + let mut constraints = Vec::new(); + for (query, candidates) in inputs.query_outputs { + let covered: Expression = candidates.iter().map(|&v| variables.z[&(*query, v)]).sum(); + constraints.push(constraint!(covered == 1)); + } + for (&(_, v), &z) in sorted_by_key(&variables.z) { + constraints.push(constraint!(z <= variables.x[v.0])); + } + constraints } /// (E) $t_u + 1 \le t_c + n(1 - y_{u,c,i})$, only on edges inside a strongly /// connected component of the admissible edges, $n$ its size. fn acyclicity_constraints( - physical: &PhysicalASAPMemoCandidates, + inputs: &SelectionInputs<'_>, variables: &SelectionVariables, ) -> Vec { - let _ = (physical, variables); - todo!("#1207 PR 6: constraint (E)") + let components = selectable_components(inputs); + let sizes = component_sizes(inputs); + sorted(&variables.y) + .filter_map(|(edge, y)| { + let edge = inputs.physical.edge(edge); + let (u, c) = (edge.producer_candidate_id.0, edge.consumer_candidate_id.0); + if components[u] != components[c] { + return None; + } + let (t_u, t_c) = (variables.t[u]?, variables.t[c]?); + let n = sizes[u] as f64; + Some(constraint!(t_u + 1 <= t_c + n * (1 - y))) + }) + .collect() } /// (F) $x_v \le \sum_{(v,c,i)} y_{v,c,i} + \sum_q z_{q,v}$. fn utilization_constraints( - physical: &PhysicalASAPMemoCandidates, + inputs: &SelectionInputs<'_>, variables: &SelectionVariables, ) -> Vec { - let _ = (physical, variables); - todo!("#1207 PR 6: constraint (F)") + let uses = uses_of(inputs, variables); + uses.into_iter() + .enumerate() + .map(|(v, used)| constraint!(variables.x[v] <= used)) + .collect() +} + +/// A query-time result that is not materialized ($\ell$ = None) has at most +/// one consumer: $\sum_{(v,c,i)} y_{v,c,i} + \sum_q z_{q,v} \le x_v$. +/// Consumers that share a result read a `OneEvaluation` or a materialized +/// producer (section 5, per execution context). An ingestion-time +/// producer is exempt: there is no `OneEvaluation` at ingestion time, and +/// the consumers of one ingestion run share its rows. +fn unshared_result_constraints( + inputs: &SelectionInputs<'_>, + variables: &SelectionVariables, +) -> Vec { + let uses = uses_of(inputs, variables); + uses.into_iter() + .enumerate() + .filter(|(v, _)| { + let properties = inputs.physical.physical_candidates[*v].physical_properties; + properties.lifetime() == Lifetime::None + && properties.timing() == ExecutionTiming::QueryTime + }) + .map(|(v, used)| constraint!(used <= variables.x[v])) + .collect() +} + +/// Per candidate, its selected edges to consumers and query outputs: +/// $\sum_{(v,c,i)} y_{v,c,i} + \sum_q z_{q,v}$. +fn uses_of(inputs: &SelectionInputs<'_>, variables: &SelectionVariables) -> Vec { + let mut uses: Vec = (0..inputs.physical.physical_candidates.len()) + .map(|v| { + inputs + .physical + .admissible_edges_out_of(PhysicalCandidateId(v)) + .iter() + .filter_map(|edge| variables.y.get(edge)) + .sum() + }) + .collect(); + for (&(_, v), &z) in sorted_by_key(&variables.z) { + uses[v.0] += z; + } + uses +} + +/// `y` in edge order, so the model is the same in every run. +fn sorted( + y: &HashMap, +) -> impl Iterator { + let mut y: Vec<(PhysicalEdgeId, Variable)> = y.iter().map(|(&e, &v)| (e, v)).collect(); + y.sort_unstable_by_key(|(edge, _)| *edge); + y.into_iter() +} + +/// `z` in key order. +fn sorted_by_key( + z: &HashMap<(QueryId, PhysicalCandidateId), Variable>, +) -> Vec<(&(QueryId, PhysicalCandidateId), &Variable)> { + let mut z: Vec<_> = z.iter().collect(); + z.sort_unstable_by_key(|(key, _)| **key); + z +} + +/// Each candidate's strongly connected component of the selectable edges. +fn selectable_components(inputs: &SelectionInputs<'_>) -> Vec { + strongly_connected_components( + inputs.physical.physical_candidates.len(), + inputs.costs.edge.keys().map(|&edge| { + let edge = inputs.physical.edge(edge); + (edge.producer_candidate_id.0, edge.consumer_candidate_id.0) + }), + ) +} + +/// The size of each candidate's [`selectable_components`] component: 1 +/// for a candidate on no cycle. +fn component_sizes(inputs: &SelectionInputs<'_>) -> Vec { + let components = selectable_components(inputs); + let mut size: HashMap = HashMap::new(); + for &component in &components { + *size.entry(component).or_default() += 1; + } + components.iter().map(|component| size[component]).collect() +} + +/// Each node's strongly connected component of the graph with `arcs` +/// (Kosaraju), as a component number. +fn strongly_connected_components( + node_count: usize, + arcs: impl Iterator, +) -> Vec { + let mut forward = vec![Vec::new(); node_count]; + let mut backward = vec![Vec::new(); node_count]; + for (from, to) in arcs { + forward[from].push(to); + backward[to].push(from); + } + // Nodes by finishing time of a depth-first search over `forward`. + let mut finished = Vec::with_capacity(node_count); + let mut visited = vec![false; node_count]; + for start in 0..node_count { + if std::mem::replace(&mut visited[start], true) { + continue; + } + let mut stack = vec![(start, 0)]; + while let Some((node, next)) = stack.pop() { + if let Some(&to) = forward[node].get(next) { + stack.push((node, next + 1)); + if !std::mem::replace(&mut visited[to], true) { + stack.push((to, 0)); + } + } else { + finished.push(node); + } + } + } + let mut component = vec![usize::MAX; node_count]; + for (number, &start) in finished.iter().rev().enumerate() { + if component[start] != usize::MAX { + continue; + } + component[start] = number; + let mut stack = vec![start]; + while let Some(node) = stack.pop() { + for &from in &backward[node] { + if component[from] == usize::MAX { + component[from] = number; + stack.push(from); + } + } + } + } + component +} + +/// The baseline MILP's optimum: $\{x = 1\}$, $\{y = 1\}$ and $\{z = 1\}$, +/// and the MILP's size and time. +#[derive(Debug, Clone)] +pub(crate) struct SolvedSelection { + /// The selected candidates, ascending. + pub(crate) candidates: Vec, + /// The selected edges, ascending. + pub(crate) edges: Vec, + /// The candidate answering each of `SelectionInputs::query_outputs`, in + /// its order. + pub(crate) outputs: Vec, + pub(crate) variables: usize, + pub(crate) constraints: usize, + pub(crate) construction_ms: u64, + pub(crate) solve_ms: u64, +} + +/// Why the baseline MILP has no optimum. +#[derive(Debug, Clone, PartialEq)] +pub(crate) enum SelectionMilpError { + /// No plan meets constraints (A)–(F). + Infeasible, + /// The solver failed. + Solver(String), +} + +/// Builds and solves the baseline MILP: minimize +/// $\sum_v w_v x_v + \sum_{(u,c,i)} c_{u,c,i} y_{u,c,i}$ under (A)–(F) and +/// [`unshared_result_constraints`], with HiGHS at gap 0 on one thread, the +/// coefficients scaled so the largest is 1,000 (HiGHS's tolerances are +/// absolute). +pub(crate) fn solve_selection( + inputs: &SelectionInputs<'_>, +) -> Result { + let started = Instant::now(); + let mut problem = ProblemVariables::new(); + let variables = selection_variables(inputs, &mut problem); + let constraints: Vec = [ + input_satisfaction_constraints(inputs, &variables), + producer_selection_constraints(inputs, &variables), + query_coverage_constraints(inputs, &variables), + acyclicity_constraints(inputs, &variables), + utilization_constraints(inputs, &variables), + unshared_result_constraints(inputs, &variables), + ] + .into_iter() + .flatten() + .collect(); + let largest = inputs + .costs + .candidate + .iter() + .flatten() + .chain(inputs.costs.edge.values()) + .fold(0.0_f64, |largest, &cost| largest.max(cost.abs())); + let scale = match largest > 0.0 && largest.is_finite() { + true => 1_000.0 / largest, + false => 1.0, + }; + let candidate_terms = inputs + .costs + .candidate + .iter() + .zip(&variables.x) + .filter_map(|(cost, &x)| cost.as_ref().ok().map(|cost| cost * scale * x)); + let edge_terms = sorted(&variables.y).map(|(edge, y)| inputs.costs.edge[&edge] * scale * y); + let objective: Expression = candidate_terms.chain(edge_terms).sum(); + let variable_count = problem.len(); + // HiGHS's columns are the variables in the order they were added. + let column_of: HashMap = problem + .iter_variables_with_def() + .enumerate() + .map(|(column, (variable, _))| (variable, column)) + .collect(); + let constraint_count = constraints.len(); + let mut model = problem + .minimise(objective) + .using(highs) + .with_all(constraints) + .try_into_inner() + .map_err(|error| SelectionMilpError::Solver(error.to_string()))?; + model.set_option("mip_rel_gap", 0.0); + model.set_option("mip_abs_gap", 0.0); + model.set_option("output_flag", false); + model.set_option("threads", 1); + let construction_ms = started.elapsed().as_millis() as u64; + let started = Instant::now(); + let solved = model + .try_solve() + .map_err(|status| SelectionMilpError::Solver(format!("{status:?}")))?; + let solve_ms = started.elapsed().as_millis() as u64; + use highs::{HighsModelStatus, HighsSolutionStatus}; + let columns: Vec = match solved.status() { + // No constraint: no query reads an operator (literal-only scalar + // roots), so nothing is selected. + HighsModelStatus::ModelEmpty => vec![0.0; variable_count], + HighsModelStatus::Optimal + if solved.primal_solution_status() == HighsSolutionStatus::Feasible => + { + solved.get_solution().columns().to_vec() + } + HighsModelStatus::Infeasible | HighsModelStatus::UnboundedOrInfeasible => { + return Err(SelectionMilpError::Infeasible) + } + other => return Err(SelectionMilpError::Solver(format!("{other:?}"))), + }; + let chosen = |variable: Variable| columns[column_of[&variable]] > 0.5; + Ok(SolvedSelection { + candidates: (0..variables.x.len()) + .filter(|&v| chosen(variables.x[v])) + .map(PhysicalCandidateId) + .collect(), + edges: sorted(&variables.y) + .filter(|&(_, y)| chosen(y)) + .map(|(edge, _)| edge) + .collect(), + outputs: inputs + .query_outputs + .iter() + .map(|(query, candidates)| { + candidates + .iter() + .copied() + .find(|&v| chosen(variables.z[&(*query, v)])) + .expect("(C): one output per query output reference") + }) + .collect(), + variables: variable_count, + constraints: constraint_count, + construction_ms, + solve_ms, + }) +} + +/// The execution-context variables (section 5), which replace $x$ and the +/// unindexed $y$ of the baseline. +#[allow(dead_code)] // Read once execution contexts are selected. +struct ExecutionContextVariables { + /// $x^{\mathrm{mat}}_v$, for an `AcrossEvaluations` candidate. + x_materialized: HashMap, + /// $x_{\kappa,v}$, for a candidate with $\ell$ = None or `OneEvaluation`. + x_in_execution_context: HashMap<(ExecutionContextId, PhysicalCandidateId), Variable>, + /// $y_{\kappa,u,c,i}$, indexed by the consumer's context. + y_in_execution_context: HashMap<(ExecutionContextId, PhysicalEdgeId), Variable>, + /// $h_v$, for an `AcrossEvaluations` producer. + retention_amount: HashMap, +} + +/// The accuracy variables of constraint (K) (section 4), all continuous. +#[allow(dead_code)] // Read once constraint (K) is added. +struct AccuracyVariables { + /// $E_{c,m}$: candidate $c$'s error bound in metric $m$. + error_bound: HashMap<(PhysicalCandidateId, AccuracyMetric), Variable>, + /// $F_{c,i,m}$: the chosen producer's error bound in metric $m$ on input + /// $i$ of $c$. + error_bound_of_input: HashMap<(PhysicalCandidateId, usize, AccuracyMetric), Variable>, + /// $D_c$, by [`PhysicalCandidateId`]. + failure_probability: Vec, + /// $H_{c,i}$: the chosen producer's failure probability on input $i$ of $c$. + failure_probability_of_input: HashMap<(PhysicalCandidateId, usize), Variable>, } /// (R) $h_v \ge \operatorname{need}_{\kappa,v,c,i}\, y_{\kappa,v,c,i}$: for a @@ -115,6 +472,7 @@ fn utilization_constraints( /// time) the reading path's offset plus range, a constant per edge, so (R) /// is linear. Query latency is ignored: a result counts as read when its /// evaluation starts. +#[allow(dead_code)] // Called once execution contexts are selected. fn retention_constraints( physical: &PhysicalASAPMemoCandidates, variables: &ExecutionContextVariables, @@ -127,6 +485,7 @@ fn retention_constraints( /// selected edges, the metric rule on the selected path, and the root /// checks $E_{v,m_q} \le \varepsilon_q + M_q(1 - z_{q,v})$, /// $D_v \le \delta_q + (1 - z_{q,v})$. +#[allow(dead_code)] // Called once constraint (K) is added. fn accuracy_constraints( physical: &PhysicalASAPMemoCandidates, selection: &SelectionVariables, @@ -135,3 +494,6 @@ fn accuracy_constraints( let _ = (physical, selection, accuracy); todo!("#1207 PR 8: constraint (K)") } + +#[cfg(test)] +mod tests; diff --git a/crates/stage3-plan-selection/src/milp/model/tests.rs b/crates/stage3-plan-selection/src/milp/model/tests.rs new file mode 100644 index 000000000..24076c603 --- /dev/null +++ b/crates/stage3-plan-selection/src/milp/model/tests.rs @@ -0,0 +1,283 @@ +//! The baseline MILP over small hand-built candidate graphs with given +//! costs: which candidates and edges its optimum selects, and the plan +//! built from it. + +use std::collections::HashMap; + +use asap_types::ir::properties::ExecutionTiming; +use asap_types::ir::{Operator, QueryRoot}; +use asap_types::stages::{ + CandidateForMemoGroupId, LogicalASAPMemoCandidates, Memo, MemoGroupId, + PhysicalASAPMemoCandidates, PhysicalCandidate, PhysicalCandidateId, PhysicalEdge, + PhysicalEdgeId, +}; +use asap_types::stages::{Lifetime, PhysicalProperties, StorageMedium}; +use asap_types::test_support::memo_builders::{ + group, own_candidate, query, scan_group, try_candidate, +}; +use asap_types::test_support::memo_fixtures; +use asap_types::workload::DataWorkload; + +use super::*; +use crate::cost::SelectionCosts; +use crate::plan::plan_of; + +const NONE: Lifetime = Lifetime::None; +const ONE_EVALUATION: Lifetime = Lifetime::OneEvaluation(StorageMedium::Memory); + +fn scan(memo: &mut Memo) -> MemoGroupId { + scan_group(memo, "m", &["job"], vec![]) +} + +fn range(memo: &mut Memo, seconds: i64, child: MemoGroupId) -> MemoGroupId { + group(memo, memo_fixtures::range(seconds * 1_000, child)) +} + +fn shift(offset_ms: i64, child: MemoGroupId) -> Operator { + memo_fixtures::shift(offset_ms, None, child) +} + +fn empty_memo() -> Memo { + Memo::new(&DataWorkload::default()) +} + +/// Stage 2's output over `memo`, with one query-time candidate per +/// (logical candidate, lifetime) of `candidates` and every edge between +/// a candidate of an input group and its consumer admissible. +fn physical( + memo: Memo, + candidates: &[(CandidateForMemoGroupId, Lifetime)], +) -> PhysicalASAPMemoCandidates { + let physical_candidates: Vec = candidates + .iter() + .map(|&(logical_candidate_id, lifetime)| PhysicalCandidate { + logical_candidate_id, + physical_properties: PhysicalProperties::new(ExecutionTiming::QueryTime, lifetime) + .unwrap(), + }) + .collect(); + let mut edges = Vec::new(); + for (c, consumer) in physical_candidates.iter().enumerate() { + let inputs = memo + .candidate(consumer.logical_candidate_id) + .expression + .operator + .children() + .into_iter() + .copied() + .collect::>(); + for (i, group) in inputs.into_iter().enumerate() { + for (u, producer) in physical_candidates.iter().enumerate() { + if producer.logical_candidate_id.group == group { + edges.push(PhysicalEdge { + producer_candidate_id: PhysicalCandidateId(u), + consumer_candidate_id: PhysicalCandidateId(c), + input_index_of_consumer: i, + satisfies_logical_requirement: true, + satisfies_physical_requirement: true, + }); + } + } + } + } + let groups = memo.groups().len(); + PhysicalASAPMemoCandidates::new( + LogicalASAPMemoCandidates { + memo, + edges: vec![], + pruned: vec![], + evaluation_grid_of_group: vec![None; groups], + exact_only_groups_of_query: vec![], + }, + physical_candidates, + edges, + vec![], + ) +} + +/// The id of the edge from `u` to `c`. +fn edge(physical: &PhysicalASAPMemoCandidates, u: usize, c: usize) -> PhysicalEdgeId { + physical + .edges() + .find(|(_, edge)| edge.producer_candidate_id.0 == u && edge.consumer_candidate_id.0 == c) + .unwrap() + .0 +} + +/// Costs `w` per candidate and `edge_costs` per (producer, consumer), 0 for +/// every other edge. +fn costs( + physical: &PhysicalASAPMemoCandidates, + w: &[f64], + edge_costs: &[((usize, usize), f64)], +) -> SelectionCosts { + let mut edge_cost: HashMap = + physical.edges().map(|(id, _)| (id, 0.0)).collect(); + for &((u, c), cost) in edge_costs { + edge_cost.insert(edge(physical, u, c), cost); + } + SelectionCosts { + candidate: w.iter().map(|&w| Ok(w)).collect(), + edge: edge_cost, + } +} + +/// Each query output reference of `physical`'s memo with every candidate of +/// its group. +fn outputs(physical: &PhysicalASAPMemoCandidates) -> Vec<(QueryId, Vec)> { + physical + .logical_candidates + .memo + .queries() + .iter() + .enumerate() + .map(|(query, memo_query)| { + let QueryRoot::Operator(group) = memo_query.root else { + unreachable!("operator roots only") + }; + let candidates = (0..physical.physical_candidates.len()) + .filter(|&v| physical.physical_candidates[v].logical_candidate_id.group == group) + .map(PhysicalCandidateId) + .collect(); + (QueryId(query), candidates) + }) + .collect() +} + +fn solve( + physical: &PhysicalASAPMemoCandidates, + costs: &SelectionCosts, +) -> Result { + solve_selection(&SelectionInputs { + physical, + costs, + query_outputs: &outputs(physical), + }) +} + +fn ids(ids: &[usize]) -> Vec { + ids.iter().copied().map(PhysicalCandidateId).collect() +} + +/// `m[5m]`, its scan with two physical candidates. Through v0 the read +/// costs 1 + 5, through v1 2 + 0: the optimum takes v1, and the plan built +/// from it is the scan v1 below the range v2, the scan retaining the 5 +/// minutes the range reads. +#[test] +fn selects_the_producer_whose_own_and_edge_costs_are_lowest() { + let mut memo = empty_memo(); + let m = scan(&mut memo); + let last_5m = range(&mut memo, 300, m); + query(&mut memo, last_5m, None); + let first = own_candidate; + let physical = physical( + memo, + &[ + (first(m), ONE_EVALUATION), + (first(m), NONE), + (first(last_5m), NONE), + ], + ); + let costs = costs(&physical, &[1.0, 2.0, 1.0], &[((0, 2), 5.0)]); + let solved = solve(&physical, &costs).unwrap(); + assert_eq!(solved.candidates, ids(&[1, 2])); + assert_eq!(solved.edges, vec![edge(&physical, 1, 2)]); + assert_eq!(solved.outputs, ids(&[2])); + + let outputs = vec![(QueryId(0), last_5m)]; + let plan = plan_of(&physical, &[], &solved, &outputs).unwrap(); + assert_eq!(plan.roots, vec![1]); + assert_eq!(plan.edges.len(), 1); + assert_eq!((plan.edges[0].producer, plan.edges[0].consumer), (0, 1)); + let candidates: Vec<_> = plan + .nodes + .iter() + .map(|node| node.selected_physical_candidate) + .collect(); + assert_eq!( + candidates, + vec![Some(PhysicalCandidateId(1)), Some(PhysicalCandidateId(2))] + ); + assert_eq!(plan.nodes[0].retention_ms, Some(300_000)); + assert_eq!(plan.nodes[1].retention_ms, None); +} + +/// `m[5m]` and `m[1m]` both read the scan. Its candidate that is not +/// materialized (cost 1) may have one consumer, so the optimum reads the +/// scan's `OneEvaluation` candidate (cost 10) twice rather than both. +#[test] +fn a_query_time_result_that_is_not_materialized_has_one_consumer() { + let mut memo = empty_memo(); + let m = scan(&mut memo); + let last_5m = range(&mut memo, 300, m); + let last_1m = range(&mut memo, 60, m); + query(&mut memo, last_5m, None); + query(&mut memo, last_1m, None); + let first = own_candidate; + let physical = physical( + memo, + &[ + (first(m), NONE), + (first(last_5m), NONE), + (first(last_1m), NONE), + (first(m), ONE_EVALUATION), + ], + ); + let with_one_evaluation = costs(&physical, &[1.0, 1.0, 1.0, 10.0], &[]); + let solved = solve(&physical, &with_one_evaluation).unwrap(); + assert_eq!(solved.candidates, ids(&[1, 2, 3])); + + let without_one_evaluation = PhysicalASAPMemoCandidates::new( + physical.logical_candidates.clone(), + physical.physical_candidates[..3].to_vec(), + physical + .edges() + .map(|(_, edge)| edge) + .filter(|edge| edge.producer_candidate_id.0 < 3) + .collect(), + vec![], + ); + let only_unshared = costs(&without_one_evaluation, &[0.0, 1.0, 1.0], &[]); + assert_eq!( + solve(&without_one_evaluation, &only_unshared).unwrap_err(), + SelectionMilpError::Infeasible + ); +} + +/// Ga = `m offset 1s` and Gb = `m offset 2s`, each also computed from the +/// other (Ga② = Gb shifted by −1 s, Gb② = Ga shifted by 1 s). Reading each +/// other costs 1 and each own expression 10; without (E) both would read +/// the other and neither be computed. The optimum computes one from the +/// scan and the other from it. +#[test] +fn acyclicity_forbids_two_groups_reading_each_other() { + let mut memo = empty_memo(); + let m = scan(&mut memo); + let a = group(&mut memo, shift(1_000, m)); + let b = group(&mut memo, shift(2_000, m)); + let a_from_b = try_candidate(&mut memo, a, shift(-1_000, b), None).unwrap(); + let b_from_a = try_candidate(&mut memo, b, shift(1_000, a), None).unwrap(); + query(&mut memo, a, None); + query(&mut memo, b, None); + let first = own_candidate; + // v0 scan; v1 Ga①, v2 Gb①, v3 Ga②, v4 Gb②. + let physical = physical( + memo, + &[ + (first(m), ONE_EVALUATION), + (first(a), ONE_EVALUATION), + (first(b), ONE_EVALUATION), + (a_from_b, ONE_EVALUATION), + (b_from_a, ONE_EVALUATION), + ], + ); + let costs = costs(&physical, &[0.0, 10.0, 10.0, 1.0, 1.0], &[]); + let solved = solve(&physical, &costs).unwrap(); + assert!( + solved.candidates == ids(&[0, 1, 4]) || solved.candidates == ids(&[0, 2, 3]), + "{:?}", + solved.candidates + ); + let outputs = vec![(QueryId(0), a), (QueryId(1), b)]; + plan_of(&physical, &[], &solved, &outputs).unwrap(); +} diff --git a/crates/stage3-plan-selection/src/plan.rs b/crates/stage3-plan-selection/src/plan.rs new file mode 100644 index 000000000..9ca6a825e --- /dev/null +++ b/crates/stage3-plan-selection/src/plan.rs @@ -0,0 +1,132 @@ +//! The plan Stage 3 builds from the baseline MILP's optimum as its last +//! step (concepts doc, section 3, "Plan output"): one `PhysicalASAPDAG` +//! node per selected candidate, its inputs from the selected edges, each +//! node's `selected_physical_candidate` set. + +use std::collections::{BTreeMap, HashMap}; + +use asap_types::ir::QueryRoot; +use asap_types::plan::physical_asap_dag::{ + PhysicalASAPDAG, PhysicalASAPDAGValidationError, PhysicalASAPNodeId, +}; +use asap_types::stages::{ + MemoGroupId, PhysicalASAPMemoCandidates, PhysicalCandidateId, PhysicalEdgeId, +}; +use asap_types::workload::QueryWorkloadEntry; + +use crate::cost::{candidate_node, input_count, read_back_ms, retains_rows}; +use crate::milp::model::{QueryId, SolvedSelection}; + +/// The plan of `solved`, whose output `j` answers `outputs[j]` (a query and +/// the group its root, or one operator its scalar root, reads). Nodes are +/// numbered producers first. A node that retains rows (materialized, or a +/// query-time `Scan`) has `retention_ms` the most its selected consumers +/// read back ([`read_back_ms`] with the range-evaluation span), and at least the range-evaluation +/// span of a query it answers directly. +pub(crate) fn plan_of( + physical: &PhysicalASAPMemoCandidates, + entries: &[QueryWorkloadEntry], + solved: &SolvedSelection, + outputs: &[(QueryId, MemoGroupId)], +) -> Result { + let mut producer_of: HashMap<(PhysicalCandidateId, usize), PhysicalCandidateId> = + HashMap::new(); + let mut consumers_of: BTreeMap> = BTreeMap::new(); + for &id in &solved.edges { + let edge = physical.edge(id); + producer_of.insert( + (edge.consumer_candidate_id, edge.input_index_of_consumer), + edge.producer_candidate_id, + ); + consumers_of + .entry(edge.producer_candidate_id) + .or_default() + .push(id); + } + let consumers = |v: PhysicalCandidateId| consumers_of.get(&v).cloned().unwrap_or_default(); + let inputs = |v: PhysicalCandidateId| -> Vec { + (0..input_count(physical, v)) + .map(|i| producer_of[&(v, i)]) + .collect() + }; + // Producers first: Kahn's order over the selected edges, which (E) + // keeps acyclic. + let mut waiting: HashMap = solved + .candidates + .iter() + .map(|&v| (v, input_count(physical, v))) + .collect(); + let mut ready: Vec = solved + .candidates + .iter() + .rev() + .copied() + .filter(|v| waiting[v] == 0) + .collect(); + let mut node_of: HashMap = HashMap::new(); + let mut order = Vec::with_capacity(solved.candidates.len()); + while let Some(v) = ready.pop() { + node_of.insert(v, order.len()); + order.push(v); + for edge in consumers(v) { + let consumer = physical.edge(edge).consumer_candidate_id; + let left = waiting.get_mut(&consumer).expect("a selected consumer"); + *left -= 1; + if *left == 0 { + ready.push(consumer); + } + } + } + if order.len() != solved.candidates.len() { + return Err(PhysicalASAPDAGValidationError::Cycle); + } + // The span of the queries each candidate answers directly. + let mut output_span_ms: HashMap = HashMap::new(); + for (&(query, _), &v) in outputs.iter().zip(&solved.outputs) { + let span_ms = entries + .get(query.0) + .and_then(|entry| entry.range_evaluation) + .map_or(0, |range| range.span_ms); + let known = output_span_ms.entry(v).or_default(); + *known = (*known).max(span_ms); + } + let nodes = order + .iter() + .enumerate() + .map(|(id, &v)| { + let input_ids: Vec = + inputs(v).iter().map(|producer| node_of[producer]).collect(); + let mut node = candidate_node(physical, v, id, &input_ids); + if retains_rows(physical, v) { + node.retention_ms = + read_back_ms(physical, entries, v, &consumers(v), &consumers, true) + .map(|need_ms| need_ms.max(output_span_ms.get(&v).copied().unwrap_or(0))); + } + node + }) + .collect(); + let output_node: HashMap<(QueryId, MemoGroupId), PhysicalASAPNodeId> = outputs + .iter() + .zip(&solved.outputs) + .map(|(&output, v)| (output, node_of[v])) + .collect(); + let mut roots = Vec::new(); + let mut scalar_roots = Vec::new(); + for (query, memo_query) in physical + .logical_candidates + .memo + .queries() + .iter() + .enumerate() + { + let node = |group: &MemoGroupId| output_node[&(QueryId(query), *group)]; + match &memo_query.root { + QueryRoot::Operator(group) => roots.push(node(group)), + QueryRoot::Scalar(expression) => scalar_roots.push(( + query, + expression.map_operator_refs(&mut |group| node(group)), + )), + } + } + PhysicalASAPDAG::new(nodes, roots, scalar_roots) +} diff --git a/crates/types/src/ir/properties/execution_data_state.rs b/crates/types/src/ir/properties/execution_data_state.rs index 287e5035c..4e7585e87 100644 --- a/crates/types/src/ir/properties/execution_data_state.rs +++ b/crates/types/src/ir/properties/execution_data_state.rs @@ -36,6 +36,26 @@ pub enum DataPrimitive { } impl DataPrimitive { + /// What a node with `operator` and `result_kind` outputs: a summary + /// state (a summary operator's state or a maintained population, or any + /// `State` result) or raw values. + pub fn of( + operator: &crate::ir::Operator, + result_kind: crate::ir::OperatorResultKind, + ) -> Self { + use crate::ir::{ASAPOp, Operator, OperatorResultKind}; + match operator { + Operator::ASAP( + ASAPOp::SummaryAgg { .. } + | ASAPOp::SummaryTumblingWindowChain { .. } + | ASAPOp::SummaryJoin { .. } + | ASAPOp::MaintainPopulation { .. }, + ) => Self::SummaryState, + _ if result_kind == OperatorResultKind::State => Self::SummaryState, + _ => Self::Raw, + } + } + pub(crate) fn as_str(self) -> &'static str { match self { Self::Raw => "raw", diff --git a/crates/types/src/plan/materialized_view.rs b/crates/types/src/plan/materialized_view.rs index 5915bf633..e495eb040 100644 --- a/crates/types/src/plan/materialized_view.rs +++ b/crates/types/src/plan/materialized_view.rs @@ -1,16 +1,29 @@ //! What a deployment runtime keeps for an installed physical ASAP DAG: its //! materialized views ([`PhysicalASAPDAG::materialized_views`]) and how far -//! back it keeps each raw-data source for query-time scans +//! back it keeps each raw-data source for raw-data scans //! ([`PhysicalASAPDAG::raw_data_retention`]), derived from the DAG and the -//! `retention_ms` installation sets on its nodes -//! ([`PhysicalASAPDAG::set_retention`]). +//! `retention_ms` Stage 3 sets on its materialized nodes and query-time +//! `Scan`s, which these functions check. //! //! A **materialized view** is a node whose output is materialized across //! evaluations: its lifetime is `AcrossEvaluations`, at ingestion time or //! at query time, and it carries its `retention_ms`. A node materialized for //! `OneEvaluation` is not a view: its output is discarded after one -//! evaluation. A query-time `Scan` carries how long its source's rows are -//! retained for it (`None` when it reads them unbounded). +//! evaluation. +//! +//! ## The time-indexed store +//! +//! A runtime keeps every view, and the raw data of every raw-data `Scan`, +//! in one time-indexed, append-only store: a `SummaryTumblingWindowChain`'s +//! rows by the end of their window, any other view's state by the interval +//! of data it summarizes, raw rows by their time. Each is read only through +//! the `TimeRange` and `TimeShift` nodes above it, which select rows by time: +//! over a chain, `TimeRange(W)` over shifts summing to `o` selects, at +//! evaluation time `T`, the windows ending in `(T - o - W, T - o]`. A node's +//! `retention_ms` is how far back from the newest evaluation time its rows +//! are kept; it covers what those `TimeRange`s and `TimeShift`s above it +//! read back (`o + W`), which [`PhysicalASAPDAG::materialized_views`] and +//! [`PhysicalASAPDAG::raw_data_retention`] check, and older rows are dropped. //! //! ## Refreshing a view //! @@ -21,20 +34,18 @@ //! that view's node (for a view refreshed at ingestion, together with the //! rest of its refresh group, below), with each view it reads that the run //! does not refresh as a materialized view slot and raw data read by -//! `Source` at each `Scan`. It -//! refreshes the view in an ingestion run +//! `Source` at each `Scan`. It refreshes the view in an ingestion run //! ([`MaterializedViewRefresh::AtIngestion`]) or in the query run of the -//! evaluation that first reads it -//! ([`MaterializedViewRefresh::OnFirstRead`]), keeps the result, and binds -//! it the same way wherever the DAG reads that node. An ingestion run over +//! evaluation that first reads it ([`MaterializedViewRefresh::OnFirstRead`]) +//! and appends the result to the store. An ingestion run over //! `(window_start_ms, window_end_ms]` refreshes the windows ending at -//! `window_end_ms`: each `TimeRange(w)` keeps `(window_end_ms - w, -//! window_end_ms]`, whatever the shifts below it, so a runtime refreshes a -//! window `(start, end]` of `raw_data_windows` ([`RawDataWindow::bounds_ms`] -//! at the evaluation it is refreshed for) in the run over `(start, end]`. A -//! view refreshed in an ingestion run therefore reads windows that all end -//! at one offset before the evaluation; a DAG with one that does not is -//! refused ([`InstallableDagError::IngestionBuildReadsWindowsOfDifferentOffsets`]). +//! `window_end_ms`: a chain builds its window ending there, each +//! `TimeRange(w)` keeps `(window_end_ms - w, window_end_ms]` whatever the +//! shifts below it, and each view it reads is read for the window ending +//! there. A view refreshed in an ingestion run therefore reads windows that +//! all end at one offset before the evaluation; a DAG with one that does +//! not is refused +//! ([`InstallableDagError::IngestionBuildReadsWindowsOfDifferentOffsets`]). //! A query run binds the views it reaches from the roots without passing //! another view; a view only other views read is read through them. //! @@ -44,17 +55,14 @@ //! ([`PhysicalASAPDAG::ingestion_refresh_groups`]): the views whose windows //! have one width `W` and end at one offset `p` modulo `W` (their //! [`RefreshWindow`]; a view with an unbounded window is a group of its -//! own). A runtime refreshes a group with one ingestion run per window -//! `(e - W, e]`, `e ≡ p (mod W)`. The run's roots are the views of the group -//! whose state for the window ending at `e` is not stored yet: in steady -//! state the newest tumbling window of each chain, since older tumbling windows are the same -//! states, stored by earlier runs. A view of the group that a root reads is -//! computed in the run; views of other groups are materialized view slots. -//! A node several roots share runs once per run, so a `Scan` below a KLL -//! tumbling-window chain and a DDSketch tumbling-window chain of equal tumbling-window width reads each -//! tumbling window's raw data once. Stage 3's cost assumes this grouping; a runtime -//! that splits a group computes the same states at a higher cost than -//! priced. +//! own). A chain's window is one of its tumbling windows. A runtime +//! refreshes a group with one ingestion run per window `(e - W, e]`, `e ≡ p +//! (mod W)`, whose roots are the views of the group; a view of the group a +//! root reads is computed in the run, and views of other groups are +//! materialized view slots. A node several roots share runs once per run, +//! so a `Scan` below two chains of equal width reads each window's raw data +//! once. Stage 3's cost assumes this grouping; a runtime that splits a group +//! computes the same states at a higher cost than priced. //! //! Each view's `coverage` says what its stored state is: a stable id of its //! definition (what it computes) and its selection (which rows went in, @@ -93,25 +101,26 @@ pub struct MaterializedView { /// What its refresh reads. pub inputs: MaterializedViewInputs, /// The node's `retention_ms`: how far back from the newest evaluation - /// time its output must stay available once refreshed. At least how - /// far back the data it summarizes reaches (its raw-data windows', and - /// the views it reads, `end_offset_ms + width_ms`). + /// time its stored rows are kept. At least how far back the + /// `TimeRange`s and `TimeShift`s above it read. pub(crate) retention_ms: u64, } -#[cfg(any(test, feature = "test-support"))] impl MaterializedView { /// Which run refreshes it. pub fn refresh(&self) -> MaterializedViewRefresh { self.refresh } - /// How far back from the newest evaluation time its output stays - /// available. + /// How far back from the newest evaluation time its stored rows are + /// kept. pub fn retention_ms(&self) -> u64 { self.retention_ms } +} +#[cfg(any(test, feature = "test-support"))] +impl MaterializedView { /// What the stored state is. pub fn coverage(&self) -> &ViewCoverage { &self.coverage @@ -184,15 +193,9 @@ impl RawDataWindow { } } +#[cfg(any(test, feature = "test-support"))] impl RawDataWindow { - /// How far back from the evaluation time the window reaches: - /// `end_offset_ms + width_ms`. - pub(crate) fn lookback_ms(&self) -> u64 { - u64::try_from(self.end_offset_ms.saturating_add(self.width_ms)).unwrap_or(0) - } - /// `(start, end]` of the window at evaluation time `evaluation_time_ms`. - #[cfg(any(test, feature = "test-support"))] pub fn bounds_ms(&self, evaluation_time_ms: i64) -> (i64, i64) { let end = evaluation_time_ms - self.end_offset_ms; (end - self.width_ms, end) @@ -246,10 +249,8 @@ impl RefreshWindow { /// The views refreshed at ingestion that share one refresh window /// ([`PhysicalASAPDAG::ingestion_refresh_groups`]). A runtime refreshes -/// them with one ingestion run per window `(e - W, e]`; each run's roots are -/// the views whose state for the window ending at `e` is not stored yet (in -/// steady state the newest tumbling window of each chain), and a view of the group a -/// root reads is computed in the run. +/// them with one ingestion run per window `(e - W, e]`, whose roots are its +/// views; a view of the group a root reads is computed in the run. #[derive(Debug, Clone, PartialEq)] pub struct IngestionRefreshGroup { /// The refresh window of every view of the group; `None` for a view @@ -260,13 +261,13 @@ pub struct IngestionRefreshGroup { } /// How far back from the evaluation time a runtime keeps a raw-data source -/// for the DAG's query-time scans ([`PhysicalASAPDAG::raw_data_retention`]). +/// for the DAG's raw-data scans ([`PhysicalASAPDAG::raw_data_retention`]). #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct RawDataRetention { /// The source. pub(crate) source: Source, - /// The longest `retention_ms` of a query-time `Scan` of it; `None` when + /// The longest `retention_ms` of a raw-data `Scan` of it; `None` when /// one reads it unbounded (every row the source holds). pub(crate) lookback_ms: Option, } @@ -285,7 +286,7 @@ impl RawDataRetention { &self.source } - /// The longest `retention_ms` of a query-time `Scan` of it; `None` when + /// The longest `retention_ms` of a raw-data `Scan` of it; `None` when /// one reads it unbounded. pub fn lookback_ms(&self) -> Option { self.lookback_ms @@ -344,15 +345,15 @@ pub enum InstallableDagError { #[error("materialized view {node_id} has no retention_ms")] ViewWithoutRetention { node_id: PhysicalASAPNodeId }, /// Bad input (a plan file): view `node_id` is retained for less than the - /// data it summarizes reaches back, so a later evaluation would miss - /// it. - #[error("materialized view {node_id} is retained for less than its data reaches back")] + /// `TimeRange`s and `TimeShift`s above it read back, so a later + /// evaluation would miss its rows. + #[error("materialized view {node_id} is retained for less than its consumers read back")] ViewRetentionTooShort { node_id: PhysicalASAPNodeId }, - /// Bad input (a plan file): query-time `Scan` `node_id` reads a bounded + /// Bad input (a plan file): raw-data `Scan` `node_id` reads a bounded /// window but has no `retention_ms`. #[error("query-time scan {node_id} reads a bounded window but has no retention_ms")] ScanWithoutRetention { node_id: PhysicalASAPNodeId }, - /// Bad input (a plan file): query-time `Scan` `node_id` keeps its rows + /// Bad input (a plan file): raw-data `Scan` `node_id` keeps its rows /// for less than its consumers' window reaches back. #[error("query-time scan {node_id} is retained for less than its window reaches back")] ScanRetentionTooShort { node_id: PhysicalASAPNodeId }, @@ -363,29 +364,25 @@ impl PhysicalASAPDAG { /// it reads), with its refresh mode, coverage, inputs and the node's /// `retention_ms`. Validates the DAG and checks that every raw-data /// window it reads has a supported shape and that each view's retention - /// covers how far back its data reaches. O(views · nodes) for the - /// coverages, plus O(nodes + edges). + /// covers what the `TimeRange`s and `TimeShift`s above it read back. + /// O(views · nodes) for the inputs, plus O(nodes + edges). pub fn materialized_views(&self) -> Result, InstallableDagError> { if let Some(node) = self.nodes.iter().find(|node| { - matches!(node.lifetime, Lifetime::AcrossEvaluations(_)) && node.retention_ms.is_none() + matches!(node.lifetime, Lifetime::AcrossEvaluations(_)) + && !is_raw_data_scan(node) + && node.retention_ms.is_none() }) { return Err(InstallableDagError::ViewWithoutRetention { node_id: node.id }); } self.validate()?; - let views: Vec = self - .views_with_node_retention()? - .into_iter() - .map(|(view, _)| view) - .collect(); - let mut reach_ms_by_node = HashMap::new(); + let index = ViewIndex::new(self); + let views = index.views()?; for view in &views { - let reach_ms = reach_ms_of(&view.inputs, &reach_ms_by_node); - if view.retention_ms < reach_ms { + if view.retention_ms < index.read_back_ms(view.node_id).0 { return Err(InstallableDagError::ViewRetentionTooShort { node_id: view.node_id, }); } - reach_ms_by_node.insert(view.node_id, reach_ms); } Ok(views) } @@ -474,12 +471,12 @@ impl PhysicalASAPDAG { } /// The refresh window of `node` ([`RefreshWindow::of_window`]): for a - /// `SummaryTumblingWindowChain` (a DAG with its chains unexpanded), its newest tumbling window's, - /// `(tumbling_window_width, s mod tumbling_window_width)` with `s` the sum of the offsets of - /// the `TimeShift`s below it; for any other node, that of its window - /// (the windows below it, a chain below it counting as all its tumbling - /// windows). A chain and each of its expanded tumbling windows have the same one. - /// `None` when the window is unbounded or empty. O(nodes + edges). + /// `SummaryTumblingWindowChain` of width `w`, `(w, s mod w)` with `s` + /// the sum of the offsets of the `TimeShift`s below it; for any other + /// node, that of the smallest window holding the windows below it (each + /// `TimeRange`'s, and each chain's reached without passing one). `None` + /// when that window is unbounded (none below it, or an `@` anchor below + /// a `TimeRange`) or empty. O(nodes + edges). pub fn refresh_window(&self, node: PhysicalASAPNodeId) -> Option { self.refresh_windows([node])[&node] } @@ -490,161 +487,102 @@ impl PhysicalASAPDAG { &self, nodes: impl IntoIterator, ) -> HashMap> { - let _ = nodes.into_iter(); - todo!("#1207 PR 6: a node's refresh window from the TimeRange and TimeShift nodes above its inputs") - } - - /// Sets `retention_ms` on every materialized view and every query-time - /// `Scan` of an installed DAG (tumbling-window chains expanded), and returns the - /// views ([`Self::materialized_views`]). - /// - /// A view takes `tumbling_window_retention_ms_by_node`'s value (Stage 3's retention - /// of a tumbling window's chain: its offset plus its window plus the longest - /// range-evaluation span of its consumers); otherwise how far back its - /// data reaches, plus, for a view built from other views, its consumers' - /// longest range-evaluation span (`reader_span_ms_by_node`). Then each - /// view is retained at least as long as every view built from it needs it: - /// as long as that view's own retention when it is refreshed on first - /// read (a range request may refresh it for its oldest step), else as - /// far back as that view's data reaches. This is each node's own - /// requirement: a runtime that stores identical outputs of several - /// nodes once keeps them for the largest requirement among those nodes. - /// - /// A query-time `Scan` keeps its rows for its consumers' window - /// (how far back they read) plus their longest - /// range-evaluation span, and at least the retention of every view - /// refreshed on first read from it (which may be refreshed for its - /// oldest step); `None` when its window is unbounded. In an installed - /// DAG a chain materialized across evaluations is its expanded tumbling windows, so its - /// scan keeps the reach of all of them: reusing a tumbling window refreshed at an - /// earlier evaluation is optional, and a restart or an older range step - /// refreshes older tumbling windows from raw data. (Stage 3 prices a materialized chain on - /// the unexpanded DAG as reading its newest tumbling window.) - pub fn set_retention( - &mut self, - tumbling_window_retention_ms_by_node: &HashMap, - reader_span_ms_by_node: &HashMap, - ) -> Result, InstallableDagError> { - let _ = (tumbling_window_retention_ms_by_node, reader_span_ms_by_node); - todo!("#1207 PR 6: each materialized node's retention_ms is its h_v, which Stage 3 sets") + let index = ViewIndex::new(self); + nodes + .into_iter() + .map(|node| { + let window = index.windows_below(node).and_then(|windows| { + windows + .into_iter() + .reduce(|(a_end, a_width), (b_end, b_width)| { + let end = a_end.min(b_end); + let start_back = a_end + .saturating_add(a_width) + .max(b_end.saturating_add(b_width)); + (end, start_back.saturating_sub(end)) + }) + }); + let refresh_window = window.and_then(|(end_offset_ms, width_ms)| { + RefreshWindow::of_window(end_offset_ms, width_ms) + }); + (node, refresh_window) + }) + .collect() } - /// For each raw-data source some query-time `Scan` reads, in the order - /// of its first such `Scan`, the longest `retention_ms` among those - /// scans, `None` when one reads it unbounded: the raw data a runtime - /// keeps for the DAG's queries, the raw-data counterpart of each view's - /// retention. A source read only at ingestion time is absent. Checks - /// that each scan reading a bounded window has a - /// retention covering it. + /// For each raw-data source some raw-data `Scan` (at query time, or + /// materialized at ingestion time) reads, in the order of its first such + /// `Scan`, the longest `retention_ms` among those + /// scans, `None` when one reads it unbounded (a consumer reads it + /// outside a `TimeRange` or chain): the raw data a runtime keeps for the + /// DAG's queries, the raw-data counterpart of each view's retention. A + /// source read only at ingestion time is absent. Checks that each scan + /// read through windows has a retention covering what the `TimeRange`s + /// and `TimeShift`s above it read back. pub fn raw_data_retention(&self) -> Result, InstallableDagError> { - todo!("#1207 PR 6: each source's retention from its Scan nodes' retention_ms") + let index = ViewIndex::new(self); + let mut retention: Vec = Vec::new(); + for node in self.nodes.iter().filter(|node| is_raw_data_scan(node)) { + let Operator::NonASAP(NonASAPOp::Scan { source, .. }) = &node.payload else { + continue; + }; + let (read_back_ms, read_outside_windows) = index.read_back_ms(node.id); + let lookback_ms = match (read_outside_windows, node.retention_ms) { + (true, _) => None, + (false, Some(kept_ms)) if kept_ms >= read_back_ms => Some(kept_ms), + (false, Some(_)) => { + return Err(InstallableDagError::ScanRetentionTooShort { node_id: node.id }) + } + (false, None) => { + return Err(InstallableDagError::ScanWithoutRetention { node_id: node.id }) + } + }; + match retention + .iter_mut() + .find(|retained| &retained.source == source) + { + Some(retained) => { + retained.lookback_ms = + retained.lookback_ms.zip(lookback_ms).map(|(a, b)| a.max(b)) + } + None => retention.push(RawDataRetention { + source: source.clone(), + lookback_ms, + }), + } + } + Ok(retention) } /// Checks that every raw-data window the DAG reads (each `TimeRange`, - /// and each `SummaryTumblingWindowChain`'s tumbling window, reading a `Scan` without passing another - /// `TimeRange`) has a supported shape + /// and each `SummaryTumblingWindowChain`'s tumbling window, reading a + /// `Scan` without passing another `TimeRange`) has a supported shape /// ([`InstallableDagError::UnsupportedRawDataWindow`]). O(nodes + edges) /// plus the depth below each window. pub fn check_raw_data_windows(&self) -> Result<(), InstallableDagError> { ViewIndex::new(self).check_raw_data_windows() } - - /// The views, each with the node's `retention_ms` (0 when unset) and the - /// `Scan`s its refresh reads through its raw-data windows. - fn views_with_node_retention( - &self, - ) -> Result)>, InstallableDagError> { - let index = ViewIndex::new(self); - index.check_raw_data_windows()?; - let views: HashSet = self - .nodes - .iter() - .filter(|node| matches!(node.lifetime, Lifetime::AcrossEvaluations(_))) - .map(|node| node.id) - .collect(); - let mut derived = Vec::new(); - for &id in index.children_first.iter().filter(|id| views.contains(id)) { - let node = index.nodes[&id]; - let (inputs, scans) = index.inputs_of_view(id, &views)?; - let refreshed_at_ingestion = node.output_state.timing == ExecutionTiming::IngestionTime; - if refreshed_at_ingestion { - // An ingestion run reads each view for the window ending at - // its end, so a view read must end where the raw windows do. - if !inputs.materialized_views.is_empty() { - todo!("#1207 PR 6: the window a materialized view it reads ends at, from that node's time constraints") - } - let mut end_offsets_ms: Vec = inputs - .raw_data_windows - .iter() - .map(|window| window.end_offset_ms) - .collect(); - end_offsets_ms.sort_unstable(); - end_offsets_ms.dedup(); - if end_offsets_ms.len() > 1 { - return Err( - InstallableDagError::IngestionBuildReadsWindowsOfDifferentOffsets { - node_id: id, - end_offsets_ms, - }, - ); - } - } - let coverage = node.view_coverage.clone().ok_or_else(|| { - InstallableDagError::ViewWithoutCoverage { - node_id: id, - reason: "the plan node carries no view_coverage".into(), - } - })?; - let view = MaterializedView { - node_id: id, - refresh: if refreshed_at_ingestion { - MaterializedViewRefresh::AtIngestion - } else { - MaterializedViewRefresh::OnFirstRead - }, - coverage, - inputs, - retention_ms: node.retention_ms.unwrap_or(0), - }; - derived.push((view, scans)); - } - Ok(derived) - } } -/// How far back from the evaluation time the data a view refreshed from -/// `inputs` summarizes reaches: the farthest of its raw-data windows and of -/// the views it reads (`reach_ms_by_node`, each already computed). -/// Whether `node` is a `Scan` run at query time, which carries how long its -/// source's rows are retained for it. -pub(crate) fn is_query_time_scan(node: &PhysicalASAPDAGNode) -> bool { +/// Whether `node` is a `Scan` whose raw data is kept for its consumers to +/// read back: one run at query time, or materialized across evaluations +/// at ingestion time. It carries how long its source's rows are retained +/// for it, and is no view: raw data is stored by its `Source`. +pub(crate) fn is_raw_data_scan(node: &PhysicalASAPDAGNode) -> bool { matches!(node.payload, Operator::NonASAP(NonASAPOp::Scan { .. })) - && node.output_state.timing == ExecutionTiming::QueryTime -} - -fn reach_ms_of( - inputs: &MaterializedViewInputs, - reach_ms_by_node: &HashMap, -) -> u64 { - let windows = inputs - .raw_data_windows - .iter() - .map(RawDataWindow::lookback_ms); - let views = inputs - .materialized_views - .iter() - .map(|id| reach_ms_by_node.get(id).copied().unwrap_or(0)); - windows.chain(views).max().unwrap_or(0) + && (node.output_state.timing == ExecutionTiming::QueryTime + || matches!(node.lifetime, Lifetime::AcrossEvaluations(_))) } -/// The nodes of a DAG by id, each node's inputs in edge order, and two -/// facts every raw-data window check reads, computed in one pass each over -/// the DAG (O(nodes + edges)); it walks down from each window to check its -/// shape. +/// The nodes of a DAG by id, each node's inputs and consumers in edge +/// order, and two facts every raw-data window check reads, computed in one +/// pass each over the DAG (O(nodes + edges)); it walks down from each +/// window to check its shape. struct ViewIndex<'a> { dag: &'a PhysicalASAPDAG, nodes: HashMap, inputs: HashMap>, + consumers: HashMap>, /// Every node after its inputs, from the DAG's node order. children_first: Vec, /// The nodes whose rows come from raw data: a `Scan` is reachable below @@ -671,8 +609,13 @@ impl<'a> ViewIndex<'a> { fn new(dag: &'a PhysicalASAPDAG) -> Self { let nodes: HashMap<_, _> = dag.nodes.iter().map(|n| (n.id, n)).collect(); let mut inputs: HashMap> = HashMap::new(); + let mut consumers: HashMap> = HashMap::new(); for edge in &dag.edges { inputs.entry(edge.consumer).or_default().push(edge.producer); + consumers + .entry(edge.producer) + .or_default() + .push(edge.consumer); } // Every node after its inputs: an iterative depth-first postorder. let mut children_first = Vec::with_capacity(dag.nodes.len()); @@ -719,6 +662,7 @@ impl<'a> ViewIndex<'a> { dag, nodes, inputs, + consumers, children_first, reading_raw_data, re_timed_from_above, @@ -739,6 +683,155 @@ impl<'a> ViewIndex<'a> { } } + /// Every materialized view, in dependency order, with the node's + /// `retention_ms` (0 when unset), after checking the DAG's raw-data + /// windows. + fn views(&self) -> Result, InstallableDagError> { + self.check_raw_data_windows()?; + let views: HashSet = self + .dag + .nodes + .iter() + .filter(|node| { + matches!(node.lifetime, Lifetime::AcrossEvaluations(_)) && !is_raw_data_scan(node) + }) + .map(|node| node.id) + .collect(); + let mut derived = Vec::new(); + for &id in self.children_first.iter().filter(|id| views.contains(id)) { + let node = self.nodes[&id]; + let inputs = self.inputs_of_view(id, &views)?; + let refreshed_at_ingestion = node.output_state.timing == ExecutionTiming::IngestionTime; + if refreshed_at_ingestion { + // An ingestion run builds the windows ending at its end, so + // every window the view reads must end at one offset. + let mut end_offsets_ms: Vec = self + .windows_below(id) + .unwrap_or_default() + .into_iter() + .map(|(end_offset_ms, _)| end_offset_ms) + .collect(); + end_offsets_ms.sort_unstable(); + end_offsets_ms.dedup(); + if end_offsets_ms.len() > 1 { + return Err( + InstallableDagError::IngestionBuildReadsWindowsOfDifferentOffsets { + node_id: id, + end_offsets_ms, + }, + ); + } + } + let coverage = node.view_coverage.clone().ok_or_else(|| { + InstallableDagError::ViewWithoutCoverage { + node_id: id, + reason: "the plan node carries no view_coverage".into(), + } + })?; + derived.push(MaterializedView { + node_id: id, + refresh: if refreshed_at_ingestion { + MaterializedViewRefresh::AtIngestion + } else { + MaterializedViewRefresh::OnFirstRead + }, + coverage, + inputs, + retention_ms: node.retention_ms.unwrap_or(0), + }); + } + Ok(derived) + } + + /// The windows `(end_offset_ms, width_ms)` back from the evaluation + /// time that `node` reads: a chain of width `w` over `TimeShift`s + /// summing to `s` its own `(s, w)`; any other node each `TimeRange(w)` + /// below it over shifts summing to `s`, `(s, w)`, and each chain below + /// it reached without passing a `TimeRange`. `None` when one is read + /// through an `@` anchor below its `TimeRange` or chain. + fn windows_below(&self, node: PhysicalASAPNodeId) -> Option> { + let mut windows = Vec::new(); + let mut seen = HashSet::new(); + let mut pending = vec![node]; + while let Some(id) = pending.pop() { + if !seen.insert(id) { + continue; + } + let width_ms = match &self.nodes.get(&id)?.payload { + Operator::NonASAP(NonASAPOp::TimeRange { range, .. }) => { + i64::try_from(range.as_millis()).ok()? + } + Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { + tumbling_window_width_ms, + .. + }) => i64::try_from(*tumbling_window_width_ms).ok()?, + _ => { + pending.extend(self.inputs.get(&id).into_iter().flatten().copied()); + continue; + } + }; + windows.push((self.offset_below(id)?, width_ms)); + } + Some(windows) + } + + /// The sum of the offsets of the `TimeShift`s right below `node`; + /// `None` when one has an `@` anchor. + fn offset_below(&self, node: PhysicalASAPNodeId) -> Option { + let mut end_offset_ms = 0i64; + let mut current = node; + while let Some(input) = self.only_input(current) { + let Some(NonASAPOp::TimeShift { shift, .. }) = self.relational(input) else { + break; + }; + if shift.at.is_some() { + return None; + } + end_offset_ms = end_offset_ms.saturating_add(shift.offset_ms); + current = input; + } + Some(end_offset_ms) + } + + /// How far back from the evaluation time the consumers of `node` read + /// it: through `TimeShift`s (without an `@` anchor) summing to `o`, a + /// `TimeRange(r)` reads `o + r` back, a chain of width `w` `o + w`, and + /// any other consumer `o`; the most of these, and whether a consumer + /// other than a `TimeRange` or chain (or a root) reads it, outside a + /// window. + fn read_back_ms(&self, node: PhysicalASAPNodeId) -> (u64, bool) { + let mut read_back_ms = 0u64; + let mut read_outside_windows = false; + let mut pending = vec![(node, 0i64)]; + while let Some((id, offset_ms)) = pending.pop() { + if self.dag.roots.contains(&id) { + read_outside_windows = true; + } + for &consumer in self.consumers.get(&id).into_iter().flatten() { + let width_ms = match &self.nodes[&consumer].payload { + Operator::NonASAP(NonASAPOp::TimeShift { shift, .. }) if shift.at.is_none() => { + pending.push((consumer, offset_ms.saturating_add(shift.offset_ms))); + continue; + } + Operator::NonASAP(NonASAPOp::TimeRange { range, .. }) => { + u64::try_from(range.as_millis()).unwrap_or(u64::MAX) + } + Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { + tumbling_window_width_ms, + .. + }) => *tumbling_window_width_ms, + _ => { + read_outside_windows = true; + 0 + } + }; + let offset_ms = u64::try_from(offset_ms).unwrap_or(0); + read_back_ms = read_back_ms.max(offset_ms.saturating_add(width_ms)); + } + } + (read_back_ms, read_outside_windows) + } + /// See [`PhysicalASAPDAG::check_raw_data_windows`]. fn check_raw_data_windows(&self) -> Result<(), InstallableDagError> { for node in &self.dag.nodes { @@ -761,12 +854,11 @@ impl<'a> ViewIndex<'a> { Ok(()) } - /// The raw-data window read through the `TimeRange` `time_range` and - /// the `Scan` it reads, or why its shape is unsupported. + /// The raw-data window read through the `TimeRange` `time_range`, or why its shape is unsupported. fn window_at( &self, time_range: PhysicalASAPNodeId, - ) -> Result<(RawDataWindow, PhysicalASAPNodeId), InstallableDagError> { + ) -> Result { let Some(NonASAPOp::TimeRange { range, .. }) = self.relational(time_range) else { return Err(InstallableDagError::UnsupportedRawDataWindow { time_range_node_id: time_range, @@ -777,19 +869,25 @@ impl<'a> ViewIndex<'a> { } /// The window of width `width_ms` that `time_range` (a `TimeRange`, or - /// a `SummaryTumblingWindowChain`'s tumbling window) reads from the `Scan` below it, and that - /// `Scan`, or why its shape is unsupported. It does not allocate or - /// compile every tumbling window in a chain. + /// a `SummaryTumblingWindowChain`'s tumbling window) reads from the + /// `Scan` below it, or why its shape is unsupported. A + /// `TimeRange` run at ingestion time may have no `TimeShift` or subquery + /// above it; a query run evaluates one, and a chain's windows are + /// selected by the `TimeRange`s above it. fn raw_window_below( &self, time_range: PhysicalASAPNodeId, width_ms: u128, - ) -> Result<(RawDataWindow, PhysicalASAPNodeId), InstallableDagError> { + ) -> Result { let unsupported = |reason| InstallableDagError::UnsupportedRawDataWindow { time_range_node_id: time_range, reason, }; - if self.re_timed_from_above.contains(&time_range) { + let node = self.nodes[&time_range]; + if self.re_timed_from_above.contains(&time_range) + && node.output_state.timing == ExecutionTiming::IngestionTime + && matches!(node.payload, Operator::NonASAP(NonASAPOp::TimeRange { .. })) + { return Err(unsupported("a subquery or a TimeShift above the range")); } let width_ms = @@ -818,7 +916,7 @@ impl<'a> ViewIndex<'a> { end_offset_ms, width_ms, }; - return Ok((window, current)); + return Ok(window); } _ => { return Err(unsupported( @@ -829,14 +927,28 @@ impl<'a> ViewIndex<'a> { } } - /// What view `node`'s refresh reads: below it, each view (`views`) and - /// each raw-data `TimeRange` it reaches without passing another of - /// them, in node order, and the `Scan` below each `TimeRange`. + /// What view `node`'s refresh reads: a chain reading raw data, the + /// window of its tumbling window; any other view, below it, each view + /// (`views`) and each raw-data `TimeRange` it reaches without passing + /// another of them, in node order. fn inputs_of_view( &self, node: PhysicalASAPNodeId, views: &HashSet, - ) -> Result<(MaterializedViewInputs, Vec), InstallableDagError> { + ) -> Result { + if let Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { + tumbling_window_width_ms, + .. + }) = &self.nodes[&node].payload + { + if self.reading_raw_data.contains(&node) { + let window = self.raw_window_below(node, u128::from(*tumbling_window_width_ms))?; + return Ok(MaterializedViewInputs { + raw_data_windows: vec![window], + materialized_views: Vec::new(), + }); + } + } let mut raw_ranges = Vec::new(); let mut view_inputs = Vec::new(); let mut seen = HashSet::new(); @@ -868,16 +980,13 @@ impl<'a> ViewIndex<'a> { } raw_ranges.sort_unstable(); view_inputs.sort_unstable(); - let (raw_data_windows, scans) = raw_ranges + let raw_data_windows = raw_ranges .into_iter() .map(|range| self.window_at(range)) - .collect::, _>>()? - .into_iter() - .unzip(); - let inputs = MaterializedViewInputs { + .collect::, _>>()?; + Ok(MaterializedViewInputs { raw_data_windows, materialized_views: view_inputs, - }; - Ok((inputs, scans)) + }) } } diff --git a/crates/types/src/plan/physical_asap_dag.rs b/crates/types/src/plan/physical_asap_dag.rs index 91a5c4243..104ce2588 100644 --- a/crates/types/src/plan/physical_asap_dag.rs +++ b/crates/types/src/plan/physical_asap_dag.rs @@ -75,19 +75,20 @@ pub struct PhysicalASAPDAGNode { /// with no assignment has lifetime `None`. With `output_state.timing` it /// is the node's `PhysicalProperties`. pub lifetime: Lifetime, - /// θ of a summary build (`SummaryAgg`, `SummaryTumblingWindowChain`): its - /// candidate's `LogicalProperties::parameters`, which the executor builds - /// the sketch from. `None` on every other node and for an exact build. + /// θ of a sketch state node: a build's (`SummaryAgg`, + /// `SummaryTumblingWindowChain`), which the executor builds the sketch + /// from, or, on a `SummaryMerge` or a `Filter`, `TimeRange` or + /// `TimeShift` over sketch states, the θ of the states it reads: its + /// candidate's `LogicalProperties::parameters`. `None` on every other + /// node and for exact states. #[serde(default, skip_serializing_if = "Option::is_none")] pub parameters: Option, pub output_schema: Schema, /// For a materialized view (a node whose lifetime materializes its output /// across evaluations), how far back from the newest evaluation - /// time its output must stay available once refreshed; for a query-time - /// `Scan`, how far back its source's rows must stay available (`None` - /// when it reads them unbounded). `None` on every other node, and on - /// every node of a DAG exported before installation sets it - /// ([`PhysicalASAPDAG::set_retention`]). A + /// time its stored rows are kept; for a raw-data `Scan` (at query time or materialized), how far back + /// its source's rows are kept (`None` when it reads them unbounded): + /// $h_v$, which Stage 3 sets. `None` on every other node. A /// node field rather than part of [`Lifetime`], because Stage 2 /// enumerates lifetimes before Stage 3 knows retention. #[serde(default, skip_serializing_if = "Option::is_none")] @@ -132,6 +133,14 @@ pub struct ViewCoverage { #[cfg(any(test, feature = "test-support"))] impl ViewCoverage { + /// The coverage of definition `definition_id` over `selection`. + pub fn new(definition_id: String, selection: Vec) -> Self { + Self { + definition_id, + selection, + } + } + /// The plan-scope content id of what the view computes. pub fn definition_id(&self) -> &str { &self.definition_id @@ -241,7 +250,7 @@ pub enum PhysicalASAPDAGValidationError { consumer: PhysicalASAPNodeId, }, #[error( - "node {0:?} has a retention_ms but is neither a materialized view nor a query-time scan" + "node {0:?} has a retention_ms but is neither a materialized view nor a raw-data scan" )] UnexpectedRetention(PhysicalASAPNodeId), #[error("duplicate physical ASAP node id {0:?}")] @@ -363,7 +372,7 @@ impl PhysicalASAPDAG { } if node.retention_ms.is_some() && !matches!(node.lifetime, Lifetime::AcrossEvaluations(_)) - && !super::materialized_view::is_query_time_scan(node) + && !super::materialized_view::is_raw_data_scan(node) { return Err(PhysicalASAPDAGValidationError::UnexpectedRetention(node.id)); } @@ -588,6 +597,46 @@ pub(crate) fn is_materializable_state_output(operator: &Operator) -> bool ) } +impl PhysicalASAPDAG { + /// The DAG of `nodes` (each after its inputs), with one edge per input + /// of each node's payload ([`edge_roles`]), carrying its producer's + /// schema and data state; `roots` and `scalar_roots` as the fields say. + /// Validated. Stage 3 builds its plan this way. + pub fn new( + nodes: Vec, + roots: Vec, + scalar_roots: Vec<(usize, ScalarExpr)>, + ) -> Result { + let by_id: HashMap = + nodes.iter().map(|node| (node.id, node)).collect(); + let mut edges = Vec::new(); + for node in &nodes { + for (producer, role) in edge_roles(&node.payload) { + let input = by_id.get(&producer).ok_or( + PhysicalASAPDAGValidationError::MissingEdgeEndpoint(producer), + )?; + edges.push(PhysicalASAPDAGEdge { + producer, + consumer: node.id, + role, + intermediate_schema: input.output_schema.clone(), + data_state: input.output_state, + grouping: grouping_compatibility(&input.payload, &node.payload), + window: WindowEdgeCompatibility::NotApplicable, + }); + } + } + let dag = Self { + nodes, + edges, + roots, + scalar_roots, + }; + dag.validate()?; + Ok(dag) + } +} + /// Test-only: `roots` exported as one DAG with every node at query time and /// not materialized, and no θ (every `parameters` is `None`), with each /// exported node's operator node by node id. Executor tests hand-build DAGs @@ -612,57 +661,31 @@ pub(crate) fn physical_asap_dag_at_query_time_with_node_ids( } }) .collect(); - let mut nodes: Vec = Vec::with_capacity(flat.nodes.len()); - let mut edges = Vec::new(); - for (id, flat_node) in flat.nodes.into_iter().enumerate() { - let node = &nodes_by_id[id]; - let primitive = match &node.operator { - Operator::ASAP(op) if op.produced_state().is_some() => DataPrimitive::SummaryState, - Operator::ASAP(ASAPOp::MaintainPopulation { .. }) => DataPrimitive::SummaryState, - _ if node.result_kind == crate::ir::OperatorResultKind::State => { - DataPrimitive::SummaryState - } - _ => DataPrimitive::Raw, - }; - let output_state = ExecutionDataState { - timing: ExecutionTiming::QueryTime, - primitive, - }; - // Children come before their parents, so every producer is already in `nodes`. - for (producer, role) in edge_roles(&flat_node.operator) { - edges.push(PhysicalASAPDAGEdge { - producer, - consumer: id, - role, - intermediate_schema: nodes[producer].output_schema.clone(), - data_state: nodes[producer].output_state, - grouping: grouping_compatibility(&nodes[producer].payload, &flat_node.operator), - window: WindowEdgeCompatibility::NotApplicable, - }); - } - nodes.push(PhysicalASAPDAGNode { + let nodes = flat + .nodes + .into_iter() + .enumerate() + .map(|(id, flat_node)| PhysicalASAPDAGNode { id, + output_state: ExecutionDataState { + timing: ExecutionTiming::QueryTime, + primitive: DataPrimitive::of(&flat_node.operator, nodes_by_id[id].result_kind), + }, payload: flat_node.operator, - output_state, lifetime: Lifetime::None, parameters: None, output_schema: flat_node.schema, retention_ms: None, selected_physical_candidate: None, view_coverage: None, - }); - } - let dag = PhysicalASAPDAG { - nodes, - edges, - roots, - scalar_roots, - }; - dag.validate()?; - Ok((dag, nodes_by_id)) + }) + .collect(); + Ok(( + PhysicalASAPDAG::new(nodes, roots, scalar_roots)?, + nodes_by_id, + )) } -#[cfg(any(test, feature = "test-support"))] fn grouping_compatibility( producer: &Operator, consumer: &Operator, diff --git a/crates/types/src/stages/physical/memo_candidates/mod.rs b/crates/types/src/stages/physical/memo_candidates/mod.rs index 79fabb86f..2d07acad9 100644 --- a/crates/types/src/stages/physical/memo_candidates/mod.rs +++ b/crates/types/src/stages/physical/memo_candidates/mod.rs @@ -73,6 +73,11 @@ impl PhysicalASAPMemoCandidates { .map(|(id, edge)| (PhysicalEdgeId(id), *edge)) } + /// Edge `id` of $\mathcal E$. + pub fn edge(&self, id: PhysicalEdgeId) -> &PhysicalEdge { + &self.edges[id.0] + } + /// $P^{\mathrm{adm}}_{c,i}$: the admissible edges into input /// `input_index_of_consumer` of `consumer` /// ($\alpha^{\log} = \alpha^{\mathrm{phys}} = 1$). diff --git a/crates/types/src/test_support/forwarders.rs b/crates/types/src/test_support/forwarders.rs index f5a9ae22d..34213563b 100644 --- a/crates/types/src/test_support/forwarders.rs +++ b/crates/types/src/test_support/forwarders.rs @@ -6,7 +6,6 @@ use crate::ir::operator::maintained_population::CurrentSeriesInput; use crate::ir::operator::node::Operator; -use crate::plan::materialized_view::RawDataWindow; use crate::ir::schema::Schema; use crate::ir::{ASAPOp, NonASAPOp}; @@ -33,11 +32,6 @@ pub fn matches_node(input: &CurrentSeriesInput, node: &Rc) -> bool input.matches_node(node) } -/// `RawDataWindow::lookback_ms`. -pub fn lookback_ms(window: &RawDataWindow) -> u64 { - window.lookback_ms() -} - /// `physical_asap_dag::physical_asap_dag_at_query_time_with_node_ids`: `roots` /// exported with every node at query time, not materialized and without θ, /// with each exported node's operator node by node id. diff --git a/crates/types/tests/physical_asap_dag.rs b/crates/types/tests/physical_asap_dag.rs index b9e388758..6d36f0330 100644 --- a/crates/types/tests/physical_asap_dag.rs +++ b/crates/types/tests/physical_asap_dag.rs @@ -259,7 +259,7 @@ fn physical_node_ids_serialize_in_deterministic_binding_order() { } /// A window 600,000 ms wide ending 300,000 ms before T = 3,600,000 covers -/// `(2,700,000, 3,300,000]`, and reaches 900,000 ms back. +/// `(2,700,000, 3,300,000]`. #[test] fn a_raw_data_window_ends_its_offset_before_the_evaluation() { let window = asap_types::plan::materialized_view::RawDataWindow::new( @@ -270,10 +270,6 @@ fn a_raw_data_window_ends_its_offset_before_the_evaluation() { 600_000, ); assert_eq!(window.bounds_ms(3_600_000), (2_700_000, 3_300_000)); - assert_eq!( - asap_types::test_support::forwarders::lookback_ms(&window), - 900_000 - ); } /// High-fan-in merge/concat validation is independent of wire edge order and diff --git a/docs/design_docs/physical-planning-and-deployment.md b/docs/design_docs/physical-planning-and-deployment.md index 597811fd6..c7518fdb2 100644 --- a/docs/design_docs/physical-planning-and-deployment.md +++ b/docs/design_docs/physical-planning-and-deployment.md @@ -478,21 +478,32 @@ needs that the DAG determines is derived from the DAG, so the plan file has no second copy to keep consistent. Stage 3's selection report is returned beside the plan, not inside it. -**Retention.** Installation writes `retention_ms` on two kinds of node: - -- **Materialized view:** how long the view's output must stay available after - it is built. -- **Query-time `Scan`:** how far back raw data must be kept. This is the - scan's window lookback (`end_offset + width`, from `scan_window`) plus the - longest range-evaluation span of the queries reading it. It is never less - than the retention of a view refreshed on first read from that scan. - -`raw_data_retention()` is the per-source maximum over these scans. Example: for -`max_over_time(m[5m])` evaluated over the last hour, raw `m` is kept 65 -minutes, because refreshing the tumbling window for the oldest step reads that far back. -A tumbling-window chain kept across evaluations counts as all its tumbling windows. Reusing a tumbling window is -optional for a runtime, and a restart or an older step rebuilds older tumbling windows -from raw data. +**Retention.** Stage 3 writes `retention_ms` on two kinds of node, and +installation checks it: + +- **Materialized view:** how far back from the newest evaluation time its + stored rows are kept. A tumbling-window chain keeps the windows ending in + `(T - retention_ms, T]`; any other view its results of those times. +- **Query-time `Scan`:** how far back raw data is kept. + +Each covers what the `TimeRange` and `TimeShift` nodes above the node read +back: through shifts summing to `o`, a `TimeRange(r)` reads `o + r` back +(Stage 3 adds the longest range-evaluation span of the queries reading it). +`raw_data_retention()` is the per-source maximum over the query-time scans. +Example: for `max_over_time(m[5m])` evaluated over the last hour, raw `m` is +kept 65 minutes. + +**The time-indexed store.** A runtime keeps every materialized node with a +time index in one append-only store with retention: raw data by sample time, +a chain's rows by the end of their window, any other view's state by the +interval of data it summarizes. Each is read only through the `TimeRange` and +`TimeShift` nodes above it, and rows older than the node's `retention_ms` are +dropped. A chain runs natively: each ingestion run builds its window ending +at the run's end (one row per key, `(window_end, keys, state)`), the runtime +appends it, and a query reads the windows `TimeRange(W)` over shifts summing +to `o` selects, `(T - o - W, T - o]` on `window_end`, which a `SummaryMerge` +merges per key. A `TimeShift { at }` evaluates its input once per request, at +the request's end (`@ end()`), start (`@ start()`) or the given time. ### Input slots and compilation @@ -502,8 +513,8 @@ time, together with that position's schema, which is the DAG node's | Slot | Where | Holds | Read by | | --- | --- | --- | --- | -| **Raw data slot** | every `Scan` leaf | the `Scan`'s `Source`, schema, compiled predicates, and window (the union of its readers' windows, `scan_window`) | `Source` | -| **Materialized view slot** | each view the DAG reads | the `MaterializedView`, its schema, and the window its state summarizes (`view_window`: the union of the windows of every `TimeRange` below it) | `MaterializedView` | +| **Raw data slot** | every `Scan` leaf | the `Scan`'s `Source`, schema, compiled predicates, and the window its consumers read (from the `TimeRange`, `TimeShift` and chain nodes above it) | `Source` | +| **Materialized view slot** | each view the DAG reads | the `MaterializedView`, its schema, and the rows a read selects: a chain's, the windows the `TimeRange`s above it select; any other view's, the window its state summarizes (the union of the windows of every `TimeRange` below it) | `MaterializedView` | ```rust pub fn compile(dag: &PhysicalASAPDAG, views: &[MaterializedView], roots: &[NodeId]) @@ -590,9 +601,9 @@ ingestion window, or a query evaluation time `T`. Two views refreshed at ingestion can be built in one ingestion run, with the run's interval equal to each one's window, exactly when they have the same **refresh window**: windows of width `W` that end `p` after a multiple of `W` -(`0 ≤ p < W`), relative to the evaluation time. A view whose `view_window` is -`(T - o - W, T - o]` has the refresh window `(W, o mod W)`. All tumbling windows of a -tumbling-window chain have their newest tumbling window's refresh window. The source, the summary +(`0 ≤ p < W`), relative to the evaluation time. A view whose window (below it) +is `(T - o - W, T - o]` has the refresh window `(W, o mod W)`. A chain of width +`w` over shifts summing to `s` has `(w, s mod w)`. The source, the summary family and where the state is kept do not enter. A view with an unbounded window is a group of its own. diff --git a/docs/design_docs/proposals/planner-layering.md b/docs/design_docs/proposals/planner-layering.md index 93faa0821..0ddf66196 100644 --- a/docs/design_docs/proposals/planner-layering.md +++ b/docs/design_docs/proposals/planner-layering.md @@ -544,7 +544,7 @@ The sections below describe each stage. | 1, Pass 1 | nothing is recorded: a way the measure or the deployment cannot compute is not offered (the exact accumulator of an `avg` over a nullable input, a count sketch over relational rows, a summary the deployment does not support, a quantile summary outside [0, 1], a top-k heap over one row per item), nor a way Pass 1 cannot build | — | — | | 1, Pass 2 | nothing: it only adds candidates | — | — | | 2 | what cannot be built or materialized: `NoIngestionPipeline`, `RawDataNotRetained` (a scan of raw data the deployment does not keep, at query time or materialized across evaluations), `QueryTimeResultsNotRetained`, `CountMinWeightsNotProvenNonNegative`, `InputHasNoBuildableProducer`, `ReachesNoQueryOutput`; the planner does not check executor support | `PhysicalASAPMemoCandidates::pruned_candidates` | the `PhysicalCandidate` it would have been | -| 3 | valid candidates that lose, and the checks only a whole plan can make (accuracy, latency, cluster capacity) | its own `RejectionCause` | — | +| 3 | valid candidates that lose, and the checks only a whole plan can make (accuracy; latency and cluster capacity later) | its own `RejectionCause` | — | Stage 2 records a pruned candidate as the `PhysicalCandidate` it would have been, since kept candidates are renumbered after pruning and a pruned @@ -1326,9 +1326,9 @@ flowchart TB ### 3. Plan selection -Selection rejects every candidate that misses an accuracy target or a latency -bound, or that is over the cluster's capacity, and then picks the -cheapest remaining plan. It is the only stage that uses the deployment's cost +Selection rejects every candidate that misses an accuracy target and then +picks the cheapest remaining plan. Query latency bounds and cluster capacity +are left for later: selection does not check them yet. It is the only stage that uses the deployment's cost and accuracy models, and the only stage that discards valid candidates; earlier stages only prune provably invalid ones. Accuracy is estimated by the deployment's accuracy model, not assumed from a summary's @@ -1607,10 +1607,9 @@ the 6 options do (all except the two ingestion-time ones). That adds **Stage 3: 1 plan.** Selection first rejects invalid candidates. The deployment's accuracy model checks the summary candidates against ε = 0.01, δ = 0.001, including the approximate heap merge of Count-Min with tumbling -windows. Its cost model estimates Q2's latency against the 100 ms bound; for -example, an exact top 10 rebuilt from one million series at every refresh may -miss it. Among the rest, selection picks the cheapest plan for the whole -workload. Typical winners: +windows. Q2's 100 ms latency bound is left for later (selection does not +check latency yet). Among the rest, selection picks the cheapest plan for the +whole workload. Typical winners: * **Hydra for Q2, with both queries on tumbling windows at ingestion time**, when there are many small jobs: each sample updates one window, and each diff --git a/docs/develop_docs/extend-asap-aware-mapping.md b/docs/develop_docs/extend-asap-aware-mapping.md index 6b0d24b4d..1c626d653 100644 --- a/docs/develop_docs/extend-asap-aware-mapping.md +++ b/docs/develop_docs/extend-asap-aware-mapping.md @@ -85,7 +85,7 @@ contains the candidate and that Stage 3 prices it. (`LIFETIMES`, `offered_physical_properties`; a per-group offer in `group.rs`), and gate it on the deployment input that allows it with a `PruneReason`. -3. Price it in `crates/cost/src/node/usage.rs` (`asap_cost::node_usage`): what it keeps in memory or on +3. Price it in `crates/cost/src/node/usage/mod.rs` (`asap_cost::node_resource_usage_and_cost`): what it keeps in memory or on disk, and what each evaluation rebuilds. Tests: Stage 2 unit tests for when the option is offered; From 8807b74256ce3d34d82221ebc8aee86f109b51a6 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Thu, 12 Nov 2026 20:31:43 +0000 Subject: [PATCH 02/31] test(integration): SQL avg from its sum and count states executes as the exact avg, through Stage 3's plan (#1207 PR 6) Re-adds the three avg tests Pass 1's PR moved here: each plans the query through every stage with the avg group's own expression left out, so Stage 3 selects the plan that computes avg as sum over count, and executes it beside the exact plan (NULL and '' groups, a non-Utf8 key, an empty global input, avg's count of its own input, an Int64 column). --- .../tests/pass1_candidates.rs | 255 +++++++++++++++++- 1 file changed, 253 insertions(+), 2 deletions(-) diff --git a/crates/integration-tests/tests/pass1_candidates.rs b/crates/integration-tests/tests/pass1_candidates.rs index 8457b6506..81614f9cd 100644 --- a/crates/integration-tests/tests/pass1_candidates.rs +++ b/crates/integration-tests/tests/pass1_candidates.rs @@ -3,6 +3,13 @@ //! a query keeps its output columns (names, types and nullability); where //! each SQL `HAVING` is applied and what a Top-K heap sketch ranks. +use std::rc::Rc; +use std::sync::Arc; + +use asap_executor::physical_planner::compile; +use asap_executor::runtime::{Limits, RunContext, Scope}; +use asap_executor::sources::InMemoryDataStore; +use asap_executor::values::{Batch, Value}; use asap_frontend_sql::{lower_sql, SqlCatalog}; use asap_stage1_pass1_per_computation_candidates::pass1_memo; use asap_types::accuracy::AccuracyTarget; @@ -10,13 +17,18 @@ use asap_types::ir::operator::AggIntent; use asap_types::ir::scalar::{ColumnRef, CompareOpKind, ScalarValue}; use asap_types::ir::schema::FieldDataType; use asap_types::ir::schema::{DataType, Field, Schema}; -use asap_types::ir::{ASAPOp, NonASAPOp, Operator, Predicate, QueryRoot, ScalarExpr}; -use asap_types::stages::{Memo, MemoGroupId}; +use asap_types::ir::{ASAPOp, NonASAPOp, Operator, OperatorNode, Predicate, QueryRoot, ScalarExpr}; +use asap_types::plan::physical_asap_dag::PhysicalASAPOperatorPayload; +use asap_types::stages::{ + Memo, MemoGroupId, PhysicalASAPMemoCandidates, PhysicalCandidate, PhysicalCandidateId, + PhysicalEdge, +}; use asap_types::summary_type::{SketchAlgorithm, SummaryInputExpr, SummaryUpdate}; use asap_types::workload::{ BatchEntry, DataWorkload, DurationMs, PlanningWorkload, Query, QueryLanguage, QueryRequirements, QueryWorkload, }; +use futures::StreamExt; fn accuracy() -> AccuracyTarget { AccuracyTarget::max_error(0.01).failure_probability(0.01) @@ -285,6 +297,245 @@ fn avg_table() -> SqlCatalog { ) } +/// Run the logical DAG `root` over `rows` of its one scanned table, each row +/// given by column name. Rows come back shown and sorted. +fn execute(root: &Rc, rows: &[Vec<(&str, Value)>]) -> Vec> { + let dag = asap_types::test_support::forwarders::physical_asap_dag_at_query_time(root).unwrap(); + let scan = dag + .nodes + .iter() + .find(|node| { + matches!( + &node.payload, + PhysicalASAPOperatorPayload::NonASAP(NonASAPOp::Scan { .. }) + ) + }) + .expect("one scan"); + let PhysicalASAPOperatorPayload::NonASAP(NonASAPOp::Scan { source, .. }) = &scan.payload else { + unreachable!() + }; + let schema = Arc::new(scan.output_schema.clone()); + let plan = compile(&dag, &[], &[dag.roots[0] as u64]).unwrap(); + let rows = rows + .iter() + .map(|row| { + schema + .fields + .iter() + .map(|field| { + row.iter() + .find(|(name, _)| *name == field.name) + .map(|(_, value)| value.clone()) + .expect("value for every column") + }) + .collect() + }) + .collect(); + let store = InMemoryDataStore::default() + .with_raw_data( + source.clone(), + schema.clone(), + vec![Batch::try_new(schema.clone(), rows).unwrap()], + ) + .unwrap(); + let bound = plan.instantiate(Arc::new(store)).unwrap(); + let context = RunContext::new( + Scope::Query { + evaluation_time_ms: 0, + }, + Limits::default(), + ) + .unwrap(); + let show = |value: &Value| match value { + Value::Null => "NULL".to_string(), + Value::Bool(value) => value.to_string(), + Value::Float64(value) => value.to_string(), + Value::Utf8(value) => format!("{value:?}"), + other => format!("{other:?}"), + }; + let mut out: Vec> = futures::executor::block_on(async { + let mut stream = bound.execute(plan.roots(), context).unwrap().remove(0); + let mut rows = Vec::new(); + while let Some(batch) = stream.next().await { + rows.extend( + batch + .unwrap() + .to_rows() + .iter() + .map(|row| row.iter().map(show).collect()), + ); + } + rows + }); + out.sort(); + out +} + +/// SQL `avg` from its `sum` and `count` states: the exact plan and the +/// plan computing `avg` as `sum` and `count`, to execute: Stage 3's plan +/// over the physical candidates without the `avg` group's own expression. +async fn avg_as_sum_and_count( + catalog: &SqlCatalog, + query: &str, +) -> (Rc, Rc) { + use asap_integration_tests::planning::{declared_data, run_all_stages, single_query_entry}; + let exact = lower_sql(query, catalog).await.unwrap(); + let run = run_all_stages(Rc::clone(&exact), accuracy()); + let physical = run + .run + .physical_asap_candidates + .as_ref() + .expect("Stage 2 ran"); + let avg = root_group(&physical.logical_candidates.memo); + let own = |candidate: &PhysicalCandidate| { + candidate.logical_candidate_id.group == avg + && candidate.logical_candidate_id.index_in_group == 0 + }; + let (plan, _) = asap_stage3_plan_selection::select_plan( + &without_candidates(physical, own), + &[single_query_entry(accuracy())], + &declared_data(&exact), + &asap_cost::CostInputs::default(), + ) + .expect("a plan computing avg as sum over count"); + match plan.query_roots().unwrap().as_slice() { + [QueryRoot::Operator(quotient)] => (exact, Rc::clone(quotient)), + other => panic!("one operator root: {other:?}"), + } +} + +/// `physical` without the candidates `excluded` selects, and their edges. +fn without_candidates( + physical: &PhysicalASAPMemoCandidates, + excluded: impl Fn(&PhysicalCandidate) -> bool, +) -> PhysicalASAPMemoCandidates { + let mut new_id = Vec::with_capacity(physical.physical_candidates.len()); + let mut kept = Vec::new(); + for candidate in &physical.physical_candidates { + new_id.push((!excluded(candidate)).then_some(PhysicalCandidateId(kept.len()))); + if !excluded(candidate) { + kept.push(*candidate); + } + } + let edges = physical + .edges() + .filter_map(|(_, edge)| { + Some(PhysicalEdge { + producer_candidate_id: new_id[edge.producer_candidate_id.0]?, + consumer_candidate_id: new_id[edge.consumer_candidate_id.0]?, + ..edge + }) + }) + .collect(); + PhysicalASAPMemoCandidates::new( + physical.logical_candidates.clone(), + kept, + edges, + physical.pruned_candidates.clone(), + ) +} + +/// SQL `avg` as `sum` and `count` states executes as the exact aggregate: +/// NULL and '' groups stay apart, a non-Utf8 key works, and an empty global +/// input averages to NULL. +#[tokio::test] +async fn sql_avg_from_sum_and_count_executes_as_avg() { + let catalog = avg_table(); + let row = |k: Option<&str>, ok: bool, value: f64| { + vec![ + ("k", k.map_or(Value::Null, |k| Value::Utf8(k.into()))), + ("ok", Value::Bool(ok)), + ("value", Value::Float64(value)), + ] + }; + let rows = vec![ + row(None, true, 2.), + row(None, true, 4.), + row(Some(""), false, 10.), + row(Some("a"), false, 200.), + row(Some("a"), true, 1.), + ]; + let rows_of = |rows: &[&[&str]]| -> Vec> { + rows.iter() + .map(|row| row.iter().map(|cell| cell.to_string()).collect()) + .collect() + }; + for (query, input, want) in [ + ( + "SELECT k, avg(value) FROM t GROUP BY k", + rows.clone(), + rows_of(&[&["\"\"", "10"], &["\"a\"", "100.5"], &["NULL", "3"]]), + ), + ( + "SELECT ok, avg(value) FROM t GROUP BY ok", + rows.clone(), + rows_of(&[&["false", "105"], &["true", "2.3333333333333335"]]), + ), + ( + "SELECT avg(value) FROM t", + rows.clone(), + rows_of(&[&["43.4"]]), + ), + ("SELECT avg(value) FROM t", vec![], rows_of(&[&["NULL"]])), + ] { + let (exact, root) = avg_as_sum_and_count(&catalog, query).await; + assert_eq!(execute(&exact, &input), want, "{query}"); + assert_eq!(execute(&root, &input), want, "{query}"); + } +} + +/// `avg(c)`'s count counts `c`, not a nullable `value` column: the result is +/// the exact `avg`. +#[tokio::test] +async fn sql_avg_counts_its_own_input() { + let catalog = table(&[("value", true), ("c", false)]); + let query = "SELECT k, avg(c) FROM t GROUP BY k"; + let (exact, root) = avg_as_sum_and_count(&catalog, query).await; + let row = |k: &str, value: Option, c: f64| { + vec![ + ("k", Value::Utf8(k.into())), + ("value", value.map_or(Value::Null, Value::Float64)), + ("c", Value::Float64(c)), + ] + }; + let rows = vec![ + row("a", None, 1.), + row("a", Some(5.), 3.), + row("b", None, 4.), + ]; + let want = vec![ + vec!["\"a\"".to_string(), "2".to_string()], + vec!["\"b\"".to_string(), "4".to_string()], + ]; + assert_eq!(execute(&exact, &rows), want); + assert_eq!(execute(&root, &rows), want); +} + +/// SQL `avg` of a non-null Int64 column as `sum` and `count` keeps the exact plan's Float64 columns. +#[tokio::test] +async fn sql_avg_of_an_int64_column_keeps_the_exact_columns() { + let catalog = SqlCatalog::new().with_table( + "t", + Schema::new(vec![ + Field::plain("k", DataType::Utf8, true), + Field::plain("n", DataType::Int64, false), + ]), + ); + let query = "SELECT k, avg(n) FROM t GROUP BY k"; + let (exact, root) = avg_as_sum_and_count(&catalog, query).await; + assert_eq!( + root.schema.fields.last().unwrap().plain_dtype(), + Some(&DataType::Float64) + ); + let row = |k: &str, n: i64| vec![("k", Value::Utf8(k.into())), ("n", Value::Int64(n))]; + let rows = vec![row("a", 1), row("a", 2), row("b", 4)]; + let want = vec![ + vec!["\"a\"".to_string(), "1.5".to_string()], + vec!["\"b\"".to_string(), "4".to_string()], + ]; + assert_eq!(execute(&exact, &rows), want); +} + /// A filtered SQL `avg` is NULL where no row passes the filter: its quotient /// is NULL when the count is 0. The runtime executes no filtered state yet, /// so the shape is checked. From 78ec24c48e63fb244feacdf334deadbdbd05dd1e Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Thu, 12 Nov 2026 20:33:05 +0000 Subject: [PATCH 03/31] test: move the inline tests of the files PR 6 touches into sibling files (#1207 PR 6) Move-only: each inline test module of a file PR 6 changes becomes a sibling file (/mod.rs + /tests.rs, or a sibling of mod.rs): the planner fuzz relations, the executor's compilation, state-column updater, supported-summaries, batch and weighted-state tests, the installer's tests and the execution data state's. --- .../{relations.rs => relations/mod.rs} | 265 +--- .../src/planner_fuzz/relations/tests.rs | 262 ++++ .../src/physical_planner/compilation_tests.rs | 19 + crates/executor/src/physical_planner/mod.rs | 22 +- .../mod.rs} | 29 +- .../state_column_updater/tests.rs | 26 + crates/executor/src/supported_summaries.rs | 211 --- .../executor/src/supported_summaries/mod.rs | 23 + .../executor/src/supported_summaries/tests.rs | 187 +++ crates/executor/src/values/batch.rs | 1276 ----------------- crates/executor/src/values/batch/mod.rs | 609 ++++++++ crates/executor/src/values/batch/tests.rs | 665 +++++++++ crates/executor/src/values/mod.rs | 24 +- .../src/values/weighted_state_tests.rs | 20 + .../{installable.rs => installable/mod.rs} | 260 +--- crates/planner/src/installable/tests.rs | 256 ++++ .../mod.rs} | 37 +- .../properties/execution_data_state/tests.rs | 34 + .../asap-aware-mapping-architecture.md | 2 +- 19 files changed, 2108 insertions(+), 2119 deletions(-) rename crates/devtools/src/planner_fuzz/{relations.rs => relations/mod.rs} (87%) create mode 100644 crates/devtools/src/planner_fuzz/relations/tests.rs create mode 100644 crates/executor/src/physical_planner/compilation_tests.rs rename crates/executor/src/summary_kernels/{state_column_updater.rs => state_column_updater/mod.rs} (95%) create mode 100644 crates/executor/src/summary_kernels/state_column_updater/tests.rs delete mode 100644 crates/executor/src/supported_summaries.rs create mode 100644 crates/executor/src/supported_summaries/mod.rs create mode 100644 crates/executor/src/supported_summaries/tests.rs delete mode 100644 crates/executor/src/values/batch.rs create mode 100644 crates/executor/src/values/batch/mod.rs create mode 100644 crates/executor/src/values/batch/tests.rs create mode 100644 crates/executor/src/values/weighted_state_tests.rs rename crates/planner/src/{installable.rs => installable/mod.rs} (69%) create mode 100644 crates/planner/src/installable/tests.rs rename crates/types/src/ir/properties/{execution_data_state.rs => execution_data_state/mod.rs} (69%) create mode 100644 crates/types/src/ir/properties/execution_data_state/tests.rs diff --git a/crates/devtools/src/planner_fuzz/relations.rs b/crates/devtools/src/planner_fuzz/relations/mod.rs similarity index 87% rename from crates/devtools/src/planner_fuzz/relations.rs rename to crates/devtools/src/planner_fuzz/relations/mod.rs index 4054736ae..da20e4b0a 100644 --- a/crates/devtools/src/planner_fuzz/relations.rs +++ b/crates/devtools/src/planner_fuzz/relations/mod.rs @@ -1778,267 +1778,4 @@ pub fn disjoint_copy(workload: &FuzzWorkload) -> Option { } #[cfg(test)] -mod tests { - use super::*; - use crate::planner_fuzz::generate::{generated_workload, SqlSpec}; - use asap_types::accuracy::AccuracyTarget; - - fn promql(spec: PromqlSpec, accuracy: AccuracyTarget) -> FuzzWorkload { - generated_workload( - Language::Promql, - vec![FuzzQuery::generated( - QuerySpec::Promql(spec), - accuracy, - 10_000, - None, - )], - 1_000, - 15_000, - ) - } - - fn sum_by(labels: &[&str]) -> PromqlSpec { - PromqlSpec { - metric: "m_counter".into(), - matchers: vec![], - range: Some(RangeFunction::Rate), - window_ms: 300_000, - outer: Some(OuterAggregation { - op: OuterOp::Sum, - labels: labels.iter().map(|l| l.to_string()).collect(), - without: false, - }), - } - } - - /// Each marginal relation derives the documented `q'` from `q`. - #[test] - fn marginal_relations_derive_the_documented_query() { - let w = promql(sum_by(&["job", "instance"]), AccuracyTarget::exact()); - let text = |r| derive(r, &w, 0).map(|d| d.query.text().to_string()); - assert_eq!( - text(Relation::FilterOnGroupKey).as_deref(), - Some("sum by (job, instance) (rate(m_counter{job=\"job1\"}[5m]))") - ); - let coarser = derive(Relation::CoarserGrouping, &w, 0).unwrap(); - assert_eq!(coarser.query.text(), "sum by (job) (rate(m_counter[5m]))"); - assert_eq!( - coarser.witnesses[0], - Witness::ExactRollup { - labels: vec!["job".into()], - op: RollupOp::Sum - } - ); - // A counter's rate reads more than merged tumbling windows: no nested window. - assert_eq!(text(Relation::NestedShorterWindow), None); - assert_eq!(text(Relation::OtherQuantileRank), None); - assert_eq!( - text(Relation::OuterOverResult).as_deref(), - Some("topk(3, sum by (job, instance) (rate(m_counter[5m])))") - ); - assert_eq!( - text(Relation::ExactDuplicate).as_deref(), - Some(w.queries[0].text()) - ); - let looser = derive(Relation::LooserAccuracy, &w, 0).unwrap(); - assert_eq!(looser.query.accuracy().max_error, 0.01); - let interval = derive(Relation::OtherRepeatInterval, &w, 0).unwrap(); - assert_eq!(repeat_interval_ms(&interval.query), Some(30_000)); - } - - /// Quantile, top-k, count and union derivations, PromQL and SQL. - #[test] - fn statistic_and_selection_relations_derive() { - let mut quantile = sum_by(&["job"]); - quantile.metric = "m_gauge".into(); - quantile.range = Some(RangeFunction::QuantileOverTime(0.9)); - quantile.outer = None; - quantile.matchers = vec![("job".into(), "job1".into())]; - let w = promql(quantile, AccuracyTarget::max_error(0.01)); - let rank = derive(Relation::OtherQuantileRank, &w, 0).unwrap(); - assert_eq!( - rank.query.text(), - "quantile_over_time(0.5, m_gauge{job=\"job1\"}[5m])" - ); - assert_eq!( - rank.witnesses, - vec![Witness::QuantileRank { from: 0.9, to: 0.5 }] - ); - // A quantile sketch answers only quantiles: (e), not (h). - assert!(derive(Relation::OtherStatistic, &w, 0).is_none()); - let union = derive(Relation::UnionOfSelections, &w, 0).unwrap(); - assert_eq!( - union.base_addition.unwrap().text(), - "quantile_over_time(0.9, m_gauge{job=\"job2\"}[5m])" - ); - assert_eq!( - union.query.text(), - "quantile_over_time(0.9, m_gauge{job=~\"job1|job2\"}[5m])" - ); - - let mut topk = sum_by(&["job"]); - topk.outer.as_mut().unwrap().op = OuterOp::Topk(10); - let w = promql(topk, AccuracyTarget::exact()); - let smaller = derive(Relation::SmallerTopK, &w, 0).unwrap(); - assert_eq!( - smaller.query.text(), - "topk by (job) (3, rate(m_counter[5m]))" - ); - - let sql = SqlSpec { - table: "metrics".into(), - aggregate: SqlAggregate::Sum, - group_by: vec!["city".into(), "host".into()], - filters: vec![], - window_ms: 300_000, - }; - let w = generated_workload( - Language::Sql, - vec![FuzzQuery::generated( - QuerySpec::Sql(sql), - AccuracyTarget::exact(), - 10_000, - None, - )], - 100, - 10_000, - ); - let filtered = derive(Relation::FilterOnGroupKey, &w, 0).unwrap(); - assert!(filtered - .query - .text() - .contains("AND city = 'city1' GROUP BY city, host")); - let coarser = derive(Relation::CoarserGrouping, &w, 0).unwrap(); - assert!(coarser.query.text().ends_with("GROUP BY city")); - } - - /// The relations taken from the ASAPWorkloads query catalog derive its - /// shapes: a ratio over q, a longer window (rolled up), `rate` as - /// `increase`, and an outer operator that follows q's own. - #[test] - fn workload_catalog_relations_derive() { - let rate = promql(sum_by(&["job", "instance"]), AccuracyTarget::exact()); - let text = |w: &FuzzWorkload, r| derive(r, w, 0).map(|d| d.query.text().to_string()); - assert_eq!( - text(&rate, Relation::BinaryOverResult).as_deref(), - Some( - "sum by (job, instance) (rate(m_counter{region=\"region1\"}[5m])) / \ - sum by (job, instance) (rate(m_counter[5m]))" - ) - ); - let increase = derive(Relation::EquivalentRangeFunction, &rate, 0).unwrap(); - assert_eq!( - increase.query.text(), - "sum by (job, instance) (increase(m_counter[5m]))" - ); - assert_eq!( - increase.witnesses, - vec![Witness::Graft { - text: "(sum by (job, instance) (rate(m_counter[5m]))) * 300".into() - }] - ); - // As for (d), a rate's window is not merged tumbling-window state. - assert_eq!(text(&rate, Relation::LongerWindow), None); - - let mut sum = sum_by(&["job", "instance"]); - sum.metric = "m_gauge".into(); - sum.range = Some(RangeFunction::SumOverTime); - sum.outer.as_mut().unwrap().op = OuterOp::Max; - let w = promql(sum, AccuracyTarget::exact()); - assert_eq!( - text(&w, Relation::BinaryOverResult).as_deref(), - // Not an average under max: the filtered share instead. - Some( - "max by (job, instance) (sum_over_time(m_gauge{region=\"region1\"}[5m])) / \ - max by (job, instance) (sum_over_time(m_gauge[5m]))" - ) - ); - assert_eq!( - text(&w, Relation::OuterOverResult).as_deref(), - Some("max(max by (job, instance) (sum_over_time(m_gauge[5m])))") - ); - assert_eq!( - text(&w, Relation::LongerWindow).as_deref(), - Some("max by (job, instance) (sum_over_time(m_gauge[10m]))") - ); - let rollup = derive(Relation::LongerWindowRollup, &w, 0).unwrap(); - assert_eq!( - rollup.query.text(), - "max by (job) (sum_over_time(m_gauge[10m]))" - ); - assert_eq!( - rollup.witnesses[0], - Witness::LongerWindow { - window_ms: 600_000, - rollup: Some(Box::new(Witness::ExactRollup { - labels: vec!["job".into()], - op: RollupOp::Max - })) - } - ); - // (h): one UnivMon answers the count, distinct count, L2 and - // entropy; q' asks for another than q. - let entropy = SqlSpec { - table: "metrics".into(), - aggregate: SqlAggregate::FrequencyEntropy("src_ip".into()), - group_by: vec![], - filters: vec![], - window_ms: 60_000, - }; - let w = generated_workload( - Language::Sql, - vec![FuzzQuery::generated( - QuerySpec::Sql(entropy), - AccuracyTarget::max_error(0.05), - 10_000, - None, - )], - 100_000, - 15_000, - ); - let h = derive(Relation::OtherStatistic, &w, 0).unwrap(); - assert_eq!( - h.query.text(), - "SELECT SQRT(SUM(CAST(c AS DOUBLE) * CAST(c AS DOUBLE))) AS v FROM \ - (SELECT src_ip, COUNT(*) AS c FROM metrics WHERE ts > now() - INTERVAL '1' \ - MINUTE GROUP BY src_ip)" - ); - assert_eq!(h.witnesses[1], Witness::UnivMonOnly); - assert_eq!(longer_window(600_000), Some(1_800_000)); - assert_eq!(longer_window(3_600_000), None); - } - - /// Renaming renames sources, labels, facts and tables; a disjoint copy - /// doubles the queries, sources and arrival rate. - #[test] - fn rename_and_copy_rewrite_facts() { - let w = promql(sum_by(&["job"]), AccuracyTarget::exact()); - let r = renamed(&w, "_r", true).unwrap(); - assert_eq!( - r.queries[0].text(), - "sum by (job_r) (rate(m_counter_r[5m]))" - ); - assert!(r.data.sources["m_counter_r"] - .distinct_values_per_label - .contains_key("job_r")); - let both = disjoint_copy(&w).unwrap(); - assert_eq!(both.queries.len(), 2); - assert_eq!(both.data.sources.len(), 4); - assert_eq!( - both.data.arrival.rows_per_second(), - w.data.arrival.rows_per_second().map(|r| 2.0 * r) - ); - } - - /// Every relation has a distinct name that parses back, and letters - /// parse for the marginal ones. - #[test] - fn relation_names_parse() { - for relation in Relation::ALL { - assert_eq!(Relation::parse(relation.name()), Some(relation)); - } - assert_eq!(Relation::parse("a"), Some(Relation::LooserAccuracy)); - assert_eq!(Relation::parse("k"), Some(Relation::UnionOfSelections)); - assert_eq!(Relation::parse("z"), None); - } -} +mod tests; diff --git a/crates/devtools/src/planner_fuzz/relations/tests.rs b/crates/devtools/src/planner_fuzz/relations/tests.rs new file mode 100644 index 000000000..7432d3dd6 --- /dev/null +++ b/crates/devtools/src/planner_fuzz/relations/tests.rs @@ -0,0 +1,262 @@ +use super::*; +use crate::planner_fuzz::generate::{generated_workload, SqlSpec}; +use asap_types::accuracy::AccuracyTarget; + +fn promql(spec: PromqlSpec, accuracy: AccuracyTarget) -> FuzzWorkload { + generated_workload( + Language::Promql, + vec![FuzzQuery::generated( + QuerySpec::Promql(spec), + accuracy, + 10_000, + None, + )], + 1_000, + 15_000, + ) +} + +fn sum_by(labels: &[&str]) -> PromqlSpec { + PromqlSpec { + metric: "m_counter".into(), + matchers: vec![], + range: Some(RangeFunction::Rate), + window_ms: 300_000, + outer: Some(OuterAggregation { + op: OuterOp::Sum, + labels: labels.iter().map(|l| l.to_string()).collect(), + without: false, + }), + } +} + +/// Each marginal relation derives the documented `q'` from `q`. +#[test] +fn marginal_relations_derive_the_documented_query() { + let w = promql(sum_by(&["job", "instance"]), AccuracyTarget::exact()); + let text = |r| derive(r, &w, 0).map(|d| d.query.text().to_string()); + assert_eq!( + text(Relation::FilterOnGroupKey).as_deref(), + Some("sum by (job, instance) (rate(m_counter{job=\"job1\"}[5m]))") + ); + let coarser = derive(Relation::CoarserGrouping, &w, 0).unwrap(); + assert_eq!(coarser.query.text(), "sum by (job) (rate(m_counter[5m]))"); + assert_eq!( + coarser.witnesses[0], + Witness::ExactRollup { + labels: vec!["job".into()], + op: RollupOp::Sum + } + ); + // A counter's rate reads more than merged tumbling windows: no nested window. + assert_eq!(text(Relation::NestedShorterWindow), None); + assert_eq!(text(Relation::OtherQuantileRank), None); + assert_eq!( + text(Relation::OuterOverResult).as_deref(), + Some("topk(3, sum by (job, instance) (rate(m_counter[5m])))") + ); + assert_eq!( + text(Relation::ExactDuplicate).as_deref(), + Some(w.queries[0].text()) + ); + let looser = derive(Relation::LooserAccuracy, &w, 0).unwrap(); + assert_eq!(looser.query.accuracy().max_error, 0.01); + let interval = derive(Relation::OtherRepeatInterval, &w, 0).unwrap(); + assert_eq!(repeat_interval_ms(&interval.query), Some(30_000)); +} + +/// Quantile, top-k, count and union derivations, PromQL and SQL. +#[test] +fn statistic_and_selection_relations_derive() { + let mut quantile = sum_by(&["job"]); + quantile.metric = "m_gauge".into(); + quantile.range = Some(RangeFunction::QuantileOverTime(0.9)); + quantile.outer = None; + quantile.matchers = vec![("job".into(), "job1".into())]; + let w = promql(quantile, AccuracyTarget::max_error(0.01)); + let rank = derive(Relation::OtherQuantileRank, &w, 0).unwrap(); + assert_eq!( + rank.query.text(), + "quantile_over_time(0.5, m_gauge{job=\"job1\"}[5m])" + ); + assert_eq!( + rank.witnesses, + vec![Witness::QuantileRank { from: 0.9, to: 0.5 }] + ); + // A quantile sketch answers only quantiles: (e), not (h). + assert!(derive(Relation::OtherStatistic, &w, 0).is_none()); + let union = derive(Relation::UnionOfSelections, &w, 0).unwrap(); + assert_eq!( + union.base_addition.unwrap().text(), + "quantile_over_time(0.9, m_gauge{job=\"job2\"}[5m])" + ); + assert_eq!( + union.query.text(), + "quantile_over_time(0.9, m_gauge{job=~\"job1|job2\"}[5m])" + ); + + let mut topk = sum_by(&["job"]); + topk.outer.as_mut().unwrap().op = OuterOp::Topk(10); + let w = promql(topk, AccuracyTarget::exact()); + let smaller = derive(Relation::SmallerTopK, &w, 0).unwrap(); + assert_eq!( + smaller.query.text(), + "topk by (job) (3, rate(m_counter[5m]))" + ); + + let sql = SqlSpec { + table: "metrics".into(), + aggregate: SqlAggregate::Sum, + group_by: vec!["city".into(), "host".into()], + filters: vec![], + window_ms: 300_000, + }; + let w = generated_workload( + Language::Sql, + vec![FuzzQuery::generated( + QuerySpec::Sql(sql), + AccuracyTarget::exact(), + 10_000, + None, + )], + 100, + 10_000, + ); + let filtered = derive(Relation::FilterOnGroupKey, &w, 0).unwrap(); + assert!(filtered + .query + .text() + .contains("AND city = 'city1' GROUP BY city, host")); + let coarser = derive(Relation::CoarserGrouping, &w, 0).unwrap(); + assert!(coarser.query.text().ends_with("GROUP BY city")); +} + +/// The relations taken from the ASAPWorkloads query catalog derive its +/// shapes: a ratio over q, a longer window (rolled up), `rate` as +/// `increase`, and an outer operator that follows q's own. +#[test] +fn workload_catalog_relations_derive() { + let rate = promql(sum_by(&["job", "instance"]), AccuracyTarget::exact()); + let text = |w: &FuzzWorkload, r| derive(r, w, 0).map(|d| d.query.text().to_string()); + assert_eq!( + text(&rate, Relation::BinaryOverResult).as_deref(), + Some( + "sum by (job, instance) (rate(m_counter{region=\"region1\"}[5m])) / \ + sum by (job, instance) (rate(m_counter[5m]))" + ) + ); + let increase = derive(Relation::EquivalentRangeFunction, &rate, 0).unwrap(); + assert_eq!( + increase.query.text(), + "sum by (job, instance) (increase(m_counter[5m]))" + ); + assert_eq!( + increase.witnesses, + vec![Witness::Graft { + text: "(sum by (job, instance) (rate(m_counter[5m]))) * 300".into() + }] + ); + // As for (d), a rate's window is not merged tumbling-window state. + assert_eq!(text(&rate, Relation::LongerWindow), None); + + let mut sum = sum_by(&["job", "instance"]); + sum.metric = "m_gauge".into(); + sum.range = Some(RangeFunction::SumOverTime); + sum.outer.as_mut().unwrap().op = OuterOp::Max; + let w = promql(sum, AccuracyTarget::exact()); + assert_eq!( + text(&w, Relation::BinaryOverResult).as_deref(), + // Not an average under max: the filtered share instead. + Some( + "max by (job, instance) (sum_over_time(m_gauge{region=\"region1\"}[5m])) / \ + max by (job, instance) (sum_over_time(m_gauge[5m]))" + ) + ); + assert_eq!( + text(&w, Relation::OuterOverResult).as_deref(), + Some("max(max by (job, instance) (sum_over_time(m_gauge[5m])))") + ); + assert_eq!( + text(&w, Relation::LongerWindow).as_deref(), + Some("max by (job, instance) (sum_over_time(m_gauge[10m]))") + ); + let rollup = derive(Relation::LongerWindowRollup, &w, 0).unwrap(); + assert_eq!( + rollup.query.text(), + "max by (job) (sum_over_time(m_gauge[10m]))" + ); + assert_eq!( + rollup.witnesses[0], + Witness::LongerWindow { + window_ms: 600_000, + rollup: Some(Box::new(Witness::ExactRollup { + labels: vec!["job".into()], + op: RollupOp::Max + })) + } + ); + // (h): one UnivMon answers the count, distinct count, L2 and + // entropy; q' asks for another than q. + let entropy = SqlSpec { + table: "metrics".into(), + aggregate: SqlAggregate::FrequencyEntropy("src_ip".into()), + group_by: vec![], + filters: vec![], + window_ms: 60_000, + }; + let w = generated_workload( + Language::Sql, + vec![FuzzQuery::generated( + QuerySpec::Sql(entropy), + AccuracyTarget::max_error(0.05), + 10_000, + None, + )], + 100_000, + 15_000, + ); + let h = derive(Relation::OtherStatistic, &w, 0).unwrap(); + assert_eq!( + h.query.text(), + "SELECT SQRT(SUM(CAST(c AS DOUBLE) * CAST(c AS DOUBLE))) AS v FROM \ + (SELECT src_ip, COUNT(*) AS c FROM metrics WHERE ts > now() - INTERVAL '1' \ + MINUTE GROUP BY src_ip)" + ); + assert_eq!(h.witnesses[1], Witness::UnivMonOnly); + assert_eq!(longer_window(600_000), Some(1_800_000)); + assert_eq!(longer_window(3_600_000), None); +} + +/// Renaming renames sources, labels, facts and tables; a disjoint copy +/// doubles the queries, sources and arrival rate. +#[test] +fn rename_and_copy_rewrite_facts() { + let w = promql(sum_by(&["job"]), AccuracyTarget::exact()); + let r = renamed(&w, "_r", true).unwrap(); + assert_eq!( + r.queries[0].text(), + "sum by (job_r) (rate(m_counter_r[5m]))" + ); + assert!(r.data.sources["m_counter_r"] + .distinct_values_per_label + .contains_key("job_r")); + let both = disjoint_copy(&w).unwrap(); + assert_eq!(both.queries.len(), 2); + assert_eq!(both.data.sources.len(), 4); + assert_eq!( + both.data.arrival.rows_per_second(), + w.data.arrival.rows_per_second().map(|r| 2.0 * r) + ); +} + +/// Every relation has a distinct name that parses back, and letters +/// parse for the marginal ones. +#[test] +fn relation_names_parse() { + for relation in Relation::ALL { + assert_eq!(Relation::parse(relation.name()), Some(relation)); + } + assert_eq!(Relation::parse("a"), Some(Relation::LooserAccuracy)); + assert_eq!(Relation::parse("k"), Some(Relation::UnionOfSelections)); + assert_eq!(Relation::parse("z"), None); +} diff --git a/crates/executor/src/physical_planner/compilation_tests.rs b/crates/executor/src/physical_planner/compilation_tests.rs new file mode 100644 index 000000000..58f45534a --- /dev/null +++ b/crates/executor/src/physical_planner/compilation_tests.rs @@ -0,0 +1,19 @@ +use super::*; + +fn empty_dag() -> PhysicalASAPDAG { + PhysicalASAPDAG { + nodes: vec![], + edges: vec![], + roots: vec![], + scalar_roots: vec![], + } +} + +/// An empty DAG (no roots) is invalid. +#[test] +fn an_empty_dag_is_invalid() { + assert!(matches!( + compile(&empty_dag(), &[], &[]), + Err(Error::Invalid(_)) + )); +} diff --git a/crates/executor/src/physical_planner/mod.rs b/crates/executor/src/physical_planner/mod.rs index 9943f6e43..9fd8cfd01 100644 --- a/crates/executor/src/physical_planner/mod.rs +++ b/crates/executor/src/physical_planner/mod.rs @@ -1799,27 +1799,7 @@ fn local_scalar(expr: &ScalarExpr) -> Result PhysicalASAPDAG { - PhysicalASAPDAG { - nodes: vec![], - edges: vec![], - roots: vec![], - scalar_roots: vec![], - } - } - - /// An empty DAG (no roots) is invalid. - #[test] - fn an_empty_dag_is_invalid() { - assert!(matches!( - compile(&empty_dag(), &[], &[]), - Err(Error::Invalid(_)) - )); - } -} +mod compilation_tests; /// Helpers for the Planner's own tests (`test-support`). #[cfg(feature = "test-support")] diff --git a/crates/executor/src/summary_kernels/state_column_updater.rs b/crates/executor/src/summary_kernels/state_column_updater/mod.rs similarity index 95% rename from crates/executor/src/summary_kernels/state_column_updater.rs rename to crates/executor/src/summary_kernels/state_column_updater/mod.rs index 7768c5b21..194b7eb44 100644 --- a/crates/executor/src/summary_kernels/state_column_updater.rs +++ b/crates/executor/src/summary_kernels/state_column_updater/mod.rs @@ -500,31 +500,4 @@ impl StateColumnUpdater { } #[cfg(test)] -mod tests { - use super::*; - use planner_types::summary_type::ExactParams; - - // An exact updater charges the buffer it holds (its capacity), the - // same bytes its sealed column charges. - #[test] - fn exact_memory_is_the_buffer_capacity() { - let family = FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum); - for groups in [1, 5, 17] { - let mut updater = StateColumnUpdater::new(&family, None).unwrap(); - updater.ensure_groups(groups).unwrap(); - let charged = updater.memory_bytes(); - let GroupStates::Exact { states, .. } = &updater.states else { - unreachable!() - }; - assert_eq!( - charged, - states.capacity() * std::mem::size_of::() - ); - assert_eq!( - charged, - updater.seal().charged_state_bytes(), - "{groups} groups" - ); - } - } -} +mod tests; diff --git a/crates/executor/src/summary_kernels/state_column_updater/tests.rs b/crates/executor/src/summary_kernels/state_column_updater/tests.rs new file mode 100644 index 000000000..74598e7cb --- /dev/null +++ b/crates/executor/src/summary_kernels/state_column_updater/tests.rs @@ -0,0 +1,26 @@ +use super::*; +use planner_types::summary_type::ExactParams; + +// An exact updater charges the buffer it holds (its capacity), the +// same bytes its sealed column charges. +#[test] +fn exact_memory_is_the_buffer_capacity() { + let family = FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum); + for groups in [1, 5, 17] { + let mut updater = StateColumnUpdater::new(&family, None).unwrap(); + updater.ensure_groups(groups).unwrap(); + let charged = updater.memory_bytes(); + let GroupStates::Exact { states, .. } = &updater.states else { + unreachable!() + }; + assert_eq!( + charged, + states.capacity() * std::mem::size_of::() + ); + assert_eq!( + charged, + updater.seal().charged_state_bytes(), + "{groups} groups" + ); + } +} diff --git a/crates/executor/src/supported_summaries.rs b/crates/executor/src/supported_summaries.rs deleted file mode 100644 index 185edfb08..000000000 --- a/crates/executor/src/supported_summaries.rs +++ /dev/null @@ -1,211 +0,0 @@ -//! The summary families this executor builds, with the readouts it answers -//! from each: what a deployment running it declares as -//! `DeploymentCapabilities::supported_summaries`, so the Planner offers no -//! summary the executor would reject when it binds the plan. - -use crate::capability::EVALUATED_READOUTS; -use planner_types::summary_type::SupportedSummary; - -/// Every sketch family this executor builds from rows and the statistics it -/// answers from one state of it (`EVALUATED_READOUTS`). Exact accumulators -/// are always supported and are not listed. -pub fn supported_summary_families() -> Vec { - EVALUATED_READOUTS - .iter() - .map(|&(summary_type, readouts)| SupportedSummary { - summary_type, - readouts: readouts.to_vec(), - }) - .collect() -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::capability::validate_sketch_evaluation; - use crate::factory::create_planner_accumulator; - - use planner_types::ir::operator::arguments::{Reduction, Source}; - use planner_types::ir::scalar::ColumnRef; - use planner_types::ir::schema::{DataType, Field, FieldDataType, Schema}; - use planner_types::ir::{ASAPOp, NonASAPOp, Operator, OperatorNode}; - use planner_types::summary_type::{ - SketchAlgorithm, SketchStatistic, StatisticKind, SummaryInputExpr, SummaryTypeCapability, - SummaryUpdate, - }; - - const ALGORITHMS: [SketchAlgorithm; 9] = [ - SketchAlgorithm::UnivMon, - SketchAlgorithm::Kll, - SketchAlgorithm::Cms, - SketchAlgorithm::Hll, - SketchAlgorithm::DDSketch, - SketchAlgorithm::CmsWithHeap, - SketchAlgorithm::Kmv, - SketchAlgorithm::CountSketch, - SketchAlgorithm::CountSketchWithHeap, - ]; - - const READOUTS: [StatisticKind; 7] = [ - StatisticKind::Total, - StatisticKind::ItemFrequency, - StatisticKind::Quantile, - StatisticKind::Distinct, - StatisticKind::FrequencyL2, - StatisticKind::FrequencyEntropy, - StatisticKind::TopK, - ]; - - fn per_instance(algorithm: &SketchAlgorithm) -> FieldDataType { - FieldDataType::Sketch(*algorithm) - } - - /// A statistic of kind `readout`. - fn statistic(readout: StatisticKind) -> SketchStatistic { - let key = ColumnRef::Named("item".into()); - match readout { - // Pass 1 reads a total keyed by the sample value. - StatisticKind::Total => SketchStatistic::PointCount { - key: ColumnRef::SampleValue, - value: None, - }, - StatisticKind::ItemFrequency => SketchStatistic::PointCount { - key, - value: Some("a".into()), - }, - StatisticKind::Quantile => SketchStatistic::Quantile { q: 0.99 }, - StatisticKind::Distinct => SketchStatistic::Cardinality, - StatisticKind::FrequencyL2 => SketchStatistic::FrequencyL2, - StatisticKind::FrequencyEntropy => SketchStatistic::FrequencyEntropy, - StatisticKind::TopK => SketchStatistic::TopK { k: 5 }, - other => unreachable!("{other:?} is not probed"), - } - } - - /// Whether the factory builds the per-instance `family` from rows, with - /// a value column or keyed items. - fn factory_builds(family: &FieldDataType) -> bool { - let FieldDataType::Sketch(algorithm) = family else { - return false; - }; - let theta = planner_types::summary_type::parameter_list(*algorithm).first(); - let value = SummaryUpdate::column(ColumnRef::SampleValue); - let keyed = SummaryUpdate { - item: Some(SummaryInputExpr::Column(ColumnRef::Named("item".into()))), - ..value.clone() - }; - create_planner_accumulator(family, theta, &value).is_ok() - || create_planner_accumulator(family, theta, &keyed).is_ok() - } - - /// Whether the executor answers `readout` from `family`'s state: top-k - /// through the keyed heap evaluation, every other readout through the - /// sketch evaluation, whose kernel must then read it from a built state. - fn evaluation_answers(family: &FieldDataType, readout: StatisticKind) -> bool { - match (readout, family) { - (StatisticKind::TopK, FieldDataType::Sketch(kind)) => matches!( - kind, - planner_types::summary_type::SketchAlgorithm::CmsWithHeap - | planner_types::summary_type::SketchAlgorithm::CountSketchWithHeap - ), - _ => { - let accepted = validate_sketch_evaluation(family, &statistic(readout)).is_ok(); - if accepted { - assert!(kernel_answers(family, readout), "{family:?} {readout:?}"); - } - accepted - } - } - } - - /// Whether the kernel of a per-instance `family` state built from one - /// update reads `readout`. - fn kernel_answers(family: &FieldDataType, readout: StatisticKind) -> bool { - let FieldDataType::Sketch(algorithm) = family else { - return false; - }; - let theta = planner_types::summary_type::parameter_list(*algorithm).first(); - let value = SummaryUpdate::column(ColumnRef::SampleValue); - let keyed = SummaryUpdate { - item: Some(SummaryInputExpr::Column(ColumnRef::Named("item".into()))), - ..value.clone() - }; - let Ok(mut updater) = create_planner_accumulator(family, theta, &value) - .or_else(|_| create_planner_accumulator(family, theta, &keyed)) - else { - return false; - }; - let updated = if updater.is_keyed() { - updater.update_keyed(&["a".to_string()], 1.0, 0) - } else { - updater.update_single(1.0, 0) - }; - updated.is_ok() - && updater - .snapshot_accumulator() - .estimate(&statistic(readout)) - .is_ok() - } - - /// Whether the IR builds a structurally valid `SummaryEstimate` of - /// `readout` over a `family` state. - fn ir_builds_estimate(family: &FieldDataType, readout: StatisticKind) -> bool { - let scan = OperatorNode::new_shared(Operator::NonASAP(NonASAPOp::Scan { - source: Source::Table { - table_ref: "t".into(), - }, - predicates: vec![], - schema: Schema::new(vec![ - Field::plain("item", DataType::Utf8, false), - Field::plain("value", DataType::Float64, false), - ]), - })) - .unwrap(); - let state = OperatorNode::new_shared(Operator::ASAP(ASAPOp::SummaryAgg { - child: scan, - summary_type: family.clone(), - input: SummaryUpdate::column(ColumnRef::SampleValue), - reduction: Reduction::by(vec![]), - filter: None, - })) - .unwrap(); - OperatorNode::new_shared(Operator::ASAP(ASAPOp::SummaryEstimate { - summary_input: state, - query: statistic(readout), - })) - .is_ok_and(|estimate| estimate.validate_structure().is_ok()) - } - - /// The list holds exactly the (family, readout) pairs the factory builds - /// per instance and an evaluation answers from that state, and the - /// IR's `SummaryEstimate` validation accepts exactly what the - /// family-capability table answers. Every declared readout is in the - /// family's table row. - #[test] - fn supported_summary_families_match_the_factory_and_evaluations() { - let listed = supported_summary_families(); - for algorithm in &ALGORITHMS { - // Per instance: Stage 1 builds every family per instance; the - // Hydra grid is not produced outside tests yet. - let family = per_instance(algorithm); - for readout in READOUTS { - let executed = factory_builds(&family) && evaluation_answers(&family, readout); - let declared = listed - .iter() - .any(|s| &s.summary_type == algorithm && s.readouts.contains(&readout)); - assert_eq!(declared, executed, "{algorithm:?} {readout:?}"); - let answered = SummaryTypeCapability::of(&family).answers(&statistic(readout)); - assert_eq!( - ir_builds_estimate(&family, readout), - answered, - "IR: {algorithm:?} {readout:?}" - ); - assert!( - !declared || answered, - "declared but not in the table: {algorithm:?} {readout:?}" - ); - } - } - assert!(listed.iter().all(|s| !s.readouts.is_empty())); - } -} diff --git a/crates/executor/src/supported_summaries/mod.rs b/crates/executor/src/supported_summaries/mod.rs new file mode 100644 index 000000000..ec411cab9 --- /dev/null +++ b/crates/executor/src/supported_summaries/mod.rs @@ -0,0 +1,23 @@ +//! The summary families this executor builds, with the readouts it answers +//! from each: what a deployment running it declares as +//! `DeploymentCapabilities::supported_summaries`, so the Planner offers no +//! summary the executor would reject when it binds the plan. + +use crate::capability::EVALUATED_READOUTS; +use planner_types::summary_type::SupportedSummary; + +/// Every sketch family this executor builds from rows and the statistics it +/// answers from one state of it (`EVALUATED_READOUTS`). Exact accumulators +/// are always supported and are not listed. +pub fn supported_summary_families() -> Vec { + EVALUATED_READOUTS + .iter() + .map(|&(summary_type, readouts)| SupportedSummary { + summary_type, + readouts: readouts.to_vec(), + }) + .collect() +} + +#[cfg(test)] +mod tests; diff --git a/crates/executor/src/supported_summaries/tests.rs b/crates/executor/src/supported_summaries/tests.rs new file mode 100644 index 000000000..e02398258 --- /dev/null +++ b/crates/executor/src/supported_summaries/tests.rs @@ -0,0 +1,187 @@ +use super::*; +use crate::capability::validate_sketch_evaluation; +use crate::factory::create_planner_accumulator; + +use planner_types::ir::operator::arguments::{Reduction, Source}; +use planner_types::ir::scalar::ColumnRef; +use planner_types::ir::schema::{DataType, Field, FieldDataType, Schema}; +use planner_types::ir::{ASAPOp, NonASAPOp, Operator, OperatorNode}; +use planner_types::summary_type::{ + SketchAlgorithm, SketchStatistic, StatisticKind, SummaryInputExpr, SummaryTypeCapability, + SummaryUpdate, +}; + +const ALGORITHMS: [SketchAlgorithm; 9] = [ + SketchAlgorithm::UnivMon, + SketchAlgorithm::Kll, + SketchAlgorithm::Cms, + SketchAlgorithm::Hll, + SketchAlgorithm::DDSketch, + SketchAlgorithm::CmsWithHeap, + SketchAlgorithm::Kmv, + SketchAlgorithm::CountSketch, + SketchAlgorithm::CountSketchWithHeap, +]; + +const READOUTS: [StatisticKind; 7] = [ + StatisticKind::Total, + StatisticKind::ItemFrequency, + StatisticKind::Quantile, + StatisticKind::Distinct, + StatisticKind::FrequencyL2, + StatisticKind::FrequencyEntropy, + StatisticKind::TopK, +]; + +fn per_instance(algorithm: &SketchAlgorithm) -> FieldDataType { + FieldDataType::Sketch(*algorithm) +} + +/// A statistic of kind `readout`. +fn statistic(readout: StatisticKind) -> SketchStatistic { + let key = ColumnRef::Named("item".into()); + match readout { + // Pass 1 reads a total keyed by the sample value. + StatisticKind::Total => SketchStatistic::PointCount { + key: ColumnRef::SampleValue, + value: None, + }, + StatisticKind::ItemFrequency => SketchStatistic::PointCount { + key, + value: Some("a".into()), + }, + StatisticKind::Quantile => SketchStatistic::Quantile { q: 0.99 }, + StatisticKind::Distinct => SketchStatistic::Cardinality, + StatisticKind::FrequencyL2 => SketchStatistic::FrequencyL2, + StatisticKind::FrequencyEntropy => SketchStatistic::FrequencyEntropy, + StatisticKind::TopK => SketchStatistic::TopK { k: 5 }, + other => unreachable!("{other:?} is not probed"), + } +} + +/// Whether the factory builds the per-instance `family` from rows, with +/// a value column or keyed items. +fn factory_builds(family: &FieldDataType) -> bool { + let FieldDataType::Sketch(algorithm) = family else { + return false; + }; + let theta = planner_types::summary_type::parameter_list(*algorithm).first(); + let value = SummaryUpdate::column(ColumnRef::SampleValue); + let keyed = SummaryUpdate { + item: Some(SummaryInputExpr::Column(ColumnRef::Named("item".into()))), + ..value.clone() + }; + create_planner_accumulator(family, theta, &value).is_ok() + || create_planner_accumulator(family, theta, &keyed).is_ok() +} + +/// Whether the executor answers `readout` from `family`'s state: top-k +/// through the keyed heap evaluation, every other readout through the +/// sketch evaluation, whose kernel must then read it from a built state. +fn evaluation_answers(family: &FieldDataType, readout: StatisticKind) -> bool { + match (readout, family) { + (StatisticKind::TopK, FieldDataType::Sketch(kind)) => matches!( + kind, + planner_types::summary_type::SketchAlgorithm::CmsWithHeap + | planner_types::summary_type::SketchAlgorithm::CountSketchWithHeap + ), + _ => { + let accepted = validate_sketch_evaluation(family, &statistic(readout)).is_ok(); + if accepted { + assert!(kernel_answers(family, readout), "{family:?} {readout:?}"); + } + accepted + } + } +} + +/// Whether the kernel of a per-instance `family` state built from one +/// update reads `readout`. +fn kernel_answers(family: &FieldDataType, readout: StatisticKind) -> bool { + let FieldDataType::Sketch(algorithm) = family else { + return false; + }; + let theta = planner_types::summary_type::parameter_list(*algorithm).first(); + let value = SummaryUpdate::column(ColumnRef::SampleValue); + let keyed = SummaryUpdate { + item: Some(SummaryInputExpr::Column(ColumnRef::Named("item".into()))), + ..value.clone() + }; + let Ok(mut updater) = create_planner_accumulator(family, theta, &value) + .or_else(|_| create_planner_accumulator(family, theta, &keyed)) + else { + return false; + }; + let updated = if updater.is_keyed() { + updater.update_keyed(&["a".to_string()], 1.0, 0) + } else { + updater.update_single(1.0, 0) + }; + updated.is_ok() + && updater + .snapshot_accumulator() + .estimate(&statistic(readout)) + .is_ok() +} + +/// Whether the IR builds a structurally valid `SummaryEstimate` of +/// `readout` over a `family` state. +fn ir_builds_estimate(family: &FieldDataType, readout: StatisticKind) -> bool { + let scan = OperatorNode::new_shared(Operator::NonASAP(NonASAPOp::Scan { + source: Source::Table { + table_ref: "t".into(), + }, + predicates: vec![], + schema: Schema::new(vec![ + Field::plain("item", DataType::Utf8, false), + Field::plain("value", DataType::Float64, false), + ]), + })) + .unwrap(); + let state = OperatorNode::new_shared(Operator::ASAP(ASAPOp::SummaryAgg { + child: scan, + summary_type: family.clone(), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::by(vec![]), + filter: None, + })) + .unwrap(); + OperatorNode::new_shared(Operator::ASAP(ASAPOp::SummaryEstimate { + summary_input: state, + query: statistic(readout), + })) + .is_ok_and(|estimate| estimate.validate_structure().is_ok()) +} + +/// The list holds exactly the (family, readout) pairs the factory builds +/// per instance and an evaluation answers from that state, and the +/// IR's `SummaryEstimate` validation accepts exactly what the +/// family-capability table answers. Every declared readout is in the +/// family's table row. +#[test] +fn supported_summary_families_match_the_factory_and_evaluations() { + let listed = supported_summary_families(); + for algorithm in &ALGORITHMS { + // Per instance: Stage 1 builds every family per instance; the + // Hydra grid is not produced outside tests yet. + let family = per_instance(algorithm); + for readout in READOUTS { + let executed = factory_builds(&family) && evaluation_answers(&family, readout); + let declared = listed + .iter() + .any(|s| &s.summary_type == algorithm && s.readouts.contains(&readout)); + assert_eq!(declared, executed, "{algorithm:?} {readout:?}"); + let answered = SummaryTypeCapability::of(&family).answers(&statistic(readout)); + assert_eq!( + ir_builds_estimate(&family, readout), + answered, + "IR: {algorithm:?} {readout:?}" + ); + assert!( + !declared || answered, + "declared but not in the table: {algorithm:?} {readout:?}" + ); + } + } + assert!(listed.iter().all(|s| !s.readouts.is_empty())); +} diff --git a/crates/executor/src/values/batch.rs b/crates/executor/src/values/batch.rs deleted file mode 100644 index c125625e7..000000000 --- a/crates/executor/src/values/batch.rs +++ /dev/null @@ -1,1276 +0,0 @@ -//! A batch is a set of equal-length Arrow columns in one Planner schema, -//! with summary states held beside them. -use super::arrow_columns::{self, ColumnDepth}; -use super::{validate_schema, validate_state, SchemaRef, StateColumn, Value}; -use crate::{AggregateCore, Error}; -use arrow::array::{Array, ArrayRef, BooleanArray, UInt32Array}; -use arrow::datatypes::{DataType as ArrowType, Field as ArrowField, Schema as ArrowSchema}; -use arrow::record_batch::{RecordBatch, RecordBatchOptions}; -use planner_types::ir::schema::FieldDataType; -use std::sync::Arc; - -/// Each row is charged this many bytes on top of its column buffers, so a -/// batch of zero-width rows still consumes memory budget. -const ROW_SLOT_BYTES: usize = 8; - -/// The output of one operator: equal-length columns in one Planner schema. -/// -/// Plain columns are Arrow arrays in a [`RecordBatch`], in the layout -/// `arrow_columns` gives each Planner type. A summary column is a -/// `UInt32` array of state indices in the record batch, plus its -/// [`StateColumn`] beside it. Cloning a batch shares its buffers. -#[derive(Clone)] -pub struct Batch { - /// The Planner schema, which the columns follow field by field. - schema: SchemaRef, - /// One Arrow column per Planner field; a summary field holds state - /// indices into its entry of `state_columns`. - record_batch: RecordBatch, - /// Per Planner field, the states of a summary field; `None` for a plain - /// field. - state_columns: Arc<[Option>]>, -} - -impl std::fmt::Debug for Batch { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Batch") - .field("schema", &self.schema) - .field("num_rows", &self.num_rows()) - .finish() - } -} - -/// One column of a batch built with [`Batch::try_from_columns`]. -pub enum BatchColumn { - /// A plain column: an Arrow array in its Planner type's layout (see - /// `super::arrow_columns`: text is `DictionaryArray` over - /// `Utf8`, a timestamp `TimestampMillisecondArray`, …). - Plain(ArrayRef), - /// A summary column: row `i`'s state is `states.state(indices[i])`. - States { - /// Each row's state index; never NULL. - indices: UInt32Array, - /// The states, e.g. a sealed [`crate::StateColumnUpdater`]. - states: Arc, - }, -} - -/// The Arrow schema of a Planner schema's columns. -fn arrow_schema(schema: &SchemaRef) -> Arc { - Arc::new(ArrowSchema::new( - schema - .fields - .iter() - .map(|field| { - ArrowField::new( - &field.name, - match &field.dtype { - FieldDataType::Plain(dtype) => { - arrow_columns::arrow_type(dtype, ColumnDepth::BatchColumn) - } - _ => ArrowType::UInt32, - }, - true, - ) - }) - .collect::>(), - )) -} - -impl Batch { - /// The batch of `rows` in `schema`, encoded into columns (the rows are - /// not kept). For literal rows (`VALUES`, constants, tests); runtimes - /// build batches from columns with [`Self::try_from_columns`]. Bad input - /// when a row's width or a value's type differs from the schema, or a - /// summary state differs from its declared family or parameters. - pub fn try_new(schema: SchemaRef, rows: Vec>) -> Result { - validate_schema(&schema)?; - for row in &rows { - if row.len() != schema.fields.len() { - return Err(Error::Invalid( - "row width differs from Planner schema".into(), - )); - } - for (value, field) in row.iter().zip(&schema.fields) { - let matches = match (&field.dtype, value) { - (FieldDataType::Plain(dtype), value) => value.matches(dtype, field.nullable), - (expected, Value::Summary { family, state }) => { - expected == family && validate_state(family, state.as_ref()).is_ok() - } - _ => false, - }; - if !matches { - return Err(Error::Invalid(format!( - "value differs from type of {}", - field.name - ))); - } - } - } - let mut arrays = Vec::with_capacity(schema.fields.len()); - let mut state_columns = Vec::with_capacity(schema.fields.len()); - for (index, field) in schema.fields.iter().enumerate() { - let cells = rows.iter().map(|row| &row[index]).collect::>(); - match &field.dtype { - FieldDataType::Plain(dtype) => { - arrays.push(arrow_columns::encode( - dtype, - ColumnDepth::BatchColumn, - &cells, - )?); - state_columns.push(None); - } - family => { - // Rows sharing one state object keep sharing it. - let mut positions = std::collections::HashMap::new(); - let mut states: Vec> = Vec::new(); - let indices = cells - .iter() - .map(|cell| { - let Value::Summary { state, .. } = cell else { - unreachable!("validated above"); - }; - let identity = Arc::as_ptr(state) as *const () as usize; - *positions.entry(identity).or_insert_with(|| { - states.push(state.clone()); - (states.len() - 1) as u32 - }) - }) - .collect::(); - arrays.push(Arc::new(indices)); - state_columns.push(Some(Arc::new(StateColumn::from_objects( - family.clone(), - states, - )))); - } - } - } - Self::from_parts(schema, arrays, state_columns, rows.len()) - } - - /// The batch of `columns` in `schema`, one per field, without running - /// the DAG: how a runtime installs states it built sample by sample - /// (a sealed [`crate::StateColumnUpdater`]) as a stored tumbling window. Bad input - /// when a column's count, layout, length or nullability differs from - /// the schema, a dictionary holds a NULL value (a NULL is a NULL key), - /// a summary field's states are of another family, or a state index is - /// NULL or past its states. - /// - /// A stored tumbling window has one row per group: the caller must not repeat a - /// group's labels (two rows of one series would be merged as two - /// tumbling windows of it). This is not checked, as it would cost a pass grouping - /// every row. - pub fn try_from_columns(schema: SchemaRef, columns: Vec) -> Result { - validate_schema(&schema)?; - if columns.len() != schema.fields.len() { - return Err(Error::Invalid( - "column count differs from Planner schema".into(), - )); - } - let row_count = columns.first().map_or(0, |column| match column { - BatchColumn::Plain(array) => array.len(), - BatchColumn::States { indices, .. } => indices.len(), - }); - let mut arrays = Vec::with_capacity(columns.len()); - let mut state_columns = Vec::with_capacity(columns.len()); - for (column, field) in columns.into_iter().zip(&schema.fields) { - match (column, &field.dtype) { - (BatchColumn::Plain(array), FieldDataType::Plain(dtype)) => { - // A NULL is a NULL key, never a NULL dictionary value, - // so every dictionary read finds a text. - if array - .as_any() - .downcast_ref::>() - .is_some_and(|dictionary| dictionary.values().null_count() > 0) - { - return Err(Error::Invalid(format!( - "column {} has a NULL dictionary value; a NULL is a NULL key", - field.name - ))); - } - if array.data_type() - != &arrow_columns::arrow_type(dtype, ColumnDepth::BatchColumn) - { - return Err(Error::Invalid(format!( - "column {} is not in its type's Arrow layout", - field.name - ))); - } - arrays.push(array); - state_columns.push(None); - } - (BatchColumn::States { indices, states }, family) - if !matches!(family, FieldDataType::Plain(_)) => - { - if states.family() != family { - return Err(Error::Invalid(format!( - "states of {} are of another family", - field.name - ))); - } - if indices.null_count() > 0 - || indices.values().iter().any(|&i| i as usize >= states.len()) - { - return Err(Error::Invalid(format!( - "a state index of {} is NULL or past its states", - field.name - ))); - } - arrays.push(Arc::new(indices)); - state_columns.push(Some(states)); - } - _ => { - return Err(Error::Invalid(format!( - "column {} is a summary column exactly when its field is", - field.name - ))) - } - } - } - if arrays.iter().any(|array| array.len() != row_count) { - return Err(Error::Invalid("columns differ in length".into())); - } - Self::from_parts(schema, arrays, state_columns, row_count) - } - - /// The batch of `arrays` in `schema`, with `state_columns` for its - /// summary fields. The arrays must have the layout of their Planner - /// types ([`arrow_columns`]) and `row_count` rows; a summary field's - /// array holds indices into its state column. Internal callers only. - pub(crate) fn from_parts( - schema: SchemaRef, - arrays: Vec, - state_columns: Vec>>, - row_count: usize, - ) -> Result { - if arrays.len() != schema.fields.len() || state_columns.len() != schema.fields.len() { - return Err(Error::Invalid( - "column count differs from Planner schema".into(), - )); - } - for ((array, states), field) in arrays.iter().zip(&state_columns).zip(&schema.fields) { - let plain = matches!(field.dtype, FieldDataType::Plain(_)); - if plain == states.is_some() { - return Err(Error::Invalid(format!( - "column {} has states exactly when it is a summary column", - field.name - ))); - } - if !field.nullable - && field.dtype != FieldDataType::Plain(planner_types::ir::schema::DataType::Null) - && array.logical_null_count() > 0 - { - return Err(Error::Invalid(format!( - "value differs from type of {}", - field.name - ))); - } - } - let record_batch = RecordBatch::try_new_with_options( - arrow_schema(&schema), - arrays, - &RecordBatchOptions::new().with_row_count(Some(row_count)), - ) - .map_err(|error| Error::Invalid(format!("columnar batch: {error}")))?; - Ok(Self { - schema, - record_batch, - state_columns: state_columns.into(), - }) - } - - /// Rows `offset..offset + length`, sharing the column buffers. - pub(crate) fn slice(&self, offset: usize, length: usize) -> Self { - if offset == 0 && length == self.num_rows() { - return self.clone(); - } - Self { - schema: self.schema.clone(), - record_batch: self.record_batch.slice(offset, length), - state_columns: self.state_columns.clone(), - } - } - - /// The rows where `keep` is true (not false or null), in order. Shares - /// the state columns; copies only the kept cells of plain columns. - pub(crate) fn filter(&self, keep: &BooleanArray) -> Result { - if keep.true_count() == self.num_rows() && keep.null_count() == 0 { - return Ok(self.clone()); - } - let record_batch = arrow::compute::filter_record_batch(&self.record_batch, keep) - .map_err(|error| Error::Invalid(format!("columnar filter: {error}")))?; - Self { - schema: self.schema.clone(), - record_batch, - state_columns: self.state_columns.clone(), - } - .compacted() - } - - /// This batch with each state column that its rows use less than half - /// of reduced to the states they use (exact records kept as records), - /// so a selective filter does not hold every state of its input. - fn compacted(self) -> Result { - let mut arrays = self.record_batch.columns().to_vec(); - let mut states = self.state_columns.to_vec(); - let mut changed = false; - for (column, slot) in states.iter_mut().enumerate() { - let Some(state_column) = slot.clone() else { - continue; - }; - let indices = arrays[column] - .as_any() - .downcast_ref::() - .expect("summary columns hold state indices") - .clone(); - if 2 * indices.len() >= state_column.len() { - continue; - } - let mut moved = ahash::AHashMap::::new(); - let mut used = Vec::new(); - let remapped = indices - .values() - .iter() - .map(|&index| { - *moved.entry(index).or_insert_with(|| { - used.push(index as usize); - (used.len() - 1) as u32 - }) - }) - .collect::(); - if used.len() == state_column.len() { - continue; - } - let family = state_column.family().clone(); - *slot = Some(Arc::new(match state_column.storage() { - super::StateStorage::Exact(records) => StateColumn::from_exact( - family, - used.iter().map(|&index| records[index].clone()).collect(), - ), - // Shared by pointer: stored sketches are not charged again. - super::StateStorage::Objects(_) => StateColumn::from_objects_of( - family, - used.iter() - .map(|&index| { - ( - state_column.state(index), - state_column.state_held_outside_run(index), - ) - }) - .collect(), - ), - })); - arrays[column] = Arc::new(remapped); - changed = true; - } - if !changed { - return Ok(self); - } - let rows = self.num_rows(); - Self::from_parts(self.schema, arrays, states, rows) - } - - /// The rows `indices` (in that order, repeats allowed), sharing the - /// state columns. A NULL index gives a row of NULLs, which a summary - /// column cannot hold (bad input). - pub(crate) fn take(&self, indices: &UInt32Array) -> Result { - if indices.null_count() > 0 && self.state_columns.iter().any(Option::is_some) { - return Err(Error::Invalid("a summary column has no NULL row".into())); - } - let arrays = self - .record_batch - .columns() - .iter() - .map(|array| { - arrow::compute::take(array.as_ref(), indices, None) - .map_err(|error| Error::Invalid(format!("columnar take: {error}"))) - }) - .collect::, _>>()?; - Self::from_parts( - self.schema.clone(), - arrays, - self.state_columns.to_vec(), - indices.len(), - ) - } - - /// The columns `columns` of this batch (shared, not copied), as a batch - /// of `schema`. - pub(crate) fn project(&self, schema: SchemaRef, columns: &[usize]) -> Result { - Self::from_parts( - schema, - columns.iter().map(|&c| self.column(c).clone()).collect(), - columns - .iter() - .map(|&c| self.state_column(c).cloned()) - .collect(), - self.num_rows(), - ) - } - - /// This batch with column `index` replaced by `array` (a plain column - /// of its field's layout). - pub(crate) fn with_plain_column(&self, index: usize, array: ArrayRef) -> Result { - let mut arrays = self.record_batch.columns().to_vec(); - let mut states = self.state_columns.to_vec(); - arrays[index] = array; - states[index] = None; - Self::from_parts(self.schema.clone(), arrays, states, self.num_rows()) - } - - /// `batches`, all in `schema`, as one batch: plain columns copied, - /// state columns joined (shared when every batch shares one). - pub(crate) fn concat(schema: &SchemaRef, batches: &[Self]) -> Result { - match batches { - [] => return Self::try_new(schema.clone(), Vec::new()), - [batch] => return Ok(batch.clone()), - _ => {} - } - let rows = batches.iter().map(Self::num_rows).sum(); - let mut arrays = Vec::with_capacity(schema.fields.len()); - let mut states = Vec::with_capacity(schema.fields.len()); - for column in 0..schema.fields.len() { - let pieces = batches - .iter() - .map(|batch| batch.column(column).as_ref()) - .collect::>(); - let columns = batches - .iter() - .map(|batch| batch.state_column(column)) - .collect::>(); - let Some(Some(first)) = columns.first().copied() else { - arrays.push( - arrow::compute::concat(&pieces) - .map_err(|error| Error::Invalid(format!("columnar concat: {error}")))?, - ); - states.push(None); - continue; - }; - if columns - .iter() - .all(|other| other.is_some_and(|other| Arc::ptr_eq(other, first))) - { - arrays.push( - arrow::compute::concat(&pieces) - .map_err(|error| Error::Invalid(format!("columnar concat: {error}")))?, - ); - states.push(Some(first.clone())); - continue; - } - // Different state columns: one column of the states the rows - // reference (each once), exact records kept as records. - let all_exact = columns.iter().all(|column| { - matches!( - column.map(|column| column.storage()), - Some(super::StateStorage::Exact(_)) - ) - }); - let mut indices = Vec::with_capacity(rows); - let mut objects = Vec::new(); - let mut records = Vec::new(); - for (batch, state_column) in batches.iter().zip(&columns) { - let state_column = state_column.expect("one schema"); - let local = batch - .column(column) - .as_any() - .downcast_ref::() - .expect("summary columns hold state indices"); - let mut moved = ahash::AHashMap::::new(); - for &index in local.values().iter() { - let next = (objects.len() + records.len()) as u32; - let new_index = *moved.entry(index).or_insert_with(|| { - match state_column.storage() { - super::StateStorage::Exact(states) if all_exact => { - records.push(states[index as usize].clone()) - } - // An object is shared by pointer (not charged - // again when held outside the run); an exact - // record becomes a new object. - super::StateStorage::Objects(_) => objects.push(( - state_column.state(index as usize), - state_column.state_held_outside_run(index as usize), - )), - super::StateStorage::Exact(_) => { - objects.push((state_column.state(index as usize), false)) - } - } - next - }); - indices.push(new_index); - } - } - arrays.push(Arc::new(UInt32Array::from(indices)) as ArrayRef); - let family = first.family().clone(); - states.push(Some(Arc::new(if all_exact { - StateColumn::from_exact(family, records) - } else { - StateColumn::from_objects_of(family, objects) - }))); - } - Self::from_parts(schema.clone(), arrays, states, rows) - } - - /// The Planner schema of the columns. - pub fn schema(&self) -> &SchemaRef { - &self.schema - } - - /// The number of rows. - pub fn num_rows(&self) -> usize { - self.record_batch.num_rows() - } - - /// The Arrow columns, one per Planner field. A summary field's column - /// holds `UInt32` indices into [`Self::state_column`]. - pub fn record_batch(&self) -> &RecordBatch { - &self.record_batch - } - - /// The Arrow column of field `index`. - pub fn column(&self, index: usize) -> &ArrayRef { - self.record_batch.column(index) - } - - /// The states of summary field `index`; `None` for a plain field. - pub fn state_column(&self, index: usize) -> Option<&Arc> { - self.state_columns.get(index).and_then(Option::as_ref) - } - - /// The rows as values, decoded from the columns on every call: for - /// results leaving the executor and for tests. Operators read columns. - pub fn to_rows(&self) -> Vec> { - { - let length = self.num_rows(); - let width = self.schema.fields.len(); - let mut rows: Vec> = - (0..length).map(|_| Vec::with_capacity(width)).collect(); - for (index, field) in self.schema.fields.iter().enumerate() { - let array = self.record_batch.column(index); - let column = match (&field.dtype, self.state_column(index)) { - (FieldDataType::Plain(dtype), _) => { - arrow_columns::decode(array.as_ref(), dtype, ColumnDepth::BatchColumn) - } - (_, Some(states)) => { - let indices = array - .as_any() - .downcast_ref::() - .expect("summary columns hold state indices"); - indices - .values() - .iter() - .map(|&i| Value::Summary { - family: states.family().clone(), - state: states.state(i as usize), - }) - .collect() - } - (_, None) => unreachable!("summary columns have states"), - }; - for (row, value) in rows.iter_mut().zip(column) { - row.push(value); - } - } - rows - } - } - - /// Records that something outside any run (the caller of a plan, a - /// store of tumbling windows) holds this batch's states, so runs that read them - /// are not charged for them again. States a run copies or creates from - /// them are new state columns and are charged. - pub(crate) fn mark_states_held_outside_run(&self) { - for column in self.state_columns.iter().flatten() { - column.mark_held_outside_run(); - } - } - - /// Memory the batch holds, for execution memory reservations: its - /// column buffers, a fixed slot per row, and each state column once, - /// except states held outside the run (see - /// `Self::mark_states_held_outside_run`). Constant time in the number - /// of rows. - pub fn memory_bytes(&self) -> usize { - let mut seen = std::collections::BTreeSet::new(); - let states = self - .state_columns - .iter() - .flatten() - .filter(|column| seen.insert(Arc::as_ptr(column))) - .map(|column| column.charged_state_bytes()) - .sum::(); - std::mem::size_of::() - + self.record_batch.get_array_memory_size() - + self.num_rows() * ROW_SLOT_BYTES - + states - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::summary_kernels::DDSketchAccumulator; - use planner_types::ir::schema::{DataType, Field, Schema}; - use planner_types::summary_type::SketchAlgorithm; - - fn field(name: &str, dtype: DataType, nullable: bool) -> Field { - Field { - name: name.into(), - dtype: FieldDataType::Plain(dtype), - nullable, - } - } - - fn schema(fields: Vec) -> SchemaRef { - Arc::new(Schema { - fields, - time_index: None, - unique_keys: vec![], - closed: false, - }) - } - - /// Exact equality, including float bits (`Value`'s key merges zero signs). - fn same(a: &Value, b: &Value) -> bool { - match (a, b) { - (Value::Float64(a), Value::Float64(b)) => a.to_bits() == b.to_bits(), - (Value::List(a), Value::List(b)) | (Value::Struct(a), Value::Struct(b)) => { - a.len() == b.len() && a.iter().zip(b.iter()).all(|(a, b)| same(a, b)) - } - (Value::Map(a), Value::Map(b)) => { - a.len() == b.len() - && a.iter() - .zip(b.iter()) - .all(|((ak, av), (bk, bv))| same(ak, bk) && same(av, bv)) - } - (Value::Summary { state: a, .. }, Value::Summary { state: b, .. }) => Arc::ptr_eq(a, b), - _ => format!("{a:?}") == format!("{b:?}"), - } - } - - /// The rows as values. - fn decoded(batch: &Batch) -> Vec> { - batch.to_rows() - } - - // Every Planner type survives the trip into Arrow columns and back, - // nulls, NaN and negative zero included. - #[test] - fn rows_round_trip_through_arrow_columns() { - let element = Field::::new("item", DataType::Float64, true); - let inner = vec![ - Field:: { - name: "a".into(), - dtype: DataType::Int64, - nullable: false, - }, - Field:: { - name: "b".into(), - dtype: DataType::Utf8, - nullable: true, - }, - ]; - let labels = DataType::Map { - key: Box::new(DataType::Utf8), - value: Box::new(DataType::Utf8), - value_nullable: true, - }; - let schema = schema(vec![ - field("null", DataType::Null, true), - field("bool", DataType::Bool, true), - field("int", DataType::Int64, true), - field("float", DataType::Float64, false), - field("text", DataType::Utf8, true), - field("time", DataType::Timestamp, false), - field("date", DataType::Date, true), - field("interval", DataType::Interval, true), - field( - "list", - DataType::List { - element: Box::new(element), - }, - true, - ), - field("struct", DataType::Struct { fields: inner }, true), - field("map", labels, true), - ]); - let text = |s: &str| Value::Utf8(s.into()); - let rows = vec![ - vec![ - Value::Null, - Value::Bool(true), - Value::Int64(-3), - Value::Float64(-0.0), - text("api"), - Value::Timestamp(1_000), - Value::Date(7), - Value::Interval { - months: 1, - days: 2, - nanos: 3, - }, - Value::List(vec![Value::Float64(f64::NAN), Value::Float64(1.5)].into()), - Value::Struct(vec![Value::Int64(1), Value::Null].into()), - Value::Map(vec![(text("job"), text("api")), (text("x"), Value::Null)].into()), - ], - vec![ - Value::Null, - Value::Null, - Value::Null, - Value::Float64(f64::INFINITY), - Value::Null, - Value::Timestamp(-5), - Value::Null, - Value::Null, - Value::Null, - Value::Null, - Value::Null, - ], - vec![ - Value::Null, - Value::Bool(false), - Value::Int64(i64::MAX), - Value::Float64(2.0), - text("api"), - Value::Timestamp(0), - Value::Date(-1), - Value::Interval { - months: 0, - days: 0, - nanos: 0, - }, - Value::List(Arc::from([])), - Value::Struct(vec![Value::Int64(2), text("")].into()), - Value::Map(Arc::from([])), - ], - ]; - let batch = Batch::try_new(schema, rows.clone()).unwrap(); - assert_eq!(batch.num_rows(), 3); - let back = decoded(&batch); - for (expected, actual) in rows.iter().zip(&back) { - for (e, a) in expected.iter().zip(actual) { - assert!(same(e, a), "{e:?} became {a:?}"); - } - } - } - - // A batch text column is a dictionary with one entry per distinct value. - #[test] - fn text_columns_are_dictionary_encoded() { - use arrow::array::{AsArray, DictionaryArray}; - let schema = schema(vec![field("job", DataType::Utf8, false)]); - let rows = ["api", "web", "api", "api", "web"] - .iter() - .map(|job| vec![Value::Utf8((*job).into())]) - .collect(); - let batch = Batch::try_new(schema, rows).unwrap(); - let column: &DictionaryArray = - batch.column(0).as_dictionary(); - assert_eq!(column.values().len(), 2); - assert_eq!(column.keys().values().to_vec(), vec![0, 1, 0, 0, 1]); - } - - fn dd_family() -> FieldDataType { - FieldDataType::Sketch(SketchAlgorithm::DDSketch) - } - - // Rows sharing a state keep sharing one state; filtering rows shares the - // state column instead of copying sketches. - #[test] - fn state_columns_hold_each_state_once_and_filter_shares_them() { - let schema = Arc::new(Schema { - fields: vec![ - field("job", DataType::Utf8, false), - Field { - name: "state".into(), - dtype: dd_family(), - nullable: false, - }, - ], - time_index: None, - unique_keys: vec![], - closed: false, - }); - let shared: Arc = Arc::new(DDSketchAccumulator::new(0.01)); - let other: Arc = Arc::new(DDSketchAccumulator::new(0.01)); - let state = |s: &Arc| Value::Summary { - family: dd_family(), - state: s.clone(), - }; - let rows = vec![ - vec![Value::Utf8("a".into()), state(&shared)], - vec![Value::Utf8("b".into()), state(&other)], - vec![Value::Utf8("c".into()), state(&shared)], - ]; - let batch = Batch::try_new(schema, rows).unwrap(); - let states = batch.state_column(1).unwrap(); - assert_eq!(states.len(), 2); - assert!(batch.state_column(0).is_none()); - let kept = batch - .filter(&BooleanArray::from(vec![true, false, true])) - .unwrap(); - assert!(Arc::ptr_eq(kept.state_column(1).unwrap(), states)); - let rows = decoded(&kept); - assert_eq!(rows.len(), 2); - assert!(same(&rows[0][1], &state(&shared)) && same(&rows[1][1], &state(&shared))); - } - - // Memory is counted per batch from its buffers; zero-width rows still - // cost their slot. - #[test] - fn zero_width_rows_consume_memory() { - let empty = schema(vec![]); - let none = Batch::try_new(empty.clone(), vec![]).unwrap(); - let many = Batch::try_new(empty, vec![vec![]; 200]).unwrap(); - assert_eq!(many.num_rows(), 200); - assert_eq!( - many.memory_bytes() - none.memory_bytes(), - 200 * ROW_SLOT_BYTES - ); - } - - // A runtime can share batches, stored tumbling windows included, across threads. - #[test] - fn batches_and_state_columns_are_send_and_sync() { - fn shareable() {} - shareable::(); - shareable::(); - } - - // A value of the wrong type or width is rejected, as before. - #[test] - fn rows_must_match_the_schema() { - let schema = schema(vec![field("v", DataType::Int64, false)]); - assert!(Batch::try_new(schema.clone(), vec![vec![Value::Null]]).is_err()); - assert!(Batch::try_new(schema.clone(), vec![vec![Value::Float64(1.)]]).is_err()); - assert!(Batch::try_new(schema, vec![vec![]]).is_err()); - } - - // A runtime turns sealed per-sample states and their group columns into - // a batch: the states and labels read back as the rows say. - #[test] - fn a_batch_from_columns_holds_sealed_states_and_labels() { - use crate::StateColumnUpdater; - use planner_types::summary_type::{ExactKind, ExactParams}; - let family = FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum); - let mut updater = StateColumnUpdater::new(&family, None).unwrap(); - updater.ensure_groups(2).unwrap(); - updater.update(0, 1.5, 0).unwrap(); - updater.update(1, 4.0, 0).unwrap(); - updater.update(0, 2.0, 0).unwrap(); - let states = Arc::new(updater.seal()); - let schema = Arc::new(Schema { - fields: vec![ - field("job", DataType::Utf8, false), - Field { - name: "state".into(), - dtype: family.clone(), - nullable: false, - }, - ], - time_index: None, - unique_keys: vec![], - closed: false, - }); - let jobs: ArrayRef = Arc::new( - ["api", "web"] - .into_iter() - .collect::>(), - ); - let batch = Batch::try_from_columns( - schema.clone(), - vec![ - BatchColumn::Plain(jobs.clone()), - BatchColumn::States { - indices: UInt32Array::from(vec![1, 0]), - states: states.clone(), - }, - ], - ) - .unwrap(); - let rows = batch.to_rows(); - let sum = |value: &Value| { - let Value::Summary { state, .. } = value else { - panic!() - }; - state - .as_any() - .downcast_ref::() - .unwrap() - .evaluation(planner_types::summary_type::ExactKind::Sum, None, None) - .unwrap() - .unwrap() - }; - assert_eq!( - format!("{:?}", rows[0][0]), - format!("{:?}", Value::Utf8("api".into())) - ); - assert_eq!((sum(&rows[0][1]), sum(&rows[1][1])), (4.0, 3.5)); - // Bad input: a column of another layout, a state index past the - // column, another family, or columns of different lengths. - let plain_text: ArrayRef = Arc::new(arrow::array::StringArray::from(vec!["api", "web"])); - let indices = |values: Vec| BatchColumn::States { - indices: UInt32Array::from(values), - states: states.clone(), - }; - let other_family = Arc::new( - StateColumnUpdater::new( - &FieldDataType::ExactAggregate(ExactKind::Count, ExactParams::Count), - None, - ) - .unwrap() - .seal(), - ); - for columns in [ - vec![BatchColumn::Plain(plain_text), indices(vec![0, 1])], - vec![BatchColumn::Plain(jobs.clone()), indices(vec![0, 2])], - vec![ - BatchColumn::Plain(jobs.clone()), - BatchColumn::States { - indices: UInt32Array::from(vec![0, 0]), - states: other_family, - }, - ], - vec![BatchColumn::Plain(jobs.clone()), indices(vec![0])], - vec![indices(vec![0, 1]), BatchColumn::Plain(jobs.clone())], - ] { - assert!(matches!( - Batch::try_from_columns(schema.clone(), columns), - Err(Error::Invalid(_)) - )); - } - } - - // A NULL among a dictionary's values is a NULL in the column: refused - // in a non-nullable field, not read back as "". - #[test] - fn a_null_dictionary_value_is_a_null() { - use arrow::array::{DictionaryArray, StringArray}; - let schema = Arc::new(Schema { - fields: vec![field("job", DataType::Utf8, false)], - time_index: None, - unique_keys: vec![], - closed: false, - }); - let jobs = DictionaryArray::::try_new( - UInt32Array::from(vec![0, 1]), - Arc::new(StringArray::from(vec![Some("a"), None])), - ) - .unwrap(); - assert!(matches!( - Batch::try_from_columns(schema, vec![BatchColumn::Plain(Arc::new(jobs.clone()))]), - Err(Error::Invalid(_)) - )); - // A nullable field refuses it too: a NULL is a NULL key, so every - // dictionary read finds a text. A NULL key there reads NULL, in rows - // and in the column's logical nulls. - let nullable = Arc::new(Schema { - fields: vec![field("job", DataType::Utf8, true)], - time_index: None, - unique_keys: vec![], - closed: false, - }); - assert!(matches!( - Batch::try_from_columns(nullable.clone(), vec![BatchColumn::Plain(Arc::new(jobs))]), - Err(Error::Invalid(_)) - )); - let null_key = DictionaryArray::::try_new( - UInt32Array::from(vec![Some(0), None]), - Arc::new(StringArray::from(vec!["a"])), - ) - .unwrap(); - let batch = Batch::try_from_columns(nullable, vec![BatchColumn::Plain(Arc::new(null_key))]) - .unwrap(); - let rows = batch.to_rows(); - assert!(matches!(&rows[0][0], Value::Utf8(job) if job.as_ref() == "a")); - assert!(matches!(rows[1][0], Value::Null)); - let nulls = batch.column(0).logical_nulls().unwrap(); - assert!(nulls.is_valid(0) && nulls.is_null(1)); - } - - // A filter that keeps few rows keeps only their states: a tumbling window filtered - // to two of ten series holds two states, not ten. - #[test] - fn a_selective_filter_compacts_its_state_column() { - use crate::StateColumnUpdater; - use planner_types::summary_type::{ExactKind, ExactParams}; - let family = FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum); - let mut updater = StateColumnUpdater::new(&family, None).unwrap(); - updater.ensure_groups(10).unwrap(); - for group in 0..10 { - updater.update(group, f64::from(group), 0).unwrap(); - } - let schema = Arc::new(Schema { - fields: vec![ - field("k", DataType::Int64, false), - Field { - name: "state".into(), - dtype: family, - nullable: false, - }, - ], - time_index: None, - unique_keys: vec![], - closed: false, - }); - let batch = Batch::try_from_columns( - schema, - vec![ - BatchColumn::Plain(Arc::new(arrow::array::Int64Array::from_iter_values(0..10))), - BatchColumn::States { - indices: UInt32Array::from_iter_values(0..10), - states: Arc::new(updater.seal()), - }, - ], - ) - .unwrap(); - let keep = BooleanArray::from((0..10).map(|i| i == 3 || i == 7).collect::>()); - let kept = batch.filter(&keep).unwrap(); - assert_eq!(kept.state_column(1).unwrap().len(), 2); - assert!(kept.memory_bytes() < batch.memory_bytes()); - // The rows still read their own states. - let sums = decoded(&kept) - .iter() - .map(|row| { - let Value::Summary { state, .. } = &row[1] else { - panic!() - }; - state - .as_any() - .downcast_ref::() - .unwrap() - .evaluation(planner_types::summary_type::ExactKind::Sum, None, None) - .unwrap() - .unwrap() - }) - .collect::>(); - assert_eq!(sums, vec![3.0, 7.0]); - } - - // Two batches over different exact state columns join into one exact - // column of the states their rows reference, each once, still records. - #[test] - fn concat_of_different_exact_columns_keeps_records() { - use crate::StateColumnUpdater; - use planner_types::summary_type::{ExactKind, ExactParams}; - let family = FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum); - let schema = Arc::new(Schema { - fields: vec![Field { - name: "state".into(), - dtype: family.clone(), - nullable: false, - }], - time_index: None, - unique_keys: vec![], - closed: false, - }); - // States [10, 20, 30] referenced as rows [2, 0, 2]; states [1, 2] - // referenced as rows [1]. - let batch = |sums: &[f64], rows: Vec| { - let mut updater = StateColumnUpdater::new(&family, None).unwrap(); - updater.ensure_groups(sums.len()).unwrap(); - for (group, &sum) in sums.iter().enumerate() { - updater.update(group as u32, sum, 0).unwrap(); - } - Batch::try_from_columns( - schema.clone(), - vec![BatchColumn::States { - indices: UInt32Array::from(rows), - states: Arc::new(updater.seal()), - }], - ) - .unwrap() - }; - let first = batch(&[10., 20., 30.], vec![2, 0, 2]); - let second = batch(&[1., 2.], vec![1]); - assert!(matches!( - first.state_column(0).unwrap().storage(), - super::super::StateStorage::Exact(_) - )); - let joined = Batch::concat(&schema, &[first, second]).unwrap(); - let states = joined.state_column(0).unwrap(); - let super::super::StateStorage::Exact(records) = states.storage() else { - panic!("exact states stay records"); - }; - assert_eq!(records.len(), 3); - let indices = joined - .column(0) - .as_any() - .downcast_ref::() - .unwrap(); - assert_eq!(indices.values().to_vec(), vec![0, 1, 0, 2]); - let sums = decoded(&joined) - .iter() - .map(|row| { - let Value::Summary { state, .. } = &row[0] else { - panic!() - }; - state - .as_any() - .downcast_ref::() - .unwrap() - .evaluation(planner_types::summary_type::ExactKind::Sum, None, None) - .unwrap() - .unwrap() - }) - .collect::>(); - assert_eq!(sums, vec![30.0, 10.0, 30.0, 2.0]); - } - - /// A batch of `count` rows, one state each: KLL (k = 200) objects, or - /// exact sums. - fn state_batch(count: u32, exact: bool) -> Batch { - use crate::StateColumnUpdater; - use planner_types::summary_type::{ExactKind, ExactParams, SketchAlgorithm}; - let (family, parameters) = if exact { - ( - FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum), - None, - ) - } else { - ( - FieldDataType::Sketch(SketchAlgorithm::Kll), - Some(planner_types::summary_type::SketchParams::Kll { k: 200 }), - ) - }; - let mut updater = StateColumnUpdater::new(&family, parameters.as_ref()).unwrap(); - updater.ensure_groups(count as usize).unwrap(); - for group in 0..count { - updater.update(group, f64::from(group), 0).unwrap(); - } - let schema = Arc::new(Schema { - fields: vec![Field { - name: "state".into(), - dtype: family, - nullable: false, - }], - time_index: None, - unique_keys: vec![], - closed: false, - }); - Batch::try_from_columns( - schema, - vec![BatchColumn::States { - indices: UInt32Array::from_iter_values(0..count), - states: Arc::new(updater.seal()), - }], - ) - .unwrap() - } - - /// The bytes `batch` is charged for its state columns. - fn charged_states(batch: &Batch) -> usize { - batch - .state_columns - .iter() - .flatten() - .map(|column| column.charged_state_bytes()) - .sum() - } - - // Compacting or concatenating stored (held outside the run) object - // columns shares their sketches by pointer: the new column is charged - // its pointer slots, not the sketches. - #[test] - fn derived_columns_of_stored_sketches_charge_only_pointers() { - let stored = state_batch(100, false); - stored.mark_states_held_outside_run(); - // Each derived state: its pointer slot and its "held outside" bit. - let pointers = - |states: usize| states * (std::mem::size_of::>() + 1); - // Compaction: 10 of 100 rows kept. - let keep = BooleanArray::from((0..100).map(|i| i % 10 == 0).collect::>()); - let kept = stored.filter(&keep).unwrap(); - assert_eq!(kept.state_column(0).unwrap().len(), 10); - assert_eq!(charged_states(&kept), pointers(10)); - // Concat of two different stored columns. - let other = state_batch(50, false); - other.mark_states_held_outside_run(); - let joined = Batch::concat(stored.schema(), &[stored.clone(), other]).unwrap(); - assert_eq!(joined.state_column(0).unwrap().len(), 150); - assert_eq!(charged_states(&joined), pointers(150)); - // A run-built column concatenated with a stored one: only the - // run-built sketches are charged in full. - let built = state_batch(50, false); - let built_bytes = charged_states(&built); - let mixed = Batch::concat(&stored.schema().clone(), &[stored, built]).unwrap(); - assert_eq!(charged_states(&mixed), pointers(150) + built_bytes); - } - - // Exact records are copied, so compacting or concatenating stored exact - // columns is charged for the copied records. - #[test] - fn derived_columns_of_stored_exact_records_charge_the_copies() { - let stored = state_batch(100, true); - stored.mark_states_held_outside_run(); - assert_eq!(charged_states(&stored), 0); - let record = std::mem::size_of::(); - let keep = BooleanArray::from((0..100).map(|i| i % 10 == 0).collect::>()); - let kept = stored.filter(&keep).unwrap(); - assert_eq!(charged_states(&kept), 10 * record); - let other = state_batch(50, true); - other.mark_states_held_outside_run(); - let joined = Batch::concat(&stored.schema().clone(), &[stored, other]).unwrap(); - assert!(charged_states(&joined) >= 150 * record); - } - - // Deriving twice keeps each sketch's "held outside the run" bit: concat - // then compaction, or compaction then concat, of stored sketches is - // charged only pointers; run-built sketches stay charged in full. - #[test] - fn derived_twice_stored_sketches_still_charge_only_pointers() { - let slot = std::mem::size_of::>(); - let stored = |count| { - let batch = state_batch(count, false); - batch.mark_states_held_outside_run(); - batch - }; - let every_other = - |rows: usize| BooleanArray::from((0..rows).map(|i| i % 10 == 0).collect::>()); - // Concat, then compaction. - let (a, b) = (stored(100), stored(100)); - let schema = a.schema().clone(); - let joined = Batch::concat(&schema, &[a, b]).unwrap(); - let kept = joined.filter(&every_other(200)).unwrap(); - assert_eq!(kept.state_column(0).unwrap().len(), 20); - assert!(charged_states(&kept) <= 20 * (slot + 1)); - // Compaction, then concat. - let (a, b) = (stored(100), stored(100)); - let (a, b) = ( - a.filter(&every_other(100)).unwrap(), - b.filter(&every_other(100)).unwrap(), - ); - let joined = Batch::concat(&schema, &[a, b]).unwrap(); - assert_eq!(joined.state_column(0).unwrap().len(), 20); - assert!(charged_states(&joined) <= 20 * (slot + 1)); - // Mixed: 50 run-built and 100 stored sketches, concatenated, then - // compacted to every tenth row (5 run-built, 10 stored), then - // concatenated with another compacted stored batch (10 stored). - let built = state_batch(50, false); - let one_built = charged_states(&built) / 50; - let mixed = Batch::concat(&schema, &[built, stored(100)]).unwrap(); - let kept = mixed.filter(&every_other(150)).unwrap(); - assert_eq!(kept.state_column(0).unwrap().len(), 15); - let charged = charged_states(&kept); - assert!(charged >= 5 * one_built && charged <= 5 * one_built + 15 * (slot + 1)); - let other = stored(100).filter(&every_other(100)).unwrap(); - let joined = Batch::concat(&schema, &[kept, other]).unwrap(); - assert_eq!(joined.state_column(0).unwrap().len(), 25); - let charged = charged_states(&joined); - assert!(charged >= 5 * one_built && charged <= 5 * one_built + 25 * (slot + 1)); - } - - // An index past a derived column reads "not held outside the run", so a - // wrong index charges the state in full rather than undercharging. - #[test] - fn an_index_past_a_derived_column_is_not_held_outside_the_run() { - let stored = state_batch(4, false); - stored.mark_states_held_outside_run(); - let kept = stored - .filter(&BooleanArray::from(vec![true, false, false, false])) - .unwrap(); - let column = kept.state_column(0).unwrap(); - assert_eq!(column.len(), 1); - assert!(column.state_held_outside_run(0)); - assert!(!column.state_held_outside_run(1)); - } -} diff --git a/crates/executor/src/values/batch/mod.rs b/crates/executor/src/values/batch/mod.rs new file mode 100644 index 000000000..07b07ddec --- /dev/null +++ b/crates/executor/src/values/batch/mod.rs @@ -0,0 +1,609 @@ +//! A batch is a set of equal-length Arrow columns in one Planner schema, +//! with summary states held beside them. +use super::arrow_columns::{self, ColumnDepth}; +use super::{validate_schema, validate_state, SchemaRef, StateColumn, Value}; +use crate::{AggregateCore, Error}; +use arrow::array::{Array, ArrayRef, BooleanArray, UInt32Array}; +use arrow::datatypes::{DataType as ArrowType, Field as ArrowField, Schema as ArrowSchema}; +use arrow::record_batch::{RecordBatch, RecordBatchOptions}; +use planner_types::ir::schema::FieldDataType; +use std::sync::Arc; + +/// Each row is charged this many bytes on top of its column buffers, so a +/// batch of zero-width rows still consumes memory budget. +const ROW_SLOT_BYTES: usize = 8; + +/// The output of one operator: equal-length columns in one Planner schema. +/// +/// Plain columns are Arrow arrays in a [`RecordBatch`], in the layout +/// `arrow_columns` gives each Planner type. A summary column is a +/// `UInt32` array of state indices in the record batch, plus its +/// [`StateColumn`] beside it. Cloning a batch shares its buffers. +#[derive(Clone)] +pub struct Batch { + /// The Planner schema, which the columns follow field by field. + schema: SchemaRef, + /// One Arrow column per Planner field; a summary field holds state + /// indices into its entry of `state_columns`. + record_batch: RecordBatch, + /// Per Planner field, the states of a summary field; `None` for a plain + /// field. + state_columns: Arc<[Option>]>, +} + +impl std::fmt::Debug for Batch { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Batch") + .field("schema", &self.schema) + .field("num_rows", &self.num_rows()) + .finish() + } +} + +/// One column of a batch built with [`Batch::try_from_columns`]. +pub enum BatchColumn { + /// A plain column: an Arrow array in its Planner type's layout (see + /// `super::arrow_columns`: text is `DictionaryArray` over + /// `Utf8`, a timestamp `TimestampMillisecondArray`, …). + Plain(ArrayRef), + /// A summary column: row `i`'s state is `states.state(indices[i])`. + States { + /// Each row's state index; never NULL. + indices: UInt32Array, + /// The states, e.g. a sealed [`crate::StateColumnUpdater`]. + states: Arc, + }, +} + +/// The Arrow schema of a Planner schema's columns. +fn arrow_schema(schema: &SchemaRef) -> Arc { + Arc::new(ArrowSchema::new( + schema + .fields + .iter() + .map(|field| { + ArrowField::new( + &field.name, + match &field.dtype { + FieldDataType::Plain(dtype) => { + arrow_columns::arrow_type(dtype, ColumnDepth::BatchColumn) + } + _ => ArrowType::UInt32, + }, + true, + ) + }) + .collect::>(), + )) +} + +impl Batch { + /// The batch of `rows` in `schema`, encoded into columns (the rows are + /// not kept). For literal rows (`VALUES`, constants, tests); runtimes + /// build batches from columns with [`Self::try_from_columns`]. Bad input + /// when a row's width or a value's type differs from the schema, or a + /// summary state differs from its declared family or parameters. + pub fn try_new(schema: SchemaRef, rows: Vec>) -> Result { + validate_schema(&schema)?; + for row in &rows { + if row.len() != schema.fields.len() { + return Err(Error::Invalid( + "row width differs from Planner schema".into(), + )); + } + for (value, field) in row.iter().zip(&schema.fields) { + let matches = match (&field.dtype, value) { + (FieldDataType::Plain(dtype), value) => value.matches(dtype, field.nullable), + (expected, Value::Summary { family, state }) => { + expected == family && validate_state(family, state.as_ref()).is_ok() + } + _ => false, + }; + if !matches { + return Err(Error::Invalid(format!( + "value differs from type of {}", + field.name + ))); + } + } + } + let mut arrays = Vec::with_capacity(schema.fields.len()); + let mut state_columns = Vec::with_capacity(schema.fields.len()); + for (index, field) in schema.fields.iter().enumerate() { + let cells = rows.iter().map(|row| &row[index]).collect::>(); + match &field.dtype { + FieldDataType::Plain(dtype) => { + arrays.push(arrow_columns::encode( + dtype, + ColumnDepth::BatchColumn, + &cells, + )?); + state_columns.push(None); + } + family => { + // Rows sharing one state object keep sharing it. + let mut positions = std::collections::HashMap::new(); + let mut states: Vec> = Vec::new(); + let indices = cells + .iter() + .map(|cell| { + let Value::Summary { state, .. } = cell else { + unreachable!("validated above"); + }; + let identity = Arc::as_ptr(state) as *const () as usize; + *positions.entry(identity).or_insert_with(|| { + states.push(state.clone()); + (states.len() - 1) as u32 + }) + }) + .collect::(); + arrays.push(Arc::new(indices)); + state_columns.push(Some(Arc::new(StateColumn::from_objects( + family.clone(), + states, + )))); + } + } + } + Self::from_parts(schema, arrays, state_columns, rows.len()) + } + + /// The batch of `columns` in `schema`, one per field, without running + /// the DAG: how a runtime installs states it built sample by sample + /// (a sealed [`crate::StateColumnUpdater`]) as a stored tumbling window. Bad input + /// when a column's count, layout, length or nullability differs from + /// the schema, a dictionary holds a NULL value (a NULL is a NULL key), + /// a summary field's states are of another family, or a state index is + /// NULL or past its states. + /// + /// A stored tumbling window has one row per group: the caller must not repeat a + /// group's labels (two rows of one series would be merged as two + /// tumbling windows of it). This is not checked, as it would cost a pass grouping + /// every row. + pub fn try_from_columns(schema: SchemaRef, columns: Vec) -> Result { + validate_schema(&schema)?; + if columns.len() != schema.fields.len() { + return Err(Error::Invalid( + "column count differs from Planner schema".into(), + )); + } + let row_count = columns.first().map_or(0, |column| match column { + BatchColumn::Plain(array) => array.len(), + BatchColumn::States { indices, .. } => indices.len(), + }); + let mut arrays = Vec::with_capacity(columns.len()); + let mut state_columns = Vec::with_capacity(columns.len()); + for (column, field) in columns.into_iter().zip(&schema.fields) { + match (column, &field.dtype) { + (BatchColumn::Plain(array), FieldDataType::Plain(dtype)) => { + // A NULL is a NULL key, never a NULL dictionary value, + // so every dictionary read finds a text. + if array + .as_any() + .downcast_ref::>() + .is_some_and(|dictionary| dictionary.values().null_count() > 0) + { + return Err(Error::Invalid(format!( + "column {} has a NULL dictionary value; a NULL is a NULL key", + field.name + ))); + } + if array.data_type() + != &arrow_columns::arrow_type(dtype, ColumnDepth::BatchColumn) + { + return Err(Error::Invalid(format!( + "column {} is not in its type's Arrow layout", + field.name + ))); + } + arrays.push(array); + state_columns.push(None); + } + (BatchColumn::States { indices, states }, family) + if !matches!(family, FieldDataType::Plain(_)) => + { + if states.family() != family { + return Err(Error::Invalid(format!( + "states of {} are of another family", + field.name + ))); + } + if indices.null_count() > 0 + || indices.values().iter().any(|&i| i as usize >= states.len()) + { + return Err(Error::Invalid(format!( + "a state index of {} is NULL or past its states", + field.name + ))); + } + arrays.push(Arc::new(indices)); + state_columns.push(Some(states)); + } + _ => { + return Err(Error::Invalid(format!( + "column {} is a summary column exactly when its field is", + field.name + ))) + } + } + } + if arrays.iter().any(|array| array.len() != row_count) { + return Err(Error::Invalid("columns differ in length".into())); + } + Self::from_parts(schema, arrays, state_columns, row_count) + } + + /// The batch of `arrays` in `schema`, with `state_columns` for its + /// summary fields. The arrays must have the layout of their Planner + /// types ([`arrow_columns`]) and `row_count` rows; a summary field's + /// array holds indices into its state column. Internal callers only. + pub(crate) fn from_parts( + schema: SchemaRef, + arrays: Vec, + state_columns: Vec>>, + row_count: usize, + ) -> Result { + if arrays.len() != schema.fields.len() || state_columns.len() != schema.fields.len() { + return Err(Error::Invalid( + "column count differs from Planner schema".into(), + )); + } + for ((array, states), field) in arrays.iter().zip(&state_columns).zip(&schema.fields) { + let plain = matches!(field.dtype, FieldDataType::Plain(_)); + if plain == states.is_some() { + return Err(Error::Invalid(format!( + "column {} has states exactly when it is a summary column", + field.name + ))); + } + if !field.nullable + && field.dtype != FieldDataType::Plain(planner_types::ir::schema::DataType::Null) + && array.logical_null_count() > 0 + { + return Err(Error::Invalid(format!( + "value differs from type of {}", + field.name + ))); + } + } + let record_batch = RecordBatch::try_new_with_options( + arrow_schema(&schema), + arrays, + &RecordBatchOptions::new().with_row_count(Some(row_count)), + ) + .map_err(|error| Error::Invalid(format!("columnar batch: {error}")))?; + Ok(Self { + schema, + record_batch, + state_columns: state_columns.into(), + }) + } + + /// Rows `offset..offset + length`, sharing the column buffers. + pub(crate) fn slice(&self, offset: usize, length: usize) -> Self { + if offset == 0 && length == self.num_rows() { + return self.clone(); + } + Self { + schema: self.schema.clone(), + record_batch: self.record_batch.slice(offset, length), + state_columns: self.state_columns.clone(), + } + } + + /// The rows where `keep` is true (not false or null), in order. Shares + /// the state columns; copies only the kept cells of plain columns. + pub(crate) fn filter(&self, keep: &BooleanArray) -> Result { + if keep.true_count() == self.num_rows() && keep.null_count() == 0 { + return Ok(self.clone()); + } + let record_batch = arrow::compute::filter_record_batch(&self.record_batch, keep) + .map_err(|error| Error::Invalid(format!("columnar filter: {error}")))?; + Self { + schema: self.schema.clone(), + record_batch, + state_columns: self.state_columns.clone(), + } + .compacted() + } + + /// This batch with each state column that its rows use less than half + /// of reduced to the states they use (exact records kept as records), + /// so a selective filter does not hold every state of its input. + fn compacted(self) -> Result { + let mut arrays = self.record_batch.columns().to_vec(); + let mut states = self.state_columns.to_vec(); + let mut changed = false; + for (column, slot) in states.iter_mut().enumerate() { + let Some(state_column) = slot.clone() else { + continue; + }; + let indices = arrays[column] + .as_any() + .downcast_ref::() + .expect("summary columns hold state indices") + .clone(); + if 2 * indices.len() >= state_column.len() { + continue; + } + let mut moved = ahash::AHashMap::::new(); + let mut used = Vec::new(); + let remapped = indices + .values() + .iter() + .map(|&index| { + *moved.entry(index).or_insert_with(|| { + used.push(index as usize); + (used.len() - 1) as u32 + }) + }) + .collect::(); + if used.len() == state_column.len() { + continue; + } + let family = state_column.family().clone(); + *slot = Some(Arc::new(match state_column.storage() { + super::StateStorage::Exact(records) => StateColumn::from_exact( + family, + used.iter().map(|&index| records[index].clone()).collect(), + ), + // Shared by pointer: stored sketches are not charged again. + super::StateStorage::Objects(_) => StateColumn::from_objects_of( + family, + used.iter() + .map(|&index| { + ( + state_column.state(index), + state_column.state_held_outside_run(index), + ) + }) + .collect(), + ), + })); + arrays[column] = Arc::new(remapped); + changed = true; + } + if !changed { + return Ok(self); + } + let rows = self.num_rows(); + Self::from_parts(self.schema, arrays, states, rows) + } + + /// The rows `indices` (in that order, repeats allowed), sharing the + /// state columns. A NULL index gives a row of NULLs, which a summary + /// column cannot hold (bad input). + pub(crate) fn take(&self, indices: &UInt32Array) -> Result { + if indices.null_count() > 0 && self.state_columns.iter().any(Option::is_some) { + return Err(Error::Invalid("a summary column has no NULL row".into())); + } + let arrays = self + .record_batch + .columns() + .iter() + .map(|array| { + arrow::compute::take(array.as_ref(), indices, None) + .map_err(|error| Error::Invalid(format!("columnar take: {error}"))) + }) + .collect::, _>>()?; + Self::from_parts( + self.schema.clone(), + arrays, + self.state_columns.to_vec(), + indices.len(), + ) + } + + /// The columns `columns` of this batch (shared, not copied), as a batch + /// of `schema`. + pub(crate) fn project(&self, schema: SchemaRef, columns: &[usize]) -> Result { + Self::from_parts( + schema, + columns.iter().map(|&c| self.column(c).clone()).collect(), + columns + .iter() + .map(|&c| self.state_column(c).cloned()) + .collect(), + self.num_rows(), + ) + } + + /// This batch with column `index` replaced by `array` (a plain column + /// of its field's layout). + pub(crate) fn with_plain_column(&self, index: usize, array: ArrayRef) -> Result { + let mut arrays = self.record_batch.columns().to_vec(); + let mut states = self.state_columns.to_vec(); + arrays[index] = array; + states[index] = None; + Self::from_parts(self.schema.clone(), arrays, states, self.num_rows()) + } + + /// `batches`, all in `schema`, as one batch: plain columns copied, + /// state columns joined (shared when every batch shares one). + pub(crate) fn concat(schema: &SchemaRef, batches: &[Self]) -> Result { + match batches { + [] => return Self::try_new(schema.clone(), Vec::new()), + [batch] => return Ok(batch.clone()), + _ => {} + } + let rows = batches.iter().map(Self::num_rows).sum(); + let mut arrays = Vec::with_capacity(schema.fields.len()); + let mut states = Vec::with_capacity(schema.fields.len()); + for column in 0..schema.fields.len() { + let pieces = batches + .iter() + .map(|batch| batch.column(column).as_ref()) + .collect::>(); + let columns = batches + .iter() + .map(|batch| batch.state_column(column)) + .collect::>(); + let Some(Some(first)) = columns.first().copied() else { + arrays.push( + arrow::compute::concat(&pieces) + .map_err(|error| Error::Invalid(format!("columnar concat: {error}")))?, + ); + states.push(None); + continue; + }; + if columns + .iter() + .all(|other| other.is_some_and(|other| Arc::ptr_eq(other, first))) + { + arrays.push( + arrow::compute::concat(&pieces) + .map_err(|error| Error::Invalid(format!("columnar concat: {error}")))?, + ); + states.push(Some(first.clone())); + continue; + } + // Different state columns: one column of the states the rows + // reference (each once), exact records kept as records. + let all_exact = columns.iter().all(|column| { + matches!( + column.map(|column| column.storage()), + Some(super::StateStorage::Exact(_)) + ) + }); + let mut indices = Vec::with_capacity(rows); + let mut objects = Vec::new(); + let mut records = Vec::new(); + for (batch, state_column) in batches.iter().zip(&columns) { + let state_column = state_column.expect("one schema"); + let local = batch + .column(column) + .as_any() + .downcast_ref::() + .expect("summary columns hold state indices"); + let mut moved = ahash::AHashMap::::new(); + for &index in local.values().iter() { + let next = (objects.len() + records.len()) as u32; + let new_index = *moved.entry(index).or_insert_with(|| { + match state_column.storage() { + super::StateStorage::Exact(states) if all_exact => { + records.push(states[index as usize].clone()) + } + // An object is shared by pointer (not charged + // again when held outside the run); an exact + // record becomes a new object. + super::StateStorage::Objects(_) => objects.push(( + state_column.state(index as usize), + state_column.state_held_outside_run(index as usize), + )), + super::StateStorage::Exact(_) => { + objects.push((state_column.state(index as usize), false)) + } + } + next + }); + indices.push(new_index); + } + } + arrays.push(Arc::new(UInt32Array::from(indices)) as ArrayRef); + let family = first.family().clone(); + states.push(Some(Arc::new(if all_exact { + StateColumn::from_exact(family, records) + } else { + StateColumn::from_objects_of(family, objects) + }))); + } + Self::from_parts(schema.clone(), arrays, states, rows) + } + + /// The Planner schema of the columns. + pub fn schema(&self) -> &SchemaRef { + &self.schema + } + + /// The number of rows. + pub fn num_rows(&self) -> usize { + self.record_batch.num_rows() + } + + /// The Arrow columns, one per Planner field. A summary field's column + /// holds `UInt32` indices into [`Self::state_column`]. + pub fn record_batch(&self) -> &RecordBatch { + &self.record_batch + } + + /// The Arrow column of field `index`. + pub fn column(&self, index: usize) -> &ArrayRef { + self.record_batch.column(index) + } + + /// The states of summary field `index`; `None` for a plain field. + pub fn state_column(&self, index: usize) -> Option<&Arc> { + self.state_columns.get(index).and_then(Option::as_ref) + } + + /// The rows as values, decoded from the columns on every call: for + /// results leaving the executor and for tests. Operators read columns. + pub fn to_rows(&self) -> Vec> { + { + let length = self.num_rows(); + let width = self.schema.fields.len(); + let mut rows: Vec> = + (0..length).map(|_| Vec::with_capacity(width)).collect(); + for (index, field) in self.schema.fields.iter().enumerate() { + let array = self.record_batch.column(index); + let column = match (&field.dtype, self.state_column(index)) { + (FieldDataType::Plain(dtype), _) => { + arrow_columns::decode(array.as_ref(), dtype, ColumnDepth::BatchColumn) + } + (_, Some(states)) => { + let indices = array + .as_any() + .downcast_ref::() + .expect("summary columns hold state indices"); + indices + .values() + .iter() + .map(|&i| Value::Summary { + family: states.family().clone(), + state: states.state(i as usize), + }) + .collect() + } + (_, None) => unreachable!("summary columns have states"), + }; + for (row, value) in rows.iter_mut().zip(column) { + row.push(value); + } + } + rows + } + } + + /// Records that something outside any run (the caller of a plan, a + /// store of tumbling windows) holds this batch's states, so runs that read them + /// are not charged for them again. States a run copies or creates from + /// them are new state columns and are charged. + pub(crate) fn mark_states_held_outside_run(&self) { + for column in self.state_columns.iter().flatten() { + column.mark_held_outside_run(); + } + } + + /// Memory the batch holds, for execution memory reservations: its + /// column buffers, a fixed slot per row, and each state column once, + /// except states held outside the run (see + /// `Self::mark_states_held_outside_run`). Constant time in the number + /// of rows. + pub fn memory_bytes(&self) -> usize { + let mut seen = std::collections::BTreeSet::new(); + let states = self + .state_columns + .iter() + .flatten() + .filter(|column| seen.insert(Arc::as_ptr(column))) + .map(|column| column.charged_state_bytes()) + .sum::(); + std::mem::size_of::() + + self.record_batch.get_array_memory_size() + + self.num_rows() * ROW_SLOT_BYTES + + states + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/executor/src/values/batch/tests.rs b/crates/executor/src/values/batch/tests.rs new file mode 100644 index 000000000..3430a2eea --- /dev/null +++ b/crates/executor/src/values/batch/tests.rs @@ -0,0 +1,665 @@ +use super::*; +use crate::summary_kernels::DDSketchAccumulator; +use planner_types::ir::schema::{DataType, Field, Schema}; +use planner_types::summary_type::SketchAlgorithm; + +fn field(name: &str, dtype: DataType, nullable: bool) -> Field { + Field { + name: name.into(), + dtype: FieldDataType::Plain(dtype), + nullable, + } +} + +fn schema(fields: Vec) -> SchemaRef { + Arc::new(Schema { + fields, + time_index: None, + unique_keys: vec![], + closed: false, + }) +} + +/// Exact equality, including float bits (`Value`'s key merges zero signs). +fn same(a: &Value, b: &Value) -> bool { + match (a, b) { + (Value::Float64(a), Value::Float64(b)) => a.to_bits() == b.to_bits(), + (Value::List(a), Value::List(b)) | (Value::Struct(a), Value::Struct(b)) => { + a.len() == b.len() && a.iter().zip(b.iter()).all(|(a, b)| same(a, b)) + } + (Value::Map(a), Value::Map(b)) => { + a.len() == b.len() + && a.iter() + .zip(b.iter()) + .all(|((ak, av), (bk, bv))| same(ak, bk) && same(av, bv)) + } + (Value::Summary { state: a, .. }, Value::Summary { state: b, .. }) => Arc::ptr_eq(a, b), + _ => format!("{a:?}") == format!("{b:?}"), + } +} + +/// The rows as values. +fn decoded(batch: &Batch) -> Vec> { + batch.to_rows() +} + +// Every Planner type survives the trip into Arrow columns and back, +// nulls, NaN and negative zero included. +#[test] +fn rows_round_trip_through_arrow_columns() { + let element = Field::::new("item", DataType::Float64, true); + let inner = vec![ + Field:: { + name: "a".into(), + dtype: DataType::Int64, + nullable: false, + }, + Field:: { + name: "b".into(), + dtype: DataType::Utf8, + nullable: true, + }, + ]; + let labels = DataType::Map { + key: Box::new(DataType::Utf8), + value: Box::new(DataType::Utf8), + value_nullable: true, + }; + let schema = schema(vec![ + field("null", DataType::Null, true), + field("bool", DataType::Bool, true), + field("int", DataType::Int64, true), + field("float", DataType::Float64, false), + field("text", DataType::Utf8, true), + field("time", DataType::Timestamp, false), + field("date", DataType::Date, true), + field("interval", DataType::Interval, true), + field( + "list", + DataType::List { + element: Box::new(element), + }, + true, + ), + field("struct", DataType::Struct { fields: inner }, true), + field("map", labels, true), + ]); + let text = |s: &str| Value::Utf8(s.into()); + let rows = vec![ + vec![ + Value::Null, + Value::Bool(true), + Value::Int64(-3), + Value::Float64(-0.0), + text("api"), + Value::Timestamp(1_000), + Value::Date(7), + Value::Interval { + months: 1, + days: 2, + nanos: 3, + }, + Value::List(vec![Value::Float64(f64::NAN), Value::Float64(1.5)].into()), + Value::Struct(vec![Value::Int64(1), Value::Null].into()), + Value::Map(vec![(text("job"), text("api")), (text("x"), Value::Null)].into()), + ], + vec![ + Value::Null, + Value::Null, + Value::Null, + Value::Float64(f64::INFINITY), + Value::Null, + Value::Timestamp(-5), + Value::Null, + Value::Null, + Value::Null, + Value::Null, + Value::Null, + ], + vec![ + Value::Null, + Value::Bool(false), + Value::Int64(i64::MAX), + Value::Float64(2.0), + text("api"), + Value::Timestamp(0), + Value::Date(-1), + Value::Interval { + months: 0, + days: 0, + nanos: 0, + }, + Value::List(Arc::from([])), + Value::Struct(vec![Value::Int64(2), text("")].into()), + Value::Map(Arc::from([])), + ], + ]; + let batch = Batch::try_new(schema, rows.clone()).unwrap(); + assert_eq!(batch.num_rows(), 3); + let back = decoded(&batch); + for (expected, actual) in rows.iter().zip(&back) { + for (e, a) in expected.iter().zip(actual) { + assert!(same(e, a), "{e:?} became {a:?}"); + } + } +} + +// A batch text column is a dictionary with one entry per distinct value. +#[test] +fn text_columns_are_dictionary_encoded() { + use arrow::array::{AsArray, DictionaryArray}; + let schema = schema(vec![field("job", DataType::Utf8, false)]); + let rows = ["api", "web", "api", "api", "web"] + .iter() + .map(|job| vec![Value::Utf8((*job).into())]) + .collect(); + let batch = Batch::try_new(schema, rows).unwrap(); + let column: &DictionaryArray = batch.column(0).as_dictionary(); + assert_eq!(column.values().len(), 2); + assert_eq!(column.keys().values().to_vec(), vec![0, 1, 0, 0, 1]); +} + +fn dd_family() -> FieldDataType { + FieldDataType::Sketch(SketchAlgorithm::DDSketch) +} + +// Rows sharing a state keep sharing one state; filtering rows shares the +// state column instead of copying sketches. +#[test] +fn state_columns_hold_each_state_once_and_filter_shares_them() { + let schema = Arc::new(Schema { + fields: vec![ + field("job", DataType::Utf8, false), + Field { + name: "state".into(), + dtype: dd_family(), + nullable: false, + }, + ], + time_index: None, + unique_keys: vec![], + closed: false, + }); + let shared: Arc = Arc::new(DDSketchAccumulator::new(0.01)); + let other: Arc = Arc::new(DDSketchAccumulator::new(0.01)); + let state = |s: &Arc| Value::Summary { + family: dd_family(), + state: s.clone(), + }; + let rows = vec![ + vec![Value::Utf8("a".into()), state(&shared)], + vec![Value::Utf8("b".into()), state(&other)], + vec![Value::Utf8("c".into()), state(&shared)], + ]; + let batch = Batch::try_new(schema, rows).unwrap(); + let states = batch.state_column(1).unwrap(); + assert_eq!(states.len(), 2); + assert!(batch.state_column(0).is_none()); + let kept = batch + .filter(&BooleanArray::from(vec![true, false, true])) + .unwrap(); + assert!(Arc::ptr_eq(kept.state_column(1).unwrap(), states)); + let rows = decoded(&kept); + assert_eq!(rows.len(), 2); + assert!(same(&rows[0][1], &state(&shared)) && same(&rows[1][1], &state(&shared))); +} + +// Memory is counted per batch from its buffers; zero-width rows still +// cost their slot. +#[test] +fn zero_width_rows_consume_memory() { + let empty = schema(vec![]); + let none = Batch::try_new(empty.clone(), vec![]).unwrap(); + let many = Batch::try_new(empty, vec![vec![]; 200]).unwrap(); + assert_eq!(many.num_rows(), 200); + assert_eq!( + many.memory_bytes() - none.memory_bytes(), + 200 * ROW_SLOT_BYTES + ); +} + +// A runtime can share batches, stored tumbling windows included, across threads. +#[test] +fn batches_and_state_columns_are_send_and_sync() { + fn shareable() {} + shareable::(); + shareable::(); +} + +// A value of the wrong type or width is rejected, as before. +#[test] +fn rows_must_match_the_schema() { + let schema = schema(vec![field("v", DataType::Int64, false)]); + assert!(Batch::try_new(schema.clone(), vec![vec![Value::Null]]).is_err()); + assert!(Batch::try_new(schema.clone(), vec![vec![Value::Float64(1.)]]).is_err()); + assert!(Batch::try_new(schema, vec![vec![]]).is_err()); +} + +// A runtime turns sealed per-sample states and their group columns into +// a batch: the states and labels read back as the rows say. +#[test] +fn a_batch_from_columns_holds_sealed_states_and_labels() { + use crate::StateColumnUpdater; + use planner_types::summary_type::{ExactKind, ExactParams}; + let family = FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum); + let mut updater = StateColumnUpdater::new(&family, None).unwrap(); + updater.ensure_groups(2).unwrap(); + updater.update(0, 1.5, 0).unwrap(); + updater.update(1, 4.0, 0).unwrap(); + updater.update(0, 2.0, 0).unwrap(); + let states = Arc::new(updater.seal()); + let schema = Arc::new(Schema { + fields: vec![ + field("job", DataType::Utf8, false), + Field { + name: "state".into(), + dtype: family.clone(), + nullable: false, + }, + ], + time_index: None, + unique_keys: vec![], + closed: false, + }); + let jobs: ArrayRef = Arc::new( + ["api", "web"] + .into_iter() + .collect::>(), + ); + let batch = Batch::try_from_columns( + schema.clone(), + vec![ + BatchColumn::Plain(jobs.clone()), + BatchColumn::States { + indices: UInt32Array::from(vec![1, 0]), + states: states.clone(), + }, + ], + ) + .unwrap(); + let rows = batch.to_rows(); + let sum = |value: &Value| { + let Value::Summary { state, .. } = value else { + panic!() + }; + state + .as_any() + .downcast_ref::() + .unwrap() + .evaluation(planner_types::summary_type::ExactKind::Sum, None, None) + .unwrap() + .unwrap() + }; + assert_eq!( + format!("{:?}", rows[0][0]), + format!("{:?}", Value::Utf8("api".into())) + ); + assert_eq!((sum(&rows[0][1]), sum(&rows[1][1])), (4.0, 3.5)); + // Bad input: a column of another layout, a state index past the + // column, another family, or columns of different lengths. + let plain_text: ArrayRef = Arc::new(arrow::array::StringArray::from(vec!["api", "web"])); + let indices = |values: Vec| BatchColumn::States { + indices: UInt32Array::from(values), + states: states.clone(), + }; + let other_family = Arc::new( + StateColumnUpdater::new( + &FieldDataType::ExactAggregate(ExactKind::Count, ExactParams::Count), + None, + ) + .unwrap() + .seal(), + ); + for columns in [ + vec![BatchColumn::Plain(plain_text), indices(vec![0, 1])], + vec![BatchColumn::Plain(jobs.clone()), indices(vec![0, 2])], + vec![ + BatchColumn::Plain(jobs.clone()), + BatchColumn::States { + indices: UInt32Array::from(vec![0, 0]), + states: other_family, + }, + ], + vec![BatchColumn::Plain(jobs.clone()), indices(vec![0])], + vec![indices(vec![0, 1]), BatchColumn::Plain(jobs.clone())], + ] { + assert!(matches!( + Batch::try_from_columns(schema.clone(), columns), + Err(Error::Invalid(_)) + )); + } +} + +// A NULL among a dictionary's values is a NULL in the column: refused +// in a non-nullable field, not read back as "". +#[test] +fn a_null_dictionary_value_is_a_null() { + use arrow::array::{DictionaryArray, StringArray}; + let schema = Arc::new(Schema { + fields: vec![field("job", DataType::Utf8, false)], + time_index: None, + unique_keys: vec![], + closed: false, + }); + let jobs = DictionaryArray::::try_new( + UInt32Array::from(vec![0, 1]), + Arc::new(StringArray::from(vec![Some("a"), None])), + ) + .unwrap(); + assert!(matches!( + Batch::try_from_columns(schema, vec![BatchColumn::Plain(Arc::new(jobs.clone()))]), + Err(Error::Invalid(_)) + )); + // A nullable field refuses it too: a NULL is a NULL key, so every + // dictionary read finds a text. A NULL key there reads NULL, in rows + // and in the column's logical nulls. + let nullable = Arc::new(Schema { + fields: vec![field("job", DataType::Utf8, true)], + time_index: None, + unique_keys: vec![], + closed: false, + }); + assert!(matches!( + Batch::try_from_columns(nullable.clone(), vec![BatchColumn::Plain(Arc::new(jobs))]), + Err(Error::Invalid(_)) + )); + let null_key = DictionaryArray::::try_new( + UInt32Array::from(vec![Some(0), None]), + Arc::new(StringArray::from(vec!["a"])), + ) + .unwrap(); + let batch = + Batch::try_from_columns(nullable, vec![BatchColumn::Plain(Arc::new(null_key))]).unwrap(); + let rows = batch.to_rows(); + assert!(matches!(&rows[0][0], Value::Utf8(job) if job.as_ref() == "a")); + assert!(matches!(rows[1][0], Value::Null)); + let nulls = batch.column(0).logical_nulls().unwrap(); + assert!(nulls.is_valid(0) && nulls.is_null(1)); +} + +// A filter that keeps few rows keeps only their states: a tumbling window filtered +// to two of ten series holds two states, not ten. +#[test] +fn a_selective_filter_compacts_its_state_column() { + use crate::StateColumnUpdater; + use planner_types::summary_type::{ExactKind, ExactParams}; + let family = FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum); + let mut updater = StateColumnUpdater::new(&family, None).unwrap(); + updater.ensure_groups(10).unwrap(); + for group in 0..10 { + updater.update(group, f64::from(group), 0).unwrap(); + } + let schema = Arc::new(Schema { + fields: vec![ + field("k", DataType::Int64, false), + Field { + name: "state".into(), + dtype: family, + nullable: false, + }, + ], + time_index: None, + unique_keys: vec![], + closed: false, + }); + let batch = Batch::try_from_columns( + schema, + vec![ + BatchColumn::Plain(Arc::new(arrow::array::Int64Array::from_iter_values(0..10))), + BatchColumn::States { + indices: UInt32Array::from_iter_values(0..10), + states: Arc::new(updater.seal()), + }, + ], + ) + .unwrap(); + let keep = BooleanArray::from((0..10).map(|i| i == 3 || i == 7).collect::>()); + let kept = batch.filter(&keep).unwrap(); + assert_eq!(kept.state_column(1).unwrap().len(), 2); + assert!(kept.memory_bytes() < batch.memory_bytes()); + // The rows still read their own states. + let sums = decoded(&kept) + .iter() + .map(|row| { + let Value::Summary { state, .. } = &row[1] else { + panic!() + }; + state + .as_any() + .downcast_ref::() + .unwrap() + .evaluation(planner_types::summary_type::ExactKind::Sum, None, None) + .unwrap() + .unwrap() + }) + .collect::>(); + assert_eq!(sums, vec![3.0, 7.0]); +} + +// Two batches over different exact state columns join into one exact +// column of the states their rows reference, each once, still records. +#[test] +fn concat_of_different_exact_columns_keeps_records() { + use crate::StateColumnUpdater; + use planner_types::summary_type::{ExactKind, ExactParams}; + let family = FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum); + let schema = Arc::new(Schema { + fields: vec![Field { + name: "state".into(), + dtype: family.clone(), + nullable: false, + }], + time_index: None, + unique_keys: vec![], + closed: false, + }); + // States [10, 20, 30] referenced as rows [2, 0, 2]; states [1, 2] + // referenced as rows [1]. + let batch = |sums: &[f64], rows: Vec| { + let mut updater = StateColumnUpdater::new(&family, None).unwrap(); + updater.ensure_groups(sums.len()).unwrap(); + for (group, &sum) in sums.iter().enumerate() { + updater.update(group as u32, sum, 0).unwrap(); + } + Batch::try_from_columns( + schema.clone(), + vec![BatchColumn::States { + indices: UInt32Array::from(rows), + states: Arc::new(updater.seal()), + }], + ) + .unwrap() + }; + let first = batch(&[10., 20., 30.], vec![2, 0, 2]); + let second = batch(&[1., 2.], vec![1]); + assert!(matches!( + first.state_column(0).unwrap().storage(), + super::super::StateStorage::Exact(_) + )); + let joined = Batch::concat(&schema, &[first, second]).unwrap(); + let states = joined.state_column(0).unwrap(); + let super::super::StateStorage::Exact(records) = states.storage() else { + panic!("exact states stay records"); + }; + assert_eq!(records.len(), 3); + let indices = joined + .column(0) + .as_any() + .downcast_ref::() + .unwrap(); + assert_eq!(indices.values().to_vec(), vec![0, 1, 0, 2]); + let sums = decoded(&joined) + .iter() + .map(|row| { + let Value::Summary { state, .. } = &row[0] else { + panic!() + }; + state + .as_any() + .downcast_ref::() + .unwrap() + .evaluation(planner_types::summary_type::ExactKind::Sum, None, None) + .unwrap() + .unwrap() + }) + .collect::>(); + assert_eq!(sums, vec![30.0, 10.0, 30.0, 2.0]); +} + +/// A batch of `count` rows, one state each: KLL (k = 200) objects, or +/// exact sums. +fn state_batch(count: u32, exact: bool) -> Batch { + use crate::StateColumnUpdater; + use planner_types::summary_type::{ExactKind, ExactParams, SketchAlgorithm}; + let (family, parameters) = if exact { + ( + FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum), + None, + ) + } else { + ( + FieldDataType::Sketch(SketchAlgorithm::Kll), + Some(planner_types::summary_type::SketchParams::Kll { k: 200 }), + ) + }; + let mut updater = StateColumnUpdater::new(&family, parameters.as_ref()).unwrap(); + updater.ensure_groups(count as usize).unwrap(); + for group in 0..count { + updater.update(group, f64::from(group), 0).unwrap(); + } + let schema = Arc::new(Schema { + fields: vec![Field { + name: "state".into(), + dtype: family, + nullable: false, + }], + time_index: None, + unique_keys: vec![], + closed: false, + }); + Batch::try_from_columns( + schema, + vec![BatchColumn::States { + indices: UInt32Array::from_iter_values(0..count), + states: Arc::new(updater.seal()), + }], + ) + .unwrap() +} + +/// The bytes `batch` is charged for its state columns. +fn charged_states(batch: &Batch) -> usize { + batch + .state_columns + .iter() + .flatten() + .map(|column| column.charged_state_bytes()) + .sum() +} + +// Compacting or concatenating stored (held outside the run) object +// columns shares their sketches by pointer: the new column is charged +// its pointer slots, not the sketches. +#[test] +fn derived_columns_of_stored_sketches_charge_only_pointers() { + let stored = state_batch(100, false); + stored.mark_states_held_outside_run(); + // Each derived state: its pointer slot and its "held outside" bit. + let pointers = + |states: usize| states * (std::mem::size_of::>() + 1); + // Compaction: 10 of 100 rows kept. + let keep = BooleanArray::from((0..100).map(|i| i % 10 == 0).collect::>()); + let kept = stored.filter(&keep).unwrap(); + assert_eq!(kept.state_column(0).unwrap().len(), 10); + assert_eq!(charged_states(&kept), pointers(10)); + // Concat of two different stored columns. + let other = state_batch(50, false); + other.mark_states_held_outside_run(); + let joined = Batch::concat(stored.schema(), &[stored.clone(), other]).unwrap(); + assert_eq!(joined.state_column(0).unwrap().len(), 150); + assert_eq!(charged_states(&joined), pointers(150)); + // A run-built column concatenated with a stored one: only the + // run-built sketches are charged in full. + let built = state_batch(50, false); + let built_bytes = charged_states(&built); + let mixed = Batch::concat(&stored.schema().clone(), &[stored, built]).unwrap(); + assert_eq!(charged_states(&mixed), pointers(150) + built_bytes); +} + +// Exact records are copied, so compacting or concatenating stored exact +// columns is charged for the copied records. +#[test] +fn derived_columns_of_stored_exact_records_charge_the_copies() { + let stored = state_batch(100, true); + stored.mark_states_held_outside_run(); + assert_eq!(charged_states(&stored), 0); + let record = std::mem::size_of::(); + let keep = BooleanArray::from((0..100).map(|i| i % 10 == 0).collect::>()); + let kept = stored.filter(&keep).unwrap(); + assert_eq!(charged_states(&kept), 10 * record); + let other = state_batch(50, true); + other.mark_states_held_outside_run(); + let joined = Batch::concat(&stored.schema().clone(), &[stored, other]).unwrap(); + assert!(charged_states(&joined) >= 150 * record); +} + +// Deriving twice keeps each sketch's "held outside the run" bit: concat +// then compaction, or compaction then concat, of stored sketches is +// charged only pointers; run-built sketches stay charged in full. +#[test] +fn derived_twice_stored_sketches_still_charge_only_pointers() { + let slot = std::mem::size_of::>(); + let stored = |count| { + let batch = state_batch(count, false); + batch.mark_states_held_outside_run(); + batch + }; + let every_other = + |rows: usize| BooleanArray::from((0..rows).map(|i| i % 10 == 0).collect::>()); + // Concat, then compaction. + let (a, b) = (stored(100), stored(100)); + let schema = a.schema().clone(); + let joined = Batch::concat(&schema, &[a, b]).unwrap(); + let kept = joined.filter(&every_other(200)).unwrap(); + assert_eq!(kept.state_column(0).unwrap().len(), 20); + assert!(charged_states(&kept) <= 20 * (slot + 1)); + // Compaction, then concat. + let (a, b) = (stored(100), stored(100)); + let (a, b) = ( + a.filter(&every_other(100)).unwrap(), + b.filter(&every_other(100)).unwrap(), + ); + let joined = Batch::concat(&schema, &[a, b]).unwrap(); + assert_eq!(joined.state_column(0).unwrap().len(), 20); + assert!(charged_states(&joined) <= 20 * (slot + 1)); + // Mixed: 50 run-built and 100 stored sketches, concatenated, then + // compacted to every tenth row (5 run-built, 10 stored), then + // concatenated with another compacted stored batch (10 stored). + let built = state_batch(50, false); + let one_built = charged_states(&built) / 50; + let mixed = Batch::concat(&schema, &[built, stored(100)]).unwrap(); + let kept = mixed.filter(&every_other(150)).unwrap(); + assert_eq!(kept.state_column(0).unwrap().len(), 15); + let charged = charged_states(&kept); + assert!(charged >= 5 * one_built && charged <= 5 * one_built + 15 * (slot + 1)); + let other = stored(100).filter(&every_other(100)).unwrap(); + let joined = Batch::concat(&schema, &[kept, other]).unwrap(); + assert_eq!(joined.state_column(0).unwrap().len(), 25); + let charged = charged_states(&joined); + assert!(charged >= 5 * one_built && charged <= 5 * one_built + 25 * (slot + 1)); +} + +// An index past a derived column reads "not held outside the run", so a +// wrong index charges the state in full rather than undercharging. +#[test] +fn an_index_past_a_derived_column_is_not_held_outside_the_run() { + let stored = state_batch(4, false); + stored.mark_states_held_outside_run(); + let kept = stored + .filter(&BooleanArray::from(vec![true, false, false, false])) + .unwrap(); + let column = kept.state_column(0).unwrap(); + assert_eq!(column.len(), 1); + assert!(column.state_held_outside_run(0)); + assert!(!column.state_held_outside_run(1)); +} diff --git a/crates/executor/src/values/mod.rs b/crates/executor/src/values/mod.rs index 7678541e0..430ca0466 100644 --- a/crates/executor/src/values/mod.rs +++ b/crates/executor/src/values/mod.rs @@ -293,26 +293,4 @@ pub(crate) fn plain(schema: &SchemaRef, column: usize) -> Result<(&DataType, boo } #[cfg(test)] -mod weighted_state_tests { - use super::*; - use crate::summary_kernels::weighted_frequency::{FrequencyAlgorithm, WeightedFrequency}; - use planner_types::summary_type::SketchAlgorithm; - - // A state cannot acquire a different family merely by relabeling its - // batch; states of different shapes (θ) do not merge. - #[test] - fn weighted_state_family_must_match_and_shapes_must_merge() { - let cms = SummaryFamilyType::Sketch(SketchAlgorithm::CmsWithHeap); - let cs = SummaryFamilyType::Sketch(SketchAlgorithm::CountSketchWithHeap); - let state = WeightedFrequency::new(FrequencyAlgorithm::CountSketch, 32, 5, 8).unwrap(); - assert!(validate_state(&cs, &state).is_ok()); - assert!(validate_state(&cms, &state).is_err()); - // A Count Sketch without a heap is not a heap family's state. - let no_heap = WeightedFrequency::new(FrequencyAlgorithm::CountSketch, 32, 5, 0).unwrap(); - assert!(validate_state(&cs, &no_heap).is_err()); - let wrong_shape = - WeightedFrequency::new(FrequencyAlgorithm::CountSketch, 64, 5, 8).unwrap(); - assert!(validate_state(&cs, &wrong_shape).is_ok()); - assert!(state.merge(&wrong_shape).is_err()); - } -} +mod weighted_state_tests; diff --git a/crates/executor/src/values/weighted_state_tests.rs b/crates/executor/src/values/weighted_state_tests.rs new file mode 100644 index 000000000..180ac9629 --- /dev/null +++ b/crates/executor/src/values/weighted_state_tests.rs @@ -0,0 +1,20 @@ +use super::*; +use crate::summary_kernels::weighted_frequency::{FrequencyAlgorithm, WeightedFrequency}; +use planner_types::summary_type::SketchAlgorithm; + +// A state cannot acquire a different family merely by relabeling its +// batch; states of different shapes (θ) do not merge. +#[test] +fn weighted_state_family_must_match_and_shapes_must_merge() { + let cms = SummaryFamilyType::Sketch(SketchAlgorithm::CmsWithHeap); + let cs = SummaryFamilyType::Sketch(SketchAlgorithm::CountSketchWithHeap); + let state = WeightedFrequency::new(FrequencyAlgorithm::CountSketch, 32, 5, 8).unwrap(); + assert!(validate_state(&cs, &state).is_ok()); + assert!(validate_state(&cms, &state).is_err()); + // A Count Sketch without a heap is not a heap family's state. + let no_heap = WeightedFrequency::new(FrequencyAlgorithm::CountSketch, 32, 5, 0).unwrap(); + assert!(validate_state(&cs, &no_heap).is_err()); + let wrong_shape = WeightedFrequency::new(FrequencyAlgorithm::CountSketch, 64, 5, 8).unwrap(); + assert!(validate_state(&cs, &wrong_shape).is_ok()); + assert!(state.merge(&wrong_shape).is_err()); +} diff --git a/crates/planner/src/installable.rs b/crates/planner/src/installable/mod.rs similarity index 69% rename from crates/planner/src/installable.rs rename to crates/planner/src/installable/mod.rs index 974680d4e..3b59a927c 100644 --- a/crates/planner/src/installable.rs +++ b/crates/planner/src/installable/mod.rs @@ -588,262 +588,4 @@ fn report_of(cost: &PlanCost) -> SelectedPlanReport { } #[cfg(test)] -mod tests { - use super::*; - use asap_types::summary_type::{SketchAlgorithm, StatisticKind, SupportedSummary}; - use std::rc::Rc; - - thread_local! { - /// The plan nodes handed to the executor's compiler by the views' - /// refreshes in [`InstallablePlan::of_dag`] and - /// [`InstallablePlan::check_compiles`], summed over their compiles. - pub(super) static COMPILED_NODES: std::cell::Cell = const { std::cell::Cell::new(0) }; - } - - /// A p99 panel over a 300 s window every second, with no raw data - /// retained: one DDSketch per series in a chain of one-second windows - /// built at ingestion. Each refresh (here one refresh group) is compiled - /// over the nodes below its views, and the whole plan's checks run once, - /// so the nodes handed to the executor grow with the plan, not with the - /// plan times its views. The count covers both an installation and - /// `check_compiles`. - #[tokio::test] - async fn view_refreshes_compile_only_the_nodes_below_them() { - let workload: crate::PlanningWorkload = serde_json::from_str( - r#"{"query_workload": {"language": "promql", "repeating_queries": [{ - "query": "avg by (job) (quantile_over_time(0.99, data[300s]))", - "demand": {"fixed_interval_at": {"interval": 1000, "evaluation_phase": 0}}, - "requirements": {"accuracy": {"max_error": 0.05, "failure_probability": 0.3, - "accuracy_scope": "whole_result"}}, - "predictability": {"predictable": {"known_at": null}}}]}, - "data_workload": {"arrival": {"ingesting": {"rows_per_second": 10000.0}}, - "sources": {"data": {"series_count": 1000, "metric_type": "gauge", - "sample_interval": 100, "column_signs": {"value": "non_negative"}}}}}"#, - ) - .unwrap(); - let capabilities = DeploymentCapabilities { - raw_data_retained: false, - ..DeploymentCapabilities::default() - }; - COMPILED_NODES.with(|count| count.set(0)); - let (plan, _) = plan_installable(crate::UserInput::new( - &workload, - crate::FrontendInput::Promql, - crate::PlanningModels::builtin().with_deployment_capabilities(&capabilities), - )) - .await - .expect("a plan"); - let nodes = plan.dag.nodes.len(); - let views = plan.materialized_views().unwrap().len(); - // Measure one installation and one `check_compiles`, independently of - // how many candidates the optimizer explored before selecting this DAG. - COMPILED_NODES.with(|count| count.set(0)); - let rebuilt = InstallablePlan::of_dag(plan.dag.clone(), plan.query_texts.clone()).unwrap(); - assert_eq!(rebuilt.materialized_views().unwrap().len(), views); - plan.check_compiles().unwrap(); - let compiled = COMPILED_NODES.with(std::cell::Cell::get); - assert!( - compiled <= 6 * nodes, - "{compiled} nodes compiled for a plan of {nodes} nodes and {views} views" - ); - } - - /// The plan of the `sum_over_time(latency[1h])` panel as Stage 3 builds - /// it: a chain of 10-minute windows of exact sums per `job` over - /// `latency`, built at ingestion and retained for `chain_retention_ms`, - /// read through `TimeRange(1h)` by a merge and its finalization at query - /// time; and the chain's node id. - fn chain_plan(chain_retention_ms: Option) -> (PhysicalASAPDAG, PhysicalASAPNodeId) { - use asap_types::ir::operator::{Reduction, Source}; - use asap_types::ir::scalar::ColumnRef; - use asap_types::ir::schema::{DataType, Field, FieldDataType, Schema}; - use asap_types::ir::{ASAPOp, NonASAPOp, Operator, OperatorNode, QueryRoot, TimeRangeKind}; - use asap_types::plan::physical_asap_dag::ViewCoverage; - use asap_types::stages::{Lifetime, StorageMedium}; - use asap_types::summary_type::{ExactKind, ExactParams, SummaryUpdate}; - let node = |operator| OperatorNode::new_shared(operator).unwrap(); - let scan = node(Operator::NonASAP(NonASAPOp::Scan { - source: Source::TimeSeries { - metric: "latency".into(), - }, - predicates: vec![], - schema: Schema::with_time_index( - vec![ - Field::plain("job", DataType::Utf8, false), - Field::plain("ts", DataType::Timestamp, false), - Field::plain("value", DataType::Float64, false), - ], - 1, - vec![], - ), - })); - let chain = node(Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { - child: scan, - summary_type: FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum), - input: SummaryUpdate::column(ColumnRef::SampleValue), - reduction: Reduction::by(vec![0]), - filter: None, - tumbling_window_width_ms: 600_000, - })); - let windows = node(Operator::NonASAP(NonASAPOp::TimeRange { - range: std::time::Duration::from_secs(3_600), - kind: TimeRangeKind::Range, - child: chain.clone(), - })); - let merged = node(Operator::ASAP(ASAPOp::SummaryMerge { - children: vec![windows], - group_by: None, - })); - let root = node(Operator::ASAP(ASAPOp::FinalizeExactAccumulator { - child: merged, - })); - let (mut dag, nodes) = - asap_types::test_support::forwarders::physical_asap_dag_at_query_time_with_node_ids(&[ - QueryRoot::Operator(root), - ]) - .unwrap(); - let chain_id = nodes.iter().position(|n| Rc::ptr_eq(n, &chain)).unwrap(); - let scan_id = producer_of(&dag, chain_id); - for node in &mut dag.nodes { - if node.id == chain_id { - node.lifetime = Lifetime::AcrossEvaluations(StorageMedium::Memory); - node.retention_ms = chain_retention_ms; - node.view_coverage = - Some(ViewCoverage::new("sum of latency by job".into(), vec![])); - } - if node.id == chain_id || node.id == scan_id { - node.output_state.timing = ExecutionTiming::IngestionTime; - } - } - for edge in &mut dag.edges { - if edge.producer == chain_id || edge.producer == scan_id { - edge.data_state_mut().timing = ExecutionTiming::IngestionTime; - } - } - (dag, chain_id) - } - - /// One exact query's entry. - fn entry() -> QueryWorkloadEntry { - QueryWorkloadEntry { - query: Default::default(), - requirements: asap_types::workload::QueryRequirements { - accuracy: asap_types::accuracy::AccuracyTarget::exact(), - max_latency_ms: None, - }, - predictability: Default::default(), - recurrence: Default::default(), - range_evaluation: None, - } - } - - /// The input of `consumer`, which reads one. - fn producer_of(dag: &PhysicalASAPDAG, consumer: PhysicalASAPNodeId) -> PhysicalASAPNodeId { - dag.edges - .iter() - .find(|edge| edge.consumer == consumer) - .unwrap() - .producer - } - - /// The installable plan of a DAG with a chain is the DAG as Stage 3 - /// built it: the chain is one materialized view, refreshed at ingestion - /// one 10-minute window per run, and no raw data is kept for queries. - #[test] - fn a_chain_is_installed_as_one_view() { - let (dag, chain) = chain_plan(Some(3_600_000)); - let (plan, report) = installable_plan_of(&dag, None, &[entry()]).unwrap(); - assert_eq!(plan.dag, dag); - assert!(report.is_none()); - let views = plan.materialized_views().unwrap(); - assert_eq!(views.len(), 1); - assert_eq!(views[0].node_id, chain); - assert_eq!(views[0].retention_ms(), 3_600_000); - let groups = plan.ingestion_refresh_groups().unwrap(); - assert_eq!( - groups[0].refresh_window, - Some(asap_types::plan::materialized_view::RefreshWindow { - width_ms: 600_000, - phase_ms: 0 - }) - ); - assert!(plan.raw_data_retention().unwrap().is_empty()); - plan.check_compiles().unwrap(); - } - - /// Stage 3 sets each view's retention; installation checks it: a view - /// without one, or retained for less than the hour its query reads back, - /// is refused. - #[test] - fn installation_checks_the_retention_stage3_set() { - let entries = [entry()]; - assert!(matches!( - installable_plan_of(&chain_plan(None).0, None, &entries), - Err(InstallablePlanError::Uninstallable( - InstallableDagError::ViewWithoutRetention { .. } - )) - )); - assert!(matches!( - installable_plan_of(&chain_plan(Some(3_000_000)).0, None, &entries), - Err(InstallablePlanError::Uninstallable( - InstallableDagError::ViewRetentionTooShort { .. } - )) - )); - } - - /// A query-time `Scan` read through a window carries its retention; - /// one without is refused, and one with it is the raw data kept for its - /// source. - #[test] - fn a_query_time_scan_carries_its_retention() { - let (mut dag, chain) = chain_plan(Some(3_600_000)); - let scan = producer_of(&dag, chain); - for node in &mut dag.nodes { - if node.id == chain || node.id == scan { - node.output_state.timing = ExecutionTiming::QueryTime; - } - } - for edge in &mut dag.edges { - if edge.producer == chain || edge.producer == scan { - edge.data_state_mut().timing = ExecutionTiming::QueryTime; - } - } - assert!(matches!( - dag.raw_data_retention(), - Err(InstallableDagError::ScanWithoutRetention { .. }) - )); - dag.nodes - .iter_mut() - .find(|n| n.id == scan) - .unwrap() - .retention_ms = Some(600_000); - let retention = dag.raw_data_retention().unwrap(); - assert_eq!(retention.len(), 1); - assert_eq!(retention[0].lookback_ms(), Some(600_000)); - } - - /// Unrestricted summaries become the executor's families; a caller's - /// own list and its retention flags are kept. - #[test] - fn unrestricted_summaries_default_to_the_executors_families() { - let unrestricted = DeploymentCapabilities { - raw_data_retained: false, - ..DeploymentCapabilities::default() - }; - let defaulted = executor_summaries_when_unrestricted(&unrestricted); - assert_eq!( - defaulted.supported_summaries, - Some(asap_executor::supported_summary_families()) - ); - assert!(!defaulted.raw_data_retained); - assert!(!defaulted.supports(&SketchAlgorithm::CountSketch, StatisticKind::ItemFrequency)); - let kll_only = DeploymentCapabilities { - supported_summaries: Some(vec![SupportedSummary { - summary_type: SketchAlgorithm::Kll, - readouts: vec![StatisticKind::Quantile], - }]), - ..DeploymentCapabilities::default() - }; - assert_eq!(executor_summaries_when_unrestricted(&kll_only), kll_only); - } -} +mod tests; diff --git a/crates/planner/src/installable/tests.rs b/crates/planner/src/installable/tests.rs new file mode 100644 index 000000000..84b74a4b5 --- /dev/null +++ b/crates/planner/src/installable/tests.rs @@ -0,0 +1,256 @@ +use super::*; +use asap_types::summary_type::{SketchAlgorithm, StatisticKind, SupportedSummary}; +use std::rc::Rc; + +thread_local! { + /// The plan nodes handed to the executor's compiler by the views' + /// refreshes in [`InstallablePlan::of_dag`] and + /// [`InstallablePlan::check_compiles`], summed over their compiles. + pub(super) static COMPILED_NODES: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// A p99 panel over a 300 s window every second, with no raw data +/// retained: one DDSketch per series in a chain of one-second windows +/// built at ingestion. Each refresh (here one refresh group) is compiled +/// over the nodes below its views, and the whole plan's checks run once, +/// so the nodes handed to the executor grow with the plan, not with the +/// plan times its views. The count covers both an installation and +/// `check_compiles`. +#[tokio::test] +async fn view_refreshes_compile_only_the_nodes_below_them() { + let workload: crate::PlanningWorkload = serde_json::from_str( + r#"{"query_workload": {"language": "promql", "repeating_queries": [{ + "query": "avg by (job) (quantile_over_time(0.99, data[300s]))", + "demand": {"fixed_interval_at": {"interval": 1000, "evaluation_phase": 0}}, + "requirements": {"accuracy": {"max_error": 0.05, "failure_probability": 0.3, + "accuracy_scope": "whole_result"}}, + "predictability": {"predictable": {"known_at": null}}}]}, + "data_workload": {"arrival": {"ingesting": {"rows_per_second": 10000.0}}, + "sources": {"data": {"series_count": 1000, "metric_type": "gauge", + "sample_interval": 100, "column_signs": {"value": "non_negative"}}}}}"#, + ) + .unwrap(); + let capabilities = DeploymentCapabilities { + raw_data_retained: false, + ..DeploymentCapabilities::default() + }; + COMPILED_NODES.with(|count| count.set(0)); + let (plan, _) = plan_installable(crate::UserInput::new( + &workload, + crate::FrontendInput::Promql, + crate::PlanningModels::builtin().with_deployment_capabilities(&capabilities), + )) + .await + .expect("a plan"); + let nodes = plan.dag.nodes.len(); + let views = plan.materialized_views().unwrap().len(); + // Measure one installation and one `check_compiles`, independently of + // how many candidates the optimizer explored before selecting this DAG. + COMPILED_NODES.with(|count| count.set(0)); + let rebuilt = InstallablePlan::of_dag(plan.dag.clone(), plan.query_texts.clone()).unwrap(); + assert_eq!(rebuilt.materialized_views().unwrap().len(), views); + plan.check_compiles().unwrap(); + let compiled = COMPILED_NODES.with(std::cell::Cell::get); + assert!( + compiled <= 6 * nodes, + "{compiled} nodes compiled for a plan of {nodes} nodes and {views} views" + ); +} + +/// The plan of the `sum_over_time(latency[1h])` panel as Stage 3 builds +/// it: a chain of 10-minute windows of exact sums per `job` over +/// `latency`, built at ingestion and retained for `chain_retention_ms`, +/// read through `TimeRange(1h)` by a merge and its finalization at query +/// time; and the chain's node id. +fn chain_plan(chain_retention_ms: Option) -> (PhysicalASAPDAG, PhysicalASAPNodeId) { + use asap_types::ir::operator::{Reduction, Source}; + use asap_types::ir::scalar::ColumnRef; + use asap_types::ir::schema::{DataType, Field, FieldDataType, Schema}; + use asap_types::ir::{ASAPOp, NonASAPOp, Operator, OperatorNode, QueryRoot, TimeRangeKind}; + use asap_types::plan::physical_asap_dag::ViewCoverage; + use asap_types::stages::{Lifetime, StorageMedium}; + use asap_types::summary_type::{ExactKind, ExactParams, SummaryUpdate}; + let node = |operator| OperatorNode::new_shared(operator).unwrap(); + let scan = node(Operator::NonASAP(NonASAPOp::Scan { + source: Source::TimeSeries { + metric: "latency".into(), + }, + predicates: vec![], + schema: Schema::with_time_index( + vec![ + Field::plain("job", DataType::Utf8, false), + Field::plain("ts", DataType::Timestamp, false), + Field::plain("value", DataType::Float64, false), + ], + 1, + vec![], + ), + })); + let chain = node(Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { + child: scan, + summary_type: FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::by(vec![0]), + filter: None, + tumbling_window_width_ms: 600_000, + })); + let windows = node(Operator::NonASAP(NonASAPOp::TimeRange { + range: std::time::Duration::from_secs(3_600), + kind: TimeRangeKind::Range, + child: chain.clone(), + })); + let merged = node(Operator::ASAP(ASAPOp::SummaryMerge { + children: vec![windows], + group_by: None, + })); + let root = node(Operator::ASAP(ASAPOp::FinalizeExactAccumulator { + child: merged, + })); + let (mut dag, nodes) = + asap_types::test_support::forwarders::physical_asap_dag_at_query_time_with_node_ids(&[ + QueryRoot::Operator(root), + ]) + .unwrap(); + let chain_id = nodes.iter().position(|n| Rc::ptr_eq(n, &chain)).unwrap(); + let scan_id = producer_of(&dag, chain_id); + for node in &mut dag.nodes { + if node.id == chain_id { + node.lifetime = Lifetime::AcrossEvaluations(StorageMedium::Memory); + node.retention_ms = chain_retention_ms; + node.view_coverage = Some(ViewCoverage::new("sum of latency by job".into(), vec![])); + } + if node.id == chain_id || node.id == scan_id { + node.output_state.timing = ExecutionTiming::IngestionTime; + } + } + for edge in &mut dag.edges { + if edge.producer == chain_id || edge.producer == scan_id { + edge.data_state_mut().timing = ExecutionTiming::IngestionTime; + } + } + (dag, chain_id) +} + +/// One exact query's entry. +fn entry() -> QueryWorkloadEntry { + QueryWorkloadEntry { + query: Default::default(), + requirements: asap_types::workload::QueryRequirements { + accuracy: asap_types::accuracy::AccuracyTarget::exact(), + max_latency_ms: None, + }, + predictability: Default::default(), + recurrence: Default::default(), + range_evaluation: None, + } +} + +/// The input of `consumer`, which reads one. +fn producer_of(dag: &PhysicalASAPDAG, consumer: PhysicalASAPNodeId) -> PhysicalASAPNodeId { + dag.edges + .iter() + .find(|edge| edge.consumer == consumer) + .unwrap() + .producer +} + +/// The installable plan of a DAG with a chain is the DAG as Stage 3 +/// built it: the chain is one materialized view, refreshed at ingestion +/// one 10-minute window per run, and no raw data is kept for queries. +#[test] +fn a_chain_is_installed_as_one_view() { + let (dag, chain) = chain_plan(Some(3_600_000)); + let (plan, report) = installable_plan_of(&dag, None, &[entry()]).unwrap(); + assert_eq!(plan.dag, dag); + assert!(report.is_none()); + let views = plan.materialized_views().unwrap(); + assert_eq!(views.len(), 1); + assert_eq!(views[0].node_id, chain); + assert_eq!(views[0].retention_ms(), 3_600_000); + let groups = plan.ingestion_refresh_groups().unwrap(); + assert_eq!( + groups[0].refresh_window, + Some(asap_types::plan::materialized_view::RefreshWindow { + width_ms: 600_000, + phase_ms: 0 + }) + ); + assert!(plan.raw_data_retention().unwrap().is_empty()); + plan.check_compiles().unwrap(); +} + +/// Stage 3 sets each view's retention; installation checks it: a view +/// without one, or retained for less than the hour its query reads back, +/// is refused. +#[test] +fn installation_checks_the_retention_stage3_set() { + let entries = [entry()]; + assert!(matches!( + installable_plan_of(&chain_plan(None).0, None, &entries), + Err(InstallablePlanError::Uninstallable( + InstallableDagError::ViewWithoutRetention { .. } + )) + )); + assert!(matches!( + installable_plan_of(&chain_plan(Some(3_000_000)).0, None, &entries), + Err(InstallablePlanError::Uninstallable( + InstallableDagError::ViewRetentionTooShort { .. } + )) + )); +} + +/// A query-time `Scan` read through a window carries its retention; +/// one without is refused, and one with it is the raw data kept for its +/// source. +#[test] +fn a_query_time_scan_carries_its_retention() { + let (mut dag, chain) = chain_plan(Some(3_600_000)); + let scan = producer_of(&dag, chain); + for node in &mut dag.nodes { + if node.id == chain || node.id == scan { + node.output_state.timing = ExecutionTiming::QueryTime; + } + } + for edge in &mut dag.edges { + if edge.producer == chain || edge.producer == scan { + edge.data_state_mut().timing = ExecutionTiming::QueryTime; + } + } + assert!(matches!( + dag.raw_data_retention(), + Err(InstallableDagError::ScanWithoutRetention { .. }) + )); + dag.nodes + .iter_mut() + .find(|n| n.id == scan) + .unwrap() + .retention_ms = Some(600_000); + let retention = dag.raw_data_retention().unwrap(); + assert_eq!(retention.len(), 1); + assert_eq!(retention[0].lookback_ms(), Some(600_000)); +} + +/// Unrestricted summaries become the executor's families; a caller's +/// own list and its retention flags are kept. +#[test] +fn unrestricted_summaries_default_to_the_executors_families() { + let unrestricted = DeploymentCapabilities { + raw_data_retained: false, + ..DeploymentCapabilities::default() + }; + let defaulted = executor_summaries_when_unrestricted(&unrestricted); + assert_eq!( + defaulted.supported_summaries, + Some(asap_executor::supported_summary_families()) + ); + assert!(!defaulted.raw_data_retained); + assert!(!defaulted.supports(&SketchAlgorithm::CountSketch, StatisticKind::ItemFrequency)); + let kll_only = DeploymentCapabilities { + supported_summaries: Some(vec![SupportedSummary { + summary_type: SketchAlgorithm::Kll, + readouts: vec![StatisticKind::Quantile], + }]), + ..DeploymentCapabilities::default() + }; + assert_eq!(executor_summaries_when_unrestricted(&kll_only), kll_only); +} diff --git a/crates/types/src/ir/properties/execution_data_state.rs b/crates/types/src/ir/properties/execution_data_state/mod.rs similarity index 69% rename from crates/types/src/ir/properties/execution_data_state.rs rename to crates/types/src/ir/properties/execution_data_state/mod.rs index 4e7585e87..a9334f5c7 100644 --- a/crates/types/src/ir/properties/execution_data_state.rs +++ b/crates/types/src/ir/properties/execution_data_state/mod.rs @@ -94,39 +94,4 @@ impl std::fmt::Display for ExecutionDataState { } #[cfg(test)] -mod tests { - use super::*; - - /// Both execution phases use raw values, distinct from maintained state. - #[test] - fn raw_primitive_labels() { - assert_eq!( - ExecutionDataState::INGESTION_ROWS.primitive, - DataPrimitive::Raw - ); - assert_eq!(ExecutionDataState::QUERY_ROWS.primitive, DataPrimitive::Raw); - assert_eq!( - ExecutionDataState::INGESTION_ROWS.to_string(), - "ingestion_time/raw" - ); - assert_eq!(ExecutionDataState::QUERY_ROWS.to_string(), "query_time/raw"); - assert_eq!(DataPrimitive::SummaryState.as_str(), "summary_state"); - } - - #[test] - fn execution_phase_wire_names_are_ingestion_and_query_time() { - for (phase, name) in [ - (ExecutionTiming::IngestionTime, "ingestion_time"), - (ExecutionTiming::QueryTime, "query_time"), - ] { - assert_eq!(phase.as_str(), name); - assert_eq!(serde_json::to_value(phase).unwrap(), name); - assert_eq!( - serde_json::from_value::(serde_json::json!(name)).unwrap(), - phase - ); - } - assert!(serde_json::from_str::("\"maintenance_time\"").is_err()); - assert!(serde_json::from_str::("\"MaintenanceTime\"").is_err()); - } -} +mod tests; diff --git a/crates/types/src/ir/properties/execution_data_state/tests.rs b/crates/types/src/ir/properties/execution_data_state/tests.rs new file mode 100644 index 000000000..550dc6217 --- /dev/null +++ b/crates/types/src/ir/properties/execution_data_state/tests.rs @@ -0,0 +1,34 @@ +use super::*; + +/// Both execution phases use raw values, distinct from maintained state. +#[test] +fn raw_primitive_labels() { + assert_eq!( + ExecutionDataState::INGESTION_ROWS.primitive, + DataPrimitive::Raw + ); + assert_eq!(ExecutionDataState::QUERY_ROWS.primitive, DataPrimitive::Raw); + assert_eq!( + ExecutionDataState::INGESTION_ROWS.to_string(), + "ingestion_time/raw" + ); + assert_eq!(ExecutionDataState::QUERY_ROWS.to_string(), "query_time/raw"); + assert_eq!(DataPrimitive::SummaryState.as_str(), "summary_state"); +} + +#[test] +fn execution_phase_wire_names_are_ingestion_and_query_time() { + for (phase, name) in [ + (ExecutionTiming::IngestionTime, "ingestion_time"), + (ExecutionTiming::QueryTime, "query_time"), + ] { + assert_eq!(phase.as_str(), name); + assert_eq!(serde_json::to_value(phase).unwrap(), name); + assert_eq!( + serde_json::from_value::(serde_json::json!(name)).unwrap(), + phase + ); + } + assert!(serde_json::from_str::("\"maintenance_time\"").is_err()); + assert!(serde_json::from_str::("\"MaintenanceTime\"").is_err()); +} diff --git a/docs/develop_docs/asap-aware-mapping-architecture.md b/docs/develop_docs/asap-aware-mapping-architecture.md index 5d974ced4..95e2f4777 100644 --- a/docs/develop_docs/asap-aware-mapping-architecture.md +++ b/docs/develop_docs/asap-aware-mapping-architecture.md @@ -77,7 +77,7 @@ same pipeline behind the `OptimizationPass` trait. | Enumeration of the plans a memo stands for (reference pricer, DAG viewer) | none today (the standalone enumeration was deleted) | | What each summary type can merge and answer (`SummaryTypeCapability::of`: `merge_inputs`, `readouts`) | `asap-types`, `summary_type/capability.rs` | | Stage 2: the timings and lifetimes offered per logical candidate and their prune rules, the per-group offers (ingestion time, across evaluations), the physical edges, and the alternatives no plan can use | `asap-stage2-physical-optimization`: `candidate.rs`, `group.rs`, `edge.rs`, `pruning.rs`; the admissible-edge indexes in `asap-types`, `stages/physical/memo_candidates/mod.rs` | -| Execution timing and data-state edge rules; each physical candidate's `PhysicalProperties` and `Lifetime`, and the physical edge check (`producer_satisfies_physical_requirements`) | `asap-types`, `ir/properties/execution_data_state.rs`; `stages/physical/properties/mod.rs` (`PhysicalProperties`, `Lifetime`) and `stages/physical/candidate.rs` (`PhysicalCandidate`, the edge check) | +| Execution timing and data-state edge rules; each physical candidate's `PhysicalProperties` and `Lifetime`, and the physical edge check (`producer_satisfies_physical_requirements`) | `asap-types`, `ir/properties/execution_data_state/mod.rs`; `stages/physical/properties/mod.rs` (`PhysicalProperties`, `Lifetime`) and `stages/physical/candidate.rs` (`PhysicalCandidate`, the edge check) | | Stage 3: accuracy check, latency check, resource usage (`ResourceUsage`), cost models 1–3 (`CostModel`), measured operation costs | `asap-stage3-plan-selection`, `lib.rs`; `asap-cost`, `model.rs` (`CostModel`, `CostInputs`), `resources/mod.rs` (`ResourceUsage`) and `measured.rs` | | The analytical per-operator resource model Stage 3 measures with | `asap-cost`, `node/analytical.rs` and `node/operator_statistics.rs` | | The accuracy rules: per-family guarantees, each operator's error contract, the metric rule, each readout's required accuracy, α^log of a logical edge | `asap-accuracy` (`AccuracyEvidence`, `theoretical_guarantee`, `guarantee/`, `evidence.rs`, `error_contract/`, `metric_rule/`, `readout_caps/`, `logical_edge/`) | From 2c6abd119be70e88f838e7b23d7b0f3722357c10 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:34:22 +0000 Subject: [PATCH 04/31] fix(stage3,cost): the selection objective equals plan_cost: an ingestion-time scan runs once per second, raw data on disk once per source, request overhead included (#1207 PR 6) - asap-cost `computations`: a node computed at ingestion time runs once per second whatever views of whatever refresh windows read it (each arriving row is scanned once), as Stage 3 costs it. It was counted once per refresh window: a scan under a 60 s and a 420 s chain cost twice. - Stage 3 charges each source's raw data on disk once, for the selected query-time scan that keeps the most (one variable r_s per source, r_s >= d_v x_v), as plan_cost does; it added it to every scan's w_v. - The MILP's optimum includes the per-request overhead every plan pays (`asap_cost::request_overhead_cost_per_second`, shared with plan_cost) and is reported as `Selection::selected_plan_cost_per_second`. - Test: integration `stage3_objective` checks objective == plan_cost on two chain widths without raw data, and with request overhead and disk costed on two filtered scans of one source (both failed before). --- crates/cost/src/lib.rs | 5 +- crates/cost/src/plan/cost.rs | 71 +++++++++-- crates/cost/src/plan/execution_demand.rs | 28 ++--- crates/integration-tests/tests/main.rs | 1 + .../tests/stage3_objective.rs | 116 ++++++++++++++++++ crates/stage3-plan-selection/src/cost.rs | 53 +++++--- crates/stage3-plan-selection/src/lib.rs | 10 +- .../stage3-plan-selection/src/milp/model.rs | 91 +++++++++++++- .../src/milp/model/tests.rs | 2 + crates/stage3-plan-selection/src/report.rs | 7 +- 10 files changed, 327 insertions(+), 57 deletions(-) create mode 100644 crates/integration-tests/tests/stage3_objective.rs diff --git a/crates/cost/src/lib.rs b/crates/cost/src/lib.rs index a2c1d3bcf..c1339910a 100644 --- a/crates/cost/src/lib.rs +++ b/crates/cost/src/lib.rs @@ -40,7 +40,10 @@ pub use node::usage::{ node_resource_usage_and_cost, summary_type_name, NodeCost, NodeResourceUsageAndCost, ProducerOutput, }; -pub use plan::cost::{plan_cost, PlanCost, QueryLatency}; +pub use plan::cost::{ + plan_cost, raw_data_disk_cost_per_second, request_overhead_cost_per_second, PlanCost, + QueryLatency, +}; pub use plan::ranking::{cheaper, worst_latency_ms, PlanRanking}; pub use plan::read_back::read_back_ms; pub use resources::ResourceUsage; diff --git a/crates/cost/src/plan/cost.rs b/crates/cost/src/plan/cost.rs index dbe4e3829..155af79b4 100644 --- a/crates/cost/src/plan/cost.rs +++ b/crates/cost/src/plan/cost.rs @@ -180,12 +180,8 @@ pub fn plan_cost( continue; } let diagnostic_node = start.first().copied().unwrap_or(first); - let requests_per_second = evaluation_rate( - recurrence.get(query).map(|r| (r, None)), - settings.non_repeating_query_horizon_seconds, - ) - .map_err(|error| (diagnostic_node, error))?; - let ops_per_second = evaluation_ms / speed.ms_per_cpu_op * requests_per_second; + let (requests_per_second, ops_per_second) = request_overhead(entries.get(query), settings) + .map_err(|error| (diagnostic_node, error))?; usage.cpu_ops_per_second += ops_per_second; request_ms_per_second += evaluation_ms * requests_per_second; if !usage.cpu_ops_per_second.is_finite() || !request_ms_per_second.is_finite() { @@ -225,8 +221,11 @@ pub fn plan_cost( let node = per_node.get_mut(&id).expect("every node is measured"); usage.disk_bytes = usage.disk_bytes.saturating_add(bytes); node.detail += &format!("; keeps {bytes} bytes of raw data on disk"); - if let (Some(cost), Some(prices)) = (&mut node.cost_per_second, usage_prices) { - *cost += prices.cost_per_disk_byte_second * bytes as f64; + if let (Some(cost), Some(disk_cost)) = ( + &mut node.cost_per_second, + raw_data_disk_cost_per_second(bytes, settings), + ) { + *cost += disk_cost; } } // With k ingest workers, each of the k − 1 beyond the first builds its @@ -321,6 +320,62 @@ pub fn plan_cost( )) } +/// The runtime's fixed overhead of query `entry`'s requests: its requests +/// per second (once per evaluation of its recurrence, a range query's +/// steps in one request) and the CPU operations per second they cost, +/// `measured_operation_costs.ns_per_evaluation` each. `(0, 0)` without an +/// entry. +fn request_overhead( + entry: Option<&QueryWorkloadEntry>, + settings: &CostInputs, +) -> Result<(f64, f64), AnalyticalCostError> { + let requests_per_second = evaluation_rate( + entry.map(|entry| (&entry.recurrence, None)), + settings.non_repeating_query_horizon_seconds, + )?; + let evaluation_ms = settings.measured_operation_costs.ns_per_evaluation * 1e-6; + Ok(( + requests_per_second, + evaluation_ms / settings.resource_speed.ms_per_cpu_op * requests_per_second, + )) +} + +/// Cost model 1's cost per second of the runtime's fixed overhead per +/// query request, over every query of `entries`: what [`plan_cost`] +/// charges every plan for them, whichever nodes it has. 0 under cost +/// models 2 and 3 and when `ns_per_evaluation` is 0. +pub fn request_overhead_cost_per_second( + entries: &[QueryWorkloadEntry], + settings: &CostInputs, +) -> Result { + let CostModel::UsageOverTime(unit_cost) = &settings.cost_model else { + return Ok(0.0); + }; + if settings.measured_operation_costs.ns_per_evaluation <= 0.0 { + return Ok(0.0); + } + let mut cost = 0.0; + for entry in entries { + cost += unit_cost.cost_per_cpu_op * request_overhead(Some(entry), settings)?.1; + } + match cost.is_finite() { + true => Ok(cost), + false => Err(AnalyticalCostError::Overflow), + } +} + +/// Cost model 1's cost per second of keeping `bytes` of raw data on disk; +/// `None` under cost models 2 and 3. A plan keeps each source once, for its +/// scan that keeps the most ([`plan_cost`]). +pub fn raw_data_disk_cost_per_second(bytes: u64, settings: &CostInputs) -> Option { + match &settings.cost_model { + CostModel::UsageOverTime(unit_cost) => { + Some(unit_cost.cost_per_disk_byte_second * bytes as f64) + } + _ => None, + } +} + /// Every node of a physical DAG priced ([`each_node_resource_usage_and_cost`]), with each /// node's `(multiplier, per_second)` ([`computations`]). struct EachNodeResourceUsageAndCost<'a> { diff --git a/crates/cost/src/plan/execution_demand.rs b/crates/cost/src/plan/execution_demand.rs index 241b38eb7..02a00ab02 100644 --- a/crates/cost/src/plan/execution_demand.rs +++ b/crates/cost/src/plan/execution_demand.rs @@ -315,11 +315,10 @@ pub fn consumers(dag: &PhysicalASAPDAG) -> HashMap = HashMap::new(); for (id, of_node) in consumers(dag) { let entry = count.entry(id).or_default(); - // The refresh windows whose ingestion run already computes the node. - let mut runs = HashSet::new(); for consumer in of_node { - if let Consumer::Materialized(node) = consumer { - if let Some(Some(window)) = refresh_windows.get(&node) { - if !runs.insert(*window) { - continue; - } - } - } entry.0 += 1; entry.1 += match consumer { Consumer::Query(query) => query_rates[query], @@ -416,6 +399,9 @@ pub fn computations( .map(|n| { let times = match rate_of_materialized.get(&n.id) { Some(&rate) => (1, rate), + // Each arriving row is computed once at ingestion, whatever + // its consumers. + None if n.output_state.timing == ExecutionTiming::IngestionTime => (1, 1.0), None => count.get(&n.id).copied().unwrap_or((0, 0.0)), }; (n.id, times) diff --git a/crates/integration-tests/tests/main.rs b/crates/integration-tests/tests/main.rs index 2f36e7401..2a06f2b5c 100644 --- a/crates/integration-tests/tests/main.rs +++ b/crates/integration-tests/tests/main.rs @@ -32,4 +32,5 @@ mod schema; mod sql_cardinality; mod sql_frequency; mod sql_to_post_asap; +mod stage3_objective; mod time_range; diff --git a/crates/integration-tests/tests/stage3_objective.rs b/crates/integration-tests/tests/stage3_objective.rs new file mode 100644 index 000000000..c7f19fcc4 --- /dev/null +++ b/crates/integration-tests/tests/stage3_objective.rs @@ -0,0 +1,116 @@ +//! Stage 3's selection objective is the cost of the plan it builds: the +//! MILP's optimum (`Selection::selected_plan_cost_per_second`) equals +//! `asap_cost::plan_cost` of the selected plan, under the default unit +//! costs and under unit costs that charge per-request overhead and disk. + +use asap_cost::{CostInputs, CostModel, ResourceUnitCost}; +use asap_devtools::planning_stages::{lower_promql_roots, run_planning_stages_over}; +use asap_planner::{DeploymentCapabilities, PlanningModels}; +use asap_types::workload::PlanningWorkload; + +/// PromQL `queries`, each `(query, interval_ms, accuracy JSON)`, over the +/// gauge `m` (1,000 series, one sample per second, 1,000 rows/s). +fn workload(queries: &[(&str, u32, &str)]) -> PlanningWorkload { + let entries: Vec = queries + .iter() + .map(|(query, interval_ms, accuracy)| { + format!( + r#"{{"query": {query:?}, "demand": {{"fixed_interval_at": {{"interval": {interval_ms}, "evaluation_phase": 0}}}}, + "requirements": {{{accuracy}}}, "predictability": {{"predictable": {{"known_at": null}}}}}}"# + ) + }) + .collect(); + serde_json::from_str(&format!( + r#"{{"query_workload": {{"language": "promql", "repeating_queries": [{}]}}, + "data_workload": {{"arrival": {{"ingesting": {{"rows_per_second": 1000.0}}}}, + "sources": {{"m": {{"series_count": 1000, "metric_type": "gauge", "sample_interval": 1000}}}}}}}}"#, + entries.join(",") + )) + .unwrap() +} + +/// The MILP's optimum and `plan_cost` of the plan Stage 3 selects for +/// `workload` under `settings`, with raw data kept or not. +fn objective_and_plan_cost( + workload: &PlanningWorkload, + settings: &CostInputs, + raw_data_retained: bool, +) -> (f64, f64) { + let capabilities = DeploymentCapabilities { + raw_data_retained, + ..Default::default() + }; + let models = PlanningModels::builtin() + .with_cost_inputs(settings) + .with_deployment_capabilities(&capabilities); + let entries: Vec<_> = workload.query_workload.entries().collect(); + let data = workload.data_workload.clone().unwrap(); + let roots = lower_promql_roots(workload).unwrap(); + let run = run_planning_stages_over(roots, &entries, &data, models).unwrap(); + let (plan, selection) = run.run.selection.as_ref().expect("a plan is selected"); + let (cost, _) = asap_cost::plan_cost(plan, &entries, &data, settings).unwrap(); + ( + selection.selected_plan_cost_per_second, + cost.cost_per_second.expect("cost model 1"), + ) +} + +fn assert_equal(name: &str, (objective, plan_cost): (f64, f64)) { + assert!( + (objective - plan_cost).abs() <= 1e-9 * plan_cost.abs().max(1e-12), + "{name}: objective {objective} != plan_cost {plan_cost}" + ); +} + +/// Unit costs that charge what the default ones leave at 0: 50 µs of +/// runtime overhead per query request and 1e-9 per byte-second on disk. +fn overhead_and_disk_costs() -> CostInputs { + let mut settings = CostInputs::default(); + settings.measured_operation_costs.ns_per_evaluation = 50_000.0; + settings.cost_model = CostModel::UsageOverTime(ResourceUnitCost { + cost_per_disk_byte_second: 1e-9, + ..ResourceUnitCost::illustrative() + }); + settings +} + +/// A 5-minute sum every minute and a 14-minute max every 7 minutes, no raw +/// data kept: one ingestion-time `Scan` feeds a 60 s chain and a 420 s +/// chain. Each row is scanned once at ingestion, so the scan runs once +/// per second for both (not once per refresh window, which made +/// `plan_cost` 5.6% higher than the objective). +#[test] +fn an_ingestion_time_scan_feeding_chains_of_two_widths_is_charged_once_per_second() { + let w = workload(&[ + ("sum_over_time(m[5m])", 60_000, ""), + ("max_over_time(m[14m])", 420_000, ""), + ]); + assert_equal( + "two widths", + objective_and_plan_cost(&w, &CostInputs::default(), false), + ); +} + +/// The same objective as `plan_cost` with per-request overhead and disk +/// charged: a raw-data source two query-time scans read is kept on disk +/// once, at the larger read, and each query request pays its overhead. +#[test] +fn the_objective_includes_request_overhead_and_charges_raw_data_on_disk_once_per_source() { + let settings = overhead_and_disk_costs(); + let workloads = [ + ( + "two series filters on m", + workload(&[ + (r#"sum_over_time(m{job="a"}[5m])"#, 60_000, ""), + (r#"sum_over_time(m{job="b"}[5m])"#, 60_000, ""), + ]), + ), + ( + "one query", + workload(&[("count_over_time(m[1m])", 60_000, "")]), + ), + ]; + for (name, w) in &workloads { + assert_equal(name, objective_and_plan_cost(w, &settings, true)); + } +} diff --git a/crates/stage3-plan-selection/src/cost.rs b/crates/stage3-plan-selection/src/cost.rs index 40ce63de4..2778282b3 100644 --- a/crates/stage3-plan-selection/src/cost.rs +++ b/crates/stage3-plan-selection/src/cost.rs @@ -27,8 +27,10 @@ use std::collections::HashMap; use asap_cost::plan::execution_demand::evaluation_rate; use asap_cost::{ - node_resource_usage_and_cost, CostInputs, CostModel, NodeResourceUsageAndCost, ProducerOutput, + node_resource_usage_and_cost, raw_data_disk_cost_per_second, CostInputs, + NodeResourceUsageAndCost, ProducerOutput, }; +use asap_types::ir::operator::Source; use asap_types::ir::properties::ExecutionTiming; use asap_types::ir::properties::{DataPrimitive, ExecutionDataState}; use asap_types::ir::{ASAPOp, NonASAPOp, Operator}; @@ -49,6 +51,16 @@ pub(crate) struct SelectionCosts { /// $c_{u,c,i}$ per second of each selectable edge whose producer and /// consumer are costable; an edge missing here cannot be selected. pub(crate) edge: HashMap, + /// By [`PhysicalCandidateId`], for a query-time `Scan`: its source and + /// the cost per second of keeping the raw data it reads on disk + /// ([`asap_cost::raw_data_disk_cost_per_second`]). A plan keeps each + /// source once, for the selected scan that keeps the most, as + /// `plan_cost` charges it. + pub(crate) raw_data_on_disk: Vec>, + /// The runtime's fixed overhead per query request, per second + /// ([`asap_cost::request_overhead_cost_per_second`]): every plan pays + /// it. + pub(crate) request_overhead_cost_per_second: f64, } /// Candidate `v` as a plan node with id `id`, its input `i` read from node @@ -183,6 +195,7 @@ pub(crate) fn selection_costs( data: &DataWorkload, cost_inputs: &CostInputs, formula_scale: f64, + request_overhead_cost_per_second: f64, ) -> SelectionCosts { let memo = &physical.logical_candidates.memo; let count = physical.physical_candidates.len(); @@ -275,11 +288,19 @@ pub(crate) fn selection_costs( let candidate: Vec> = usage .iter() .map(|usage| match usage { - Some(Ok(usage)) => cost_per_second(usage, cost_inputs), + Some(Ok(usage)) => cost_per_second(usage), Some(Err(error)) => Err(error.clone()), None => Err("it reads a group on a cycle of candidates".to_string()), }) .collect(); + let raw_data_on_disk = usage + .iter() + .map(|usage| { + let (source, bytes) = usage.as_ref()?.as_ref().ok()?.raw_scan?; + let cost = raw_data_disk_cost_per_second(bytes, cost_inputs)?; + Some((source.clone(), cost)) + }) + .collect(); let mut edge = HashMap::new(); for &id in selectable { let e = physical.edge(id); @@ -304,12 +325,17 @@ pub(crate) fn selection_costs( formula_scale, scan_read[c.0], ) - .and_then(|usage| cost_per_second(&usage, cost_inputs)); + .and_then(|usage| cost_per_second(&usage)); if let Ok(with_u) = with_u { edge.insert(id, with_u - own); } } - SelectionCosts { candidate, edge } + SelectionCosts { + candidate, + edge, + raw_data_on_disk, + request_overhead_cost_per_second, + } } /// The logical output of each input of `v` over the costed candidates @@ -476,22 +502,13 @@ fn cost_node<'a>( } /// $w_v$ from a candidate's usage: its cost per second under cost model 1 -/// plus the raw data a query-time scan retains on disk. -fn cost_per_second( - usage: &NodeResourceUsageAndCost<'_>, - cost_inputs: &CostInputs, -) -> Result { - let CostModel::UsageOverTime(unit_cost) = &cost_inputs.cost_model else { - return Err("the selection MILP costs under cost model 1 only".into()); - }; - let own = usage +/// (the raw data a query-time scan keeps on disk is charged per source, +/// [`SelectionCosts::raw_data_on_disk`]). +fn cost_per_second(usage: &NodeResourceUsageAndCost<'_>) -> Result { + let cost = usage .cost .cost_per_second - .ok_or("cost model 1 gives every node a cost per second")?; - let raw_data = usage.raw_scan.map_or(0.0, |(_, bytes)| { - unit_cost.cost_per_disk_byte_second * bytes as f64 - }); - let cost = own + raw_data; + .ok_or("the selection MILP costs under cost model 1 only")?; match cost.is_finite() && cost >= 0.0 { true => Ok(cost), false => Err(format!( diff --git a/crates/stage3-plan-selection/src/lib.rs b/crates/stage3-plan-selection/src/lib.rs index 0bfd35a58..23c809e15 100644 --- a/crates/stage3-plan-selection/src/lib.rs +++ b/crates/stage3-plan-selection/src/lib.rs @@ -20,7 +20,7 @@ mod report; use std::collections::BTreeSet; -use asap_cost::{summary_type_name, CostInputs, CostModel}; +use asap_cost::{request_overhead_cost_per_second, summary_type_name, CostInputs, CostModel}; use asap_types::ir::properties::ExecutionTiming; use asap_types::ir::schema::FieldDataType; use asap_types::ir::{ASAPOp, Operator, QueryRoot}; @@ -83,6 +83,12 @@ pub fn select_plan( }) .map(|(id, _)| id) .collect(); + let request_overhead_cost_per_second = request_overhead_cost_per_second(entries, cost_inputs) + .map_err(|error| ErrorSelectingPlan::Cost { + candidate: "every plan (the request overhead)".into(), + node: 0, + error, + })?; let costs = selection_costs( physical, &selectable, @@ -90,6 +96,7 @@ pub fn select_plan( data, cost_inputs, formula_scale, + request_overhead_cost_per_second, ); let roots: Vec> = queries.iter().map(|query| query.root.clone()).collect(); @@ -150,6 +157,7 @@ pub fn select_plan( reason: PruneReason::UsageNotCostable, }) .collect(), + selected_plan_cost_per_second: solved.objective_cost_per_second, milp_variables: solved.variables, milp_constraints: solved.constraints, milp_construction_ms: solved.construction_ms, diff --git a/crates/stage3-plan-selection/src/milp/model.rs b/crates/stage3-plan-selection/src/milp/model.rs index 6b7382e83..c65ad927d 100644 --- a/crates/stage3-plan-selection/src/milp/model.rs +++ b/crates/stage3-plan-selection/src/milp/model.rs @@ -3,14 +3,14 @@ //! //! | Family | Variables | Constraints | //! |---|---|---| -//! | baseline (section 3) | $x_v$, $y_{u,c,i}$, $z_{q,v}$, $t_v$ ([`SelectionVariables`]) | (A) [`input_satisfaction_constraints`], (B) [`producer_selection_constraints`], (C)/(D) [`query_coverage_constraints`], (E) [`acyclicity_constraints`], (F) [`utilization_constraints`], one consumer of a query-time result that is not materialized [`unshared_result_constraints`]; (G), (H): $y$ exists only on admissible edges | +//! | baseline (section 3) | $x_v$, $y_{u,c,i}$, $z_{q,v}$, $t_v$, $r_s$ ([`SelectionVariables`]) | (A) [`input_satisfaction_constraints`], (B) [`producer_selection_constraints`], (C)/(D) [`query_coverage_constraints`], (E) [`acyclicity_constraints`], (F) [`utilization_constraints`], one consumer of a query-time result that is not materialized [`unshared_result_constraints`], each source's raw data on disk once [`raw_data_disk_constraints`]; (G), (H): $y$ exists only on admissible edges | //! | execution contexts (section 5) | $x^{\mathrm{mat}}_v$, $x_{\kappa,v}$, $y_{\kappa,u,c,i}$, $h_v$ ([`ExecutionContextVariables`]) | (R) [`retention_constraints`] | //! | accuracy (section 4) | $E_{c,m}$, $F_{c,i,m}$, $D_c$, $H_{c,i}$ ([`AccuracyVariables`]) | (K) [`accuracy_constraints`] | //! //! The baseline is solved by [`solve_selection`]; the extensions' bodies //! are `todo!()` until execution contexts and constraint (K) are added. -use std::collections::HashMap; +use std::collections::{BTreeMap, HashMap}; use std::time::Instant; use good_lp::solvers::highs::highs; @@ -19,6 +19,7 @@ use good_lp::{ }; use asap_types::accuracy::AccuracyMetric; +use asap_types::ir::operator::Source; use asap_types::ir::properties::ExecutionTiming; use asap_types::stages::Lifetime; use asap_types::stages::{ @@ -55,6 +56,10 @@ pub(crate) struct SelectionVariables { /// $t_v$, by [`PhysicalCandidateId`]: only for candidates on a cycle of /// admissible edges (a strongly connected component); `None` elsewhere. t: Vec>, + /// Per raw-data source a query-time scan keeps on disk at a cost: the + /// cost per second of keeping it, at least each selected scan's + /// ([`raw_data_disk_constraints`]). + raw_data_on_disk: BTreeMap, } /// $x$, $y$, $z$ and $t$ over `inputs`, added to `problem`. @@ -88,7 +93,19 @@ fn selection_variables( .into_iter() .map(|size| (size > 1).then(|| problem.add(variable().min(0).max((size - 1) as f64)))) .collect(); - SelectionVariables { x, y, z, t } + let mut raw_data_on_disk = BTreeMap::new(); + for (source, _) in inputs.costs.raw_data_on_disk.iter().flatten() { + raw_data_on_disk + .entry(source.clone()) + .or_insert_with(|| problem.add(variable().min(0))); + } + SelectionVariables { + x, + y, + z, + t, + raw_data_on_disk, + } } /// (A) $\sum_{u \in P_{c,i}} y_{u,c,i} = x_c$. @@ -196,6 +213,27 @@ fn unshared_result_constraints( .collect() } +/// The raw data of each source is kept on disk once, for the selected scan +/// that keeps the most: $r_s \ge d_v x_v$ for each query-time `Scan` $v$ of +/// source $s$, $d_v$ its cost per second of the raw data it keeps. +fn raw_data_disk_constraints( + inputs: &SelectionInputs<'_>, + variables: &SelectionVariables, +) -> Vec { + inputs + .costs + .raw_data_on_disk + .iter() + .enumerate() + .filter_map(|(v, on_disk)| { + let (source, cost) = on_disk.as_ref()?; + Some(constraint!( + variables.raw_data_on_disk[source] >= *cost * variables.x[v] + )) + }) + .collect() +} + /// Per candidate, its selected edges to consumers and query outputs: /// $\sum_{(v,c,i)} y_{v,c,i} + \sum_q z_{q,v}$. fn uses_of(inputs: &SelectionInputs<'_>, variables: &SelectionVariables) -> Vec { @@ -316,6 +354,8 @@ pub(crate) struct SolvedSelection { /// The candidate answering each of `SelectionInputs::query_outputs`, in /// its order. pub(crate) outputs: Vec, + /// The optimum, unscaled: the selected plan's cost per second. + pub(crate) objective_cost_per_second: f64, pub(crate) variables: usize, pub(crate) constraints: usize, pub(crate) construction_ms: u64, @@ -332,8 +372,9 @@ pub(crate) enum SelectionMilpError { } /// Builds and solves the baseline MILP: minimize -/// $\sum_v w_v x_v + \sum_{(u,c,i)} c_{u,c,i} y_{u,c,i}$ under (A)–(F) and -/// [`unshared_result_constraints`], with HiGHS at gap 0 on one thread, the +/// $\sum_v w_v x_v + \sum_{(u,c,i)} c_{u,c,i} y_{u,c,i} + \sum_s r_s$ under +/// (A)–(F), [`unshared_result_constraints`] and +/// [`raw_data_disk_constraints`], with HiGHS at gap 0 on one thread, the /// coefficients scaled so the largest is 1,000 (HiGHS's tolerances are /// absolute). pub(crate) fn solve_selection( @@ -349,6 +390,7 @@ pub(crate) fn solve_selection( acyclicity_constraints(inputs, &variables), utilization_constraints(inputs, &variables), unshared_result_constraints(inputs, &variables), + raw_data_disk_constraints(inputs, &variables), ] .into_iter() .flatten() @@ -359,6 +401,14 @@ pub(crate) fn solve_selection( .iter() .flatten() .chain(inputs.costs.edge.values()) + .chain( + inputs + .costs + .raw_data_on_disk + .iter() + .flatten() + .map(|(_, cost)| cost), + ) .fold(0.0_f64, |largest, &cost| largest.max(cost.abs())); let scale = match largest > 0.0 && largest.is_finite() { true => 1_000.0 / largest, @@ -371,7 +421,14 @@ pub(crate) fn solve_selection( .zip(&variables.x) .filter_map(|(cost, &x)| cost.as_ref().ok().map(|cost| cost * scale * x)); let edge_terms = sorted(&variables.y).map(|(edge, y)| inputs.costs.edge[&edge] * scale * y); - let objective: Expression = candidate_terms.chain(edge_terms).sum(); + let raw_data_terms = variables + .raw_data_on_disk + .values() + .map(|&on_disk| scale * on_disk); + let objective: Expression = candidate_terms + .chain(edge_terms) + .chain(raw_data_terms) + .sum(); let variable_count = problem.len(); // HiGHS's columns are the variables in the order they were added. let column_of: HashMap = problem @@ -412,7 +469,29 @@ pub(crate) fn solve_selection( other => return Err(SelectionMilpError::Solver(format!("{other:?}"))), }; let chosen = |variable: Variable| columns[column_of[&variable]] > 0.5; + // The optimum, unscaled: the selected candidates' and edges' costs, + // each source's raw data on disk at its largest selected scan, and the + // request overhead every plan pays. + let mut raw_data_on_disk: BTreeMap<&Source, f64> = BTreeMap::new(); + for (source, cost) in (0..variables.x.len()) + .filter(|&v| chosen(variables.x[v])) + .filter_map(|v| inputs.costs.raw_data_on_disk[v].as_ref()) + { + let kept = raw_data_on_disk.entry(source).or_default(); + *kept = kept.max(*cost); + } + let objective_cost_per_second = (0..variables.x.len()) + .filter(|&v| chosen(variables.x[v])) + .filter_map(|v| inputs.costs.candidate[v].as_ref().ok()) + .sum::() + + sorted(&variables.y) + .filter(|&(_, y)| chosen(y)) + .map(|(edge, _)| inputs.costs.edge[&edge]) + .sum::() + + raw_data_on_disk.values().sum::() + + inputs.costs.request_overhead_cost_per_second; Ok(SolvedSelection { + objective_cost_per_second, candidates: (0..variables.x.len()) .filter(|&v| chosen(variables.x[v])) .map(PhysicalCandidateId) diff --git a/crates/stage3-plan-selection/src/milp/model/tests.rs b/crates/stage3-plan-selection/src/milp/model/tests.rs index 24076c603..ffddb86e9 100644 --- a/crates/stage3-plan-selection/src/milp/model/tests.rs +++ b/crates/stage3-plan-selection/src/milp/model/tests.rs @@ -119,6 +119,8 @@ fn costs( SelectionCosts { candidate: w.iter().map(|&w| Ok(w)).collect(), edge: edge_cost, + raw_data_on_disk: vec![None; w.len()], + request_overhead_cost_per_second: 0.0, } } diff --git a/crates/stage3-plan-selection/src/report.rs b/crates/stage3-plan-selection/src/report.rs index e20bb2a60..a90cc4128 100644 --- a/crates/stage3-plan-selection/src/report.rs +++ b/crates/stage3-plan-selection/src/report.rs @@ -135,8 +135,7 @@ impl std::fmt::Display for AcceptedAccuracy { } /// What Stage 3 reports about its own run, beside the plan -/// ([`select_plan`](crate::select_plan)). The plan's cost is -/// `asap_cost::plan_cost` of the plan; this does not repeat it. +/// ([`select_plan`](crate::select_plan)). #[derive(Debug, Clone, PartialEq)] pub struct Selection { /// Per query, the accuracy Stage 3 accepted for the selected plan. @@ -144,6 +143,10 @@ pub struct Selection { /// The candidates Stage 3 ruled out before the selection MILP: /// dominated ones and those whose usage could not be costed. pub pruned_candidates: Vec>, + /// The selection MILP's optimum: the selected plan's cost per second + /// under cost model 1, which `asap_cost::plan_cost` of the plan + /// reports too. + pub selected_plan_cost_per_second: f64, /// The selection MILP's size. pub milp_variables: usize, pub milp_constraints: usize, From 18aa65c5defbed8d4fd524a8d471514eae86e36a Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:38:42 +0000 Subject: [PATCH 05/31] fix(stage3): leave out candidates dearer than a feasible plan before scaling the MILP's objective (#1207 PR 6) The objective was scaled so its largest coefficient is 1,000. One unused candidate costing 2.55e6/s shrank a 0.001/s difference between two plans to 4e-7, under HiGHS's tolerances, and the solver returned a plan 12.6% dearer (`max by (job)(count_over_time(m[1m]))`). `milp::upper_bound`: a feasible plan found greedily (each query output's cheapest tree of candidates, trees sharing what they have in common, rebuilt without a candidate that would get two consumers while not materialized, or close a cycle) bounds the optimum. A candidate whose own cost plus the most its input edges take off is over that bound has x fixed at 0 and no y, and the scale is the largest coefficient left. Test: `milp::model::tests::a_candidate_dearer_than_a_feasible_plan_does_not_hide_a_cheaper_plan` (the reviewed workload's shape and costs; failed before). --- crates/stage3-plan-selection/src/milp/mod.rs | 1 + .../stage3-plan-selection/src/milp/model.rs | 65 +++-- .../src/milp/model/tests.rs | 51 ++++ .../src/milp/upper_bound.rs | 252 ++++++++++++++++++ 4 files changed, 343 insertions(+), 26 deletions(-) create mode 100644 crates/stage3-plan-selection/src/milp/upper_bound.rs diff --git a/crates/stage3-plan-selection/src/milp/mod.rs b/crates/stage3-plan-selection/src/milp/mod.rs index 9a2d7f7e3..83351b8d4 100644 --- a/crates/stage3-plan-selection/src/milp/mod.rs +++ b/crates/stage3-plan-selection/src/milp/mod.rs @@ -1,3 +1,4 @@ //! Stage 3's selection MILP (concepts doc, sections 3–5): its variables, //! one function per constraint family, and the execution contexts. pub(crate) mod model; +mod upper_bound; diff --git a/crates/stage3-plan-selection/src/milp/model.rs b/crates/stage3-plan-selection/src/milp/model.rs index c65ad927d..6c3d602e3 100644 --- a/crates/stage3-plan-selection/src/milp/model.rs +++ b/crates/stage3-plan-selection/src/milp/model.rs @@ -26,6 +26,7 @@ use asap_types::stages::{ ExecutionContextId, PhysicalASAPMemoCandidates, PhysicalCandidateId, PhysicalEdgeId, }; +use super::upper_bound::dearer_than_a_feasible_plan; use crate::cost::{input_count, SelectionCosts}; /// A query, by index in the workload. @@ -62,19 +63,33 @@ pub(crate) struct SelectionVariables { raw_data_on_disk: BTreeMap, } -/// $x$, $y$, $z$ and $t$ over `inputs`, added to `problem`. +/// $x$, $y$, $z$, $t$ and $r$ over `inputs`, added to `problem`: $x_v$ +/// fixed at 0 for a candidate that could not be costed or is `left_out`, +/// and no $y$ on an edge of one. fn selection_variables( inputs: &SelectionInputs<'_>, + left_out: &[bool], problem: &mut ProblemVariables, ) -> SelectionVariables { let count = inputs.physical.physical_candidates.len(); let x = (0..count) - .map(|v| match inputs.costs.candidate[v].is_ok() { - true => problem.add(variable().binary()), - false => problem.add(variable().min(0).max(0)), + .map( + |v| match inputs.costs.candidate[v].is_ok() && !left_out[v] { + true => problem.add(variable().binary()), + false => problem.add(variable().min(0).max(0)), + }, + ) + .collect(); + let mut edges: Vec = inputs + .costs + .edge + .keys() + .copied() + .filter(|&edge| { + let edge = inputs.physical.edge(edge); + !left_out[edge.producer_candidate_id.0] && !left_out[edge.consumer_candidate_id.0] }) .collect(); - let mut edges: Vec = inputs.costs.edge.keys().copied().collect(); edges.sort_unstable(); let y = edges .into_iter() @@ -374,15 +389,17 @@ pub(crate) enum SelectionMilpError { /// Builds and solves the baseline MILP: minimize /// $\sum_v w_v x_v + \sum_{(u,c,i)} c_{u,c,i} y_{u,c,i} + \sum_s r_s$ under /// (A)–(F), [`unshared_result_constraints`] and -/// [`raw_data_disk_constraints`], with HiGHS at gap 0 on one thread, the -/// coefficients scaled so the largest is 1,000 (HiGHS's tolerances are -/// absolute). +/// [`raw_data_disk_constraints`], with HiGHS at gap 0 on one thread. The +/// candidates dearer than a feasible plan are left out first +/// ([`dearer_than_a_feasible_plan`]), then the coefficients are scaled so +/// the largest left is 1,000 (HiGHS's tolerances are absolute). pub(crate) fn solve_selection( inputs: &SelectionInputs<'_>, ) -> Result { let started = Instant::now(); + let left_out = dearer_than_a_feasible_plan(inputs); let mut problem = ProblemVariables::new(); - let variables = selection_variables(inputs, &mut problem); + let variables = selection_variables(inputs, &left_out, &mut problem); let constraints: Vec = [ input_satisfaction_constraints(inputs, &variables), producer_selection_constraints(inputs, &variables), @@ -395,18 +412,16 @@ pub(crate) fn solve_selection( .into_iter() .flatten() .collect(); - let largest = inputs - .costs - .candidate - .iter() - .flatten() - .chain(inputs.costs.edge.values()) + // Scaled by the largest coefficient of a candidate the bound keeps. + let kept = |v: &usize| !left_out[*v]; + let largest = (0..left_out.len()) + .filter(kept) + .filter_map(|v| inputs.costs.candidate[v].as_ref().ok()) + .chain(sorted(&variables.y).map(|(edge, _)| &inputs.costs.edge[&edge])) .chain( - inputs - .costs - .raw_data_on_disk - .iter() - .flatten() + (0..left_out.len()) + .filter(kept) + .filter_map(|v| inputs.costs.raw_data_on_disk[v].as_ref()) .map(|(_, cost)| cost), ) .fold(0.0_f64, |largest, &cost| largest.max(cost.abs())); @@ -414,12 +429,10 @@ pub(crate) fn solve_selection( true => 1_000.0 / largest, false => 1.0, }; - let candidate_terms = inputs - .costs - .candidate - .iter() - .zip(&variables.x) - .filter_map(|(cost, &x)| cost.as_ref().ok().map(|cost| cost * scale * x)); + let candidate_terms = (0..left_out.len()).filter(kept).filter_map(|v| { + let cost = inputs.costs.candidate[v].as_ref().ok()?; + Some(cost * scale * variables.x[v]) + }); let edge_terms = sorted(&variables.y).map(|(edge, y)| inputs.costs.edge[&edge] * scale * y); let raw_data_terms = variables .raw_data_on_disk diff --git a/crates/stage3-plan-selection/src/milp/model/tests.rs b/crates/stage3-plan-selection/src/milp/model/tests.rs index ffddb86e9..555b0018c 100644 --- a/crates/stage3-plan-selection/src/milp/model/tests.rs +++ b/crates/stage3-plan-selection/src/milp/model/tests.rs @@ -283,3 +283,54 @@ fn acyclicity_forbids_two_groups_reading_each_other() { let outputs = vec![(QueryId(0), a), (QueryId(1), b)]; plan_of(&physical, &[], &solved, &outputs).unwrap(); } + +/// x08's shape: `max by (job)(count_over_time(m[1m]))`. Path A (scan and +/// range `OneEvaluation`, the root reading the range: 0.0058 + 0.0010 + +/// 0.0020) costs 0.0088/s; path B (each `None`, through three more groups) +/// 0.0078/s. An unused candidate costing 2.55e6/s scaled every coefficient +/// so the 0.001 difference fell under HiGHS's tolerances and the optimum +/// took path A, 12.6% dearer. A candidate dearer than a feasible plan is +/// left out before scaling, so the optimum is path B. +#[test] +fn a_candidate_dearer_than_a_feasible_plan_does_not_hide_a_cheaper_plan() { + let mut memo = empty_memo(); + let m = scan(&mut memo); + let r = range(&mut memo, 60, m); + let a = group(&mut memo, shift(1_000, r)); + let b = group(&mut memo, shift(2_000, a)); + let c = group(&mut memo, shift(3_000, b)); + let root = group(&mut memo, shift(4_000, c)); + let root_from_r = try_candidate(&mut memo, root, shift(10_000, r), None).unwrap(); + query(&mut memo, root, None); + let first = own_candidate; + let physical = physical( + memo, + &[ + (first(m), ONE_EVALUATION), // v0: path A's scan + (first(r), ONE_EVALUATION), // v1: path A's range + (root_from_r, NONE), // v2: path A's root, reading the range + (first(m), NONE), // v3: path B's scan + (first(r), NONE), // v4: path B's range + (first(a), NONE), // v5 + (first(b), NONE), // v6 + (first(c), NONE), // v7 + (first(root), NONE), // v8: path B's root + (first(c), ONE_EVALUATION), // v9: unused, 2.55e6/s + ], + ); + let w = [ + 0.005_802_088, + 0.001_000_36, + 0.002_033_333_5, + 0.005_800_000_034_8, + 0.001_000_000_006, + 0.001_000_007, + 1.666_666_666_666_666_7e-5, + 1.666_678_333_333_333_4e-5, + 1.666_666_666_666_666_7e-5, + 2_553_517.822_748_626_6, + ]; + let costs = costs(&physical, &w, &[]); + let solved = solve(&physical, &costs).unwrap(); + assert_eq!(solved.candidates, ids(&[3, 4, 5, 6, 7, 8])); +} diff --git a/crates/stage3-plan-selection/src/milp/upper_bound.rs b/crates/stage3-plan-selection/src/milp/upper_bound.rs new file mode 100644 index 000000000..731ae0a55 --- /dev/null +++ b/crates/stage3-plan-selection/src/milp/upper_bound.rs @@ -0,0 +1,252 @@ +//! An upper bound on the baseline MILP's optimum, and the candidates it +//! rules out: the cost of one feasible plan found greedily, and every +//! candidate that alone costs more than it. Leaving those out before the +//! objective is scaled keeps a candidate costing millions per second from +//! shrinking the differences between plans costing thousandths under the +//! solver's tolerances. +//! +//! Example (`max by (job)(count_over_time(m[1m]))`): the cheapest plan +//! costs 0.0078/s and an unused ingestion-time chain 2.55e6/s. Scaled so +//! the largest coefficient is 1,000, a 0.001/s difference between two +//! plans became 4e-7, and the solver returned a plan 12.6% dearer. The +//! greedy plan bounds the optimum by about 0.0088/s, so the chain is left +//! out and the largest coefficient left is about 0.006/s. + +use std::collections::{BTreeMap, HashMap}; + +use asap_types::ir::operator::Source; +use asap_types::ir::properties::ExecutionTiming; +use asap_types::stages::{Lifetime, PhysicalCandidateId}; + +use super::model::SelectionInputs; +use crate::cost::input_count; + +/// How many times the greedy plan is rebuilt without a candidate that +/// made it infeasible before giving up on a bound. +const REPAIRS: usize = 32; + +/// Per (consumer, input), the costed edges into it, each as (producer, +/// $c_{u,c,i}$). +type EdgesInto = HashMap<(usize, usize), Vec<(usize, f64)>>; + +/// Per input of a candidate, the edge its cheapest tree reads it over, as +/// (producer, $c_{u,c,i}$). +type TreeInputs = Vec>; + +/// Per candidate, whether it costs more than a feasible plan +/// ([`feasible_plan_cost_per_second`]): its own $w_v$ plus the most its +/// input edges can take off ($\min(0, c_{u,v,i})$ per input) is over the +/// bound. Such a candidate is in no optimal plan, as long as every other +/// selected candidate costs at least 0 with its selected inputs. None is +/// ruled out when no feasible plan is found. +pub(crate) fn dearer_than_a_feasible_plan(inputs: &SelectionInputs<'_>) -> Vec { + let count = inputs.physical.physical_candidates.len(); + let Some(bound) = feasible_plan_cost_per_second(inputs) else { + return vec![false; count]; + }; + let mut cheapest_input_edge: HashMap<(PhysicalCandidateId, usize), f64> = HashMap::new(); + for (&edge, &cost) in &inputs.costs.edge { + let edge = inputs.physical.edge(edge); + let key = (edge.consumer_candidate_id, edge.input_index_of_consumer); + let cheapest = cheapest_input_edge.entry(key).or_insert(0.0); + *cheapest = cheapest.min(cost); + } + let tolerance = 1e-9 * bound.abs().max(1e-12); + (0..count) + .map(PhysicalCandidateId) + .map(|v| { + let Ok(own) = &inputs.costs.candidate[v.0] else { + return false; + }; + let least = own + + (0..input_count(inputs.physical, v)) + .filter_map(|i| cheapest_input_edge.get(&(v, i))) + .sum::(); + least > bound + tolerance + }) + .collect() +} + +/// The cost per second (without the request overhead every plan pays) of +/// a feasible plan: each query output reads the cheapest tree of +/// candidates below it, each candidate's input from the producer whose +/// tree is cheapest ($w_v + \sum_i \min_u (c_{u,v,i} + \text{tree}_u)$). +/// Trees share the candidates they have in common; when that gives a +/// query-time result that is not materialized two consumers, or a cycle, +/// the plan is rebuilt without that candidate. `None` when no feasible +/// plan is found that way. +fn feasible_plan_cost_per_second(inputs: &SelectionInputs<'_>) -> Option { + let physical = inputs.physical; + let count = physical.physical_candidates.len(); + // Each (consumer, input)'s costed edges: (edge, producer, c_{u,c,i}). + let mut into: EdgesInto = HashMap::new(); + for (&id, &cost) in &inputs.costs.edge { + let edge = physical.edge(id); + into.entry((edge.consumer_candidate_id.0, edge.input_index_of_consumer)) + .or_default() + .push((edge.producer_candidate_id.0, cost)); + } + let inputs_of: Vec = (0..count) + .map(|v| input_count(physical, PhysicalCandidateId(v))) + .collect(); + let unshared = |v: usize| { + let properties = physical.physical_candidates[v].physical_properties; + properties.lifetime() == Lifetime::None && properties.timing() == ExecutionTiming::QueryTime + }; + let mut excluded = vec![false; count]; + for _ in 0..REPAIRS { + let (tree_cost, chosen) = cheapest_trees(inputs, &into, &inputs_of, &excluded); + // Each query output's cheapest candidate, then the trees below. + let mut uses = vec![0usize; count]; + let mut outputs = Vec::new(); + for (_, candidates) in inputs.query_outputs { + let best = candidates + .iter() + .map(|v| v.0) + .filter(|&v| tree_cost[v].is_finite()) + .min_by(|&a, &b| tree_cost[a].total_cmp(&tree_cost[b]))?; + uses[best] += 1; + outputs.push(best); + } + let plan = match plan_below(&outputs, &chosen, &inputs_of) { + Ok(plan) => plan, + Err(on_cycle) => { + excluded[on_cycle] = true; + continue; + } + }; + for &(v, _) in &plan.edges { + uses[v] += 1; + } + if let Some(shared) = plan + .candidates + .iter() + .copied() + .find(|&v| unshared(v) && uses[v] > 1) + { + excluded[shared] = true; + continue; + } + let mut raw_data_on_disk: BTreeMap<&Source, f64> = BTreeMap::new(); + for &v in &plan.candidates { + if let Some((source, cost)) = &inputs.costs.raw_data_on_disk[v] { + let kept = raw_data_on_disk.entry(source).or_default(); + *kept = kept.max(*cost); + } + } + let cost = plan + .candidates + .iter() + .filter_map(|&v| inputs.costs.candidate[v].as_ref().ok()) + .sum::() + + plan.edges.iter().map(|&(_, cost)| cost).sum::() + + raw_data_on_disk.values().sum::(); + return cost.is_finite().then_some(cost); + } + None +} + +/// Per candidate not `excluded`, the cost of its cheapest tree and, per +/// input, the edge it reads it over: relaxed until no tree gets cheaper +/// (at most once per candidate), so a tree over a cycle of candidates is +/// never cheaper than one without it when costs are not negative. +fn cheapest_trees( + inputs: &SelectionInputs<'_>, + into: &EdgesInto, + inputs_of: &[usize], + excluded: &[bool], +) -> (Vec, Vec) { + let count = inputs_of.len(); + let mut tree_cost = vec![f64::INFINITY; count]; + let mut chosen: Vec = inputs_of.iter().map(|&n| vec![None; n]).collect(); + for _ in 0..count.max(1) { + let mut changed = false; + for v in 0..count { + let Ok(own) = &inputs.costs.candidate[v] else { + continue; + }; + if excluded[v] { + continue; + } + let mut cost = *own; + let mut reads = Vec::with_capacity(inputs_of[v]); + for i in 0..inputs_of[v] { + let cheapest = into + .get(&(v, i)) + .into_iter() + .flatten() + .filter(|&&(u, _)| !excluded[u] && tree_cost[u].is_finite()) + .map(|&(u, c)| (u, c, c + tree_cost[u])) + .min_by(|a, b| a.2.total_cmp(&b.2)); + match cheapest { + Some((u, c, through)) => { + cost += through; + reads.push(Some((u, c))); + } + None => { + cost = f64::INFINITY; + break; + } + } + } + if cost < tree_cost[v] - 1e-12 * cost.abs().max(1e-300) { + tree_cost[v] = cost; + chosen[v] = reads; + changed = true; + } + } + if !changed { + break; + } + } + (tree_cost, chosen) +} + +/// A plan of the trees below some query outputs. +struct GreedyPlan { + /// Its candidates, each once. + candidates: Vec, + /// Its edges, as (producer, $c_{u,c,i}$). + edges: Vec<(usize, f64)>, +} + +/// The plan of the trees `chosen` below `outputs`; Err with a candidate +/// on a cycle of them. +fn plan_below( + outputs: &[usize], + chosen: &[TreeInputs], + inputs_of: &[usize], +) -> Result { + // 0 unvisited, 1 on the walk, 2 done. + let mut state = vec![0u8; inputs_of.len()]; + let mut plan = GreedyPlan { + candidates: Vec::new(), + edges: Vec::new(), + }; + for &output in outputs { + if state[output] == 2 { + continue; + } + let mut stack = vec![(output, 0usize)]; + state[output] = 1; + while let Some((v, i)) = stack.pop() { + if i == inputs_of[v] { + state[v] = 2; + plan.candidates.push(v); + continue; + } + stack.push((v, i + 1)); + let (u, c) = chosen[v][i].expect("a finite tree reads every input"); + plan.edges.push((u, c)); + match state[u] { + 0 => { + state[u] = 1; + stack.push((u, 0)); + } + 1 => return Err(u), + _ => {} + } + } + } + Ok(plan) +} From 70a45cf376ec16467bfa1d06a693a3b353dc2bd8 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:40:47 +0000 Subject: [PATCH 06/31] fix(accuracy): an exact query reads every group below its root only exactly (#1207 PR 6) `ErrorMetricsOfMemo::exact_only_groups_of_query` seeded each query's root with its target's metrics; an exact target names no metric, so it allowed every metric and no group was exact-only for it. Example: `quantile_over_time(0.9, m[5m])` exact (Q0) and at rank error 0.01 (Q1): the KLL readouts were readable by Q0, and a plan let Q0 read Q1's KLL (0/12 values exact). An exact target now allows no metric, so every group below its root is exact-only for that query. Stage 3 enforces the list per query with execution contexts. Test: integration `pass2_promql::exact_only_groups::an_exact_query_reads_every_sketch_readout_only_exactly` (failed before). --- crates/accuracy/src/metric_rule/mod.rs | 10 +++++++-- .../tests/pass2_promql/exact_only_groups.rs | 22 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/crates/accuracy/src/metric_rule/mod.rs b/crates/accuracy/src/metric_rule/mod.rs index 46adafebb..51dd3e7e9 100644 --- a/crates/accuracy/src/metric_rule/mod.rs +++ b/crates/accuracy/src/metric_rule/mod.rs @@ -143,7 +143,7 @@ impl ErrorMetricsOfMemo { /// metric rule per query where the edges apply it per group. Walking /// down the live candidates (not in `dead`) from each root with a /// target, which allows its target's metrics - /// ([`query_roots_with_target`]), a candidate passes to its inputs the + /// ([`query_roots_with_target`]; an exact target none), a candidate passes to its inputs the /// metrics its error contract passes; a group keeps, per query, the /// metrics every path to it passes. A group is exact-only for a query /// when no metric reaches it (one path through a consumer without a @@ -184,7 +184,13 @@ impl ErrorMetricsOfMemo { .and_modify(|known| known.retain(|metric| metrics.contains(metric))) .or_insert(metrics); } - for (query, root, _, metrics) in query_roots_with_target(memo) { + for (query, root, target, metrics) in query_roots_with_target(memo) { + // An exact target allows no error: the query reads every group + // below its root exactly. + let metrics = match target.is_exact() { + true => Vec::new(), + false => metrics, + }; keep_common(&mut allowed, root, query, metrics); } let mut exact_only = vec![BTreeSet::new(); memo.queries().len()]; diff --git a/crates/integration-tests/tests/pass2_promql/exact_only_groups.rs b/crates/integration-tests/tests/pass2_promql/exact_only_groups.rs index aa6461292..127f8687d 100644 --- a/crates/integration-tests/tests/pass2_promql/exact_only_groups.rs +++ b/crates/integration-tests/tests/pass2_promql/exact_only_groups.rs @@ -117,3 +117,25 @@ fn a_dead_candidate_makes_no_group_exact_only() { ); } } + +/// `quantile_over_time(0.9, m[5m])` twice, Q0 exact and Q1 at rank error +/// 0.01: the KLL readouts survive for Q1, but Q0 reads no sketch, so every +/// sketch readout group (and every group below one) is exact-only for Q0. +/// An exact target allows no error metric. +#[test] +fn an_exact_query_reads_every_sketch_readout_only_exactly() { + let query = "quantile_over_time(0.9, m[5m])"; + let exact = serde_json::to_value(asap_types::accuracy::AccuracyTarget::exact()).unwrap(); + let logical = promql_pass2(&[ + (query, exact), + (query, json!({"max_error": 0.01, "accuracy_metric": "rank"})), + ]); + let exact_only = &logical.exact_only_groups_of_query; + let kll = readout_groups(&logical, SketchAlgorithm::Kll); + assert!(!kll.is_empty()); + for group in kll { + assert!(live(&logical, group), "G{} survives", group.0); + assert!(exact_only[0].contains(&group), "G{} for Q0", group.0); + assert!(!exact_only[1].contains(&group), "G{} for Q1", group.0); + } +} From dc52d5177dddcd98d7ec68ab7f67bd732e0f33ac Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:40:20 +0000 Subject: [PATCH 07/31] fix(executor): query-time tumbling-window chains are built at each evaluation; a query-time chain's readers set how far back its producer is read (#1207 PR 6) --- crates/cost/src/node/usage/tests.rs | 9 +- crates/cost/src/plan/cost.rs | 1 + crates/cost/src/plan/read_back.rs | 29 ++- crates/executor/src/operators/mod.rs | 3 + .../executor/src/operators/scope_timestamp.rs | 24 +- crates/executor/src/operators/time_range.rs | 66 +++++- crates/executor/src/operators/unchecked.rs | 30 ++- crates/executor/src/physical_planner/mod.rs | 106 +++++++-- .../executor/tests/tumbling_window_chain.rs | 130 ++++++++++- crates/planner/src/installable/tests.rs | 14 +- crates/planner/tests/runtime_install.rs | 205 ++++++++++++++++++ crates/stage3-plan-selection/src/cost.rs | 6 + crates/types/src/plan/materialized_view.rs | 18 +- .../physical-planning-and-deployment.md | 10 +- 14 files changed, 593 insertions(+), 58 deletions(-) diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index a65166844..b013064da 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -195,7 +195,14 @@ fn a_query_time_scan_reads_what_its_consumers_read_and_keeps_its_retention() { kind: TimeRangeKind::Range, child: 0, })); - let read_ms = read_back_ms([1], |_| &instant.payload, |_| vec![], |_| 0, true); + let read_ms = read_back_ms( + [1], + |_| &instant.payload, + |_| vec![], + |_| 0, + |_| false, + true, + ); assert_eq!(read_ms, Some(1_000)); let usage = node_resource_usage_and_cost(&scan, &[], (1, 1.0), read_ms, &data, &settings, 1.0).unwrap(); diff --git a/crates/cost/src/plan/cost.rs b/crates/cost/src/plan/cost.rs index 155af79b4..233e7ddfd 100644 --- a/crates/cost/src/plan/cost.rs +++ b/crates/cost/src/plan/cost.rs @@ -472,6 +472,7 @@ fn scan_read_ms( |id| &nodes[&id].payload, readers, |_| 0, + |id| nodes[&id].output_state.timing == ExecutionTiming::QueryTime, true, ) } diff --git a/crates/cost/src/plan/read_back.rs b/crates/cost/src/plan/read_back.rs index 06c09f5ca..e8e3554d0 100644 --- a/crates/cost/src/plan/read_back.rs +++ b/crates/cost/src/plan/read_back.rs @@ -14,17 +14,23 @@ use asap_types::ir::{ASAPOp, NonASAPOp, Operator}; /// through `TimeShift`s without `@` (`consumers_of` gives a node's own /// readers), the most of the shifts' offsets plus the `TimeRange`'s range /// plus `span_ms` of that `TimeRange` (a range query's span, for retention; -/// 0 for one execution's read). A tumbling-window chain reads its newest -/// window back. Any other reader reads at the evaluation time (its -/// `span_ms`), except over raw data (`reads_raw_data`), which it reads -/// whole: `None`, unbounded, as under a `TimeShift` anchored with `@`. +/// 0 for one execution's read). A tumbling-window chain built at ingestion +/// time reads its newest window back. A chain built at query time +/// (`query_time_chain`) builds, at each evaluation, the windows its own +/// readers read: the walk passes up through it as through a shift of 0. +/// Any other reader reads at the evaluation time (its `span_ms`), except +/// over raw data (`reads_raw_data`), which it reads whole: `None`, +/// unbounded, as under a `TimeShift` anchored with `@`. /// -/// Example: `sum_over_time(m[5m] offset 1h)` reads `m` back 1h + 5m. +/// Example: `sum_over_time(m[5m] offset 1h)` reads `m` back 1h + 5m, +/// directly or through a query-time chain of 1-minute windows; through an +/// ingestion-time chain of 1-minute windows, `m` is read back 1 minute. pub fn read_back_ms<'a, N: Copy + Ord, C: 'a>( consumers: impl IntoIterator, operator_of: impl Fn(N) -> &'a Operator, consumers_of: impl Fn(N) -> Vec, span_ms: impl Fn(N) -> i64, + query_time_chain: impl Fn(N) -> bool, reads_raw_data: bool, ) -> Option { let mut need_ms: i64 = 0; @@ -52,7 +58,18 @@ pub fn read_back_ms<'a, N: Copy + Ord, C: 'a>( continue; } Operator::NonASAP(NonASAPOp::TimeShift { .. }) => return None, - // A chain builds its newest window from it. + // A query-time chain reads what its readers read. + Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) + if query_time_chain(consumer) => + { + pending.extend( + consumers_of(consumer) + .into_iter() + .map(|reader| (reader, offset_ms)), + ); + continue; + } + // An ingestion-time chain builds its newest window from it. Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { tumbling_window_width_ms, .. diff --git a/crates/executor/src/operators/mod.rs b/crates/executor/src/operators/mod.rs index ee8b917b1..989950635 100644 --- a/crates/executor/src/operators/mod.rs +++ b/crates/executor/src/operators/mod.rs @@ -126,6 +126,9 @@ enum Kind { sample_value: Option, #[serde(default, skip_serializing_if = "Option::is_none")] latest_per_series: Option>, + /// Set by [`Operator::with_tumbling_window_ends`]. + #[serde(default, skip_serializing_if = "Option::is_none")] + tumbling_window_width_ms: Option, }, Limit { n: u64, diff --git a/crates/executor/src/operators/scope_timestamp.rs b/crates/executor/src/operators/scope_timestamp.rs index d0985a4ae..4e312a976 100644 --- a/crates/executor/src/operators/scope_timestamp.rs +++ b/crates/executor/src/operators/scope_timestamp.rs @@ -3,7 +3,22 @@ use super::*; use crate::runtime::Scope; impl Operator { + /// `input`'s columns as `output`'s, its time column stamped with the + /// run's time: an ingestion run's window end, a query run's evaluation + /// time. pub(crate) fn scope_timestamp(input: SchemaRef, output: SchemaRef) -> Result { + Self::window_end_timestamp(input, output, None) + } + + /// As [`Self::scope_timestamp`], except that with `window_end`, the + /// time column copies that input column: each row's window end, for the + /// states of a tumbling-window chain built at query time, one per + /// window ([`Self::with_tumbling_window_ends`]). + pub(crate) fn window_end_timestamp( + input: SchemaRef, + output: SchemaRef, + window_end: Option, + ) -> Result { crate::values::validate_schema(&output)?; let coordinate = output .time_index @@ -15,7 +30,14 @@ impl Operator { let mut used = std::collections::BTreeSet::new(); for (index, field) in output.fields.iter().enumerate() { if index == coordinate { - columns.push(None); + if let Some(window_end) = window_end { + if plain(&input, window_end)?.0 != &DataType::Timestamp + || !used.insert(window_end) + { + return Err(invalid("window end column must be a timestamp")); + } + } + columns.push(window_end); continue; } let matches: Vec<_> = input diff --git a/crates/executor/src/operators/time_range.rs b/crates/executor/src/operators/time_range.rs index f7f044f55..68462c232 100644 --- a/crates/executor/src/operators/time_range.rs +++ b/crates/executor/src/operators/time_range.rs @@ -46,11 +46,35 @@ impl Operator { width_ms, sample_value, latest_per_series, + tumbling_window_width_ms: None, }, inputs: vec![input.clone()], output: input, }) } + + /// 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. + pub fn with_tumbling_window_ends(mut self, width_ms: i64) -> Result { + let Kind::TimeRange { + latest_per_series: None, + tumbling_window_width_ms: slot, + .. + } = &mut self.kind + else { + return Err(invalid("tumbling-window ends need a range window")); + }; + if width_ms <= 0 { + return Err(invalid("a tumbling window has a positive width")); + } + *slot = Some(width_ms); + Ok(self) + } } impl Operator { @@ -148,6 +172,7 @@ pub(super) fn execute<'a>( width_ms, sample_value, ref latest_per_series, + tumbling_window_width_ms, } = operator.kind else { unreachable!() @@ -196,10 +221,15 @@ pub(super) fn execute<'a>( } let batch = batch?; let keep = kept(&batch, true)?; - if keep.true_count() == batch.num_rows() { - return Ok((*batch).clone()); + let batch = if keep.true_count() == batch.num_rows() { + (*batch).clone() + } else { + batch.filter(&keep)? + }; + match tumbling_window_width_ms { + Some(width_ms) => window_ends(batch, time, end, width_ms), + None => Ok(batch), } - batch.filter(&keep) }) .boxed_local()); }; @@ -242,3 +272,33 @@ 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 { + let times = batch + .column(time) + .as_any() + .downcast_ref::() + .ok_or_else(|| invalid("time range column is not a timestamp"))?; + // `end - t` is in `[k · width_ms, (k+1) · width_ms)` for the window + // ending at `end - k · width_ms`. + let ends: arrow::array::TimestampMillisecondArray = times + .iter() + .map(|t| t.map(|t| end - (end - t) / width_ms * width_ms)) + .collect(); + let rows = batch.num_rows(); + let mut arrays = Vec::with_capacity(batch.schema().fields.len()); + let mut states = Vec::with_capacity(arrays.capacity()); + for column in 0..batch.schema().fields.len() { + if column == time { + arrays.push(std::sync::Arc::new(ends.clone()) as arrow::array::ArrayRef); + states.push(None); + } else { + arrays.push(batch.column(column).clone()); + states.push(batch.state_column(column).cloned()); + } + } + Batch::from_parts(batch.schema().clone(), arrays, states, rows) +} diff --git a/crates/executor/src/operators/unchecked.rs b/crates/executor/src/operators/unchecked.rs index 3ab8bf172..84c6a44c6 100644 --- a/crates/executor/src/operators/unchecked.rs +++ b/crates/executor/src/operators/unchecked.rs @@ -31,7 +31,12 @@ impl TryFrom for Operator { Kind::Source(_) => return Err(invalid("physical plans cannot serialize live sources")), Kind::EvaluationTime => Operator::evaluation_time(), Kind::Constant { value, dtype } => Operator::scalar(value, dtype)?, - Kind::ScopeTimestamp { .. } => Operator::scope_timestamp(input(0)?, output.clone())?, + Kind::ScopeTimestamp { columns } => { + // The time column copies an input column when stamped with + // its rows' window ends. + let window_end = output.time_index.and_then(|time| *columns.get(time)?); + Operator::window_end_timestamp(input(0)?, output.clone(), window_end)? + } Kind::Union => Operator::union(input(0)?, inputs.len())?, Kind::CurrentSeries { identity, @@ -115,14 +120,21 @@ impl TryFrom for Operator { width_ms, sample_value, latest_per_series, - } => Operator::time_range( - input(0)?, - time, - end_offset_ms, - width_ms, - sample_value, - latest_per_series, - )?, + tumbling_window_width_ms, + } => { + let time_range = Operator::time_range( + input(0)?, + time, + end_offset_ms, + width_ms, + sample_value, + latest_per_series, + )?; + match tumbling_window_width_ms { + Some(width_ms) => time_range.with_tumbling_window_ends(width_ms)?, + None => time_range, + } + } Kind::Limit { n, offset, groups } => Operator::limit(input(0)?, n, offset, groups)?, Kind::Sort { keys, groups } => Operator::sort(input(0)?, keys, groups)?, Kind::Window { diff --git a/crates/executor/src/physical_planner/mod.rs b/crates/executor/src/physical_planner/mod.rs index 9fd8cfd01..deedcb94a 100644 --- a/crates/executor/src/physical_planner/mod.rs +++ b/crates/executor/src/physical_planner/mod.rs @@ -439,6 +439,11 @@ fn compile_internal( // 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. if let Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { summary_type, input: update, @@ -448,15 +453,12 @@ fn compile_internal( .. }) = &node.payload { - if node.output_state.timing - != planner_types::ir::properties::ExecutionTiming::IngestionTime - { - return Err(invalid(format!( - "node {id}: a tumbling-window chain is built in ingestion runs" - ))); + let query_time = node.output_state.timing + == planner_types::ir::properties::ExecutionTiming::QueryTime; + if !query_time { + crate::capability::validate_ingestion_time_summary(filter.as_ref()) + .map_err(Error::Invalid)?; } - crate::capability::validate_ingestion_time_summary(filter.as_ref()) - .map_err(Error::Invalid)?; let [input] = schemas.as_slice() else { return Err(invalid("a tumbling-window chain reads one input")); }; @@ -475,8 +477,37 @@ fn compile_internal( })) => Some(named_column(input, &ColumnRef::SampleValue)?), _ => None, }; - let window = - Operator::time_range(input.clone(), time, 0, width_ms, sample_value, None)?; + let window = if query_time { + // The windows its readers read, whole tumbling windows. + let (end_offset_ms, read_width_ms) = + window_read_above(id, &compile_roots, &consumers, &nodes, &dependencies) + .ok_or_else(|| { + invalid(format!( + "node {id}: a query-time tumbling-window chain is read \ + outside the time ranges above it" + )) + })?; + 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)? + } 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); let build = match reduction { PlannerReduction::PerEntity => { let SummaryInputExpr::Column(value) = &update.weight else { @@ -486,7 +517,9 @@ fn compile_internal( }; let value = named_column(input, value)?; let groups = (0..input.fields.len()) - .filter(|&column| column != value && column != time) + .filter(|&column| { + column != value && (column != time || window_end.is_some()) + }) .collect(); Operator::per_group_summary_build( input.clone(), @@ -503,6 +536,7 @@ fn compile_internal( node.parameters.as_ref(), update, reduction, + window_end, )?, }; let build = match filter { @@ -510,12 +544,22 @@ fn compile_internal( 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()?; physical_dag.add(auxiliary, inputs, window)?; physical_dag.add(helper_id(id, 1)?, vec![auxiliary], build)?; physical_dag.add( id, vec![helper_id(id, 1)?], - Operator::scope_timestamp(compact, output)?, + Operator::window_end_timestamp(compact, output, window_end)?, )?; continue; } @@ -914,11 +958,17 @@ fn window_union(a: (i64, i64), b: (i64, i64)) -> (i64, i64) { /// The window `(end_offset_ms, width_ms)` back from the evaluation time that /// the compiled consumers of `id` read through the `TimeShift`s (without an -/// `@` anchor) above it: a `TimeRange(w)`, or a chain of width `w` (its -/// window ending at the evaluation time), over shifts summing to `s` reads -/// `(s, w)`; several read the smallest window holding all of theirs. +/// `@` 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 it reads what they read: the walk passes up through it. /// `None` when `id` is a compile root, a root or another consumer reads it /// outside a window, or nothing reads it. +/// +/// 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 +/// `(0, 6m)`. fn window_read_above( id: NodeId, roots: &BTreeSet, @@ -941,6 +991,13 @@ fn window_read_above( Payload::NonASAP(NonASAPOp::TimeRange { .. }) => { time_range_window(consumer, nodes, dependencies)? } + Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) + if nodes[&consumer].output_state.timing + == planner_types::ir::properties::ExecutionTiming::QueryTime => + { + pending.push((consumer, end_offset_ms)); + continue; + } Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { tumbling_window_width_ms, .. @@ -1442,8 +1499,14 @@ fn bind_operation(node: &PhysicalASAPDAGNode, inputs: &[SchemaRef]) -> Result build.with_row_filter(expression(&filter.0, input)?), None => Ok(build), @@ -1506,14 +1569,21 @@ fn bind_operation(node: &PhysicalASAPDAGNode, inputs: &[SchemaRef]) -> Result, update: &planner_types::summary_type::SummaryUpdate, reduction: &PlannerReduction, + window_end: Option, ) -> Result { + let groups = |input: &SchemaRef, keys: &GroupKeys| -> Result, Error> { + let mut columns = groups(input, keys)?; + columns.extend(window_end); + Ok(columns) + }; if matches!( family, FieldDataType::Sketch(planner_types::summary_type::SketchAlgorithm::HydraCms) diff --git a/crates/executor/tests/tumbling_window_chain.rs b/crates/executor/tests/tumbling_window_chain.rs index 6745fe07e..daa1e1662 100644 --- a/crates/executor/tests/tumbling_window_chain.rs +++ b/crates/executor/tests/tumbling_window_chain.rs @@ -1,7 +1,8 @@ //! A tumbling-window chain runs natively: each ingestion run builds the //! chain's window ending at the run's end, the runtime appends it to its //! time-indexed store, and a query run merges the windows the `TimeRange` -//! (and `TimeShift`) above the chain select. And a `TimeShift` with an `@` +//! (and `TimeShift`) above the chain select; a chain built at query time +//! builds those windows in the query run. And a `TimeShift` with an `@` //! anchor evaluates its input at the anchor's time. use asap_executor::physical_planner::{compile, test_support::views_at}; use asap_executor::runtime::{Limits, RunContext, Scope}; @@ -304,19 +305,124 @@ fn a_chain_built_at_ingestion_answers_queries_through_its_time_ranges() { assert!(error.is_some_and(|error| error.to_string().contains("no rows tiling"))); } -// A query run never builds a chain: it is built one window per ingestion -// run. +// A chain built at query time, of 1-minute windows over the raw samples, +// read by `sum_over_time(latency[5m])` and `sum_over_time(latency[5m] +// offset 1m)`: at T = 4h the run builds, from the `Scan`'s rows in `(T - +// 6m, T]`, one state per job and window `(T - (k+1)m, T - km]` stamped with +// its end, and the `TimeRange`s merge the windows of `(T - 5m, T]` and +// `(T - 6m, T - 1m]`, equal to the sums computed directly over the raw +// samples (one sample per minute, on the windows' ends). #[test] -fn a_chain_is_built_only_at_ingestion() { - let (mut dag, chain, _) = chain_plan(); - // At query time the chain is not materialized, so it has no retention. - dag.nodes - .iter_mut() - .find(|node| node.id == chain) +fn a_chain_built_at_query_time_answers_queries_through_its_time_ranges() { + 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 merge = |windows| { + node(Operator::ASAP(ASAPOp::SummaryMerge { + children: vec![windows], + group_by: None, + })) + }; + let now = finalized_sum(merge(range(5 * MINUTE_MS, chain.clone()))); + let shifted = finalized_sum(merge(range( + 5 * MINUTE_MS, + shift(MINUTE_MS, None, chain.clone()), + ))); + let (dag, nodes) = + planner_types::test_support::forwarders::physical_asap_dag_at_query_time_with_node_ids(&[ + QueryRoot::Operator(now.clone()), + QueryRoot::Operator(shifted.clone()), + ]) + .unwrap(); + let chain_node = dag + .nodes + .iter() + .find(|node| node.id == id_of(&nodes, &chain)) + .unwrap(); + assert_eq!( + chain_node.output_state.timing, + planner_types::ir::properties::ExecutionTiming::QueryTime + ); + let roots = [id_of(&nodes, &now) as u64, id_of(&nodes, &shifted) as u64]; + let query = compile(&dag, &[], &roots).unwrap(); + let evaluation_time_ms = 4 * HOUR_MS; + let context = RunContext::new(Scope::Query { evaluation_time_ms }, Limits::default()).unwrap(); + let answers = { + let query = query.instantiate(Arc::new(store_with_samples())).unwrap(); + run(&query, &roots, context) + }; + assert_eq!( + sums_of(answers[0].clone()), + direct_sums(evaluation_time_ms - 5 * MINUTE_MS, evaluation_time_ms) + ); + assert_eq!( + sums_of(answers[1].clone()), + direct_sums( + evaluation_time_ms - 6 * MINUTE_MS, + evaluation_time_ms - MINUTE_MS + ) + ); +} + +// 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. +#[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(), + ) + .unwrap(); + let window = ExecutorOperator::time_range(schema, 1, 0, 3 * MINUTE_MS, None, None) .unwrap() - .retention_ms = None; - assert!(compile(&dag, &[], &[chain as u64]) - .is_err_and(|error| error.to_string().contains("built in ingestion runs"))); + .with_tumbling_window_ends(MINUTE_MS) + .unwrap(); + dag.add(1, vec![0], window).unwrap(); + let context = RunContext::new( + Scope::Query { + evaluation_time_ms: 10 * MINUTE_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)); } // `sum_over_time(latency[5m] @ end())` in a range request over (1h, 3h]: diff --git a/crates/planner/src/installable/tests.rs b/crates/planner/src/installable/tests.rs index 84b74a4b5..78612506e 100644 --- a/crates/planner/src/installable/tests.rs +++ b/crates/planner/src/installable/tests.rs @@ -201,7 +201,8 @@ fn installation_checks_the_retention_stage3_set() { /// A query-time `Scan` read through a window carries its retention; /// one without is refused, and one with it is the raw data kept for its -/// source. +/// source. Read through the chain built at query time, it is read as far +/// back as the chain's `TimeRange(1h)` reads, not one 10-minute window. #[test] fn a_query_time_scan_carries_its_retention() { let (mut dag, chain) = chain_plan(Some(3_600_000)); @@ -225,9 +226,18 @@ fn a_query_time_scan_carries_its_retention() { .find(|n| n.id == scan) .unwrap() .retention_ms = Some(600_000); + assert!(matches!( + dag.raw_data_retention(), + Err(InstallableDagError::ScanRetentionTooShort { .. }) + )); + dag.nodes + .iter_mut() + .find(|n| n.id == scan) + .unwrap() + .retention_ms = Some(3_600_000); let retention = dag.raw_data_retention().unwrap(); assert_eq!(retention.len(), 1); - assert_eq!(retention[0].lookback_ms(), Some(600_000)); + assert_eq!(retention[0].lookback_ms(), Some(3_600_000)); } /// Unrestricted summaries become the executor's families; a caller's diff --git a/crates/planner/tests/runtime_install.rs b/crates/planner/tests/runtime_install.rs index 2fdff66df..38806d5e0 100644 --- a/crates/planner/tests/runtime_install.rs +++ b/crates/planner/tests/runtime_install.rs @@ -402,3 +402,208 @@ async fn check_compiles_reports_the_executors_error() { // ── Classic-histogram `histogram_quantile` (issue #890) ───────────────────── // ── PromQL absent() (issue #895) ───────────────────────────────────────────── + +// ── Tumbling-window chains built at query time ─────────────────────────────── + +/// Repeating PromQL `queries`, each `(query, requirements)` every minute at +/// phase 0, over 1,000 ingesting gauge series `m` sampled every second; +/// `requirements` is the JSON inside the entry's `requirements` object +/// (empty for none). +fn every_minute_workload(queries: &[(&str, &str)]) -> PlanningWorkload { + let entries: Vec = queries + .iter() + .map(|(query, requirements)| { + format!( + r#"{{"query": {query:?}, "demand": {{"fixed_interval_at": {{"interval": 60000, "evaluation_phase": 0}}}}, + "requirements": {{{requirements}}}, "predictability": {{"predictable": {{"known_at": null}}}}}}"# + ) + }) + .collect(); + serde_json::from_str(&format!( + r#"{{"query_workload": {{"language": "promql", "repeating_queries": [{}]}}, + "data_workload": {{"arrival": {{"ingesting": {{"rows_per_second": 1000.0}}}}, + "sources": {{"m": {{"series_count": 1000, "metric_type": "gauge", "sample_interval": 1000}}}}}}}}"#, + entries.join(",") + )) + .unwrap() +} + +/// The built-in plan for `queries` ([`every_minute_workload`]), on a +/// deployment that keeps raw data. +async fn plan_every_minute(queries: &[(&str, &str)]) -> InstallablePlan { + let workload = every_minute_workload(queries); + let planned = plan_installable(UserInput::new( + &workload, + FrontendInput::Promql, + PlanningModels::builtin(), + )) + .await; + assert!(planned.is_ok(), "{:?}", planned.err()); + planned.unwrap().0 +} + +/// One sample of `m` for series `(instance, job)`. +fn m_sample(instance: &str, job: &str, timestamp_ms: i64, value: f64) -> RawDataSample { + RawDataSample { + labels: BTreeMap::from([ + ("__name__".to_string(), "m".to_string()), + ("instance".to_string(), instance.to_string()), + ("job".to_string(), job.to_string()), + ]), + timestamp_ms, + value, + } +} + +/// Samples of `m` for four series (instances `a`..`d`, jobs `x` and `y`) +/// every 15 s from 8 minutes before [`EVALUATION_MS`] through it, so some +/// lie exactly on the minute boundaries that tumbling windows end at. +fn m_samples() -> Vec { + let series = [("a", "x"), ("b", "x"), ("c", "y"), ("d", "y")]; + let mut samples = Vec::new(); + for (index, (instance, job)) in series.iter().enumerate() { + for step in 0..=32i64 { + let timestamp_ms = EVALUATION_MS - 480_000 + step * 15_000; + let value = (index as f64 + 1.0) * 10.0 + step as f64; + samples.push(m_sample(instance, job, timestamp_ms, value)); + } + } + samples +} + +/// Per `labels(sample)`, the sum of `value(sample)` over the samples in +/// `(T - end_offset_ms - width_ms, T - end_offset_ms]` at `T` = +/// [`EVALUATION_MS`]: the exact answer of a range function over that +/// window. +fn exact_over_window( + samples: &[RawDataSample], + end_offset_ms: i64, + width_ms: i64, + labels: impl Fn(&RawDataSample) -> BTreeMap, + value: impl Fn(&RawDataSample) -> f64, +) -> Vec { + let end = EVALUATION_MS - end_offset_ms; + let mut sums = BTreeMap::, f64>::new(); + for sample in samples { + if end - width_ms < sample.timestamp_ms && sample.timestamp_ms <= end { + *sums.entry(labels(sample)).or_default() += value(sample); + } + } + sums.into_iter().collect() +} + +/// A sample's labels without its metric name, as range functions output. +fn labels_without_name(sample: &RawDataSample) -> BTreeMap { + let mut labels = sample.labels.clone(); + labels.remove("__name__"); + labels +} + +/// Each query root's answers of `plan` at [`EVALUATION_MS`] over `samples`. +fn query_answers(plan: &InstallablePlan, samples: &[RawDataSample]) -> Vec> { + let query = Scope::Query { + evaluation_time_ms: EVALUATION_MS, + }; + run(&plan.dag, samples, &query_roots(plan), query) + .iter() + .map(|batches| answers(batches)) + .collect() +} + +/// Whether `answered` matches `expected`, label set for label set, each +/// value within `tolerance`. +fn assert_answers_near(answered: &[Answer], expected: &[Answer], tolerance: f64) { + assert_eq!( + answered.len(), + expected.len(), + "{answered:?} vs {expected:?}" + ); + for (answer, expected) in answered.iter().zip(expected) { + assert_eq!(answer.0, expected.0, "{answered:?} vs {expected:?}"); + assert!( + (answer.1 - expected.1).abs() <= tolerance, + "{answered:?} vs {expected:?}" + ); + } +} + +/// `sum_over_time(m[5m] offset 1m)` and `sum_over_time(m[5m])`, every +/// minute, may share one tumbling-window chain of `m` built at query time: +/// the plan installs, a query-time `Scan` read through such a chain keeps +/// the 6 minutes its readers read back (5 m + 1 m offset, not one +/// window), and both queries answer the exact sums of their windows. +#[tokio::test] +async fn shifted_and_unshifted_sums_over_a_shared_query_time_chain_install_and_answer_exactly() { + let plan = plan_every_minute(&[ + ("sum_over_time(m[5m] offset 1m)", ""), + ("sum_over_time(m[5m])", ""), + ]) + .await; + let query_time_chain = plan.dag.nodes.iter().any(|node| { + matches!( + node.payload, + asap_types::ir::Operator::ASAP( + asap_types::ir::ASAPOp::SummaryTumblingWindowChain { .. } + ) + ) && node.output_state.timing == asap_planner::ExecutionTiming::QueryTime + }); + if query_time_chain { + for node in &plan.dag.nodes { + if matches!( + node.payload, + asap_types::ir::Operator::NonASAP(asap_types::ir::NonASAPOp::Scan { .. }) + ) && node.output_state.timing == asap_planner::ExecutionTiming::QueryTime + { + assert!( + node.retention_ms.is_some_and(|kept_ms| kept_ms >= 360_000), + "scan {} keeps {:?}", + node.id, + node.retention_ms + ); + } + } + } + let samples = m_samples(); + let answered = query_answers(&plan, &samples); + let value = |sample: &RawDataSample| sample.value; + assert_answers_near( + &answered[0], + &exact_over_window(&samples, 60_000, 300_000, labels_without_name, value), + 1e-9, + ); + assert_answers_near( + &answered[1], + &exact_over_window(&samples, 0, 300_000, labels_without_name, value), + 1e-9, + ); +} + +/// `sum by (job) (count_over_time(m[1m]))` and `count_over_time(m[1m])`, +/// every minute within absolute error 0.05, may share one tumbling-window +/// chain of `m` built at query time: the plan installs and both queries +/// answer their counts within 0.05. +#[tokio::test] +async fn grouped_and_per_series_counts_over_a_shared_query_time_chain_install_and_answer() { + let accuracy = r#""accuracy": {"max_error": 0.05, "accuracy_metric": "absolute"}"#; + let plan = plan_every_minute(&[ + ("sum by (job) (count_over_time(m[1m]))", accuracy), + ("count_over_time(m[1m])", accuracy), + ]) + .await; + let samples = m_samples(); + let answered = query_answers(&plan, &samples); + let job = |sample: &RawDataSample| { + BTreeMap::from([("job".to_string(), sample.labels["job"].clone())]) + }; + let one = |_: &RawDataSample| 1.0; + assert_answers_near( + &answered[0], + &exact_over_window(&samples, 0, 60_000, job, one), + 0.05, + ); + assert_answers_near( + &answered[1], + &exact_over_window(&samples, 0, 60_000, labels_without_name, one), + 0.05, + ); +} diff --git a/crates/stage3-plan-selection/src/cost.rs b/crates/stage3-plan-selection/src/cost.rs index 2778282b3..91a4a0b6b 100644 --- a/crates/stage3-plan-selection/src/cost.rs +++ b/crates/stage3-plan-selection/src/cost.rs @@ -178,6 +178,12 @@ pub(crate) fn read_back_ms( operator_of, |v| readers(consumers_of(v)), span_ms, + |v| { + physical.physical_candidates[v.0] + .physical_properties + .timing() + == ExecutionTiming::QueryTime + }, matches!( operator_of(producer), Operator::NonASAP(NonASAPOp::Scan { .. }) diff --git a/crates/types/src/plan/materialized_view.rs b/crates/types/src/plan/materialized_view.rs index e495eb040..3f0dae209 100644 --- a/crates/types/src/plan/materialized_view.rs +++ b/crates/types/src/plan/materialized_view.rs @@ -795,10 +795,13 @@ impl<'a> ViewIndex<'a> { /// How far back from the evaluation time the consumers of `node` read /// it: through `TimeShift`s (without an `@` anchor) summing to `o`, a - /// `TimeRange(r)` reads `o + r` back, a chain of width `w` `o + w`, and - /// any other consumer `o`; the most of these, and whether a consumer - /// other than a `TimeRange` or chain (or a root) reads it, outside a - /// window. + /// `TimeRange(r)` reads `o + r` back, a chain of width `w` built at + /// ingestion time `o + w`, and any other consumer `o`; the most of + /// these, and whether a consumer other than a `TimeRange` or chain (or + /// a root) reads it, outside a window. A chain built at query time + /// builds the windows its own readers read, so it reads `node` as far + /// back as they read it: the walk passes up through it like a shift of + /// 0. fn read_back_ms(&self, node: PhysicalASAPNodeId) -> (u64, bool) { let mut read_back_ms = 0u64; let mut read_outside_windows = false; @@ -816,6 +819,13 @@ impl<'a> ViewIndex<'a> { Operator::NonASAP(NonASAPOp::TimeRange { range, .. }) => { u64::try_from(range.as_millis()).unwrap_or(u64::MAX) } + Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) + if self.nodes[&consumer].output_state.timing + == ExecutionTiming::QueryTime => + { + pending.push((consumer, offset_ms)); + continue; + } Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { tumbling_window_width_ms, .. diff --git a/docs/design_docs/physical-planning-and-deployment.md b/docs/design_docs/physical-planning-and-deployment.md index c7518fdb2..25f9580fc 100644 --- a/docs/design_docs/physical-planning-and-deployment.md +++ b/docs/design_docs/physical-planning-and-deployment.md @@ -489,7 +489,9 @@ installation checks it: Each covers what the `TimeRange` and `TimeShift` nodes above the node read back: through shifts summing to `o`, a `TimeRange(r)` reads `o + r` back (Stage 3 adds the longest range-evaluation span of the queries reading it). -`raw_data_retention()` is the per-source maximum over the query-time scans. +A chain built at ingestion time reads its newest window back; a chain built +at query time reads what its own readers read, so the walk passes up through +it. `raw_data_retention()` is the per-source maximum over the query-time scans. Example: for `max_over_time(m[5m])` evaluated over the last hour, raw `m` is kept 65 minutes. @@ -502,7 +504,11 @@ dropped. A chain runs natively: each ingestion run builds its window ending at the run's end (one row per key, `(window_end, keys, state)`), the runtime appends it, and a query reads the windows `TimeRange(W)` over shifts summing to `o` selects, `(T - o - W, T - o]` on `window_end`, which a `SummaryMerge` -merges per key. A `TimeShift { at }` evaluates its input once per request, at +merges per key. A chain built at query time builds, in the query run at +`T`, every window `(T - (k+1)c, T - kc]` its readers select, in one pass over +its input: each row is grouped with its key by its window's end, and each +state is stamped with that end, so the same `TimeRange` selects them. A +`TimeShift { at }` evaluates its input once per request, at the request's end (`@ end()`), start (`@ start()`) or the given time. ### Input slots and compilation From d665fb803b26336c427568eab40ad8ca653f7cb4 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:02:07 +0000 Subject: [PATCH 08/31] refactor(types,cost,stage3,executor): one time-window walk and `TimeWindow` for every read-back and window computation (#1207 PR 6) `asap_types::plan::time_window` holds the one walk each copy re-implemented: - `TimeWindow { end_offset_ms, width_ms }` (a window relative to an evaluation time t: (t - end_offset_ms - width_ms, t - end_offset_ms]), with `read_back_ms`, `union` and `bounds_ms`; - `TimeIndexedRead::of(operator, timing)`: how a reader reads a time-indexed producer (through a shift or a query-time chain, a window, at the evaluation time, or whole); - `windows_read_above` (up from a producer through its readers), `windows_below` and `end_offset_below` (down from a node), generic over the node type through closures. Callers: asap-cost `read_back_ms` (now `producer, readers_of, read_of, span_ms, reads_raw_data`), Stage 3 (`retention_need_ms`, `scan_read_ms` replace `read_back_ms(.., with_span)`; the walk reads each candidate's readers without cloning a Vec per step), `ViewIndex::read_back_ms`/ `windows_below` (the installer's checks) and the executor's `time_range_window`, `window_read_above`, `window_below` (`window_union` deleted). The executor's slots carry `TimeWindow`; its `window_bounds_ms` duplicate is deleted. Deleted (no non-test reader): `RawDataWindow`, `MaterializedViewInputs:: raw_data_windows` and its re-export; the tests read a view's windows from `PhysicalASAPDAG::refresh_window`. `RefreshWindow::of_window` takes a `TimeWindow`; `IngestionBuildReadsWindowsOfDifferentOffsets::end_offsets_ms` is `Vec`. A negative offset below a window is now read unbounded everywhere (the installer already refused it at ingestion). --- crates/cost/src/node/usage/tests.rs | 11 +- crates/cost/src/plan/cost.rs | 9 +- crates/cost/src/plan/read_back.rs | 95 ++--- .../executor/src/physical_planner/compiled.rs | 18 +- crates/executor/src/physical_planner/mod.rs | 179 ++++----- crates/executor/src/sources/mod.rs | 64 ++-- .../executor/tests/tumbling_window_chain.rs | 7 +- .../tests/materialized_views.rs | 37 +- crates/planner/src/lib.rs | 3 +- crates/planner/tests/columnar_batches.rs | 18 +- crates/planner/tests/sql_table_scans.rs | 19 +- crates/stage3-plan-selection/src/cost.rs | 106 +++--- crates/stage3-plan-selection/src/plan.rs | 13 +- crates/types/src/plan/materialized_view.rs | 340 ++++++------------ crates/types/src/plan/mod.rs | 5 +- crates/types/src/plan/time_window.rs | 312 ++++++++++++++++ crates/types/tests/physical_asap_dag.rs | 13 +- 17 files changed, 675 insertions(+), 574 deletions(-) create mode 100644 crates/types/src/plan/time_window.rs diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index b013064da..1e3005fe4 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -5,6 +5,7 @@ use std::time::Duration; use asap_types::ir::properties::ExecutionDataState; use asap_types::ir::scalar::ColumnRef; use asap_types::plan::physical_asap_dag::PhysicalASAPDAGNode; +use asap_types::plan::time_window::TimeIndexedRead; use asap_types::summary_type::{ExactKind, ExactParams, SummaryUpdate}; use super::*; @@ -196,11 +197,13 @@ fn a_query_time_scan_reads_what_its_consumers_read_and_keeps_its_retention() { child: 0, })); let read_ms = read_back_ms( - [1], - |_| &instant.payload, - |_| vec![], + 0, + |id| match id { + 0 => vec![1], + _ => vec![], + }, + |_| TimeIndexedRead::of(&instant.payload, ExecutionTiming::QueryTime), |_| 0, - |_| false, true, ); assert_eq!(read_ms, Some(1_000)); diff --git a/crates/cost/src/plan/cost.rs b/crates/cost/src/plan/cost.rs index 233e7ddfd..47fe22af7 100644 --- a/crates/cost/src/plan/cost.rs +++ b/crates/cost/src/plan/cost.rs @@ -25,6 +25,7 @@ use asap_types::plan::physical_asap_dag::{ PhysicalASAPDAG, PhysicalASAPDAGNode, PhysicalASAPNodeId, PhysicalASAPOperatorPayload as Payload, }; +use asap_types::plan::time_window::TimeIndexedRead; use asap_types::summary_type::SketchParams; use asap_types::workload::{DataWorkload, QueryRecurrence, QueryWorkloadEntry, RangeEvaluation}; @@ -466,13 +467,11 @@ fn scan_read_ms( if !matches!(node.payload, Payload::NonASAP(NonASAPOp::Scan { .. })) { return None; } - let readers = |id: PhysicalASAPNodeId| consumers_of.get(&id).cloned().unwrap_or_default(); read_back_ms( - readers(node.id), - |id| &nodes[&id].payload, - readers, + node.id, + |id| consumers_of.get(&id).into_iter().flatten().copied(), + |id| TimeIndexedRead::of(&nodes[&id].payload, nodes[&id].output_state.timing), |_| 0, - |id| nodes[&id].output_state.timing == ExecutionTiming::QueryTime, true, ) } diff --git a/crates/cost/src/plan/read_back.rs b/crates/cost/src/plan/read_back.rs index e8e3554d0..f294ac753 100644 --- a/crates/cost/src/plan/read_back.rs +++ b/crates/cost/src/plan/read_back.rs @@ -1,83 +1,36 @@ -//! How far back from the evaluation time the consumers of a time-indexed +//! How far back from the evaluation time the readers of a time-indexed //! producer (raw data, a chain, results stored per evaluation time) read its -//! rows: one walk, shared by Stage 3 (over candidates) and [`plan_cost`] -//! (over plan nodes) for retention and for a query-time `Scan`'s read -//! window (concepts doc, section 5, "Cost and retention"). +//! rows, over the shared walk [`asap_types::plan::time_window::windows_read_above`]: +//! used by Stage 3 (over candidates) and [`plan_cost`] (over plan nodes) for +//! retention and for a query-time `Scan`'s read per execution +//! (`docs/design_docs/concepts/memo-groups-properties-requirements.md`, +//! section 5, "Cost and retention"). //! //! [`plan_cost`]: crate::plan_cost -use std::collections::BTreeSet; +use asap_types::plan::time_window::{windows_read_above, TimeIndexedRead}; -use asap_types::ir::{ASAPOp, NonASAPOp, Operator}; - -/// Over the paths from `consumers` (each a reader of the producer) up -/// through `TimeShift`s without `@` (`consumers_of` gives a node's own -/// readers), the most of the shifts' offsets plus the `TimeRange`'s range -/// plus `span_ms` of that `TimeRange` (a range query's span, for retention; -/// 0 for one execution's read). A tumbling-window chain built at ingestion -/// time reads its newest window back. A chain built at query time -/// (`query_time_chain`) builds, at each evaluation, the windows its own -/// readers read: the walk passes up through it as through a shift of 0. -/// Any other reader reads at the evaluation time (its `span_ms`), except -/// over raw data (`reads_raw_data`), which it reads whole: `None`, -/// unbounded, as under a `TimeShift` anchored with `@`. +/// The most any reader of `producer` reads back from the evaluation time +/// (`readers_of` gives a node's readers, `read_of` how a reader reads, +/// [`TimeIndexedRead::of`]): each window's `read_back_ms` plus the +/// `span_ms` of the reader at its end (a range query's span, for +/// retention; 0 for one execution's read). `None`, unbounded, when one +/// reads it whole (an `@` anchor, or a reader at the evaluation time over +/// raw data, `reads_raw_data`). 0 when nothing reads it. /// /// Example: `sum_over_time(m[5m] offset 1h)` reads `m` back 1h + 5m, /// directly or through a query-time chain of 1-minute windows; through an /// ingestion-time chain of 1-minute windows, `m` is read back 1 minute. -pub fn read_back_ms<'a, N: Copy + Ord, C: 'a>( - consumers: impl IntoIterator, - operator_of: impl Fn(N) -> &'a Operator, - consumers_of: impl Fn(N) -> Vec, - span_ms: impl Fn(N) -> i64, - query_time_chain: impl Fn(N) -> bool, +pub fn read_back_ms>( + producer: N, + readers_of: impl Fn(N) -> R, + read_of: impl Fn(N) -> TimeIndexedRead, + span_ms: impl Fn(N) -> u64, reads_raw_data: bool, ) -> Option { - let mut need_ms: i64 = 0; - // (reader, offset of the shifts below it), walked up through shifts. - let mut pending: Vec<(N, i64)> = consumers.into_iter().map(|c| (c, 0)).collect(); - let mut seen = BTreeSet::new(); - while let Some((consumer, offset_ms)) = pending.pop() { - if !seen.insert((consumer, offset_ms)) { - continue; - } - let read_ms = match operator_of(consumer) { - Operator::NonASAP(NonASAPOp::TimeRange { range, .. }) => { - let range_ms = i64::try_from(range.as_millis()).ok()?; - offset_ms - .checked_add(range_ms)? - .checked_add(span_ms(consumer))? - } - Operator::NonASAP(NonASAPOp::TimeShift { shift, .. }) if shift.at.is_none() => { - let offset_ms = offset_ms.checked_add(shift.offset_ms)?; - pending.extend( - consumers_of(consumer) - .into_iter() - .map(|reader| (reader, offset_ms)), - ); - continue; - } - Operator::NonASAP(NonASAPOp::TimeShift { .. }) => return None, - // A query-time chain reads what its readers read. - Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) - if query_time_chain(consumer) => - { - pending.extend( - consumers_of(consumer) - .into_iter() - .map(|reader| (reader, offset_ms)), - ); - continue; - } - // An ingestion-time chain builds its newest window from it. - Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { - tumbling_window_width_ms, - .. - }) => offset_ms.checked_add(i64::try_from(*tumbling_window_width_ms).ok()?)?, - _ if reads_raw_data => return None, - _ => offset_ms.checked_add(span_ms(consumer))?, - }; - need_ms = need_ms.max(read_ms); - } - u64::try_from(need_ms).ok() + windows_read_above(producer, readers_of, read_of, reads_raw_data)? + .into_iter() + .try_fold(0_u64, |most, (reader, window)| { + Some(most.max(window.read_back_ms().checked_add(span_ms(reader))?)) + }) } diff --git a/crates/executor/src/physical_planner/compiled.rs b/crates/executor/src/physical_planner/compiled.rs index 94e4c2fa5..7d074d659 100644 --- a/crates/executor/src/physical_planner/compiled.rs +++ b/crates/executor/src/physical_planner/compiled.rs @@ -24,10 +24,10 @@ pub(super) struct MaterializedViewSlot { pub(super) view: MaterializedView, /// The view node's schema. pub(super) schema: SchemaRef, - /// The rows a read selects, `(end_offset_ms, width_ms)` back from the - /// evaluation time: for a chain, the windows the `TimeRange`s above it - /// select; for any other view, the data its state summarizes. - pub(super) read_window_ms: (i64, i64), + /// The rows a read selects, back from the evaluation time: for a + /// chain, the windows the `TimeRange`s above it select; for any other + /// view, the data its state summarizes. + pub(super) read_window: TimeWindow, } /// A `Scan` leaf, computed once at compile time: what [`CompiledPhysicalDAG::instantiate`] @@ -39,9 +39,9 @@ pub(super) struct RawDataSlot { pub(super) schema: SchemaRef, /// The `Scan`'s predicates, compiled over `schema`. pub(super) predicates: Vec, - /// The rows its consumers read, `(end_offset_ms, width_ms)` back from - /// the evaluation time; `None` when one reads every row. - pub(super) read_window_ms: Option<(i64, i64)>, + /// The rows its consumers read, back from the evaluation time; `None` + /// when one reads every row. + pub(super) read_window: Option, } /// Selected native operators and input slots. Rebinding never repeats lowering. @@ -186,7 +186,7 @@ impl CompiledPhysicalDAG { store.clone(), slot.view.clone(), slot.schema.clone(), - slot.read_window_ms, + slot.read_window, ); dag.add(id, vec![], scan)?; } @@ -196,7 +196,7 @@ impl CompiledPhysicalDAG { slot.source.clone(), slot.schema.clone(), slot.predicates.clone(), - slot.read_window_ms, + slot.read_window, ); dag.add(id, vec![], scan)?; } diff --git a/crates/executor/src/physical_planner/mod.rs b/crates/executor/src/physical_planner/mod.rs index deedcb94a..fff5384f7 100644 --- a/crates/executor/src/physical_planner/mod.rs +++ b/crates/executor/src/physical_planner/mod.rs @@ -20,6 +20,8 @@ use planner_types::plan::physical_asap_dag::{ PhysicalASAPDAG, PhysicalASAPDAGNode, PhysicalASAPNodeId, PhysicalASAPOperatorPayload as Payload, }; +use planner_types::plan::time_window::{self, TimeIndexedRead}; +use planner_types::plan::TimeWindow; use planner_types::summary_type::{ SketchParams, SketchStatistic, SummaryInputExpr, SummaryTypeCapability, }; @@ -170,9 +172,9 @@ fn compile_internal( // A chain's rows are read through the `TimeRange`s above it, // which select its windows; any other view's state is read for // the window of data it summarizes. Either is exact, so bounded. - let read_window_ms = match node.payload { + let read_window = match node.payload { Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) => { - window_read_above(id, &compile_roots, &consumers, &nodes, &dependencies) + window_read_above(id, &compile_roots, &consumers, &nodes) } _ => window_below(id, &nodes, &dependencies), } @@ -182,7 +184,7 @@ fn compile_internal( compiled::MaterializedViewSlot { view: (*view).clone(), schema: output, - read_window_ms, + read_window, }, )?; } else if let Payload::NonASAP(NonASAPOp::Scan { @@ -208,16 +210,16 @@ fn compile_internal( .collect::, _>>() .map_err(|error| invalid(format!("scan {id}: {error}")))?; // The rows its consumers read; a compile root reads them all. - let read_window_ms = output.time_index.and_then(|_| { - window_read_above(id, &compile_roots, &consumers, &nodes, &dependencies) - }); + let read_window = output + .time_index + .and_then(|_| window_read_above(id, &compile_roots, &consumers, &nodes)); physical_dag.add_raw_data( id, compiled::RawDataSlot { source: source.clone(), schema: output, predicates, - read_window_ms, + read_window, }, )?; } else { @@ -479,14 +481,18 @@ fn compile_internal( }; let window = if query_time { // The windows its readers read, whole tumbling windows. + let read = window_read_above(id, &compile_roots, &consumers, &nodes) + .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) = - window_read_above(id, &compile_roots, &consumers, &nodes, &dependencies) - .ok_or_else(|| { - invalid(format!( - "node {id}: a query-time tumbling-window chain is read \ - outside the time ranges above it" - )) - })?; + (millis(read.end_offset_ms)?, millis(read.width_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 \ @@ -867,9 +873,11 @@ fn time_window_operator( .collect(); return Operator::project(input.clone(), columns).map(Some); }; - let Some((end_offset_ms, width_ms)) = time_range_window(id, nodes, dependencies) else { + let Some(window) = time_range_window(id, nodes, dependencies) else { return Ok(None); }; + let millis = |ms: u64| i64::try_from(ms).map_err(|_| invalid("a time window overflows Int64")); + let (end_offset_ms, width_ms) = (millis(window.end_offset_ms)?, millis(window.width_ms)?); let time = input .time_index .ok_or_else(|| invalid("a time range needs its input's time column"))?; @@ -923,120 +931,81 @@ fn time_window_operator( .map(Some) } -/// `(s, w)` of the `TimeRange(w)` `id` over `TimeShift`s summing to `s`: it -/// reads `(T - s - w, T - s]` at evaluation time `T`. `None` when `id` is not -/// a `TimeRange` or a shift below it has an `@` anchor. +/// The window the `TimeRange` `id` reads, its range over the `TimeShift`s +/// right below it ([`time_window::end_offset_below`]): `(T - s - w, T - s]` +/// at evaluation time `T`. `None` when `id` is not a `TimeRange`, or a +/// shift below it has an `@` anchor or the offsets are negative. fn time_range_window( id: NodeId, nodes: &BTreeMap, dependencies: &BTreeMap>, -) -> Option<(i64, i64)> { +) -> Option { let Payload::NonASAP(NonASAPOp::TimeRange { range, .. }) = &nodes.get(&id)?.payload else { return None; }; - let mut end_offset_ms = 0i64; - let mut below = first_input(id, dependencies); - while let Some(Payload::NonASAP(NonASAPOp::TimeShift { shift, .. })) = below - .and_then(|below| nodes.get(&below)) - .map(|node| &node.payload) - { - if shift.at.is_some() { - return None; - } - end_offset_ms = end_offset_ms.saturating_add(shift.offset_ms); - below = below.and_then(|below| first_input(below, dependencies)); - } - Some((end_offset_ms, i64::try_from(range.as_millis()).ok()?)) + Some(TimeWindow { + end_offset_ms: time_window::end_offset_below( + id, + |node| first_input(node, dependencies), + |node| &nodes[&node].payload, + )?, + width_ms: u64::try_from(range.as_millis()).ok()?, + }) } -/// The smallest window `(end_offset_ms, width_ms)` holding both. -fn window_union(a: (i64, i64), b: (i64, i64)) -> (i64, i64) { - let end_offset_ms = a.0.min(b.0); - let start_back_ms = a.0.saturating_add(a.1).max(b.0.saturating_add(b.1)); - (end_offset_ms, start_back_ms.saturating_sub(end_offset_ms)) -} - -/// The window `(end_offset_ms, width_ms)` back from the evaluation time that -/// the compiled consumers of `id` read 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 it reads what they read: the walk passes up through it. -/// `None` when `id` is a compile root, a root or another consumer reads it -/// outside a window, or nothing reads it. +/// The window back from the evaluation time that the compiled consumers of +/// `id` read ([`time_window::windows_read_above`]): 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. /// /// 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 -/// `(0, 6m)`. +/// `{0, 6m}`. fn window_read_above( id: NodeId, roots: &BTreeSet, consumers: &BTreeMap>, nodes: &BTreeMap, - dependencies: &BTreeMap>, -) -> Option<(i64, i64)> { - let mut window: Option<(i64, i64)> = None; - let mut pending = vec![(id, 0i64)]; - while let Some((node, end_offset_ms)) = pending.pop() { - if roots.contains(&node) { - return None; - } - for &consumer in consumers.get(&node).into_iter().flatten() { - let read = match &nodes[&consumer].payload { - Payload::NonASAP(NonASAPOp::TimeShift { shift, .. }) if shift.at.is_none() => { - pending.push((consumer, end_offset_ms.saturating_add(shift.offset_ms))); - continue; - } - Payload::NonASAP(NonASAPOp::TimeRange { .. }) => { - time_range_window(consumer, nodes, dependencies)? - } - Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) - if nodes[&consumer].output_state.timing - == planner_types::ir::properties::ExecutionTiming::QueryTime => - { - pending.push((consumer, end_offset_ms)); - continue; - } - Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { - tumbling_window_width_ms, - .. - }) => ( - end_offset_ms, - i64::try_from(*tumbling_window_width_ms).ok()?, - ), - _ => return None, - }; - window = Some(window.map_or(read, |window| window_union(window, read))); - } +) -> Option { + if roots.contains(&id) { + return None; } - window + time_window::windows_read_above( + id, + |node| consumers.get(&node).into_iter().flatten().copied(), + |node| match TimeIndexedRead::of(&nodes[&node].payload, nodes[&node].output_state.timing) { + TimeIndexedRead::Through { .. } if roots.contains(&node) => TimeIndexedRead::Whole, + read => read, + }, + true, + )? + .into_iter() + .map(|(_, window)| window) + .reduce(TimeWindow::union) } -/// The window `(end_offset_ms, width_ms)` back from the evaluation time of -/// the data `id`'s state summarizes: the smallest window holding every -/// `TimeRange` below it. `None` when none is below it, or one is read -/// through an `@` anchor. +/// The window back from the evaluation time of the data `id`'s state +/// summarizes: the smallest window holding every window below it +/// ([`time_window::windows_below`]). `None` when none is below it, or one +/// is read through an `@` anchor or a negative offset. fn window_below( id: NodeId, nodes: &BTreeMap, dependencies: &BTreeMap>, -) -> Option<(i64, i64)> { - let mut seen = BTreeSet::new(); - let mut pending = vec![id]; - let mut window: Option<(i64, i64)> = None; - while let Some(node) = pending.pop() { - if !seen.insert(node) { - continue; - } - if let Payload::NonASAP(NonASAPOp::TimeRange { .. }) = &nodes[&node].payload { - let read = time_range_window(node, nodes, dependencies)?; - window = Some(window.map_or(read, |window| window_union(window, read))); - } else { - pending.extend(dependencies.get(&node).into_iter().flatten().copied()); - } - } - window +) -> Option { + time_window::windows_below( + id, + |node| dependencies.get(&node).into_iter().flatten().copied(), + |node| first_input(node, dependencies), + |node| &nodes[&node].payload, + )? + .into_iter() + .reduce(TimeWindow::union) } fn first_input(id: NodeId, dependencies: &BTreeMap>) -> Option { diff --git a/crates/executor/src/sources/mod.rs b/crates/executor/src/sources/mod.rs index 6cdcdcf34..a03b32d3d 100644 --- a/crates/executor/src/sources/mod.rs +++ b/crates/executor/src/sources/mod.rs @@ -14,6 +14,7 @@ use planner_types::ir::operator::Source; use planner_types::ir::schema::DataType; use planner_types::ir::ScalarExpr; use planner_types::plan::materialized_view::MaterializedView; +use planner_types::plan::TimeWindow; use std::sync::Arc; mod data_store; @@ -43,18 +44,16 @@ enum ScanInput { predicates: Vec, /// `predicates` as the store receives them. predicate_hints: Vec>, - /// The rows its consumers read, `(end_offset_ms, width_ms)` back - /// from the time the run evaluates it at - /// ([`window_bounds_ms`]); `None` when one reads every row. - read_window_ms: Option<(i64, i64)>, + /// The rows its consumers read, back from the time the run + /// evaluates it at; `None` when one reads every row. + read_window: Option, }, /// A materialized view slot: exact, never re-filtered; its states are /// the store's. MaterializedView { view: MaterializedView, - /// The rows a read selects, `(end_offset_ms, width_ms)` back from - /// the evaluation time ([`window_bounds_ms`]). - read_window_ms: (i64, i64), + /// The rows a read selects, back from the evaluation time. + read_window: TimeWindow, }, } @@ -72,7 +71,7 @@ impl Scan { source: Source, output: SchemaRef, predicates: Vec, - read_window_ms: Option<(i64, i64)>, + read_window: Option, ) -> Self { let predicate_hints = predicates .iter() @@ -85,25 +84,22 @@ impl Scan { source, predicates, predicate_hints, - read_window_ms, + read_window, }, } } /// A materialized view slot reading the rows of `view` that - /// `read_window_ms` selects. + /// `read_window` selects. pub(crate) fn materialized_view( store: Arc, view: MaterializedView, output: SchemaRef, - read_window_ms: (i64, i64), + read_window: TimeWindow, ) -> Self { Self { store, output, - input: ScanInput::MaterializedView { - view, - read_window_ms, - }, + input: ScanInput::MaterializedView { view, read_window }, } } /// `batch` in the slot's schema; bad input when its columns differ. @@ -165,18 +161,11 @@ impl PhysicalOperator for Scan { source, predicates, predicate_hints, - read_window_ms, - } => self.start_raw_data( - source, - predicates, - predicate_hints, - *read_window_ms, - context, - ), - ScanInput::MaterializedView { - view, - read_window_ms, - } => self.start_materialized_view(view, *read_window_ms, context), + read_window, + } => self.start_raw_data(source, predicates, predicate_hints, *read_window, context), + ScanInput::MaterializedView { view, read_window } => { + self.start_materialized_view(view, *read_window, context) + } } } } @@ -184,18 +173,18 @@ impl Scan { fn start_materialized_view<'a>( &'a self, view: &'a MaterializedView, - read_window_ms: (i64, i64), + read_window: TimeWindow, context: RunContext, ) -> Result, Error> { // A query run reads the window back from its evaluation time; an // ingestion run reads the window ending at its end, whatever the // offset. - let (end_offset_ms, _) = read_window_ms; let at_ms = match context.scope { Scope::Query { evaluation_time_ms } => evaluation_time_ms, - Scope::Ingestion { window_end_ms, .. } => window_end_ms.saturating_add(end_offset_ms), + Scope::Ingestion { window_end_ms, .. } => window_end_ms + .saturating_add(i64::try_from(read_window.end_offset_ms).unwrap_or(i64::MAX)), }; - let time_range_ms = window_bounds_ms(read_window_ms, at_ms); + let time_range_ms = read_window.bounds_ms(at_ms); let opening = context.clone(); let stream = stream::once(async move { if opening.is_cancelled() { @@ -226,7 +215,7 @@ impl Scan { source: &'a Source, predicates: &'a [CompiledExpression], predicate_hints: &'a [ScalarExpr<()>], - read_window_ms: Option<(i64, i64)>, + read_window: Option, context: RunContext, ) -> Result, Error> { // `now()` in the predicates is this run's evaluation time. @@ -239,14 +228,14 @@ impl Scan { .collect(); let time_range_ms = match context.scope { Scope::Query { evaluation_time_ms } => { - read_window_ms.map(|window| window_bounds_ms(window, evaluation_time_ms)) + read_window.map(|window| window.bounds_ms(evaluation_time_ms)) } // A slot read through windows reads the rows of the window the // run builds. Scope::Ingestion { window_start_ms, window_end_ms, - } => read_window_ms.map(|_| (window_start_ms, window_end_ms)), + } => read_window.map(|_| (window_start_ms, window_end_ms)), }; let time_filter = match time_range_ms { Some(range) => Some(( @@ -326,13 +315,6 @@ impl Scan { } } -/// `(start, end]` of the window `(end_offset_ms, width_ms)` back from -/// `at_ms`: `(at_ms - end_offset_ms - width_ms, at_ms - end_offset_ms]`. -pub(crate) fn window_bounds_ms((end_offset_ms, width_ms): (i64, i64), at_ms: i64) -> (i64, i64) { - let end = at_ms.saturating_sub(end_offset_ms); - (end.saturating_sub(width_ms), end) -} - /// The rows of `batch` that `keep` keeps; `batch` itself, uncopied, when it /// keeps every row. fn keep_rows(batch: Batch, keep: &arrow::array::BooleanArray) -> Result { diff --git a/crates/executor/tests/tumbling_window_chain.rs b/crates/executor/tests/tumbling_window_chain.rs index daa1e1662..b18377218 100644 --- a/crates/executor/tests/tumbling_window_chain.rs +++ b/crates/executor/tests/tumbling_window_chain.rs @@ -220,7 +220,12 @@ fn a_chain_built_at_ingestion_answers_queries_through_its_time_ranges() { dag.materialized_views().unwrap(); assert_eq!( dag.refresh_window(chain), - planner_types::plan::materialized_view::RefreshWindow::of_window(0, WIDTH_MS) + planner_types::plan::materialized_view::RefreshWindow::of_window( + planner_types::plan::TimeWindow { + end_offset_ms: 0, + width_ms: WIDTH_MS as u64, + } + ) ); let refresh = compile(&dag, &[], &[chain as u64]).unwrap(); let query = compile(&dag, &views, &[now as u64, shifted as u64]).unwrap(); diff --git a/crates/integration-tests/tests/materialized_views.rs b/crates/integration-tests/tests/materialized_views.rs index 17eecc9a7..e0c8e384e 100644 --- a/crates/integration-tests/tests/materialized_views.rs +++ b/crates/integration-tests/tests/materialized_views.rs @@ -4,9 +4,9 @@ use std::collections::HashSet; use asap_planner::{ - InstallablePlan, InstallablePlanError, MaterializedViewRefresh, INSTALLABLE_PLAN_WIRE_VERSION, + InstallablePlan, InstallablePlanError, MaterializedViewRefresh, RefreshWindow, TimeWindow, + INSTALLABLE_PLAN_WIRE_VERSION, }; -use asap_types::ir::operator::Source; use asap_types::plan::physical_asap_dag::PhysicalASAPDAG; use asap_types::ir::{NonASAPOp, Operator}; @@ -122,27 +122,30 @@ fn materialized_views_equal_the_stored_outputs_main_listed() { views_read, "{name}" ); - let windows: Vec<(Source, i64, i64)> = output["inputs"]["raw_sample_windows"] + // The raw-data windows it scans: each reads a source, and their + // union is its refresh window when it reads no view. + let windows: Vec = output["inputs"]["raw_sample_windows"] .as_array() .unwrap() .iter() .map(|window| { - ( - source_of(window["scan_node_id"].as_u64().unwrap()), - window["end_offset_ms"].as_i64().unwrap(), - window["width_ms"].as_i64().unwrap(), - ) + source_of(window["scan_node_id"].as_u64().unwrap()); + TimeWindow { + end_offset_ms: window["end_offset_ms"].as_u64().unwrap(), + width_ms: window["width_ms"].as_u64().unwrap(), + } }) .collect(); - assert_eq!( - view.inputs - .raw_data_windows() - .iter() - .map(|w| (w.source().clone(), w.end_offset_ms(), w.width_ms())) - .collect::>(), - windows, - "{name}" - ); + if views_read.is_empty() && !windows.is_empty() { + assert_eq!( + dag.refresh_window(view.node_id), + windows + .into_iter() + .reduce(TimeWindow::union) + .and_then(RefreshWindow::of_window), + "{name}" + ); + } } } assert_eq!(refreshes.len(), 2, "both refresh modes are covered"); diff --git a/crates/planner/src/lib.rs b/crates/planner/src/lib.rs index 939e8ff63..11a98a6fa 100644 --- a/crates/planner/src/lib.rs +++ b/crates/planner/src/lib.rs @@ -63,9 +63,10 @@ pub use asap_types::ir::properties::selection::{ pub use asap_types::ir::properties::ExecutionTiming; pub use asap_types::plan::materialized_view::{ IngestionRefreshGroup, InstallableDagError, MaterializedView, MaterializedViewInputs, - MaterializedViewRefresh, RawDataRetention, RawDataWindow, RefreshWindow, + MaterializedViewRefresh, RawDataRetention, RefreshWindow, }; pub use asap_types::plan::physical_asap_dag::{PhysicalASAPDAG, PhysicalASAPNodeId, ViewCoverage}; +pub use asap_types::plan::TimeWindow; pub use asap_types::summary_type::{SketchAlgorithm, SketchParams, StatisticKind}; pub use asap_types::workload::PlanningWorkload; diff --git a/crates/planner/tests/columnar_batches.rs b/crates/planner/tests/columnar_batches.rs index e9b8449d8..e2b376682 100644 --- a/crates/planner/tests/columnar_batches.rs +++ b/crates/planner/tests/columnar_batches.rs @@ -61,18 +61,22 @@ async fn a_tumbling_window_is_built_from_arrow_columns_with_one_label_entry_per_ .await .unwrap() .0; - // The newest tumbling window: its node, and the raw-data window it reads. + // The newest tumbling window: its node, and the raw-data window it reads + // (its refresh window's, ending at or before the evaluation). let views = plan.materialized_views().unwrap(); + let newest_window = |node_id| { + let refresh = plan.dag.refresh_window(node_id).expect("a bounded window"); + asap_planner::TimeWindow { + end_offset_ms: refresh.phase_ms, + width_ms: refresh.width_ms, + } + }; let output = views .iter() - .max_by_key(|output| { - output.inputs.raw_data_windows()[0] - .bounds_ms(EVALUATION_MS) - .1 - }) + .max_by_key(|output| newest_window(output.node_id).bounds_ms(EVALUATION_MS).1) .unwrap(); // The time range the tumbling window covers at this evaluation, `(start_ms, end_ms]`. - let (start_ms, end_ms) = output.inputs.raw_data_windows()[0].bounds_ms(EVALUATION_MS); + let (start_ms, end_ms) = newest_window(output.node_id).bounds_ms(EVALUATION_MS); // Feed both series within the selected tumbling window, whose width is an optimizer // decision. Keep distinct millisecond timestamps even for very fine tumbling windows. let width_ms = end_ms - start_ms; diff --git a/crates/planner/tests/sql_table_scans.rs b/crates/planner/tests/sql_table_scans.rs index c078dbe2f..ee51e7c1a 100644 --- a/crates/planner/tests/sql_table_scans.rs +++ b/crates/planner/tests/sql_table_scans.rs @@ -268,11 +268,16 @@ fn run_data_plane(plan: &InstallablePlan) -> Vec>> { }) .collect() } - None => vec![view - .inputs - .raw_data_windows() - .iter() - .map(|window| window.bounds_ms(EVALUATION_MS)) + None => vec![dag + .refresh_window(view.node_id) + .map(|refresh| { + asap_planner::TimeWindow { + end_offset_ms: refresh.phase_ms, + width_ms: refresh.width_ms, + } + .bounds_ms(EVALUATION_MS) + }) + .into_iter() .chain( view.inputs .materialized_views @@ -350,7 +355,7 @@ async fn ingestion_time_tumbling_windows_built_from_table_rows_answer_exactly() views .iter() .any(|v| v.refresh() == MaterializedViewRefresh::AtIngestion - && !v.inputs.raw_data_windows().is_empty()), + && v.inputs.materialized_views.is_empty()), "tumbling windows built at ingestion time from table rows: {:?}", views ); @@ -402,7 +407,7 @@ async fn a_window_equal_to_its_interval_is_built_at_ingestion_time_from_table_ro views .iter() .any(|v| v.refresh() == MaterializedViewRefresh::AtIngestion - && !v.inputs.raw_data_windows().is_empty()), + && v.inputs.materialized_views.is_empty()), "built at ingestion time from table rows: {:?}", views ); diff --git a/crates/stage3-plan-selection/src/cost.rs b/crates/stage3-plan-selection/src/cost.rs index 91a4a0b6b..f0b4a7a3e 100644 --- a/crates/stage3-plan-selection/src/cost.rs +++ b/crates/stage3-plan-selection/src/cost.rs @@ -21,7 +21,7 @@ //! evaluation of the queries reaching its group (once per request when it //! is materialized across evaluations). A materialized candidate and a //! query-time `Scan` retain the most their selectable consumers read back -//! ([`read_back_ms`]). Execution contexts make both per context. +//! ([`retention_need_ms`]). Execution contexts make both per context. use std::collections::HashMap; @@ -37,6 +37,7 @@ use asap_types::ir::{ASAPOp, NonASAPOp, Operator}; use asap_types::plan::physical_asap_dag::{ PhysicalASAPDAGNode, PhysicalASAPNodeId, PhysicalASAPOperatorPayload, }; +use asap_types::plan::time_window::TimeIndexedRead; use asap_types::plan::view_coverage; use asap_types::stages::{ Lifetime, Memo, MemoGroupId, PhysicalASAPMemoCandidates, PhysicalCandidateId, PhysicalEdgeId, @@ -133,57 +134,68 @@ pub(crate) fn retains_rows(physical: &PhysicalASAPMemoCandidates, v: PhysicalCan ) || (scan && candidate.physical_properties.timing() == ExecutionTiming::QueryTime) } -/// How far back from the evaluation time `producer`'s rows are read -/// through `edges` (its edges to consumers; `consumers_of` gives each -/// candidate's edges to its own consumers): [`asap_cost::read_back_ms`] -/// with the longest range-evaluation span of the queries reaching each -/// `TimeRange` when `with_span` (retention), without it (one execution's -/// read of a query-time `Scan`). -pub(crate) fn read_back_ms( +/// How far back from the evaluation time `producer`'s rows must be kept +/// for its readers (`readers_of`, the candidates that may read it): +/// [`asap_cost::read_back_ms`] with, at each reader, the longest +/// range-evaluation span of the queries reaching it. +pub(crate) fn retention_need_ms>( physical: &PhysicalASAPMemoCandidates, entries: &[QueryWorkloadEntry], producer: PhysicalCandidateId, - edges: &[PhysicalEdgeId], - consumers_of: &dyn Fn(PhysicalCandidateId) -> Vec, - with_span: bool, + readers_of: impl Fn(PhysicalCandidateId) -> R, ) -> Option { let memo = &physical.logical_candidates.memo; - let operator_of = |v: PhysicalCandidateId| { - &memo - .candidate(physical.physical_candidates[v.0].logical_candidate_id) - .expression - .operator - }; let span_ms = |v: PhysicalCandidateId| { - if !with_span { - return 0; - } let group = physical.physical_candidates[v.0].logical_candidate_id.group; memo.groups()[group.0 as usize] .reached_by_queries() .iter() .filter_map(|&query| entries.get(query)?.range_evaluation) - .map(|range| i64::try_from(range.span_ms).unwrap_or(i64::MAX)) + .map(|range| range.span_ms) .max() .unwrap_or(0) }; - let readers = |edges: Vec| -> Vec { - edges - .into_iter() - .map(|edge| physical.edge(edge).consumer_candidate_id) - .collect() + read_back(physical, producer, readers_of, span_ms) +} + +/// How far back from the evaluation time one execution of the query-time +/// `Scan` `producer` reads, for its readers (`readers_of`): +/// [`asap_cost::read_back_ms`] without a range-evaluation span. +pub(crate) fn scan_read_ms>( + physical: &PhysicalASAPMemoCandidates, + producer: PhysicalCandidateId, + readers_of: impl Fn(PhysicalCandidateId) -> R, +) -> Option { + read_back(physical, producer, readers_of, |_| 0) +} + +/// [`asap_cost::read_back_ms`] over candidates, each read as its operator +/// at its timing reads ([`TimeIndexedRead::of`]). +fn read_back>( + physical: &PhysicalASAPMemoCandidates, + producer: PhysicalCandidateId, + readers_of: impl Fn(PhysicalCandidateId) -> R, + span_ms: impl Fn(PhysicalCandidateId) -> u64, +) -> Option { + let memo = &physical.logical_candidates.memo; + let operator_of = |v: PhysicalCandidateId| { + &memo + .candidate(physical.physical_candidates[v.0].logical_candidate_id) + .expression + .operator }; asap_cost::read_back_ms( - readers(edges.to_vec()), - operator_of, - |v| readers(consumers_of(v)), - span_ms, + producer, + readers_of, |v| { - physical.physical_candidates[v.0] - .physical_properties - .timing() - == ExecutionTiming::QueryTime + TimeIndexedRead::of( + operator_of(v), + physical.physical_candidates[v.0] + .physical_properties + .timing(), + ) }, + span_ms, matches!( operator_of(producer), Operator::NonASAP(NonASAPOp::Scan { .. }) @@ -209,6 +221,12 @@ pub(crate) fn selection_costs( for &edge in selectable { out_edges[physical.edge(edge).producer_candidate_id.0].push(edge); } + // The candidates that may read each candidate. + let readers_of = |v: PhysicalCandidateId| { + out_edges[v.0] + .iter() + .map(|&edge| physical.edge(edge).consumer_candidate_id) + }; // Each candidate's node, its inputs numbered 0.. (the ids do not // matter to `node_resource_usage_and_cost`), with the retention it would need. let nodes: Vec = (0..count) @@ -217,14 +235,7 @@ pub(crate) fn selection_costs( let inputs: Vec = (0..input_count(physical, v)).collect(); let mut node = candidate_node(physical, v, v.0, &inputs); if retains_rows(physical, v) { - node.retention_ms = read_back_ms( - physical, - entries, - v, - &out_edges[v.0], - &|consumer| out_edges[consumer.0].clone(), - true, - ); + node.retention_ms = retention_need_ms(physical, entries, v, readers_of); } node }) @@ -242,16 +253,7 @@ pub(crate) fn selection_costs( Operator::NonASAP(NonASAPOp::Scan { .. }) ); (scan && candidate.physical_properties.timing() == ExecutionTiming::QueryTime) - .then(|| { - read_back_ms( - physical, - entries, - v, - &out_edges[v.0], - &|consumer| out_edges[consumer.0].clone(), - false, - ) - }) + .then(|| scan_read_ms(physical, v, readers_of)) .flatten() }) .collect(); diff --git a/crates/stage3-plan-selection/src/plan.rs b/crates/stage3-plan-selection/src/plan.rs index 9ca6a825e..34f2bc76f 100644 --- a/crates/stage3-plan-selection/src/plan.rs +++ b/crates/stage3-plan-selection/src/plan.rs @@ -14,14 +14,14 @@ use asap_types::stages::{ }; use asap_types::workload::QueryWorkloadEntry; -use crate::cost::{candidate_node, input_count, read_back_ms, retains_rows}; +use crate::cost::{candidate_node, input_count, retains_rows, retention_need_ms}; use crate::milp::model::{QueryId, SolvedSelection}; /// The plan of `solved`, whose output `j` answers `outputs[j]` (a query and /// the group its root, or one operator its scalar root, reads). Nodes are /// numbered producers first. A node that retains rows (materialized, or a /// query-time `Scan`) has `retention_ms` the most its selected consumers -/// read back ([`read_back_ms`] with the range-evaluation span), and at least the range-evaluation +/// read back ([`retention_need_ms`]), and at least the range-evaluation /// span of a query it answers directly. pub(crate) fn plan_of( physical: &PhysicalASAPMemoCandidates, @@ -43,7 +43,9 @@ pub(crate) fn plan_of( .or_default() .push(id); } - let consumers = |v: PhysicalCandidateId| consumers_of.get(&v).cloned().unwrap_or_default(); + let consumers = |v: PhysicalCandidateId| consumers_of.get(&v).into_iter().flatten().copied(); + let readers_of = + |v: PhysicalCandidateId| consumers(v).map(|edge| physical.edge(edge).consumer_candidate_id); let inputs = |v: PhysicalCandidateId| -> Vec { (0..input_count(physical, v)) .map(|i| producer_of[&(v, i)]) @@ -98,9 +100,8 @@ pub(crate) fn plan_of( inputs(v).iter().map(|producer| node_of[producer]).collect(); let mut node = candidate_node(physical, v, id, &input_ids); if retains_rows(physical, v) { - node.retention_ms = - read_back_ms(physical, entries, v, &consumers(v), &consumers, true) - .map(|need_ms| need_ms.max(output_span_ms.get(&v).copied().unwrap_or(0))); + node.retention_ms = retention_need_ms(physical, entries, v, readers_of) + .map(|need_ms| need_ms.max(output_span_ms.get(&v).copied().unwrap_or(0))); } node }) diff --git a/crates/types/src/plan/materialized_view.rs b/crates/types/src/plan/materialized_view.rs index 3f0dae209..648a67627 100644 --- a/crates/types/src/plan/materialized_view.rs +++ b/crates/types/src/plan/materialized_view.rs @@ -80,6 +80,7 @@ use super::physical_asap_dag::{ PhysicalASAPDAG, PhysicalASAPDAGNode, PhysicalASAPDAGValidationError, PhysicalASAPNodeId, ViewCoverage, }; +use super::time_window::{self, TimeIndexedRead, TimeWindow}; use crate::ir::operator::arguments::Source; use crate::ir::operator::asap::ASAPOp; use crate::ir::operator::node::Operator; @@ -127,81 +128,17 @@ impl MaterializedView { } } -/// What a view's refresh reads, each bound as a source of the refresh. +/// What a view's refresh reads, each bound as a source of the refresh; +/// the raw data it scans is read by `Source` through the windows below it +/// ([`PhysicalASAPDAG::refresh_window`]). #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct MaterializedViewInputs { - /// The windows of raw data it scans directly, one per `TimeRange` it - /// reaches, in node order. - pub(crate) raw_data_windows: Vec, /// The views it reads, by node id, in node order; each is listed before /// it in [`PhysicalASAPDAG::materialized_views`]. pub materialized_views: Vec, } -#[cfg(any(test, feature = "test-support"))] -impl MaterializedViewInputs { - /// The windows of raw data it scans directly. - pub fn raw_data_windows(&self) -> &[RawDataWindow] { - &self.raw_data_windows - } -} - -/// A window of a raw-data source a view's refresh scans: at evaluation time -/// `T`, the rows of `source` in `(T - end_offset_ms - width_ms, T - -/// end_offset_ms]`. Read as `TimeRange(width) <- TimeShift(offset)* <- -/// Scan`, with no `TimeShift`, `TimeRange` or subquery above the -/// `TimeRange`, so the window depends on `T` alone. For an instant selector -/// `width_ms` is its lookback. -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct RawDataWindow { - /// The source the `Scan` reads. - pub(crate) source: Source, - /// How long before the evaluation time the window ends: the sum of the - /// offset `TimeShift`s between the `TimeRange` and the `Scan`. - pub(crate) end_offset_ms: i64, - /// Width of the window, from the `TimeRange`. - pub(crate) width_ms: i64, -} - -#[cfg(any(test, feature = "test-support"))] -impl RawDataWindow { - /// The source the `Scan` reads. - pub fn source(&self) -> &Source { - &self.source - } - - /// How long before the evaluation time the window ends. - pub fn end_offset_ms(&self) -> i64 { - self.end_offset_ms - } - - /// Width of the window. - pub fn width_ms(&self) -> i64 { - self.width_ms - } - - /// The window of `source` ending `end_offset_ms` before the evaluation - /// time, `width_ms` wide. - pub fn new(source: Source, end_offset_ms: i64, width_ms: i64) -> Self { - Self { - source, - end_offset_ms, - width_ms, - } - } -} - -#[cfg(any(test, feature = "test-support"))] -impl RawDataWindow { - /// `(start, end]` of the window at evaluation time `evaluation_time_ms`. - pub fn bounds_ms(&self, evaluation_time_ms: i64) -> (i64, i64) { - let end = evaluation_time_ms - self.end_offset_ms; - (end - self.width_ms, end) - } -} - /// Which run refreshes a materialized view. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] @@ -233,16 +170,13 @@ pub struct RefreshWindow { } impl RefreshWindow { - /// The refresh window of the window `(T - end_offset_ms - width_ms, T - - /// end_offset_ms]`: `(width_ms, end_offset_ms mod width_ms)`; `None` for - /// an empty window (`width_ms <= 0`). - pub fn of_window(end_offset_ms: i64, width_ms: i64) -> Option { - let phase_ms = end_offset_ms - .checked_rem_euclid(width_ms) - .filter(|_| width_ms > 0)?; + /// The refresh window of `window`: `(width_ms, end_offset_ms mod + /// width_ms)`; `None` for an empty window. + pub fn of_window(window: TimeWindow) -> Option { + let phase_ms = window.end_offset_ms.checked_rem(window.width_ms)?; Some(RefreshWindow { - width_ms: u64::try_from(width_ms).ok()?, - phase_ms: u64::try_from(phase_ms).ok()?, + width_ms: window.width_ms, + phase_ms, }) } } @@ -339,7 +273,7 @@ pub enum InstallableDagError { )] IngestionBuildReadsWindowsOfDifferentOffsets { node_id: PhysicalASAPNodeId, - end_offsets_ms: Vec, + end_offsets_ms: Vec, }, /// Bad input (a plan file): view `node_id` has no `retention_ms`. #[error("materialized view {node_id} has no retention_ms")] @@ -378,7 +312,12 @@ impl PhysicalASAPDAG { let index = ViewIndex::new(self); let views = index.views()?; for view in &views { - if view.retention_ms < index.read_back_ms(view.node_id).0 { + // A view read at a time not relative to the evaluation (`@`) is + // read unbounded, which no retention covers; it is not checked. + if index + .read_back_ms(view.node_id) + .is_some_and(|read_back_ms| view.retention_ms < read_back_ms) + { return Err(InstallableDagError::ViewRetentionTooShort { node_id: view.node_id, }); @@ -491,20 +430,10 @@ impl PhysicalASAPDAG { nodes .into_iter() .map(|node| { - let window = index.windows_below(node).and_then(|windows| { - windows - .into_iter() - .reduce(|(a_end, a_width), (b_end, b_width)| { - let end = a_end.min(b_end); - let start_back = a_end - .saturating_add(a_width) - .max(b_end.saturating_add(b_width)); - (end, start_back.saturating_sub(end)) - }) - }); - let refresh_window = window.and_then(|(end_offset_ms, width_ms)| { - RefreshWindow::of_window(end_offset_ms, width_ms) - }); + let refresh_window = index + .windows_below(node) + .and_then(|windows| windows.into_iter().reduce(TimeWindow::union)) + .and_then(RefreshWindow::of_window); (node, refresh_window) }) .collect() @@ -526,14 +455,13 @@ impl PhysicalASAPDAG { let Operator::NonASAP(NonASAPOp::Scan { source, .. }) = &node.payload else { continue; }; - let (read_back_ms, read_outside_windows) = index.read_back_ms(node.id); - let lookback_ms = match (read_outside_windows, node.retention_ms) { - (true, _) => None, - (false, Some(kept_ms)) if kept_ms >= read_back_ms => Some(kept_ms), - (false, Some(_)) => { + let lookback_ms = match (index.read_back_ms(node.id), node.retention_ms) { + (None, _) => None, + (Some(read_back_ms), Some(kept_ms)) if kept_ms >= read_back_ms => Some(kept_ms), + (Some(_), Some(_)) => { return Err(InstallableDagError::ScanRetentionTooShort { node_id: node.id }) } - (false, None) => { + (Some(_), None) => { return Err(InstallableDagError::ScanWithoutRetention { node_id: node.id }) } }; @@ -705,11 +633,11 @@ impl<'a> ViewIndex<'a> { if refreshed_at_ingestion { // An ingestion run builds the windows ending at its end, so // every window the view reads must end at one offset. - let mut end_offsets_ms: Vec = self + let mut end_offsets_ms: Vec = self .windows_below(id) .unwrap_or_default() .into_iter() - .map(|(end_offset_ms, _)| end_offset_ms) + .map(|window| window.end_offset_ms) .collect(); end_offsets_ms.sort_unstable(); end_offsets_ms.dedup(); @@ -743,103 +671,51 @@ impl<'a> ViewIndex<'a> { Ok(derived) } - /// The windows `(end_offset_ms, width_ms)` back from the evaluation - /// time that `node` reads: a chain of width `w` over `TimeShift`s - /// summing to `s` its own `(s, w)`; any other node each `TimeRange(w)` - /// below it over shifts summing to `s`, `(s, w)`, and each chain below - /// it reached without passing a `TimeRange`. `None` when one is read - /// through an `@` anchor below its `TimeRange` or chain. - fn windows_below(&self, node: PhysicalASAPNodeId) -> Option> { - let mut windows = Vec::new(); - let mut seen = HashSet::new(); - let mut pending = vec![node]; - while let Some(id) = pending.pop() { - if !seen.insert(id) { - continue; - } - let width_ms = match &self.nodes.get(&id)?.payload { - Operator::NonASAP(NonASAPOp::TimeRange { range, .. }) => { - i64::try_from(range.as_millis()).ok()? - } - Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { - tumbling_window_width_ms, - .. - }) => i64::try_from(*tumbling_window_width_ms).ok()?, - _ => { - pending.extend(self.inputs.get(&id).into_iter().flatten().copied()); - continue; - } - }; - windows.push((self.offset_below(id)?, width_ms)); - } - Some(windows) - } - - /// The sum of the offsets of the `TimeShift`s right below `node`; - /// `None` when one has an `@` anchor. - fn offset_below(&self, node: PhysicalASAPNodeId) -> Option { - let mut end_offset_ms = 0i64; - let mut current = node; - while let Some(input) = self.only_input(current) { - let Some(NonASAPOp::TimeShift { shift, .. }) = self.relational(input) else { - break; - }; - if shift.at.is_some() { - return None; - } - end_offset_ms = end_offset_ms.saturating_add(shift.offset_ms); - current = input; - } - Some(end_offset_ms) + /// The windows back from the evaluation time that `node` reads + /// ([`time_window::windows_below`]): a chain its own tumbling window; + /// any other node each `TimeRange` below it and each chain below it + /// reached without passing a `TimeRange`. `None` when one is read + /// through an `@` anchor or a negative offset. + fn windows_below(&self, node: PhysicalASAPNodeId) -> Option> { + time_window::windows_below( + node, + |id| self.inputs.get(&id).into_iter().flatten().copied(), + |id| self.only_input(id), + |id| &self.nodes[&id].payload, + ) } - /// How far back from the evaluation time the consumers of `node` read - /// it: through `TimeShift`s (without an `@` anchor) summing to `o`, a - /// `TimeRange(r)` reads `o + r` back, a chain of width `w` built at - /// ingestion time `o + w`, and any other consumer `o`; the most of - /// these, and whether a consumer other than a `TimeRange` or chain (or - /// a root) reads it, outside a window. A chain built at query time - /// builds the windows its own readers read, so it reads `node` as far - /// back as they read it: the walk passes up through it like a shift of - /// 0. - fn read_back_ms(&self, node: PhysicalASAPNodeId) -> (u64, bool) { - let mut read_back_ms = 0u64; - let mut read_outside_windows = false; - let mut pending = vec![(node, 0i64)]; - while let Some((id, offset_ms)) = pending.pop() { - if self.dag.roots.contains(&id) { - read_outside_windows = true; - } - for &consumer in self.consumers.get(&id).into_iter().flatten() { - let width_ms = match &self.nodes[&consumer].payload { - Operator::NonASAP(NonASAPOp::TimeShift { shift, .. }) if shift.at.is_none() => { - pending.push((consumer, offset_ms.saturating_add(shift.offset_ms))); - continue; - } - Operator::NonASAP(NonASAPOp::TimeRange { range, .. }) => { - u64::try_from(range.as_millis()).unwrap_or(u64::MAX) - } - Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) - if self.nodes[&consumer].output_state.timing - == ExecutionTiming::QueryTime => - { - pending.push((consumer, offset_ms)); - continue; - } - Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { - tumbling_window_width_ms, - .. - }) => *tumbling_window_width_ms, - _ => { - read_outside_windows = true; - 0 - } - }; - let offset_ms = u64::try_from(offset_ms).unwrap_or(0); - read_back_ms = read_back_ms.max(offset_ms.saturating_add(width_ms)); - } + /// How far back from the evaluation time the readers of `node` read it + /// ([`time_window::windows_read_above`]): the most any reads back. + /// `None`, unbounded, when a root or a reader at a time not relative + /// to the evaluation (`@`) reads it, or, for a raw-data `Scan`, a reader + /// other than a window (a `TimeRange` or an ingestion-time chain). + /// + /// Example: a `Scan` read by `TimeShift(1m) -> TimeRange(5m)`: 360,000. + fn read_back_ms(&self, node: PhysicalASAPNodeId) -> Option { + let root = |id: PhysicalASAPNodeId| self.dag.roots.contains(&id); + if root(node) { + return None; } - (read_back_ms, read_outside_windows) + let windows = time_window::windows_read_above( + node, + |id| self.consumers.get(&id).into_iter().flatten().copied(), + |id| match TimeIndexedRead::of( + &self.nodes[&id].payload, + self.nodes[&id].output_state.timing, + ) { + TimeIndexedRead::Through { .. } if root(id) => TimeIndexedRead::Whole, + read => read, + }, + is_raw_data_scan(self.nodes[&node]), + )?; + Some( + windows + .iter() + .map(|(_, window)| window.read_back_ms()) + .max() + .unwrap_or(0), + ) } /// See [`PhysicalASAPDAG::check_raw_data_windows`]. @@ -850,13 +726,10 @@ impl<'a> ViewIndex<'a> { } match &node.payload { Operator::NonASAP(NonASAPOp::TimeRange { .. }) => { - self.window_at(node.id)?; + self.check_raw_window_at(node.id)?; } - Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { - tumbling_window_width_ms, - .. - }) => { - self.raw_window_below(node.id, u128::from(*tumbling_window_width_ms))?; + Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) => { + self.check_raw_window_below(node.id)?; } _ => {} } @@ -864,31 +737,32 @@ impl<'a> ViewIndex<'a> { Ok(()) } - /// The raw-data window read through the `TimeRange` `time_range`, or why its shape is unsupported. - fn window_at( + /// Checks the shape of the raw-data window read through the `TimeRange` + /// `time_range` ([`Self::check_raw_window_below`]). + fn check_raw_window_at( &self, time_range: PhysicalASAPNodeId, - ) -> Result { - let Some(NonASAPOp::TimeRange { range, .. }) = self.relational(time_range) else { + ) -> Result<(), InstallableDagError> { + let Some(NonASAPOp::TimeRange { .. }) = self.relational(time_range) else { return Err(InstallableDagError::UnsupportedRawDataWindow { time_range_node_id: time_range, reason: "not a TimeRange", }); }; - self.raw_window_below(time_range, range.as_millis()) + self.check_raw_window_below(time_range) } - /// The window of width `width_ms` that `time_range` (a `TimeRange`, or - /// a `SummaryTumblingWindowChain`'s tumbling window) reads from the - /// `Scan` below it, or why its shape is unsupported. A - /// `TimeRange` run at ingestion time may have no `TimeShift` or subquery - /// above it; a query run evaluates one, and a chain's windows are - /// selected by the `TimeRange`s above it. - fn raw_window_below( + /// Checks that `time_range` (a `TimeRange`, or a + /// `SummaryTumblingWindowChain`'s tumbling window) reads the `Scan` + /// below it through `TimeShift`s without `@` summing to a non-negative + /// offset, or says why its shape is unsupported. A `TimeRange` run at + /// ingestion time may have no `TimeShift` or subquery above it; a query + /// run evaluates one, and a chain's windows are selected by the + /// `TimeRange`s above it. + fn check_raw_window_below( &self, time_range: PhysicalASAPNodeId, - width_ms: u128, - ) -> Result { + ) -> Result<(), InstallableDagError> { let unsupported = |reason| InstallableDagError::UnsupportedRawDataWindow { time_range_node_id: time_range, reason, @@ -900,8 +774,6 @@ impl<'a> ViewIndex<'a> { { return Err(unsupported("a subquery or a TimeShift above the range")); } - let width_ms = - i64::try_from(width_ms).map_err(|_| unsupported("window width overflows"))?; let mut end_offset_ms = 0i64; let mut current = time_range; loop { @@ -920,14 +792,7 @@ impl<'a> ViewIndex<'a> { Some(NonASAPOp::Scan { .. }) if end_offset_ms < 0 => { return Err(unsupported("a negative offset below the range")) } - Some(NonASAPOp::Scan { source, .. }) => { - let window = RawDataWindow { - source: source.clone(), - end_offset_ms, - width_ms, - }; - return Ok(window); - } + Some(NonASAPOp::Scan { .. }) => return Ok(()), _ => { return Err(unsupported( "an operator other than TimeShift below the range", @@ -937,24 +802,23 @@ impl<'a> ViewIndex<'a> { } } - /// What view `node`'s refresh reads: a chain reading raw data, the - /// window of its tumbling window; any other view, below it, each view - /// (`views`) and each raw-data `TimeRange` it reaches without passing - /// another of them, in node order. + /// What view `node`'s refresh reads: below it, each view (`views`) it + /// reaches without passing another, in node order; a chain reading raw + /// data none. Checks the shape of each raw-data window it reads (its + /// tumbling window over raw data, or each raw-data `TimeRange` it + /// reaches without passing a view), and that it reads no `Scan` outside + /// one. fn inputs_of_view( &self, node: PhysicalASAPNodeId, views: &HashSet, ) -> Result { - if let Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { - tumbling_window_width_ms, - .. - }) = &self.nodes[&node].payload + if let Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) = + &self.nodes[&node].payload { if self.reading_raw_data.contains(&node) { - let window = self.raw_window_below(node, u128::from(*tumbling_window_width_ms))?; + self.check_raw_window_below(node)?; return Ok(MaterializedViewInputs { - raw_data_windows: vec![window], materialized_views: Vec::new(), }); } @@ -990,12 +854,10 @@ impl<'a> ViewIndex<'a> { } raw_ranges.sort_unstable(); view_inputs.sort_unstable(); - let raw_data_windows = raw_ranges - .into_iter() - .map(|range| self.window_at(range)) - .collect::, _>>()?; + for range in raw_ranges { + self.check_raw_window_at(range)?; + } Ok(MaterializedViewInputs { - raw_data_windows, materialized_views: view_inputs, }) } diff --git a/crates/types/src/plan/mod.rs b/crates/types/src/plan/mod.rs index 8729cbcf9..d9359f465 100644 --- a/crates/types/src/plan/mod.rs +++ b/crates/types/src/plan/mod.rs @@ -1,8 +1,11 @@ //! Stage 3's output: the plan, one physical ASAP DAG //! ([`physical_asap_dag`]), and the materialized views it installs -//! ([`materialized_view`]). +//! ([`materialized_view`]); the time windows its nodes read +//! ([`time_window`]). pub mod materialized_view; pub mod physical_asap_dag; +pub mod time_window; mod view_coverage; +pub use time_window::TimeWindow; pub use view_coverage::view_coverage; diff --git a/crates/types/src/plan/time_window.rs b/crates/types/src/plan/time_window.rs new file mode 100644 index 000000000..1c97ddc81 --- /dev/null +++ b/crates/types/src/plan/time_window.rs @@ -0,0 +1,312 @@ +//! Time windows relative to an evaluation time, and the two walks over a +//! plan that find them, shared by the cost model (retention and a scan's +//! read), Stage 3, the installer's checks and the executor: +//! +//! - up from a time-indexed producer (raw data, a chain, results stored +//! per evaluation time) through its readers, the window each reads +//! ([`windows_read_above`]); +//! - down from a node, the windows its rows were read through +//! ([`windows_below`], [`end_offset_below`]). +//! +//! The walks are generic over the node type `N`: a plan node, a Stage 3 +//! candidate or an executor node, with closures giving each node's +//! operator, inputs or readers. +//! +//! Example: `sum_over_time(m[5m] offset 1m)` reads `m` through +//! `TimeShift(1m) -> TimeRange(5m)`: `TimeWindow { end_offset_ms: 60_000, +//! width_ms: 300_000 }`, `(t - 6m, t - 1m]`, read back 360,000 ms. + +use serde::{Deserialize, Serialize}; + +use crate::ir::operator::asap::ASAPOp; +use crate::ir::operator::node::Operator; +use crate::ir::operator::non_asap::NonASAPOp; +use crate::ir::properties::execution_data_state::ExecutionTiming; + +/// A time window relative to an evaluation time t: (t - end_offset_ms - width_ms, t - end_offset_ms]. +/// +/// E.g. a 5m read with offset 1m = `TimeWindow { end_offset_ms: 60_000, +/// width_ms: 300_000 }`; its `read_back_ms` is 360,000. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] +pub struct TimeWindow { + pub end_offset_ms: u64, + pub width_ms: u64, +} + +impl TimeWindow { + /// How far back from t the window reaches: `end_offset_ms + width_ms` + /// (saturating). + pub fn read_back_ms(&self) -> u64 { + self.end_offset_ms.saturating_add(self.width_ms) + } + + /// The smallest window holding both. + pub fn union(self, other: Self) -> Self { + let end_offset_ms = self.end_offset_ms.min(other.end_offset_ms); + Self { + end_offset_ms, + width_ms: self + .read_back_ms() + .max(other.read_back_ms()) + .saturating_sub(end_offset_ms), + } + } + + /// `(start, end]` of the window at evaluation time `evaluation_time_ms`, + /// in ms (saturating). + pub fn bounds_ms(&self, evaluation_time_ms: i64) -> (i64, i64) { + let millis = |ms: u64| i64::try_from(ms).unwrap_or(i64::MAX); + let end = evaluation_time_ms.saturating_sub(millis(self.end_offset_ms)); + (end.saturating_sub(millis(self.width_ms)), end) + } +} + +/// How a reader of a time-indexed producer reads its rows, relative to the +/// reader's own evaluation time ([`Self::of`]). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum TimeIndexedRead { + /// Its own readers read the rows `offset_ms` earlier: a `TimeShift` + /// without `@`, or, with offset 0, a chain built at query time, which + /// builds at each evaluation the windows its readers read. + Through { offset_ms: i64 }, + /// The window ending at its evaluation time, `width_ms` wide: a + /// `TimeRange`'s range, or a chain built at ingestion time's tumbling + /// window (it builds its newest window from it). + Window { width_ms: u64 }, + /// The rows of its own evaluation time: any other operator. + AtEvaluationTime, + /// Every row: a `TimeShift` anchored with `@`, which reads at a time + /// that is not relative to the evaluation time. + Whole, +} + +impl TimeIndexedRead { + /// How `operator`, run at `timing`, reads its input. + /// + /// Example: `TimeRange(5m)` is `Window { width_ms: 300_000 }`; a + /// query-time chain `Through { offset_ms: 0 }`. + pub fn of(operator: &Operator, timing: ExecutionTiming) -> Self { + match operator { + Operator::NonASAP(NonASAPOp::TimeShift { shift, .. }) => match shift.at { + None => Self::Through { + offset_ms: shift.offset_ms, + }, + Some(_) => Self::Whole, + }, + Operator::NonASAP(NonASAPOp::TimeRange { range, .. }) => Self::Window { + width_ms: u64::try_from(range.as_millis()).unwrap_or(u64::MAX), + }, + Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { + tumbling_window_width_ms, + .. + }) => match timing { + ExecutionTiming::IngestionTime => Self::Window { + width_ms: *tumbling_window_width_ms, + }, + ExecutionTiming::QueryTime => Self::Through { offset_ms: 0 }, + }, + _ => Self::AtEvaluationTime, + } + } +} + +/// The windows the readers of the time-indexed `producer` read it through: +/// walking up from it through `Through` readers (`readers_of` gives a +/// node's readers, `read_of` how a reader reads), each reader that ends a +/// path with the window it reads back from the evaluation time, the +/// offsets of the shifts walked through included: a `Window` reader its +/// window, an `AtEvaluationTime` reader the empty window at its offset. +/// `None`, unbounded, when one reads it `Whole`, an `AtEvaluationTime` +/// reader reads it and `at_evaluation_time_reads_whole` (raw data, which +/// is not stored by evaluation time), or a path's offsets are negative or +/// overflow. +/// +/// Example: a `Scan` read by `TimeRange(5m)` and by `TimeShift(1h) -> +/// TimeRange(5m)` is read through `{0, 5m}` and `{1h, 5m}`. +pub fn windows_read_above>( + producer: N, + readers_of: impl Fn(N) -> R, + read_of: impl Fn(N) -> TimeIndexedRead, + at_evaluation_time_reads_whole: bool, +) -> Option> { + let mut windows = Vec::new(); + // (node whose readers are next, the offsets walked through below them) + let mut pending: Vec<(N, i64)> = vec![(producer, 0)]; + let mut seen = std::collections::BTreeSet::new(); + while let Some((node, offset_ms)) = pending.pop() { + if !seen.insert((node, offset_ms)) { + continue; + } + for reader in readers_of(node) { + let width_ms = match read_of(reader) { + TimeIndexedRead::Through { + offset_ms: shift_ms, + } => { + pending.push((reader, offset_ms.checked_add(shift_ms)?)); + continue; + } + TimeIndexedRead::Window { width_ms } => width_ms, + TimeIndexedRead::AtEvaluationTime if !at_evaluation_time_reads_whole => 0, + TimeIndexedRead::AtEvaluationTime | TimeIndexedRead::Whole => return None, + }; + let end_offset_ms = u64::try_from(offset_ms).ok()?; + windows.push(( + reader, + TimeWindow { + end_offset_ms, + width_ms, + }, + )); + } + } + Some(windows) +} + +/// How far before its evaluation time `node` reads its input: the sum of +/// the offsets of the `TimeShift`s right below it (`input_of` gives a +/// node's only input, `operator_of` its operator). `None` when one has an +/// `@` anchor, or the sum is negative or overflows. +/// +/// Example: `TimeRange(5m)` over `TimeShift(1m)` over `TimeShift(1h)`: +/// 3,660,000. +pub fn end_offset_below<'a, N: Copy, C: 'a>( + node: N, + input_of: impl Fn(N) -> Option, + operator_of: impl Fn(N) -> &'a Operator, +) -> Option { + let mut end_offset_ms = 0i64; + let mut current = node; + while let Some(input) = input_of(current) { + let Operator::NonASAP(NonASAPOp::TimeShift { shift, .. }) = operator_of(input) else { + break; + }; + if shift.at.is_some() { + return None; + } + end_offset_ms = end_offset_ms.checked_add(shift.offset_ms)?; + current = input; + } + u64::try_from(end_offset_ms).ok() +} + +/// The windows back from the evaluation time that `node` reads its rows +/// through: each `TimeRange` at or below it, and each +/// `SummaryTumblingWindowChain` at or below it reached without passing +/// one (its tumbling window), each over the shifts right below it +/// ([`end_offset_below`]). `inputs_of` gives a node's inputs and +/// `input_of` its only input. `None` when one is read through an `@` +/// anchor or a negative offset. +/// +/// Example: a merge of `TimeRange(5m)` over a scan and of `TimeShift(1h) +/// -> TimeRange(5m)` over it: `{0, 5m}` and `{1h, 5m}`. +pub fn windows_below<'a, N: Copy + Ord + 'a, C: 'a, I: IntoIterator>( + node: N, + inputs_of: impl Fn(N) -> I, + input_of: impl Fn(N) -> Option, + operator_of: impl Fn(N) -> &'a Operator, +) -> Option> { + let mut windows = Vec::new(); + let mut seen = std::collections::BTreeSet::new(); + let mut pending = vec![node]; + while let Some(id) = pending.pop() { + if !seen.insert(id) { + continue; + } + let width_ms = match operator_of(id) { + Operator::NonASAP(NonASAPOp::TimeRange { range, .. }) => { + u64::try_from(range.as_millis()).ok()? + } + Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { + tumbling_window_width_ms, + .. + }) => *tumbling_window_width_ms, + _ => { + pending.extend(inputs_of(id)); + continue; + } + }; + windows.push(TimeWindow { + end_offset_ms: end_offset_below(id, &input_of, &operator_of)?, + width_ms, + }); + } + Some(windows) +} + +#[cfg(test)] +mod tests { + use super::*; + + const FIVE_MINUTES: TimeWindow = TimeWindow { + end_offset_ms: 0, + width_ms: 300_000, + }; + + /// `(t - 6m, t - 1m]` and `(t - 5m, t]` are both held by `(t - 6m, t]`. + #[test] + fn the_union_of_two_windows_holds_both() { + let shifted = TimeWindow { + end_offset_ms: 60_000, + width_ms: 300_000, + }; + assert_eq!(shifted.read_back_ms(), 360_000); + assert_eq!( + shifted.union(FIVE_MINUTES), + TimeWindow { + end_offset_ms: 0, + width_ms: 360_000 + } + ); + assert_eq!(shifted.bounds_ms(1_000_000), (640_000, 940_000)); + } + + /// Nodes 0 (a scan) ← 1 `TimeRange(5m)`, and 0 ← 2 `TimeShift(1h)` ← 3 + /// `TimeRange(5m)`; node 4, a reader at the evaluation time, reads 0 + /// too. The scan is read through `{0, 5m}` and `{1h, 5m}`; the reader + /// at the evaluation time reads raw data whole. + #[test] + fn readers_read_a_producer_through_the_shifts_above_it() { + let readers = |node: u32| -> Vec { + match node { + 0 => vec![1, 2], + 2 => vec![3], + _ => vec![], + } + }; + let read_of = |node: u32| match node { + 1 | 3 => TimeIndexedRead::Window { width_ms: 300_000 }, + 2 => TimeIndexedRead::Through { + offset_ms: 3_600_000, + }, + _ => TimeIndexedRead::AtEvaluationTime, + }; + let mut windows = windows_read_above(0, readers, read_of, true).unwrap(); + windows.sort(); + assert_eq!( + windows, + vec![ + (1, FIVE_MINUTES), + ( + 3, + TimeWindow { + end_offset_ms: 3_600_000, + width_ms: 300_000 + } + ) + ] + ); + let with_reader_at_evaluation = |node: u32| match node { + 0 => vec![1, 4], + _ => vec![], + }; + assert_eq!( + windows_read_above(0, with_reader_at_evaluation, read_of, true), + None + ); + assert_eq!( + windows_read_above(0, with_reader_at_evaluation, read_of, false) + .map(|windows| windows.len()), + Some(2) + ); + } +} diff --git a/crates/types/tests/physical_asap_dag.rs b/crates/types/tests/physical_asap_dag.rs index 6d36f0330..0e20aa9c6 100644 --- a/crates/types/tests/physical_asap_dag.rs +++ b/crates/types/tests/physical_asap_dag.rs @@ -261,14 +261,11 @@ fn physical_node_ids_serialize_in_deterministic_binding_order() { /// A window 600,000 ms wide ending 300,000 ms before T = 3,600,000 covers /// `(2,700,000, 3,300,000]`. #[test] -fn a_raw_data_window_ends_its_offset_before_the_evaluation() { - let window = asap_types::plan::materialized_view::RawDataWindow::new( - Source::TimeSeries { - metric: "latency".into(), - }, - 300_000, - 600_000, - ); +fn a_time_window_ends_its_offset_before_the_evaluation() { + let window = asap_types::plan::TimeWindow { + end_offset_ms: 300_000, + width_ms: 600_000, + }; assert_eq!(window.bounds_ms(3_600_000), (2_700_000, 3_300_000)); } From 397c914bf820267adc2c9884b8fc8932000918b6 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:10:05 +0000 Subject: [PATCH 09/31] refactor(cost,stage3): Stage 3 costs candidates with asap-cost's rules, not copies of them (#1207 PR 6) Stage 3's `cost.rs` re-implemented four `plan_cost` rules; each now lives once in asap-cost and both call it: - `plan::execution_demand::executions_per_second(timing, lifetime, reaching entries, horizon)`: once per second at ingestion time, the queries' combined rate at query time, once per request when materialized across evaluations (`computations` and Stage 3's `cost_node`). - `maintained_window_ms(node, operator_of, input_of)`: the window an ingestion-time grouping maintains, over plan nodes or memo groups (replaces `plan_cost`'s private copy and Stage 3's `window_ms_below`). - `NodeResourceUsageAndCost::producer_output(payload, schema)`: what a consumer reads (replaces three field-by-field constructions). - the raw-data disk charge (`raw_data_disk_cost_per_second`, previous commit). Also: - `PhysicalASAPDAGNode::retains_rows()` replaces Stage 3's `retains_rows` and its inline copies. - `node_resource_usage_and_cost` reads the calibration factor from `settings` (its `formula_scale` parameter was always that); `computations` takes `entries` (drops `recurrence`, `range_evaluations` and the always- `None` `root_rates`); `node_map`/`NodeMap` move to `plan/cost.rs`, their only user; a scan's retained lookback is `node.retention_ms` directly. - Stage 3 computes each candidate's logical inputs once and reuses them for every edge into it, and reads the readable producers through a set. - Module docs: asap-cost no longer says Stage 3 "computes no cost of its own"; they name the shared rules. --- crates/cost/src/lib.rs | 19 +- crates/cost/src/node/usage/mod.rs | 144 +++++++++----- crates/cost/src/node/usage/tests.rs | 29 +-- crates/cost/src/plan/cost.rs | 109 +++-------- crates/cost/src/plan/execution_demand.rs | 80 +++++--- crates/stage3-plan-selection/src/cost.rs | 213 +++++++-------------- crates/stage3-plan-selection/src/lib.rs | 2 - crates/stage3-plan-selection/src/plan.rs | 4 +- crates/types/src/plan/materialized_view.rs | 12 ++ 9 files changed, 270 insertions(+), 342 deletions(-) diff --git a/crates/cost/src/lib.rs b/crates/cost/src/lib.rs index c1339910a..540ed3034 100644 --- a/crates/cost/src/lib.rs +++ b/crates/cost/src/lib.rs @@ -9,17 +9,20 @@ //! their lookup) and `resources` (resource dimensions with explicit CPU //! units). //! - The whole plan ([`plan`]): [`plan::execution_demand`] (how often each -//! node of a physical DAG is computed), its cost, retention and ranking. +//! node of a physical DAG is computed), its cost, how far back its +//! time-indexed nodes are read and how plans rank. //! - Cost models (`model`): [`ResourceUsage`], [`ResourceUnitCost`], //! [`ResourceSpeed`], [`CostModel`] ([`ClusterLimits`], //! [`ClusterObjective`]) and the deployment's [`CostInputs`], with the //! per-node and per-plan costs ([`NodeCost`], [`PlanCost`]). //! - Costing a physical plan DAG: [`node_resource_usage_and_cost`] gives one node's -//! [`NodeResourceUsageAndCost`]; [`plan_cost`] the whole plan's [`PlanCost`]. Stage 3 -//! computes no cost of its own. -//! -//! Plan selection (Stage 3, `asap-stage3-plan-selection`) documents how it prices -//! a candidate with these. +//! [`NodeResourceUsageAndCost`]; [`plan_cost`] the whole plan's [`PlanCost`]. +//! Stage 3 (`asap-stage3-plan-selection`) costs each candidate as one node +//! with the same function and the rules `plan_cost` applies: +//! [`plan::execution_demand::executions_per_second`], +//! [`maintained_window_ms`], [`read_back_ms`], +//! [`raw_data_disk_cost_per_second`] and +//! [`request_overhead_cost_per_second`]. mod measured; mod model; pub mod node; @@ -37,8 +40,8 @@ pub use model::{ }; pub use node::operator_statistics::EdgeStatistics; pub use node::usage::{ - node_resource_usage_and_cost, summary_type_name, NodeCost, NodeResourceUsageAndCost, - ProducerOutput, + maintained_window_ms, node_resource_usage_and_cost, summary_type_name, NodeCost, + NodeResourceUsageAndCost, ProducerOutput, }; pub use plan::cost::{ plan_cost, raw_data_disk_cost_per_second, request_overhead_cost_per_second, PlanCost, diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index 1cdff4e30..84064141e 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -1,7 +1,7 @@ //! One node of a physical plan DAG: its resource usage and cost //! ([`node_resource_usage_and_cost`], [`NodeCost`]). -use std::collections::{BTreeMap, BTreeSet, HashMap}; +use std::collections::{BTreeMap, BTreeSet}; use crate::node::analytical::{ estimate_operator, AnalyticalCostError, PhysicalOperator, ResourceEstimate, @@ -20,9 +20,9 @@ use crate::{ use asap_types::ir::operator::{GroupKeys, Reduction, Source}; use asap_types::ir::properties::ExecutionTiming; use asap_types::ir::schema::{FieldDataType, Schema}; -use asap_types::ir::{ASAPOp, NonASAPOp, TimeRangeKind}; +use asap_types::ir::{ASAPOp, NonASAPOp, Operator, TimeRangeKind}; use asap_types::plan::physical_asap_dag::{ - PhysicalASAPDAG, PhysicalASAPNodeId, PhysicalASAPOperatorPayload as Payload, + PhysicalASAPNodeId, PhysicalASAPOperatorPayload as Payload, }; use asap_types::stages::{Lifetime, StorageMedium}; use asap_types::summary_type::{SketchParams, SketchStatistic}; @@ -151,17 +151,77 @@ pub struct NodeResourceUsageAndCost<'a> { pub cost: NodeCost, } +impl<'a> NodeResourceUsageAndCost<'a> { + /// What a consumer reads of the node with `payload` and + /// `output_schema` that uses this. + pub fn producer_output( + &self, + payload: &'a Payload, + output_schema: &'a Schema, + ) -> ProducerOutput<'a> { + ProducerOutput { + payload, + output_schema, + output: self.output, + sources: self.sources.clone(), + family: self.family, + unchanged_columns: self.unchanged_columns.clone(), + tumbling_windows: self.tumbling_windows, + } + } +} + +/// The window an ingestion-time grouping `node` maintains, in ms: a +/// tumbling-window chain's tumbling window, a `TimeRange`'s range, or the +/// first `TimeRange` below it through filters, projections and time shifts +/// (`input_of` gives a node's first input, `operator_of` its operator). +/// `None` when there is none. Over plan nodes ([`plan_cost`]) or memo +/// groups (Stage 3, before any plan exists). +/// +/// Example: `Aggregate(Sum) <- Filter <- TimeRange(1m) <- Scan`: 60,000. +/// +/// [`plan_cost`]: crate::plan_cost +pub fn maintained_window_ms<'o, N: Copy + Ord, C: 'o>( + node: N, + operator_of: impl Fn(N) -> &'o Operator, + input_of: impl Fn(N) -> Option, +) -> Option { + let range_ms = |range: &std::time::Duration| u64::try_from(range.as_millis()).ok(); + match operator_of(node) { + Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { + tumbling_window_width_ms, + .. + }) => return Some(*tumbling_window_width_ms), + Operator::NonASAP(NonASAPOp::TimeRange { range, .. }) => return range_ms(range), + _ => {} + } + let mut seen = BTreeSet::new(); + let mut below = input_of(node)?; + while seen.insert(below) { + match operator_of(below) { + Operator::NonASAP(NonASAPOp::TimeRange { range, .. }) => return range_ms(range), + Operator::NonASAP( + NonASAPOp::Filter { .. } | NonASAPOp::Project { .. } | NonASAPOp::TimeShift { .. }, + ) => below = input_of(below)?, + _ => return None, + } + } + None +} + /// Measure and price one node of a physical DAG (one step of -/// [`plan_cost`]): `producers` are its producers in edge order, -/// `rate` its `(multiplier, per_second)` ([`computations`]), and -/// `window_ms` the time window one execution covers: the window an -/// ingestion-time node maintains, or a query-time `Scan`'s read, the most -/// offset + range its consumers read ([`read_back_ms`] without a span; -/// `None`: it reads what it retains) -/// (`maintained_window_ms`). A materialized node keeps what its +/// [`plan_cost`]; Stage 3 calls it per candidate): `producers` are its +/// producers in edge order, `rate` its `(multiplier, per_second)` +/// ([`computations`], [`executions_per_second`]), and `window_ms` the time +/// window one execution covers: the window an ingestion-time node +/// maintains ([`maintained_window_ms`]), or a query-time `Scan`'s read, the +/// most offset + range its consumers read ([`read_back_ms`] without a span; +/// `None`: it reads what it retains). A materialized node keeps what its /// `retention_ms` ($h_v$) covers: a tumbling-window chain its windows in -/// it, a query-time `Scan` its raw rows in it. -#[allow(clippy::too_many_arguments)] +/// it, a query-time `Scan` its raw rows in it. The formulas' CPU is scaled +/// by `settings.frozen_calibration_factor` (1 when unset). +/// +/// [`executions_per_second`]: crate::plan::execution_demand::executions_per_second pub fn node_resource_usage_and_cost<'a>( node: &'a asap_types::plan::physical_asap_dag::PhysicalASAPDAGNode, producers: &[ProducerOutput<'a>], @@ -169,8 +229,9 @@ pub fn node_resource_usage_and_cost<'a>( window_ms: Option, data: &DataWorkload, settings: &'a CostInputs, - formula_scale: f64, ) -> Result, AnalyticalCostError> { + // The formulas' CPU scaled by the deployment's calibration, 1 unset. + let formula_scale = settings.frozen_calibration_factor.unwrap_or(1.0); let table = &settings.measured_operation_costs; let speed = settings.resource_speed; let usage_prices = match &settings.cost_model { @@ -202,6 +263,16 @@ pub fn node_resource_usage_and_cost<'a>( _ => 1, }; let inputs: Vec = producers.iter().map(|p| p.output).collect(); + // The tumbling windows its rows span: those a `TimeRange` selects from + // a chain, 1 otherwise. + let tumbling_windows = match &node.payload { + Payload::NonASAP(NonASAPOp::TimeRange { range, .. }) + if chain_width_ms(producers).is_some() => + { + selected_tumbling_windows(range, producers)? + } + _ => 1, + }; let sources: BTreeSet<&str> = match &node.payload { Payload::NonASAP(NonASAPOp::Scan { source, .. }) => [source_name(source)].into(), _ => producers @@ -332,35 +403,25 @@ pub fn node_resource_usage_and_cost<'a>( // without a window). let source = source_name(source); let rows_held = data.sources.get(source).and_then(|facts| facts.rows_held); - let (window_width_ms, retained_lookback_ms): (Option, Option) = - match ingestion { - true => (None, None), - // One execution reads the most its consumers read - // (`window_ms`), else what the scan retains (its - // `retention_ms`, $h_v$); `None` reads every row - // held. It keeps `retention_ms`. - false => (window_ms.or(node.retention_ms), node.retention_ms), - }; + // At ingestion, one second of rows; at query time, one + // execution reads the most its consumers read (`window_ms`), + // else what the scan retains (its `retention_ms`, $h_v$); + // `None` reads every row held. let lookback = match ingestion { true => Some(1_000), - false => window_width_ms, + false => window_ms.or(node.retention_ms), }; let by_rate = |lookback: Option| { rows_per_ms.zip(lookback).map(|(rows_per_ms, lookback)| { (rows_per_ms * lookback as f64).round() as u64 }) }; - // The raw rows retained for a query-time scan: back to the - // oldest row it reads, `end_offset + width` (an offset + // The raw rows a query-time scan, or an ingestion-time scan + // materialized across evaluations, keeps: back to the + // oldest row read, its `retention_ms` ($h_v$; an offset // window keeps more than it reads), at most those held. - // So are those an ingestion-time scan materialized across - // evaluations keeps: back `retention_ms` ($h_v$). - let retained_lookback_ms = match ingestion { - true => node.retention_ms, - false => retained_lookback_ms, - }; if !ingestion || matches!(node.lifetime, Lifetime::AcrossEvaluations(_)) { - let retained_rows = match (by_rate(retained_lookback_ms), rows_held) { + let retained_rows = match (by_rate(node.retention_ms), rows_held) { (Some(by_rate), Some(rows_held)) => Some(by_rate.min(rows_held)), (by_rate, rows_held) => by_rate.or(rows_held), }; @@ -511,7 +572,7 @@ pub fn node_resource_usage_and_cost<'a>( NonASAPOp::TimeRange { range, .. } if chain_width_ms(producers).is_some() => { // Over a chain, the windows inside the range: one // window's states per window. - let windows = selected_tumbling_windows(range, producers)?; + let windows = tumbling_windows; let out = EdgeStatistics { rows: input .rows @@ -893,14 +954,6 @@ pub fn node_resource_usage_and_cost<'a>( if cost.is_some_and(|cost| !cost.is_finite()) { return Err(AnalyticalCostError::Overflow); } - let tumbling_windows = match &node.payload { - Payload::NonASAP(NonASAPOp::TimeRange { range, .. }) - if chain_width_ms(producers).is_some() => - { - selected_tumbling_windows(range, producers)? - } - _ => 1, - }; Ok(NodeResourceUsageAndCost { output: out, sources, @@ -974,15 +1027,6 @@ fn merged_states(producers: &[ProducerOutput<'_>]) -> Result NodeMap<'_> { - dag.nodes.iter().map(|n| (n.id, n)).collect() -} - -/// The nodes of a physical DAG by id. -pub(crate) type NodeMap<'a> = - HashMap; - /// A summary type's name in reports: a sketch by its algorithm, e.g. /// `HydraCms`; an exact accumulator by its kind. pub fn summary_type_name(summary_type: &FieldDataType) -> String { diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index 1e3005fe4..5942ec575 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -29,15 +29,7 @@ fn produced<'a>( node: &'a PhysicalASAPDAGNode, usage: &NodeResourceUsageAndCost<'a>, ) -> ProducerOutput<'a> { - ProducerOutput { - payload: &node.payload, - output_schema: &node.output_schema, - output: usage.output, - sources: usage.sources.clone(), - family: usage.family, - unchanged_columns: usage.unchanged_columns.clone(), - tumbling_windows: usage.tumbling_windows, - } + usage.producer_output(&node.payload, &node.output_schema) } /// A merge of the five 60 s windows a 5m `TimeRange` selects from a @@ -72,16 +64,9 @@ fn a_merge_over_a_chain_s_windows_outputs_one_state_per_group() { kind: TimeRangeKind::Range, child: 0, })); - let windows_usage = node_resource_usage_and_cost( - &windows, - &[chain_output], - (1, 1.0), - None, - &data, - &settings, - 1.0, - ) - .unwrap(); + let windows_usage = + node_resource_usage_and_cost(&windows, &[chain_output], (1, 1.0), None, &data, &settings) + .unwrap(); assert_eq!(windows_usage.output.rows, 5_000); assert_eq!(windows_usage.tumbling_windows, 5); let merge = node(Payload::ASAP(ASAPOp::SummaryMerge { @@ -95,7 +80,6 @@ fn a_merge_over_a_chain_s_windows_outputs_one_state_per_group() { None, &data, &settings, - 1.0, ) .unwrap(); assert_eq!(merge_usage.output.rows, 1_000); @@ -145,8 +129,7 @@ fn query_time_memory_is_held_only_while_the_node_computes() { })) }; let usage = - node_resource_usage_and_cost(&merge, &[states], (1, 0.1), None, &data, &settings, 1.0) - .unwrap(); + node_resource_usage_and_cost(&merge, &[states], (1, 0.1), None, &data, &settings).unwrap(); let held: u64 = usage .cost .detail @@ -208,7 +191,7 @@ fn a_query_time_scan_reads_what_its_consumers_read_and_keeps_its_retention() { ); assert_eq!(read_ms, Some(1_000)); let usage = - node_resource_usage_and_cost(&scan, &[], (1, 1.0), read_ms, &data, &settings, 1.0).unwrap(); + node_resource_usage_and_cost(&scan, &[], (1, 1.0), read_ms, &data, &settings).unwrap(); assert_eq!(usage.output.rows, 1_000); let (_, kept_bytes) = usage.raw_scan.expect("a query-time scan keeps raw data"); assert_eq!(kept_bytes, usage.output.bytes * 3_601); diff --git a/crates/cost/src/plan/cost.rs b/crates/cost/src/plan/cost.rs index 47fe22af7..a41a39b9b 100644 --- a/crates/cost/src/plan/cost.rs +++ b/crates/cost/src/plan/cost.rs @@ -1,15 +1,17 @@ //! Costing a physical plan DAG node by node: each node's resource usage //! ([`node_resource_usage_and_cost`]), then the plan's usage, latency and -//! cost ([`plan_cost`]). Moved from Stage 3 (`asap-stage3-plan-selection`, -//! whose module doc, "Cost model", describes the model), so Stage 3 computes -//! no cost of its own. +//! cost ([`plan_cost`]). Stage 3 costs its candidates with the same +//! function and rules (`executions_per_second`, `maintained_window_ms`, +//! `read_back_ms`, [`raw_data_disk_cost_per_second`], +//! [`request_overhead_cost_per_second`]), so its objective is the +//! `plan_cost` of the plan it selects. use std::collections::{BTreeMap, HashMap, HashSet}; use crate::node::analytical::AnalyticalCostError; use crate::node::operator_statistics::EdgeStatistics; use crate::node::state_bytes::{sketch_shape, StateInputRows}; -use crate::node::usage::{cost_source, node_map, NodeMap}; +use crate::node::usage::{cost_source, maintained_window_ms}; use crate::plan::execution_demand::{computations, evaluation_rate, query_starts}; use crate::plan::read_back::read_back_ms; use crate::{lookup, StateFamily}; @@ -20,14 +22,14 @@ use crate::{ }; use asap_types::ir::operator::Source; use asap_types::ir::properties::ExecutionTiming; -use asap_types::ir::{ASAPOp, NonASAPOp}; +use asap_types::ir::NonASAPOp; use asap_types::plan::physical_asap_dag::{ PhysicalASAPDAG, PhysicalASAPDAGNode, PhysicalASAPNodeId, PhysicalASAPOperatorPayload as Payload, }; use asap_types::plan::time_window::TimeIndexedRead; use asap_types::summary_type::SketchParams; -use asap_types::workload::{DataWorkload, QueryRecurrence, QueryWorkloadEntry, RangeEvaluation}; +use asap_types::workload::{DataWorkload, QueryWorkloadEntry}; /// One candidate's usage and price over the whole workload: `usage` is /// the whole candidate's, `per_node` one price per operator node of its @@ -88,14 +90,6 @@ pub fn plan_cost( settings: &CostInputs, ) -> Result<(PlanCost, CalibrationFit), (PhysicalASAPNodeId, AnalyticalCostError)> { let nodes = &node_map(dag); - let recurrence: &[QueryRecurrence] = &entries - .iter() - .map(|entry| entry.recurrence.clone()) - .collect::>(); - let range_evaluations: &[Option] = &entries - .iter() - .map(|entry| entry.range_evaluation) - .collect::>(); let formula_scale = settings.frozen_calibration_factor.unwrap_or(1.0); let first = dag.nodes.first().map(|n| n.id).unwrap_or_default(); let table = &settings.measured_operation_costs; @@ -109,16 +103,7 @@ pub fn plan_cost( let EachNodeResourceUsageAndCost { priced, multipliers, - } = each_node_resource_usage_and_cost( - dag, - nodes, - recurrence, - range_evaluations, - data, - settings, - formula_scale, - None, - )?; + } = each_node_resource_usage_and_cost(dag, nodes, entries, data, settings)?; let mut output: HashMap = HashMap::new(); let mut per_node = BTreeMap::new(); // The candidate's usage, accumulated node by node; peak cores and @@ -384,30 +369,18 @@ struct EachNodeResourceUsageAndCost<'a> { multipliers: HashMap, } -/// [`node_resource_usage_and_cost`] over every node of `dag`, children first, with the -/// arguments of [`plan_cost`]. `root_rates`, when given, are -/// the computations per second of each root's consumer in place of its -/// query's own rate ([`computations`]). -#[allow(clippy::too_many_arguments)] +/// [`node_resource_usage_and_cost`] over every node of `dag`, children +/// first, with the arguments of [`plan_cost`]. fn each_node_resource_usage_and_cost<'a>( dag: &'a PhysicalASAPDAG, nodes: &NodeMap<'a>, - recurrence: &[QueryRecurrence], - range_evaluations: &[Option], + entries: &[QueryWorkloadEntry], data: &DataWorkload, settings: &'a CostInputs, - formula_scale: f64, - root_rates: Option<&[f64]>, ) -> Result, (PhysicalASAPNodeId, AnalyticalCostError)> { let first = dag.nodes.first().map(|n| n.id).unwrap_or_default(); - let multipliers = computations( - dag, - recurrence, - range_evaluations, - settings.non_repeating_query_horizon_seconds, - root_rates, - ) - .map_err(|error| (first, error))?; + let multipliers = computations(dag, entries, settings.non_repeating_query_horizon_seconds) + .map_err(|error| (first, error))?; let mut producers_of: HashMap> = HashMap::new(); let mut consumers_of: HashMap> = HashMap::new(); for e in &dag.edges { @@ -420,21 +393,14 @@ fn each_node_resource_usage_and_cost<'a>( .get(&node.id) .map_or(&[][..], Vec::as_slice) .iter() - .map(|p| { - let produced: &NodeResourceUsageAndCost<'a> = &priced[p]; - ProducerOutput { - payload: &nodes[p].payload, - output_schema: &nodes[p].output_schema, - output: produced.output, - sources: produced.sources.clone(), - family: produced.family, - unchanged_columns: produced.unchanged_columns.clone(), - tumbling_windows: produced.tumbling_windows, - } - }) + .map(|p| priced[p].producer_output(&nodes[p].payload, &nodes[p].output_schema)) .collect(); let window_ms = match node.output_state.timing { - ExecutionTiming::IngestionTime => maintained_window_ms(node.id, nodes, &producers_of), + ExecutionTiming::IngestionTime => maintained_window_ms( + node.id, + |id| &nodes[&id].payload, + |id| producers_of.get(&id)?.first().copied(), + ), ExecutionTiming::QueryTime => scan_read_ms(node, nodes, &consumers_of), }; let node_priced = node_resource_usage_and_cost( @@ -444,7 +410,6 @@ fn each_node_resource_usage_and_cost<'a>( window_ms, data, settings, - formula_scale, ) .map_err(|error| (node.id, error))?; priced.insert(node.id, node_priced); @@ -584,32 +549,10 @@ fn ingest_workers( (ingest_ms_per_s / 1_000.0).ceil().max(1.0) as u64 } -/// The window an ingestion-time grouping `node` maintains: a tumbling-window chain's -/// tumbling windows, or the time range below it (through filters, projections and -/// time shifts). `None` when there is none. -fn maintained_window_ms( - node: PhysicalASAPNodeId, - nodes: &NodeMap<'_>, - producers_of: &HashMap>, -) -> Option { - match &nodes[&node].payload { - Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { - tumbling_window_width_ms, - .. - }) => return Some(*tumbling_window_width_ms), - Payload::NonASAP(NonASAPOp::TimeRange { range, .. }) => { - return Some(range.as_millis() as u64) - } - _ => {} - } - let producer = *producers_of.get(&node)?.first()?; - match &nodes[&producer].payload { - Payload::NonASAP( - NonASAPOp::TimeRange { .. } - | NonASAPOp::Filter { .. } - | NonASAPOp::Project { .. } - | NonASAPOp::TimeShift { .. }, - ) => maintained_window_ms(producer, nodes, producers_of), - _ => None, - } +/// The nodes of `dag` by id. +fn node_map(dag: &PhysicalASAPDAG) -> NodeMap<'_> { + dag.nodes.iter().map(|n| (n.id, n)).collect() } + +/// The nodes of a physical DAG by id. +type NodeMap<'a> = HashMap; diff --git a/crates/cost/src/plan/execution_demand.rs b/crates/cost/src/plan/execution_demand.rs index 02a00ab02..55b7cdfc5 100644 --- a/crates/cost/src/plan/execution_demand.rs +++ b/crates/cost/src/plan/execution_demand.rs @@ -11,7 +11,7 @@ use crate::node::analytical::AnalyticalCostError; use asap_types::ir::properties::ExecutionTiming; use asap_types::plan::physical_asap_dag::{PhysicalASAPDAG, PhysicalASAPNodeId}; use asap_types::stages::Lifetime; -use asap_types::workload::{QueryRecurrence, RangeEvaluation, RepeatedDemand}; +use asap_types::workload::{QueryRecurrence, QueryWorkloadEntry, RangeEvaluation, RepeatedDemand}; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; /// How a [`ScheduleGroup`]'s evaluations add to the others'. @@ -250,6 +250,37 @@ pub fn query_starts(dag: &PhysicalASAPDAG) -> Vec> { .collect() } +/// How many times per second a node computed once for the queries +/// `reaching` it runs, at `timing` with `lifetime`: at ingestion time once +/// per second (each arriving row once); at query time at their combined +/// rate ([`evaluation_rate`]), a range query's steps each an evaluation, +/// except a node materialized across evaluations, which is computed once +/// per request (its newest window). Shared by [`computations`] and +/// Stage 3, which costs each candidate at the queries reaching its group. +/// +/// Example: two queries every 60 s at phase 0 reaching a query-time +/// `OneEvaluation` node: 1/60 per second. +pub fn executions_per_second<'e>( + timing: ExecutionTiming, + lifetime: Lifetime, + reaching: impl IntoIterator, + non_repeating_query_horizon_seconds: f64, +) -> Result { + if timing == ExecutionTiming::IngestionTime { + return Ok(1.0); + } + let per_request = matches!(lifetime, Lifetime::AcrossEvaluations(_)); + evaluation_rate( + reaching.into_iter().map(|entry| { + ( + &entry.recurrence, + (!per_request).then_some(entry.range_evaluation).flatten(), + ) + }), + non_repeating_query_horizon_seconds, + ) +} + /// What computes a not-materialized node once per its own execution /// ([`consumers`]). #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] @@ -309,8 +340,9 @@ pub fn consumers(dag: &PhysicalASAPDAG) -> HashMap HashMap], + entries: &[QueryWorkloadEntry], non_repeating_query_horizon_seconds: f64, - root_rates: Option<&[f64]>, ) -> Result, AnalyticalCostError> { - let range_of = |query: usize| range_evaluations.get(query).copied().flatten(); // Each materialized node's timing and lifetime. let materialized_nodes: HashMap = dag .nodes @@ -356,31 +385,28 @@ pub fn computations( } let mut rate_of_materialized = HashMap::new(); for (&id, &(timing, lifetime)) in &materialized_nodes { - let at_ingestion = timing == ExecutionTiming::IngestionTime; - // A query-time chain materialized across evaluations is built once per request. - let per_request = !at_ingestion && matches!(lifetime, Lifetime::AcrossEvaluations(_)); - let rate = match at_ingestion { - true => 1.0, - _ => evaluation_rate( - reached.get(&id).into_iter().flatten().filter_map(|&q| { - let range = if per_request { None } else { range_of(q) }; - recurrence.get(q).map(|r| (r, range)) - }), - non_repeating_query_horizon_seconds, - )?, - }; + let reaching = reached + .get(&id) + .into_iter() + .flatten() + .filter_map(|&q| entries.get(q)); + let rate = executions_per_second( + timing, + lifetime, + reaching, + non_repeating_query_horizon_seconds, + )?; rate_of_materialized.insert(id, rate); } // Each query consumer's rate. let mut query_rates = Vec::new(); for query in 0..starts.len() { - query_rates.push(match root_rates.and_then(|rates| rates.get(query)) { - Some(&rate) => rate, - None => evaluation_rate( - recurrence.get(query).map(|r| (r, range_of(query))), - non_repeating_query_horizon_seconds, - )?, - }); + query_rates.push(evaluation_rate( + entries + .get(query) + .map(|entry| (&entry.recurrence, entry.range_evaluation)), + non_repeating_query_horizon_seconds, + )?); } let mut count: HashMap<_, (u64, f64)> = HashMap::new(); for (id, of_node) in consumers(dag) { diff --git a/crates/stage3-plan-selection/src/cost.rs b/crates/stage3-plan-selection/src/cost.rs index f0b4a7a3e..4f2159832 100644 --- a/crates/stage3-plan-selection/src/cost.rs +++ b/crates/stage3-plan-selection/src/cost.rs @@ -23,11 +23,11 @@ //! query-time `Scan` retain the most their selectable consumers read back //! ([`retention_need_ms`]). Execution contexts make both per context. -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; -use asap_cost::plan::execution_demand::evaluation_rate; +use asap_cost::plan::execution_demand::executions_per_second; use asap_cost::{ - node_resource_usage_and_cost, raw_data_disk_cost_per_second, CostInputs, + maintained_window_ms, node_resource_usage_and_cost, raw_data_disk_cost_per_second, CostInputs, NodeResourceUsageAndCost, ProducerOutput, }; use asap_types::ir::operator::Source; @@ -114,26 +114,6 @@ pub(crate) fn input_count(physical: &PhysicalASAPMemoCandidates, v: PhysicalCand .len() } -/// Whether candidate `v` keeps time-indexed rows for its consumers to read -/// back: materialized across evaluations, or a `Scan` at query time (raw -/// data). Its node carries `retention_ms`. -pub(crate) fn retains_rows(physical: &PhysicalASAPMemoCandidates, v: PhysicalCandidateId) -> bool { - let candidate = &physical.physical_candidates[v.0]; - let scan = matches!( - physical - .logical_candidates - .memo - .candidate(candidate.logical_candidate_id) - .expression - .operator, - Operator::NonASAP(NonASAPOp::Scan { .. }) - ); - matches!( - candidate.physical_properties.lifetime(), - Lifetime::AcrossEvaluations(_) - ) || (scan && candidate.physical_properties.timing() == ExecutionTiming::QueryTime) -} - /// How far back from the evaluation time `producer`'s rows must be kept /// for its readers (`readers_of`, the candidates that may read it): /// [`asap_cost::read_back_ms`] with, at each reader, the longest @@ -212,7 +192,6 @@ pub(crate) fn selection_costs( entries: &[QueryWorkloadEntry], data: &DataWorkload, cost_inputs: &CostInputs, - formula_scale: f64, request_overhead_cost_per_second: f64, ) -> SelectionCosts { let memo = &physical.logical_candidates.memo; @@ -234,7 +213,7 @@ pub(crate) fn selection_costs( .map(|v| { let inputs: Vec = (0..input_count(physical, v)).collect(); let mut node = candidate_node(physical, v, v.0, &inputs); - if retains_rows(physical, v) { + if node.retains_rows() { node.retention_ms = retention_need_ms(physical, entries, v, readers_of); } node @@ -242,18 +221,14 @@ pub(crate) fn selection_costs( .collect(); // Each query-time `Scan`'s read per execution: the most any selectable // consumer reads (no range-evaluation span), charged once. - let scan_read: Vec> = (0..count) - .map(PhysicalCandidateId) - .map(|v| { - let candidate = &physical.physical_candidates[v.0]; - let scan = matches!( - memo.candidate(candidate.logical_candidate_id) - .expression - .operator, - Operator::NonASAP(NonASAPOp::Scan { .. }) - ); - (scan && candidate.physical_properties.timing() == ExecutionTiming::QueryTime) - .then(|| scan_read_ms(physical, v, readers_of)) + let scan_read_ms_of: Vec> = nodes + .iter() + .enumerate() + .map(|(v, node)| { + let query_time_scan = node.output_state.timing == ExecutionTiming::QueryTime + && matches!(node.payload, Operator::NonASAP(NonASAPOp::Scan { .. })); + query_time_scan + .then(|| scan_read_ms(physical, PhysicalCandidateId(v), readers_of)) .flatten() }) .collect(); @@ -274,23 +249,25 @@ pub(crate) fn selection_costs( .unwrap_or_else(|| (0..memo.groups().len() as u32).map(MemoGroupId).collect()); let mut usage: Vec, String>>> = (0..count).map(|_| None).collect(); + // Each candidate's logical inputs, as costed for $w_v$; each edge into + // it replaces one. + let mut inputs_of: Vec>>> = (0..count).map(|_| None).collect(); for group in order { for &v in &candidates_of_group[group.0 as usize] { let inputs = logical_inputs(physical, &group_payloads, &candidates_of_group, &usage, v); - usage[v.0] = Some(match inputs { + usage[v.0] = Some(match &inputs { Some(inputs) => cost_node( physical, - v, &nodes[v.0], - &inputs, + inputs, entries, data, cost_inputs, - formula_scale, - scan_read[v.0], + scan_read_ms_of[v.0], ), None => Err("an input group has no costable candidate".to_string()), }); + inputs_of[v.0] = inputs; } } let candidate: Vec> = usage @@ -316,22 +293,19 @@ pub(crate) fn selection_costs( let (Some(Ok(produced)), Ok(own)) = (&usage[u.0], &candidate[c.0]) else { continue; }; - let Some(mut inputs) = - logical_inputs(physical, &group_payloads, &candidates_of_group, &usage, c) - else { + let Some(mut inputs) = inputs_of[c.0].clone() else { continue; }; - inputs[e.input_index_of_consumer] = producer_output(&nodes[u.0], produced); + inputs[e.input_index_of_consumer] = + produced.producer_output(&nodes[u.0].payload, &nodes[u.0].output_schema); let with_u = cost_node( physical, - c, &nodes[c.0], &inputs, entries, data, cost_inputs, - formula_scale, - scan_read[c.0], + scan_read_ms_of[c.0], ) .and_then(|usage| cost_per_second(&usage)); if let Ok(with_u) = with_u { @@ -362,7 +336,7 @@ fn logical_inputs<'a>( .into_iter() .enumerate() .map(|(input, group)| { - let readable = memo.producers(consumer, input); + let readable: HashSet<_> = memo.producers(consumer, input).into_iter().collect(); logical_output( &group_payloads[group.0 as usize], &memo.groups()[group.0 as usize].group_properties.schema, @@ -392,22 +366,6 @@ fn input_groups( .collect() } -/// What a consumer reads of candidate `node` with `usage`. -fn producer_output<'a>( - node: &'a PhysicalASAPDAGNode, - usage: &NodeResourceUsageAndCost<'a>, -) -> ProducerOutput<'a> { - ProducerOutput { - payload: &node.payload, - output_schema: &node.output_schema, - output: usage.output, - sources: usage.sources.clone(), - family: usage.family, - unchanged_columns: usage.unchanged_columns.clone(), - tumbling_windows: usage.tumbling_windows, - } -} - /// An input's logical output over its readable costed candidates' `usages`: the most /// rows and bytes any outputs, the state family of the one with the most /// bytes, the sources any reads and the columns every one leaves unchanged; @@ -421,15 +379,7 @@ fn logical_output<'a: 'b, 'b>( let mut output: Option> = None; for usage in usages { let Some(known) = output.as_mut() else { - output = Some(ProducerOutput { - payload, - output_schema: schema, - output: usage.output, - sources: usage.sources.clone(), - family: usage.family, - unchanged_columns: usage.unchanged_columns.clone(), - tumbling_windows: usage.tumbling_windows, - }); + output = Some(usage.producer_output(payload, schema)); continue; }; if usage.output.bytes > known.output.bytes { @@ -445,68 +395,61 @@ fn logical_output<'a: 'b, 'b>( output } -/// Candidate `v`'s usage as `node` over `inputs`, at its rate (module doc); -/// a query-time `Scan` reads `scan_read_ms` per execution. -#[allow(clippy::too_many_arguments)] +/// The usage of the candidate `node` over `inputs` (module doc): run +/// [`executions_per_second`] for the queries reaching its group, over the +/// window an ingestion-time grouping maintains +/// ([`asap_cost::maintained_window_ms`], over the memo's groups) or, for a +/// query-time `Scan`, `scan_read_ms` per execution. fn cost_node<'a>( physical: &PhysicalASAPMemoCandidates, - v: PhysicalCandidateId, node: &'a PhysicalASAPDAGNode, inputs: &[ProducerOutput<'a>], entries: &[QueryWorkloadEntry], data: &DataWorkload, cost_inputs: &'a CostInputs, - formula_scale: f64, scan_read_ms: Option, ) -> Result, String> { let memo = &physical.logical_candidates.memo; - let candidate = &physical.physical_candidates[v.0]; - let group = candidate.logical_candidate_id.group; - let ingestion = candidate.physical_properties.timing() == ExecutionTiming::IngestionTime; - let per_second = match ingestion { - true => 1.0, - false => { - let per_request = matches!( - candidate.physical_properties.lifetime(), - Lifetime::AcrossEvaluations(_) - ); - evaluation_rate( - memo.groups()[group.0 as usize] - .reached_by_queries() - .iter() - .filter_map(|&query| entries.get(query)) - .map(|entry| { - ( - &entry.recurrence, - (!per_request).then_some(entry.range_evaluation).flatten(), - ) - }), - cost_inputs.non_repeating_query_horizon_seconds, - ) - .map_err(|error| error.to_string())? - } - }; - let window_ms = match (&node.payload, ingestion) { - (_, false) => scan_read_ms, - ( + let candidate = node + .selected_physical_candidate + .expect("a candidate's node names it"); + let group = physical.physical_candidates[candidate.0] + .logical_candidate_id + .group; + let per_second = executions_per_second( + node.output_state.timing, + node.lifetime, + memo.groups()[group.0 as usize] + .reached_by_queries() + .iter() + .filter_map(|&query| entries.get(query)), + cost_inputs.non_repeating_query_horizon_seconds, + ) + .map_err(|error| error.to_string())?; + let window_ms = match node.output_state.timing { + ExecutionTiming::QueryTime => scan_read_ms, + // A chain maintains its own window; any other grouping the first + // range below its group. + ExecutionTiming::IngestionTime => match &node.payload { Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { tumbling_window_width_ms, .. - }), - true, - ) => Some(*tumbling_window_width_ms), - (_, true) => window_ms_below(memo, group), + }) => Some(*tumbling_window_width_ms), + _ => maintained_window_ms( + group, + |group: MemoGroupId| &memo.groups()[group.0 as usize].expression, + |group| { + memo.groups()[group.0 as usize] + .expression + .children() + .first() + .map(|g| **g) + }, + ), + }, }; - node_resource_usage_and_cost( - node, - inputs, - (1, per_second), - window_ms, - data, - cost_inputs, - formula_scale, - ) - .map_err(|error| error.to_string()) + node_resource_usage_and_cost(node, inputs, (1, per_second), window_ms, data, cost_inputs) + .map_err(|error| error.to_string()) } /// $w_v$ from a candidate's usage: its cost per second under cost model 1 @@ -524,27 +467,3 @@ fn cost_per_second(usage: &NodeResourceUsageAndCost<'_>) -> Result )), } } - -/// The window, in ms, of the first range below `group`'s operator, -/// through filters, projections and time shifts: what an ingestion-time -/// grouping maintains (what asap-cost reads below a plan node, over the memo's -/// groups, since a candidate is costed before any plan exists). -fn window_ms_below(memo: &Memo, group: MemoGroupId) -> Option { - let range_ms = |range: &std::time::Duration| u64::try_from(range.as_millis()).ok(); - let expression = &memo.groups()[group.0 as usize].expression; - if let Operator::NonASAP(NonASAPOp::TimeRange { range, .. }) = expression { - return range_ms(range); - } - let mut below = **expression.children().first()?; - for _ in 0..memo.groups().len() { - let expression = &memo.groups()[below.0 as usize].expression; - match expression { - Operator::NonASAP(NonASAPOp::TimeRange { range, .. }) => return range_ms(range), - Operator::NonASAP( - NonASAPOp::Filter { .. } | NonASAPOp::Project { .. } | NonASAPOp::TimeShift { .. }, - ) => below = **expression.children().first()?, - _ => return None, - } - } - None -} diff --git a/crates/stage3-plan-selection/src/lib.rs b/crates/stage3-plan-selection/src/lib.rs index 23c809e15..eee6cc05c 100644 --- a/crates/stage3-plan-selection/src/lib.rs +++ b/crates/stage3-plan-selection/src/lib.rs @@ -74,7 +74,6 @@ pub fn select_plan( entries: entries.len(), }); } - let formula_scale = cost_inputs.frozen_calibration_factor.unwrap_or(1.0); let selectable: Vec = physical .edges() .filter(|(_, edge)| { @@ -95,7 +94,6 @@ pub fn select_plan( entries, data, cost_inputs, - formula_scale, request_overhead_cost_per_second, ); let roots: Vec> = diff --git a/crates/stage3-plan-selection/src/plan.rs b/crates/stage3-plan-selection/src/plan.rs index 34f2bc76f..8bd2bd51b 100644 --- a/crates/stage3-plan-selection/src/plan.rs +++ b/crates/stage3-plan-selection/src/plan.rs @@ -14,7 +14,7 @@ use asap_types::stages::{ }; use asap_types::workload::QueryWorkloadEntry; -use crate::cost::{candidate_node, input_count, retains_rows, retention_need_ms}; +use crate::cost::{candidate_node, input_count, retention_need_ms}; use crate::milp::model::{QueryId, SolvedSelection}; /// The plan of `solved`, whose output `j` answers `outputs[j]` (a query and @@ -99,7 +99,7 @@ pub(crate) fn plan_of( let input_ids: Vec = inputs(v).iter().map(|producer| node_of[producer]).collect(); let mut node = candidate_node(physical, v, id, &input_ids); - if retains_rows(physical, v) { + if node.retains_rows() { node.retention_ms = retention_need_ms(physical, entries, v, readers_of) .map(|need_ms| need_ms.max(output_span_ms.get(&v).copied().unwrap_or(0))); } diff --git a/crates/types/src/plan/materialized_view.rs b/crates/types/src/plan/materialized_view.rs index 648a67627..d776dc487 100644 --- a/crates/types/src/plan/materialized_view.rs +++ b/crates/types/src/plan/materialized_view.rs @@ -502,6 +502,18 @@ pub(crate) fn is_raw_data_scan(node: &PhysicalASAPDAGNode) -> bool { || matches!(node.lifetime, Lifetime::AcrossEvaluations(_))) } +impl PhysicalASAPDAGNode { + /// Whether it keeps time-indexed rows for its readers to read back, so + /// carries `retention_ms`: materialized across evaluations (a view, or + /// raw data kept at ingestion), or a raw-data `Scan` at query time. + /// + /// Example: a query-time `Scan` of `m` read by `TimeRange(5m)` keeps 5 + /// minutes of `m`; a `OneEvaluation` merge keeps nothing. + pub fn retains_rows(&self) -> bool { + matches!(self.lifetime, Lifetime::AcrossEvaluations(_)) || is_raw_data_scan(self) + } +} + /// The nodes of a DAG by id, each node's inputs and consumers in edge /// order, and two facts every raw-data window check reads, computed in one /// pass each over the DAG (O(nodes + edges)); it walks down from each From 63048945853dfa9afe1a0d9d0bbf40ef2c5df891 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:11:41 +0000 Subject: [PATCH 10/31] perf(stage3): build the selection MILP in one pass over each structure (#1207 PR 6) - y and z are `BTreeMap`s, in edge and key order once, instead of sorting y four times and z twice. - The strongly connected components of the selectable edges are computed once (`Components`), not three times. - The uses of each candidate are built once, and a query-time candidate that is not materialized gets one equality row, uses = x, for (F) and the one-consumer rule together (`use_constraints` replaces `utilization_constraints` and `unshared_result_constraints`): one row less per such candidate. Same model, same optima; `milp::model::tests` unchanged and passing. --- .../stage3-plan-selection/src/milp/model.rs | 178 +++++++++--------- 1 file changed, 86 insertions(+), 92 deletions(-) diff --git a/crates/stage3-plan-selection/src/milp/model.rs b/crates/stage3-plan-selection/src/milp/model.rs index 6c3d602e3..8710d469c 100644 --- a/crates/stage3-plan-selection/src/milp/model.rs +++ b/crates/stage3-plan-selection/src/milp/model.rs @@ -3,7 +3,7 @@ //! //! | Family | Variables | Constraints | //! |---|---|---| -//! | baseline (section 3) | $x_v$, $y_{u,c,i}$, $z_{q,v}$, $t_v$, $r_s$ ([`SelectionVariables`]) | (A) [`input_satisfaction_constraints`], (B) [`producer_selection_constraints`], (C)/(D) [`query_coverage_constraints`], (E) [`acyclicity_constraints`], (F) [`utilization_constraints`], one consumer of a query-time result that is not materialized [`unshared_result_constraints`], each source's raw data on disk once [`raw_data_disk_constraints`]; (G), (H): $y$ exists only on admissible edges | +//! | baseline (section 3) | $x_v$, $y_{u,c,i}$, $z_{q,v}$, $t_v$, $r_s$ ([`SelectionVariables`]) | (A) [`input_satisfaction_constraints`], (B) [`producer_selection_constraints`], (C)/(D) [`query_coverage_constraints`], (E) [`acyclicity_constraints`], (F) and one consumer of a query-time result that is not materialized [`use_constraints`], each source's raw data on disk once [`raw_data_disk_constraints`]; (G), (H): $y$ exists only on admissible edges | //! | execution contexts (section 5) | $x^{\mathrm{mat}}_v$, $x_{\kappa,v}$, $y_{\kappa,u,c,i}$, $h_v$ ([`ExecutionContextVariables`]) | (R) [`retention_constraints`] | //! | accuracy (section 4) | $E_{c,m}$, $F_{c,i,m}$, $D_c$, $H_{c,i}$ ([`AccuracyVariables`]) | (K) [`accuracy_constraints`] | //! @@ -50,10 +50,10 @@ pub(crate) struct SelectionInputs<'a> { pub(crate) struct SelectionVariables { /// $x_v$, by [`PhysicalCandidateId`]. x: Vec, - /// $y_{u,c,i}$, on admissible edges only. - y: HashMap, - /// $z_{q,v}$, for $v \in G_q$. - z: HashMap<(QueryId, PhysicalCandidateId), Variable>, + /// $y_{u,c,i}$, on admissible edges only, in edge order. + y: BTreeMap, + /// $z_{q,v}$, for $v \in G_q$, in key order. + z: BTreeMap<(QueryId, PhysicalCandidateId), Variable>, /// $t_v$, by [`PhysicalCandidateId`]: only for candidates on a cycle of /// admissible edges (a strongly connected component); `None` elsewhere. t: Vec>, @@ -69,6 +69,7 @@ pub(crate) struct SelectionVariables { fn selection_variables( inputs: &SelectionInputs<'_>, left_out: &[bool], + components: &Components, problem: &mut ProblemVariables, ) -> SelectionVariables { let count = inputs.physical.physical_candidates.len(); @@ -95,7 +96,7 @@ fn selection_variables( .into_iter() .map(|edge| (edge, problem.add(variable().binary()))) .collect(); - let mut z = HashMap::new(); + let mut z = BTreeMap::new(); for (query, candidates) in inputs.query_outputs { for &v in candidates { z.entry((*query, v)) @@ -104,9 +105,10 @@ fn selection_variables( } // t_v only inside a strongly connected component of the selectable // edges, bounded by its size. - let t = component_sizes(inputs) - .into_iter() - .map(|size| (size > 1).then(|| problem.add(variable().min(0).max((size - 1) as f64)))) + let t = components + .size + .iter() + .map(|&size| (size > 1).then(|| problem.add(variable().min(0).max((size - 1) as f64)))) .collect(); let mut raw_data_on_disk = BTreeMap::new(); for (source, _) in inputs.costs.raw_data_on_disk.iter().flatten() { @@ -148,8 +150,10 @@ fn producer_selection_constraints( inputs: &SelectionInputs<'_>, variables: &SelectionVariables, ) -> Vec { - sorted(&variables.y) - .map(|(edge, y)| { + variables + .y + .iter() + .map(|(&edge, &y)| { let producer = inputs.physical.edge(edge).producer_candidate_id; constraint!(y <= variables.x[producer.0]) }) @@ -166,7 +170,7 @@ fn query_coverage_constraints( let covered: Expression = candidates.iter().map(|&v| variables.z[&(*query, v)]).sum(); constraints.push(constraint!(covered == 1)); } - for (&(_, v), &z) in sorted_by_key(&variables.z) { + for (&(_, v), &z) in &variables.z { constraints.push(constraint!(z <= variables.x[v.0])); } constraints @@ -177,54 +181,49 @@ fn query_coverage_constraints( fn acyclicity_constraints( inputs: &SelectionInputs<'_>, variables: &SelectionVariables, + components: &Components, ) -> Vec { - let components = selectable_components(inputs); - let sizes = component_sizes(inputs); - sorted(&variables.y) - .filter_map(|(edge, y)| { + variables + .y + .iter() + .filter_map(|(&edge, &y)| { let edge = inputs.physical.edge(edge); let (u, c) = (edge.producer_candidate_id.0, edge.consumer_candidate_id.0); - if components[u] != components[c] { + if components.of[u] != components.of[c] { return None; } let (t_u, t_c) = (variables.t[u]?, variables.t[c]?); - let n = sizes[u] as f64; + let n = components.size[u] as f64; Some(constraint!(t_u + 1 <= t_c + n * (1 - y))) }) .collect() } -/// (F) $x_v \le \sum_{(v,c,i)} y_{v,c,i} + \sum_q z_{q,v}$. -fn utilization_constraints( - inputs: &SelectionInputs<'_>, - variables: &SelectionVariables, -) -> Vec { - let uses = uses_of(inputs, variables); - uses.into_iter() - .enumerate() - .map(|(v, used)| constraint!(variables.x[v] <= used)) - .collect() -} - -/// A query-time result that is not materialized ($\ell$ = None) has at most -/// one consumer: $\sum_{(v,c,i)} y_{v,c,i} + \sum_q z_{q,v} \le x_v$. -/// Consumers that share a result read a `OneEvaluation` or a materialized -/// producer (section 5, per execution context). An ingestion-time -/// producer is exempt: there is no `OneEvaluation` at ingestion time, and -/// the consumers of one ingestion run share its rows. -fn unshared_result_constraints( +/// (F) $x_v \le \sum_{(v,c,i)} y_{v,c,i} + \sum_q z_{q,v}$ for every +/// candidate; with it, a query-time result that is not materialized +/// ($\ell$ = None) has at most one consumer, $\sum_{(v,c,i)} y_{v,c,i} + +/// \sum_q z_{q,v} \le x_v$, so for such a candidate the two are one row, +/// $\sum_{(v,c,i)} y_{v,c,i} + \sum_q z_{q,v} = x_v$. Consumers that share a +/// result read a `OneEvaluation` or a materialized producer (section 5, per +/// execution context). An ingestion-time producer is exempt from the +/// one-consumer rule: there is no `OneEvaluation` at ingestion time, and the +/// consumers of one ingestion run share its rows. +fn use_constraints( inputs: &SelectionInputs<'_>, variables: &SelectionVariables, ) -> Vec { - let uses = uses_of(inputs, variables); - uses.into_iter() + uses_of(inputs, variables) + .into_iter() .enumerate() - .filter(|(v, _)| { - let properties = inputs.physical.physical_candidates[*v].physical_properties; - properties.lifetime() == Lifetime::None + .map(|(v, used)| { + let properties = inputs.physical.physical_candidates[v].physical_properties; + match properties.lifetime() == Lifetime::None && properties.timing() == ExecutionTiming::QueryTime + { + true => constraint!(used == variables.x[v]), + false => constraint!(variables.x[v] <= used), + } }) - .map(|(v, used)| constraint!(used <= variables.x[v])) .collect() } @@ -262,50 +261,38 @@ fn uses_of(inputs: &SelectionInputs<'_>, variables: &SelectionVariables) -> Vec< .sum() }) .collect(); - for (&(_, v), &z) in sorted_by_key(&variables.z) { + for (&(_, v), &z) in &variables.z { uses[v.0] += z; } uses } -/// `y` in edge order, so the model is the same in every run. -fn sorted( - y: &HashMap, -) -> impl Iterator { - let mut y: Vec<(PhysicalEdgeId, Variable)> = y.iter().map(|(&e, &v)| (e, v)).collect(); - y.sort_unstable_by_key(|(edge, _)| *edge); - y.into_iter() -} - -/// `z` in key order. -fn sorted_by_key( - z: &HashMap<(QueryId, PhysicalCandidateId), Variable>, -) -> Vec<(&(QueryId, PhysicalCandidateId), &Variable)> { - let mut z: Vec<_> = z.iter().collect(); - z.sort_unstable_by_key(|(key, _)| **key); - z +/// Each candidate's strongly connected component of the selectable edges +/// (those [`SelectionCosts::edge`] costs), computed once per model. +pub(crate) struct Components { + /// Each candidate's component number. + of: Vec, + /// The size of each candidate's component: 1 for a candidate on no + /// cycle. + size: Vec, } -/// Each candidate's strongly connected component of the selectable edges. -fn selectable_components(inputs: &SelectionInputs<'_>) -> Vec { - strongly_connected_components( - inputs.physical.physical_candidates.len(), - inputs.costs.edge.keys().map(|&edge| { - let edge = inputs.physical.edge(edge); - (edge.producer_candidate_id.0, edge.consumer_candidate_id.0) - }), - ) -} - -/// The size of each candidate's [`selectable_components`] component: 1 -/// for a candidate on no cycle. -fn component_sizes(inputs: &SelectionInputs<'_>) -> Vec { - let components = selectable_components(inputs); - let mut size: HashMap = HashMap::new(); - for &component in &components { - *size.entry(component).or_default() += 1; +impl Components { + fn of(inputs: &SelectionInputs<'_>) -> Self { + let of = strongly_connected_components( + inputs.physical.physical_candidates.len(), + inputs.costs.edge.keys().map(|&edge| { + let edge = inputs.physical.edge(edge); + (edge.producer_candidate_id.0, edge.consumer_candidate_id.0) + }), + ); + let mut count: HashMap = HashMap::new(); + for &component in &of { + *count.entry(component).or_default() += 1; + } + let size = of.iter().map(|component| count[component]).collect(); + Self { of, size } } - components.iter().map(|component| size[component]).collect() } /// Each node's strongly connected component of the graph with `arcs` @@ -388,7 +375,7 @@ pub(crate) enum SelectionMilpError { /// Builds and solves the baseline MILP: minimize /// $\sum_v w_v x_v + \sum_{(u,c,i)} c_{u,c,i} y_{u,c,i} + \sum_s r_s$ under -/// (A)–(F), [`unshared_result_constraints`] and +/// (A)–(F), the one-consumer rule ([`use_constraints`]) and /// [`raw_data_disk_constraints`], with HiGHS at gap 0 on one thread. The /// candidates dearer than a feasible plan are left out first /// ([`dearer_than_a_feasible_plan`]), then the coefficients are scaled so @@ -398,15 +385,15 @@ pub(crate) fn solve_selection( ) -> Result { let started = Instant::now(); let left_out = dearer_than_a_feasible_plan(inputs); + let components = Components::of(inputs); let mut problem = ProblemVariables::new(); - let variables = selection_variables(inputs, &left_out, &mut problem); + let variables = selection_variables(inputs, &left_out, &components, &mut problem); let constraints: Vec = [ input_satisfaction_constraints(inputs, &variables), producer_selection_constraints(inputs, &variables), query_coverage_constraints(inputs, &variables), - acyclicity_constraints(inputs, &variables), - utilization_constraints(inputs, &variables), - unshared_result_constraints(inputs, &variables), + acyclicity_constraints(inputs, &variables, &components), + use_constraints(inputs, &variables), raw_data_disk_constraints(inputs, &variables), ] .into_iter() @@ -417,7 +404,7 @@ pub(crate) fn solve_selection( let largest = (0..left_out.len()) .filter(kept) .filter_map(|v| inputs.costs.candidate[v].as_ref().ok()) - .chain(sorted(&variables.y).map(|(edge, _)| &inputs.costs.edge[&edge])) + .chain(variables.y.keys().map(|edge| &inputs.costs.edge[edge])) .chain( (0..left_out.len()) .filter(kept) @@ -433,7 +420,10 @@ pub(crate) fn solve_selection( let cost = inputs.costs.candidate[v].as_ref().ok()?; Some(cost * scale * variables.x[v]) }); - let edge_terms = sorted(&variables.y).map(|(edge, y)| inputs.costs.edge[&edge] * scale * y); + let edge_terms = variables + .y + .iter() + .map(|(edge, &y)| inputs.costs.edge[edge] * scale * y); let raw_data_terms = variables .raw_data_on_disk .values() @@ -497,9 +487,11 @@ pub(crate) fn solve_selection( .filter(|&v| chosen(variables.x[v])) .filter_map(|v| inputs.costs.candidate[v].as_ref().ok()) .sum::() - + sorted(&variables.y) - .filter(|&(_, y)| chosen(y)) - .map(|(edge, _)| inputs.costs.edge[&edge]) + + variables + .y + .iter() + .filter(|&(_, &y)| chosen(y)) + .map(|(edge, _)| inputs.costs.edge[edge]) .sum::() + raw_data_on_disk.values().sum::() + inputs.costs.request_overhead_cost_per_second; @@ -509,9 +501,11 @@ pub(crate) fn solve_selection( .filter(|&v| chosen(variables.x[v])) .map(PhysicalCandidateId) .collect(), - edges: sorted(&variables.y) - .filter(|&(_, y)| chosen(y)) - .map(|(edge, _)| edge) + edges: variables + .y + .iter() + .filter(|&(_, &y)| chosen(y)) + .map(|(&edge, _)| edge) .collect(), outputs: inputs .query_outputs From 3e236632199dc0ffa6b83ed3eaa4b6268bb38c62 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:16:14 +0000 Subject: [PATCH 11/31] refactor(stage3): files by scope: selectable edges, cost, retention, plan, report; the MILP's model, graph, bound and extension stubs (#1207 PR 6) Move-only, plus names that say what they hold: - `lib.rs` is `select_plan` only. Edge and query-output selection move to `selectable.rs` (`selectable_edges`, `output_groups`, `query_outputs`); `infeasible`, `accepted_accuracy` and `sketches_below` to `report.rs` (`sketches_below` reads a producers-of map built once per plan instead of scanning every edge per node). - `cost.rs` keeps $w_v$ and $c_{u,c,i}$; `candidate_node` moves to `plan.rs` (costing imports it from there), `retention_need_ms`/`scan_read_ms` to `retention.rs`; `input_count` is `PhysicalASAPMemoCandidates::input_count`. - `milp/model.rs` becomes `milp/model/mod.rs` (the baseline); `milp/graph.rs` holds the strongly connected components, and the `todo!()` stubs move to `milp/execution_contexts.rs` (variables, (R)) and `milp/accuracy.rs` (variables, (K)), so the later extensions fill their own files. - `SelectionCosts::{candidate, edge}` -> `candidate_cost_per_second`, `edge_cost_change_per_second`; `Selection::{milp_variables, milp_constraints}` -> `milp_variable_count`, `milp_constraint_count` (the devtools JSON keys stay). - Module docs cite the concepts doc by its path (`docs/design_docs/concepts/memo-groups-properties-requirements.md`). --- crates/devtools/src/bin/asap_planner.rs | 4 +- crates/stage3-plan-selection/src/cost.rs | 157 ++---------- crates/stage3-plan-selection/src/lib.rs | 223 ++---------------- .../src/milp/accuracy.rs | 41 ++++ .../src/milp/execution_contexts.rs | 40 ++++ .../stage3-plan-selection/src/milp/graph.rs | 89 +++++++ crates/stage3-plan-selection/src/milp/mod.rs | 11 +- .../src/milp/{model.rs => model/mod.rs} | 189 +++------------ .../src/milp/model/tests.rs | 4 +- .../src/milp/upper_bound.rs | 15 +- crates/stage3-plan-selection/src/plan.rs | 57 ++++- crates/stage3-plan-selection/src/report.rs | 120 +++++++++- crates/stage3-plan-selection/src/retention.rs | 80 +++++++ .../stage3-plan-selection/src/selectable.rs | 123 ++++++++++ .../stages/physical/memo_candidates/mod.rs | 12 + 15 files changed, 645 insertions(+), 520 deletions(-) create mode 100644 crates/stage3-plan-selection/src/milp/accuracy.rs create mode 100644 crates/stage3-plan-selection/src/milp/execution_contexts.rs create mode 100644 crates/stage3-plan-selection/src/milp/graph.rs rename crates/stage3-plan-selection/src/milp/{model.rs => model/mod.rs} (71%) create mode 100644 crates/stage3-plan-selection/src/retention.rs create mode 100644 crates/stage3-plan-selection/src/selectable.rs diff --git a/crates/devtools/src/bin/asap_planner.rs b/crates/devtools/src/bin/asap_planner.rs index 2230c8df5..2e8fdde5c 100644 --- a/crates/devtools/src/bin/asap_planner.rs +++ b/crates/devtools/src/bin/asap_planner.rs @@ -619,8 +619,8 @@ fn selection_json(selection: &Result<(PhysicalASAPDAG, Selection), SelectionErro .iter() .map(|p| json!({ "candidate": format!("{:?}", p.candidate), "reason": format!("{:?}", p.reason) })) .collect::>(), - "milp_variables": selection.milp_variables, - "milp_constraints": selection.milp_constraints, + "milp_variables": selection.milp_variable_count, + "milp_constraints": selection.milp_constraint_count, "milp_construction_ms": selection.milp_construction_ms, "milp_solve_ms": selection.milp_solve_ms, }) diff --git a/crates/stage3-plan-selection/src/cost.rs b/crates/stage3-plan-selection/src/cost.rs index 4f2159832..d9249a5b9 100644 --- a/crates/stage3-plan-selection/src/cost.rs +++ b/crates/stage3-plan-selection/src/cost.rs @@ -32,26 +32,27 @@ use asap_cost::{ }; use asap_types::ir::operator::Source; use asap_types::ir::properties::ExecutionTiming; -use asap_types::ir::properties::{DataPrimitive, ExecutionDataState}; use asap_types::ir::{ASAPOp, NonASAPOp, Operator}; use asap_types::plan::physical_asap_dag::{ PhysicalASAPDAGNode, PhysicalASAPNodeId, PhysicalASAPOperatorPayload, }; -use asap_types::plan::time_window::TimeIndexedRead; -use asap_types::plan::view_coverage; use asap_types::stages::{ - Lifetime, Memo, MemoGroupId, PhysicalASAPMemoCandidates, PhysicalCandidateId, PhysicalEdgeId, + Memo, MemoGroupId, PhysicalASAPMemoCandidates, PhysicalCandidateId, PhysicalEdgeId, }; use asap_types::workload::{DataWorkload, QueryWorkloadEntry}; +use crate::plan::candidate_node; +use crate::retention::{retention_need_ms, scan_read_ms}; + /// $w_v$ and $c_{u,c,i}$. pub(crate) struct SelectionCosts { - /// $w_v$ per second, by [`PhysicalCandidateId`], or why `asap-cost` - /// could not cost it (the MILP leaves such a candidate out). - pub(crate) candidate: Vec>, - /// $c_{u,c,i}$ per second of each selectable edge whose producer and - /// consumer are costable; an edge missing here cannot be selected. - pub(crate) edge: HashMap, + /// $w_v$, cost per second, by [`PhysicalCandidateId`], or why + /// `asap-cost` could not cost it (the MILP leaves such a candidate out). + pub(crate) candidate_cost_per_second: Vec>, + /// $c_{u,c,i}$, the change of its consumer's cost per second, of each + /// selectable edge whose producer and consumer are costable; an edge + /// missing here cannot be selected. + pub(crate) edge_cost_change_per_second: HashMap, /// By [`PhysicalCandidateId`], for a query-time `Scan`: its source and /// the cost per second of keeping the raw data it reads on disk /// ([`asap_cost::raw_data_disk_cost_per_second`]). A plan keeps each @@ -64,128 +65,10 @@ pub(crate) struct SelectionCosts { pub(crate) request_overhead_cost_per_second: f64, } -/// Candidate `v` as a plan node with id `id`, its input `i` read from node -/// `input_ids[i]`: its operator, timing, lifetime and θ, its group's schema -/// and, materialized across evaluations, its group's coverage. -/// `retention_ms` is left `None` for the caller to set. -pub(crate) fn candidate_node( - physical: &PhysicalASAPMemoCandidates, - v: PhysicalCandidateId, - id: PhysicalASAPNodeId, - input_ids: &[PhysicalASAPNodeId], -) -> PhysicalASAPDAGNode { - let memo = &physical.logical_candidates.memo; - let candidate = &physical.physical_candidates[v.0]; - let logical = memo.candidate(candidate.logical_candidate_id); - let group = &memo.groups()[candidate.logical_candidate_id.group.0 as usize].group_properties; - let mut inputs = input_ids.iter(); - let payload = logical - .expression - .operator - .map_children(|_| *inputs.next().expect("one node per input")); - let lifetime = candidate.physical_properties.lifetime(); - PhysicalASAPDAGNode { - id, - output_state: ExecutionDataState { - timing: candidate.physical_properties.timing(), - primitive: DataPrimitive::of(&payload, group.result_kind), - }, - payload, - lifetime, - parameters: logical.logical_properties.parameters().cloned(), - output_schema: group.schema.clone(), - retention_ms: None, - selected_physical_candidate: Some(v), - view_coverage: matches!(lifetime, Lifetime::AcrossEvaluations(_)) - .then(|| view_coverage(memo, candidate.logical_candidate_id.group)) - .flatten(), - } -} - -/// The number of inputs of candidate `v`. -pub(crate) fn input_count(physical: &PhysicalASAPMemoCandidates, v: PhysicalCandidateId) -> usize { - physical - .logical_candidates - .memo - .candidate(physical.physical_candidates[v.0].logical_candidate_id) - .expression - .operator - .children() - .len() -} - -/// How far back from the evaluation time `producer`'s rows must be kept -/// for its readers (`readers_of`, the candidates that may read it): -/// [`asap_cost::read_back_ms`] with, at each reader, the longest -/// range-evaluation span of the queries reaching it. -pub(crate) fn retention_need_ms>( - physical: &PhysicalASAPMemoCandidates, - entries: &[QueryWorkloadEntry], - producer: PhysicalCandidateId, - readers_of: impl Fn(PhysicalCandidateId) -> R, -) -> Option { - let memo = &physical.logical_candidates.memo; - let span_ms = |v: PhysicalCandidateId| { - let group = physical.physical_candidates[v.0].logical_candidate_id.group; - memo.groups()[group.0 as usize] - .reached_by_queries() - .iter() - .filter_map(|&query| entries.get(query)?.range_evaluation) - .map(|range| range.span_ms) - .max() - .unwrap_or(0) - }; - read_back(physical, producer, readers_of, span_ms) -} - -/// How far back from the evaluation time one execution of the query-time -/// `Scan` `producer` reads, for its readers (`readers_of`): -/// [`asap_cost::read_back_ms`] without a range-evaluation span. -pub(crate) fn scan_read_ms>( - physical: &PhysicalASAPMemoCandidates, - producer: PhysicalCandidateId, - readers_of: impl Fn(PhysicalCandidateId) -> R, -) -> Option { - read_back(physical, producer, readers_of, |_| 0) -} - -/// [`asap_cost::read_back_ms`] over candidates, each read as its operator -/// at its timing reads ([`TimeIndexedRead::of`]). -fn read_back>( - physical: &PhysicalASAPMemoCandidates, - producer: PhysicalCandidateId, - readers_of: impl Fn(PhysicalCandidateId) -> R, - span_ms: impl Fn(PhysicalCandidateId) -> u64, -) -> Option { - let memo = &physical.logical_candidates.memo; - let operator_of = |v: PhysicalCandidateId| { - &memo - .candidate(physical.physical_candidates[v.0].logical_candidate_id) - .expression - .operator - }; - asap_cost::read_back_ms( - producer, - readers_of, - |v| { - TimeIndexedRead::of( - operator_of(v), - physical.physical_candidates[v.0] - .physical_properties - .timing(), - ) - }, - span_ms, - matches!( - operator_of(producer), - Operator::NonASAP(NonASAPOp::Scan { .. }) - ), - ) -} - /// [`SelectionCosts`] over `selectable` (each admissible edge a plan may /// select, by consumer and input). Errs on nothing: a candidate -/// `asap-cost` cannot cost is reported in [`SelectionCosts::candidate`]. +/// `asap-cost` cannot cost is reported in +/// [`SelectionCosts::candidate_cost_per_second`]. pub(crate) fn selection_costs( physical: &PhysicalASAPMemoCandidates, selectable: &[PhysicalEdgeId], @@ -211,7 +94,7 @@ pub(crate) fn selection_costs( let nodes: Vec = (0..count) .map(PhysicalCandidateId) .map(|v| { - let inputs: Vec = (0..input_count(physical, v)).collect(); + let inputs: Vec = (0..physical.input_count(v)).collect(); let mut node = candidate_node(physical, v, v.0, &inputs); if node.retains_rows() { node.retention_ms = retention_need_ms(physical, entries, v, readers_of); @@ -270,7 +153,7 @@ pub(crate) fn selection_costs( inputs_of[v.0] = inputs; } } - let candidate: Vec> = usage + let candidate_cost_per_second: Vec> = usage .iter() .map(|usage| match usage { Some(Ok(usage)) => cost_per_second(usage), @@ -286,11 +169,11 @@ pub(crate) fn selection_costs( Some((source.clone(), cost)) }) .collect(); - let mut edge = HashMap::new(); + let mut edge_cost_change_per_second = HashMap::new(); for &id in selectable { let e = physical.edge(id); let (u, c) = (e.producer_candidate_id, e.consumer_candidate_id); - let (Some(Ok(produced)), Ok(own)) = (&usage[u.0], &candidate[c.0]) else { + let (Some(Ok(produced)), Ok(own)) = (&usage[u.0], &candidate_cost_per_second[c.0]) else { continue; }; let Some(mut inputs) = inputs_of[c.0].clone() else { @@ -309,12 +192,12 @@ pub(crate) fn selection_costs( ) .and_then(|usage| cost_per_second(&usage)); if let Ok(with_u) = with_u { - edge.insert(id, with_u - own); + edge_cost_change_per_second.insert(id, with_u - own); } } SelectionCosts { - candidate, - edge, + candidate_cost_per_second, + edge_cost_change_per_second, raw_data_on_disk, request_overhead_cost_per_second, } diff --git a/crates/stage3-plan-selection/src/lib.rs b/crates/stage3-plan-selection/src/lib.rs index eee6cc05c..a7cc68613 100644 --- a/crates/stage3-plan-selection/src/lib.rs +++ b/crates/stage3-plan-selection/src/lib.rs @@ -1,13 +1,16 @@ -//! #509 Stage 3, plan selection (concepts doc -//! `memo-groups-properties-requirements.md`, section 3, "Stage 3: baseline -//! selection and realization", and sections 4 and 5). +//! Stage 3, plan selection +//! (`docs/design_docs/concepts/memo-groups-properties-requirements.md`, +//! section 3, "Stage 3: baseline selection and plan output", and sections +//! 4 and 5). //! //! [`select_plan`] reads Stage 2's physical candidates $\mathcal C$ and the -//! admissible part of the physical edges $\mathcal E$, selects with a MILP -//! over $x_v$, $y_{u,c,i}$, $z_{q,v}$ and $t_v$ (constraints (A)–(I); -//! (G) and (H) hold because $y$ exists only on admissible edges), and -//! builds the plan (`PhysicalASAPDAG`) from $\{x = 1\}$ and $\{y = 1\}$ -//! as its last step. The extensions add execution contexts $\kappa$ with $x^{\mathrm{mat}}_v$, +//! selectable part of the physical edges $\mathcal E$ ([`selectable`]), +//! costs them with `asap-cost` ([`cost`]), selects with a MILP over $x_v$, +//! $y_{u,c,i}$, $z_{q,v}$, $t_v$ and $r_s$ ([`milp`]; constraints (A)–(F) +//! and the one-consumer rule; (G) and (H) hold because $y$ exists only on +//! admissible edges), and builds the plan (`PhysicalASAPDAG`) from +//! $\{x = 1\}$ and $\{y = 1\}$ as its last step ([`plan`], with each +//! node's retention from [`retention`]). The extensions add execution contexts $\kappa$ with $x^{\mathrm{mat}}_v$, //! $x_{\kappa,v}$, $y_{\kappa,u,c,i}$ and the retention amount $h_v$ //! (section 5), and accuracy at each query root, constraint (K), with //! per-candidate error bounds $E_c$ and $D_c$ (section 4). Costs come from @@ -17,25 +20,19 @@ mod cost; mod milp; mod plan; mod report; +mod retention; +mod selectable; -use std::collections::BTreeSet; - -use asap_cost::{request_overhead_cost_per_second, summary_type_name, CostInputs, CostModel}; -use asap_types::ir::properties::ExecutionTiming; -use asap_types::ir::schema::FieldDataType; -use asap_types::ir::{ASAPOp, Operator, QueryRoot}; -use asap_types::plan::physical_asap_dag::{PhysicalASAPDAG, PhysicalASAPNodeId}; -use asap_types::stages::{ - output_references, producer_satisfies_physical_requirements, EvaluationGrid, ExecutionContext, - MemoGroupId, PhysicalASAPMemoCandidates, PhysicalCandidate, PhysicalCandidateId, PhysicalEdge, - PhysicalEdgeId, PruneReason, PrunedCandidate, -}; +use asap_cost::{request_overhead_cost_per_second, CostInputs, CostModel}; +use asap_types::plan::physical_asap_dag::PhysicalASAPDAG; +use asap_types::stages::{PhysicalASAPMemoCandidates, PruneReason, PrunedCandidate}; use asap_types::workload::{DataWorkload, QueryWorkloadEntry}; -use crate::cost::{selection_costs, SelectionCosts}; -use crate::milp::model::{solve_selection, QueryId, SelectionInputs, SelectionMilpError}; +use crate::cost::selection_costs; +use crate::milp::model::{solve_selection, SelectionInputs, SelectionMilpError}; use crate::plan::plan_of; -use crate::report::Rejection; +use crate::report::{accepted_accuracy, infeasible}; +use crate::selectable::{output_groups, query_outputs, selectable_edges}; pub use report::{ large_peak_memory_warning, AcceptedAccuracy, ErrorSelectingPlan, RejectionCause, Selection, @@ -74,14 +71,7 @@ pub fn select_plan( entries: entries.len(), }); } - let selectable: Vec = physical - .edges() - .filter(|(_, edge)| { - edge.is_logically_and_physically_admissible() - && delivers_to_consumer(physical, entries, edge) - }) - .map(|(id, _)| id) - .collect(); + let selectable = selectable_edges(physical, entries); let request_overhead_cost_per_second = request_overhead_cost_per_second(entries, cost_inputs) .map_err(|error| ErrorSelectingPlan::Cost { candidate: "every plan (the request overhead)".into(), @@ -96,35 +86,8 @@ pub fn select_plan( cost_inputs, request_overhead_cost_per_second, ); - let roots: Vec> = - queries.iter().map(|query| query.root.clone()).collect(); - let outputs: Vec<(QueryId, MemoGroupId)> = output_references(&roots) - .into_iter() - .map(|(query, group)| (QueryId(query), group)) - .collect::>() - .into_iter() - .collect(); - let query_outputs: Vec<(QueryId, Vec)> = outputs - .iter() - .map(|&(query, group)| { - let candidates = physical - .physical_candidates - .iter() - .enumerate() - .filter(|(v, candidate)| { - candidate.logical_candidate_id.group == group - && costs.candidate[*v].is_ok() - && producer_satisfies_physical_requirements( - candidate, - ExecutionTiming::QueryTime, - ) - && candidate.delivers_on(physical, &evaluation_context(&entries[query.0])) - }) - .map(|(v, _)| PhysicalCandidateId(v)) - .collect(); - (query, candidates) - }) - .collect(); + let outputs = output_groups(physical); + let query_outputs = query_outputs(physical, entries, &costs, &outputs); if query_outputs .iter() .any(|(_, candidates)| candidates.is_empty()) @@ -146,7 +109,7 @@ pub fn select_plan( let selection = Selection { accepted_accuracy_per_query: accepted_accuracy(physical, &plan), pruned_candidates: costs - .candidate + .candidate_cost_per_second .iter() .zip(&physical.physical_candidates) .filter(|(cost, _)| cost.is_err()) @@ -156,144 +119,10 @@ pub fn select_plan( }) .collect(), selected_plan_cost_per_second: solved.objective_cost_per_second, - milp_variables: solved.variables, - milp_constraints: solved.constraints, + milp_variable_count: solved.variable_count, + milp_constraint_count: solved.constraint_count, milp_construction_ms: solved.construction_ms, milp_solve_ms: solved.solve_ms, }; Ok((plan, selection)) } - -/// The evaluation context of `entry`'s query: the grid its evaluations -/// fall on (`QueryRecurrence::evaluation_grid_ms`), anchored at the epoch. -/// Times that are not known may fall on any millisecond: a 1 ms grid, on -/// which only producers stored at every time deliver. -fn evaluation_context(entry: &QueryWorkloadEntry) -> ExecutionContext { - ExecutionContext::Evaluation { - grid: EvaluationGrid { - period_ms: entry - .recurrence - .evaluation_grid_ms(entry.range_evaluation) - .unwrap_or(1), - phase_ms: 0, - }, - } -} - -/// Whether `edge`'s producer delivers on every evaluation its query-time -/// consumer serves: on the evaluation context of each query reaching the -/// consumer's group ([`PhysicalCandidate::delivers_on`]). The baseline has -/// no other context: an ingestion-time consumer's (its pipeline) is checked -/// with execution contexts. -fn delivers_to_consumer( - physical: &PhysicalASAPMemoCandidates, - entries: &[QueryWorkloadEntry], - edge: &PhysicalEdge, -) -> bool { - let producer: &PhysicalCandidate = &physical.physical_candidates[edge.producer_candidate_id.0]; - let consumer = &physical.physical_candidates[edge.consumer_candidate_id.0]; - if consumer.physical_properties.timing() != ExecutionTiming::QueryTime { - return true; - } - physical.logical_candidates.memo.groups()[consumer.logical_candidate_id.group.0 as usize] - .reached_by_queries() - .iter() - .filter_map(|&query| entries.get(query)) - .all(|entry| producer.delivers_on(physical, &evaluation_context(entry))) -} - -/// The selection MILP is infeasible, with the reasons candidates could not -/// be costed. -fn infeasible(costs: &SelectionCosts) -> ErrorSelectingPlan { - let mut reasons: Vec<&str> = Vec::new(); - for reason in costs - .candidate - .iter() - .filter_map(|cost| cost.as_ref().err()) - { - if !reasons.contains(&reason.as_str()) { - reasons.push(reason); - } - } - let mut reason = "no plan gives every query output a candidate whose every input has an \ - admissible producer (the selection MILP is infeasible)" - .to_string(); - if !reasons.is_empty() { - reason += &format!( - "; candidates left out because they could not be costed: {}", - reasons.join("; ") - ); - } - ErrorSelectingPlan::NoValidCandidate(vec![Rejection { - id: "workload".into(), - cause: RejectionCause::Infeasible, - reason, - }]) -} - -/// Per query, the sketches its output reads in `plan` (each sketch build -/// below its root, in node order) and, when it reads one, its target's -/// metric. -fn accepted_accuracy( - physical: &PhysicalASAPMemoCandidates, - plan: &PhysicalASAPDAG, -) -> Vec { - let mut operator_roots = plan.roots.iter(); - physical - .logical_candidates - .memo - .queries() - .iter() - .enumerate() - .map(|(query, memo_query)| { - let starts: Vec = match plan - .scalar_roots - .iter() - .find(|(position, _)| *position == query) - { - Some((_, expression)) => expression.operator_refs().into_iter().copied().collect(), - None => vec![*operator_roots.next().expect("one operator root per query")], - }; - let sketches = sketches_below(plan, starts); - AcceptedAccuracy { - accepted_metric: (!sketches.is_empty()) - .then(|| memo_query.accuracy_target.as_ref()?.accuracy_metric) - .flatten(), - sketches, - } - }) - .collect() -} - -/// The summary type names of the sketch builds at or below `starts` in -/// `plan`, in node order, each once. -fn sketches_below(plan: &PhysicalASAPDAG, starts: Vec) -> Vec { - let mut below = BTreeSet::new(); - let mut pending = starts; - while let Some(id) = pending.pop() { - if below.insert(id) { - pending.extend( - plan.edges - .iter() - .filter(|e| e.consumer == id) - .map(|e| e.producer), - ); - } - } - let mut sketches: Vec = Vec::new(); - for node in plan.nodes.iter().filter(|node| below.contains(&node.id)) { - if let Operator::ASAP( - ASAPOp::SummaryAgg { summary_type, .. } - | ASAPOp::SummaryTumblingWindowChain { summary_type, .. }, - ) = &node.payload - { - if matches!(summary_type, FieldDataType::Sketch(_)) { - let name = summary_type_name(summary_type); - if !sketches.contains(&name) { - sketches.push(name); - } - } - } - } - sketches -} diff --git a/crates/stage3-plan-selection/src/milp/accuracy.rs b/crates/stage3-plan-selection/src/milp/accuracy.rs new file mode 100644 index 000000000..43a76e733 --- /dev/null +++ b/crates/stage3-plan-selection/src/milp/accuracy.rs @@ -0,0 +1,41 @@ +//! The accuracy extension of the selection MILP +//! (`docs/design_docs/concepts/memo-groups-properties-requirements.md`, +//! section 4): its variables and constraint (K), `todo!()` until (K) is +//! added. + +use std::collections::HashMap; + +use good_lp::{Constraint, Variable}; + +use asap_types::accuracy::AccuracyMetric; +use asap_types::stages::{PhysicalASAPMemoCandidates, PhysicalCandidateId}; + +use super::model::SelectionVariables; + +/// The accuracy variables of constraint (K) (section 4), all continuous. +#[allow(dead_code)] // Read once constraint (K) is added. +struct AccuracyVariables { + /// $E_{c,m}$: candidate $c$'s error bound in metric $m$. + error_bound: HashMap<(PhysicalCandidateId, AccuracyMetric), Variable>, + /// $F_{c,i,m}$: the chosen producer's error bound in metric $m$ on input + /// $i$ of $c$. + error_bound_of_input: HashMap<(PhysicalCandidateId, usize, AccuracyMetric), Variable>, + /// $D_c$, by [`PhysicalCandidateId`]. + failure_probability: Vec, + /// $H_{c,i}$: the chosen producer's failure probability on input $i$ of $c$. + failure_probability_of_input: HashMap<(PhysicalCandidateId, usize), Variable>, +} + +/// (K): $E_{c,m}$ per candidate and metric and $D_c$ per candidate, big-M on +/// selected edges, the metric rule on the selected path, and the root +/// checks $E_{v,m_q} \le \varepsilon_q + M_q(1 - z_{q,v})$, +/// $D_v \le \delta_q + (1 - z_{q,v})$. +#[allow(dead_code)] // Called once constraint (K) is added. +fn accuracy_constraints( + physical: &PhysicalASAPMemoCandidates, + selection: &SelectionVariables, + accuracy: &AccuracyVariables, +) -> Vec { + let _ = (physical, selection, accuracy); + todo!("#1207 PR 8: constraint (K)") +} diff --git a/crates/stage3-plan-selection/src/milp/execution_contexts.rs b/crates/stage3-plan-selection/src/milp/execution_contexts.rs new file mode 100644 index 000000000..e1b66418f --- /dev/null +++ b/crates/stage3-plan-selection/src/milp/execution_contexts.rs @@ -0,0 +1,40 @@ +//! The execution-context extension of the selection MILP +//! (`docs/design_docs/concepts/memo-groups-properties-requirements.md`, +//! section 5): its variables and constraint (R), `todo!()` until execution +//! contexts are selected. + +use std::collections::HashMap; + +use good_lp::{Constraint, Variable}; + +use asap_types::stages::{ + ExecutionContextId, PhysicalASAPMemoCandidates, PhysicalCandidateId, PhysicalEdgeId, +}; + +/// The execution-context variables (section 5), which replace $x$ and the +/// unindexed $y$ of the baseline. +#[allow(dead_code)] // Read once execution contexts are selected. +struct ExecutionContextVariables { + /// $x^{\mathrm{mat}}_v$, for an `AcrossEvaluations` candidate. + x_materialized: HashMap, + /// $x_{\kappa,v}$, for a candidate with $\ell$ = None or `OneEvaluation`. + x_in_execution_context: HashMap<(ExecutionContextId, PhysicalCandidateId), Variable>, + /// $y_{\kappa,u,c,i}$, indexed by the consumer's context. + y_in_execution_context: HashMap<(ExecutionContextId, PhysicalEdgeId), Variable>, + /// $h_v$, for an `AcrossEvaluations` producer. + retention_amount: HashMap, +} + +/// (R) $h_v \ge \operatorname{need}_{\kappa,v,c,i}\, y_{\kappa,v,c,i}$: for a +/// time-indexed producer (raw data, a chain, results stored per evaluation +/// time) the reading path's offset plus range, a constant per edge, so (R) +/// is linear. Query latency is ignored: a result counts as read when its +/// evaluation starts. +#[allow(dead_code)] // Called once execution contexts are selected. +fn retention_constraints( + physical: &PhysicalASAPMemoCandidates, + variables: &ExecutionContextVariables, +) -> Vec { + let _ = (physical, variables); + todo!("#1207 PR 7: constraint (R)") +} diff --git a/crates/stage3-plan-selection/src/milp/graph.rs b/crates/stage3-plan-selection/src/milp/graph.rs new file mode 100644 index 000000000..ec2da8fd5 --- /dev/null +++ b/crates/stage3-plan-selection/src/milp/graph.rs @@ -0,0 +1,89 @@ +//! The strongly connected components of the selectable edges between +//! candidates: acyclicity (E) needs an order variable $t_v$ only inside a +//! component, bounded by its size. + +use std::collections::HashMap; + +use super::model::SelectionInputs; + +/// Each candidate's strongly connected component of the selectable edges +/// (those [`crate::cost::SelectionCosts::edge_cost_change_per_second`] costs), computed once per model. +pub(crate) struct Components { + /// Each candidate's component number. + pub(crate) of: Vec, + /// The size of each candidate's component: 1 for a candidate on no + /// cycle. + pub(crate) size: Vec, +} + +impl Components { + pub(crate) fn of(inputs: &SelectionInputs<'_>) -> Self { + let of = strongly_connected_components( + inputs.physical.physical_candidates.len(), + inputs + .costs + .edge_cost_change_per_second + .keys() + .map(|&edge| { + let edge = inputs.physical.edge(edge); + (edge.producer_candidate_id.0, edge.consumer_candidate_id.0) + }), + ); + let mut count: HashMap = HashMap::new(); + for &component in &of { + *count.entry(component).or_default() += 1; + } + let size = of.iter().map(|component| count[component]).collect(); + Self { of, size } + } +} + +/// Each node's strongly connected component of the graph with `arcs` +/// (Kosaraju), as a component number. +fn strongly_connected_components( + node_count: usize, + arcs: impl Iterator, +) -> Vec { + let mut forward = vec![Vec::new(); node_count]; + let mut backward = vec![Vec::new(); node_count]; + for (from, to) in arcs { + forward[from].push(to); + backward[to].push(from); + } + // Nodes by finishing time of a depth-first search over `forward`. + let mut finished = Vec::with_capacity(node_count); + let mut visited = vec![false; node_count]; + for start in 0..node_count { + if std::mem::replace(&mut visited[start], true) { + continue; + } + let mut stack = vec![(start, 0)]; + while let Some((node, next)) = stack.pop() { + if let Some(&to) = forward[node].get(next) { + stack.push((node, next + 1)); + if !std::mem::replace(&mut visited[to], true) { + stack.push((to, 0)); + } + } else { + finished.push(node); + } + } + } + let mut component = vec![usize::MAX; node_count]; + for (number, &start) in finished.iter().rev().enumerate() { + if component[start] != usize::MAX { + continue; + } + component[start] = number; + let mut stack = vec![start]; + while let Some(node) = stack.pop() { + for &from in &backward[node] { + if component[from] == usize::MAX { + component[from] = number; + stack.push(from); + } + } + } + } + component +} diff --git a/crates/stage3-plan-selection/src/milp/mod.rs b/crates/stage3-plan-selection/src/milp/mod.rs index 83351b8d4..3b30ee22c 100644 --- a/crates/stage3-plan-selection/src/milp/mod.rs +++ b/crates/stage3-plan-selection/src/milp/mod.rs @@ -1,4 +1,11 @@ -//! Stage 3's selection MILP (concepts doc, sections 3–5): its variables, -//! one function per constraint family, and the execution contexts. +//! Stage 3's selection MILP +//! (`docs/design_docs/concepts/memo-groups-properties-requirements.md`, +//! sections 3–5): the baseline's variables and one function per constraint +//! family ([`model`]), the graph algorithm it needs ([`graph`]), the bound +//! it scales by ([`upper_bound`]), and the execution-context and accuracy +//! extensions ([`execution_contexts`], [`accuracy`]). +mod accuracy; +mod execution_contexts; +mod graph; pub(crate) mod model; mod upper_bound; diff --git a/crates/stage3-plan-selection/src/milp/model.rs b/crates/stage3-plan-selection/src/milp/model/mod.rs similarity index 71% rename from crates/stage3-plan-selection/src/milp/model.rs rename to crates/stage3-plan-selection/src/milp/model/mod.rs index 8710d469c..546ebf52a 100644 --- a/crates/stage3-plan-selection/src/milp/model.rs +++ b/crates/stage3-plan-selection/src/milp/model/mod.rs @@ -4,11 +4,14 @@ //! | Family | Variables | Constraints | //! |---|---|---| //! | baseline (section 3) | $x_v$, $y_{u,c,i}$, $z_{q,v}$, $t_v$, $r_s$ ([`SelectionVariables`]) | (A) [`input_satisfaction_constraints`], (B) [`producer_selection_constraints`], (C)/(D) [`query_coverage_constraints`], (E) [`acyclicity_constraints`], (F) and one consumer of a query-time result that is not materialized [`use_constraints`], each source's raw data on disk once [`raw_data_disk_constraints`]; (G), (H): $y$ exists only on admissible edges | -//! | execution contexts (section 5) | $x^{\mathrm{mat}}_v$, $x_{\kappa,v}$, $y_{\kappa,u,c,i}$, $h_v$ ([`ExecutionContextVariables`]) | (R) [`retention_constraints`] | -//! | accuracy (section 4) | $E_{c,m}$, $F_{c,i,m}$, $D_c$, $H_{c,i}$ ([`AccuracyVariables`]) | (K) [`accuracy_constraints`] | +//! | execution contexts (section 5) | $x^{\mathrm{mat}}_v$, $x_{\kappa,v}$, $y_{\kappa,u,c,i}$, $h_v$ (`ExecutionContextVariables`) | (R) `retention_constraints` | +//! | accuracy (section 4) | $E_{c,m}$, $F_{c,i,m}$, $D_c$, $H_{c,i}$ (`AccuracyVariables`) | (K) `accuracy_constraints` | //! -//! The baseline is solved by [`solve_selection`]; the extensions' bodies -//! are `todo!()` until execution contexts and constraint (K) are added. +//! The baseline is solved by [`solve_selection`]; the extensions' +//! variables and constraints are in [`super::execution_contexts`] and +//! [`super::accuracy`], `todo!()` until they are added. The strongly +//! connected components (E) needs are in [`super::graph`]; the bound that +//! leaves out candidates before scaling in [`super::upper_bound`]. use std::collections::{BTreeMap, HashMap}; use std::time::Instant; @@ -18,16 +21,14 @@ use good_lp::{ constraint, variable, Constraint, Expression, ProblemVariables, SolverModel, Variable, }; -use asap_types::accuracy::AccuracyMetric; use asap_types::ir::operator::Source; use asap_types::ir::properties::ExecutionTiming; use asap_types::stages::Lifetime; -use asap_types::stages::{ - ExecutionContextId, PhysicalASAPMemoCandidates, PhysicalCandidateId, PhysicalEdgeId, -}; +use asap_types::stages::{PhysicalASAPMemoCandidates, PhysicalCandidateId, PhysicalEdgeId}; +use super::graph::Components; use super::upper_bound::dearer_than_a_feasible_plan; -use crate::cost::{input_count, SelectionCosts}; +use crate::cost::SelectionCosts; /// A query, by index in the workload. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] @@ -37,8 +38,8 @@ pub struct QueryId(pub usize); pub(crate) struct SelectionInputs<'a> { pub(crate) physical: &'a PhysicalASAPMemoCandidates, /// $w_v$ and $c_{u,c,i}$: $y$ exists only on the edges - /// [`SelectionCosts::edge`] costs, $x_v$ is 0 for a candidate it could - /// not cost. + /// [`SelectionCosts::edge_cost_change_per_second`] costs, $x_v$ is 0 for a + /// candidate it could not cost. pub(crate) costs: &'a SelectionCosts, /// Each query output reference (a query, or one operator a scalar query /// reads), with the candidates that may answer it: $z_{q,v}$ exists @@ -75,7 +76,7 @@ fn selection_variables( let count = inputs.physical.physical_candidates.len(); let x = (0..count) .map( - |v| match inputs.costs.candidate[v].is_ok() && !left_out[v] { + |v| match inputs.costs.candidate_cost_per_second[v].is_ok() && !left_out[v] { true => problem.add(variable().binary()), false => problem.add(variable().min(0).max(0)), }, @@ -83,7 +84,7 @@ fn selection_variables( .collect(); let mut edges: Vec = inputs .costs - .edge + .edge_cost_change_per_second .keys() .copied() .filter(|&edge| { @@ -133,7 +134,7 @@ fn input_satisfaction_constraints( let physical = inputs.physical; let mut constraints = Vec::new(); for c in (0..physical.physical_candidates.len()).map(PhysicalCandidateId) { - for i in 0..input_count(physical, c) { + for i in 0..physical.input_count(c) { let producers: Expression = physical .admissible_edges_into(c, i) .iter() @@ -267,84 +268,6 @@ fn uses_of(inputs: &SelectionInputs<'_>, variables: &SelectionVariables) -> Vec< uses } -/// Each candidate's strongly connected component of the selectable edges -/// (those [`SelectionCosts::edge`] costs), computed once per model. -pub(crate) struct Components { - /// Each candidate's component number. - of: Vec, - /// The size of each candidate's component: 1 for a candidate on no - /// cycle. - size: Vec, -} - -impl Components { - fn of(inputs: &SelectionInputs<'_>) -> Self { - let of = strongly_connected_components( - inputs.physical.physical_candidates.len(), - inputs.costs.edge.keys().map(|&edge| { - let edge = inputs.physical.edge(edge); - (edge.producer_candidate_id.0, edge.consumer_candidate_id.0) - }), - ); - let mut count: HashMap = HashMap::new(); - for &component in &of { - *count.entry(component).or_default() += 1; - } - let size = of.iter().map(|component| count[component]).collect(); - Self { of, size } - } -} - -/// Each node's strongly connected component of the graph with `arcs` -/// (Kosaraju), as a component number. -fn strongly_connected_components( - node_count: usize, - arcs: impl Iterator, -) -> Vec { - let mut forward = vec![Vec::new(); node_count]; - let mut backward = vec![Vec::new(); node_count]; - for (from, to) in arcs { - forward[from].push(to); - backward[to].push(from); - } - // Nodes by finishing time of a depth-first search over `forward`. - let mut finished = Vec::with_capacity(node_count); - let mut visited = vec![false; node_count]; - for start in 0..node_count { - if std::mem::replace(&mut visited[start], true) { - continue; - } - let mut stack = vec![(start, 0)]; - while let Some((node, next)) = stack.pop() { - if let Some(&to) = forward[node].get(next) { - stack.push((node, next + 1)); - if !std::mem::replace(&mut visited[to], true) { - stack.push((to, 0)); - } - } else { - finished.push(node); - } - } - } - let mut component = vec![usize::MAX; node_count]; - for (number, &start) in finished.iter().rev().enumerate() { - if component[start] != usize::MAX { - continue; - } - component[start] = number; - let mut stack = vec![start]; - while let Some(node) = stack.pop() { - for &from in &backward[node] { - if component[from] == usize::MAX { - component[from] = number; - stack.push(from); - } - } - } - } - component -} - /// The baseline MILP's optimum: $\{x = 1\}$, $\{y = 1\}$ and $\{z = 1\}$, /// and the MILP's size and time. #[derive(Debug, Clone)] @@ -358,8 +281,9 @@ pub(crate) struct SolvedSelection { pub(crate) outputs: Vec, /// The optimum, unscaled: the selected plan's cost per second. pub(crate) objective_cost_per_second: f64, - pub(crate) variables: usize, - pub(crate) constraints: usize, + /// The MILP's variables and constraint rows. + pub(crate) variable_count: usize, + pub(crate) constraint_count: usize, pub(crate) construction_ms: u64, pub(crate) solve_ms: u64, } @@ -403,8 +327,13 @@ pub(crate) fn solve_selection( let kept = |v: &usize| !left_out[*v]; let largest = (0..left_out.len()) .filter(kept) - .filter_map(|v| inputs.costs.candidate[v].as_ref().ok()) - .chain(variables.y.keys().map(|edge| &inputs.costs.edge[edge])) + .filter_map(|v| inputs.costs.candidate_cost_per_second[v].as_ref().ok()) + .chain( + variables + .y + .keys() + .map(|edge| &inputs.costs.edge_cost_change_per_second[edge]), + ) .chain( (0..left_out.len()) .filter(kept) @@ -417,13 +346,13 @@ pub(crate) fn solve_selection( false => 1.0, }; let candidate_terms = (0..left_out.len()).filter(kept).filter_map(|v| { - let cost = inputs.costs.candidate[v].as_ref().ok()?; + let cost = inputs.costs.candidate_cost_per_second[v].as_ref().ok()?; Some(cost * scale * variables.x[v]) }); let edge_terms = variables .y .iter() - .map(|(edge, &y)| inputs.costs.edge[edge] * scale * y); + .map(|(edge, &y)| inputs.costs.edge_cost_change_per_second[edge] * scale * y); let raw_data_terms = variables .raw_data_on_disk .values() @@ -485,13 +414,13 @@ pub(crate) fn solve_selection( } let objective_cost_per_second = (0..variables.x.len()) .filter(|&v| chosen(variables.x[v])) - .filter_map(|v| inputs.costs.candidate[v].as_ref().ok()) + .filter_map(|v| inputs.costs.candidate_cost_per_second[v].as_ref().ok()) .sum::() + variables .y .iter() .filter(|&(_, &y)| chosen(y)) - .map(|(edge, _)| inputs.costs.edge[edge]) + .map(|(edge, _)| inputs.costs.edge_cost_change_per_second[edge]) .sum::() + raw_data_on_disk.values().sum::() + inputs.costs.request_overhead_cost_per_second; @@ -518,68 +447,12 @@ pub(crate) fn solve_selection( .expect("(C): one output per query output reference") }) .collect(), - variables: variable_count, - constraints: constraint_count, + variable_count, + constraint_count, construction_ms, solve_ms, }) } -/// The execution-context variables (section 5), which replace $x$ and the -/// unindexed $y$ of the baseline. -#[allow(dead_code)] // Read once execution contexts are selected. -struct ExecutionContextVariables { - /// $x^{\mathrm{mat}}_v$, for an `AcrossEvaluations` candidate. - x_materialized: HashMap, - /// $x_{\kappa,v}$, for a candidate with $\ell$ = None or `OneEvaluation`. - x_in_execution_context: HashMap<(ExecutionContextId, PhysicalCandidateId), Variable>, - /// $y_{\kappa,u,c,i}$, indexed by the consumer's context. - y_in_execution_context: HashMap<(ExecutionContextId, PhysicalEdgeId), Variable>, - /// $h_v$, for an `AcrossEvaluations` producer. - retention_amount: HashMap, -} - -/// The accuracy variables of constraint (K) (section 4), all continuous. -#[allow(dead_code)] // Read once constraint (K) is added. -struct AccuracyVariables { - /// $E_{c,m}$: candidate $c$'s error bound in metric $m$. - error_bound: HashMap<(PhysicalCandidateId, AccuracyMetric), Variable>, - /// $F_{c,i,m}$: the chosen producer's error bound in metric $m$ on input - /// $i$ of $c$. - error_bound_of_input: HashMap<(PhysicalCandidateId, usize, AccuracyMetric), Variable>, - /// $D_c$, by [`PhysicalCandidateId`]. - failure_probability: Vec, - /// $H_{c,i}$: the chosen producer's failure probability on input $i$ of $c$. - failure_probability_of_input: HashMap<(PhysicalCandidateId, usize), Variable>, -} - -/// (R) $h_v \ge \operatorname{need}_{\kappa,v,c,i}\, y_{\kappa,v,c,i}$: for a -/// time-indexed producer (raw data, a chain, results stored per evaluation -/// time) the reading path's offset plus range, a constant per edge, so (R) -/// is linear. Query latency is ignored: a result counts as read when its -/// evaluation starts. -#[allow(dead_code)] // Called once execution contexts are selected. -fn retention_constraints( - physical: &PhysicalASAPMemoCandidates, - variables: &ExecutionContextVariables, -) -> Vec { - let _ = (physical, variables); - todo!("#1207 PR 7: constraint (R)") -} - -/// (K): $E_{c,m}$ per candidate and metric and $D_c$ per candidate, big-M on -/// selected edges, the metric rule on the selected path, and the root -/// checks $E_{v,m_q} \le \varepsilon_q + M_q(1 - z_{q,v})$, -/// $D_v \le \delta_q + (1 - z_{q,v})$. -#[allow(dead_code)] // Called once constraint (K) is added. -fn accuracy_constraints( - physical: &PhysicalASAPMemoCandidates, - selection: &SelectionVariables, - accuracy: &AccuracyVariables, -) -> Vec { - let _ = (physical, selection, accuracy); - todo!("#1207 PR 8: constraint (K)") -} - #[cfg(test)] mod tests; diff --git a/crates/stage3-plan-selection/src/milp/model/tests.rs b/crates/stage3-plan-selection/src/milp/model/tests.rs index 555b0018c..56c0bb7c7 100644 --- a/crates/stage3-plan-selection/src/milp/model/tests.rs +++ b/crates/stage3-plan-selection/src/milp/model/tests.rs @@ -117,8 +117,8 @@ fn costs( edge_cost.insert(edge(physical, u, c), cost); } SelectionCosts { - candidate: w.iter().map(|&w| Ok(w)).collect(), - edge: edge_cost, + candidate_cost_per_second: w.iter().map(|&w| Ok(w)).collect(), + edge_cost_change_per_second: edge_cost, raw_data_on_disk: vec![None; w.len()], request_overhead_cost_per_second: 0.0, } diff --git a/crates/stage3-plan-selection/src/milp/upper_bound.rs b/crates/stage3-plan-selection/src/milp/upper_bound.rs index 731ae0a55..a1d905551 100644 --- a/crates/stage3-plan-selection/src/milp/upper_bound.rs +++ b/crates/stage3-plan-selection/src/milp/upper_bound.rs @@ -19,7 +19,6 @@ use asap_types::ir::properties::ExecutionTiming; use asap_types::stages::{Lifetime, PhysicalCandidateId}; use super::model::SelectionInputs; -use crate::cost::input_count; /// How many times the greedy plan is rebuilt without a candidate that /// made it infeasible before giving up on a bound. @@ -45,7 +44,7 @@ pub(crate) fn dearer_than_a_feasible_plan(inputs: &SelectionInputs<'_>) -> Vec = HashMap::new(); - for (&edge, &cost) in &inputs.costs.edge { + for (&edge, &cost) in &inputs.costs.edge_cost_change_per_second { let edge = inputs.physical.edge(edge); let key = (edge.consumer_candidate_id, edge.input_index_of_consumer); let cheapest = cheapest_input_edge.entry(key).or_insert(0.0); @@ -55,11 +54,11 @@ pub(crate) fn dearer_than_a_feasible_plan(inputs: &SelectionInputs<'_>) -> Vec(); least > bound + tolerance @@ -80,14 +79,14 @@ fn feasible_plan_cost_per_second(inputs: &SelectionInputs<'_>) -> Option { let count = physical.physical_candidates.len(); // Each (consumer, input)'s costed edges: (edge, producer, c_{u,c,i}). let mut into: EdgesInto = HashMap::new(); - for (&id, &cost) in &inputs.costs.edge { + for (&id, &cost) in &inputs.costs.edge_cost_change_per_second { let edge = physical.edge(id); into.entry((edge.consumer_candidate_id.0, edge.input_index_of_consumer)) .or_default() .push((edge.producer_candidate_id.0, cost)); } let inputs_of: Vec = (0..count) - .map(|v| input_count(physical, PhysicalCandidateId(v))) + .map(|v| physical.input_count(PhysicalCandidateId(v))) .collect(); let unshared = |v: usize| { let properties = physical.physical_candidates[v].physical_properties; @@ -137,7 +136,7 @@ fn feasible_plan_cost_per_second(inputs: &SelectionInputs<'_>) -> Option { let cost = plan .candidates .iter() - .filter_map(|&v| inputs.costs.candidate[v].as_ref().ok()) + .filter_map(|&v| inputs.costs.candidate_cost_per_second[v].as_ref().ok()) .sum::() + plan.edges.iter().map(|&(_, cost)| cost).sum::() + raw_data_on_disk.values().sum::(); @@ -162,7 +161,7 @@ fn cheapest_trees( for _ in 0..count.max(1) { let mut changed = false; for v in 0..count { - let Ok(own) = &inputs.costs.candidate[v] else { + let Ok(own) = &inputs.costs.candidate_cost_per_second[v] else { continue; }; if excluded[v] { diff --git a/crates/stage3-plan-selection/src/plan.rs b/crates/stage3-plan-selection/src/plan.rs index 8bd2bd51b..f955df74e 100644 --- a/crates/stage3-plan-selection/src/plan.rs +++ b/crates/stage3-plan-selection/src/plan.rs @@ -1,21 +1,62 @@ //! The plan Stage 3 builds from the baseline MILP's optimum as its last -//! step (concepts doc, section 3, "Plan output"): one `PhysicalASAPDAG` -//! node per selected candidate, its inputs from the selected edges, each -//! node's `selected_physical_candidate` set. +//! step (`docs/design_docs/concepts/memo-groups-properties-requirements.md`, +//! section 3, "Plan output"): one `PhysicalASAPDAG` node per selected +//! candidate ([`candidate_node`], which costing builds too), its inputs +//! from the selected edges, each node's `selected_physical_candidate` set. use std::collections::{BTreeMap, HashMap}; +use asap_types::ir::properties::{DataPrimitive, ExecutionDataState}; use asap_types::ir::QueryRoot; use asap_types::plan::physical_asap_dag::{ - PhysicalASAPDAG, PhysicalASAPDAGValidationError, PhysicalASAPNodeId, + PhysicalASAPDAG, PhysicalASAPDAGNode, PhysicalASAPDAGValidationError, PhysicalASAPNodeId, }; +use asap_types::plan::view_coverage; use asap_types::stages::{ - MemoGroupId, PhysicalASAPMemoCandidates, PhysicalCandidateId, PhysicalEdgeId, + Lifetime, MemoGroupId, PhysicalASAPMemoCandidates, PhysicalCandidateId, PhysicalEdgeId, }; use asap_types::workload::QueryWorkloadEntry; -use crate::cost::{candidate_node, input_count, retention_need_ms}; use crate::milp::model::{QueryId, SolvedSelection}; +use crate::retention::retention_need_ms; + +/// Candidate `v` as a plan node with id `id`, its input `i` read from node +/// `input_ids[i]`: its operator, timing, lifetime and θ, its group's schema +/// and, materialized across evaluations, its group's coverage. +/// `retention_ms` is left `None` for the caller to set. +pub(crate) fn candidate_node( + physical: &PhysicalASAPMemoCandidates, + v: PhysicalCandidateId, + id: PhysicalASAPNodeId, + input_ids: &[PhysicalASAPNodeId], +) -> PhysicalASAPDAGNode { + let memo = &physical.logical_candidates.memo; + let candidate = &physical.physical_candidates[v.0]; + let logical = memo.candidate(candidate.logical_candidate_id); + let group = &memo.groups()[candidate.logical_candidate_id.group.0 as usize].group_properties; + let mut inputs = input_ids.iter(); + let payload = logical + .expression + .operator + .map_children(|_| *inputs.next().expect("one node per input")); + let lifetime = candidate.physical_properties.lifetime(); + PhysicalASAPDAGNode { + id, + output_state: ExecutionDataState { + timing: candidate.physical_properties.timing(), + primitive: DataPrimitive::of(&payload, group.result_kind), + }, + payload, + lifetime, + parameters: logical.logical_properties.parameters().cloned(), + output_schema: group.schema.clone(), + retention_ms: None, + selected_physical_candidate: Some(v), + view_coverage: matches!(lifetime, Lifetime::AcrossEvaluations(_)) + .then(|| view_coverage(memo, candidate.logical_candidate_id.group)) + .flatten(), + } +} /// The plan of `solved`, whose output `j` answers `outputs[j]` (a query and /// the group its root, or one operator its scalar root, reads). Nodes are @@ -47,7 +88,7 @@ pub(crate) fn plan_of( let readers_of = |v: PhysicalCandidateId| consumers(v).map(|edge| physical.edge(edge).consumer_candidate_id); let inputs = |v: PhysicalCandidateId| -> Vec { - (0..input_count(physical, v)) + (0..physical.input_count(v)) .map(|i| producer_of[&(v, i)]) .collect() }; @@ -56,7 +97,7 @@ pub(crate) fn plan_of( let mut waiting: HashMap = solved .candidates .iter() - .map(|&v| (v, input_count(physical, v))) + .map(|&v| (v, physical.input_count(v))) .collect(); let mut ready: Vec = solved .candidates diff --git a/crates/stage3-plan-selection/src/report.rs b/crates/stage3-plan-selection/src/report.rs index a90cc4128..51f551507 100644 --- a/crates/stage3-plan-selection/src/report.rs +++ b/crates/stage3-plan-selection/src/report.rs @@ -2,13 +2,19 @@ //! candidates it pruned and the selection MILP's size and time; and each //! rejected candidate and why. +use std::collections::{BTreeSet, HashMap}; + use asap_cost::node::analytical::AnalyticalCostError; -use asap_cost::CostModel; +use asap_cost::{summary_type_name, CostModel}; use asap_types::accuracy::AccuracyMetric; -use asap_types::plan::physical_asap_dag::PhysicalASAPNodeId; -use asap_types::stages::{PhysicalCandidate, PrunedCandidate}; +use asap_types::ir::schema::FieldDataType; +use asap_types::ir::{ASAPOp, Operator}; +use asap_types::plan::physical_asap_dag::{PhysicalASAPDAG, PhysicalASAPNodeId}; +use asap_types::stages::{PhysicalASAPMemoCandidates, PhysicalCandidate, PrunedCandidate}; use thiserror::Error; +use crate::cost::SelectionCosts; + /// Peak memory above which a candidate's report carries a warning when the /// deployment declares no memory capacity: 10^12 bytes (1 TB), more than /// one large machine holds. @@ -147,9 +153,9 @@ pub struct Selection { /// under cost model 1, which `asap_cost::plan_cost` of the plan /// reports too. pub selected_plan_cost_per_second: f64, - /// The selection MILP's size. - pub milp_variables: usize, - pub milp_constraints: usize, + /// The selection MILP's size: its variables and its constraint rows. + pub milp_variable_count: usize, + pub milp_constraint_count: usize, /// How long building and solving the selection MILP took. pub milp_construction_ms: u64, pub milp_solve_ms: u64, @@ -186,6 +192,108 @@ pub enum ErrorSelectingPlan { UnsupportedCostModel(String), } +/// The selection MILP is infeasible, with the reasons candidates could not +/// be costed. +pub(crate) fn infeasible(costs: &SelectionCosts) -> ErrorSelectingPlan { + let mut reasons: Vec<&str> = Vec::new(); + for reason in costs + .candidate_cost_per_second + .iter() + .filter_map(|cost| cost.as_ref().err()) + { + if !reasons.contains(&reason.as_str()) { + reasons.push(reason); + } + } + let mut reason = "no plan gives every query output a candidate whose every input has an \ + admissible producer (the selection MILP is infeasible)" + .to_string(); + if !reasons.is_empty() { + reason += &format!( + "; candidates left out because they could not be costed: {}", + reasons.join("; ") + ); + } + ErrorSelectingPlan::NoValidCandidate(vec![Rejection { + id: "workload".into(), + cause: RejectionCause::Infeasible, + reason, + }]) +} + +/// Per query, the sketches its output reads in `plan` (each sketch build +/// below its root, in node order) and, when it reads one, its target's +/// metric. +pub(crate) fn accepted_accuracy( + physical: &PhysicalASAPMemoCandidates, + plan: &PhysicalASAPDAG, +) -> Vec { + let mut operator_roots = plan.roots.iter(); + let mut producers_of: HashMap> = HashMap::new(); + for edge in &plan.edges { + producers_of + .entry(edge.consumer) + .or_default() + .push(edge.producer); + } + physical + .logical_candidates + .memo + .queries() + .iter() + .enumerate() + .map(|(query, memo_query)| { + let starts: Vec = match plan + .scalar_roots + .iter() + .find(|(position, _)| *position == query) + { + Some((_, expression)) => expression.operator_refs().into_iter().copied().collect(), + None => vec![*operator_roots.next().expect("one operator root per query")], + }; + let sketches = sketches_below(plan, &producers_of, starts); + AcceptedAccuracy { + accepted_metric: (!sketches.is_empty()) + .then(|| memo_query.accuracy_target.as_ref()?.accuracy_metric) + .flatten(), + sketches, + } + }) + .collect() +} + +/// The summary type names of the sketch builds at or below `starts` in +/// `plan`, in node order, each once. +fn sketches_below( + plan: &PhysicalASAPDAG, + producers_of: &HashMap>, + starts: Vec, +) -> Vec { + let mut below = BTreeSet::new(); + let mut pending = starts; + while let Some(id) = pending.pop() { + if below.insert(id) { + pending.extend(producers_of.get(&id).into_iter().flatten().copied()); + } + } + let mut sketches: Vec = Vec::new(); + for node in plan.nodes.iter().filter(|node| below.contains(&node.id)) { + if let Operator::ASAP( + ASAPOp::SummaryAgg { summary_type, .. } + | ASAPOp::SummaryTumblingWindowChain { summary_type, .. }, + ) = &node.payload + { + if matches!(summary_type, FieldDataType::Sketch(_)) { + let name = summary_type_name(summary_type); + if !sketches.contains(&name) { + sketches.push(name); + } + } + } + } + sketches +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/stage3-plan-selection/src/retention.rs b/crates/stage3-plan-selection/src/retention.rs new file mode 100644 index 000000000..b0ced7e1f --- /dev/null +++ b/crates/stage3-plan-selection/src/retention.rs @@ -0,0 +1,80 @@ +//! How far back from the evaluation time a candidate that retains rows (a +//! materialized one, or a query-time `Scan`) must keep them, and how far +//! back one execution of a query-time `Scan` reads, for the candidates +//! that may read it: [`asap_cost::read_back_ms`] over candidates +//! (`docs/design_docs/concepts/memo-groups-properties-requirements.md`, +//! section 5, "Cost and retention"). + +use asap_types::ir::{NonASAPOp, Operator}; +use asap_types::plan::time_window::TimeIndexedRead; +use asap_types::stages::{PhysicalASAPMemoCandidates, PhysicalCandidateId}; +use asap_types::workload::QueryWorkloadEntry; + +/// How far back from the evaluation time `producer`'s rows must be kept +/// for its readers (`readers_of`, the candidates that may read it): +/// [`asap_cost::read_back_ms`] with, at each reader, the longest +/// range-evaluation span of the queries reaching it. +pub(crate) fn retention_need_ms>( + physical: &PhysicalASAPMemoCandidates, + entries: &[QueryWorkloadEntry], + producer: PhysicalCandidateId, + readers_of: impl Fn(PhysicalCandidateId) -> R, +) -> Option { + let memo = &physical.logical_candidates.memo; + let span_ms = |v: PhysicalCandidateId| { + let group = physical.physical_candidates[v.0].logical_candidate_id.group; + memo.groups()[group.0 as usize] + .reached_by_queries() + .iter() + .filter_map(|&query| entries.get(query)?.range_evaluation) + .map(|range| range.span_ms) + .max() + .unwrap_or(0) + }; + read_back(physical, producer, readers_of, span_ms) +} + +/// How far back from the evaluation time one execution of the query-time +/// `Scan` `producer` reads, for its readers (`readers_of`): +/// [`asap_cost::read_back_ms`] without a range-evaluation span. +pub(crate) fn scan_read_ms>( + physical: &PhysicalASAPMemoCandidates, + producer: PhysicalCandidateId, + readers_of: impl Fn(PhysicalCandidateId) -> R, +) -> Option { + read_back(physical, producer, readers_of, |_| 0) +} + +/// [`asap_cost::read_back_ms`] over candidates, each read as its operator +/// at its timing reads ([`TimeIndexedRead::of`]). +fn read_back>( + physical: &PhysicalASAPMemoCandidates, + producer: PhysicalCandidateId, + readers_of: impl Fn(PhysicalCandidateId) -> R, + span_ms: impl Fn(PhysicalCandidateId) -> u64, +) -> Option { + let memo = &physical.logical_candidates.memo; + let operator_of = |v: PhysicalCandidateId| { + &memo + .candidate(physical.physical_candidates[v.0].logical_candidate_id) + .expression + .operator + }; + asap_cost::read_back_ms( + producer, + readers_of, + |v| { + TimeIndexedRead::of( + operator_of(v), + physical.physical_candidates[v.0] + .physical_properties + .timing(), + ) + }, + span_ms, + matches!( + operator_of(producer), + Operator::NonASAP(NonASAPOp::Scan { .. }) + ), + ) +} diff --git a/crates/stage3-plan-selection/src/selectable.rs b/crates/stage3-plan-selection/src/selectable.rs new file mode 100644 index 000000000..f97f30726 --- /dev/null +++ b/crates/stage3-plan-selection/src/selectable.rs @@ -0,0 +1,123 @@ +//! What the baseline selection may select: the selectable edges (each +//! admissible edge whose producer delivers on every evaluation its +//! consumer serves) and, per query output, the candidates that may answer +//! it. + +use std::collections::BTreeSet; + +use asap_types::ir::properties::ExecutionTiming; +use asap_types::ir::QueryRoot; +use asap_types::stages::{ + output_references, producer_satisfies_physical_requirements, EvaluationGrid, ExecutionContext, + MemoGroupId, PhysicalASAPMemoCandidates, PhysicalCandidate, PhysicalCandidateId, PhysicalEdge, + PhysicalEdgeId, +}; +use asap_types::workload::QueryWorkloadEntry; + +use crate::cost::SelectionCosts; +use crate::milp::model::QueryId; + +/// The edges a plan may select: admissible, with a producer that delivers +/// on every evaluation its query-time consumer serves +/// ([`delivers_to_consumer`]). +pub(crate) fn selectable_edges( + physical: &PhysicalASAPMemoCandidates, + entries: &[QueryWorkloadEntry], +) -> Vec { + physical + .edges() + .filter(|(_, edge)| { + edge.is_logically_and_physically_admissible() + && delivers_to_consumer(physical, entries, edge) + }) + .map(|(id, _)| id) + .collect() +} + +/// Each query output reference (a query, and the group its operator root, +/// or one operator of its scalar root, reads), in order. +pub(crate) fn output_groups(physical: &PhysicalASAPMemoCandidates) -> Vec<(QueryId, MemoGroupId)> { + let roots: Vec> = physical + .logical_candidates + .memo + .queries() + .iter() + .map(|query| query.root.clone()) + .collect(); + output_references(&roots) + .into_iter() + .map(|(query, group)| (QueryId(query), group)) + .collect::>() + .into_iter() + .collect() +} + +/// Per output of `outputs`, the candidates of its group that may answer +/// it: costed, readable at query time and delivering on its query's +/// evaluations. +pub(crate) fn query_outputs( + physical: &PhysicalASAPMemoCandidates, + entries: &[QueryWorkloadEntry], + costs: &SelectionCosts, + outputs: &[(QueryId, MemoGroupId)], +) -> Vec<(QueryId, Vec)> { + outputs + .iter() + .map(|&(query, group)| { + let candidates = physical + .physical_candidates + .iter() + .enumerate() + .filter(|(v, candidate)| { + candidate.logical_candidate_id.group == group + && costs.candidate_cost_per_second[*v].is_ok() + && producer_satisfies_physical_requirements( + candidate, + ExecutionTiming::QueryTime, + ) + && candidate.delivers_on(physical, &evaluation_context(&entries[query.0])) + }) + .map(|(v, _)| PhysicalCandidateId(v)) + .collect(); + (query, candidates) + }) + .collect() +} + +/// The evaluation context of `entry`'s query: the grid its evaluations +/// fall on (`QueryRecurrence::evaluation_grid_ms`), anchored at the epoch. +/// Times that are not known may fall on any millisecond: a 1 ms grid, on +/// which only producers stored at every time deliver. +fn evaluation_context(entry: &QueryWorkloadEntry) -> ExecutionContext { + ExecutionContext::Evaluation { + grid: EvaluationGrid { + period_ms: entry + .recurrence + .evaluation_grid_ms(entry.range_evaluation) + .unwrap_or(1), + phase_ms: 0, + }, + } +} + +/// Whether `edge`'s producer delivers on every evaluation its query-time +/// consumer serves: on the evaluation context of each query reaching the +/// consumer's group ([`PhysicalCandidate::delivers_on`]). The baseline has +/// no other context: an ingestion-time consumer's (its pipeline) is checked +/// with execution contexts. +fn delivers_to_consumer( + physical: &PhysicalASAPMemoCandidates, + entries: &[QueryWorkloadEntry], + edge: &PhysicalEdge, +) -> bool { + let producer: &PhysicalCandidate = &physical.physical_candidates[edge.producer_candidate_id.0]; + let consumer = &physical.physical_candidates[edge.consumer_candidate_id.0]; + if consumer.physical_properties.timing() != ExecutionTiming::QueryTime { + return true; + } + physical.logical_candidates.memo.groups()[consumer.logical_candidate_id.group.0 as usize] + .reached_by_queries() + .iter() + .filter_map(|&query| entries.get(query)) + .all(|entry| producer.delivers_on(physical, &evaluation_context(entry))) +} diff --git a/crates/types/src/stages/physical/memo_candidates/mod.rs b/crates/types/src/stages/physical/memo_candidates/mod.rs index 2d07acad9..e799cc98f 100644 --- a/crates/types/src/stages/physical/memo_candidates/mod.rs +++ b/crates/types/src/stages/physical/memo_candidates/mod.rs @@ -78,6 +78,18 @@ impl PhysicalASAPMemoCandidates { &self.edges[id.0] } + /// The number of inputs of candidate `v`: its logical candidate's + /// operator's children (e.g. 2 for a binary operator). + pub fn input_count(&self, v: PhysicalCandidateId) -> usize { + self.logical_candidates + .memo + .candidate(self.physical_candidates[v.0].logical_candidate_id) + .expression + .operator + .children() + .len() + } + /// $P^{\mathrm{adm}}_{c,i}$: the admissible edges into input /// `input_index_of_consumer` of `consumer` /// ($\alpha^{\log} = \alpha^{\mathrm{phys}} = 1$). From fefab3670db63ab77c755d57b875501741c4c1b2 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:21:19 +0000 Subject: [PATCH 12/31] refactor(types,executor): split the files this PR grew by scope; inline test modules named tests.rs (#1207 PR 6) Move-only: - `types/src/plan/materialized_view.rs` -> `materialized_view/mod.rs` (the public types and the module doc on the time-indexed store and refresh), `views.rs` (`ViewIndex`, the views, refresh groups and windows) and `raw_data.rs` (`raw_data_retention`, `retains_rows`, the raw-data window checks). - executor `physical_planner/mod.rs`: the time-window lowering (`time_window_operator`, `time_range_window`, `window_read_above`, `window_below`, `first_input`, `node_below_shifts`, `below_time_shifts`, `counter_window`) -> `physical_planner/time_window.rs`; the inline `test_support` module -> `physical_planner/test_support.rs`. - `physical_planner/compilation_tests.rs` and `values/weighted_state_tests.rs` -> `tests.rs`, as every other extracted test module. --- crates/executor/src/physical_planner/mod.rs | 407 +------- .../src/physical_planner/test_support.rs | 110 +++ .../{compilation_tests.rs => tests.rs} | 0 .../src/physical_planner/time_window.rs | 300 ++++++ crates/executor/src/values/mod.rs | 2 +- .../{weighted_state_tests.rs => tests.rs} | 0 crates/types/src/plan/materialized_view.rs | 876 ------------------ .../types/src/plan/materialized_view/mod.rs | 289 ++++++ .../src/plan/materialized_view/raw_data.rs | 175 ++++ .../types/src/plan/materialized_view/views.rs | 446 +++++++++ 10 files changed, 1329 insertions(+), 1276 deletions(-) create mode 100644 crates/executor/src/physical_planner/test_support.rs rename crates/executor/src/physical_planner/{compilation_tests.rs => tests.rs} (100%) create mode 100644 crates/executor/src/physical_planner/time_window.rs rename crates/executor/src/values/{weighted_state_tests.rs => tests.rs} (100%) delete mode 100644 crates/types/src/plan/materialized_view.rs create mode 100644 crates/types/src/plan/materialized_view/mod.rs create mode 100644 crates/types/src/plan/materialized_view/raw_data.rs create mode 100644 crates/types/src/plan/materialized_view/views.rs diff --git a/crates/executor/src/physical_planner/mod.rs b/crates/executor/src/physical_planner/mod.rs index fff5384f7..6cf011585 100644 --- a/crates/executor/src/physical_planner/mod.rs +++ b/crates/executor/src/physical_planner/mod.rs @@ -20,7 +20,6 @@ use planner_types::plan::physical_asap_dag::{ PhysicalASAPDAG, PhysicalASAPDAGNode, PhysicalASAPNodeId, PhysicalASAPOperatorPayload as Payload, }; -use planner_types::plan::time_window::{self, TimeIndexedRead}; use planner_types::plan::TimeWindow; use planner_types::summary_type::{ SketchParams, SketchStatistic, SummaryInputExpr, SummaryTypeCapability, @@ -51,6 +50,12 @@ pub mod promql_values; mod compiled; pub use compiled::CompiledPhysicalDAG; +mod time_window; +use time_window::{ + below_time_shifts, counter_window, first_input, node_below_shifts, time_window_operator, + window_below, window_read_above, +}; + mod row_values; /// Compile computation without opening or retaining deployment readers. @@ -819,293 +824,6 @@ fn built_from_relation(state: &std::rc::Rc) -> bool { } } -/// The native operator of `id` when it is a `TimeRange` or a `TimeShift` -/// without an `@` anchor over a `Scan` of a SQL table or a time series, or -/// over a tumbling-window chain (below any stacked `TimeShift`s); `None` -/// for any other node. -/// -/// A `TimeRange(w)` over shifts summing to `s` keeps the rows whose time -/// column lies in `(T - s - w, T - s]` at evaluation time `T` -/// ([`time_range_window`]), the window a PromQL selector and a SQL filter -/// against `now()` read; an ingestion run keeps the window ending at its -/// `window_end_ms` ([`Operator::time_range`]). Over a time series a stale -/// marker is not a row, and an instant window keeps each series' (each -/// label set's) latest row. Over a chain of width `c`, it keeps the windows -/// whose `window_end` lies there, `w / c` of them: `c` must divide `w` and -/// `s`, and the window must be a range. A `TimeShift` only moves the window -/// its `TimeRange` reads (the range above applies its offset), so it passes -/// the rows through. An instant window over a table is refused. -fn time_window_operator( - id: NodeId, - nodes: &BTreeMap, - dependencies: &BTreeMap>, - input_schemas: &[SchemaRef], -) -> Result, Error> { - let below = match &nodes[&id].payload { - Payload::NonASAP(NonASAPOp::TimeRange { .. }) => first_input(id, dependencies), - Payload::NonASAP(NonASAPOp::TimeShift { .. }) => Some(id), - _ => return Ok(None), - }; - let Some(below) = node_below_shifts(below, nodes, dependencies) else { - return Ok(None); - }; - let chain_width_ms = match &nodes[&below].payload { - Payload::NonASAP(NonASAPOp::Scan { .. }) => None, - Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { - tumbling_window_width_ms, - .. - }) => Some( - i64::try_from(*tumbling_window_width_ms) - .map_err(|_| invalid("tumbling-window width exceeds Int64"))?, - ), - _ => return Ok(None), - }; - let [input] = input_schemas else { - return Err(invalid("a time range or shift reads one input")); - }; - let Payload::NonASAP(NonASAPOp::TimeRange { kind, .. }) = &nodes[&id].payload else { - // A shift passes its rows through. - let columns = input - .fields - .iter() - .enumerate() - .map(|(i, field)| (field.name.clone(), Expression::Column(i))) - .collect(); - return Operator::project(input.clone(), columns).map(Some); - }; - let Some(window) = time_range_window(id, nodes, dependencies) else { - return Ok(None); - }; - let millis = |ms: u64| i64::try_from(ms).map_err(|_| invalid("a time window overflows Int64")); - let (end_offset_ms, width_ms) = (millis(window.end_offset_ms)?, millis(window.width_ms)?); - let time = input - .time_index - .ok_or_else(|| invalid("a time range needs its input's time column"))?; - let (sample_value, latest_per_series) = match (&nodes[&below].payload, kind) { - (_, TimeRangeKind::Instant) if chain_width_ms.is_some() => { - return Err(invalid("an instant window over a tumbling-window chain")) - } - (Payload::ASAP(_), TimeRangeKind::Range) => { - let chain_width_ms = chain_width_ms.expect("a chain"); - if width_ms % chain_width_ms != 0 || end_offset_ms % chain_width_ms != 0 { - return Err(invalid(format!( - "a window ({end_offset_ms} ms back, {width_ms} ms wide) is not whole \ - tumbling windows of {chain_width_ms} ms" - ))); - } - (None, None) - } - ( - Payload::NonASAP(NonASAPOp::Scan { - source: planner_types::ir::operator::Source::Table { .. }, - .. - }), - TimeRangeKind::Range, - ) => (None, None), - ( - Payload::NonASAP(NonASAPOp::Scan { - source: planner_types::ir::operator::Source::Table { .. }, - .. - }), - TimeRangeKind::Instant, - ) => return Err(invalid("an instant window over a SQL table is unsupported")), - (_, kind) => { - let value = named_column(input, &ColumnRef::SampleValue)?; - // A series is its label set: every column but its time and value. - let series = (*kind == TimeRangeKind::Instant).then(|| { - (0..input.fields.len()) - .filter(|&column| column != value && column != time) - .collect() - }); - (Some(value), series) - } - }; - Operator::time_range( - input.clone(), - time, - end_offset_ms, - width_ms, - sample_value, - latest_per_series, - ) - .map(Some) -} - -/// The window the `TimeRange` `id` reads, its range over the `TimeShift`s -/// right below it ([`time_window::end_offset_below`]): `(T - s - w, T - s]` -/// at evaluation time `T`. `None` when `id` is not a `TimeRange`, or a -/// shift below it has an `@` anchor or the offsets are negative. -fn time_range_window( - id: NodeId, - nodes: &BTreeMap, - dependencies: &BTreeMap>, -) -> Option { - let Payload::NonASAP(NonASAPOp::TimeRange { range, .. }) = &nodes.get(&id)?.payload else { - return None; - }; - Some(TimeWindow { - end_offset_ms: time_window::end_offset_below( - id, - |node| first_input(node, dependencies), - |node| &nodes[&node].payload, - )?, - width_ms: u64::try_from(range.as_millis()).ok()?, - }) -} - -/// The window back from the evaluation time that the compiled consumers of -/// `id` read ([`time_window::windows_read_above`]): 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. -/// -/// 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 -/// `{0, 6m}`. -fn window_read_above( - id: NodeId, - roots: &BTreeSet, - consumers: &BTreeMap>, - nodes: &BTreeMap, -) -> Option { - if roots.contains(&id) { - return None; - } - time_window::windows_read_above( - id, - |node| consumers.get(&node).into_iter().flatten().copied(), - |node| match TimeIndexedRead::of(&nodes[&node].payload, nodes[&node].output_state.timing) { - TimeIndexedRead::Through { .. } if roots.contains(&node) => TimeIndexedRead::Whole, - read => read, - }, - true, - )? - .into_iter() - .map(|(_, window)| window) - .reduce(TimeWindow::union) -} - -/// The window back from the evaluation time of the data `id`'s state -/// summarizes: the smallest window holding every window below it -/// ([`time_window::windows_below`]). `None` when none is below it, or one -/// is read through an `@` anchor or a negative offset. -fn window_below( - id: NodeId, - nodes: &BTreeMap, - dependencies: &BTreeMap>, -) -> Option { - time_window::windows_below( - id, - |node| dependencies.get(&node).into_iter().flatten().copied(), - |node| first_input(node, dependencies), - |node| &nodes[&node].payload, - )? - .into_iter() - .reduce(TimeWindow::union) -} - -fn first_input(id: NodeId, dependencies: &BTreeMap>) -> Option { - dependencies.get(&id).and_then(|d| d.first()).copied() -} - -/// The first node at or below `start` that is not a `TimeShift`; `None` -/// when `start` is `None`, a shift has an `@` anchor, or a shift lacks its -/// child. -fn node_below_shifts( - start: Option, - nodes: &BTreeMap, - dependencies: &BTreeMap>, -) -> Option { - let mut below = start; - while let Some(node) = below { - let Payload::NonASAP(NonASAPOp::TimeShift { shift, .. }) = &nodes[&node].payload else { - return Some(node); - }; - if shift.at.is_some() { - return None; - } - below = first_input(node, dependencies); - } - None -} - -/// The first node at or below `node` that is not a `TimeShift`, following -/// each shift's child: a range reads its raw source through any number of -/// stacked shifts. `operator` gives a node's non-ASAP operator (`None` for an -/// ASAP one) and `resolve` a child's node; an error when a child is missing. -pub(crate) fn below_time_shifts<'a, N, C: 'a>( - mut node: &'a N, - operator: impl Fn(&'a N) -> Option<&'a NonASAPOp>, - resolve: impl Fn(&'a C) -> Result<&'a N, Error>, -) -> Result<&'a N, Error> { - while let Some(NonASAPOp::TimeShift { child, .. }) = operator(node) { - node = resolve(child)?; - } - Ok(node) -} - -/// The logical window of the counter evaluation `id`, as `(offset, -/// lookback)`: it ends `offset` ms before the evaluation time and spans -/// `lookback` ms. It is the union of the range windows the evaluation -/// reads, each moved back by the time shifts below its range: one range for -/// a single build or for the windows of a chain it merges. `None` -/// without a range; an error when the ranges do not form one window. -fn counter_window( - id: NodeId, - nodes: &BTreeMap, - dependencies: &BTreeMap>, -) -> Result, Error> { - let millis = - |ms: u128| i64::try_from(ms).map_err(|_| invalid("counter lookback exceeds Int64")); - let mut pending = vec![id]; - let mut visited = BTreeSet::new(); - // (offset, end of the window back from the evaluation time) per range. - let mut windows = BTreeSet::new(); - while let Some(ancestor) = pending.pop() { - if !visited.insert(ancestor) { - continue; - } - if let Payload::NonASAP(NonASAPOp::TimeRange { range, .. }) = &nodes[&ancestor].payload { - let mut offset = 0i64; - let mut below = dependencies.get(&ancestor).and_then(|d| d.first()).copied(); - while let Some(input) = below { - let Payload::NonASAP(NonASAPOp::TimeShift { shift, .. }) = &nodes[&input].payload - else { - break; - }; - if shift.at.is_none() { - offset = offset - .checked_add(shift.offset_ms) - .ok_or_else(|| invalid("counter window overflows Int64"))?; - } - below = dependencies.get(&input).and_then(|d| d.first()).copied(); - } - let back = offset - .checked_add(millis(range.as_millis())?) - .ok_or_else(|| invalid("counter window overflows Int64"))?; - windows.insert((offset, back)); - continue; - } - pending.extend(dependencies.get(&ancestor).into_iter().flatten().copied()); - } - let mut windows = windows.into_iter(); - let Some((offset, mut back)) = windows.next() else { - return Ok(None); - }; - // Back-to-back windows, newest first, form one window. - for (start, end) in windows { - if start != back { - return Err(invalid("counter evaluation has ambiguous logical windows")); - } - back = end; - } - Ok(Some((offset, back - offset))) -} - // Temporal summary evaluations produce PromQL vectors, whose range functions drop // the metric name before matching/filtering. Stored state retains its full identity. fn temporal_evaluation_drops_name(node: &PhysicalASAPDAGNode) -> bool { @@ -1838,117 +1556,8 @@ fn local_scalar(expr: &ScalarExpr) -> Result( - nodes: impl IntoIterator, - parameters: Option<&SketchParams>, - ) { - for node in nodes { - if let Payload::ASAP( - ASAPOp::SummaryAgg { - summary_type: FieldDataType::Sketch(algorithm), - .. - } - | ASAPOp::SummaryTumblingWindowChain { - summary_type: FieldDataType::Sketch(algorithm), - .. - }, - ) = &node.payload - { - node.parameters = parameters.cloned().or_else(|| { - planner_types::summary_type::parameter_list(*algorithm) - .first() - .cloned() - }); - } - } - } - - /// The materialized views at `nodes`, in that order: each node that is - /// not a view is marked materialized at ingestion time, and it and - /// every node below it run at ingestion time (their outputs and - /// outgoing edges); every view without a retention or coverage is - /// retained for ever with a coverage of its own. For tests that bind - /// states at those nodes, as a deployment binds its views. - pub fn views_at( - dag: &mut PhysicalASAPDAG, - nodes: &[NodeId], - ) -> Result, Error> { - use planner_types::ir::properties::ExecutionTiming; - let mut producers: HashMap> = HashMap::new(); - for edge in &dag.edges { - producers - .entry(edge.consumer as NodeId) - .or_default() - .push(edge.producer as NodeId); - } - let marked: Vec = dag - .nodes - .iter() - .filter(|node| { - nodes.contains(&(node.id as NodeId)) - && !matches!( - node.lifetime, - planner_types::stages::Lifetime::AcrossEvaluations(_) - ) - }) - .map(|node| node.id as NodeId) - .collect(); - let mut at_ingestion = BTreeSet::new(); - let mut pending = marked.clone(); - while let Some(id) = pending.pop() { - if at_ingestion.insert(id) { - pending.extend(producers.get(&id).into_iter().flatten()); - } - } - for node in &mut dag.nodes { - let id = node.id as NodeId; - if marked.contains(&id) { - node.lifetime = Lifetime::AcrossEvaluations(StorageMedium::Memory); - } - if matches!(node.lifetime, Lifetime::AcrossEvaluations(_)) { - node.retention_ms.get_or_insert(i64::MAX as u64); - node.view_coverage.get_or_insert_with(|| { - planner_types::plan::physical_asap_dag::ViewCoverage::new( - format!("node {id}"), - vec![], - ) - }); - } - if at_ingestion.contains(&id) { - node.output_state.timing = ExecutionTiming::IngestionTime; - } - } - for edge in &mut dag.edges { - if at_ingestion.contains(&(edge.producer as NodeId)) { - edge.data_state_mut().timing = ExecutionTiming::IngestionTime; - } - } - let views = dag - .materialized_views() - .map_err(|error| invalid(error.to_string()))?; - nodes - .iter() - .map(|&id| { - views - .iter() - .find(|view| view.node_id as NodeId == id) - .cloned() - .ok_or_else(|| invalid(format!("node {id} is not a materialized view"))) - }) - .collect() - } -} +pub mod test_support; diff --git a/crates/executor/src/physical_planner/test_support.rs b/crates/executor/src/physical_planner/test_support.rs new file mode 100644 index 000000000..fb288a546 --- /dev/null +++ b/crates/executor/src/physical_planner/test_support.rs @@ -0,0 +1,110 @@ +//! Helpers for the Planner's own tests (`test-support`). + +use super::*; +use planner_types::stages::{Lifetime, StorageMedium}; +use std::collections::HashMap; + +/// Set θ on every sketch build among `nodes` (`SummaryAgg`, +/// `SummaryTumblingWindowChain` of a sketch family): `parameters` when +/// given, else the first θ its algorithm lists (`parameter_list`). +/// Exact builds keep `None`. For tests of hand-built plans, whose nodes +/// carry no θ: pass `&mut dag.nodes`, or `[&mut node]`. +pub fn set_build_parameters<'a>( + nodes: impl IntoIterator, + parameters: Option<&SketchParams>, +) { + for node in nodes { + if let Payload::ASAP( + ASAPOp::SummaryAgg { + summary_type: FieldDataType::Sketch(algorithm), + .. + } + | ASAPOp::SummaryTumblingWindowChain { + summary_type: FieldDataType::Sketch(algorithm), + .. + }, + ) = &node.payload + { + node.parameters = parameters.cloned().or_else(|| { + planner_types::summary_type::parameter_list(*algorithm) + .first() + .cloned() + }); + } + } +} + +/// The materialized views at `nodes`, in that order: each node that is +/// not a view is marked materialized at ingestion time, and it and +/// every node below it run at ingestion time (their outputs and +/// outgoing edges); every view without a retention or coverage is +/// retained for ever with a coverage of its own. For tests that bind +/// states at those nodes, as a deployment binds its views. +pub fn views_at( + dag: &mut PhysicalASAPDAG, + nodes: &[NodeId], +) -> Result, Error> { + use planner_types::ir::properties::ExecutionTiming; + let mut producers: HashMap> = HashMap::new(); + for edge in &dag.edges { + producers + .entry(edge.consumer as NodeId) + .or_default() + .push(edge.producer as NodeId); + } + let marked: Vec = dag + .nodes + .iter() + .filter(|node| { + nodes.contains(&(node.id as NodeId)) + && !matches!( + node.lifetime, + planner_types::stages::Lifetime::AcrossEvaluations(_) + ) + }) + .map(|node| node.id as NodeId) + .collect(); + let mut at_ingestion = BTreeSet::new(); + let mut pending = marked.clone(); + while let Some(id) = pending.pop() { + if at_ingestion.insert(id) { + pending.extend(producers.get(&id).into_iter().flatten()); + } + } + for node in &mut dag.nodes { + let id = node.id as NodeId; + if marked.contains(&id) { + node.lifetime = Lifetime::AcrossEvaluations(StorageMedium::Memory); + } + if matches!(node.lifetime, Lifetime::AcrossEvaluations(_)) { + node.retention_ms.get_or_insert(i64::MAX as u64); + node.view_coverage.get_or_insert_with(|| { + planner_types::plan::physical_asap_dag::ViewCoverage::new( + format!("node {id}"), + vec![], + ) + }); + } + if at_ingestion.contains(&id) { + node.output_state.timing = ExecutionTiming::IngestionTime; + } + } + for edge in &mut dag.edges { + if at_ingestion.contains(&(edge.producer as NodeId)) { + edge.data_state_mut().timing = ExecutionTiming::IngestionTime; + } + } + let views = dag + .materialized_views() + .map_err(|error| invalid(error.to_string()))?; + nodes + .iter() + .map(|&id| { + views + .iter() + .find(|view| view.node_id as NodeId == id) + .cloned() + .ok_or_else(|| invalid(format!("node {id} is not a materialized view"))) + }) + .collect() +} diff --git a/crates/executor/src/physical_planner/compilation_tests.rs b/crates/executor/src/physical_planner/tests.rs similarity index 100% rename from crates/executor/src/physical_planner/compilation_tests.rs rename to crates/executor/src/physical_planner/tests.rs diff --git a/crates/executor/src/physical_planner/time_window.rs b/crates/executor/src/physical_planner/time_window.rs new file mode 100644 index 000000000..52fdc51b9 --- /dev/null +++ b/crates/executor/src/physical_planner/time_window.rs @@ -0,0 +1,300 @@ +//! The time-window lowering: the native operator of a `TimeRange` or a +//! `TimeShift` over raw data or a tumbling-window chain, and the windows a +//! node is read through, above it ([`window_read_above`]) or below it +//! ([`window_below`]), over the shared walks of +//! `asap_types::plan::time_window`. + +use super::*; +use planner_types::plan::time_window::{ + end_offset_below, windows_below, windows_read_above, TimeIndexedRead, +}; + +/// The native operator of `id` when it is a `TimeRange` or a `TimeShift` +/// without an `@` anchor over a `Scan` of a SQL table or a time series, or +/// over a tumbling-window chain (below any stacked `TimeShift`s); `None` +/// for any other node. +/// +/// A `TimeRange(w)` over shifts summing to `s` keeps the rows whose time +/// column lies in `(T - s - w, T - s]` at evaluation time `T` +/// ([`time_range_window`]), the window a PromQL selector and a SQL filter +/// against `now()` read; an ingestion run keeps the window ending at its +/// `window_end_ms` ([`Operator::time_range`]). Over a time series a stale +/// marker is not a row, and an instant window keeps each series' (each +/// label set's) latest row. Over a chain of width `c`, it keeps the windows +/// whose `window_end` lies there, `w / c` of them: `c` must divide `w` and +/// `s`, and the window must be a range. A `TimeShift` only moves the window +/// its `TimeRange` reads (the range above applies its offset), so it passes +/// the rows through. An instant window over a table is refused. +pub(super) fn time_window_operator( + id: NodeId, + nodes: &BTreeMap, + dependencies: &BTreeMap>, + input_schemas: &[SchemaRef], +) -> Result, Error> { + let below = match &nodes[&id].payload { + Payload::NonASAP(NonASAPOp::TimeRange { .. }) => first_input(id, dependencies), + Payload::NonASAP(NonASAPOp::TimeShift { .. }) => Some(id), + _ => return Ok(None), + }; + let Some(below) = node_below_shifts(below, nodes, dependencies) else { + return Ok(None); + }; + let chain_width_ms = match &nodes[&below].payload { + Payload::NonASAP(NonASAPOp::Scan { .. }) => None, + Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { + tumbling_window_width_ms, + .. + }) => Some( + i64::try_from(*tumbling_window_width_ms) + .map_err(|_| invalid("tumbling-window width exceeds Int64"))?, + ), + _ => return Ok(None), + }; + let [input] = input_schemas else { + return Err(invalid("a time range or shift reads one input")); + }; + let Payload::NonASAP(NonASAPOp::TimeRange { kind, .. }) = &nodes[&id].payload else { + // A shift passes its rows through. + let columns = input + .fields + .iter() + .enumerate() + .map(|(i, field)| (field.name.clone(), Expression::Column(i))) + .collect(); + return Operator::project(input.clone(), columns).map(Some); + }; + let Some(window) = time_range_window(id, nodes, dependencies) else { + return Ok(None); + }; + let millis = |ms: u64| i64::try_from(ms).map_err(|_| invalid("a time window overflows Int64")); + let (end_offset_ms, width_ms) = (millis(window.end_offset_ms)?, millis(window.width_ms)?); + let time = input + .time_index + .ok_or_else(|| invalid("a time range needs its input's time column"))?; + let (sample_value, latest_per_series) = match (&nodes[&below].payload, kind) { + (_, TimeRangeKind::Instant) if chain_width_ms.is_some() => { + return Err(invalid("an instant window over a tumbling-window chain")) + } + (Payload::ASAP(_), TimeRangeKind::Range) => { + let chain_width_ms = chain_width_ms.expect("a chain"); + if width_ms % chain_width_ms != 0 || end_offset_ms % chain_width_ms != 0 { + return Err(invalid(format!( + "a window ({end_offset_ms} ms back, {width_ms} ms wide) is not whole \ + tumbling windows of {chain_width_ms} ms" + ))); + } + (None, None) + } + ( + Payload::NonASAP(NonASAPOp::Scan { + source: planner_types::ir::operator::Source::Table { .. }, + .. + }), + TimeRangeKind::Range, + ) => (None, None), + ( + Payload::NonASAP(NonASAPOp::Scan { + source: planner_types::ir::operator::Source::Table { .. }, + .. + }), + TimeRangeKind::Instant, + ) => return Err(invalid("an instant window over a SQL table is unsupported")), + (_, kind) => { + let value = named_column(input, &ColumnRef::SampleValue)?; + // A series is its label set: every column but its time and value. + let series = (*kind == TimeRangeKind::Instant).then(|| { + (0..input.fields.len()) + .filter(|&column| column != value && column != time) + .collect() + }); + (Some(value), series) + } + }; + Operator::time_range( + input.clone(), + time, + end_offset_ms, + width_ms, + sample_value, + latest_per_series, + ) + .map(Some) +} + +/// The window the `TimeRange` `id` reads, its range over the `TimeShift`s +/// right below it ([`end_offset_below`]): `(T - s - w, T - s]` +/// at evaluation time `T`. `None` when `id` is not a `TimeRange`, or a +/// shift below it has an `@` anchor or the offsets are negative. +pub(super) fn time_range_window( + id: NodeId, + nodes: &BTreeMap, + dependencies: &BTreeMap>, +) -> Option { + let Payload::NonASAP(NonASAPOp::TimeRange { range, .. }) = &nodes.get(&id)?.payload else { + return None; + }; + Some(TimeWindow { + end_offset_ms: end_offset_below( + id, + |node| first_input(node, dependencies), + |node| &nodes[&node].payload, + )?, + width_ms: u64::try_from(range.as_millis()).ok()?, + }) +} + +/// The window back from the evaluation time that the compiled consumers of +/// `id` read ([`windows_read_above`]): 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. +/// +/// 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 +/// `{0, 6m}`. +pub(super) fn window_read_above( + id: NodeId, + roots: &BTreeSet, + consumers: &BTreeMap>, + nodes: &BTreeMap, +) -> Option { + if roots.contains(&id) { + return None; + } + windows_read_above( + id, + |node| consumers.get(&node).into_iter().flatten().copied(), + |node| match TimeIndexedRead::of(&nodes[&node].payload, nodes[&node].output_state.timing) { + TimeIndexedRead::Through { .. } if roots.contains(&node) => TimeIndexedRead::Whole, + read => read, + }, + true, + )? + .into_iter() + .map(|(_, window)| window) + .reduce(TimeWindow::union) +} + +/// The window back from the evaluation time of the data `id`'s state +/// summarizes: the smallest window holding every window below it +/// ([`windows_below`]). `None` when none is below it, or one +/// is read through an `@` anchor or a negative offset. +pub(super) fn window_below( + id: NodeId, + nodes: &BTreeMap, + dependencies: &BTreeMap>, +) -> Option { + windows_below( + id, + |node| dependencies.get(&node).into_iter().flatten().copied(), + |node| first_input(node, dependencies), + |node| &nodes[&node].payload, + )? + .into_iter() + .reduce(TimeWindow::union) +} + +pub(super) fn first_input( + id: NodeId, + dependencies: &BTreeMap>, +) -> Option { + dependencies.get(&id).and_then(|d| d.first()).copied() +} + +/// The first node at or below `start` that is not a `TimeShift`; `None` +/// when `start` is `None`, a shift has an `@` anchor, or a shift lacks its +/// child. +pub(super) fn node_below_shifts( + start: Option, + nodes: &BTreeMap, + dependencies: &BTreeMap>, +) -> Option { + let mut below = start; + while let Some(node) = below { + let Payload::NonASAP(NonASAPOp::TimeShift { shift, .. }) = &nodes[&node].payload else { + return Some(node); + }; + if shift.at.is_some() { + return None; + } + below = first_input(node, dependencies); + } + None +} + +/// The first node at or below `node` that is not a `TimeShift`, following +/// each shift's child: a range reads its raw source through any number of +/// stacked shifts. `operator` gives a node's non-ASAP operator (`None` for an +/// ASAP one) and `resolve` a child's node; an error when a child is missing. +pub(crate) fn below_time_shifts<'a, N, C: 'a>( + mut node: &'a N, + operator: impl Fn(&'a N) -> Option<&'a NonASAPOp>, + resolve: impl Fn(&'a C) -> Result<&'a N, Error>, +) -> Result<&'a N, Error> { + while let Some(NonASAPOp::TimeShift { child, .. }) = operator(node) { + node = resolve(child)?; + } + Ok(node) +} + +/// The logical window of the counter evaluation `id`, as `(offset, +/// lookback)`: it ends `offset` ms before the evaluation time and spans +/// `lookback` ms. It is the union of the range windows the evaluation +/// reads, each moved back by the time shifts below its range: one range for +/// a single build or for the windows of a chain it merges. `None` +/// without a range; an error when the ranges do not form one window. +pub(super) fn counter_window( + id: NodeId, + nodes: &BTreeMap, + dependencies: &BTreeMap>, +) -> Result, Error> { + let millis = + |ms: u128| i64::try_from(ms).map_err(|_| invalid("counter lookback exceeds Int64")); + let mut pending = vec![id]; + let mut visited = BTreeSet::new(); + // (offset, end of the window back from the evaluation time) per range. + let mut windows = BTreeSet::new(); + while let Some(ancestor) = pending.pop() { + if !visited.insert(ancestor) { + continue; + } + if let Payload::NonASAP(NonASAPOp::TimeRange { range, .. }) = &nodes[&ancestor].payload { + let mut offset = 0i64; + let mut below = dependencies.get(&ancestor).and_then(|d| d.first()).copied(); + while let Some(input) = below { + let Payload::NonASAP(NonASAPOp::TimeShift { shift, .. }) = &nodes[&input].payload + else { + break; + }; + if shift.at.is_none() { + offset = offset + .checked_add(shift.offset_ms) + .ok_or_else(|| invalid("counter window overflows Int64"))?; + } + below = dependencies.get(&input).and_then(|d| d.first()).copied(); + } + let back = offset + .checked_add(millis(range.as_millis())?) + .ok_or_else(|| invalid("counter window overflows Int64"))?; + windows.insert((offset, back)); + continue; + } + pending.extend(dependencies.get(&ancestor).into_iter().flatten().copied()); + } + let mut windows = windows.into_iter(); + let Some((offset, mut back)) = windows.next() else { + return Ok(None); + }; + // Back-to-back windows, newest first, form one window. + for (start, end) in windows { + if start != back { + return Err(invalid("counter evaluation has ambiguous logical windows")); + } + back = end; + } + Ok(Some((offset, back - offset))) +} diff --git a/crates/executor/src/values/mod.rs b/crates/executor/src/values/mod.rs index 430ca0466..8febfc053 100644 --- a/crates/executor/src/values/mod.rs +++ b/crates/executor/src/values/mod.rs @@ -293,4 +293,4 @@ pub(crate) fn plain(schema: &SchemaRef, column: usize) -> Result<(&DataType, boo } #[cfg(test)] -mod weighted_state_tests; +mod tests; diff --git a/crates/executor/src/values/weighted_state_tests.rs b/crates/executor/src/values/tests.rs similarity index 100% rename from crates/executor/src/values/weighted_state_tests.rs rename to crates/executor/src/values/tests.rs diff --git a/crates/types/src/plan/materialized_view.rs b/crates/types/src/plan/materialized_view.rs deleted file mode 100644 index d776dc487..000000000 --- a/crates/types/src/plan/materialized_view.rs +++ /dev/null @@ -1,876 +0,0 @@ -//! What a deployment runtime keeps for an installed physical ASAP DAG: its -//! materialized views ([`PhysicalASAPDAG::materialized_views`]) and how far -//! back it keeps each raw-data source for raw-data scans -//! ([`PhysicalASAPDAG::raw_data_retention`]), derived from the DAG and the -//! `retention_ms` Stage 3 sets on its materialized nodes and query-time -//! `Scan`s, which these functions check. -//! -//! A **materialized view** is a node whose output is materialized across -//! evaluations: its lifetime is `AcrossEvaluations`, at ingestion time or -//! at query time, and it carries its `retention_ms`. A node materialized for -//! `OneEvaluation` is not a view: its output is discarded after one -//! evaluation. -//! -//! ## The time-indexed store -//! -//! A runtime keeps every view, and the raw data of every raw-data `Scan`, -//! in one time-indexed, append-only store: a `SummaryTumblingWindowChain`'s -//! rows by the end of their window, any other view's state by the interval -//! of data it summarizes, raw rows by their time. Each is read only through -//! the `TimeRange` and `TimeShift` nodes above it, which select rows by time: -//! over a chain, `TimeRange(W)` over shifts summing to `o` selects, at -//! evaluation time `T`, the windows ending in `(T - o - W, T - o]`. A node's -//! `retention_ms` is how far back from the newest evaluation time its rows -//! are kept; it covers what those `TimeRange`s and `TimeShift`s above it -//! read back (`o + W`), which [`PhysicalASAPDAG::materialized_views`] and -//! [`PhysicalASAPDAG::raw_data_retention`] check, and older rows are dropped. -//! -//! ## Refreshing a view -//! -//! The views are in dependency order: every view a refresh reads is listed -//! before it. A runtime refreshes a view once all of its inputs are -//! complete: each raw-data window's samples have arrived (sealed) and each -//! view it reads is refreshed. It compiles the DAG with the executor for -//! that view's node (for a view refreshed at ingestion, together with the -//! rest of its refresh group, below), with each view it reads that the run -//! does not refresh as a materialized view slot and raw data read by -//! `Source` at each `Scan`. It refreshes the view in an ingestion run -//! ([`MaterializedViewRefresh::AtIngestion`]) or in the query run of the -//! evaluation that first reads it ([`MaterializedViewRefresh::OnFirstRead`]) -//! and appends the result to the store. An ingestion run over -//! `(window_start_ms, window_end_ms]` refreshes the windows ending at -//! `window_end_ms`: a chain builds its window ending there, each -//! `TimeRange(w)` keeps `(window_end_ms - w, window_end_ms]` whatever the -//! shifts below it, and each view it reads is read for the window ending -//! there. A view refreshed in an ingestion run therefore reads windows that -//! all end at one offset before the evaluation; a DAG with one that does -//! not is refused -//! ([`InstallableDagError::IngestionBuildReadsWindowsOfDifferentOffsets`]). -//! A query run binds the views it reaches from the roots without passing -//! another view; a view only other views read is read through them. -//! -//! ## Refresh groups -//! -//! Views refreshed in an ingestion run are refreshed by **refresh group** -//! ([`PhysicalASAPDAG::ingestion_refresh_groups`]): the views whose windows -//! have one width `W` and end at one offset `p` modulo `W` (their -//! [`RefreshWindow`]; a view with an unbounded window is a group of its -//! own). A chain's window is one of its tumbling windows. A runtime -//! refreshes a group with one ingestion run per window `(e - W, e]`, `e ≡ p -//! (mod W)`, whose roots are the views of the group; a view of the group a -//! root reads is computed in the run, and views of other groups are -//! materialized view slots. A node several roots share runs once per run, -//! so a `Scan` below two chains of equal width reads each window's raw data -//! once. Stage 3's cost assumes this grouping; a runtime that splits a group -//! computes the same states at a higher cost than priced. -//! -//! Each view's `coverage` says what its stored state is: a stable id of its -//! definition (what it computes) and its selection (which rows went in, -//! with time relative to the evaluation time). Two views with the same -//! definition id and the same concrete selection (relative time made -//! absolute at an evaluation) hold the same state, so a runtime may store -//! it once. - -use std::collections::{HashMap, HashSet}; - -use serde::{Deserialize, Serialize}; -use thiserror::Error; - -use super::physical_asap_dag::{ - PhysicalASAPDAG, PhysicalASAPDAGNode, PhysicalASAPDAGValidationError, PhysicalASAPNodeId, - ViewCoverage, -}; -use super::time_window::{self, TimeIndexedRead, TimeWindow}; -use crate::ir::operator::arguments::Source; -use crate::ir::operator::asap::ASAPOp; -use crate::ir::operator::node::Operator; -use crate::ir::operator::non_asap::NonASAPOp; -use crate::ir::properties::execution_data_state::ExecutionTiming; -use crate::stages::Lifetime; - -/// One materialized view of a DAG: what its stored state is, what it is -/// refreshed from, when, and for how long it is retained. -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct MaterializedView { - /// The node whose output is materialized. - pub node_id: PhysicalASAPNodeId, - /// Which run refreshes it. - pub(crate) refresh: MaterializedViewRefresh, - /// What the stored state is: the node's aggregate coverage. - pub(crate) coverage: ViewCoverage, - /// What its refresh reads. - pub inputs: MaterializedViewInputs, - /// The node's `retention_ms`: how far back from the newest evaluation - /// time its stored rows are kept. At least how far back the - /// `TimeRange`s and `TimeShift`s above it read. - pub(crate) retention_ms: u64, -} - -impl MaterializedView { - /// Which run refreshes it. - pub fn refresh(&self) -> MaterializedViewRefresh { - self.refresh - } - - /// How far back from the newest evaluation time its stored rows are - /// kept. - pub fn retention_ms(&self) -> u64 { - self.retention_ms - } -} - -#[cfg(any(test, feature = "test-support"))] -impl MaterializedView { - /// What the stored state is. - pub fn coverage(&self) -> &ViewCoverage { - &self.coverage - } -} - -/// What a view's refresh reads, each bound as a source of the refresh; -/// the raw data it scans is read by `Source` through the windows below it -/// ([`PhysicalASAPDAG::refresh_window`]). -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct MaterializedViewInputs { - /// The views it reads, by node id, in node order; each is listed before - /// it in [`PhysicalASAPDAG::materialized_views`]. - pub materialized_views: Vec, -} - -/// Which run refreshes a materialized view. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "snake_case")] -pub enum MaterializedViewRefresh { - /// Refreshed at ingestion time (materialized at ingestion time): the runtime runs the - /// node in an ingestion run once its inputs are complete (its windows' - /// samples have arrived and the views it reads are refreshed), and - /// keeps the result. - AtIngestion, - /// Refreshed at query time and materialized across evaluations - /// (query-time `AcrossEvaluations`): when an evaluation reads this - /// view for a selection it has not been refreshed for yet, the runtime - /// runs the node in a query run over its inputs, keeps the result, and - /// binds it like a view refreshed at ingestion. - OnFirstRead, -} - -/// The windows an ingestion run refreshes for a view -/// ([`PhysicalASAPDAG::refresh_window`]): those `width_ms` wide that end -/// `phase_ms` (`< width_ms`) after a multiple of `width_ms`, relative to the -/// evaluation time. Views refreshed at ingestion with one refresh window -/// are refreshed together, in one ingestion run per window. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] -pub struct RefreshWindow { - /// Width of each window. - pub width_ms: u64, - /// Where each window ends, modulo `width_ms`. - pub phase_ms: u64, -} - -impl RefreshWindow { - /// The refresh window of `window`: `(width_ms, end_offset_ms mod - /// width_ms)`; `None` for an empty window. - pub fn of_window(window: TimeWindow) -> Option { - let phase_ms = window.end_offset_ms.checked_rem(window.width_ms)?; - Some(RefreshWindow { - width_ms: window.width_ms, - phase_ms, - }) - } -} - -/// The views refreshed at ingestion that share one refresh window -/// ([`PhysicalASAPDAG::ingestion_refresh_groups`]). A runtime refreshes -/// them with one ingestion run per window `(e - W, e]`, whose roots are its -/// views; a view of the group a root reads is computed in the run. -#[derive(Debug, Clone, PartialEq)] -pub struct IngestionRefreshGroup { - /// The refresh window of every view of the group; `None` for a view - /// whose window is unbounded or empty, which is a group of its own. - pub refresh_window: Option, - /// The views, in dependency order. - pub views: Vec, -} - -/// How far back from the evaluation time a runtime keeps a raw-data source -/// for the DAG's raw-data scans ([`PhysicalASAPDAG::raw_data_retention`]). -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct RawDataRetention { - /// The source. - pub(crate) source: Source, - /// The longest `retention_ms` of a raw-data `Scan` of it; `None` when - /// one reads it unbounded (every row the source holds). - pub(crate) lookback_ms: Option, -} - -#[cfg(any(test, feature = "test-support"))] -impl RawDataRetention { - pub fn new(source: Source, lookback_ms: Option) -> Self { - Self { - source, - lookback_ms, - } - } - - /// The source. - pub fn source(&self) -> &Source { - &self.source - } - - /// The longest `retention_ms` of a raw-data `Scan` of it; `None` when - /// one reads it unbounded. - pub fn lookback_ms(&self) -> Option { - self.lookback_ms - } -} - -/// Why a runtime cannot install a DAG: a materialized view or a raw-data -/// window it cannot derive. -#[derive(Debug, Clone, PartialEq, Eq, Error)] -#[non_exhaustive] -pub enum InstallableDagError { - /// Bad input: the DAG is not a valid physical ASAP DAG. - #[error("invalid physical ASAP DAG: {0}")] - InvalidDag(#[from] PhysicalASAPDAGValidationError), - /// Unsatisfiable request: the DAG reads raw data through `TimeRange` - /// `time_range_node_id` in a shape whose window is not a fixed offset - /// from the evaluation time (`reason`: a subquery or a `TimeShift` - /// above it, or an `@` anchor below it), or that ends after the - /// evaluation (a negative offset below it), which no ingestion run can - /// refresh in time. Not supported yet. - #[error("raw-data window of node {time_range_node_id} is unsupported: {reason}")] - UnsupportedRawDataWindow { - time_range_node_id: PhysicalASAPNodeId, - reason: &'static str, - }, - /// Unsatisfiable request: view `node_id`'s refresh reads a source - /// `Scan` other than through a raw-data `TimeRange` (e.g. a whole SQL - /// table), which a runtime cannot feed it. - #[error("materialized view {node_id} reads a source outside a raw-data window")] - ViewReadsUnwindowedSource { node_id: PhysicalASAPNodeId }, - /// Unsatisfiable request: view `node_id` is not a summary state with a - /// coverage (`reason`), so a runtime cannot say what it stores. - #[error("materialized view {node_id} has no aggregate coverage: {reason}")] - ViewWithoutCoverage { - node_id: PhysicalASAPNodeId, - reason: String, - }, - /// Unsatisfiable request: view `node_id` is refreshed in an ingestion - /// run but reads windows that end at different offsets before the - /// evaluation (`end_offsets_ms`): its raw-data windows and the windows - /// the views it reads summarize. An ingestion run refreshes the windows - /// ending at its `window_end_ms` (each `TimeRange(w)` keeps - /// `(window_end_ms - w, window_end_ms]`, and each view it reads is - /// read for the window ending there), so no one run refreshes them - /// all; a runtime cannot build this plan. - #[error( - "materialized view {node_id} is built in an ingestion run from windows ending at \ - different offsets {end_offsets_ms:?} ms before the evaluation, which no one \ - ingestion run builds" - )] - IngestionBuildReadsWindowsOfDifferentOffsets { - node_id: PhysicalASAPNodeId, - end_offsets_ms: Vec, - }, - /// Bad input (a plan file): view `node_id` has no `retention_ms`. - #[error("materialized view {node_id} has no retention_ms")] - ViewWithoutRetention { node_id: PhysicalASAPNodeId }, - /// Bad input (a plan file): view `node_id` is retained for less than the - /// `TimeRange`s and `TimeShift`s above it read back, so a later - /// evaluation would miss its rows. - #[error("materialized view {node_id} is retained for less than its consumers read back")] - ViewRetentionTooShort { node_id: PhysicalASAPNodeId }, - /// Bad input (a plan file): raw-data `Scan` `node_id` reads a bounded - /// window but has no `retention_ms`. - #[error("query-time scan {node_id} reads a bounded window but has no retention_ms")] - ScanWithoutRetention { node_id: PhysicalASAPNodeId }, - /// Bad input (a plan file): raw-data `Scan` `node_id` keeps its rows - /// for less than its consumers' window reaches back. - #[error("query-time scan {node_id} is retained for less than its window reaches back")] - ScanRetentionTooShort { node_id: PhysicalASAPNodeId }, -} - -impl PhysicalASAPDAG { - /// Every materialized view, in dependency order (each after the views - /// it reads), with its refresh mode, coverage, inputs and the node's - /// `retention_ms`. Validates the DAG and checks that every raw-data - /// window it reads has a supported shape and that each view's retention - /// covers what the `TimeRange`s and `TimeShift`s above it read back. - /// O(views · nodes) for the inputs, plus O(nodes + edges). - pub fn materialized_views(&self) -> Result, InstallableDagError> { - if let Some(node) = self.nodes.iter().find(|node| { - matches!(node.lifetime, Lifetime::AcrossEvaluations(_)) - && !is_raw_data_scan(node) - && node.retention_ms.is_none() - }) { - return Err(InstallableDagError::ViewWithoutRetention { node_id: node.id }); - } - self.validate()?; - let index = ViewIndex::new(self); - let views = index.views()?; - for view in &views { - // A view read at a time not relative to the evaluation (`@`) is - // read unbounded, which no retention covers; it is not checked. - if index - .read_back_ms(view.node_id) - .is_some_and(|read_back_ms| view.retention_ms < read_back_ms) - { - return Err(InstallableDagError::ViewRetentionTooShort { - node_id: view.node_id, - }); - } - } - Ok(views) - } - - /// The views refreshed at ingestion, by refresh group: the views with - /// one [`RefreshWindow`] ([`Self::refresh_window`]) form one group, - /// which a runtime refreshes with one ingestion run per window - /// ([`IngestionRefreshGroup`]). Each group lists its views in dependency - /// order and comes after the groups whose views it reads. Errs as - /// [`Self::materialized_views`]. - pub fn ingestion_refresh_groups( - &self, - ) -> Result, InstallableDagError> { - Ok(self.ingestion_refresh_groups_of(&self.materialized_views()?)) - } - - /// [`Self::ingestion_refresh_groups`] of `views`, this DAG's - /// materialized views ([`Self::materialized_views`]), for a caller that - /// already derived them. - pub fn ingestion_refresh_groups_of( - &self, - views: &[MaterializedView], - ) -> Vec { - let views: Vec = views - .iter() - .filter(|view| view.refresh == MaterializedViewRefresh::AtIngestion) - .cloned() - .collect(); - let refresh_windows = self.refresh_windows(views.iter().map(|view| view.node_id)); - let mut groups: Vec = Vec::new(); - let mut group_by_window: HashMap = HashMap::new(); - let mut group_by_view: HashMap = HashMap::new(); - for view in views { - let refresh_window = refresh_windows[&view.node_id]; - let group = match refresh_window.and_then(|window| group_by_window.get(&window)) { - Some(&group) => group, - None => { - groups.push(IngestionRefreshGroup { - refresh_window, - views: Vec::new(), - }); - if let Some(window) = refresh_window { - group_by_window.insert(window, groups.len() - 1); - } - groups.len() - 1 - } - }; - group_by_view.insert(view.node_id, group); - groups[group].views.push(view); - } - // The other groups each group reads views of. - let reads: Vec> = groups - .iter() - .enumerate() - .map(|(group, of)| { - of.views - .iter() - .flat_map(|view| &view.inputs.materialized_views) - .filter_map(|read| group_by_view.get(read).copied()) - .filter(|&read| read != group) - .collect() - }) - .collect(); - // Each group after those it reads: a depth-first postorder. A view's - // window holds the windows of the views it reads, all ending where - // it ends, so two groups never read each other. - fn visit(group: usize, reads: &[Vec], placed: &mut [bool], order: &mut Vec) { - if std::mem::replace(&mut placed[group], true) { - return; - } - for &read in &reads[group] { - visit(read, reads, placed, order); - } - order.push(group); - } - let mut placed = vec![false; groups.len()]; - let mut order = Vec::with_capacity(groups.len()); - for group in 0..groups.len() { - visit(group, &reads, &mut placed, &mut order); - } - let mut groups: Vec> = groups.into_iter().map(Some).collect(); - order - .into_iter() - .filter_map(|group| groups[group].take()) - .collect() - } - - /// The refresh window of `node` ([`RefreshWindow::of_window`]): for a - /// `SummaryTumblingWindowChain` of width `w`, `(w, s mod w)` with `s` - /// the sum of the offsets of the `TimeShift`s below it; for any other - /// node, that of the smallest window holding the windows below it (each - /// `TimeRange`'s, and each chain's reached without passing one). `None` - /// when that window is unbounded (none below it, or an `@` anchor below - /// a `TimeRange`) or empty. O(nodes + edges). - pub fn refresh_window(&self, node: PhysicalASAPNodeId) -> Option { - self.refresh_windows([node])[&node] - } - - /// [`Self::refresh_window`] of each of `nodes`, by id, with the DAG - /// indexed once for all of them. - pub fn refresh_windows( - &self, - nodes: impl IntoIterator, - ) -> HashMap> { - let index = ViewIndex::new(self); - nodes - .into_iter() - .map(|node| { - let refresh_window = index - .windows_below(node) - .and_then(|windows| windows.into_iter().reduce(TimeWindow::union)) - .and_then(RefreshWindow::of_window); - (node, refresh_window) - }) - .collect() - } - - /// For each raw-data source some raw-data `Scan` (at query time, or - /// materialized at ingestion time) reads, in the order of its first such - /// `Scan`, the longest `retention_ms` among those - /// scans, `None` when one reads it unbounded (a consumer reads it - /// outside a `TimeRange` or chain): the raw data a runtime keeps for the - /// DAG's queries, the raw-data counterpart of each view's retention. A - /// source read only at ingestion time is absent. Checks that each scan - /// read through windows has a retention covering what the `TimeRange`s - /// and `TimeShift`s above it read back. - pub fn raw_data_retention(&self) -> Result, InstallableDagError> { - let index = ViewIndex::new(self); - let mut retention: Vec = Vec::new(); - for node in self.nodes.iter().filter(|node| is_raw_data_scan(node)) { - let Operator::NonASAP(NonASAPOp::Scan { source, .. }) = &node.payload else { - continue; - }; - let lookback_ms = match (index.read_back_ms(node.id), node.retention_ms) { - (None, _) => None, - (Some(read_back_ms), Some(kept_ms)) if kept_ms >= read_back_ms => Some(kept_ms), - (Some(_), Some(_)) => { - return Err(InstallableDagError::ScanRetentionTooShort { node_id: node.id }) - } - (Some(_), None) => { - return Err(InstallableDagError::ScanWithoutRetention { node_id: node.id }) - } - }; - match retention - .iter_mut() - .find(|retained| &retained.source == source) - { - Some(retained) => { - retained.lookback_ms = - retained.lookback_ms.zip(lookback_ms).map(|(a, b)| a.max(b)) - } - None => retention.push(RawDataRetention { - source: source.clone(), - lookback_ms, - }), - } - } - Ok(retention) - } - - /// Checks that every raw-data window the DAG reads (each `TimeRange`, - /// and each `SummaryTumblingWindowChain`'s tumbling window, reading a - /// `Scan` without passing another `TimeRange`) has a supported shape - /// ([`InstallableDagError::UnsupportedRawDataWindow`]). O(nodes + edges) - /// plus the depth below each window. - pub fn check_raw_data_windows(&self) -> Result<(), InstallableDagError> { - ViewIndex::new(self).check_raw_data_windows() - } -} - -/// Whether `node` is a `Scan` whose raw data is kept for its consumers to -/// read back: one run at query time, or materialized across evaluations -/// at ingestion time. It carries how long its source's rows are retained -/// for it, and is no view: raw data is stored by its `Source`. -pub(crate) fn is_raw_data_scan(node: &PhysicalASAPDAGNode) -> bool { - matches!(node.payload, Operator::NonASAP(NonASAPOp::Scan { .. })) - && (node.output_state.timing == ExecutionTiming::QueryTime - || matches!(node.lifetime, Lifetime::AcrossEvaluations(_))) -} - -impl PhysicalASAPDAGNode { - /// Whether it keeps time-indexed rows for its readers to read back, so - /// carries `retention_ms`: materialized across evaluations (a view, or - /// raw data kept at ingestion), or a raw-data `Scan` at query time. - /// - /// Example: a query-time `Scan` of `m` read by `TimeRange(5m)` keeps 5 - /// minutes of `m`; a `OneEvaluation` merge keeps nothing. - pub fn retains_rows(&self) -> bool { - matches!(self.lifetime, Lifetime::AcrossEvaluations(_)) || is_raw_data_scan(self) - } -} - -/// The nodes of a DAG by id, each node's inputs and consumers in edge -/// order, and two facts every raw-data window check reads, computed in one -/// pass each over the DAG (O(nodes + edges)); it walks down from each -/// window to check its shape. -struct ViewIndex<'a> { - dag: &'a PhysicalASAPDAG, - nodes: HashMap, - inputs: HashMap>, - consumers: HashMap>, - /// Every node after its inputs, from the DAG's node order. - children_first: Vec, - /// The nodes whose rows come from raw data: a `Scan` is reachable below - /// them without passing a `TimeRange` or `SummaryTumblingWindowChain`. - reading_raw_data: HashSet, - /// The nodes with a `TimeShift`, `TimeRange` or `PromqlSubquery` above - /// them, which evaluates them at other times than the evaluation time. - re_timed_from_above: HashSet, -} - -/// Whether `operator` evaluates its input at other times than its own. -fn re_times_its_input(operator: &Operator) -> bool { - matches!( - operator, - Operator::NonASAP( - NonASAPOp::TimeShift { .. } - | NonASAPOp::TimeRange { .. } - | NonASAPOp::PromqlSubquery { .. } - ) - ) -} - -impl<'a> ViewIndex<'a> { - fn new(dag: &'a PhysicalASAPDAG) -> Self { - let nodes: HashMap<_, _> = dag.nodes.iter().map(|n| (n.id, n)).collect(); - let mut inputs: HashMap> = HashMap::new(); - let mut consumers: HashMap> = HashMap::new(); - for edge in &dag.edges { - inputs.entry(edge.consumer).or_default().push(edge.producer); - consumers - .entry(edge.producer) - .or_default() - .push(edge.consumer); - } - // Every node after its inputs: an iterative depth-first postorder. - let mut children_first = Vec::with_capacity(dag.nodes.len()); - let mut visited = HashSet::new(); - for node in &dag.nodes { - let mut pending = vec![(node.id, false)]; - while let Some((id, inputs_done)) = pending.pop() { - if inputs_done { - children_first.push(id); - } else if visited.insert(id) { - pending.push((id, true)); - for &input in inputs.get(&id).into_iter().flatten() { - pending.push((input, false)); - } - } - } - } - let payload = |id: &PhysicalASAPNodeId| nodes.get(id).map(|node| &node.payload); - let mut reading_raw_data = HashSet::new(); - for id in &children_first { - let reads = inputs - .get(id) - .into_iter() - .flatten() - .any(|input| match payload(input) { - Some(Operator::NonASAP(NonASAPOp::Scan { .. })) => true, - Some( - Operator::NonASAP(NonASAPOp::TimeRange { .. }) - | Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }), - ) => false, - _ => reading_raw_data.contains(input), - }); - if reads { - reading_raw_data.insert(*id); - } - } - let mut re_timed_from_above = HashSet::new(); - for id in children_first.iter().rev() { - if re_timed_from_above.contains(id) || payload(id).is_some_and(re_times_its_input) { - re_timed_from_above.extend(inputs.get(id).into_iter().flatten().copied()); - } - } - ViewIndex { - dag, - nodes, - inputs, - consumers, - children_first, - reading_raw_data, - re_timed_from_above, - } - } - - fn relational(&self, id: PhysicalASAPNodeId) -> Option<&'a NonASAPOp> { - match &self.nodes.get(&id)?.payload { - Operator::NonASAP(op) => Some(op), - Operator::ASAP(_) => None, - } - } - - fn only_input(&self, id: PhysicalASAPNodeId) -> Option { - match self.inputs.get(&id).map(Vec::as_slice) { - Some([input]) => Some(*input), - _ => None, - } - } - - /// Every materialized view, in dependency order, with the node's - /// `retention_ms` (0 when unset), after checking the DAG's raw-data - /// windows. - fn views(&self) -> Result, InstallableDagError> { - self.check_raw_data_windows()?; - let views: HashSet = self - .dag - .nodes - .iter() - .filter(|node| { - matches!(node.lifetime, Lifetime::AcrossEvaluations(_)) && !is_raw_data_scan(node) - }) - .map(|node| node.id) - .collect(); - let mut derived = Vec::new(); - for &id in self.children_first.iter().filter(|id| views.contains(id)) { - let node = self.nodes[&id]; - let inputs = self.inputs_of_view(id, &views)?; - let refreshed_at_ingestion = node.output_state.timing == ExecutionTiming::IngestionTime; - if refreshed_at_ingestion { - // An ingestion run builds the windows ending at its end, so - // every window the view reads must end at one offset. - let mut end_offsets_ms: Vec = self - .windows_below(id) - .unwrap_or_default() - .into_iter() - .map(|window| window.end_offset_ms) - .collect(); - end_offsets_ms.sort_unstable(); - end_offsets_ms.dedup(); - if end_offsets_ms.len() > 1 { - return Err( - InstallableDagError::IngestionBuildReadsWindowsOfDifferentOffsets { - node_id: id, - end_offsets_ms, - }, - ); - } - } - let coverage = node.view_coverage.clone().ok_or_else(|| { - InstallableDagError::ViewWithoutCoverage { - node_id: id, - reason: "the plan node carries no view_coverage".into(), - } - })?; - derived.push(MaterializedView { - node_id: id, - refresh: if refreshed_at_ingestion { - MaterializedViewRefresh::AtIngestion - } else { - MaterializedViewRefresh::OnFirstRead - }, - coverage, - inputs, - retention_ms: node.retention_ms.unwrap_or(0), - }); - } - Ok(derived) - } - - /// The windows back from the evaluation time that `node` reads - /// ([`time_window::windows_below`]): a chain its own tumbling window; - /// any other node each `TimeRange` below it and each chain below it - /// reached without passing a `TimeRange`. `None` when one is read - /// through an `@` anchor or a negative offset. - fn windows_below(&self, node: PhysicalASAPNodeId) -> Option> { - time_window::windows_below( - node, - |id| self.inputs.get(&id).into_iter().flatten().copied(), - |id| self.only_input(id), - |id| &self.nodes[&id].payload, - ) - } - - /// How far back from the evaluation time the readers of `node` read it - /// ([`time_window::windows_read_above`]): the most any reads back. - /// `None`, unbounded, when a root or a reader at a time not relative - /// to the evaluation (`@`) reads it, or, for a raw-data `Scan`, a reader - /// other than a window (a `TimeRange` or an ingestion-time chain). - /// - /// Example: a `Scan` read by `TimeShift(1m) -> TimeRange(5m)`: 360,000. - fn read_back_ms(&self, node: PhysicalASAPNodeId) -> Option { - let root = |id: PhysicalASAPNodeId| self.dag.roots.contains(&id); - if root(node) { - return None; - } - let windows = time_window::windows_read_above( - node, - |id| self.consumers.get(&id).into_iter().flatten().copied(), - |id| match TimeIndexedRead::of( - &self.nodes[&id].payload, - self.nodes[&id].output_state.timing, - ) { - TimeIndexedRead::Through { .. } if root(id) => TimeIndexedRead::Whole, - read => read, - }, - is_raw_data_scan(self.nodes[&node]), - )?; - Some( - windows - .iter() - .map(|(_, window)| window.read_back_ms()) - .max() - .unwrap_or(0), - ) - } - - /// See [`PhysicalASAPDAG::check_raw_data_windows`]. - fn check_raw_data_windows(&self) -> Result<(), InstallableDagError> { - for node in &self.dag.nodes { - if !self.reading_raw_data.contains(&node.id) { - continue; - } - match &node.payload { - Operator::NonASAP(NonASAPOp::TimeRange { .. }) => { - self.check_raw_window_at(node.id)?; - } - Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) => { - self.check_raw_window_below(node.id)?; - } - _ => {} - } - } - Ok(()) - } - - /// Checks the shape of the raw-data window read through the `TimeRange` - /// `time_range` ([`Self::check_raw_window_below`]). - fn check_raw_window_at( - &self, - time_range: PhysicalASAPNodeId, - ) -> Result<(), InstallableDagError> { - let Some(NonASAPOp::TimeRange { .. }) = self.relational(time_range) else { - return Err(InstallableDagError::UnsupportedRawDataWindow { - time_range_node_id: time_range, - reason: "not a TimeRange", - }); - }; - self.check_raw_window_below(time_range) - } - - /// Checks that `time_range` (a `TimeRange`, or a - /// `SummaryTumblingWindowChain`'s tumbling window) reads the `Scan` - /// below it through `TimeShift`s without `@` summing to a non-negative - /// offset, or says why its shape is unsupported. A `TimeRange` run at - /// ingestion time may have no `TimeShift` or subquery above it; a query - /// run evaluates one, and a chain's windows are selected by the - /// `TimeRange`s above it. - fn check_raw_window_below( - &self, - time_range: PhysicalASAPNodeId, - ) -> Result<(), InstallableDagError> { - let unsupported = |reason| InstallableDagError::UnsupportedRawDataWindow { - time_range_node_id: time_range, - reason, - }; - let node = self.nodes[&time_range]; - if self.re_timed_from_above.contains(&time_range) - && node.output_state.timing == ExecutionTiming::IngestionTime - && matches!(node.payload, Operator::NonASAP(NonASAPOp::TimeRange { .. })) - { - return Err(unsupported("a subquery or a TimeShift above the range")); - } - let mut end_offset_ms = 0i64; - let mut current = time_range; - loop { - current = self.only_input(current).ok_or(unsupported( - "an operator with several inputs below the range", - ))?; - match self.relational(current) { - Some(NonASAPOp::TimeShift { shift, .. }) if shift.at.is_some() => { - return Err(unsupported("an @ anchor below the range")) - } - Some(NonASAPOp::TimeShift { shift, .. }) => { - end_offset_ms = end_offset_ms - .checked_add(shift.offset_ms) - .ok_or(unsupported("window offset overflows"))?; - } - Some(NonASAPOp::Scan { .. }) if end_offset_ms < 0 => { - return Err(unsupported("a negative offset below the range")) - } - Some(NonASAPOp::Scan { .. }) => return Ok(()), - _ => { - return Err(unsupported( - "an operator other than TimeShift below the range", - )) - } - } - } - } - - /// What view `node`'s refresh reads: below it, each view (`views`) it - /// reaches without passing another, in node order; a chain reading raw - /// data none. Checks the shape of each raw-data window it reads (its - /// tumbling window over raw data, or each raw-data `TimeRange` it - /// reaches without passing a view), and that it reads no `Scan` outside - /// one. - fn inputs_of_view( - &self, - node: PhysicalASAPNodeId, - views: &HashSet, - ) -> Result { - if let Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) = - &self.nodes[&node].payload - { - if self.reading_raw_data.contains(&node) { - self.check_raw_window_below(node)?; - return Ok(MaterializedViewInputs { - materialized_views: Vec::new(), - }); - } - } - let mut raw_ranges = Vec::new(); - let mut view_inputs = Vec::new(); - let mut seen = HashSet::new(); - let mut pending: Vec = self - .inputs - .get(&node) - .into_iter() - .flatten() - .copied() - .collect(); - while let Some(id) = pending.pop() { - if !seen.insert(id) { - continue; - } - if views.contains(&id) { - view_inputs.push(id); - continue; - } - match self.relational(id) { - Some(NonASAPOp::TimeRange { .. }) if self.reading_raw_data.contains(&id) => { - raw_ranges.push(id); - continue; - } - Some(NonASAPOp::Scan { .. }) => { - return Err(InstallableDagError::ViewReadsUnwindowedSource { node_id: node }) - } - _ => pending.extend(self.inputs.get(&id).into_iter().flatten().copied()), - } - } - raw_ranges.sort_unstable(); - view_inputs.sort_unstable(); - for range in raw_ranges { - self.check_raw_window_at(range)?; - } - Ok(MaterializedViewInputs { - materialized_views: view_inputs, - }) - } -} diff --git a/crates/types/src/plan/materialized_view/mod.rs b/crates/types/src/plan/materialized_view/mod.rs new file mode 100644 index 000000000..70e717a87 --- /dev/null +++ b/crates/types/src/plan/materialized_view/mod.rs @@ -0,0 +1,289 @@ +//! What a deployment runtime keeps for an installed physical ASAP DAG: its +//! materialized views ([`PhysicalASAPDAG::materialized_views`]) and how far +//! back it keeps each raw-data source for raw-data scans +//! ([`PhysicalASAPDAG::raw_data_retention`]), derived from the DAG and the +//! `retention_ms` Stage 3 sets on its materialized nodes and query-time +//! `Scan`s, which these functions check. +//! +//! A **materialized view** is a node whose output is materialized across +//! evaluations: its lifetime is `AcrossEvaluations`, at ingestion time or +//! at query time, and it carries its `retention_ms`. A node materialized for +//! `OneEvaluation` is not a view: its output is discarded after one +//! evaluation. +//! +//! ## The time-indexed store +//! +//! A runtime keeps every view, and the raw data of every raw-data `Scan`, +//! in one time-indexed, append-only store: a `SummaryTumblingWindowChain`'s +//! rows by the end of their window, any other view's state by the interval +//! of data it summarizes, raw rows by their time. Each is read only through +//! the `TimeRange` and `TimeShift` nodes above it, which select rows by time: +//! over a chain, `TimeRange(W)` over shifts summing to `o` selects, at +//! evaluation time `T`, the windows ending in `(T - o - W, T - o]`. A node's +//! `retention_ms` is how far back from the newest evaluation time its rows +//! are kept; it covers what those `TimeRange`s and `TimeShift`s above it +//! read back (`o + W`), which [`PhysicalASAPDAG::materialized_views`] and +//! [`PhysicalASAPDAG::raw_data_retention`] check, and older rows are dropped. +//! +//! ## Refreshing a view +//! +//! The views are in dependency order: every view a refresh reads is listed +//! before it. A runtime refreshes a view once all of its inputs are +//! complete: each raw-data window's samples have arrived (sealed) and each +//! view it reads is refreshed. It compiles the DAG with the executor for +//! that view's node (for a view refreshed at ingestion, together with the +//! rest of its refresh group, below), with each view it reads that the run +//! does not refresh as a materialized view slot and raw data read by +//! `Source` at each `Scan`. It refreshes the view in an ingestion run +//! ([`MaterializedViewRefresh::AtIngestion`]) or in the query run of the +//! evaluation that first reads it ([`MaterializedViewRefresh::OnFirstRead`]) +//! and appends the result to the store. An ingestion run over +//! `(window_start_ms, window_end_ms]` refreshes the windows ending at +//! `window_end_ms`: a chain builds its window ending there, each +//! `TimeRange(w)` keeps `(window_end_ms - w, window_end_ms]` whatever the +//! shifts below it, and each view it reads is read for the window ending +//! there. A view refreshed in an ingestion run therefore reads windows that +//! all end at one offset before the evaluation; a DAG with one that does +//! not is refused +//! ([`InstallableDagError::IngestionBuildReadsWindowsOfDifferentOffsets`]). +//! A query run binds the views it reaches from the roots without passing +//! another view; a view only other views read is read through them. +//! +//! ## Refresh groups +//! +//! Views refreshed in an ingestion run are refreshed by **refresh group** +//! ([`PhysicalASAPDAG::ingestion_refresh_groups`]): the views whose windows +//! have one width `W` and end at one offset `p` modulo `W` (their +//! [`RefreshWindow`]; a view with an unbounded window is a group of its +//! own). A chain's window is one of its tumbling windows. A runtime +//! refreshes a group with one ingestion run per window `(e - W, e]`, `e ≡ p +//! (mod W)`, whose roots are the views of the group; a view of the group a +//! root reads is computed in the run, and views of other groups are +//! materialized view slots. A node several roots share runs once per run, +//! so a `Scan` below two chains of equal width reads each window's raw data +//! once. Stage 3's cost assumes this grouping; a runtime that splits a group +//! computes the same states at a higher cost than priced. +//! +//! Each view's `coverage` says what its stored state is: a stable id of its +//! definition (what it computes) and its selection (which rows went in, +//! with time relative to the evaluation time). Two views with the same +//! definition id and the same concrete selection (relative time made +//! absolute at an evaluation) hold the same state, so a runtime may store +//! it once. + +use serde::{Deserialize, Serialize}; +use thiserror::Error; + +use super::physical_asap_dag::{PhysicalASAPDAGValidationError, PhysicalASAPNodeId, ViewCoverage}; +use super::time_window::TimeWindow; +use crate::ir::operator::arguments::Source; + +/// One materialized view of a DAG: what its stored state is, what it is +/// refreshed from, when, and for how long it is retained. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct MaterializedView { + /// The node whose output is materialized. + pub node_id: PhysicalASAPNodeId, + /// Which run refreshes it. + pub(crate) refresh: MaterializedViewRefresh, + /// What the stored state is: the node's aggregate coverage. + pub(crate) coverage: ViewCoverage, + /// What its refresh reads. + pub inputs: MaterializedViewInputs, + /// The node's `retention_ms`: how far back from the newest evaluation + /// time its stored rows are kept. At least how far back the + /// `TimeRange`s and `TimeShift`s above it read. + pub(crate) retention_ms: u64, +} + +impl MaterializedView { + /// Which run refreshes it. + pub fn refresh(&self) -> MaterializedViewRefresh { + self.refresh + } + + /// How far back from the newest evaluation time its stored rows are + /// kept. + pub fn retention_ms(&self) -> u64 { + self.retention_ms + } +} + +#[cfg(any(test, feature = "test-support"))] +impl MaterializedView { + /// What the stored state is. + pub fn coverage(&self) -> &ViewCoverage { + &self.coverage + } +} + +/// What a view's refresh reads, each bound as a source of the refresh; +/// the raw data it scans is read by `Source` through the windows below it +/// ([`PhysicalASAPDAG::refresh_window`]). +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct MaterializedViewInputs { + /// The views it reads, by node id, in node order; each is listed before + /// it in [`PhysicalASAPDAG::materialized_views`]. + pub materialized_views: Vec, +} + +/// Which run refreshes a materialized view. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum MaterializedViewRefresh { + /// Refreshed at ingestion time (materialized at ingestion time): the runtime runs the + /// node in an ingestion run once its inputs are complete (its windows' + /// samples have arrived and the views it reads are refreshed), and + /// keeps the result. + AtIngestion, + /// Refreshed at query time and materialized across evaluations + /// (query-time `AcrossEvaluations`): when an evaluation reads this + /// view for a selection it has not been refreshed for yet, the runtime + /// runs the node in a query run over its inputs, keeps the result, and + /// binds it like a view refreshed at ingestion. + OnFirstRead, +} + +/// The windows an ingestion run refreshes for a view +/// ([`PhysicalASAPDAG::refresh_window`]): those `width_ms` wide that end +/// `phase_ms` (`< width_ms`) after a multiple of `width_ms`, relative to the +/// evaluation time. Views refreshed at ingestion with one refresh window +/// are refreshed together, in one ingestion run per window. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct RefreshWindow { + /// Width of each window. + pub width_ms: u64, + /// Where each window ends, modulo `width_ms`. + pub phase_ms: u64, +} + +impl RefreshWindow { + /// The refresh window of `window`: `(width_ms, end_offset_ms mod + /// width_ms)`; `None` for an empty window. + pub fn of_window(window: TimeWindow) -> Option { + let phase_ms = window.end_offset_ms.checked_rem(window.width_ms)?; + Some(RefreshWindow { + width_ms: window.width_ms, + phase_ms, + }) + } +} + +/// The views refreshed at ingestion that share one refresh window +/// ([`PhysicalASAPDAG::ingestion_refresh_groups`]). A runtime refreshes +/// them with one ingestion run per window `(e - W, e]`, whose roots are its +/// views; a view of the group a root reads is computed in the run. +#[derive(Debug, Clone, PartialEq)] +pub struct IngestionRefreshGroup { + /// The refresh window of every view of the group; `None` for a view + /// whose window is unbounded or empty, which is a group of its own. + pub refresh_window: Option, + /// The views, in dependency order. + pub views: Vec, +} + +/// How far back from the evaluation time a runtime keeps a raw-data source +/// for the DAG's raw-data scans ([`PhysicalASAPDAG::raw_data_retention`]). +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct RawDataRetention { + /// The source. + pub(crate) source: Source, + /// The longest `retention_ms` of a raw-data `Scan` of it; `None` when + /// one reads it unbounded (every row the source holds). + pub(crate) lookback_ms: Option, +} + +#[cfg(any(test, feature = "test-support"))] +impl RawDataRetention { + pub fn new(source: Source, lookback_ms: Option) -> Self { + Self { + source, + lookback_ms, + } + } + + /// The source. + pub fn source(&self) -> &Source { + &self.source + } + + /// The longest `retention_ms` of a raw-data `Scan` of it; `None` when + /// one reads it unbounded. + pub fn lookback_ms(&self) -> Option { + self.lookback_ms + } +} + +/// Why a runtime cannot install a DAG: a materialized view or a raw-data +/// window it cannot derive. +#[derive(Debug, Clone, PartialEq, Eq, Error)] +#[non_exhaustive] +pub enum InstallableDagError { + /// Bad input: the DAG is not a valid physical ASAP DAG. + #[error("invalid physical ASAP DAG: {0}")] + InvalidDag(#[from] PhysicalASAPDAGValidationError), + /// Unsatisfiable request: the DAG reads raw data through `TimeRange` + /// `time_range_node_id` in a shape whose window is not a fixed offset + /// from the evaluation time (`reason`: a subquery or a `TimeShift` + /// above it, or an `@` anchor below it), or that ends after the + /// evaluation (a negative offset below it), which no ingestion run can + /// refresh in time. Not supported yet. + #[error("raw-data window of node {time_range_node_id} is unsupported: {reason}")] + UnsupportedRawDataWindow { + time_range_node_id: PhysicalASAPNodeId, + reason: &'static str, + }, + /// Unsatisfiable request: view `node_id`'s refresh reads a source + /// `Scan` other than through a raw-data `TimeRange` (e.g. a whole SQL + /// table), which a runtime cannot feed it. + #[error("materialized view {node_id} reads a source outside a raw-data window")] + ViewReadsUnwindowedSource { node_id: PhysicalASAPNodeId }, + /// Unsatisfiable request: view `node_id` is not a summary state with a + /// coverage (`reason`), so a runtime cannot say what it stores. + #[error("materialized view {node_id} has no aggregate coverage: {reason}")] + ViewWithoutCoverage { + node_id: PhysicalASAPNodeId, + reason: String, + }, + /// Unsatisfiable request: view `node_id` is refreshed in an ingestion + /// run but reads windows that end at different offsets before the + /// evaluation (`end_offsets_ms`): its raw-data windows and the windows + /// the views it reads summarize. An ingestion run refreshes the windows + /// ending at its `window_end_ms` (each `TimeRange(w)` keeps + /// `(window_end_ms - w, window_end_ms]`, and each view it reads is + /// read for the window ending there), so no one run refreshes them + /// all; a runtime cannot build this plan. + #[error( + "materialized view {node_id} is built in an ingestion run from windows ending at \ + different offsets {end_offsets_ms:?} ms before the evaluation, which no one \ + ingestion run builds" + )] + IngestionBuildReadsWindowsOfDifferentOffsets { + node_id: PhysicalASAPNodeId, + end_offsets_ms: Vec, + }, + /// Bad input (a plan file): view `node_id` has no `retention_ms`. + #[error("materialized view {node_id} has no retention_ms")] + ViewWithoutRetention { node_id: PhysicalASAPNodeId }, + /// Bad input (a plan file): view `node_id` is retained for less than the + /// `TimeRange`s and `TimeShift`s above it read back, so a later + /// evaluation would miss its rows. + #[error("materialized view {node_id} is retained for less than its consumers read back")] + ViewRetentionTooShort { node_id: PhysicalASAPNodeId }, + /// Bad input (a plan file): raw-data `Scan` `node_id` reads a bounded + /// window but has no `retention_ms`. + #[error("query-time scan {node_id} reads a bounded window but has no retention_ms")] + ScanWithoutRetention { node_id: PhysicalASAPNodeId }, + /// Bad input (a plan file): raw-data `Scan` `node_id` keeps its rows + /// for less than its consumers' window reaches back. + #[error("query-time scan {node_id} is retained for less than its window reaches back")] + ScanRetentionTooShort { node_id: PhysicalASAPNodeId }, +} + +mod raw_data; +mod views; + +pub(crate) use raw_data::is_raw_data_scan; diff --git a/crates/types/src/plan/materialized_view/raw_data.rs b/crates/types/src/plan/materialized_view/raw_data.rs new file mode 100644 index 000000000..6932b9bcb --- /dev/null +++ b/crates/types/src/plan/materialized_view/raw_data.rs @@ -0,0 +1,175 @@ +//! How far back a runtime keeps each raw-data source for the DAG's raw-data +//! scans ([`PhysicalASAPDAG::raw_data_retention`]), which nodes keep rows +//! ([`PhysicalASAPDAGNode::retains_rows`]), and the shape checks of every +//! raw-data window ([`PhysicalASAPDAG::check_raw_data_windows`]). + +use super::views::ViewIndex; +use super::{InstallableDagError, RawDataRetention}; +use crate::ir::operator::asap::ASAPOp; +use crate::ir::operator::node::Operator; +use crate::ir::operator::non_asap::NonASAPOp; +use crate::ir::properties::execution_data_state::ExecutionTiming; +use crate::plan::physical_asap_dag::{PhysicalASAPDAG, PhysicalASAPDAGNode, PhysicalASAPNodeId}; +use crate::stages::Lifetime; + +impl PhysicalASAPDAG { + /// For each raw-data source some raw-data `Scan` (at query time, or + /// materialized at ingestion time) reads, in the order of its first such + /// `Scan`, the longest `retention_ms` among those + /// scans, `None` when one reads it unbounded (a consumer reads it + /// outside a `TimeRange` or chain): the raw data a runtime keeps for the + /// DAG's queries, the raw-data counterpart of each view's retention. A + /// source read only at ingestion time is absent. Checks that each scan + /// read through windows has a retention covering what the `TimeRange`s + /// and `TimeShift`s above it read back. + pub fn raw_data_retention(&self) -> Result, InstallableDagError> { + let index = ViewIndex::new(self); + let mut retention: Vec = Vec::new(); + for node in self.nodes.iter().filter(|node| is_raw_data_scan(node)) { + let Operator::NonASAP(NonASAPOp::Scan { source, .. }) = &node.payload else { + continue; + }; + let lookback_ms = match (index.read_back_ms(node.id), node.retention_ms) { + (None, _) => None, + (Some(read_back_ms), Some(kept_ms)) if kept_ms >= read_back_ms => Some(kept_ms), + (Some(_), Some(_)) => { + return Err(InstallableDagError::ScanRetentionTooShort { node_id: node.id }) + } + (Some(_), None) => { + return Err(InstallableDagError::ScanWithoutRetention { node_id: node.id }) + } + }; + match retention + .iter_mut() + .find(|retained| &retained.source == source) + { + Some(retained) => { + retained.lookback_ms = + retained.lookback_ms.zip(lookback_ms).map(|(a, b)| a.max(b)) + } + None => retention.push(RawDataRetention { + source: source.clone(), + lookback_ms, + }), + } + } + Ok(retention) + } + + /// Checks that every raw-data window the DAG reads (each `TimeRange`, + /// and each `SummaryTumblingWindowChain`'s tumbling window, reading a + /// `Scan` without passing another `TimeRange`) has a supported shape + /// ([`InstallableDagError::UnsupportedRawDataWindow`]). O(nodes + edges) + /// plus the depth below each window. + pub fn check_raw_data_windows(&self) -> Result<(), InstallableDagError> { + ViewIndex::new(self).check_raw_data_windows() + } +} + +/// Whether `node` is a `Scan` whose raw data is kept for its consumers to +/// read back: one run at query time, or materialized across evaluations +/// at ingestion time. It carries how long its source's rows are retained +/// for it, and is no view: raw data is stored by its `Source`. +pub(crate) fn is_raw_data_scan(node: &PhysicalASAPDAGNode) -> bool { + matches!(node.payload, Operator::NonASAP(NonASAPOp::Scan { .. })) + && (node.output_state.timing == ExecutionTiming::QueryTime + || matches!(node.lifetime, Lifetime::AcrossEvaluations(_))) +} + +impl PhysicalASAPDAGNode { + /// Whether it keeps time-indexed rows for its readers to read back, so + /// carries `retention_ms`: materialized across evaluations (a view, or + /// raw data kept at ingestion), or a raw-data `Scan` at query time. + /// + /// Example: a query-time `Scan` of `m` read by `TimeRange(5m)` keeps 5 + /// minutes of `m`; a `OneEvaluation` merge keeps nothing. + pub fn retains_rows(&self) -> bool { + matches!(self.lifetime, Lifetime::AcrossEvaluations(_)) || is_raw_data_scan(self) + } +} + +impl ViewIndex<'_> { + /// See [`PhysicalASAPDAG::check_raw_data_windows`]. + pub(super) fn check_raw_data_windows(&self) -> Result<(), InstallableDagError> { + for node in &self.dag.nodes { + if !self.reading_raw_data.contains(&node.id) { + continue; + } + match &node.payload { + Operator::NonASAP(NonASAPOp::TimeRange { .. }) => { + self.check_raw_window_at(node.id)?; + } + Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) => { + self.check_raw_window_below(node.id)?; + } + _ => {} + } + } + Ok(()) + } + + /// Checks the shape of the raw-data window read through the `TimeRange` + /// `time_range` ([`Self::check_raw_window_below`]). + pub(super) fn check_raw_window_at( + &self, + time_range: PhysicalASAPNodeId, + ) -> Result<(), InstallableDagError> { + let Some(NonASAPOp::TimeRange { .. }) = self.relational(time_range) else { + return Err(InstallableDagError::UnsupportedRawDataWindow { + time_range_node_id: time_range, + reason: "not a TimeRange", + }); + }; + self.check_raw_window_below(time_range) + } + + /// Checks that `time_range` (a `TimeRange`, or a + /// `SummaryTumblingWindowChain`'s tumbling window) reads the `Scan` + /// below it through `TimeShift`s without `@` summing to a non-negative + /// offset, or says why its shape is unsupported. A `TimeRange` run at + /// ingestion time may have no `TimeShift` or subquery above it; a query + /// run evaluates one, and a chain's windows are selected by the + /// `TimeRange`s above it. + pub(super) fn check_raw_window_below( + &self, + time_range: PhysicalASAPNodeId, + ) -> Result<(), InstallableDagError> { + let unsupported = |reason| InstallableDagError::UnsupportedRawDataWindow { + time_range_node_id: time_range, + reason, + }; + let node = self.nodes[&time_range]; + if self.re_timed_from_above.contains(&time_range) + && node.output_state.timing == ExecutionTiming::IngestionTime + && matches!(node.payload, Operator::NonASAP(NonASAPOp::TimeRange { .. })) + { + return Err(unsupported("a subquery or a TimeShift above the range")); + } + let mut end_offset_ms = 0i64; + let mut current = time_range; + loop { + current = self.only_input(current).ok_or(unsupported( + "an operator with several inputs below the range", + ))?; + match self.relational(current) { + Some(NonASAPOp::TimeShift { shift, .. }) if shift.at.is_some() => { + return Err(unsupported("an @ anchor below the range")) + } + Some(NonASAPOp::TimeShift { shift, .. }) => { + end_offset_ms = end_offset_ms + .checked_add(shift.offset_ms) + .ok_or(unsupported("window offset overflows"))?; + } + Some(NonASAPOp::Scan { .. }) if end_offset_ms < 0 => { + return Err(unsupported("a negative offset below the range")) + } + Some(NonASAPOp::Scan { .. }) => return Ok(()), + _ => { + return Err(unsupported( + "an operator other than TimeShift below the range", + )) + } + } + } + } +} diff --git a/crates/types/src/plan/materialized_view/views.rs b/crates/types/src/plan/materialized_view/views.rs new file mode 100644 index 000000000..72fb1edd0 --- /dev/null +++ b/crates/types/src/plan/materialized_view/views.rs @@ -0,0 +1,446 @@ +//! The materialized views of a DAG and their refresh groups and windows +//! ([`PhysicalASAPDAG::materialized_views`], +//! [`PhysicalASAPDAG::ingestion_refresh_groups`], +//! [`PhysicalASAPDAG::refresh_window`]), derived over one index of the DAG +//! ([`ViewIndex`]). + +use std::collections::{HashMap, HashSet}; + +use super::{ + is_raw_data_scan, IngestionRefreshGroup, InstallableDagError, MaterializedView, + MaterializedViewInputs, MaterializedViewRefresh, RefreshWindow, +}; +use crate::ir::operator::asap::ASAPOp; +use crate::ir::operator::node::Operator; +use crate::ir::operator::non_asap::NonASAPOp; +use crate::ir::properties::execution_data_state::ExecutionTiming; +use crate::plan::physical_asap_dag::{PhysicalASAPDAG, PhysicalASAPDAGNode, PhysicalASAPNodeId}; +use crate::plan::time_window::{self, TimeIndexedRead, TimeWindow}; +use crate::stages::Lifetime; + +impl PhysicalASAPDAG { + /// Every materialized view, in dependency order (each after the views + /// it reads), with its refresh mode, coverage, inputs and the node's + /// `retention_ms`. Validates the DAG and checks that every raw-data + /// window it reads has a supported shape and that each view's retention + /// covers what the `TimeRange`s and `TimeShift`s above it read back. + /// O(views · nodes) for the inputs, plus O(nodes + edges). + pub fn materialized_views(&self) -> Result, InstallableDagError> { + if let Some(node) = self.nodes.iter().find(|node| { + matches!(node.lifetime, Lifetime::AcrossEvaluations(_)) + && !is_raw_data_scan(node) + && node.retention_ms.is_none() + }) { + return Err(InstallableDagError::ViewWithoutRetention { node_id: node.id }); + } + self.validate()?; + let index = ViewIndex::new(self); + let views = index.views()?; + for view in &views { + // A view read at a time not relative to the evaluation (`@`) is + // read unbounded, which no retention covers; it is not checked. + if index + .read_back_ms(view.node_id) + .is_some_and(|read_back_ms| view.retention_ms < read_back_ms) + { + return Err(InstallableDagError::ViewRetentionTooShort { + node_id: view.node_id, + }); + } + } + Ok(views) + } + + /// The views refreshed at ingestion, by refresh group: the views with + /// one [`RefreshWindow`] ([`Self::refresh_window`]) form one group, + /// which a runtime refreshes with one ingestion run per window + /// ([`IngestionRefreshGroup`]). Each group lists its views in dependency + /// order and comes after the groups whose views it reads. Errs as + /// [`Self::materialized_views`]. + pub fn ingestion_refresh_groups( + &self, + ) -> Result, InstallableDagError> { + Ok(self.ingestion_refresh_groups_of(&self.materialized_views()?)) + } + + /// [`Self::ingestion_refresh_groups`] of `views`, this DAG's + /// materialized views ([`Self::materialized_views`]), for a caller that + /// already derived them. + pub fn ingestion_refresh_groups_of( + &self, + views: &[MaterializedView], + ) -> Vec { + let views: Vec = views + .iter() + .filter(|view| view.refresh == MaterializedViewRefresh::AtIngestion) + .cloned() + .collect(); + let refresh_windows = self.refresh_windows(views.iter().map(|view| view.node_id)); + let mut groups: Vec = Vec::new(); + let mut group_by_window: HashMap = HashMap::new(); + let mut group_by_view: HashMap = HashMap::new(); + for view in views { + let refresh_window = refresh_windows[&view.node_id]; + let group = match refresh_window.and_then(|window| group_by_window.get(&window)) { + Some(&group) => group, + None => { + groups.push(IngestionRefreshGroup { + refresh_window, + views: Vec::new(), + }); + if let Some(window) = refresh_window { + group_by_window.insert(window, groups.len() - 1); + } + groups.len() - 1 + } + }; + group_by_view.insert(view.node_id, group); + groups[group].views.push(view); + } + // The other groups each group reads views of. + let reads: Vec> = groups + .iter() + .enumerate() + .map(|(group, of)| { + of.views + .iter() + .flat_map(|view| &view.inputs.materialized_views) + .filter_map(|read| group_by_view.get(read).copied()) + .filter(|&read| read != group) + .collect() + }) + .collect(); + // Each group after those it reads: a depth-first postorder. A view's + // window holds the windows of the views it reads, all ending where + // it ends, so two groups never read each other. + fn visit(group: usize, reads: &[Vec], placed: &mut [bool], order: &mut Vec) { + if std::mem::replace(&mut placed[group], true) { + return; + } + for &read in &reads[group] { + visit(read, reads, placed, order); + } + order.push(group); + } + let mut placed = vec![false; groups.len()]; + let mut order = Vec::with_capacity(groups.len()); + for group in 0..groups.len() { + visit(group, &reads, &mut placed, &mut order); + } + let mut groups: Vec> = groups.into_iter().map(Some).collect(); + order + .into_iter() + .filter_map(|group| groups[group].take()) + .collect() + } + + /// The refresh window of `node` ([`RefreshWindow::of_window`]): for a + /// `SummaryTumblingWindowChain` of width `w`, `(w, s mod w)` with `s` + /// the sum of the offsets of the `TimeShift`s below it; for any other + /// node, that of the smallest window holding the windows below it (each + /// `TimeRange`'s, and each chain's reached without passing one). `None` + /// when that window is unbounded (none below it, or an `@` anchor below + /// a `TimeRange`) or empty. O(nodes + edges). + pub fn refresh_window(&self, node: PhysicalASAPNodeId) -> Option { + self.refresh_windows([node])[&node] + } + + /// [`Self::refresh_window`] of each of `nodes`, by id, with the DAG + /// indexed once for all of them. + pub fn refresh_windows( + &self, + nodes: impl IntoIterator, + ) -> HashMap> { + let index = ViewIndex::new(self); + nodes + .into_iter() + .map(|node| { + let refresh_window = index + .windows_below(node) + .and_then(|windows| windows.into_iter().reduce(TimeWindow::union)) + .and_then(RefreshWindow::of_window); + (node, refresh_window) + }) + .collect() + } +} + +/// The nodes of a DAG by id, each node's inputs and consumers in edge +/// order, and two facts every raw-data window check reads, computed in one +/// pass each over the DAG (O(nodes + edges)); it walks down from each +/// window to check its shape. +pub(super) struct ViewIndex<'a> { + pub(super) dag: &'a PhysicalASAPDAG, + pub(super) nodes: HashMap, + pub(super) inputs: HashMap>, + pub(super) consumers: HashMap>, + /// Every node after its inputs, from the DAG's node order. + pub(super) children_first: Vec, + /// The nodes whose rows come from raw data: a `Scan` is reachable below + /// them without passing a `TimeRange` or `SummaryTumblingWindowChain`. + pub(super) reading_raw_data: HashSet, + /// The nodes with a `TimeShift`, `TimeRange` or `PromqlSubquery` above + /// them, which evaluates them at other times than the evaluation time. + pub(super) re_timed_from_above: HashSet, +} + +/// Whether `operator` evaluates its input at other times than its own. +fn re_times_its_input(operator: &Operator) -> bool { + matches!( + operator, + Operator::NonASAP( + NonASAPOp::TimeShift { .. } + | NonASAPOp::TimeRange { .. } + | NonASAPOp::PromqlSubquery { .. } + ) + ) +} + +impl<'a> ViewIndex<'a> { + pub(super) fn new(dag: &'a PhysicalASAPDAG) -> Self { + let nodes: HashMap<_, _> = dag.nodes.iter().map(|n| (n.id, n)).collect(); + let mut inputs: HashMap> = HashMap::new(); + let mut consumers: HashMap> = HashMap::new(); + for edge in &dag.edges { + inputs.entry(edge.consumer).or_default().push(edge.producer); + consumers + .entry(edge.producer) + .or_default() + .push(edge.consumer); + } + // Every node after its inputs: an iterative depth-first postorder. + let mut children_first = Vec::with_capacity(dag.nodes.len()); + let mut visited = HashSet::new(); + for node in &dag.nodes { + let mut pending = vec![(node.id, false)]; + while let Some((id, inputs_done)) = pending.pop() { + if inputs_done { + children_first.push(id); + } else if visited.insert(id) { + pending.push((id, true)); + for &input in inputs.get(&id).into_iter().flatten() { + pending.push((input, false)); + } + } + } + } + let payload = |id: &PhysicalASAPNodeId| nodes.get(id).map(|node| &node.payload); + let mut reading_raw_data = HashSet::new(); + for id in &children_first { + let reads = inputs + .get(id) + .into_iter() + .flatten() + .any(|input| match payload(input) { + Some(Operator::NonASAP(NonASAPOp::Scan { .. })) => true, + Some( + Operator::NonASAP(NonASAPOp::TimeRange { .. }) + | Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }), + ) => false, + _ => reading_raw_data.contains(input), + }); + if reads { + reading_raw_data.insert(*id); + } + } + let mut re_timed_from_above = HashSet::new(); + for id in children_first.iter().rev() { + if re_timed_from_above.contains(id) || payload(id).is_some_and(re_times_its_input) { + re_timed_from_above.extend(inputs.get(id).into_iter().flatten().copied()); + } + } + ViewIndex { + dag, + nodes, + inputs, + consumers, + children_first, + reading_raw_data, + re_timed_from_above, + } + } + + pub(super) fn relational( + &self, + id: PhysicalASAPNodeId, + ) -> Option<&'a NonASAPOp> { + match &self.nodes.get(&id)?.payload { + Operator::NonASAP(op) => Some(op), + Operator::ASAP(_) => None, + } + } + + pub(super) fn only_input(&self, id: PhysicalASAPNodeId) -> Option { + match self.inputs.get(&id).map(Vec::as_slice) { + Some([input]) => Some(*input), + _ => None, + } + } + + /// Every materialized view, in dependency order, with the node's + /// `retention_ms` (0 when unset), after checking the DAG's raw-data + /// windows. + pub(super) fn views(&self) -> Result, InstallableDagError> { + self.check_raw_data_windows()?; + let views: HashSet = self + .dag + .nodes + .iter() + .filter(|node| { + matches!(node.lifetime, Lifetime::AcrossEvaluations(_)) && !is_raw_data_scan(node) + }) + .map(|node| node.id) + .collect(); + let mut derived = Vec::new(); + for &id in self.children_first.iter().filter(|id| views.contains(id)) { + let node = self.nodes[&id]; + let inputs = self.inputs_of_view(id, &views)?; + let refreshed_at_ingestion = node.output_state.timing == ExecutionTiming::IngestionTime; + if refreshed_at_ingestion { + // An ingestion run builds the windows ending at its end, so + // every window the view reads must end at one offset. + let mut end_offsets_ms: Vec = self + .windows_below(id) + .unwrap_or_default() + .into_iter() + .map(|window| window.end_offset_ms) + .collect(); + end_offsets_ms.sort_unstable(); + end_offsets_ms.dedup(); + if end_offsets_ms.len() > 1 { + return Err( + InstallableDagError::IngestionBuildReadsWindowsOfDifferentOffsets { + node_id: id, + end_offsets_ms, + }, + ); + } + } + let coverage = node.view_coverage.clone().ok_or_else(|| { + InstallableDagError::ViewWithoutCoverage { + node_id: id, + reason: "the plan node carries no view_coverage".into(), + } + })?; + derived.push(MaterializedView { + node_id: id, + refresh: if refreshed_at_ingestion { + MaterializedViewRefresh::AtIngestion + } else { + MaterializedViewRefresh::OnFirstRead + }, + coverage, + inputs, + retention_ms: node.retention_ms.unwrap_or(0), + }); + } + Ok(derived) + } + + /// The windows back from the evaluation time that `node` reads + /// ([`time_window::windows_below`]): a chain its own tumbling window; + /// any other node each `TimeRange` below it and each chain below it + /// reached without passing a `TimeRange`. `None` when one is read + /// through an `@` anchor or a negative offset. + fn windows_below(&self, node: PhysicalASAPNodeId) -> Option> { + time_window::windows_below( + node, + |id| self.inputs.get(&id).into_iter().flatten().copied(), + |id| self.only_input(id), + |id| &self.nodes[&id].payload, + ) + } + + /// How far back from the evaluation time the readers of `node` read it + /// ([`time_window::windows_read_above`]): the most any reads back. + /// `None`, unbounded, when a root or a reader at a time not relative + /// to the evaluation (`@`) reads it, or, for a raw-data `Scan`, a reader + /// other than a window (a `TimeRange` or an ingestion-time chain). + /// + /// Example: a `Scan` read by `TimeShift(1m) -> TimeRange(5m)`: 360,000. + pub(super) fn read_back_ms(&self, node: PhysicalASAPNodeId) -> Option { + let root = |id: PhysicalASAPNodeId| self.dag.roots.contains(&id); + if root(node) { + return None; + } + let windows = time_window::windows_read_above( + node, + |id| self.consumers.get(&id).into_iter().flatten().copied(), + |id| match TimeIndexedRead::of( + &self.nodes[&id].payload, + self.nodes[&id].output_state.timing, + ) { + TimeIndexedRead::Through { .. } if root(id) => TimeIndexedRead::Whole, + read => read, + }, + is_raw_data_scan(self.nodes[&node]), + )?; + Some( + windows + .iter() + .map(|(_, window)| window.read_back_ms()) + .max() + .unwrap_or(0), + ) + } + + /// What view `node`'s refresh reads: below it, each view (`views`) it + /// reaches without passing another, in node order; a chain reading raw + /// data none. Checks the shape of each raw-data window it reads (its + /// tumbling window over raw data, or each raw-data `TimeRange` it + /// reaches without passing a view), and that it reads no `Scan` outside + /// one. + fn inputs_of_view( + &self, + node: PhysicalASAPNodeId, + views: &HashSet, + ) -> Result { + if let Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) = + &self.nodes[&node].payload + { + if self.reading_raw_data.contains(&node) { + self.check_raw_window_below(node)?; + return Ok(MaterializedViewInputs { + materialized_views: Vec::new(), + }); + } + } + let mut raw_ranges = Vec::new(); + let mut view_inputs = Vec::new(); + let mut seen = HashSet::new(); + let mut pending: Vec = self + .inputs + .get(&node) + .into_iter() + .flatten() + .copied() + .collect(); + while let Some(id) = pending.pop() { + if !seen.insert(id) { + continue; + } + if views.contains(&id) { + view_inputs.push(id); + continue; + } + match self.relational(id) { + Some(NonASAPOp::TimeRange { .. }) if self.reading_raw_data.contains(&id) => { + raw_ranges.push(id); + continue; + } + Some(NonASAPOp::Scan { .. }) => { + return Err(InstallableDagError::ViewReadsUnwindowedSource { node_id: node }) + } + _ => pending.extend(self.inputs.get(&id).into_iter().flatten().copied()), + } + } + raw_ranges.sort_unstable(); + view_inputs.sort_unstable(); + for range in raw_ranges { + self.check_raw_window_at(range)?; + } + Ok(MaterializedViewInputs { + materialized_views: view_inputs, + }) + } +} From 0409f748227fe2f0ebf95dbbe622ed3d56a4cade Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:24:12 +0000 Subject: [PATCH 13/31] refactor(planner,cost,devtools): installable_plan_of takes the plan by value and plan_installable always reports its cost; "price" names become "cost" (#1207 PR 6) - `installable_plan_of(plan: PhysicalASAPDAG, entries) -> InstallablePlan`: its `cost: Option<&PlanCost>` was `Some` from `plan_installable` and `None` from devtools, which ignored the report; taking the plan by value drops a clone of the DAG. `plan_installable` returns `(InstallablePlan, SelectedPlanReport)`, the report no longer optional. - Code this PR touched says "cost", not "price": `Planner::price`/`Priced`/ `reference_price` -> `Planner::cost`/`Costed`/`reference_cost` (planner fuzz); `priced`, `node_priced`, `usage_prices`, `unit_price` -> `costed`, `node_costed`, `unit_costs`, `measured_unit_cost` (asap-cost); and the doc comments of `PlanCost`, `NodeCost` and `PredictedNodeUsage`. --- crates/cost/src/lib.rs | 2 +- crates/cost/src/node/usage/mod.rs | 36 ++++----- crates/cost/src/plan/cost.rs | 78 +++++++++---------- crates/cost/src/plan/execution_demand.rs | 2 +- crates/devtools/src/bin/asap_planner.rs | 2 +- crates/devtools/src/planner_fuzz/mod.rs | 30 +++---- .../src/planner_fuzz/relations/mod.rs | 52 ++++++------- crates/devtools/tests/asap_planner.rs | 1 - crates/planner/src/installable/mod.rs | 26 +++---- crates/planner/src/installable/tests.rs | 7 +- crates/planner/tests/runtime_install.rs | 8 +- 11 files changed, 119 insertions(+), 125 deletions(-) diff --git a/crates/cost/src/lib.rs b/crates/cost/src/lib.rs index 540ed3034..86ef8062c 100644 --- a/crates/cost/src/lib.rs +++ b/crates/cost/src/lib.rs @@ -1,6 +1,6 @@ //! `asap-cost` — the cost model the planning stages share: how much of each //! resource a plan uses and what that usage costs. It depends only on -//! `asap-types`, so any stage can price with it. +//! `asap-types`, so any stage can cost with it. //! //! - Resource-usage estimation of one node ([`node`]): //! [`node::analytical`] and `node::operator_statistics` (the analytical diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index 84064141e..38037d4b6 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -32,7 +32,7 @@ use asap_types::workload::bounds::{ }; use asap_types::workload::DataWorkload; -/// One node's price per second of wall time, with how it was measured. A +/// One node's cost per second of wall time, with how it was measured. A /// node is one operator node of the candidate's physical plan DAG /// (`PhysicalASAPDAG`), keyed by its [`PhysicalASAPNodeId`] in /// [`PlanCost::per_node`](crate::PlanCost::per_node); not a machine. @@ -42,8 +42,8 @@ pub struct NodeCost { pub operator: OperatorKind, /// When it runs: ingestion or query time. pub timing: ExecutionTiming, - /// Its price per second under cost model 1; `None` under cost models 2 - /// and 3, which price (or limit) the whole candidate's peaks. + /// Its cost per second under cost model 1; `None` under cost models 2 + /// and 3, which cost (or limit) the whole candidate's peaks. pub cost_per_second: Option, /// Core-seconds of CPU it takes per second of wall time: its CPU /// operations and scanned bytes timed by [`ResourceSpeed`](crate::ResourceSpeed), what a @@ -102,7 +102,7 @@ fn state_family<'a>( }) } -/// One producer of a node being priced ([`node_resource_usage_and_cost`]): its payload and +/// One producer of a node being costed ([`node_resource_usage_and_cost`]): its payload and /// schema, its output statistics, the sources it reads and its state /// family. #[derive(Debug, Clone)] @@ -209,7 +209,7 @@ pub fn maintained_window_ms<'o, N: Copy + Ord, C: 'o>( None } -/// Measure and price one node of a physical DAG (one step of +/// Measure and cost one node of a physical DAG (one step of /// [`plan_cost`]; Stage 3 calls it per candidate): `producers` are its /// producers in edge order, `rate` its `(multiplier, per_second)` /// ([`computations`], [`executions_per_second`]), and `window_ms` the time @@ -234,8 +234,8 @@ pub fn node_resource_usage_and_cost<'a>( let formula_scale = settings.frozen_calibration_factor.unwrap_or(1.0); let table = &settings.measured_operation_costs; let speed = settings.resource_speed; - let usage_prices = match &settings.cost_model { - CostModel::UsageOverTime(prices) => Some(prices), + let unit_costs = match &settings.cost_model { + CostModel::UsageOverTime(unit_cost) => Some(unit_cost), _ => None, }; let sketch_size = @@ -300,7 +300,7 @@ pub fn node_resource_usage_and_cost<'a>( .collect::>(), ); let operator = OperatorKind::of(&node.payload); - let unit_price = match &node.payload { + let measured_unit_cost = match &node.payload { Payload::NonASAP(NonASAPOp::Aggregate { measures, .. }) => { aggregate_lookup(table, node.output_state.timing, measures) } @@ -313,7 +313,7 @@ pub fn node_resource_usage_and_cost<'a>( ), }; // The kernel's measured bytes per state, when the table has them. - let measured_state_bytes = unit_price + let measured_state_bytes = measured_unit_cost .and_then(|p| p.memory_bytes_per_state) .map(|bytes| bytes.round() as u64); let mut cost_source = NodeCostSource::Analytical; @@ -788,7 +788,7 @@ pub fn node_resource_usage_and_cost<'a>( let (multiplier, per_second) = rate; let formula_ms = estimate.cpu_ops() * speed.ms_per_cpu_op + estimate.scan_bytes() as f64 * speed.ms_per_scanned_byte; - // What one computation processes, the unit its entry is priced per + // What one computation processes, the unit its entry is costed per // ([`OperatorKind`]): the samples a scan reads, the states a merge // reads, otherwise the rows of its (first) input. let units = match &node.payload { @@ -801,7 +801,7 @@ pub fn node_resource_usage_and_cost<'a>( // any other node its formulas' CPU operations times the calibration // factor, its scanned bytes unscaled (scaling only the CPU keeps it // non-negative whatever the factor). - let estimate = match unit_price { + let estimate = match measured_unit_cost { Some(unit) => { cost_source = NodeCostSource::Measured; let measured_ms = (units as f64 * unit.ns_per_unit + unit.ns_per_invocation) * 1e-6; @@ -942,14 +942,14 @@ pub fn node_resource_usage_and_cost<'a>( average_memory_bytes += bytes as f64 * node_ms / 1_000.0 * per_second; detail += &format!("; holds {bytes} bytes for {node_ms:.3} ms"); } - // One computation's CPU and scan price times its rate, then the + // One computation's CPU and scan cost times its rate, then the // materialized state's. - let cost = usage_prices.map(|prices| { - (estimate.cpu_ops() * prices.cost_per_cpu_op - + estimate.scan_bytes() as f64 * prices.cost_per_scanned_byte) + let cost = unit_costs.map(|unit_cost| { + (estimate.cpu_ops() * unit_cost.cost_per_cpu_op + + estimate.scan_bytes() as f64 * unit_cost.cost_per_scanned_byte) * per_second - + prices.cost_per_memory_byte_second * average_memory_bytes - + prices.cost_per_disk_byte_second * disk_bytes as f64 + + unit_cost.cost_per_memory_byte_second * average_memory_bytes + + unit_cost.cost_per_disk_byte_second * disk_bytes as f64 }); if cost.is_some_and(|cost| !cost.is_finite()) { return Err(AnalyticalCostError::Overflow); @@ -981,7 +981,7 @@ pub fn node_resource_usage_and_cost<'a>( }) } -/// [`PlanCost::source`] for nodes priced as `per_node` says. +/// [`PlanCost::source`] for nodes costed as `per_node` says. pub(crate) fn cost_source(per_node: &BTreeMap) -> &'static str { let measured = |node: &NodeCost| node.cost_source == NodeCostSource::Measured; if !per_node.values().any(measured) { diff --git a/crates/cost/src/plan/cost.rs b/crates/cost/src/plan/cost.rs index a41a39b9b..7904d2c11 100644 --- a/crates/cost/src/plan/cost.rs +++ b/crates/cost/src/plan/cost.rs @@ -31,22 +31,22 @@ use asap_types::plan::time_window::TimeIndexedRead; use asap_types::summary_type::SketchParams; use asap_types::workload::{DataWorkload, QueryWorkloadEntry}; -/// One candidate's usage and price over the whole workload: `usage` is -/// the whole candidate's, `per_node` one price per operator node of its +/// One candidate's usage and cost over the whole workload: `usage` is +/// the whole candidate's, `per_node` one cost per operator node of its /// physical plan DAG. #[derive(Debug, Clone, PartialEq)] pub struct PlanCost { pub usage: ResourceUsage, - /// Its price per second under cost model 1 or 2 (under cost model 1, the + /// Its cost per second under cost model 1 or 2 (under cost model 1, the /// sum of its nodes' charges plus request overhead for queries without /// operator nodes); `None` under cost model 3. pub cost_per_second: Option, - /// Where its prices came from, from its nodes' [`NodeCost::cost_source`]: + /// Where its costs came from, from its nodes' [`NodeCost::cost_source`]: /// [`COST_SOURCE`](crate::COST_SOURCE) (all formulas), [`MEASURED_COST_SOURCE`] (all /// measured) or [`MIXED_COST_SOURCE`]. pub source: &'static str, /// Each operator node of the candidate's physical plan DAG, by its id - /// in that DAG, with its price. + /// in that DAG, with its cost. pub per_node: BTreeMap, } @@ -73,7 +73,7 @@ impl QueryLatency { } /// Measure every node of `dag` per second of wall time (module doc, "Cost -/// model") and price it under `settings.cost_model`. Nodes are exported +/// model") and cost it under `settings.cost_model`. Nodes are exported /// children first, so each node's input statistics are known when it is /// reached. A query-time node is measured per computation over the rows its /// time ranges keep, times how often it is computed per second @@ -95,13 +95,13 @@ pub fn plan_cost( let table = &settings.measured_operation_costs; let mut fit = CalibrationFit::default(); let speed = settings.resource_speed; - // Cost model 1 prices each node as it is measured. - let usage_prices = match &settings.cost_model { - CostModel::UsageOverTime(prices) => Some(prices), + // Cost model 1 costs each node as it is measured. + let unit_costs = match &settings.cost_model { + CostModel::UsageOverTime(unit_cost) => Some(unit_cost), _ => None, }; let EachNodeResourceUsageAndCost { - priced, + costed, multipliers, } = each_node_resource_usage_and_cost(dag, nodes, entries, data, settings)?; let mut output: HashMap = HashMap::new(); @@ -126,30 +126,30 @@ pub fn plan_cost( let sketch_size = |params: &SketchParams| sketch_shape(params, StateInputRows::UNKNOWN).1 as f64; for node in &dag.nodes { - let node_priced = &priced[&node.id]; - usage.cpu_ops_per_second += node_priced.cpu_ops_per_second; - usage.scanned_bytes_per_second += node_priced.scanned_bytes_per_second; - usage.average_memory_bytes += node_priced.average_memory_bytes; + let node_costed = &costed[&node.id]; + usage.cpu_ops_per_second += node_costed.cpu_ops_per_second; + usage.scanned_bytes_per_second += node_costed.scanned_bytes_per_second; + usage.average_memory_bytes += node_costed.average_memory_bytes; usage.peak_memory_bytes = usage .peak_memory_bytes - .saturating_add(node_priced.peak_memory_bytes); - usage.disk_bytes = usage.disk_bytes.saturating_add(node_priced.disk_bytes); - fit.add(node_priced.fit); - work_ms.insert(node.id, node_priced.work_ms_per_execution); - output.insert(node.id, node_priced.output); - if node_priced.family != StateFamily::None { - family_of.insert(node.id, node_priced.family); + .saturating_add(node_costed.peak_memory_bytes); + usage.disk_bytes = usage.disk_bytes.saturating_add(node_costed.disk_bytes); + fit.add(node_costed.fit); + work_ms.insert(node.id, node_costed.work_ms_per_execution); + output.insert(node.id, node_costed.output); + if node_costed.family != StateFamily::None { + family_of.insert(node.id, node_costed.family); } - if let Some(window) = node_priced.maintained { + if let Some(window) = node_costed.maintained { maintained.push((node.id, window)); } - if let Some((source, bytes)) = node_priced.raw_scan { + if let Some((source, bytes)) = node_costed.raw_scan { let largest = raw_data.entry(source).or_insert((bytes, node.id)); if bytes > largest.0 { *largest = (bytes, node.id); } } - per_node.insert(node.id, node_priced.cost.clone()); + per_node.insert(node.id, node_costed.cost.clone()); } // The runtime's fixed cost per query request, charged to the node the // query reads first and added to its latency below. @@ -176,8 +176,8 @@ pub fn plan_cost( evaluation_ms_per_query[query] = evaluation_ms; let Some(&first_read) = start.first() else { has_unattached_request_overhead = true; - if let Some(prices) = usage_prices { - unattached_request_cost += prices.cost_per_cpu_op * ops_per_second; + if let Some(unit_cost) = unit_costs { + unattached_request_cost += unit_cost.cost_per_cpu_op * ops_per_second; if !unattached_request_cost.is_finite() { return Err((diagnostic_node, AnalyticalCostError::Overflow)); } @@ -196,8 +196,8 @@ pub fn plan_cost( query + 1, table.ns_per_evaluation ); - if let (Some(cost), Some(prices)) = (&mut node.cost_per_second, usage_prices) { - *cost += prices.cost_per_cpu_op * ops_per_second; + if let (Some(cost), Some(unit_cost)) = (&mut node.cost_per_second, unit_costs) { + *cost += unit_cost.cost_per_cpu_op * ops_per_second; if !cost.is_finite() { return Err((first_read, AnalyticalCostError::Overflow)); } @@ -272,11 +272,11 @@ pub fn plan_cost( } total } - CostModel::ProvisionedCapacity(prices) => { - let cost = prices.cost_per_provisioned_memory_byte_second + CostModel::ProvisionedCapacity(unit_cost) => { + let cost = unit_cost.cost_per_provisioned_memory_byte_second * usage.peak_memory_bytes as f64 - + prices.cost_per_provisioned_cpu_core_second * f64::from(usage.peak_cpu_cores) - + prices.cost_per_disk_byte_second * usage.disk_bytes as f64; + + unit_cost.cost_per_provisioned_cpu_core_second * f64::from(usage.peak_cpu_cores) + + unit_cost.cost_per_disk_byte_second * usage.disk_bytes as f64; if !cost.is_finite() { return Err((first, AnalyticalCostError::Overflow)); } @@ -362,10 +362,10 @@ pub fn raw_data_disk_cost_per_second(bytes: u64, settings: &CostInputs) -> Optio } } -/// Every node of a physical DAG priced ([`each_node_resource_usage_and_cost`]), with each +/// Every node of a physical DAG costed ([`each_node_resource_usage_and_cost`]), with each /// node's `(multiplier, per_second)` ([`computations`]). struct EachNodeResourceUsageAndCost<'a> { - priced: HashMap>, + costed: HashMap>, multipliers: HashMap, } @@ -387,13 +387,13 @@ fn each_node_resource_usage_and_cost<'a>( producers_of.entry(e.consumer).or_default().push(e.producer); consumers_of.entry(e.producer).or_default().push(e.consumer); } - let mut priced: HashMap> = HashMap::new(); + let mut costed: HashMap> = HashMap::new(); for node in &dag.nodes { let views: Vec> = producers_of .get(&node.id) .map_or(&[][..], Vec::as_slice) .iter() - .map(|p| priced[p].producer_output(&nodes[p].payload, &nodes[p].output_schema)) + .map(|p| costed[p].producer_output(&nodes[p].payload, &nodes[p].output_schema)) .collect(); let window_ms = match node.output_state.timing { ExecutionTiming::IngestionTime => maintained_window_ms( @@ -403,7 +403,7 @@ fn each_node_resource_usage_and_cost<'a>( ), ExecutionTiming::QueryTime => scan_read_ms(node, nodes, &consumers_of), }; - let node_priced = node_resource_usage_and_cost( + let node_costed = node_resource_usage_and_cost( node, &views, multipliers[&node.id], @@ -412,10 +412,10 @@ fn each_node_resource_usage_and_cost<'a>( settings, ) .map_err(|error| (node.id, error))?; - priced.insert(node.id, node_priced); + costed.insert(node.id, node_costed); } Ok(EachNodeResourceUsageAndCost { - priced, + costed, multipliers, }) } diff --git a/crates/cost/src/plan/execution_demand.rs b/crates/cost/src/plan/execution_demand.rs index 55b7cdfc5..b939d8fa6 100644 --- a/crates/cost/src/plan/execution_demand.rs +++ b/crates/cost/src/plan/execution_demand.rs @@ -5,7 +5,7 @@ //! one-off/scheduled demand uses a batch maximum. //! Call `schedule_groups` or `evaluation_rate` on the chosen query subset //! only; neither enumerates subsets. For retained query-time state pass `None` for each range evaluation -//! to price once per request, as `computations` does. +//! to cost once per request, as `computations` does. use crate::node::analytical::AnalyticalCostError; use asap_types::ir::properties::ExecutionTiming; diff --git a/crates/devtools/src/bin/asap_planner.rs b/crates/devtools/src/bin/asap_planner.rs index 2e8fdde5c..0709e5c4e 100644 --- a/crates/devtools/src/bin/asap_planner.rs +++ b/crates/devtools/src/bin/asap_planner.rs @@ -569,7 +569,7 @@ fn materialized_views_json( plan: &asap_types::plan::physical_asap_dag::PhysicalASAPDAG, entries: &[QueryWorkloadEntry], ) -> Result, String> { - let (plan, _) = installable_plan_of(plan, None, entries).map_err(|e| e.to_string())?; + let plan = installable_plan_of(plan.clone(), entries).map_err(|e| e.to_string())?; let views = plan.materialized_views().map_err(|e| e.to_string())?; views .iter() diff --git a/crates/devtools/src/planner_fuzz/mod.rs b/crates/devtools/src/planner_fuzz/mod.rs index 11e210527..8533cbd88 100644 --- a/crates/devtools/src/planner_fuzz/mod.rs +++ b/crates/devtools/src/planner_fuzz/mod.rs @@ -12,7 +12,7 @@ //! //! ```text //! cost(W ∪ {q'}) − cost(W) ≤ derive_cost + tolerance, -//! derive_cost = cost(witness) − cost(W's plan priced the same way) +//! derive_cost = cost(witness) − cost(W's plan costed the same way) //! ``` //! //! must hold. `derive_cost` is what Stage 3 charges for the derived @@ -352,10 +352,10 @@ pub struct Planned { pub logical_roots: Option>, } -/// A plan priced by Stage 2 and Stage 3 outside the search: the cheapest +/// A plan costed by Stage 2 and Stage 3 outside the search: the cheapest /// physical properties assignment of given logical roots. #[derive(Debug, Clone, PartialEq, Serialize)] -pub struct Priced { +pub struct Costed { pub selected: String, pub cost_per_second: f64, /// The nodes only the last root reads: in a witness, the operators @@ -377,7 +377,7 @@ pub struct PrivateNodes { pub struct Planner { pub settings: FuzzSettings, cache: HashMap, String>>, - reference_prices: HashMap>, + reference_costs: HashMap>, } impl Planner { @@ -385,7 +385,7 @@ impl Planner { Self { settings, cache: HashMap::new(), - reference_prices: HashMap::new(), + reference_costs: HashMap::new(), } } @@ -449,7 +449,7 @@ impl Planner { /// queries (one root per query, in order), with the same models as /// [`plan`](Self::plan): the selected plan's cost (`plan_cost`) and the /// cost of the nodes only its last root reads. - pub fn price(&self, workload: &FuzzWorkload, roots: &[QueryRoot]) -> Result { + pub fn cost(&self, workload: &FuzzWorkload, roots: &[QueryRoot]) -> Result { let planning = workload.planning_workload(); let entries: Vec<_> = planning.query_workload.entries().collect(); let cost_inputs = self.cost_inputs(); @@ -497,7 +497,7 @@ impl Planner { .filter(|node| others.iter().all(|set| !set.contains(node))) .filter_map(|node| cost.per_node.get(node)) .collect(); - Ok(Priced { + Ok(Costed { selected: "selected".into(), cost_per_second, derived_root: PrivateNodes { @@ -510,20 +510,20 @@ impl Planner { }) } - /// [`price`](Self::price) of `workload`'s selected logical plan + /// [`cost`](Self::cost) of `workload`'s selected logical plan /// `roots`, cached per workload: every marginal check of a case prices /// the same `W`. - pub fn reference_price( + pub fn reference_cost( &mut self, workload: &FuzzWorkload, roots: &[QueryRoot], - ) -> Result { + ) -> Result { let key = serde_json::to_string(workload).expect("a workload serializes"); - if let Some(priced) = self.reference_prices.get(&key) { - return priced.clone(); + if let Some(costed) = self.reference_costs.get(&key) { + return costed.clone(); } - let priced = self.price(workload, roots); - self.reference_prices.insert(key, priced.clone()); - priced + let costed = self.cost(workload, roots); + self.reference_costs.insert(key, costed.clone()); + costed } } diff --git a/crates/devtools/src/planner_fuzz/relations/mod.rs b/crates/devtools/src/planner_fuzz/relations/mod.rs index da20e4b0a..cdc0b0bfe 100644 --- a/crates/devtools/src/planner_fuzz/relations/mod.rs +++ b/crates/devtools/src/planner_fuzz/relations/mod.rs @@ -29,7 +29,7 @@ use super::generate::{ label_value, looser, tighter, OuterAggregation, OuterOp, PromqlSpec, QuerySpec, RangeFunction, SqlAggregate, }; -use super::{FuzzQuery, FuzzWorkload, Language, Planned, Planner, Priced}; +use super::{Costed, FuzzQuery, FuzzWorkload, Language, Planned, Planner}; /// A metamorphic relation the tester checks. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] @@ -270,7 +270,7 @@ pub enum Witness { GraftWithOperand { text: String, operand: String }, /// `W ∪ {q'}` planned with UnivMon as the only sketch family, so the /// planner sizes one UnivMon for both statistics (its summary-capability - /// rule), priced with every family. + /// rule), costed with every family. UnivMonOnly, } @@ -304,7 +304,7 @@ pub struct Measurement { /// The derived or added query's text, for a relation that adds one. pub related_query: Option, /// The witness (marginal relations) or the confirming plan (a violated - /// monotonicity relation: the other side's logical plan priced for this + /// monotonicity relation: the other side's logical plan costed for this /// side). pub witness: Option, /// The bound `related_cost_per_second` was checked against, tolerance @@ -321,7 +321,7 @@ pub struct WitnessReport { pub derivation: Option, pub cost_per_second: f64, pub plan: String, - /// Marginal relations: `W`'s selected logical plan priced the same way. + /// Marginal relations: `W`'s selected logical plan costed the same way. pub base_reference_cost_per_second: Option, /// `cost_per_second − base_reference_cost_per_second`. pub derive_cost_per_second: Option, @@ -341,7 +341,7 @@ pub enum Outcome { Holds(Measurement), Violated(Measurement), /// Not applicable to this workload, a side failed to plan, or no - /// witness could be built or priced. + /// witness could be built or costed. Inconclusive { reason: String, }, @@ -1481,7 +1481,7 @@ pub fn related_workloads( Ok((w.clone(), related, None)) } -/// The other side's logical plan priced for `workload` (its first +/// The other side's logical plan costed for `workload` (its first /// `keep` roots): a plan for `workload` the search could have selected. fn confirming( planner: &Planner, @@ -1489,11 +1489,11 @@ fn confirming( roots: Option<&Vec>, keep: usize, ) -> Option { - let priced = planner.price(workload, &roots?[..keep]).ok()?; + let costed = planner.cost(workload, &roots?[..keep]).ok()?; Some(WitnessReport { derivation: None, - cost_per_second: priced.cost_per_second, - plan: priced.selected, + cost_per_second: costed.cost_per_second, + plan: costed.selected, base_reference_cost_per_second: None, derive_cost_per_second: None, derived_node_cost_per_second: None, @@ -1588,12 +1588,12 @@ fn evaluate_marginal( return inconclusive("the search keeps no logical plan for W"); }; let base_reference = planner - .reference_price(base_workload, &base_roots) + .reference_cost(base_workload, &base_roots) .or_else(|e| inconclusive(format!("W's plan does not price: {e}")))?; - let mut best: Option<(Witness, Priced)> = None; + let mut best: Option<(Witness, Costed)> = None; let mut failures = Vec::new(); for witness in &derived.witnesses { - let priced = witness_roots( + let costed = witness_roots( planner, witness, base_workload, @@ -1602,33 +1602,33 @@ fn evaluate_marginal( &base_roots, check.target, ) - .and_then(|roots| planner.price(related, &roots)); - match priced { - Ok(priced) + .and_then(|roots| planner.cost(related, &roots)); + match costed { + Ok(costed) if best .as_ref() - .is_none_or(|(_, b)| priced.cost_per_second < b.cost_per_second) => + .is_none_or(|(_, b)| costed.cost_per_second < b.cost_per_second) => { - best = Some((witness.clone(), priced)) + best = Some((witness.clone(), costed)) } Ok(_) => {} Err(e) => failures.push(e), } } - let Some((witness, priced)) = best else { + let Some((witness, costed)) = best else { return inconclusive(format!("no witness: {}", failures.join("; "))); }; - let derive_cost = priced.cost_per_second - base_reference.cost_per_second; + let derive_cost = costed.cost_per_second - base_reference.cost_per_second; let delta = with.cost_per_second - base.cost_per_second; let tolerance = planner.settings.tolerance(with.cost_per_second); let violated = delta > derive_cost + tolerance - && with.cost_per_second > priced.cost_per_second + tolerance; + && with.cost_per_second > costed.cost_per_second + tolerance; let summary = format!( "cost(W ∪ {{q'}}) − cost(W) = {:.6} − {:.6} = {delta:.6}; derive_cost = {:.6} (witness {:.6} − W's plan {:.6}); tolerance {tolerance:.6}", with.cost_per_second, base.cost_per_second, derive_cost, - priced.cost_per_second, + costed.cost_per_second, base_reference.cost_per_second, ); let key_filter = matches!(witness, Witness::Filter { .. }); @@ -1640,15 +1640,15 @@ fn evaluate_marginal( related_query: Some(derived.query.text().to_string()), witness: Some(WitnessReport { derivation: Some(witness), - cost_per_second: priced.cost_per_second, - plan: priced.selected.clone(), + cost_per_second: costed.cost_per_second, + plan: costed.selected.clone(), base_reference_cost_per_second: Some(base_reference.cost_per_second), derive_cost_per_second: Some(derive_cost), - derived_node_cost_per_second: Some(priced.derived_root.cost_per_second), - derived_operators: priced.derived_root.operators.clone(), + derived_node_cost_per_second: Some(costed.derived_root.cost_per_second), + derived_operators: costed.derived_root.operators.clone(), key_filter_keeps_input_guarantee: key_filter, }), - bound_per_second: (base.cost_per_second + derive_cost).max(priced.cost_per_second) + bound_per_second: (base.cost_per_second + derive_cost).max(costed.cost_per_second) + tolerance, tolerance_per_second: tolerance, summary, diff --git a/crates/devtools/tests/asap_planner.rs b/crates/devtools/tests/asap_planner.rs index fc4060de4..91d698cf5 100644 --- a/crates/devtools/tests/asap_planner.rs +++ b/crates/devtools/tests/asap_planner.rs @@ -532,7 +532,6 @@ fn the_nine_query_workload_lowers_and_installs() { .unwrap(); plan.validate().unwrap(); assert_eq!(plan.query_texts.len(), queries); - let report = report.unwrap(); assert!(report .cost_per_second .is_some_and(|cost| cost.is_finite() && cost > 0.0)); diff --git a/crates/planner/src/installable/mod.rs b/crates/planner/src/installable/mod.rs index 3b59a927c..21e0e6070 100644 --- a/crates/planner/src/installable/mod.rs +++ b/crates/planner/src/installable/mod.rs @@ -93,7 +93,7 @@ pub struct PredictedUsage { pub disk_bytes: u64, /// CPU cores to provision for the peak. pub peak_cpu_cores: u32, - /// One per operator node of the plan Stage 3 priced, in node order. + /// One per operator node of the plan Stage 3 costed, in node order. pub per_node: Vec, /// One per workload query, in workload order. pub per_query: Vec, @@ -111,7 +111,7 @@ pub struct PredictedNodeUsage { pub timing: ExecutionTiming, /// Core-seconds of CPU per second. pub cpu_core_seconds_per_second: f64, - /// Its price per second under cost model 1; `None` under cost models + /// Its cost per second under cost model 1; `None` under cost models /// 2 and 3. pub cost_per_second: Option, /// Whether its CPU came from the measured cost table or the formulas. @@ -201,7 +201,7 @@ impl From for InstallablePlanError { /// [`InstallablePlanError::QueryRunNotExecutable`]. pub async fn plan_installable( input: UserInput<'_>, -) -> Result<(InstallablePlan, Option), PlanError> { +) -> Result<(InstallablePlan, SelectedPlanReport), PlanError> { let capabilities = executor_summaries_when_unrestricted(input.models.deployment_capabilities); let entries: Vec<_> = input.workload.query_workload.entries().collect(); let models = input.models.with_deployment_capabilities(&capabilities); @@ -222,23 +222,21 @@ pub async fn plan_installable( }) }, )?; - installable_plan_of(&output.plan, Some(&cost), &entries).map_err(PlanError::Installable) + let installable = installable_plan_of(output.plan, &entries).map_err(PlanError::Installable)?; + Ok((installable, report_of(&cost))) } /// The installable plan of `plan`, the plan Stage 3 built for a workload -/// whose entries are `entries` (in `QueryWorkload::entries()` order), with -/// the report of its `cost` (`asap_cost::plan_cost` of `plan`) when there is -/// one: what [`plan_installable`] returns after planning. A caller that runs -/// the planning stages itself (e.g. a developer tool) uses it to see what a -/// runtime would install. +/// whose entries are `entries` (in `QueryWorkload::entries()` order): what +/// [`plan_installable`] returns after planning, without its cost report. A +/// caller that runs the planning stages itself (e.g. a developer tool) uses +/// it to see what a runtime would install. pub fn installable_plan_of( - plan: &PhysicalASAPDAG, - cost: Option<&PlanCost>, + plan: PhysicalASAPDAG, entries: &[QueryWorkloadEntry], -) -> Result<(InstallablePlan, Option), InstallablePlanError> { +) -> Result { let query_texts: Vec = entries.iter().map(|entry| entry.query.0.clone()).collect(); - let installable = InstallablePlan::of_dag(plan.clone(), query_texts)?; - Ok((installable, cost.map(report_of))) + InstallablePlan::of_dag(plan, query_texts) } impl InstallablePlan { diff --git a/crates/planner/src/installable/tests.rs b/crates/planner/src/installable/tests.rs index 78612506e..c457df1ff 100644 --- a/crates/planner/src/installable/tests.rs +++ b/crates/planner/src/installable/tests.rs @@ -160,9 +160,8 @@ fn producer_of(dag: &PhysicalASAPDAG, consumer: PhysicalASAPNodeId) -> PhysicalA #[test] fn a_chain_is_installed_as_one_view() { let (dag, chain) = chain_plan(Some(3_600_000)); - let (plan, report) = installable_plan_of(&dag, None, &[entry()]).unwrap(); + let plan = installable_plan_of(dag.clone(), &[entry()]).unwrap(); assert_eq!(plan.dag, dag); - assert!(report.is_none()); let views = plan.materialized_views().unwrap(); assert_eq!(views.len(), 1); assert_eq!(views[0].node_id, chain); @@ -186,13 +185,13 @@ fn a_chain_is_installed_as_one_view() { fn installation_checks_the_retention_stage3_set() { let entries = [entry()]; assert!(matches!( - installable_plan_of(&chain_plan(None).0, None, &entries), + installable_plan_of(chain_plan(None).0, &entries), Err(InstallablePlanError::Uninstallable( InstallableDagError::ViewWithoutRetention { .. } )) )); assert!(matches!( - installable_plan_of(&chain_plan(Some(3_000_000)).0, None, &entries), + installable_plan_of(chain_plan(Some(3_000_000)).0, &entries), Err(InstallablePlanError::Uninstallable( InstallableDagError::ViewRetentionTooShort { .. } )) diff --git a/crates/planner/tests/runtime_install.rs b/crates/planner/tests/runtime_install.rs index 38806d5e0..f2e0040cd 100644 --- a/crates/planner/tests/runtime_install.rs +++ b/crates/planner/tests/runtime_install.rs @@ -154,7 +154,7 @@ fn answers(batches: &[Batch]) -> Vec { /// The plan for `queries` on a deployment that keeps no raw samples, and /// Stage 3's report of the selection it made (which the plan file does not /// carry). -async fn planned_with_report(queries: &[&str]) -> (InstallablePlan, Option) { +async fn planned_with_report(queries: &[&str]) -> (InstallablePlan, SelectedPlanReport) { let capabilities = DeploymentCapabilities { raw_data_retained: false, ..DeploymentCapabilities::default() @@ -167,7 +167,7 @@ async fn planned_with_report(queries: &[&str]) -> (InstallablePlan, Option (InstallablePlan, Option) { +) -> (InstallablePlan, SelectedPlanReport) { // The deployment's Stage 3 prices, from its JSON file: cost model 1. let settings: CostInputs = serde_json::from_str( r#"{"cost_model": {"usage_over_time": { @@ -208,7 +208,6 @@ async fn the_plan_carries_stage3s_predicted_usage() { plan.raw_data_retention().unwrap().is_empty(), "a tumbling-window-only plan" ); - let selection = selection.as_ref().expect("Stage 3's report"); let usage = &selection.predicted_usage; let node_costs: f64 = usage .per_node @@ -323,7 +322,7 @@ async fn ignoring_accuracy_guarantees_plans_the_undeclared_p99_query() { ..DeploymentCapabilities::default() }; let settings = CostInputs::default(); - let (plan, report) = plan_installable(UserInput::new( + let (plan, _report) = plan_installable(UserInput::new( &p99_avg_workload(false), FrontendInput::Promql, PlanningModels::builtin() @@ -333,7 +332,6 @@ async fn ignoring_accuracy_guarantees_plans_the_undeclared_p99_query() { )) .await .expect("a plan is selected"); - report.as_ref().expect("the plan's cost report"); assert!( plan.raw_data_retention().unwrap().is_empty(), "no raw samples read" From 08691da67699a1a4b40c264185db9527c150df3e Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:24:36 +0000 Subject: [PATCH 14/31] docs: Stage 3's files and the shared cost rules in the architecture doc; cite the concepts doc by its path; no issue numbers in Cargo.toml (#1207 PR 6) - `asap-aware-mapping-architecture.md`: Stage 3 no longer checks accuracy or latency; it costs candidates and edges with asap-cost, selects with the baseline MILP and builds the plan; the Stage 3 row names its files (`selectable.rs`, `cost.rs`, `retention.rs`, `milp/`, `plan.rs`, `report.rs`), `asap-cost`'s `plan/read_back.rs` and `asap-types`' `plan/time_window.rs`. - `extend-asap-aware-mapping.md`: "Price it" -> "Cost it". - Module docs cite `docs/design_docs/concepts/memo-groups-properties-requirements.md` instead of "concepts doc"; the Stage 3 Cargo.toml comment drops its issue numbers and says "costs". - Glossary: `RawDataWindow` is deleted; its example is `RawDataSample`. --- crates/cost/src/node/usage/mod.rs | 3 ++- crates/stage3-plan-selection/Cargo.toml | 7 +++---- crates/stage3-plan-selection/src/cost.rs | 4 +++- crates/stage3-plan-selection/src/milp/model/mod.rs | 3 ++- docs/design_docs/concepts/glossary.md | 2 +- docs/develop_docs/asap-aware-mapping-architecture.md | 4 ++-- docs/develop_docs/extend-asap-aware-mapping.md | 2 +- 7 files changed, 14 insertions(+), 11 deletions(-) diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index 38037d4b6..c2fcded60 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -934,7 +934,8 @@ pub fn node_resource_usage_and_cost<'a>( raw_scan = Some((source, retained)); } } - // Query-time memory is held only while the node computes (concepts doc, + // Query-time memory is held only while the node computes + // (`docs/design_docs/concepts/memo-groups-properties-requirements.md`, // section 5, "Memory held while computing"): its bytes for its work per // execution, at its executions per second. Query latency does not // enter it. diff --git a/crates/stage3-plan-selection/Cargo.toml b/crates/stage3-plan-selection/Cargo.toml index 948673148..dfeb3d041 100644 --- a/crates/stage3-plan-selection/Cargo.toml +++ b/crates/stage3-plan-selection/Cargo.toml @@ -3,11 +3,10 @@ name = "asap-stage3-plan-selection" version = "0.1.0" edition = "2021" -# #509 Stage 3, plan selection: the only stage that prices plans, with the -# planning models (accuracy from `asap-accuracy`; cost from `asap-cost`) +# Stage 3, plan selection: the only stage that costs plans, with the +# planning models (accuracy from `asap-accuracy`; cost from `asap-cost`). # Reads only Stage 2's output (`asap_types::stages`) and builds the plan -# itself: no dependency on Stages 1 or 2, nor on the executor (#1054 X1, -# #1047 P7). +# itself: no dependency on Stages 1 or 2, nor on the executor. [dependencies] asap-types = { path = "../types" } asap-accuracy = { path = "../accuracy" } diff --git a/crates/stage3-plan-selection/src/cost.rs b/crates/stage3-plan-selection/src/cost.rs index d9249a5b9..4f096637b 100644 --- a/crates/stage3-plan-selection/src/cost.rs +++ b/crates/stage3-plan-selection/src/cost.rs @@ -1,4 +1,6 @@ -//! The baseline objective's coefficients (concepts doc, section 3, "Stage +//! The baseline objective's coefficients +//! (`docs/design_docs/concepts/memo-groups-properties-requirements.md`, +//! section 3, "Stage //! 3: baseline selection and plan output"): $w_v$ of every physical //! candidate and $c_{u,c,i}$ of every edge a plan may select, from //! `asap-cost` under cost model 1. diff --git a/crates/stage3-plan-selection/src/milp/model/mod.rs b/crates/stage3-plan-selection/src/milp/model/mod.rs index 546ebf52a..4da7f622a 100644 --- a/crates/stage3-plan-selection/src/milp/model/mod.rs +++ b/crates/stage3-plan-selection/src/milp/model/mod.rs @@ -1,4 +1,5 @@ -//! Stage 3's selection MILP, named after the formulation (concepts doc, +//! Stage 3's selection MILP, named after the formulation +//! (`docs/design_docs/concepts/memo-groups-properties-requirements.md`, //! sections 3–5). //! //! | Family | Variables | Constraints | diff --git a/docs/design_docs/concepts/glossary.md b/docs/design_docs/concepts/glossary.md index 0e8ccfc15..784667f82 100644 --- a/docs/design_docs/concepts/glossary.md +++ b/docs/design_docs/concepts/glossary.md @@ -42,7 +42,7 @@ A node of the physical DAG whose output is materialized across evaluations (life ## Raw data sample -One timestamped value of a time series as it arrives, such as one Prometheus Remote Write sample. Raw data samples are the rows a time-series scan reads. Code names them with `raw_data_sample`, e.g. `RawDataWindow`. +One timestamped value of a time series as it arrives, such as one Prometheus Remote Write sample. Raw data samples are the rows a time-series scan reads. Code names them with `raw_data_sample`, e.g. `RawDataSample`. ## Summary sample diff --git a/docs/develop_docs/asap-aware-mapping-architecture.md b/docs/develop_docs/asap-aware-mapping-architecture.md index 95e2f4777..eb5cea838 100644 --- a/docs/develop_docs/asap-aware-mapping-architecture.md +++ b/docs/develop_docs/asap-aware-mapping-architecture.md @@ -25,7 +25,7 @@ in `asap-integration-tests` (`crate_graph`) checks these edges with | `asap-accuracy` (`crates/accuracy`) | The accuracy model the stages share: the analytical rules (per-algorithm estimators, each operator's error contract, the target comparison `within_limit`), the deployment's measured accuracy (`AccuracyEvidence`) and α^log (`ErrorMetricsOfMemo::edge_meets_logical_requirements`); depends only on `asap-types` | | `asap-stage2-physical-optimization` (`crates/stage2-physical-optimization`) | Stage 2: each logical candidate's timing and lifetime, the physical edges and the pruned alternatives; depends only on `asap-types` | | `asap-cost` (`crates/cost`) | The cost model the stages share: resource-usage estimation (the analytical per-operator model, measured operation costs, state byte sizes) and the cost models (`ResourceUsage`, `CostModel`, `CostInputs`); depends only on `asap-types` | -| `asap-stage3-plan-selection` (`crates/stage3-plan-selection`) | Stage 3: `select_plan` over Stage 2's output; accuracy and latency checks, pricing candidates with `asap-cost` | +| `asap-stage3-plan-selection` (`crates/stage3-plan-selection`) | Stage 3: `select_plan` over Stage 2's output; costs each candidate and selectable edge with `asap-cost`, selects with the baseline MILP and builds the plan (`PhysicalASAPDAG`) | | `asap-planner` (`crates/planner`) | The facade: `e2e_plan` runs Stage 0, then the optimization pass (`Stage1ToStage3Pass` by default), which orchestrates Stages 1–3 (`run_planning`); the planning models (`PlanningModels`) and the `OptimizationPass` trait | | `asap-executor` (`crates/executor`) | Stage 4, the reference executor: operators and summary kernels over `asap_sketchlib`; it runs a `PhysicalASAPDAG` | | `asap-devtools` | `asap_planner` and the other inspection tools, the design-doc example workloads (`planner_layering_examples`) and the candidate labels | @@ -78,7 +78,7 @@ same pipeline behind the `OptimizationPass` trait. | What each summary type can merge and answer (`SummaryTypeCapability::of`: `merge_inputs`, `readouts`) | `asap-types`, `summary_type/capability.rs` | | Stage 2: the timings and lifetimes offered per logical candidate and their prune rules, the per-group offers (ingestion time, across evaluations), the physical edges, and the alternatives no plan can use | `asap-stage2-physical-optimization`: `candidate.rs`, `group.rs`, `edge.rs`, `pruning.rs`; the admissible-edge indexes in `asap-types`, `stages/physical/memo_candidates/mod.rs` | | Execution timing and data-state edge rules; each physical candidate's `PhysicalProperties` and `Lifetime`, and the physical edge check (`producer_satisfies_physical_requirements`) | `asap-types`, `ir/properties/execution_data_state/mod.rs`; `stages/physical/properties/mod.rs` (`PhysicalProperties`, `Lifetime`) and `stages/physical/candidate.rs` (`PhysicalCandidate`, the edge check) | -| Stage 3: accuracy check, latency check, resource usage (`ResourceUsage`), cost models 1–3 (`CostModel`), measured operation costs | `asap-stage3-plan-selection`, `lib.rs`; `asap-cost`, `model.rs` (`CostModel`, `CostInputs`), `resources/mod.rs` (`ResourceUsage`) and `measured.rs` | +| Stage 3: the selectable edges (`selectable.rs`), each candidate's cost $w_v$ and each edge's $c_{u,c,i}$ (`cost.rs`), retention (`retention.rs`), the baseline MILP (`milp/`), the plan it builds (`plan.rs`) and its report (`report.rs`); resource usage (`ResourceUsage`), cost models 1–3 (`CostModel`), measured operation costs, how far back time-indexed nodes are read | `asap-stage3-plan-selection`; `asap-cost`, `model.rs` (`CostModel`, `CostInputs`), `resources/mod.rs` (`ResourceUsage`), `measured.rs` and `plan/read_back.rs`; the time-window walks in `asap-types`, `plan/time_window.rs` | | The analytical per-operator resource model Stage 3 measures with | `asap-cost`, `node/analytical.rs` and `node/operator_statistics.rs` | | The accuracy rules: per-family guarantees, each operator's error contract, the metric rule, each readout's required accuracy, α^log of a logical edge | `asap-accuracy` (`AccuracyEvidence`, `theoretical_guarantee`, `guarantee/`, `evidence.rs`, `error_contract/`, `metric_rule/`, `readout_caps/`, `logical_edge/`) | | The statistic a summary is read for an aggregation intent (`SketchStatistic::for_intent`) | `asap-types`, `summary_type/statistic.rs` | diff --git a/docs/develop_docs/extend-asap-aware-mapping.md b/docs/develop_docs/extend-asap-aware-mapping.md index 1c626d653..af00cceaa 100644 --- a/docs/develop_docs/extend-asap-aware-mapping.md +++ b/docs/develop_docs/extend-asap-aware-mapping.md @@ -85,7 +85,7 @@ contains the candidate and that Stage 3 prices it. (`LIFETIMES`, `offered_physical_properties`; a per-group offer in `group.rs`), and gate it on the deployment input that allows it with a `PruneReason`. -3. Price it in `crates/cost/src/node/usage/mod.rs` (`asap_cost::node_resource_usage_and_cost`): what it keeps in memory or on +3. Cost it in `crates/cost/src/node/usage/mod.rs` (`asap_cost::node_resource_usage_and_cost`): what it keeps in memory or on disk, and what each evaluation rebuilds. Tests: Stage 2 unit tests for when the option is offered; From 33c9bc65ab2e3905b1c8989c68adaa430746b32e Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:25:10 +0000 Subject: [PATCH 15/31] docs(cost): a doc comment on every field of ProducerOutput and NodeResourceUsageAndCost (#1207 PR 6) --- crates/cost/src/node/usage/mod.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index c2fcded60..ffb8ff857 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -107,10 +107,15 @@ fn state_family<'a>( /// family. #[derive(Debug, Clone)] pub struct ProducerOutput<'a> { + /// Its payload. pub payload: &'a Payload, + /// Its output schema. pub output_schema: &'a Schema, + /// Its output rows and bytes. pub output: EdgeStatistics, + /// The sources it reads. pub sources: BTreeSet<&'a str>, + /// Its state family (with θ). pub family: StateFamily<'a>, /// Its output columns that still hold one source row's label (or /// column) values unchanged ([`unchanged_source_columns`]). @@ -128,8 +133,11 @@ pub struct ProducerOutput<'a> { /// on disk); its calibration fit and its [`NodeCost`]. #[derive(Debug, Clone)] pub struct NodeResourceUsageAndCost<'a> { + /// Its output rows and bytes. pub output: EdgeStatistics, + /// The sources it reads. pub sources: BTreeSet<&'a str>, + /// Its state family. pub family: StateFamily<'a>, /// Its output columns that still hold one source row's label (or /// column) values unchanged ([`unchanged_source_columns`]). @@ -137,17 +145,26 @@ pub struct NodeResourceUsageAndCost<'a> { /// The tumbling windows its rows span: those a `TimeRange` selects /// from a chain, 1 for any other node. pub tumbling_windows: u64, + /// Work of one execution, in ms on one core. pub work_ms_per_execution: f64, + /// CPU operations per second. pub cpu_ops_per_second: f64, + /// Bytes scanned per second. pub scanned_bytes_per_second: f64, + /// Average memory, including memory a query-time node holds while it + /// computes. pub average_memory_bytes: f64, + /// Peak memory. pub peak_memory_bytes: u64, + /// Disk bytes. pub disk_bytes: u64, /// An in-memory ingestion-time state's bytes per window. pub maintained: Option, /// A query-time scan's source and the raw bytes it reads. pub raw_scan: Option<(&'a Source, u64)>, + /// Its calibration fit. pub fit: CalibrationFit, + /// Its cost. pub cost: NodeCost, } From cc180aa02d5095776bbc488285c77ccdaa06d2f7 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:32:44 +0000 Subject: [PATCH 16/31] docs(devtools,planner): the planner fuzz and the Usage test say "cost", not "price" (#1207 PR 6) --- crates/devtools/src/planner_fuzz/mod.rs | 16 ++++++++-------- .../devtools/src/planner_fuzz/relations/mod.rs | 6 +++--- crates/planner/tests/runtime_install.rs | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/crates/devtools/src/planner_fuzz/mod.rs b/crates/devtools/src/planner_fuzz/mod.rs index 8533cbd88..7ec726e99 100644 --- a/crates/devtools/src/planner_fuzz/mod.rs +++ b/crates/devtools/src/planner_fuzz/mod.rs @@ -7,8 +7,8 @@ //! from `q`'s result or state (the same query with a looser accuracy //! target, a filter on a group key, a coarser grouping, ...), it builds a //! *witness*: `W`'s selected logical plan plus `q'` derived from `q`'s -//! node by one operator. Stage 2 and Stage 3 implement and price the -//! witness exactly as they price any candidate, and the relation +//! node by one operator. Stage 2 and Stage 3 implement and cost the +//! witness exactly as they cost any candidate, and the relation //! //! ```text //! cost(W ∪ {q'}) − cost(W) ≤ derive_cost + tolerance, @@ -16,8 +16,8 @@ //! ``` //! //! must hold. `derive_cost` is what Stage 3 charges for the derived -//! operator alone (its per-node price, plus any change it causes in the -//! shared nodes' prices, such as a shared node now materialized); it is +//! operator alone (its per-node cost, plus any change it causes in the +//! shared nodes' costs, such as a shared node now materialized); it is //! never a guess. A violation means a cheaper plan for `W ∪ {q'}` exists //! that the planner did not select. Monotonicity and invariance relations //! (tightening accuracy, adding a query or dropping raw retention never @@ -343,7 +343,7 @@ impl FuzzSettings { pub struct Planned { /// The selected physical plan's stable archive ID. pub selected: String, - /// Its price per second under cost model 1. + /// Its cost per second under cost model 1. pub cost_per_second: f64, /// The frontends' roots, one per query. pub stage0_roots: Vec, @@ -372,8 +372,8 @@ pub struct PrivateNodes { pub operators: Vec, } -/// Plans and prices workloads, caching each workload's plan and the -/// reference price of its selected logical plan. +/// Plans and costs workloads, caching each workload's plan and the +/// reference cost of its selected logical plan. pub struct Planner { pub settings: FuzzSettings, cache: HashMap, String>>, @@ -511,7 +511,7 @@ impl Planner { } /// [`cost`](Self::cost) of `workload`'s selected logical plan - /// `roots`, cached per workload: every marginal check of a case prices + /// `roots`, cached per workload: every marginal check of a case costs /// the same `W`. pub fn reference_cost( &mut self, diff --git a/crates/devtools/src/planner_fuzz/relations/mod.rs b/crates/devtools/src/planner_fuzz/relations/mod.rs index cdc0b0bfe..127516b81 100644 --- a/crates/devtools/src/planner_fuzz/relations/mod.rs +++ b/crates/devtools/src/planner_fuzz/relations/mod.rs @@ -1569,11 +1569,11 @@ fn evaluate_inner(planner: &mut Planner, check: &Check) -> Result derive_cost + tolerance`) and its plain reading (the /// witness is cheaper than the planner's `W ∪ {q'}` by more than the -/// tolerance) fail, so a search that prices `W` below Stage 3's price of +/// tolerance) fail, so a search that costs `W` below Stage 3's cost of /// the same plan cannot make a false violation. fn evaluate_marginal( planner: &mut Planner, @@ -1589,7 +1589,7 @@ fn evaluate_marginal( }; let base_reference = planner .reference_cost(base_workload, &base_roots) - .or_else(|e| inconclusive(format!("W's plan does not price: {e}")))?; + .or_else(|e| inconclusive(format!("W's plan does not cost: {e}")))?; let mut best: Option<(Witness, Costed)> = None; let mut failures = Vec::new(); for witness in &derived.witnesses { diff --git a/crates/planner/tests/runtime_install.rs b/crates/planner/tests/runtime_install.rs index f2e0040cd..ef570d680 100644 --- a/crates/planner/tests/runtime_install.rs +++ b/crates/planner/tests/runtime_install.rs @@ -168,7 +168,7 @@ async fn plan_with_report_for( workload: PlanningWorkload, capabilities: &DeploymentCapabilities, ) -> (InstallablePlan, SelectedPlanReport) { - // The deployment's Stage 3 prices, from its JSON file: cost model 1. + // The deployment's unit costs, from its JSON file: cost model 1. let settings: CostInputs = serde_json::from_str( r#"{"cost_model": {"usage_over_time": { "cost_per_cpu_op": 1e-6, "cost_per_scanned_byte": 1e-7, @@ -195,7 +195,7 @@ fn query_roots(plan: &InstallablePlan) -> Vec { /// The plan file carries Stage 3's predicted usage of the selected plan, so /// a runtime reports it against what it measures without planning again: -/// for Example 4b's tumbling windows maintained at ingestion time, its nodes' prices +/// for Example 4b's tumbling windows maintained at ingestion time, its nodes' costs /// add up to Stage 3's selection cost and their CPU to the plan's, with /// one latency per query, each node naming the installed nodes that run it. #[tokio::test] From 9ec3847fc9dd45dfcb53837c1f591ab0b320ff9b Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Fri, 13 Nov 2026 17:22:12 +0000 Subject: [PATCH 17/31] fix(cost): an ingestion-time view keeps the outputs its retention covers, not one run's (#1207 PR 6) A materialized ingestion-time node that is neither a chain nor a Scan was charged for one run's output and the one being built. A TimeShift over the arriving rows keeps far more: `sum_over_time(m[5m] offset 90s)` without raw data selected such a view at 0.01225/s, charged for one second of rows, and the installer refused it. It now keeps one output per refresh that its retention_ms covers (per window it maintains, per second of rows without one), a TimeShift's offset more, plus the one being built; plan_cost and Stage 3 share the rule, and the installable chain plan (0.10425/s) is selected. Likewise `sum_over_time(m[1m] offset 1h)` hourly with a 10 s `sum_over_time(m[1m])` now selects the chain plan (0.4925/s). --- crates/cost/src/node/usage/mod.rs | 44 ++++++++++-- crates/cost/src/node/usage/tests.rs | 55 +++++++++++++++ .../tests/stage3_objective.rs | 70 ++++++++++++++++++- 3 files changed, 163 insertions(+), 6 deletions(-) diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index ffb8ff857..3a2f50aa7 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -235,8 +235,10 @@ pub fn maintained_window_ms<'o, N: Copy + Ord, C: 'o>( /// most offset + range its consumers read ([`read_back_ms`] without a span; /// `None`: it reads what it retains). A materialized node keeps what its /// `retention_ms` ($h_v$) covers: a tumbling-window chain its windows in -/// it, a query-time `Scan` its raw rows in it. The formulas' CPU is scaled -/// by `settings.frozen_calibration_factor` (1 when unset). +/// it, a query-time `Scan` its raw rows in it, any other view materialized +/// at ingestion time one output per refresh in it (one second of rows +/// when it maintains no window; a `TimeShift`'s offset more). The formulas' CPU is scaled by +/// `settings.frozen_calibration_factor` (1 when unset). /// /// [`executions_per_second`]: crate::plan::execution_demand::executions_per_second pub fn node_resource_usage_and_cost<'a>( @@ -923,8 +925,42 @@ pub fn node_resource_usage_and_cost<'a>( (true, Lifetime::AcrossEvaluations(medium), Payload::NonASAP(NonASAPOp::Scan { .. })) => { (edge(raw_retained_rows.unwrap_or(out.rows))?.bytes, medium) } - // A whole state materialized across evaluations: it and the one being built. - (_, Lifetime::AcrossEvaluations(medium), _) => ( + // Any other view materialized at ingestion time: the outputs its + // `retention_ms` covers, one per refresh (per window it maintains; + // per second of arriving rows without one), and the one being + // built. A `TimeShift` by an offset holds each output that much + // longer before its readers read it: over the arriving rows, the + // rows of its retention plus its offset, not one second of them. + (true, Lifetime::AcrossEvaluations(medium), _) => { + let refresh_interval_ms = window_ms.unwrap_or(1_000).max(1); + let shift_offset_ms = match &node.payload { + Payload::NonASAP(NonASAPOp::TimeShift { shift, .. }) if shift.at.is_none() => { + u64::try_from(shift.offset_ms).unwrap_or(0) + } + _ => 0, + }; + let retained_output_count = node + .retention_ms + .map_or(1, |retention_ms| { + retention_ms + .saturating_add(shift_offset_ms) + .div_ceil(refresh_interval_ms) + }) + .max(1); + ( + state + .checked_mul( + retained_output_count + .checked_add(1) + .ok_or(AnalyticalCostError::Overflow)?, + ) + .ok_or(AnalyticalCostError::Overflow)?, + medium, + ) + } + // A whole state materialized across evaluations at query time: it + // and the one being built. + (false, Lifetime::AcrossEvaluations(medium), _) => ( state.checked_mul(2).ok_or(AnalyticalCostError::Overflow)?, medium, ), diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index 5942ec575..c7a9eccd3 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -196,3 +196,58 @@ fn a_query_time_scan_reads_what_its_consumers_read_and_keeps_its_retention() { let (_, kept_bytes) = usage.raw_scan.expect("a query-time scan keeps raw data"); assert_eq!(kept_bytes, usage.output.bytes * 3_601); } + +/// An ingestion-time view of raw rows with no window below it (a +/// `TimeShift` by 90 s over the arriving rows) keeps what its +/// `retention_ms` and its offset cover: one second of rows per second, and +/// the second being built. Over 1,000 rows per second of 64 bytes, a 300 s +/// retention keeps 64,000 × 391 bytes, not one second's 64,000 × 2; a +/// 600 s one keeps 64,000 × 691. +#[test] +fn an_ingestion_time_view_of_raw_rows_keeps_the_rows_its_retention_covers() { + let settings = CostInputs::default(); + let data = DataWorkload::default(); + let scan = node(Payload::NonASAP(NonASAPOp::Scan { + source: Source::TimeSeries { metric: "m".into() }, + predicates: vec![], + schema: Schema::default(), + })); + let one_second_of_rows = ProducerOutput { + payload: &scan.payload, + output_schema: &scan.output_schema, + output: EdgeStatistics { + rows: 1_000, + bytes: 64_000, + }, + sources: BTreeSet::new(), + family: StateFamily::None, + unchanged_columns: BTreeSet::new(), + tumbling_windows: 1, + }; + let kept_bytes = |retention_ms: u64| { + let shift = PhysicalASAPDAGNode { + output_state: ExecutionDataState::INGESTION_ROWS, + lifetime: Lifetime::AcrossEvaluations(StorageMedium::Memory), + retention_ms: Some(retention_ms), + ..node(Payload::NonASAP(NonASAPOp::TimeShift { + shift: asap_types::ir::operator::arguments::TimeShift { + offset_ms: 90_000, + at: None, + }, + child: 0, + })) + }; + node_resource_usage_and_cost( + &shift, + std::slice::from_ref(&one_second_of_rows), + (1, 1.0), + None, + &data, + &settings, + ) + .unwrap() + .peak_memory_bytes + }; + assert_eq!(kept_bytes(300_000), 64_000 * 391); + assert_eq!(kept_bytes(600_000), 64_000 * 691); +} diff --git a/crates/integration-tests/tests/stage3_objective.rs b/crates/integration-tests/tests/stage3_objective.rs index c7f19fcc4..f2909c7cb 100644 --- a/crates/integration-tests/tests/stage3_objective.rs +++ b/crates/integration-tests/tests/stage3_objective.rs @@ -6,6 +6,10 @@ use asap_cost::{CostInputs, CostModel, ResourceUnitCost}; use asap_devtools::planning_stages::{lower_promql_roots, run_planning_stages_over}; use asap_planner::{DeploymentCapabilities, PlanningModels}; +use asap_types::ir::properties::ExecutionTiming; +use asap_types::ir::NonASAPOp; +use asap_types::plan::physical_asap_dag::{PhysicalASAPDAG, PhysicalASAPOperatorPayload}; +use asap_types::stages::Lifetime; use asap_types::workload::PlanningWorkload; /// PromQL `queries`, each `(query, interval_ms, accuracy JSON)`, over the @@ -36,6 +40,16 @@ fn objective_and_plan_cost( settings: &CostInputs, raw_data_retained: bool, ) -> (f64, f64) { + selected_plan_objective_and_plan_cost(workload, settings, raw_data_retained).1 +} + +/// The plan Stage 3 selects for `workload` under `settings`, with raw data +/// kept or not, with the MILP's optimum and its `plan_cost`. +fn selected_plan_objective_and_plan_cost( + workload: &PlanningWorkload, + settings: &CostInputs, + raw_data_retained: bool, +) -> (PhysicalASAPDAG, (f64, f64)) { let capabilities = DeploymentCapabilities { raw_data_retained, ..Default::default() @@ -50,8 +64,11 @@ fn objective_and_plan_cost( let (plan, selection) = run.run.selection.as_ref().expect("a plan is selected"); let (cost, _) = asap_cost::plan_cost(plan, &entries, &data, settings).unwrap(); ( - selection.selected_plan_cost_per_second, - cost.cost_per_second.expect("cost model 1"), + plan.clone(), + ( + selection.selected_plan_cost_per_second, + cost.cost_per_second.expect("cost model 1"), + ), ) } @@ -114,3 +131,52 @@ fn the_objective_includes_request_overhead_and_charges_raw_data_on_disk_once_per assert_equal(name, objective_and_plan_cost(w, &settings, true)); } } + +/// Offset windows, no raw data kept: `sum_over_time(m[5m] offset 90s)` +/// every minute, and `sum_over_time(m[1m] offset 1h)` every hour with +/// `sum_over_time(m[1m])` every 10 s. An ingestion-time `TimeShift` view +/// over the arriving rows keeps the rows its retention and its offset +/// cover (390 s and 61 min of rows), not one second of them, so it is +/// dearer than the chain plans and is not selected; the selected plan +/// installs, and the objective is its `plan_cost`. +#[test] +fn offset_windows_without_raw_data_select_installable_plans_costed_for_their_retention() { + let workloads = [ + ( + "offset 90s", + workload(&[("sum_over_time(m[5m] offset 90s)", 60_000, "")]), + ), + ( + "offset 1h every hour", + workload(&[ + ("sum_over_time(m[1m] offset 1h)", 3_600_000, ""), + ("sum_over_time(m[1m])", 10_000, ""), + ]), + ), + ]; + for (name, w) in &workloads { + let (plan, costs) = selected_plan_objective_and_plan_cost(w, &CostInputs::default(), false); + assert_equal(name, costs); + let ingestion_time_shift_views: Vec<_> = plan + .nodes + .iter() + .filter(|node| { + node.output_state.timing == ExecutionTiming::IngestionTime + && matches!(node.lifetime, Lifetime::AcrossEvaluations(_)) + && matches!( + node.payload, + PhysicalASAPOperatorPayload::NonASAP(NonASAPOp::TimeShift { .. }) + ) + }) + .map(|node| node.id) + .collect(); + assert!( + ingestion_time_shift_views.is_empty(), + "{name}: an ingestion-time TimeShift view is selected: {ingestion_time_shift_views:?}" + ); + let entries: Vec<_> = w.query_workload.entries().collect(); + asap_planner::installable_plan_of(plan, &entries).unwrap_or_else(|error| { + panic!("{name}: the selected plan does not install: {error:?}") + }); + } +} From eba55f7c91fa2ba79bc72ac68bc2a4c2bf6615fe Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Fri, 13 Nov 2026 19:16:19 +0000 Subject: [PATCH 18/31] perf(stage3): leave out MILP rows implied by the one-consumer equality (#1207 PR 6) A query-time result that is not materialized has the (F) equality sum y + sum z = x_v, which with y, z >= 0 already implies its (B) rows y <= x_u and its (D) rows z <= x_v. Those rows are no longer built: c09 (one topk(rate) query) goes from 7,823 rows to 4,906. The raw-data rows now carry the objective scale (r_s >= scale * d_v x_v) and r_s has objective coefficient 1, so the objective's coefficients stay in a narrow range instead of reaching scale (up to 1.7e5) on r_s. --- .../src/milp/model/mod.rs | 72 +++++++++++-------- 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/crates/stage3-plan-selection/src/milp/model/mod.rs b/crates/stage3-plan-selection/src/milp/model/mod.rs index 4da7f622a..f4c3f73a5 100644 --- a/crates/stage3-plan-selection/src/milp/model/mod.rs +++ b/crates/stage3-plan-selection/src/milp/model/mod.rs @@ -147,7 +147,8 @@ fn input_satisfaction_constraints( constraints } -/// (B) $y_{u,c,i} \le x_u$. +/// (B) $y_{u,c,i} \le x_u$, except for a producer with at most one +/// consumer ([`has_at_most_one_consumer`]), whose (F) row implies it. fn producer_selection_constraints( inputs: &SelectionInputs<'_>, variables: &SelectionVariables, @@ -155,14 +156,17 @@ fn producer_selection_constraints( variables .y .iter() - .map(|(&edge, &y)| { + .filter_map(|(&edge, &y)| { let producer = inputs.physical.edge(edge).producer_candidate_id; - constraint!(y <= variables.x[producer.0]) + (!has_at_most_one_consumer(inputs, producer.0)) + .then(|| constraint!(y <= variables.x[producer.0])) }) .collect() } -/// (C) $\sum_{v \in G_q} z_{q,v} = 1$; (D) $z_{q,v} \le x_v$. +/// (C) $\sum_{v \in G_q} z_{q,v} = 1$; (D) $z_{q,v} \le x_v$, except for +/// a candidate with at most one consumer ([`has_at_most_one_consumer`]), +/// whose (F) row implies it. fn query_coverage_constraints( inputs: &SelectionInputs<'_>, variables: &SelectionVariables, @@ -173,11 +177,24 @@ fn query_coverage_constraints( constraints.push(constraint!(covered == 1)); } for (&(_, v), &z) in &variables.z { - constraints.push(constraint!(z <= variables.x[v.0])); + if !has_at_most_one_consumer(inputs, v.0) { + constraints.push(constraint!(z <= variables.x[v.0])); + } } constraints } +/// Whether candidate `v` is a query-time result that is not materialized +/// ($\ell$ = None): it has at most one consumer, and its (F) row is the +/// equality $\sum_{(v,c,i)} y_{v,c,i} + \sum_q z_{q,v} = x_v$ +/// ([`use_constraints`]). With $y, z \ge 0$ that equality implies (B) +/// $y_{v,c,i} \le x_v$ and (D) $z_{q,v} \le x_v$, so those rows are left +/// out for it. +fn has_at_most_one_consumer(inputs: &SelectionInputs<'_>, v: usize) -> bool { + let properties = inputs.physical.physical_candidates[v].physical_properties; + properties.lifetime() == Lifetime::None && properties.timing() == ExecutionTiming::QueryTime +} + /// (E) $t_u + 1 \le t_c + n(1 - y_{u,c,i})$, only on edges inside a strongly /// connected component of the admissible edges, $n$ its size. fn acyclicity_constraints( @@ -217,24 +234,22 @@ fn use_constraints( uses_of(inputs, variables) .into_iter() .enumerate() - .map(|(v, used)| { - let properties = inputs.physical.physical_candidates[v].physical_properties; - match properties.lifetime() == Lifetime::None - && properties.timing() == ExecutionTiming::QueryTime - { - true => constraint!(used == variables.x[v]), - false => constraint!(variables.x[v] <= used), - } + .map(|(v, used)| match has_at_most_one_consumer(inputs, v) { + true => constraint!(used == variables.x[v]), + false => constraint!(variables.x[v] <= used), }) .collect() } /// The raw data of each source is kept on disk once, for the selected scan -/// that keeps the most: $r_s \ge d_v x_v$ for each query-time `Scan` $v$ of -/// source $s$, $d_v$ its cost per second of the raw data it keeps. +/// that keeps the most: $r_s \ge \mathrm{scale} \cdot d_v x_v$ for each +/// query-time `Scan` $v$ of source $s$, $d_v$ its cost per second of the +/// raw data it keeps. The objective's scale is in the row, so $r_s$ is the +/// scaled cost and its objective coefficient is 1. fn raw_data_disk_constraints( inputs: &SelectionInputs<'_>, variables: &SelectionVariables, + objective_scale: f64, ) -> Vec { inputs .costs @@ -244,7 +259,7 @@ fn raw_data_disk_constraints( .filter_map(|(v, on_disk)| { let (source, cost) = on_disk.as_ref()?; Some(constraint!( - variables.raw_data_on_disk[source] >= *cost * variables.x[v] + variables.raw_data_on_disk[source] >= *cost * objective_scale * variables.x[v] )) }) .collect() @@ -313,17 +328,6 @@ pub(crate) fn solve_selection( let components = Components::of(inputs); let mut problem = ProblemVariables::new(); let variables = selection_variables(inputs, &left_out, &components, &mut problem); - let constraints: Vec = [ - input_satisfaction_constraints(inputs, &variables), - producer_selection_constraints(inputs, &variables), - query_coverage_constraints(inputs, &variables), - acyclicity_constraints(inputs, &variables, &components), - use_constraints(inputs, &variables), - raw_data_disk_constraints(inputs, &variables), - ] - .into_iter() - .flatten() - .collect(); // Scaled by the largest coefficient of a candidate the bound keeps. let kept = |v: &usize| !left_out[*v]; let largest = (0..left_out.len()) @@ -346,6 +350,17 @@ pub(crate) fn solve_selection( true => 1_000.0 / largest, false => 1.0, }; + let constraints: Vec = [ + input_satisfaction_constraints(inputs, &variables), + producer_selection_constraints(inputs, &variables), + query_coverage_constraints(inputs, &variables), + acyclicity_constraints(inputs, &variables, &components), + use_constraints(inputs, &variables), + raw_data_disk_constraints(inputs, &variables, scale), + ] + .into_iter() + .flatten() + .collect(); let candidate_terms = (0..left_out.len()).filter(kept).filter_map(|v| { let cost = inputs.costs.candidate_cost_per_second[v].as_ref().ok()?; Some(cost * scale * variables.x[v]) @@ -354,10 +369,11 @@ pub(crate) fn solve_selection( .y .iter() .map(|(edge, &y)| inputs.costs.edge_cost_change_per_second[edge] * scale * y); + // Already scaled by its rows ([`raw_data_disk_constraints`]). let raw_data_terms = variables .raw_data_on_disk .values() - .map(|&on_disk| scale * on_disk); + .map(|&on_disk| Expression::from(on_disk)); let objective: Expression = candidate_terms .chain(edge_terms) .chain(raw_data_terms) From be4ef05ccb3f60b95e1781e3ad57e4889339f8c8 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Fri, 13 Nov 2026 19:34:45 +0000 Subject: [PATCH 19/31] test(integration): the topk-over-ratio test checks that both operands are finalized summaries (#1207 PR 6) For topk(1, sum by(job)(increase(a[6h])) / sum by(job)(increase(b[6h]))) two plans' costs differ by about 2e-11 relative (0.03720476463786/s and 0.03720476463869/s), below the solver's tolerances: each operand finalizes the sum by job itself, or finalizes an increase per series and sums it by job. The test pinned the first; with the MILP's implied rows left out the solver returns the second. It now accepts either, since both finalize an exact summary for each operand, which is what it checks. --- .../integration-tests/tests/promql_to_post_asap.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/integration-tests/tests/promql_to_post_asap.rs b/crates/integration-tests/tests/promql_to_post_asap.rs index 704ee1c23..433e81864 100644 --- a/crates/integration-tests/tests/promql_to_post_asap.rs +++ b/crates/integration-tests/tests/promql_to_post_asap.rs @@ -150,6 +150,12 @@ fn promql_binary_arithmetic_retains_two_summary_leaves() { } } +/// Each operand of the ratio is an exact summary finalized at query time: +/// a `FinalizeExactAccumulator` over a `SummaryAgg`, either the +/// `sum by (job)` itself or an increase per series finalized and then +/// summed by `job` (an `Aggregate` above it). The two plans' costs differ +/// by about 2e-11 relative, below the solver's tolerances, so it may +/// return either. #[test] fn value_ranked_topk_over_binary_ratio_finalizes_both_summary_operands() { let query = "topk(1, sum by(job)(increase(a[6h])) / sum by(job)(increase(b[6h])))"; @@ -170,7 +176,11 @@ fn value_ranked_topk_over_binary_ratio_finalizes_both_summary_operands() { panic!("expected BinaryOp below Sort, got {:?}", binary.operator); }; for operand in [lhs, rhs] { - let Some(ASAPOp::FinalizeExactAccumulator { child }) = operand.asap() else { + let finalized = match operand.non_asap() { + Some(NonASAPOp::Aggregate { child, .. }) => child, + _ => operand, + }; + let Some(ASAPOp::FinalizeExactAccumulator { child }) = finalized.asap() else { panic!( "expected exact accumulator finalization, got {:?}", operand.operator From ee55c56ff4f7df98bb13a60b7c08d1dcc36f9aa1 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Fri, 13 Nov 2026 19:16:19 +0000 Subject: [PATCH 20/31] fix(cost,types): a TimeShift view's retention holds its offset; a view over a chain refreshes per window (#1207 PR 6) The shared retention walk (windows_read_above) now starts at the producer's own offset when the producer is itself a Through reader. An ingestion-time TimeShift(1h) read by TimeRange(5m) gets retention_ms 1 h 5 m, as Stage 3, the installer's read_back_ms and the store's eviction all read it, and the cost charges what retention_ms covers with no separate offset term. maintained_window_ms returns the chain's tumbling window when the walk reaches a chain: a view over an ingestion-time chain is refreshed once per chain window, not once per second. For sum_over_time(m[5m] offset 1h) such a view was charged 218,456,000 bytes (3,901 one-second outputs); it now keeps 66 windows, as the chain below it does. The ingestion-time chain arm is gone: one retained output count (a chain's windows, a view's refreshes) serves both timings, and one ONE_SECOND_MS names the second of arriving rows. --- crates/cost/src/node/usage/mod.rs | 151 +++++++++------------ crates/cost/src/node/usage/tests.rs | 72 +++++++++- crates/cost/src/plan/read_back.rs | 3 +- crates/planner/src/installable/tests.rs | 76 +++++++++-- crates/types/src/plan/physical_asap_dag.rs | 14 +- crates/types/src/plan/time_window.rs | 33 ++++- 6 files changed, 235 insertions(+), 114 deletions(-) diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index 3a2f50aa7..8f67d7192 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -46,14 +46,16 @@ pub struct NodeCost { /// and 3, which cost (or limit) the whole candidate's peaks. pub cost_per_second: Option, /// Core-seconds of CPU it takes per second of wall time: its CPU - /// operations and scanned bytes timed by [`ResourceSpeed`](crate::ResourceSpeed), what a - /// runtime measures it against. A query's fixed per-request cost - /// ([`MeasuredOperationCosts::ns_per_evaluation`](crate::MeasuredOperationCosts::ns_per_evaluation)) is counted on the - /// node the query reads first. + /// operations and scanned bytes timed by + /// [`ResourceSpeed`](crate::ResourceSpeed), what a runtime measures it + /// against. A query's fixed per-request cost + /// ([`MeasuredOperationCosts::ns_per_evaluation`](crate::MeasuredOperationCosts::ns_per_evaluation)) + /// is counted on the node the query reads first. pub cpu_core_seconds_per_second: f64, - /// The number of consumers it is computed for, with the - /// consumers materialized at ingestion time of one refresh window counted once (1 - /// when materialized, [`execution_demand::computations`](crate::plan::execution_demand::computations)). + /// The number of consumers it is computed for, with the consumers + /// materialized at ingestion time of one refresh window counted once + /// (1 when materialized, + /// [`execution_demand::computations`](crate::plan::execution_demand::computations)). pub multiplier: u64, /// How it was measured, e.g. "scan 1000 samples; x 1.0000/s; keeps /// ... bytes": one computation's work, the times it is computed per @@ -125,12 +127,13 @@ pub struct ProducerOutput<'a> { pub tumbling_windows: u64, } -/// What one node uses and outputs ([`node_resource_usage_and_cost`]): its output statistics, sources and state family for its -/// consumers; its work for one computation (one execution) in ms on one -/// core; its usage per second (query-time memory held while it computes -/// included); what is charged once the whole plan is known (an in-memory -/// ingestion window's partial copies, the raw data a query-time scan keeps -/// on disk); its calibration fit and its [`NodeCost`]. +/// What one node uses and outputs ([`node_resource_usage_and_cost`]): its +/// output statistics, sources and state family for its consumers; its work +/// for one computation (one execution) in ms on one core; its usage per +/// second (query-time memory held while it computes included); what is +/// charged once the whole plan is known (an in-memory ingestion window's +/// partial copies, the raw data a query-time scan keeps on disk); its +/// calibration fit and its [`NodeCost`]. #[derive(Debug, Clone)] pub struct NodeResourceUsageAndCost<'a> { /// Its output rows and bytes. @@ -188,14 +191,21 @@ impl<'a> NodeResourceUsageAndCost<'a> { } } +/// One second, in ms: an ingestion-time node runs over the rows arriving +/// in one second, and an ingestion-time view that maintains no window is +/// refreshed once per second. +const ONE_SECOND_MS: u64 = 1_000; + /// The window an ingestion-time grouping `node` maintains, in ms: a /// tumbling-window chain's tumbling window, a `TimeRange`'s range, or the -/// first `TimeRange` below it through filters, projections and time shifts -/// (`input_of` gives a node's first input, `operator_of` its operator). -/// `None` when there is none. Over plan nodes ([`plan_cost`]) or memo -/// groups (Stage 3, before any plan exists). +/// first `TimeRange` or chain below it through filters, projections and +/// time shifts (a view over an ingestion-time chain is refreshed once per +/// chain window; `input_of` gives a node's first input, `operator_of` its +/// operator). `None` when there is none. Over plan nodes ([`plan_cost`]) +/// or memo groups (Stage 3, before any plan exists). /// -/// Example: `Aggregate(Sum) <- Filter <- TimeRange(1m) <- Scan`: 60,000. +/// Example: `Aggregate(Sum) <- Filter <- TimeRange(1m) <- Scan`: 60,000; +/// `TimeShift(1h) <- SummaryTumblingWindowChain(1m)`: 60,000. /// /// [`plan_cost`]: crate::plan_cost pub fn maintained_window_ms<'o, N: Copy + Ord, C: 'o>( @@ -217,6 +227,10 @@ pub fn maintained_window_ms<'o, N: Copy + Ord, C: 'o>( while seen.insert(below) { match operator_of(below) { Operator::NonASAP(NonASAPOp::TimeRange { range, .. }) => return range_ms(range), + Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { + tumbling_window_width_ms, + .. + }) => return Some(*tumbling_window_width_ms), Operator::NonASAP( NonASAPOp::Filter { .. } | NonASAPOp::Project { .. } | NonASAPOp::TimeShift { .. }, ) => below = input_of(below)?, @@ -237,7 +251,7 @@ pub fn maintained_window_ms<'o, N: Copy + Ord, C: 'o>( /// `retention_ms` ($h_v$) covers: a tumbling-window chain its windows in /// it, a query-time `Scan` its raw rows in it, any other view materialized /// at ingestion time one output per refresh in it (one second of rows -/// when it maintains no window; a `TimeShift`'s offset more). The formulas' CPU is scaled by +/// when it maintains no window). The formulas' CPU is scaled by /// `settings.frozen_calibration_factor` (1 when unset). /// /// [`executions_per_second`]: crate::plan::execution_demand::executions_per_second @@ -267,20 +281,23 @@ pub fn node_resource_usage_and_cost<'a>( // A query-time scan's raw rows retained. let mut raw_retained_rows = None; let ingestion = node.output_state.timing == ExecutionTiming::IngestionTime; - // The completed tumbling windows a chain retains: its `retention_ms` - // over its window width, at least one. - let retained_tumbling_window_count = match &node.payload { + // The outputs a node retains: its `retention_ms` over the time between + // two outputs, at least one. A chain's outputs are its tumbling + // windows; any other view materialized at ingestion time has one output + // per refresh (per window it maintains, `window_ms`; per second of + // arriving rows without one). + let output_interval_ms = match &node.payload { Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { tumbling_window_width_ms, .. - }) => node - .retention_ms - .map_or(1, |retention_ms| { - retention_ms.div_ceil((*tumbling_window_width_ms).max(1)) - }) - .max(1), - _ => 1, - }; + }) => *tumbling_window_width_ms, + _ => window_ms.unwrap_or(ONE_SECOND_MS), + } + .max(1); + let retained_output_count = node + .retention_ms + .map_or(1, |retention_ms| retention_ms.div_ceil(output_interval_ms)) + .max(1); let inputs: Vec = producers.iter().map(|p| p.output).collect(); // The tumbling windows its rows span: those a `TimeRange` selects from // a chain, 1 otherwise. @@ -427,7 +444,7 @@ pub fn node_resource_usage_and_cost<'a>( // else what the scan retains (its `retention_ms`, $h_v$); // `None` reads every row held. let lookback = match ingestion { - true => Some(1_000), + true => Some(ONE_SECOND_MS), false => window_ms.or(node.retention_ms), }; let by_rate = |lookback: Option| { @@ -710,10 +727,10 @@ pub fn node_resource_usage_and_cost<'a>( let rows_per_tumbling_window = match ingestion { true => u64::try_from( (u128::from(input.rows) * u128::from(*tumbling_window_width_ms)) - .div_ceil(1_000), + .div_ceil(u128::from(ONE_SECOND_MS)), ) .map_err(|_| AnalyticalCostError::Overflow)?, - false => input.rows.div_ceil(retained_tumbling_window_count), + false => input.rows.div_ceil(retained_output_count), }; let input_rows = StateInputRows { rows_per_state: Some(rows_per_tumbling_window.div_ceil(group_count.max(1))), @@ -855,12 +872,12 @@ pub fn node_resource_usage_and_cost<'a>( let mut peak_memory_bytes = 0u64; let mut disk_bytes = 0u64; let mut detail = format!("{detail}; x {per_second:.4}/s"); - // State retained between computations: at ingestion time, the window - // being built and the completed ones queries read (a chain of N - // tumbling windows keeps N + 1), every second. At query time the output is - // held only while the node computes (below). The state is what the - // node holds: its output, or the values an exact aggregate keeps per - // group when that is more. A computation that keeps nothing holds its + // State retained between computations: at ingestion time, the output + // being built and the completed ones its retention covers (a view of N + // retained outputs keeps N + 1), every second. At query time the + // output is held only while the node computes (below). The state is + // what the node holds: its output, or the values an exact aggregate + // keeps per group when that is more. A computation that keeps nothing holds its // working memory while it runs. let state = out.bytes.max(estimate.peak_memory_bytes()); let (materialized_bytes, medium) = match (ingestion, node.lifetime, &node.payload) { @@ -880,7 +897,7 @@ pub fn node_resource_usage_and_cost<'a>( Lifetime::AcrossEvaluations(medium), Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }), ) => { - let retained = retained_tumbling_window_count; + let retained = retained_output_count; let between = state .checked_mul(retained - 1) .ok_or(AnalyticalCostError::Overflow)?; @@ -906,58 +923,24 @@ pub fn node_resource_usage_and_cost<'a>( } (0, medium) } - // The retained tumbling windows and the one being built. - ( - true, - Lifetime::AcrossEvaluations(medium), - Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }), - ) => ( + // Raw data materialized at ingestion time: the rows it retains. + (true, Lifetime::AcrossEvaluations(medium), Payload::NonASAP(NonASAPOp::Scan { .. })) => { + (edge(raw_retained_rows.unwrap_or(out.rows))?.bytes, medium) + } + // Any other view materialized at ingestion time, a chain included: + // the outputs its `retention_ms` covers and the one being built. A + // `TimeShift`'s retention includes its offset: it holds each output + // that much longer before its readers read it. + (true, Lifetime::AcrossEvaluations(medium), _) => ( state .checked_mul( - retained_tumbling_window_count + retained_output_count .checked_add(1) .ok_or(AnalyticalCostError::Overflow)?, ) .ok_or(AnalyticalCostError::Overflow)?, medium, ), - // Raw data materialized at ingestion time: the rows it retains. - (true, Lifetime::AcrossEvaluations(medium), Payload::NonASAP(NonASAPOp::Scan { .. })) => { - (edge(raw_retained_rows.unwrap_or(out.rows))?.bytes, medium) - } - // Any other view materialized at ingestion time: the outputs its - // `retention_ms` covers, one per refresh (per window it maintains; - // per second of arriving rows without one), and the one being - // built. A `TimeShift` by an offset holds each output that much - // longer before its readers read it: over the arriving rows, the - // rows of its retention plus its offset, not one second of them. - (true, Lifetime::AcrossEvaluations(medium), _) => { - let refresh_interval_ms = window_ms.unwrap_or(1_000).max(1); - let shift_offset_ms = match &node.payload { - Payload::NonASAP(NonASAPOp::TimeShift { shift, .. }) if shift.at.is_none() => { - u64::try_from(shift.offset_ms).unwrap_or(0) - } - _ => 0, - }; - let retained_output_count = node - .retention_ms - .map_or(1, |retention_ms| { - retention_ms - .saturating_add(shift_offset_ms) - .div_ceil(refresh_interval_ms) - }) - .max(1); - ( - state - .checked_mul( - retained_output_count - .checked_add(1) - .ok_or(AnalyticalCostError::Overflow)?, - ) - .ok_or(AnalyticalCostError::Overflow)?, - medium, - ) - } // A whole state materialized across evaluations at query time: it // and the one being built. (false, Lifetime::AcrossEvaluations(medium), _) => ( diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index c7a9eccd3..c459f58d9 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -199,10 +199,11 @@ fn a_query_time_scan_reads_what_its_consumers_read_and_keeps_its_retention() { /// An ingestion-time view of raw rows with no window below it (a /// `TimeShift` by 90 s over the arriving rows) keeps what its -/// `retention_ms` and its offset cover: one second of rows per second, and -/// the second being built. Over 1,000 rows per second of 64 bytes, a 300 s +/// `retention_ms` covers, its offset included (a 5-minute read is retained +/// for 390 s, [`read_back_ms`]): one second of rows per second, and the +/// second being built. Over 1,000 rows per second of 64 bytes, a 390 s /// retention keeps 64,000 × 391 bytes, not one second's 64,000 × 2; a -/// 600 s one keeps 64,000 × 691. +/// 690 s one keeps 64,000 × 691. #[test] fn an_ingestion_time_view_of_raw_rows_keeps_the_rows_its_retention_covers() { let settings = CostInputs::default(); @@ -248,6 +249,67 @@ fn an_ingestion_time_view_of_raw_rows_keeps_the_rows_its_retention_covers() { .unwrap() .peak_memory_bytes }; - assert_eq!(kept_bytes(300_000), 64_000 * 391); - assert_eq!(kept_bytes(600_000), 64_000 * 691); + assert_eq!(kept_bytes(390_000), 64_000 * 391); + assert_eq!(kept_bytes(690_000), 64_000 * 691); +} + +/// A view over an ingestion-time chain of 60 s windows is refreshed once +/// per chain window, not once per second: `TimeShift(1h)` over the chain +/// maintains the chain's 60 s window ([`maintained_window_ms`]), and, +/// retained for 1 h 5 m (its 5-minute reader and its offset), keeps 65 +/// windows' outputs and the one being built, as many as the chain below +/// it, not 3,901 seconds' worth. +#[test] +fn a_view_over_an_ingestion_time_chain_keeps_one_output_per_chain_window() { + let settings = CostInputs::default(); + let data = DataWorkload::default(); + let chain = node(Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { + child: 0, + summary_type: FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::PerEntity, + filter: None, + tumbling_window_width_ms: 60_000, + })); + let shift = PhysicalASAPDAGNode { + id: 1, + output_state: ExecutionDataState::INGESTION_ROWS, + lifetime: Lifetime::AcrossEvaluations(StorageMedium::Memory), + retention_ms: Some(3_900_000), + ..node(Payload::NonASAP(NonASAPOp::TimeShift { + shift: asap_types::ir::operator::arguments::TimeShift { + offset_ms: 3_600_000, + at: None, + }, + child: 0, + })) + }; + let operator_of = |id: u32| match id { + 0 => &chain.payload, + _ => &shift.payload, + }; + let window_ms = maintained_window_ms(1, operator_of, |id| (id == 1).then_some(0)); + assert_eq!(window_ms, Some(60_000)); + let one_window_of_states = ProducerOutput { + payload: &chain.payload, + output_schema: &chain.output_schema, + output: EdgeStatistics { + rows: 1_000, + bytes: 16_000, + }, + sources: BTreeSet::new(), + family: StateFamily::Exact(&ExactKind::Sum), + unchanged_columns: BTreeSet::new(), + tumbling_windows: 1, + }; + let usage = node_resource_usage_and_cost( + &shift, + &[one_window_of_states], + (1, 1.0), + window_ms, + &data, + &settings, + ) + .unwrap(); + assert_eq!(usage.peak_memory_bytes, 16_000 * 66); } diff --git a/crates/cost/src/plan/read_back.rs b/crates/cost/src/plan/read_back.rs index f294ac753..d0009a124 100644 --- a/crates/cost/src/plan/read_back.rs +++ b/crates/cost/src/plan/read_back.rs @@ -20,7 +20,8 @@ use asap_types::plan::time_window::{windows_read_above, TimeIndexedRead}; /// /// Example: `sum_over_time(m[5m] offset 1h)` reads `m` back 1h + 5m, /// directly or through a query-time chain of 1-minute windows; through an -/// ingestion-time chain of 1-minute windows, `m` is read back 1 minute. +/// ingestion-time chain of 1-minute windows, `m` is read back 1 minute. A +/// stored `TimeShift(1h)` read by `TimeRange(5m)` is read back 1h + 5m. pub fn read_back_ms>( producer: N, readers_of: impl Fn(N) -> R, diff --git a/crates/planner/src/installable/tests.rs b/crates/planner/src/installable/tests.rs index c457df1ff..4c2509634 100644 --- a/crates/planner/src/installable/tests.rs +++ b/crates/planner/src/installable/tests.rs @@ -61,8 +61,14 @@ async fn view_refreshes_compile_only_the_nodes_below_them() { /// it: a chain of 10-minute windows of exact sums per `job` over /// `latency`, built at ingestion and retained for `chain_retention_ms`, /// read through `TimeRange(1h)` by a merge and its finalization at query -/// time; and the chain's node id. -fn chain_plan(chain_retention_ms: Option) -> (PhysicalASAPDAG, PhysicalASAPNodeId) { +/// time; and the chain's node id. With `shift_view` `(offset_ms, +/// retention_ms)`, the `TimeRange` reads the chain through a `TimeShift` +/// by `offset_ms`, materialized at ingestion and retained for +/// `retention_ms`. +fn chain_plan( + chain_retention_ms: Option, + shift_view: Option<(i64, u64)>, +) -> (PhysicalASAPDAG, PhysicalASAPNodeId) { use asap_types::ir::operator::{Reduction, Source}; use asap_types::ir::scalar::ColumnRef; use asap_types::ir::schema::{DataType, Field, FieldDataType, Schema}; @@ -94,10 +100,19 @@ fn chain_plan(chain_retention_ms: Option) -> (PhysicalASAPDAG, PhysicalASAP filter: None, tumbling_window_width_ms: 600_000, })); + let shift = shift_view.map(|(offset_ms, _)| { + node(Operator::NonASAP(NonASAPOp::TimeShift { + shift: asap_types::ir::operator::arguments::TimeShift { + offset_ms, + at: None, + }, + child: chain.clone(), + })) + }); let windows = node(Operator::NonASAP(NonASAPOp::TimeRange { range: std::time::Duration::from_secs(3_600), kind: TimeRangeKind::Range, - child: chain.clone(), + child: shift.clone().unwrap_or_else(|| chain.clone()), })); let merged = node(Operator::ASAP(ASAPOp::SummaryMerge { children: vec![windows], @@ -112,19 +127,29 @@ fn chain_plan(chain_retention_ms: Option) -> (PhysicalASAPDAG, PhysicalASAP ]) .unwrap(); let chain_id = nodes.iter().position(|n| Rc::ptr_eq(n, &chain)).unwrap(); + let shift_id = shift.map(|shift| nodes.iter().position(|n| Rc::ptr_eq(n, &shift)).unwrap()); let scan_id = producer_of(&dag, chain_id); + let at_ingestion = [Some(chain_id), shift_id, Some(scan_id)]; for node in &mut dag.nodes { - if node.id == chain_id { + let (retention_ms, coverage) = match node.id { + id if id == chain_id => (chain_retention_ms, "sum of latency by job"), + id if Some(id) == shift_id => ( + shift_view.map(|(_, retention_ms)| retention_ms), + "sum of latency by job, shifted", + ), + _ => (None, ""), + }; + if !coverage.is_empty() { node.lifetime = Lifetime::AcrossEvaluations(StorageMedium::Memory); - node.retention_ms = chain_retention_ms; - node.view_coverage = Some(ViewCoverage::new("sum of latency by job".into(), vec![])); + node.retention_ms = retention_ms; + node.view_coverage = Some(ViewCoverage::new(coverage.into(), vec![])); } - if node.id == chain_id || node.id == scan_id { + if at_ingestion.contains(&Some(node.id)) { node.output_state.timing = ExecutionTiming::IngestionTime; } } for edge in &mut dag.edges { - if edge.producer == chain_id || edge.producer == scan_id { + if at_ingestion.contains(&Some(edge.producer)) { edge.data_state_mut().timing = ExecutionTiming::IngestionTime; } } @@ -159,7 +184,7 @@ fn producer_of(dag: &PhysicalASAPDAG, consumer: PhysicalASAPNodeId) -> PhysicalA /// one 10-minute window per run, and no raw data is kept for queries. #[test] fn a_chain_is_installed_as_one_view() { - let (dag, chain) = chain_plan(Some(3_600_000)); + let (dag, chain) = chain_plan(Some(3_600_000), None); let plan = installable_plan_of(dag.clone(), &[entry()]).unwrap(); assert_eq!(plan.dag, dag); let views = plan.materialized_views().unwrap(); @@ -185,26 +210,53 @@ fn a_chain_is_installed_as_one_view() { fn installation_checks_the_retention_stage3_set() { let entries = [entry()]; assert!(matches!( - installable_plan_of(chain_plan(None).0, &entries), + installable_plan_of(chain_plan(None, None).0, &entries), Err(InstallablePlanError::Uninstallable( InstallableDagError::ViewWithoutRetention { .. } )) )); assert!(matches!( - installable_plan_of(chain_plan(Some(3_000_000)).0, &entries), + installable_plan_of(chain_plan(Some(3_000_000), None).0, &entries), Err(InstallablePlanError::Uninstallable( InstallableDagError::ViewRetentionTooShort { .. } )) )); } +/// A `TimeShift(1h)` view over the chain, read by `TimeRange(1h)`, keeps +/// each row an hour before it is read: its retention must cover 2 h, the +/// hour its reader reads and the hour of its offset, not only the hour +/// read. (The chain below it is read back 2 h too.) +#[test] +fn a_time_shift_view_keeps_its_offset_more() { + let entries = [entry()]; + let shifted = |retention_ms| chain_plan(Some(7_200_000), Some((3_600_000, retention_ms))).0; + assert!(matches!( + installable_plan_of(shifted(3_600_000), &entries), + Err(InstallablePlanError::Uninstallable( + InstallableDagError::ViewRetentionTooShort { .. } + )) + )); + let views = installable_plan_of(shifted(7_200_000), &entries) + .unwrap() + .materialized_views() + .unwrap(); + assert_eq!( + views + .iter() + .map(|view| view.retention_ms()) + .collect::>(), + [7_200_000, 7_200_000] + ); +} + /// A query-time `Scan` read through a window carries its retention; /// one without is refused, and one with it is the raw data kept for its /// source. Read through the chain built at query time, it is read as far /// back as the chain's `TimeRange(1h)` reads, not one 10-minute window. #[test] fn a_query_time_scan_carries_its_retention() { - let (mut dag, chain) = chain_plan(Some(3_600_000)); + let (mut dag, chain) = chain_plan(Some(3_600_000), None); let scan = producer_of(&dag, chain); for node in &mut dag.nodes { if node.id == chain || node.id == scan { diff --git a/crates/types/src/plan/physical_asap_dag.rs b/crates/types/src/plan/physical_asap_dag.rs index 104ce2588..d38329d20 100644 --- a/crates/types/src/plan/physical_asap_dag.rs +++ b/crates/types/src/plan/physical_asap_dag.rs @@ -85,12 +85,14 @@ pub struct PhysicalASAPDAGNode { pub parameters: Option, pub output_schema: Schema, /// For a materialized view (a node whose lifetime materializes its output - /// across evaluations), how far back from the newest evaluation - /// time its stored rows are kept; for a raw-data `Scan` (at query time or materialized), how far back - /// its source's rows are kept (`None` when it reads them unbounded): - /// $h_v$, which Stage 3 sets. `None` on every other node. A - /// node field rather than part of [`Lifetime`], because Stage 2 - /// enumerates lifetimes before Stage 3 knows retention. + /// across evaluations), how far back from the newest evaluation time + /// its stored rows are kept (a `TimeShift`'s offset included: its + /// readers read each row that much later); for a raw-data `Scan` (at + /// query time or materialized), how far back its source's rows are kept + /// (`None` when it reads them unbounded): $h_v$, which Stage 3 sets. + /// `None` on every other node. A node field rather than part of + /// [`Lifetime`], because Stage 2 enumerates lifetimes before Stage 3 + /// knows retention. #[serde(default, skip_serializing_if = "Option::is_none")] pub retention_ms: Option, /// The physical candidate the node was built from: in-memory provenance diff --git a/crates/types/src/plan/time_window.rs b/crates/types/src/plan/time_window.rs index 1c97ddc81..11bfe76c3 100644 --- a/crates/types/src/plan/time_window.rs +++ b/crates/types/src/plan/time_window.rs @@ -114,15 +114,19 @@ impl TimeIndexedRead { /// walking up from it through `Through` readers (`readers_of` gives a /// node's readers, `read_of` how a reader reads), each reader that ends a /// path with the window it reads back from the evaluation time, the -/// offsets of the shifts walked through included: a `Window` reader its -/// window, an `AtEvaluationTime` reader the empty window at its offset. +/// offsets of the shifts walked through included, starting with the +/// producer's own when it is a `Through` reader itself (a stored +/// `TimeShift(1h)` keeps each row an hour before its readers read it): a +/// `Window` reader its window, an `AtEvaluationTime` reader the empty +/// window at its offset. /// `None`, unbounded, when one reads it `Whole`, an `AtEvaluationTime` /// reader reads it and `at_evaluation_time_reads_whole` (raw data, which /// is not stored by evaluation time), or a path's offsets are negative or /// overflow. /// /// Example: a `Scan` read by `TimeRange(5m)` and by `TimeShift(1h) -> -/// TimeRange(5m)` is read through `{0, 5m}` and `{1h, 5m}`. +/// TimeRange(5m)` is read through `{0, 5m}` and `{1h, 5m}`; the +/// `TimeShift(1h)` itself through `{1h, 5m}`. pub fn windows_read_above>( producer: N, readers_of: impl Fn(N) -> R, @@ -131,7 +135,11 @@ pub fn windows_read_above>( ) -> Option> { let mut windows = Vec::new(); // (node whose readers are next, the offsets walked through below them) - let mut pending: Vec<(N, i64)> = vec![(producer, 0)]; + let own_offset_ms = match read_of(producer) { + TimeIndexedRead::Through { offset_ms } => offset_ms, + _ => 0, + }; + let mut pending: Vec<(N, i64)> = vec![(producer, own_offset_ms)]; let mut seen = std::collections::BTreeSet::new(); while let Some((node, offset_ms)) = pending.pop() { if !seen.insert((node, offset_ms)) { @@ -262,8 +270,9 @@ mod tests { /// Nodes 0 (a scan) ← 1 `TimeRange(5m)`, and 0 ← 2 `TimeShift(1h)` ← 3 /// `TimeRange(5m)`; node 4, a reader at the evaluation time, reads 0 - /// too. The scan is read through `{0, 5m}` and `{1h, 5m}`; the reader - /// at the evaluation time reads raw data whole. + /// too. The scan is read through `{0, 5m}` and `{1h, 5m}`, the shift + /// through `{1h, 5m}`; the reader at the evaluation time reads raw data + /// whole. #[test] fn readers_read_a_producer_through_the_shifts_above_it() { let readers = |node: u32| -> Vec { @@ -281,6 +290,18 @@ mod tests { _ => TimeIndexedRead::AtEvaluationTime, }; let mut windows = windows_read_above(0, readers, read_of, true).unwrap(); + // The shift itself is read an hour back too: what it stores is read + // an hour after it is stored. + assert_eq!( + windows_read_above(2, readers, read_of, false), + Some(vec![( + 3, + TimeWindow { + end_offset_ms: 3_600_000, + width_ms: 300_000 + } + )]) + ); windows.sort(); assert_eq!( windows, From 2b67906f8f70b8afe0ac00293f9a71656a3a5948 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Fri, 13 Nov 2026 19:16:19 +0000 Subject: [PATCH 21/31] refactor(cost): inline plan_cost's one-caller node loop and fix its doc (#1207 PR 6) The private EachNodeResourceUsageAndCost struct had one caller and only returned two maps; its loop is now in plan_cost. The plan_cost doc says that every node materialized across evaluations keeps what its retention_ms covers, not only a chain. --- crates/cost/src/plan/cost.rs | 104 ++++++++++++++--------------------- 1 file changed, 40 insertions(+), 64 deletions(-) diff --git a/crates/cost/src/plan/cost.rs b/crates/cost/src/plan/cost.rs index 7904d2c11..19e281c1e 100644 --- a/crates/cost/src/plan/cost.rs +++ b/crates/cost/src/plan/cost.rs @@ -79,8 +79,9 @@ impl QueryLatency { /// time ranges keep, times how often it is computed per second /// ([`computations`]); an ingestion-time node over one second of arriving /// rows. A materialized node's state is counted in byte-seconds per -/// second; a tumbling-window chain materialized beyond one evaluation keeps the tumbling windows its -/// `retention_ms` ($h_v$) covers. `entries[q]` says how often query `q` +/// second; a node materialized across evaluations keeps what its +/// `retention_ms` ($h_v$) covers ([`node_resource_usage_and_cost`]). +/// `entries[q]` says how often query `q` /// runs and how many steps each run evaluates; the formulas are scaled by /// `settings.frozen_calibration_factor` (1 when unset). pub fn plan_cost( @@ -100,10 +101,43 @@ pub fn plan_cost( CostModel::UsageOverTime(unit_cost) => Some(unit_cost), _ => None, }; - let EachNodeResourceUsageAndCost { - costed, - multipliers, - } = each_node_resource_usage_and_cost(dag, nodes, entries, data, settings)?; + // Each node's `(multiplier, per_second)` ([`computations`]), then its + // usage and cost ([`node_resource_usage_and_cost`]), children first. + let multipliers = computations(dag, entries, settings.non_repeating_query_horizon_seconds) + .map_err(|error| (first, error))?; + let mut producers_of: HashMap> = HashMap::new(); + let mut consumers_of: HashMap> = HashMap::new(); + for e in &dag.edges { + producers_of.entry(e.consumer).or_default().push(e.producer); + consumers_of.entry(e.producer).or_default().push(e.consumer); + } + let mut costed: HashMap> = HashMap::new(); + for node in &dag.nodes { + let views: Vec> = producers_of + .get(&node.id) + .map_or(&[][..], Vec::as_slice) + .iter() + .map(|p| costed[p].producer_output(&nodes[p].payload, &nodes[p].output_schema)) + .collect(); + let window_ms = match node.output_state.timing { + ExecutionTiming::IngestionTime => maintained_window_ms( + node.id, + |id| &nodes[&id].payload, + |id| producers_of.get(&id)?.first().copied(), + ), + ExecutionTiming::QueryTime => scan_read_ms(node, nodes, &consumers_of), + }; + let node_costed = node_resource_usage_and_cost( + node, + &views, + multipliers[&node.id], + window_ms, + data, + settings, + ) + .map_err(|error| (node.id, error))?; + costed.insert(node.id, node_costed); + } let mut output: HashMap = HashMap::new(); let mut per_node = BTreeMap::new(); // The candidate's usage, accumulated node by node; peak cores and @@ -362,64 +396,6 @@ pub fn raw_data_disk_cost_per_second(bytes: u64, settings: &CostInputs) -> Optio } } -/// Every node of a physical DAG costed ([`each_node_resource_usage_and_cost`]), with each -/// node's `(multiplier, per_second)` ([`computations`]). -struct EachNodeResourceUsageAndCost<'a> { - costed: HashMap>, - multipliers: HashMap, -} - -/// [`node_resource_usage_and_cost`] over every node of `dag`, children -/// first, with the arguments of [`plan_cost`]. -fn each_node_resource_usage_and_cost<'a>( - dag: &'a PhysicalASAPDAG, - nodes: &NodeMap<'a>, - entries: &[QueryWorkloadEntry], - data: &DataWorkload, - settings: &'a CostInputs, -) -> Result, (PhysicalASAPNodeId, AnalyticalCostError)> { - let first = dag.nodes.first().map(|n| n.id).unwrap_or_default(); - let multipliers = computations(dag, entries, settings.non_repeating_query_horizon_seconds) - .map_err(|error| (first, error))?; - let mut producers_of: HashMap> = HashMap::new(); - let mut consumers_of: HashMap> = HashMap::new(); - for e in &dag.edges { - producers_of.entry(e.consumer).or_default().push(e.producer); - consumers_of.entry(e.producer).or_default().push(e.consumer); - } - let mut costed: HashMap> = HashMap::new(); - for node in &dag.nodes { - let views: Vec> = producers_of - .get(&node.id) - .map_or(&[][..], Vec::as_slice) - .iter() - .map(|p| costed[p].producer_output(&nodes[p].payload, &nodes[p].output_schema)) - .collect(); - let window_ms = match node.output_state.timing { - ExecutionTiming::IngestionTime => maintained_window_ms( - node.id, - |id| &nodes[&id].payload, - |id| producers_of.get(&id)?.first().copied(), - ), - ExecutionTiming::QueryTime => scan_read_ms(node, nodes, &consumers_of), - }; - let node_costed = node_resource_usage_and_cost( - node, - &views, - multipliers[&node.id], - window_ms, - data, - settings, - ) - .map_err(|error| (node.id, error))?; - costed.insert(node.id, node_costed); - } - Ok(EachNodeResourceUsageAndCost { - costed, - multipliers, - }) -} - /// The window a query-time `Scan` `node` reads per execution: the most /// offset + range its consumers read ([`read_back_ms`], no range-evaluation /// span), once however many consumers read it. `None` for any other node, From a174073381a17ff1eaeb4372f410568cb80c925b Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Fri, 13 Nov 2026 19:16:19 +0000 Subject: [PATCH 22/31] feat(devtools): print the selection MILP's objective in asap_planner (#1207 PR 6) Selection::selected_plan_cost_per_second is printed as milp_objective_cost_per_second next to the MILP's size and times, and milp_constraint_count has its own doc comment. --- crates/devtools/src/bin/asap_planner.rs | 4 +++- crates/stage3-plan-selection/src/report.rs | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/devtools/src/bin/asap_planner.rs b/crates/devtools/src/bin/asap_planner.rs index 0709e5c4e..6db6b6591 100644 --- a/crates/devtools/src/bin/asap_planner.rs +++ b/crates/devtools/src/bin/asap_planner.rs @@ -61,7 +61,8 @@ // `lifetime` and `reason`); // - stage3_plan_selection (from `stage3`): the `plan` Stage 3 built (a // `PhysicalASAPDAG`) and its `Selection`: `accepted_accuracy_per_query`, -// `pruned_candidates`, `milp_variables`, `milp_constraints`, +// `pruned_candidates`, `milp_objective_cost_per_second` (the selected +// plan's cost per second), `milp_variables`, `milp_constraints`, // `milp_construction_ms` and `milp_solve_ms`; `error` when no plan was // selected. // `materialized_views` are the plan's materialized views as @@ -619,6 +620,7 @@ fn selection_json(selection: &Result<(PhysicalASAPDAG, Selection), SelectionErro .iter() .map(|p| json!({ "candidate": format!("{:?}", p.candidate), "reason": format!("{:?}", p.reason) })) .collect::>(), + "milp_objective_cost_per_second": selection.selected_plan_cost_per_second, "milp_variables": selection.milp_variable_count, "milp_constraints": selection.milp_constraint_count, "milp_construction_ms": selection.milp_construction_ms, diff --git a/crates/stage3-plan-selection/src/report.rs b/crates/stage3-plan-selection/src/report.rs index 51f551507..31768b6af 100644 --- a/crates/stage3-plan-selection/src/report.rs +++ b/crates/stage3-plan-selection/src/report.rs @@ -153,8 +153,9 @@ pub struct Selection { /// under cost model 1, which `asap_cost::plan_cost` of the plan /// reports too. pub selected_plan_cost_per_second: f64, - /// The selection MILP's size: its variables and its constraint rows. + /// The selection MILP's variables (columns). pub milp_variable_count: usize, + /// The selection MILP's constraint rows. pub milp_constraint_count: usize, /// How long building and solving the selection MILP took. pub milp_construction_ms: u64, From 7b2ed263f8ab4019ab90858c38044b47368ca973 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Fri, 13 Nov 2026 20:20:02 +0000 Subject: [PATCH 23/31] refactor(stage3,cost): one selected-plan cost, one window rule for chains, clearer names (#1207 PR 6) - has_at_most_one_consumer is is_unmaterialized_query_time: it tests a query-time result that is not materialized, which is why it has at most one consumer. The bound's own copy of the test calls it. - plan_cost_per_second sums a plan's candidates, edges and each source's raw data on disk once; the bound and the solved optimum both call it (the optimum adds the request overhead). - Stage 3's cost_node no longer matches a chain before calling maintained_window_ms, which already returns a chain's width: Pass 2 adds chain candidates only to the group that computes the chain. - Nits: the retained-count alias, a rewrapped plan_cost doc, and the installer test helper's views as an Option instead of an empty string. --- crates/cost/src/node/usage/mod.rs | 5 +- crates/cost/src/plan/cost.rs | 6 +- crates/planner/src/installable/tests.rs | 13 ++-- crates/stage3-plan-selection/src/cost.rs | 34 ++++------ .../src/milp/model/mod.rs | 68 +++++++++++-------- .../src/milp/upper_bound.rs | 32 +++------ 6 files changed, 74 insertions(+), 84 deletions(-) diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index 8f67d7192..8574bbe0c 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -897,15 +897,14 @@ pub fn node_resource_usage_and_cost<'a>( Lifetime::AcrossEvaluations(medium), Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }), ) => { - let retained = retained_output_count; let between = state - .checked_mul(retained - 1) + .checked_mul(retained_output_count - 1) .ok_or(AnalyticalCostError::Overflow)?; match medium { StorageMedium::Memory => { average_memory_bytes = between as f64; peak_memory_bytes = state - .checked_mul(retained) + .checked_mul(retained_output_count) .ok_or(AnalyticalCostError::Overflow)?; } StorageMedium::Disk => disk_bytes = between, diff --git a/crates/cost/src/plan/cost.rs b/crates/cost/src/plan/cost.rs index 19e281c1e..3cec75b0d 100644 --- a/crates/cost/src/plan/cost.rs +++ b/crates/cost/src/plan/cost.rs @@ -81,9 +81,9 @@ impl QueryLatency { /// rows. A materialized node's state is counted in byte-seconds per /// second; a node materialized across evaluations keeps what its /// `retention_ms` ($h_v$) covers ([`node_resource_usage_and_cost`]). -/// `entries[q]` says how often query `q` -/// runs and how many steps each run evaluates; the formulas are scaled by -/// `settings.frozen_calibration_factor` (1 when unset). +/// `entries[q]` says how often query `q` runs and how many steps each run +/// evaluates; the formulas are scaled by `settings.frozen_calibration_factor` +/// (1 when unset). pub fn plan_cost( dag: &PhysicalASAPDAG, entries: &[QueryWorkloadEntry], diff --git a/crates/planner/src/installable/tests.rs b/crates/planner/src/installable/tests.rs index 4c2509634..0258d827c 100644 --- a/crates/planner/src/installable/tests.rs +++ b/crates/planner/src/installable/tests.rs @@ -131,15 +131,16 @@ fn chain_plan( let scan_id = producer_of(&dag, chain_id); let at_ingestion = [Some(chain_id), shift_id, Some(scan_id)]; for node in &mut dag.nodes { - let (retention_ms, coverage) = match node.id { - id if id == chain_id => (chain_retention_ms, "sum of latency by job"), - id if Some(id) == shift_id => ( + // The views: the chain, and the shift when it is one. + let view = match node.id { + id if id == chain_id => Some((chain_retention_ms, "sum of latency by job")), + id if Some(id) == shift_id => Some(( shift_view.map(|(_, retention_ms)| retention_ms), "sum of latency by job, shifted", - ), - _ => (None, ""), + )), + _ => None, }; - if !coverage.is_empty() { + if let Some((retention_ms, coverage)) = view { node.lifetime = Lifetime::AcrossEvaluations(StorageMedium::Memory); node.retention_ms = retention_ms; node.view_coverage = Some(ViewCoverage::new(coverage.into(), vec![])); diff --git a/crates/stage3-plan-selection/src/cost.rs b/crates/stage3-plan-selection/src/cost.rs index 4f096637b..40f0b350c 100644 --- a/crates/stage3-plan-selection/src/cost.rs +++ b/crates/stage3-plan-selection/src/cost.rs @@ -34,7 +34,7 @@ use asap_cost::{ }; use asap_types::ir::operator::Source; use asap_types::ir::properties::ExecutionTiming; -use asap_types::ir::{ASAPOp, NonASAPOp, Operator}; +use asap_types::ir::{NonASAPOp, Operator}; use asap_types::plan::physical_asap_dag::{ PhysicalASAPDAGNode, PhysicalASAPNodeId, PhysicalASAPOperatorPayload, }; @@ -313,25 +313,19 @@ fn cost_node<'a>( .map_err(|error| error.to_string())?; let window_ms = match node.output_state.timing { ExecutionTiming::QueryTime => scan_read_ms, - // A chain maintains its own window; any other grouping the first - // range below its group. - ExecutionTiming::IngestionTime => match &node.payload { - Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { - tumbling_window_width_ms, - .. - }) => Some(*tumbling_window_width_ms), - _ => maintained_window_ms( - group, - |group: MemoGroupId| &memo.groups()[group.0 as usize].expression, - |group| { - memo.groups()[group.0 as usize] - .expression - .children() - .first() - .map(|g| **g) - }, - ), - }, + // A chain's group computes the chain (Pass 2 adds chain candidates + // only to it), so the group's window is the chain's. + ExecutionTiming::IngestionTime => maintained_window_ms( + group, + |group: MemoGroupId| &memo.groups()[group.0 as usize].expression, + |group| { + memo.groups()[group.0 as usize] + .expression + .children() + .first() + .map(|g| **g) + }, + ), }; node_resource_usage_and_cost(node, inputs, (1, per_second), window_ms, data, cost_inputs) .map_err(|error| error.to_string()) diff --git a/crates/stage3-plan-selection/src/milp/model/mod.rs b/crates/stage3-plan-selection/src/milp/model/mod.rs index f4c3f73a5..ce2d5c7cf 100644 --- a/crates/stage3-plan-selection/src/milp/model/mod.rs +++ b/crates/stage3-plan-selection/src/milp/model/mod.rs @@ -147,8 +147,8 @@ fn input_satisfaction_constraints( constraints } -/// (B) $y_{u,c,i} \le x_u$, except for a producer with at most one -/// consumer ([`has_at_most_one_consumer`]), whose (F) row implies it. +/// (B) $y_{u,c,i} \le x_u$, except for an unmaterialized query-time +/// producer ([`is_unmaterialized_query_time`]), whose (F) row implies it. fn producer_selection_constraints( inputs: &SelectionInputs<'_>, variables: &SelectionVariables, @@ -158,15 +158,15 @@ fn producer_selection_constraints( .iter() .filter_map(|(&edge, &y)| { let producer = inputs.physical.edge(edge).producer_candidate_id; - (!has_at_most_one_consumer(inputs, producer.0)) + (!is_unmaterialized_query_time(inputs, producer.0)) .then(|| constraint!(y <= variables.x[producer.0])) }) .collect() } /// (C) $\sum_{v \in G_q} z_{q,v} = 1$; (D) $z_{q,v} \le x_v$, except for -/// a candidate with at most one consumer ([`has_at_most_one_consumer`]), -/// whose (F) row implies it. +/// an unmaterialized query-time candidate +/// ([`is_unmaterialized_query_time`]), whose (F) row implies it. fn query_coverage_constraints( inputs: &SelectionInputs<'_>, variables: &SelectionVariables, @@ -177,7 +177,7 @@ fn query_coverage_constraints( constraints.push(constraint!(covered == 1)); } for (&(_, v), &z) in &variables.z { - if !has_at_most_one_consumer(inputs, v.0) { + if !is_unmaterialized_query_time(inputs, v.0) { constraints.push(constraint!(z <= variables.x[v.0])); } } @@ -185,16 +185,39 @@ fn query_coverage_constraints( } /// Whether candidate `v` is a query-time result that is not materialized -/// ($\ell$ = None): it has at most one consumer, and its (F) row is the +/// ($\ell$ = None), so it has at most one consumer and its (F) row is the /// equality $\sum_{(v,c,i)} y_{v,c,i} + \sum_q z_{q,v} = x_v$ /// ([`use_constraints`]). With $y, z \ge 0$ that equality implies (B) /// $y_{v,c,i} \le x_v$ and (D) $z_{q,v} \le x_v$, so those rows are left /// out for it. -fn has_at_most_one_consumer(inputs: &SelectionInputs<'_>, v: usize) -> bool { +pub(super) fn is_unmaterialized_query_time(inputs: &SelectionInputs<'_>, v: usize) -> bool { let properties = inputs.physical.physical_candidates[v].physical_properties; properties.lifetime() == Lifetime::None && properties.timing() == ExecutionTiming::QueryTime } +/// The cost per second of a plan of `candidates` reading over edges that +/// cost `edge_costs` ($c_{u,c,i}$), without the request overhead every plan +/// pays: the candidates' $w_v$, the edges' costs, and each source's raw +/// data on disk once, at its largest selected scan. +pub(super) fn plan_cost_per_second( + inputs: &SelectionInputs<'_>, + candidates: impl IntoIterator, + edge_costs: impl IntoIterator, +) -> f64 { + let mut raw_data_on_disk: BTreeMap<&Source, f64> = BTreeMap::new(); + let mut cost = 0.0; + for v in candidates { + if let Ok(own) = &inputs.costs.candidate_cost_per_second[v] { + cost += own; + } + if let Some((source, on_disk)) = &inputs.costs.raw_data_on_disk[v] { + let kept = raw_data_on_disk.entry(source).or_default(); + *kept = kept.max(*on_disk); + } + } + cost + edge_costs.into_iter().sum::() + raw_data_on_disk.values().sum::() +} + /// (E) $t_u + 1 \le t_c + n(1 - y_{u,c,i})$, only on edges inside a strongly /// connected component of the admissible edges, $n$ its size. fn acyclicity_constraints( @@ -234,7 +257,7 @@ fn use_constraints( uses_of(inputs, variables) .into_iter() .enumerate() - .map(|(v, used)| match has_at_most_one_consumer(inputs, v) { + .map(|(v, used)| match is_unmaterialized_query_time(inputs, v) { true => constraint!(used == variables.x[v]), false => constraint!(variables.x[v] <= used), }) @@ -418,29 +441,16 @@ pub(crate) fn solve_selection( other => return Err(SelectionMilpError::Solver(format!("{other:?}"))), }; let chosen = |variable: Variable| columns[column_of[&variable]] > 0.5; - // The optimum, unscaled: the selected candidates' and edges' costs, - // each source's raw data on disk at its largest selected scan, and the - // request overhead every plan pays. - let mut raw_data_on_disk: BTreeMap<&Source, f64> = BTreeMap::new(); - for (source, cost) in (0..variables.x.len()) - .filter(|&v| chosen(variables.x[v])) - .filter_map(|v| inputs.costs.raw_data_on_disk[v].as_ref()) - { - let kept = raw_data_on_disk.entry(source).or_default(); - *kept = kept.max(*cost); - } - let objective_cost_per_second = (0..variables.x.len()) - .filter(|&v| chosen(variables.x[v])) - .filter_map(|v| inputs.costs.candidate_cost_per_second[v].as_ref().ok()) - .sum::() - + variables + // The optimum, unscaled, with the request overhead every plan pays. + let objective_cost_per_second = plan_cost_per_second( + inputs, + (0..variables.x.len()).filter(|&v| chosen(variables.x[v])), + variables .y .iter() .filter(|&(_, &y)| chosen(y)) - .map(|(edge, _)| inputs.costs.edge_cost_change_per_second[edge]) - .sum::() - + raw_data_on_disk.values().sum::() - + inputs.costs.request_overhead_cost_per_second; + .map(|(edge, _)| inputs.costs.edge_cost_change_per_second[edge]), + ) + inputs.costs.request_overhead_cost_per_second; Ok(SolvedSelection { objective_cost_per_second, candidates: (0..variables.x.len()) diff --git a/crates/stage3-plan-selection/src/milp/upper_bound.rs b/crates/stage3-plan-selection/src/milp/upper_bound.rs index a1d905551..85d018eb5 100644 --- a/crates/stage3-plan-selection/src/milp/upper_bound.rs +++ b/crates/stage3-plan-selection/src/milp/upper_bound.rs @@ -12,13 +12,11 @@ //! greedy plan bounds the optimum by about 0.0088/s, so the chain is left //! out and the largest coefficient left is about 0.006/s. -use std::collections::{BTreeMap, HashMap}; +use std::collections::HashMap; -use asap_types::ir::operator::Source; -use asap_types::ir::properties::ExecutionTiming; -use asap_types::stages::{Lifetime, PhysicalCandidateId}; +use asap_types::stages::PhysicalCandidateId; -use super::model::SelectionInputs; +use super::model::{is_unmaterialized_query_time, plan_cost_per_second, SelectionInputs}; /// How many times the greedy plan is rebuilt without a candidate that /// made it infeasible before giving up on a bound. @@ -88,10 +86,7 @@ fn feasible_plan_cost_per_second(inputs: &SelectionInputs<'_>) -> Option { let inputs_of: Vec = (0..count) .map(|v| physical.input_count(PhysicalCandidateId(v))) .collect(); - let unshared = |v: usize| { - let properties = physical.physical_candidates[v].physical_properties; - properties.lifetime() == Lifetime::None && properties.timing() == ExecutionTiming::QueryTime - }; + let unshared = |v: usize| is_unmaterialized_query_time(inputs, v); let mut excluded = vec![false; count]; for _ in 0..REPAIRS { let (tree_cost, chosen) = cheapest_trees(inputs, &into, &inputs_of, &excluded); @@ -126,20 +121,11 @@ fn feasible_plan_cost_per_second(inputs: &SelectionInputs<'_>) -> Option { excluded[shared] = true; continue; } - let mut raw_data_on_disk: BTreeMap<&Source, f64> = BTreeMap::new(); - for &v in &plan.candidates { - if let Some((source, cost)) = &inputs.costs.raw_data_on_disk[v] { - let kept = raw_data_on_disk.entry(source).or_default(); - *kept = kept.max(*cost); - } - } - let cost = plan - .candidates - .iter() - .filter_map(|&v| inputs.costs.candidate_cost_per_second[v].as_ref().ok()) - .sum::() - + plan.edges.iter().map(|&(_, cost)| cost).sum::() - + raw_data_on_disk.values().sum::(); + let cost = plan_cost_per_second( + inputs, + plan.candidates.iter().copied(), + plan.edges.iter().map(|&(_, cost)| cost), + ); return cost.is_finite().then_some(cost); } None From 6035803982dff2b67882e0896627dbf25e0e05ca Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Fri, 13 Nov 2026 20:21:58 +0000 Subject: [PATCH 24/31] fix(cost): a view without retention is not costed (#1207 PR 6) A view materialized across evaluations whose readers read it unbounded (a negative offset, an `@` anchor) has no retention_ms. The cost charged that as one output, so sum_over_time(m[5m] offset -5m) selected an ingestion-time TimeShift(-5m) view at 0.01225/s that the installer then refused (ViewWithoutRetention). Such a node now fails with MissingOrZero("retention_ms"), the same set of nodes the installer refuses (any view across evaluations but a Scan), so Stage 3 does not select it. --- crates/cost/src/node/usage/mod.rs | 10 ++++++ crates/cost/src/node/usage/tests.rs | 52 +++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index 8574bbe0c..c48a93b47 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -294,6 +294,16 @@ pub fn node_resource_usage_and_cost<'a>( _ => window_ms.unwrap_or(ONE_SECOND_MS), } .max(1); + // A view materialized across evaluations keeps what its retention + // covers. Without one (a reader reads it unbounded: a negative offset, + // an `@` anchor) nothing bounds what it keeps and it cannot be + // installed, so it is not costed; a `Scan` keeps raw rows (below). + if matches!(node.lifetime, Lifetime::AcrossEvaluations(_)) + && !matches!(node.payload, Payload::NonASAP(NonASAPOp::Scan { .. })) + && node.retention_ms.is_none() + { + return Err(AnalyticalCostError::MissingOrZero("retention_ms")); + } let retained_output_count = node .retention_ms .map_or(1, |retention_ms| retention_ms.div_ceil(output_interval_ms)) diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index c459f58d9..d4027c350 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -253,6 +253,58 @@ fn an_ingestion_time_view_of_raw_rows_keeps_the_rows_its_retention_covers() { assert_eq!(kept_bytes(690_000), 64_000 * 691); } +/// A view materialized across evaluations without a retention is read +/// unbounded (here `sum_over_time(m[5m] offset -5m)`: a `TimeShift` by +/// -5 m over the arriving rows, which no retention covers), so nothing +/// bounds what it keeps and it cannot be installed: it is not costed, +/// rather than charged for one output and selected as the cheapest. +#[test] +fn a_view_without_retention_is_not_costed() { + let settings = CostInputs::default(); + let data = DataWorkload::default(); + let scan = node(Payload::NonASAP(NonASAPOp::Scan { + source: Source::TimeSeries { metric: "m".into() }, + predicates: vec![], + schema: Schema::default(), + })); + let one_second_of_rows = ProducerOutput { + payload: &scan.payload, + output_schema: &scan.output_schema, + output: EdgeStatistics { + rows: 1_000, + bytes: 64_000, + }, + sources: BTreeSet::new(), + family: StateFamily::None, + unchanged_columns: BTreeSet::new(), + tumbling_windows: 1, + }; + let shift = PhysicalASAPDAGNode { + output_state: ExecutionDataState::INGESTION_ROWS, + lifetime: Lifetime::AcrossEvaluations(StorageMedium::Memory), + retention_ms: None, + ..node(Payload::NonASAP(NonASAPOp::TimeShift { + shift: asap_types::ir::operator::arguments::TimeShift { + offset_ms: -300_000, + at: None, + }, + child: 0, + })) + }; + let usage = node_resource_usage_and_cost( + &shift, + std::slice::from_ref(&one_second_of_rows), + (1, 1.0), + None, + &data, + &settings, + ); + assert!(matches!( + usage, + Err(AnalyticalCostError::MissingOrZero("retention_ms")) + )); +} + /// A view over an ingestion-time chain of 60 s windows is refreshed once /// per chain window, not once per second: `TimeShift(1h)` over the chain /// maintains the chain's 60 s window ([`maintained_window_ms`]), and, From 619df78d1a749730be46ac652a3727687798e0fb Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Fri, 13 Nov 2026 20:33:56 +0000 Subject: [PATCH 25/31] fix(types,cost): a stored ingestion-time TimeShift keeps its offset, the chain below it does not (#1207 PR 6) A TimeShift without `@` materialized across evaluations at ingestion time keeps its own rows, which its readers read an offset later, and each refresh reads the newest output of its input. The read-back walk passed up through it as through a query-time shift, so the chain below it kept the offset too: for sum_over_time(m[5m] offset 1h) with the shift stored over a 60 s chain, both kept 3,900,000 ms (3,696,000 B each); the chain needs its newest window and the one being built (112,000 B), 0.448/s less. TimeIndexedRead::of takes the node's lifetime and gives such a shift StoredThrough { offset_ms }: the walk starts at its offset when it is the producer (its retention is still 1 h 5 m) and ends at it with the empty window when it is a reader. Stage 3's retention, the installer's read_back_ms and asap_cost::read_back_ms all read it through this one function. --- crates/cost/src/node/usage/tests.rs | 64 ++++++++++++++++- crates/cost/src/plan/cost.rs | 8 ++- .../src/physical_planner/time_window.rs | 6 +- crates/planner/src/installable/tests.rs | 7 +- crates/stage3-plan-selection/src/retention.rs | 8 +-- .../types/src/plan/materialized_view/views.rs | 1 + crates/types/src/plan/time_window.rs | 70 ++++++++++++++++--- 7 files changed, 142 insertions(+), 22 deletions(-) diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index d4027c350..254f21a17 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -185,7 +185,13 @@ fn a_query_time_scan_reads_what_its_consumers_read_and_keeps_its_retention() { 0 => vec![1], _ => vec![], }, - |_| TimeIndexedRead::of(&instant.payload, ExecutionTiming::QueryTime), + |_| { + TimeIndexedRead::of( + &instant.payload, + ExecutionTiming::QueryTime, + instant.lifetime, + ) + }, |_| 0, true, ); @@ -365,3 +371,59 @@ fn a_view_over_an_ingestion_time_chain_keeps_one_output_per_chain_window() { .unwrap(); assert_eq!(usage.peak_memory_bytes, 16_000 * 66); } + +/// `sum_over_time(m[5m] offset 1h)` with its `TimeShift(1h)` stored at +/// ingestion time over a chain of 60 s windows: the shift keeps its hour +/// (read back 1 h 5 m by `TimeRange(5m)`), and each refresh reads the +/// chain's newest window, so the chain is read back 0 (it keeps its newest +/// window and the one being built), not 1 h 5 m as well. The same walk +/// gives Stage 3's retention and the installer's check. +#[test] +fn a_stored_time_shift_keeps_its_offset_and_the_chain_below_it_does_not() { + let chain = node(Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { + child: 0, + summary_type: FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::PerEntity, + filter: None, + tumbling_window_width_ms: 60_000, + })); + let shift = PhysicalASAPDAGNode { + output_state: ExecutionDataState::INGESTION_ROWS, + lifetime: Lifetime::AcrossEvaluations(StorageMedium::Memory), + ..node(Payload::NonASAP(NonASAPOp::TimeShift { + shift: asap_types::ir::operator::arguments::TimeShift { + offset_ms: 3_600_000, + at: None, + }, + child: 0, + })) + }; + let range = node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(300), + kind: TimeRangeKind::Range, + child: 1, + })); + let nodes = [&chain, &shift, &range]; + let read_back = |producer: usize| { + read_back_ms( + producer, + |id| match id { + 0 => vec![1], + 1 => vec![2], + _ => vec![], + }, + |id| { + TimeIndexedRead::of( + &nodes[id].payload, + nodes[id].output_state.timing, + nodes[id].lifetime, + ) + }, + |_| 0, + false, + ) + }; + assert_eq!(read_back(1), Some(3_900_000)); + assert_eq!(read_back(0), Some(0)); +} diff --git a/crates/cost/src/plan/cost.rs b/crates/cost/src/plan/cost.rs index 3cec75b0d..64a7c655a 100644 --- a/crates/cost/src/plan/cost.rs +++ b/crates/cost/src/plan/cost.rs @@ -411,7 +411,13 @@ fn scan_read_ms( read_back_ms( node.id, |id| consumers_of.get(&id).into_iter().flatten().copied(), - |id| TimeIndexedRead::of(&nodes[&id].payload, nodes[&id].output_state.timing), + |id| { + TimeIndexedRead::of( + &nodes[&id].payload, + nodes[&id].output_state.timing, + nodes[&id].lifetime, + ) + }, |_| 0, true, ) diff --git a/crates/executor/src/physical_planner/time_window.rs b/crates/executor/src/physical_planner/time_window.rs index 52fdc51b9..d18077e7c 100644 --- a/crates/executor/src/physical_planner/time_window.rs +++ b/crates/executor/src/physical_planner/time_window.rs @@ -168,7 +168,11 @@ pub(super) fn window_read_above( windows_read_above( id, |node| consumers.get(&node).into_iter().flatten().copied(), - |node| match TimeIndexedRead::of(&nodes[&node].payload, nodes[&node].output_state.timing) { + |node| match TimeIndexedRead::of( + &nodes[&node].payload, + nodes[&node].output_state.timing, + nodes[&node].lifetime, + ) { TimeIndexedRead::Through { .. } if roots.contains(&node) => TimeIndexedRead::Whole, read => read, }, diff --git a/crates/planner/src/installable/tests.rs b/crates/planner/src/installable/tests.rs index 0258d827c..e545d244f 100644 --- a/crates/planner/src/installable/tests.rs +++ b/crates/planner/src/installable/tests.rs @@ -227,11 +227,12 @@ fn installation_checks_the_retention_stage3_set() { /// A `TimeShift(1h)` view over the chain, read by `TimeRange(1h)`, keeps /// each row an hour before it is read: its retention must cover 2 h, the /// hour its reader reads and the hour of its offset, not only the hour -/// read. (The chain below it is read back 2 h too.) +/// read. Its refresh reads the chain's newest window, so the chain keeps +/// only that (here one 10-minute window), not the 2 h too. #[test] fn a_time_shift_view_keeps_its_offset_more() { let entries = [entry()]; - let shifted = |retention_ms| chain_plan(Some(7_200_000), Some((3_600_000, retention_ms))).0; + let shifted = |retention_ms| chain_plan(Some(600_000), Some((3_600_000, retention_ms))).0; assert!(matches!( installable_plan_of(shifted(3_600_000), &entries), Err(InstallablePlanError::Uninstallable( @@ -247,7 +248,7 @@ fn a_time_shift_view_keeps_its_offset_more() { .iter() .map(|view| view.retention_ms()) .collect::>(), - [7_200_000, 7_200_000] + [600_000, 7_200_000] ); } diff --git a/crates/stage3-plan-selection/src/retention.rs b/crates/stage3-plan-selection/src/retention.rs index b0ced7e1f..bb8bbcb01 100644 --- a/crates/stage3-plan-selection/src/retention.rs +++ b/crates/stage3-plan-selection/src/retention.rs @@ -64,12 +64,8 @@ fn read_back>( producer, readers_of, |v| { - TimeIndexedRead::of( - operator_of(v), - physical.physical_candidates[v.0] - .physical_properties - .timing(), - ) + let properties = physical.physical_candidates[v.0].physical_properties; + TimeIndexedRead::of(operator_of(v), properties.timing(), properties.lifetime()) }, span_ms, matches!( diff --git a/crates/types/src/plan/materialized_view/views.rs b/crates/types/src/plan/materialized_view/views.rs index 72fb1edd0..15e169d42 100644 --- a/crates/types/src/plan/materialized_view/views.rs +++ b/crates/types/src/plan/materialized_view/views.rs @@ -369,6 +369,7 @@ impl<'a> ViewIndex<'a> { |id| match TimeIndexedRead::of( &self.nodes[&id].payload, self.nodes[&id].output_state.timing, + self.nodes[&id].lifetime, ) { TimeIndexedRead::Through { .. } if root(id) => TimeIndexedRead::Whole, read => read, diff --git a/crates/types/src/plan/time_window.rs b/crates/types/src/plan/time_window.rs index 11bfe76c3..61b48ac8a 100644 --- a/crates/types/src/plan/time_window.rs +++ b/crates/types/src/plan/time_window.rs @@ -22,6 +22,7 @@ use crate::ir::operator::asap::ASAPOp; use crate::ir::operator::node::Operator; use crate::ir::operator::non_asap::NonASAPOp; use crate::ir::properties::execution_data_state::ExecutionTiming; +use crate::stages::Lifetime; /// A time window relative to an evaluation time t: (t - end_offset_ms - width_ms, t - end_offset_ms]. /// @@ -69,6 +70,12 @@ pub enum TimeIndexedRead { /// without `@`, or, with offset 0, a chain built at query time, which /// builds at each evaluation the windows its readers read. Through { offset_ms: i64 }, + /// A `TimeShift` without `@` materialized across evaluations at + /// ingestion time: it keeps its rows, which its own readers read + /// `offset_ms` later (as through `Through`), and each refresh reads the + /// newest output of its input, so its input is read at its evaluation + /// time and does not keep the offset too. + StoredThrough { offset_ms: i64 }, /// The window ending at its evaluation time, `width_ms` wide: a /// `TimeRange`'s range, or a chain built at ingestion time's tumbling /// window (it builds its newest window from it). @@ -81,13 +88,22 @@ pub enum TimeIndexedRead { } impl TimeIndexedRead { - /// How `operator`, run at `timing`, reads its input. + /// How `operator`, run at `timing` with `lifetime`, reads its input. /// /// Example: `TimeRange(5m)` is `Window { width_ms: 300_000 }`; a - /// query-time chain `Through { offset_ms: 0 }`. - pub fn of(operator: &Operator, timing: ExecutionTiming) -> Self { + /// query-time chain `Through { offset_ms: 0 }`; `TimeShift(1h)` at + /// query time `Through { offset_ms: 3_600_000 }`, stored at ingestion + /// time `StoredThrough { offset_ms: 3_600_000 }`. + pub fn of(operator: &Operator, timing: ExecutionTiming, lifetime: Lifetime) -> Self { match operator { Operator::NonASAP(NonASAPOp::TimeShift { shift, .. }) => match shift.at { + None if timing == ExecutionTiming::IngestionTime + && matches!(lifetime, Lifetime::AcrossEvaluations(_)) => + { + Self::StoredThrough { + offset_ms: shift.offset_ms, + } + } None => Self::Through { offset_ms: shift.offset_ms, }, @@ -115,10 +131,11 @@ impl TimeIndexedRead { /// node's readers, `read_of` how a reader reads), each reader that ends a /// path with the window it reads back from the evaluation time, the /// offsets of the shifts walked through included, starting with the -/// producer's own when it is a `Through` reader itself (a stored -/// `TimeShift(1h)` keeps each row an hour before its readers read it): a -/// `Window` reader its window, an `AtEvaluationTime` reader the empty -/// window at its offset. +/// producer's own when it is a `Through` or `StoredThrough` reader itself +/// (a stored `TimeShift(1h)` keeps each row an hour before its readers +/// read it): a `Window` reader its window, an `AtEvaluationTime` or +/// `StoredThrough` reader the empty window at its offset (a stored shift +/// keeps the offset in its own rows, not its input's). /// `None`, unbounded, when one reads it `Whole`, an `AtEvaluationTime` /// reader reads it and `at_evaluation_time_reads_whole` (raw data, which /// is not stored by evaluation time), or a path's offsets are negative or @@ -126,7 +143,8 @@ impl TimeIndexedRead { /// /// Example: a `Scan` read by `TimeRange(5m)` and by `TimeShift(1h) -> /// TimeRange(5m)` is read through `{0, 5m}` and `{1h, 5m}`; the -/// `TimeShift(1h)` itself through `{1h, 5m}`. +/// `TimeShift(1h)` itself through `{1h, 5m}`. A chain read by a stored +/// `TimeShift(1h)` is read through `{0, 0}`, its newest window. pub fn windows_read_above>( producer: N, readers_of: impl Fn(N) -> R, @@ -136,7 +154,9 @@ pub fn windows_read_above>( let mut windows = Vec::new(); // (node whose readers are next, the offsets walked through below them) let own_offset_ms = match read_of(producer) { - TimeIndexedRead::Through { offset_ms } => offset_ms, + TimeIndexedRead::Through { offset_ms } | TimeIndexedRead::StoredThrough { offset_ms } => { + offset_ms + } _ => 0, }; let mut pending: Vec<(N, i64)> = vec![(producer, own_offset_ms)]; @@ -154,6 +174,7 @@ pub fn windows_read_above>( continue; } TimeIndexedRead::Window { width_ms } => width_ms, + TimeIndexedRead::StoredThrough { .. } => 0, TimeIndexedRead::AtEvaluationTime if !at_evaluation_time_reads_whole => 0, TimeIndexedRead::AtEvaluationTime | TimeIndexedRead::Whole => return None, }; @@ -272,7 +293,8 @@ mod tests { /// `TimeRange(5m)`; node 4, a reader at the evaluation time, reads 0 /// too. The scan is read through `{0, 5m}` and `{1h, 5m}`, the shift /// through `{1h, 5m}`; the reader at the evaluation time reads raw data - /// whole. + /// whole. A shift stored at ingestion time is read through `{1h, 5m}` + /// too, and reads the scan through `{0, 0}`. #[test] fn readers_read_a_producer_through_the_shifts_above_it() { let readers = |node: u32| -> Vec { @@ -316,6 +338,34 @@ mod tests { ) ] ); + // Stored at ingestion time, the shift keeps the hour itself: it is + // still read through `{1h, 5m}`, and the producer below it only + // through its newest output, `{0, 0}`. + let stored = |node: u32| match node { + 2 => TimeIndexedRead::StoredThrough { + offset_ms: 3_600_000, + }, + node => read_of(node), + }; + assert_eq!( + windows_read_above(2, readers, stored, false), + windows_read_above(2, readers, read_of, false) + ); + let mut windows = windows_read_above(0, readers, stored, true).unwrap(); + windows.sort(); + assert_eq!( + windows, + vec![ + (1, FIVE_MINUTES), + ( + 2, + TimeWindow { + end_offset_ms: 0, + width_ms: 0 + } + ) + ] + ); let with_reader_at_evaluation = |node: u32| match node { 0 => vec![1, 4], _ => vec![], From a02a3f209b56467e404d4cf157251a7c921d5b09 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Fri, 13 Nov 2026 20:35:29 +0000 Subject: [PATCH 26/31] test(integration): raw data on disk costing less than the solver tolerance still steers selection (#1207 PR 6) With a disk price of 3.1e-17 per byte-second, keeping raw data on disk costs about 2e-9 per second, below the 1e-9 under which HiGHS drops a matrix coefficient. With the objective scale on r_s's coefficient, the unscaled raw-data rows lost those coefficients and Stage 3 selected the plan it selects when disk is free: 4.001e-9/s for sum_over_time(m[5m] offset 30m) + sum_over_time(m[5m]) every minute and count_over_time(m[2m]) every hour. With the scale in the row it selects the 2.542e-9/s plan. The test fails on the old placement. --- .../tests/stage3_objective.rs | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/crates/integration-tests/tests/stage3_objective.rs b/crates/integration-tests/tests/stage3_objective.rs index f2909c7cb..81e58ace6 100644 --- a/crates/integration-tests/tests/stage3_objective.rs +++ b/crates/integration-tests/tests/stage3_objective.rs @@ -180,3 +180,53 @@ fn offset_windows_without_raw_data_select_installable_plans_costed_for_their_ret }); } } + +/// Unit costs with a disk price like a cloud block volume's (3.1e-17 per +/// byte-second): what keeping raw data on disk costs per second is below +/// 1e-9, where the solver drops a constraint coefficient as zero. +fn small_disk_costs() -> CostInputs { + CostInputs { + cost_model: CostModel::UsageOverTime(ResourceUnitCost { + cost_per_cpu_op: 1.1244e-14, + cost_per_scanned_byte: 1.1244e-15, + cost_per_memory_byte_second: 1.150183379650116e-15, + cost_per_disk_byte_second: 3.1e-17, + ..ResourceUnitCost::illustrative() + }), + ..CostInputs::default() + } +} + +/// `sum_over_time(m[5m] offset 30m)` and `sum_over_time(m[5m])` every +/// minute and `count_over_time(m[2m])` every hour, raw data kept, disk +/// charged at 3.1e-17 per byte-second: raw data on disk costs about 2e-9 +/// per second. Its row carries the objective's scale, so the solver keeps +/// it, and the plan selected costs less than the one selected when disk +/// is free, costed at the disk price. With the scale on the raw-data +/// variable's objective coefficient instead, the solver dropped the +/// unscaled coefficients and selected that plan, 57% dearer. +#[test] +fn raw_data_on_disk_costing_less_than_the_solver_tolerance_still_steers_selection() { + let w = workload(&[ + ("sum_over_time(m[5m] offset 30m)", 60_000, ""), + ("sum_over_time(m[5m])", 60_000, ""), + ("count_over_time(m[2m])", 3_600_000, ""), + ]); + let settings = small_disk_costs(); + let (_, (objective, plan_cost)) = selected_plan_objective_and_plan_cost(&w, &settings, true); + assert_equal("small disk price", (objective, plan_cost)); + let mut free_disk = settings.clone(); + if let CostModel::UsageOverTime(unit_costs) = &mut free_disk.cost_model { + unit_costs.cost_per_disk_byte_second = 0.0; + } + let (ignoring_disk, _) = selected_plan_objective_and_plan_cost(&w, &free_disk, true); + let entries: Vec<_> = w.query_workload.entries().collect(); + let data = w.data_workload.clone().unwrap(); + let (ignoring_disk_cost, _) = + asap_cost::plan_cost(&ignoring_disk, &entries, &data, &settings).unwrap(); + let ignoring_disk_cost = ignoring_disk_cost.cost_per_second.unwrap(); + assert!( + plan_cost < ignoring_disk_cost * (1.0 - 1e-6), + "selected {plan_cost} is not cheaper than {ignoring_disk_cost}, the plan selected ignoring disk" + ); +} From dcfd69420733787e75bfadc24dac05db83a80383 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Fri, 13 Nov 2026 20:50:11 +0000 Subject: [PATCH 27/31] fix(executor): a stored TimeShift's own refresh stays refused (#1207 PR 6) With StoredThrough, the executor's slot walk no longer mapped a stored TimeShift at a compile root to Whole, so the refresh of a stored shift over a chain compiled, read the chain's newest window, and the forced sum_over_time(m[5m] offset 5m) plan answered nothing (0 of 12 series). How a stored shift stamps the rows it stores is undefined until the executor supports such a refresh, so it is refused as before: a Through or StoredThrough reader at a compile root reads its input whole. --- crates/executor/src/physical_planner/time_window.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/executor/src/physical_planner/time_window.rs b/crates/executor/src/physical_planner/time_window.rs index d18077e7c..c42152317 100644 --- a/crates/executor/src/physical_planner/time_window.rs +++ b/crates/executor/src/physical_planner/time_window.rs @@ -150,8 +150,8 @@ pub(super) fn time_range_window( /// 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. +/// it. `None` when `id` or a shift above it (stored or not) is a compile +/// root, another consumer reads it outside a window, or nothing reads it. /// /// 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 @@ -173,7 +173,14 @@ pub(super) fn window_read_above( nodes[&node].output_state.timing, nodes[&node].lifetime, ) { - TimeIndexedRead::Through { .. } if roots.contains(&node) => TimeIndexedRead::Whole, + // A stored shift's refresh is a compile root too: how it stamps + // the rows it stores is not defined yet, so it reads its input + // whole (refused) rather than the newest window. + TimeIndexedRead::Through { .. } | TimeIndexedRead::StoredThrough { .. } + if roots.contains(&node) => + { + TimeIndexedRead::Whole + } read => read, }, true, From 62339bcf161e22de130526147cfc5977538fc9a2 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Fri, 13 Nov 2026 22:27:31 +0000 Subject: [PATCH 28/31] fix(types,executor): a stored TimeShift over raw data reads it unbounded (#1207 PR 6) As a reader, TimeIndexedRead::StoredThrough reads its input at its evaluation time, as AtEvaluationTime does, but gave {0, 0} even over raw data, which has no newest output. Forced sum_over_time(m[5m] offset 1h) with raw data kept and the shift stored over a stored Scan read the Scan back 0 and costed the plan 0.0198/s against 60,855/s with the Scan kept for the offset and range; the installer refuses that plan (ViewReadsUnwindowedSource). The walk now reads raw data unbounded under it, so such a Scan is not costable. The executor's walk reads every consumer outside a window unbounded, so a stored shift's refresh reads a chain slot unbounded and is refused without its own arm; the arm is gone and the doc says what happens over a chain (refused) and over a raw Scan (every row is read). New executor test: the chain under a stored TimeShift(5m) is read unbounded, as the root or not. The types walk test covers `of` and the raw-data case; the cost test that repeated it is removed. --- crates/cost/src/node/usage/tests.rs | 56 ------------------- crates/executor/src/physical_planner/tests.rs | 51 +++++++++++++++++ .../src/physical_planner/time_window.rs | 18 +++--- crates/types/src/plan/time_window.rs | 52 ++++++++++++----- 4 files changed, 97 insertions(+), 80 deletions(-) diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index 254f21a17..1a23c79fc 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -371,59 +371,3 @@ fn a_view_over_an_ingestion_time_chain_keeps_one_output_per_chain_window() { .unwrap(); assert_eq!(usage.peak_memory_bytes, 16_000 * 66); } - -/// `sum_over_time(m[5m] offset 1h)` with its `TimeShift(1h)` stored at -/// ingestion time over a chain of 60 s windows: the shift keeps its hour -/// (read back 1 h 5 m by `TimeRange(5m)`), and each refresh reads the -/// chain's newest window, so the chain is read back 0 (it keeps its newest -/// window and the one being built), not 1 h 5 m as well. The same walk -/// gives Stage 3's retention and the installer's check. -#[test] -fn a_stored_time_shift_keeps_its_offset_and_the_chain_below_it_does_not() { - let chain = node(Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { - child: 0, - summary_type: FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum), - input: SummaryUpdate::column(ColumnRef::SampleValue), - reduction: Reduction::PerEntity, - filter: None, - tumbling_window_width_ms: 60_000, - })); - let shift = PhysicalASAPDAGNode { - output_state: ExecutionDataState::INGESTION_ROWS, - lifetime: Lifetime::AcrossEvaluations(StorageMedium::Memory), - ..node(Payload::NonASAP(NonASAPOp::TimeShift { - shift: asap_types::ir::operator::arguments::TimeShift { - offset_ms: 3_600_000, - at: None, - }, - child: 0, - })) - }; - let range = node(Payload::NonASAP(NonASAPOp::TimeRange { - range: Duration::from_secs(300), - kind: TimeRangeKind::Range, - child: 1, - })); - let nodes = [&chain, &shift, &range]; - let read_back = |producer: usize| { - read_back_ms( - producer, - |id| match id { - 0 => vec![1], - 1 => vec![2], - _ => vec![], - }, - |id| { - TimeIndexedRead::of( - &nodes[id].payload, - nodes[id].output_state.timing, - nodes[id].lifetime, - ) - }, - |_| 0, - false, - ) - }; - assert_eq!(read_back(1), Some(3_900_000)); - assert_eq!(read_back(0), Some(0)); -} diff --git a/crates/executor/src/physical_planner/tests.rs b/crates/executor/src/physical_planner/tests.rs index 58f45534a..e9435fcee 100644 --- a/crates/executor/src/physical_planner/tests.rs +++ b/crates/executor/src/physical_planner/tests.rs @@ -17,3 +17,54 @@ fn an_empty_dag_is_invalid() { Err(Error::Invalid(_)) )); } + +/// The refresh of a `TimeShift(5m)` materialized at ingestion time over an +/// ingestion-time chain, the shift its compile root: the chain slot is read +/// unbounded, so the refresh is refused ("read unbounded"), whether the +/// shift is the root or another node reads it. How a stored shift stamps +/// its rows is not defined in the executor yet. +#[test] +fn a_stored_time_shift_over_a_chain_reads_the_chain_unbounded() { + use planner_types::ir::operator::Reduction as IrReduction; + use planner_types::ir::properties::ExecutionDataState; + use planner_types::ir::schema::Schema; + use planner_types::stages::{Lifetime, StorageMedium}; + use planner_types::summary_type::{ExactKind, ExactParams, SummaryUpdate}; + let stored = |id: PhysicalASAPNodeId, payload: Payload| PhysicalASAPDAGNode { + id, + payload, + output_state: ExecutionDataState::INGESTION_ROWS, + lifetime: Lifetime::AcrossEvaluations(StorageMedium::Memory), + parameters: None, + output_schema: Schema::default(), + retention_ms: None, + selected_physical_candidate: None, + view_coverage: None, + }; + let chain = stored( + 0, + Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { + child: 0, + summary_type: FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: IrReduction::PerEntity, + filter: None, + tumbling_window_width_ms: 60_000, + }), + ); + let shift = stored( + 1, + Payload::NonASAP(NonASAPOp::TimeShift { + shift: planner_types::ir::operator::arguments::TimeShift { + offset_ms: 300_000, + at: None, + }, + child: 0, + }), + ); + let nodes: BTreeMap = [(0, &chain), (1, &shift)].into(); + let consumers: BTreeMap> = [(0, vec![1])].into(); + for roots in [BTreeSet::from([1]), BTreeSet::new()] { + assert_eq!(window_read_above(0, &roots, &consumers, &nodes), None); + } +} diff --git a/crates/executor/src/physical_planner/time_window.rs b/crates/executor/src/physical_planner/time_window.rs index c42152317..0c99be246 100644 --- a/crates/executor/src/physical_planner/time_window.rs +++ b/crates/executor/src/physical_planner/time_window.rs @@ -150,8 +150,13 @@ pub(super) fn time_range_window( /// 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 (stored or not) is a compile -/// root, another consumer reads it outside a window, or nothing reads it. +/// 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. /// /// 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 @@ -173,14 +178,7 @@ pub(super) fn window_read_above( nodes[&node].output_state.timing, nodes[&node].lifetime, ) { - // A stored shift's refresh is a compile root too: how it stamps - // the rows it stores is not defined yet, so it reads its input - // whole (refused) rather than the newest window. - TimeIndexedRead::Through { .. } | TimeIndexedRead::StoredThrough { .. } - if roots.contains(&node) => - { - TimeIndexedRead::Whole - } + TimeIndexedRead::Through { .. } if roots.contains(&node) => TimeIndexedRead::Whole, read => read, }, true, diff --git a/crates/types/src/plan/time_window.rs b/crates/types/src/plan/time_window.rs index 61b48ac8a..60fd0babf 100644 --- a/crates/types/src/plan/time_window.rs +++ b/crates/types/src/plan/time_window.rs @@ -136,10 +136,10 @@ impl TimeIndexedRead { /// read it): a `Window` reader its window, an `AtEvaluationTime` or /// `StoredThrough` reader the empty window at its offset (a stored shift /// keeps the offset in its own rows, not its input's). -/// `None`, unbounded, when one reads it `Whole`, an `AtEvaluationTime` -/// reader reads it and `at_evaluation_time_reads_whole` (raw data, which -/// is not stored by evaluation time), or a path's offsets are negative or -/// overflow. +/// `None`, unbounded, when one reads it `Whole`, an `AtEvaluationTime` or +/// `StoredThrough` reader reads it and `at_evaluation_time_reads_whole` +/// (raw data, which is not stored by evaluation time, so has no newest +/// output), or a path's offsets are negative or overflow. /// /// Example: a `Scan` read by `TimeRange(5m)` and by `TimeShift(1h) -> /// TimeRange(5m)` is read through `{0, 5m}` and `{1h, 5m}`; the @@ -174,9 +174,14 @@ pub fn windows_read_above>( continue; } TimeIndexedRead::Window { width_ms } => width_ms, - TimeIndexedRead::StoredThrough { .. } => 0, - TimeIndexedRead::AtEvaluationTime if !at_evaluation_time_reads_whole => 0, - TimeIndexedRead::AtEvaluationTime | TimeIndexedRead::Whole => return None, + TimeIndexedRead::AtEvaluationTime | TimeIndexedRead::StoredThrough { .. } + if !at_evaluation_time_reads_whole => + { + 0 + } + TimeIndexedRead::AtEvaluationTime + | TimeIndexedRead::StoredThrough { .. } + | TimeIndexedRead::Whole => return None, }; let end_offset_ms = u64::try_from(offset_ms).ok()?; windows.push(( @@ -293,8 +298,9 @@ mod tests { /// `TimeRange(5m)`; node 4, a reader at the evaluation time, reads 0 /// too. The scan is read through `{0, 5m}` and `{1h, 5m}`, the shift /// through `{1h, 5m}`; the reader at the evaluation time reads raw data - /// whole. A shift stored at ingestion time is read through `{1h, 5m}` - /// too, and reads the scan through `{0, 0}`. + /// whole. A shift stored at ingestion time (`TimeIndexedRead::of`) is + /// read through `{1h, 5m}` too, and reads its input through `{0, 0}`, + /// its newest output, or, raw data, unbounded. #[test] fn readers_read_a_producer_through_the_shifts_above_it() { let readers = |node: u32| -> Vec { @@ -339,19 +345,37 @@ mod tests { ] ); // Stored at ingestion time, the shift keeps the hour itself: it is - // still read through `{1h, 5m}`, and the producer below it only - // through its newest output, `{0, 0}`. - let stored = |node: u32| match node { - 2 => TimeIndexedRead::StoredThrough { + // still read through `{1h, 5m}`, and the producer below it (here a + // chain, not raw data) only through its newest output, `{0, 0}`. + let shift = Operator::::NonASAP(NonASAPOp::TimeShift { + shift: crate::ir::operator::arguments::TimeShift { offset_ms: 3_600_000, + at: None, }, + child: 0, + }); + let stored_shift = TimeIndexedRead::of( + &shift, + ExecutionTiming::IngestionTime, + Lifetime::AcrossEvaluations(crate::stages::StorageMedium::Memory), + ); + assert_eq!( + stored_shift, + TimeIndexedRead::StoredThrough { + offset_ms: 3_600_000 + } + ); + let stored = |node: u32| match node { + 2 => stored_shift, node => read_of(node), }; assert_eq!( windows_read_above(2, readers, stored, false), windows_read_above(2, readers, read_of, false) ); - let mut windows = windows_read_above(0, readers, stored, true).unwrap(); + // Over raw data, which has no newest output, it reads unbounded. + assert_eq!(windows_read_above(0, readers, stored, true), None); + let mut windows = windows_read_above(0, readers, stored, false).unwrap(); windows.sort(); assert_eq!( windows, From 2e0e9ff9848c072b1fe0e8cf90459178d6331b72 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Fri, 13 Nov 2026 22:27:32 +0000 Subject: [PATCH 29/31] refactor(types,cost): one PhysicalASAPDAGNode::is_view (#1207 PR 6) The cost's no-retention check and the installer's two view filters each spelled "materialized across evaluations and not a raw-data Scan"; they call is_view now, so they refuse the same nodes by construction. The DAG check for a retention on a node that keeps no rows calls the existing retains_rows. The installer's root guard says why a stored shift at a root is not read whole. The bound's `unshared` alias is inlined. --- crates/cost/src/node/usage/mod.rs | 5 +---- .../src/milp/upper_bound.rs | 3 +-- .../src/plan/materialized_view/raw_data.rs | 10 ++++++++++ .../types/src/plan/materialized_view/views.rs | 20 ++++++++++--------- crates/types/src/plan/physical_asap_dag.rs | 5 +---- 5 files changed, 24 insertions(+), 19 deletions(-) diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index c48a93b47..668681e7c 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -298,10 +298,7 @@ pub fn node_resource_usage_and_cost<'a>( // covers. Without one (a reader reads it unbounded: a negative offset, // an `@` anchor) nothing bounds what it keeps and it cannot be // installed, so it is not costed; a `Scan` keeps raw rows (below). - if matches!(node.lifetime, Lifetime::AcrossEvaluations(_)) - && !matches!(node.payload, Payload::NonASAP(NonASAPOp::Scan { .. })) - && node.retention_ms.is_none() - { + if node.is_view() && node.retention_ms.is_none() { return Err(AnalyticalCostError::MissingOrZero("retention_ms")); } let retained_output_count = node diff --git a/crates/stage3-plan-selection/src/milp/upper_bound.rs b/crates/stage3-plan-selection/src/milp/upper_bound.rs index 85d018eb5..d1c641bd6 100644 --- a/crates/stage3-plan-selection/src/milp/upper_bound.rs +++ b/crates/stage3-plan-selection/src/milp/upper_bound.rs @@ -86,7 +86,6 @@ fn feasible_plan_cost_per_second(inputs: &SelectionInputs<'_>) -> Option { let inputs_of: Vec = (0..count) .map(|v| physical.input_count(PhysicalCandidateId(v))) .collect(); - let unshared = |v: usize| is_unmaterialized_query_time(inputs, v); let mut excluded = vec![false; count]; for _ in 0..REPAIRS { let (tree_cost, chosen) = cheapest_trees(inputs, &into, &inputs_of, &excluded); @@ -116,7 +115,7 @@ fn feasible_plan_cost_per_second(inputs: &SelectionInputs<'_>) -> Option { .candidates .iter() .copied() - .find(|&v| unshared(v) && uses[v] > 1) + .find(|&v| is_unmaterialized_query_time(inputs, v) && uses[v] > 1) { excluded[shared] = true; continue; diff --git a/crates/types/src/plan/materialized_view/raw_data.rs b/crates/types/src/plan/materialized_view/raw_data.rs index 6932b9bcb..4ac4be474 100644 --- a/crates/types/src/plan/materialized_view/raw_data.rs +++ b/crates/types/src/plan/materialized_view/raw_data.rs @@ -86,6 +86,16 @@ impl PhysicalASAPDAGNode { pub fn retains_rows(&self) -> bool { matches!(self.lifetime, Lifetime::AcrossEvaluations(_)) || is_raw_data_scan(self) } + + /// Whether it is a materialized view: materialized across evaluations + /// and not raw data kept for its source (a `Scan`), so it keeps what + /// its `retention_ms` covers. + /// + /// Example: a stored chain or `TimeShift` is a view; a `Scan` kept at + /// ingestion time is not. + pub fn is_view(&self) -> bool { + matches!(self.lifetime, Lifetime::AcrossEvaluations(_)) && !is_raw_data_scan(self) + } } impl ViewIndex<'_> { diff --git a/crates/types/src/plan/materialized_view/views.rs b/crates/types/src/plan/materialized_view/views.rs index 15e169d42..05e00bf2b 100644 --- a/crates/types/src/plan/materialized_view/views.rs +++ b/crates/types/src/plan/materialized_view/views.rs @@ -16,7 +16,6 @@ use crate::ir::operator::non_asap::NonASAPOp; use crate::ir::properties::execution_data_state::ExecutionTiming; use crate::plan::physical_asap_dag::{PhysicalASAPDAG, PhysicalASAPDAGNode, PhysicalASAPNodeId}; use crate::plan::time_window::{self, TimeIndexedRead, TimeWindow}; -use crate::stages::Lifetime; impl PhysicalASAPDAG { /// Every materialized view, in dependency order (each after the views @@ -26,11 +25,11 @@ impl PhysicalASAPDAG { /// covers what the `TimeRange`s and `TimeShift`s above it read back. /// O(views · nodes) for the inputs, plus O(nodes + edges). pub fn materialized_views(&self) -> Result, InstallableDagError> { - if let Some(node) = self.nodes.iter().find(|node| { - matches!(node.lifetime, Lifetime::AcrossEvaluations(_)) - && !is_raw_data_scan(node) - && node.retention_ms.is_none() - }) { + if let Some(node) = self + .nodes + .iter() + .find(|node| node.is_view() && node.retention_ms.is_none()) + { return Err(InstallableDagError::ViewWithoutRetention { node_id: node.id }); } self.validate()?; @@ -286,9 +285,7 @@ impl<'a> ViewIndex<'a> { .dag .nodes .iter() - .filter(|node| { - matches!(node.lifetime, Lifetime::AcrossEvaluations(_)) && !is_raw_data_scan(node) - }) + .filter(|node| node.is_view()) .map(|node| node.id) .collect(); let mut derived = Vec::new(); @@ -371,6 +368,11 @@ impl<'a> ViewIndex<'a> { self.nodes[&id].output_state.timing, self.nodes[&id].lifetime, ) { + // A root's output is read at evaluation time, so a root + // read through is read whole. A materialized shift that is a + // root still reads its input's newest output: its readers do + // not change that, so it is not mapped to `Whole` (the + // executor refuses its refresh over a chain for now). TimeIndexedRead::Through { .. } if root(id) => TimeIndexedRead::Whole, read => read, }, diff --git a/crates/types/src/plan/physical_asap_dag.rs b/crates/types/src/plan/physical_asap_dag.rs index d38329d20..77fe7eb35 100644 --- a/crates/types/src/plan/physical_asap_dag.rs +++ b/crates/types/src/plan/physical_asap_dag.rs @@ -372,10 +372,7 @@ impl PhysicalASAPDAG { }, ); } - if node.retention_ms.is_some() - && !matches!(node.lifetime, Lifetime::AcrossEvaluations(_)) - && !super::materialized_view::is_raw_data_scan(node) - { + if node.retention_ms.is_some() && !node.retains_rows() { return Err(PhysicalASAPDAGValidationError::UnexpectedRetention(node.id)); } if let Operator::ASAP(ASAPOp::SummaryAgg { From 75c29a2f9171a722dfab2095fc958751f04f0007 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Sat, 14 Nov 2026 15:57:55 +0000 Subject: [PATCH 30/31] refactor(types,cost,stage3,executor): time windows in four layers, walks over one plan trait (#1207 PR 6) TimeIndexedRead answered two questions in one enum: which input rows a node reads, and how much earlier its output rows are than those. That is why a stored TimeShift needed its own variant. The module is now four layers: 1. TimeWindow { ends_before_evaluation_ms, length_ms } (was end_offset_ms, width_ms), reaches_back_ms (was read_back_ms), union, bounds_ms. 2. InputTimeAccess { reads: InputRows, shifts_back_ms } with InputRows WhatItsReadersRead, LastWindow { length_ms }, CurrentTimeOnly, AllRows, replacing TimeIndexedRead (Through, StoredThrough, Window, AtEvaluationTime, Whole). A stored TimeShift is CurrentTimeOnly shifting back its offset. 3. TimeIndexedPlan: readers_of, inputs_of, input_time_access, is_raw_data and operator_of, replacing the closures and the at_evaluation_time_reads_whole flag. Implemented by PhysicalASAPDAG (installer and asap-cost), Stage 3's Candidates and the executor's CompileGraph. 4. windows_readers_need, windows_computed_from, shift_back_ms_below (were windows_read_above, windows_below, end_offset_below). Behaviour is unchanged: the same walk rule, the same root rule (a root read through reads all rows) in the installer and the executor, the executor still counting every node as raw data. asap-cost's scan read shares the installer's PhysicalASAPDAG plan, so it now applies the root rule too: a Scan reached only through shifts or query-time chains up to a query root is read whole. No workload of the review sets has such a root (their objectives and plan costs are unchanged). asap_cost::read_back_ms takes the plan. The installer's ViewIndex no longer keeps a consumers map. No snapshot serializes a TimeWindow, so no snapshot changes. --- crates/cost/src/node/usage/tests.rs | 49 +- crates/cost/src/plan/cost.rs | 33 +- crates/cost/src/plan/read_back.rs | 36 +- crates/executor/src/physical_planner/mod.rs | 33 +- crates/executor/src/physical_planner/tests.rs | 9 +- .../src/physical_planner/time_window.rs | 138 +++-- crates/executor/src/sources/mod.rs | 5 +- .../executor/tests/tumbling_window_chain.rs | 4 +- .../tests/materialized_views.rs | 4 +- crates/planner/tests/columnar_batches.rs | 4 +- crates/planner/tests/sql_table_scans.rs | 4 +- crates/stage3-plan-selection/src/retention.rs | 79 ++- .../types/src/plan/materialized_view/mod.rs | 13 +- .../types/src/plan/materialized_view/views.rs | 75 +-- crates/types/src/plan/time_window.rs | 509 +++++++++++------- crates/types/tests/physical_asap_dag.rs | 4 +- 16 files changed, 592 insertions(+), 407 deletions(-) diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index 1a23c79fc..0725bb642 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -5,7 +5,7 @@ use std::time::Duration; use asap_types::ir::properties::ExecutionDataState; use asap_types::ir::scalar::ColumnRef; use asap_types::plan::physical_asap_dag::PhysicalASAPDAGNode; -use asap_types::plan::time_window::TimeIndexedRead; +use asap_types::plan::time_window::{InputTimeAccess, TimeIndexedPlan}; use asap_types::summary_type::{ExactKind, ExactParams, SummaryUpdate}; use super::*; @@ -179,22 +179,37 @@ fn a_query_time_scan_reads_what_its_consumers_read_and_keeps_its_retention() { kind: TimeRangeKind::Range, child: 0, })); - let read_ms = read_back_ms( - 0, - |id| match id { - 0 => vec![1], - _ => vec![], - }, - |_| { - TimeIndexedRead::of( - &instant.payload, - ExecutionTiming::QueryTime, - instant.lifetime, - ) - }, - |_| 0, - true, - ); + // The scan (0) read by the range (1). + struct ScanReadByRange<'a>([&'a PhysicalASAPDAGNode; 2]); + impl TimeIndexedPlan for ScanReadByRange<'_> { + type Node = usize; + type Child = usize; + fn readers_of(&self, node: usize) -> Vec { + if node == 0 { + vec![1] + } else { + vec![] + } + } + fn inputs_of(&self, node: usize) -> Vec { + if node == 1 { + vec![0] + } else { + vec![] + } + } + fn input_time_access(&self, node: usize) -> InputTimeAccess { + let node = self.0[node]; + InputTimeAccess::of(&node.payload, ExecutionTiming::QueryTime, node.lifetime) + } + fn is_raw_data(&self, node: usize) -> bool { + node == 0 + } + fn operator_of(&self, node: usize) -> &Payload { + &self.0[node].payload + } + } + let read_ms = read_back_ms(&ScanReadByRange([&scan, &instant]), 0, |_| 0); assert_eq!(read_ms, Some(1_000)); let usage = node_resource_usage_and_cost(&scan, &[], (1, 1.0), read_ms, &data, &settings).unwrap(); diff --git a/crates/cost/src/plan/cost.rs b/crates/cost/src/plan/cost.rs index 64a7c655a..473aeec90 100644 --- a/crates/cost/src/plan/cost.rs +++ b/crates/cost/src/plan/cost.rs @@ -27,7 +27,6 @@ use asap_types::plan::physical_asap_dag::{ PhysicalASAPDAG, PhysicalASAPDAGNode, PhysicalASAPNodeId, PhysicalASAPOperatorPayload as Payload, }; -use asap_types::plan::time_window::TimeIndexedRead; use asap_types::summary_type::SketchParams; use asap_types::workload::{DataWorkload, QueryWorkloadEntry}; @@ -106,10 +105,8 @@ pub fn plan_cost( let multipliers = computations(dag, entries, settings.non_repeating_query_horizon_seconds) .map_err(|error| (first, error))?; let mut producers_of: HashMap> = HashMap::new(); - let mut consumers_of: HashMap> = HashMap::new(); for e in &dag.edges { producers_of.entry(e.consumer).or_default().push(e.producer); - consumers_of.entry(e.producer).or_default().push(e.consumer); } let mut costed: HashMap> = HashMap::new(); for node in &dag.nodes { @@ -125,7 +122,7 @@ pub fn plan_cost( |id| &nodes[&id].payload, |id| producers_of.get(&id)?.first().copied(), ), - ExecutionTiming::QueryTime => scan_read_ms(node, nodes, &consumers_of), + ExecutionTiming::QueryTime => scan_read_ms(dag, node), }; let node_costed = node_resource_usage_and_cost( node, @@ -396,31 +393,15 @@ pub fn raw_data_disk_cost_per_second(bytes: u64, settings: &CostInputs) -> Optio } } -/// The window a query-time `Scan` `node` reads per execution: the most -/// offset + range its consumers read ([`read_back_ms`], no range-evaluation -/// span), once however many consumers read it. `None` for any other node, -/// or when a consumer reads it whole. -fn scan_read_ms( - node: &PhysicalASAPDAGNode, - nodes: &NodeMap<'_>, - consumers_of: &HashMap>, -) -> Option { +/// The window a query-time `Scan` `node` of `dag` reads per execution: the +/// most offset + range its consumers read ([`read_back_ms`], no +/// range-evaluation span), once however many consumers read it. `None` for +/// any other node, or when a consumer reads it whole. +fn scan_read_ms(dag: &PhysicalASAPDAG, node: &PhysicalASAPDAGNode) -> Option { if !matches!(node.payload, Payload::NonASAP(NonASAPOp::Scan { .. })) { return None; } - read_back_ms( - node.id, - |id| consumers_of.get(&id).into_iter().flatten().copied(), - |id| { - TimeIndexedRead::of( - &nodes[&id].payload, - nodes[&id].output_state.timing, - nodes[&id].lifetime, - ) - }, - |_| 0, - true, - ) + read_back_ms(dag, node.id, |_| 0) } /// The CPU cores a candidate needs at its peak: enough for its diff --git a/crates/cost/src/plan/read_back.rs b/crates/cost/src/plan/read_back.rs index d0009a124..45c5e4a21 100644 --- a/crates/cost/src/plan/read_back.rs +++ b/crates/cost/src/plan/read_back.rs @@ -1,37 +1,35 @@ //! How far back from the evaluation time the readers of a time-indexed //! producer (raw data, a chain, results stored per evaluation time) read its -//! rows, over the shared walk [`asap_types::plan::time_window::windows_read_above`]: -//! used by Stage 3 (over candidates) and [`plan_cost`] (over plan nodes) for -//! retention and for a query-time `Scan`'s read per execution +//! rows, over the shared walk +//! [`asap_types::plan::time_window::windows_readers_need`]: used by Stage 3 +//! (over candidates) and [`plan_cost`] (over plan nodes) for retention and +//! for a query-time `Scan`'s read per execution //! (`docs/design_docs/concepts/memo-groups-properties-requirements.md`, //! section 5, "Cost and retention"). //! //! [`plan_cost`]: crate::plan_cost -use asap_types::plan::time_window::{windows_read_above, TimeIndexedRead}; +use asap_types::plan::time_window::{windows_readers_need, TimeIndexedPlan}; -/// The most any reader of `producer` reads back from the evaluation time -/// (`readers_of` gives a node's readers, `read_of` how a reader reads, -/// [`TimeIndexedRead::of`]): each window's `read_back_ms` plus the -/// `span_ms` of the reader at its end (a range query's span, for -/// retention; 0 for one execution's read). `None`, unbounded, when one -/// reads it whole (an `@` anchor, or a reader at the evaluation time over -/// raw data, `reads_raw_data`). 0 when nothing reads it. +/// The most any reader of `producer` in `plan` reaches back from the +/// evaluation time ([`windows_readers_need`]): each window's +/// `reaches_back_ms` plus the `span_ms` of the reader at its end (a range +/// query's span, for retention; 0 for one execution's read). `None`, +/// unbounded, when one reads it whole (an `@` anchor, or a reader of only +/// the current time over raw data). 0 when nothing reads it. /// /// Example: `sum_over_time(m[5m] offset 1h)` reads `m` back 1h + 5m, /// directly or through a query-time chain of 1-minute windows; through an /// ingestion-time chain of 1-minute windows, `m` is read back 1 minute. A /// stored `TimeShift(1h)` read by `TimeRange(5m)` is read back 1h + 5m. -pub fn read_back_ms>( - producer: N, - readers_of: impl Fn(N) -> R, - read_of: impl Fn(N) -> TimeIndexedRead, - span_ms: impl Fn(N) -> u64, - reads_raw_data: bool, +pub fn read_back_ms( + plan: &P, + producer: P::Node, + span_ms: impl Fn(P::Node) -> u64, ) -> Option { - windows_read_above(producer, readers_of, read_of, reads_raw_data)? + windows_readers_need(plan, producer)? .into_iter() .try_fold(0_u64, |most, (reader, window)| { - Some(most.max(window.read_back_ms().checked_add(span_ms(reader))?)) + Some(most.max(window.reaches_back_ms().checked_add(span_ms(reader))?)) }) } diff --git a/crates/executor/src/physical_planner/mod.rs b/crates/executor/src/physical_planner/mod.rs index 6cf011585..bcc74765e 100644 --- a/crates/executor/src/physical_planner/mod.rs +++ b/crates/executor/src/physical_planner/mod.rs @@ -53,7 +53,7 @@ pub use compiled::CompiledPhysicalDAG; mod time_window; use time_window::{ below_time_shifts, counter_window, first_input, node_below_shifts, time_window_operator, - window_below, window_read_above, + window_below, window_read_above, CompileGraph, }; mod row_values; @@ -167,6 +167,12 @@ fn compile_internal( } } let compile_roots: BTreeSet = roots.iter().copied().collect(); + let graph = CompileGraph { + nodes: &nodes, + dependencies: &dependencies, + consumers: &consumers, + roots: &compile_roots, + }; let mut physical_dag = CompiledPhysicalDAG::new(roots.to_vec()); for id in ordered { let node = nodes[&id]; @@ -179,9 +185,9 @@ fn compile_internal( // the window of data it summarizes. Either is exact, so bounded. let read_window = match node.payload { Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) => { - window_read_above(id, &compile_roots, &consumers, &nodes) + window_read_above(id, &graph) } - _ => window_below(id, &nodes, &dependencies), + _ => window_below(id, &graph), } .ok_or_else(|| invalid(format!("materialized view {id} is read unbounded")))?; physical_dag.add_materialized_view( @@ -217,7 +223,7 @@ fn compile_internal( // The rows its consumers read; a compile root reads them all. let read_window = output .time_index - .and_then(|_| window_read_above(id, &compile_roots, &consumers, &nodes)); + .and_then(|_| window_read_above(id, &graph)); physical_dag.add_raw_data( id, compiled::RawDataSlot { @@ -486,18 +492,19 @@ fn compile_internal( }; let window = if query_time { // The windows its readers read, whole tumbling windows. - let read = window_read_above(id, &compile_roots, &consumers, &nodes) - .ok_or_else(|| { - invalid(format!( - "node {id}: a query-time tumbling-window chain is read \ + 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.end_offset_ms)?, millis(read.width_ms)?); + 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 \ @@ -784,7 +791,7 @@ fn compile_internal( } // A window over a table or a time series (and a tumbling window's // shift under it) runs natively on the time column. - if let Some(operator) = time_window_operator(id, &nodes, &dependencies, &schemas) + if let Some(operator) = time_window_operator(id, &graph, &schemas) .map_err(|error| invalid(format!("node {id}: {error}")))? { physical_dag.add(id, inputs, operator.with_output_schema(output)?)?; diff --git a/crates/executor/src/physical_planner/tests.rs b/crates/executor/src/physical_planner/tests.rs index e9435fcee..fcf065b20 100644 --- a/crates/executor/src/physical_planner/tests.rs +++ b/crates/executor/src/physical_planner/tests.rs @@ -64,7 +64,14 @@ fn a_stored_time_shift_over_a_chain_reads_the_chain_unbounded() { ); let nodes: BTreeMap = [(0, &chain), (1, &shift)].into(); let consumers: BTreeMap> = [(0, vec![1])].into(); + let dependencies: BTreeMap> = [(1, vec![0])].into(); for roots in [BTreeSet::from([1]), BTreeSet::new()] { - assert_eq!(window_read_above(0, &roots, &consumers, &nodes), None); + let graph = CompileGraph { + nodes: &nodes, + dependencies: &dependencies, + consumers: &consumers, + roots: &roots, + }; + assert_eq!(window_read_above(0, &graph), None); } } diff --git a/crates/executor/src/physical_planner/time_window.rs b/crates/executor/src/physical_planner/time_window.rs index 0c99be246..972f5e650 100644 --- a/crates/executor/src/physical_planner/time_window.rs +++ b/crates/executor/src/physical_planner/time_window.rs @@ -2,13 +2,67 @@ //! `TimeShift` over raw data or a tumbling-window chain, and the windows a //! node is read through, above it ([`window_read_above`]) or below it //! ([`window_below`]), over the shared walks of -//! `asap_types::plan::time_window`. +//! `asap_types::plan::time_window` run over the nodes being compiled +//! ([`CompileGraph`]). use super::*; use planner_types::plan::time_window::{ - end_offset_below, windows_below, windows_read_above, TimeIndexedRead, + shift_back_ms_below, windows_computed_from, windows_readers_need, InputRows, InputTimeAccess, + TimeIndexedPlan, }; +/// The nodes one compile reads, as the time walks see them: each node's +/// inputs (`dependencies`), the compiled nodes reading it (`consumers`; +/// a materialized view's input is not compiled, so it has none), and the +/// compile roots. A root reached through a node reading +/// `WhatItsReadersRead` reads `AllRows`. Every node counts as raw data +/// ([`TimeIndexedPlan::is_raw_data`]): the executor reads a slot only +/// through the windows above it, so a consumer reading only its current +/// time reads it unbounded. +pub(super) struct CompileGraph<'a> { + pub(super) nodes: &'a BTreeMap, + pub(super) dependencies: &'a BTreeMap>, + pub(super) consumers: &'a BTreeMap>, + pub(super) roots: &'a BTreeSet, +} + +impl TimeIndexedPlan for CompileGraph<'_> { + type Node = NodeId; + type Child = PhysicalASAPNodeId; + + fn readers_of(&self, node: NodeId) -> Vec { + self.consumers.get(&node).cloned().unwrap_or_default() + } + + fn inputs_of(&self, node: NodeId) -> Vec { + self.dependencies.get(&node).cloned().unwrap_or_default() + } + + 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, + ); + match access.reads { + InputRows::WhatItsReadersRead if self.roots.contains(&node) => InputTimeAccess { + reads: InputRows::AllRows, + shifts_back_ms: 0, + }, + _ => access, + } + } + + fn is_raw_data(&self, _: NodeId) -> bool { + true + } + + fn operator_of(&self, node: NodeId) -> &Payload { + &self.nodes[&node].payload + } +} + /// The native operator of `id` when it is a `TimeRange` or a `TimeShift` /// without an `@` anchor over a `Scan` of a SQL table or a time series, or /// over a tumbling-window chain (below any stacked `TimeShift`s); `None` @@ -27,10 +81,10 @@ use planner_types::plan::time_window::{ /// the rows through. An instant window over a table is refused. pub(super) fn time_window_operator( id: NodeId, - nodes: &BTreeMap, - dependencies: &BTreeMap>, + graph: &CompileGraph<'_>, input_schemas: &[SchemaRef], ) -> Result, Error> { + let (nodes, dependencies) = (graph.nodes, graph.dependencies); let below = match &nodes[&id].payload { Payload::NonASAP(NonASAPOp::TimeRange { .. }) => first_input(id, dependencies), Payload::NonASAP(NonASAPOp::TimeShift { .. }) => Some(id), @@ -63,11 +117,14 @@ pub(super) fn time_window_operator( .collect(); return Operator::project(input.clone(), columns).map(Some); }; - let Some(window) = time_range_window(id, nodes, dependencies) else { + let Some(window) = time_range_window(id, graph) else { return Ok(None); }; let millis = |ms: u64| i64::try_from(ms).map_err(|_| invalid("a time window overflows Int64")); - let (end_offset_ms, width_ms) = (millis(window.end_offset_ms)?, millis(window.width_ms)?); + let (end_offset_ms, width_ms) = ( + millis(window.ends_before_evaluation_ms)?, + millis(window.length_ms)?, + ); let time = input .time_index .ok_or_else(|| invalid("a time range needs its input's time column"))?; @@ -122,29 +179,22 @@ pub(super) fn time_window_operator( } /// The window the `TimeRange` `id` reads, its range over the `TimeShift`s -/// right below it ([`end_offset_below`]): `(T - s - w, T - s]` +/// right below it ([`shift_back_ms_below`]): `(T - s - w, T - s]` /// at evaluation time `T`. `None` when `id` is not a `TimeRange`, or a /// shift below it has an `@` anchor or the offsets are negative. -pub(super) fn time_range_window( - id: NodeId, - nodes: &BTreeMap, - dependencies: &BTreeMap>, -) -> Option { - let Payload::NonASAP(NonASAPOp::TimeRange { range, .. }) = &nodes.get(&id)?.payload else { +pub(super) fn time_range_window(id: NodeId, graph: &CompileGraph<'_>) -> Option { + let Payload::NonASAP(NonASAPOp::TimeRange { range, .. }) = &graph.nodes.get(&id)?.payload + else { return None; }; Some(TimeWindow { - end_offset_ms: end_offset_below( - id, - |node| first_input(node, dependencies), - |node| &nodes[&node].payload, - )?, - width_ms: u64::try_from(range.as_millis()).ok()?, + ends_before_evaluation_ms: shift_back_ms_below(graph, id)?, + length_ms: u64::try_from(range.as_millis()).ok()?, }) } /// The window back from the evaluation time that the compiled consumers of -/// `id` read ([`windows_read_above`]): through the +/// `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 @@ -161,50 +211,24 @@ pub(super) fn time_range_window( /// 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 /// `{0, 6m}`. -pub(super) fn window_read_above( - id: NodeId, - roots: &BTreeSet, - consumers: &BTreeMap>, - nodes: &BTreeMap, -) -> Option { - if roots.contains(&id) { +pub(super) fn window_read_above(id: NodeId, graph: &CompileGraph<'_>) -> Option { + if graph.roots.contains(&id) { return None; } - windows_read_above( - id, - |node| consumers.get(&node).into_iter().flatten().copied(), - |node| match TimeIndexedRead::of( - &nodes[&node].payload, - nodes[&node].output_state.timing, - nodes[&node].lifetime, - ) { - TimeIndexedRead::Through { .. } if roots.contains(&node) => TimeIndexedRead::Whole, - read => read, - }, - true, - )? - .into_iter() - .map(|(_, window)| window) - .reduce(TimeWindow::union) + windows_readers_need(graph, id)? + .into_iter() + .map(|(_, window)| window) + .reduce(TimeWindow::union) } /// The window back from the evaluation time of the data `id`'s state /// summarizes: the smallest window holding every window below it -/// ([`windows_below`]). `None` when none is below it, or one +/// ([`windows_computed_from`]). `None` when none is below it, or one /// is read through an `@` anchor or a negative offset. -pub(super) fn window_below( - id: NodeId, - nodes: &BTreeMap, - dependencies: &BTreeMap>, -) -> Option { - windows_below( - id, - |node| dependencies.get(&node).into_iter().flatten().copied(), - |node| first_input(node, dependencies), - |node| &nodes[&node].payload, - )? - .into_iter() - .reduce(TimeWindow::union) +pub(super) fn window_below(id: NodeId, graph: &CompileGraph<'_>) -> Option { + windows_computed_from(graph, id)? + .into_iter() + .reduce(TimeWindow::union) } pub(super) fn first_input( diff --git a/crates/executor/src/sources/mod.rs b/crates/executor/src/sources/mod.rs index a03b32d3d..131b8dc0c 100644 --- a/crates/executor/src/sources/mod.rs +++ b/crates/executor/src/sources/mod.rs @@ -181,8 +181,9 @@ impl Scan { // offset. let at_ms = match context.scope { Scope::Query { evaluation_time_ms } => evaluation_time_ms, - Scope::Ingestion { window_end_ms, .. } => window_end_ms - .saturating_add(i64::try_from(read_window.end_offset_ms).unwrap_or(i64::MAX)), + Scope::Ingestion { window_end_ms, .. } => window_end_ms.saturating_add( + i64::try_from(read_window.ends_before_evaluation_ms).unwrap_or(i64::MAX), + ), }; let time_range_ms = read_window.bounds_ms(at_ms); let opening = context.clone(); diff --git a/crates/executor/tests/tumbling_window_chain.rs b/crates/executor/tests/tumbling_window_chain.rs index b18377218..e6c3950de 100644 --- a/crates/executor/tests/tumbling_window_chain.rs +++ b/crates/executor/tests/tumbling_window_chain.rs @@ -222,8 +222,8 @@ fn a_chain_built_at_ingestion_answers_queries_through_its_time_ranges() { dag.refresh_window(chain), planner_types::plan::materialized_view::RefreshWindow::of_window( planner_types::plan::TimeWindow { - end_offset_ms: 0, - width_ms: WIDTH_MS as u64, + ends_before_evaluation_ms: 0, + length_ms: WIDTH_MS as u64, } ) ); diff --git a/crates/integration-tests/tests/materialized_views.rs b/crates/integration-tests/tests/materialized_views.rs index e0c8e384e..b93db1463 100644 --- a/crates/integration-tests/tests/materialized_views.rs +++ b/crates/integration-tests/tests/materialized_views.rs @@ -131,8 +131,8 @@ fn materialized_views_equal_the_stored_outputs_main_listed() { .map(|window| { source_of(window["scan_node_id"].as_u64().unwrap()); TimeWindow { - end_offset_ms: window["end_offset_ms"].as_u64().unwrap(), - width_ms: window["width_ms"].as_u64().unwrap(), + ends_before_evaluation_ms: window["end_offset_ms"].as_u64().unwrap(), + length_ms: window["width_ms"].as_u64().unwrap(), } }) .collect(); diff --git a/crates/planner/tests/columnar_batches.rs b/crates/planner/tests/columnar_batches.rs index e2b376682..2598a088f 100644 --- a/crates/planner/tests/columnar_batches.rs +++ b/crates/planner/tests/columnar_batches.rs @@ -67,8 +67,8 @@ async fn a_tumbling_window_is_built_from_arrow_columns_with_one_label_entry_per_ let newest_window = |node_id| { let refresh = plan.dag.refresh_window(node_id).expect("a bounded window"); asap_planner::TimeWindow { - end_offset_ms: refresh.phase_ms, - width_ms: refresh.width_ms, + ends_before_evaluation_ms: refresh.phase_ms, + length_ms: refresh.width_ms, } }; let output = views diff --git a/crates/planner/tests/sql_table_scans.rs b/crates/planner/tests/sql_table_scans.rs index ee51e7c1a..77d9656cd 100644 --- a/crates/planner/tests/sql_table_scans.rs +++ b/crates/planner/tests/sql_table_scans.rs @@ -272,8 +272,8 @@ fn run_data_plane(plan: &InstallablePlan) -> Vec>> { .refresh_window(view.node_id) .map(|refresh| { asap_planner::TimeWindow { - end_offset_ms: refresh.phase_ms, - width_ms: refresh.width_ms, + ends_before_evaluation_ms: refresh.phase_ms, + length_ms: refresh.width_ms, } .bounds_ms(EVALUATION_MS) }) diff --git a/crates/stage3-plan-selection/src/retention.rs b/crates/stage3-plan-selection/src/retention.rs index bb8bbcb01..1c8cd02ee 100644 --- a/crates/stage3-plan-selection/src/retention.rs +++ b/crates/stage3-plan-selection/src/retention.rs @@ -6,8 +6,8 @@ //! section 5, "Cost and retention"). use asap_types::ir::{NonASAPOp, Operator}; -use asap_types::plan::time_window::TimeIndexedRead; -use asap_types::stages::{PhysicalASAPMemoCandidates, PhysicalCandidateId}; +use asap_types::plan::time_window::{InputTimeAccess, TimeIndexedPlan}; +use asap_types::stages::{MemoGroupId, PhysicalASAPMemoCandidates, PhysicalCandidateId}; use asap_types::workload::QueryWorkloadEntry; /// How far back from the evaluation time `producer`'s rows must be kept @@ -45,32 +45,69 @@ pub(crate) fn scan_read_ms>( read_back(physical, producer, readers_of, |_| 0) } -/// [`asap_cost::read_back_ms`] over candidates, each read as its operator -/// at its timing reads ([`TimeIndexedRead::of`]). +/// [`asap_cost::read_back_ms`] over candidates ([`Candidates`]). fn read_back>( physical: &PhysicalASAPMemoCandidates, producer: PhysicalCandidateId, readers_of: impl Fn(PhysicalCandidateId) -> R, span_ms: impl Fn(PhysicalCandidateId) -> u64, ) -> Option { - let memo = &physical.logical_candidates.memo; - let operator_of = |v: PhysicalCandidateId| { - &memo - .candidate(physical.physical_candidates[v.0].logical_candidate_id) - .expression - .operator + let readers_of = |v| readers_of(v).into_iter().collect(); + let candidates = Candidates { + physical, + readers_of: &readers_of, }; - asap_cost::read_back_ms( - producer, - readers_of, - |v| { - let properties = physical.physical_candidates[v.0].physical_properties; - TimeIndexedRead::of(operator_of(v), properties.timing(), properties.lifetime()) - }, - span_ms, + asap_cost::read_back_ms(&candidates, producer, span_ms) +} + +/// Stage 3's candidates as the time walks see them: the readers a caller +/// gives (those that may read a candidate, or the selected ones), each +/// candidate's admissible producers as its inputs, its operator at its +/// timing and lifetime ([`InputTimeAccess::of`]), and a `Scan` as raw +/// data. +struct Candidates<'a> { + physical: &'a PhysicalASAPMemoCandidates, + readers_of: &'a dyn Fn(PhysicalCandidateId) -> Vec, +} + +impl TimeIndexedPlan for Candidates<'_> { + type Node = PhysicalCandidateId; + type Child = MemoGroupId; + + fn readers_of(&self, v: PhysicalCandidateId) -> Vec { + (self.readers_of)(v) + } + + fn inputs_of(&self, v: PhysicalCandidateId) -> Vec { + (0..self.physical.input_count(v)) + .flat_map(|input| self.physical.admissible_edges_into(v, input)) + .map(|&edge| self.physical.edge(edge).producer_candidate_id) + .collect() + } + + fn input_time_access(&self, v: PhysicalCandidateId) -> InputTimeAccess { + let properties = self.physical.physical_candidates[v.0].physical_properties; + InputTimeAccess::of( + self.operator_of(v), + properties.timing(), + properties.lifetime(), + ) + } + + fn is_raw_data(&self, v: PhysicalCandidateId) -> bool { matches!( - operator_of(producer), + self.operator_of(v), Operator::NonASAP(NonASAPOp::Scan { .. }) - ), - ) + ) + } + + fn operator_of(&self, v: PhysicalCandidateId) -> &Operator { + &self + .physical + .logical_candidates + .memo + .candidate(self.physical.physical_candidates[v.0].logical_candidate_id) + .expression + .operator + } } diff --git a/crates/types/src/plan/materialized_view/mod.rs b/crates/types/src/plan/materialized_view/mod.rs index 70e717a87..a00db1e0a 100644 --- a/crates/types/src/plan/materialized_view/mod.rs +++ b/crates/types/src/plan/materialized_view/mod.rs @@ -160,12 +160,15 @@ pub struct RefreshWindow { } impl RefreshWindow { - /// The refresh window of `window`: `(width_ms, end_offset_ms mod - /// width_ms)`; `None` for an empty window. + /// The refresh window of `window`: `(length_ms, + /// ends_before_evaluation_ms mod length_ms)`; `None` for an empty + /// window. pub fn of_window(window: TimeWindow) -> Option { - let phase_ms = window.end_offset_ms.checked_rem(window.width_ms)?; + let phase_ms = window + .ends_before_evaluation_ms + .checked_rem(window.length_ms)?; Some(RefreshWindow { - width_ms: window.width_ms, + width_ms: window.length_ms, phase_ms, }) } @@ -285,5 +288,3 @@ pub enum InstallableDagError { mod raw_data; mod views; - -pub(crate) use raw_data::is_raw_data_scan; diff --git a/crates/types/src/plan/materialized_view/views.rs b/crates/types/src/plan/materialized_view/views.rs index 05e00bf2b..9dc811b4c 100644 --- a/crates/types/src/plan/materialized_view/views.rs +++ b/crates/types/src/plan/materialized_view/views.rs @@ -7,15 +7,15 @@ use std::collections::{HashMap, HashSet}; use super::{ - is_raw_data_scan, IngestionRefreshGroup, InstallableDagError, MaterializedView, - MaterializedViewInputs, MaterializedViewRefresh, RefreshWindow, + IngestionRefreshGroup, InstallableDagError, MaterializedView, MaterializedViewInputs, + MaterializedViewRefresh, RefreshWindow, }; use crate::ir::operator::asap::ASAPOp; use crate::ir::operator::node::Operator; use crate::ir::operator::non_asap::NonASAPOp; use crate::ir::properties::execution_data_state::ExecutionTiming; use crate::plan::physical_asap_dag::{PhysicalASAPDAG, PhysicalASAPDAGNode, PhysicalASAPNodeId}; -use crate::plan::time_window::{self, TimeIndexedRead, TimeWindow}; +use crate::plan::time_window::{self, TimeWindow}; impl PhysicalASAPDAG { /// Every materialized view, in dependency order (each after the views @@ -155,7 +155,7 @@ impl PhysicalASAPDAG { .into_iter() .map(|node| { let refresh_window = index - .windows_below(node) + .windows_computed_from(node) .and_then(|windows| windows.into_iter().reduce(TimeWindow::union)) .and_then(RefreshWindow::of_window); (node, refresh_window) @@ -164,15 +164,13 @@ impl PhysicalASAPDAG { } } -/// The nodes of a DAG by id, each node's inputs and consumers in edge -/// order, and two facts every raw-data window check reads, computed in one +/// The nodes of a DAG by id, each node's inputs in edge order, and two facts every raw-data window check reads, computed in one /// pass each over the DAG (O(nodes + edges)); it walks down from each /// window to check its shape. pub(super) struct ViewIndex<'a> { pub(super) dag: &'a PhysicalASAPDAG, pub(super) nodes: HashMap, pub(super) inputs: HashMap>, - pub(super) consumers: HashMap>, /// Every node after its inputs, from the DAG's node order. pub(super) children_first: Vec, /// The nodes whose rows come from raw data: a `Scan` is reachable below @@ -199,13 +197,8 @@ impl<'a> ViewIndex<'a> { pub(super) fn new(dag: &'a PhysicalASAPDAG) -> Self { let nodes: HashMap<_, _> = dag.nodes.iter().map(|n| (n.id, n)).collect(); let mut inputs: HashMap> = HashMap::new(); - let mut consumers: HashMap> = HashMap::new(); for edge in &dag.edges { inputs.entry(edge.consumer).or_default().push(edge.producer); - consumers - .entry(edge.producer) - .or_default() - .push(edge.consumer); } // Every node after its inputs: an iterative depth-first postorder. let mut children_first = Vec::with_capacity(dag.nodes.len()); @@ -252,7 +245,6 @@ impl<'a> ViewIndex<'a> { dag, nodes, inputs, - consumers, children_first, reading_raw_data, re_timed_from_above, @@ -297,10 +289,10 @@ impl<'a> ViewIndex<'a> { // An ingestion run builds the windows ending at its end, so // every window the view reads must end at one offset. let mut end_offsets_ms: Vec = self - .windows_below(id) + .windows_computed_from(id) .unwrap_or_default() .into_iter() - .map(|window| window.end_offset_ms) + .map(|window| window.ends_before_evaluation_ms) .collect(); end_offsets_ms.sort_unstable(); end_offsets_ms.dedup(); @@ -334,54 +326,35 @@ impl<'a> ViewIndex<'a> { Ok(derived) } - /// The windows back from the evaluation time that `node` reads - /// ([`time_window::windows_below`]): a chain its own tumbling window; - /// any other node each `TimeRange` below it and each chain below it - /// reached without passing a `TimeRange`. `None` when one is read + /// The windows of input rows `node`'s result is computed from + /// ([`time_window::windows_computed_from`]): a chain its own tumbling + /// window; any other node each `TimeRange` below it and each chain below + /// it reached without passing a `TimeRange`. `None` when one is read /// through an `@` anchor or a negative offset. - fn windows_below(&self, node: PhysicalASAPNodeId) -> Option> { - time_window::windows_below( - node, - |id| self.inputs.get(&id).into_iter().flatten().copied(), - |id| self.only_input(id), - |id| &self.nodes[&id].payload, - ) + fn windows_computed_from(&self, node: PhysicalASAPNodeId) -> Option> { + time_window::windows_computed_from(self.dag, node) } /// How far back from the evaluation time the readers of `node` read it - /// ([`time_window::windows_read_above`]): the most any reads back. - /// `None`, unbounded, when a root or a reader at a time not relative - /// to the evaluation (`@`) reads it, or, for a raw-data `Scan`, a reader - /// other than a window (a `TimeRange` or an ingestion-time chain). + /// ([`time_window::windows_readers_need`]): the most any reaches back. + /// `None`, unbounded, when `node` is a root, a root or a reader at a time + /// not relative to the evaluation (`@`) reads it, or, for a raw-data + /// `Scan`, a reader other than a window (a `TimeRange` or an + /// ingestion-time chain). A materialized shift that is a root still + /// reads its input's newest output: its readers do not change that, so + /// it is not read whole (the executor refuses its refresh over a chain + /// for now). /// /// Example: a `Scan` read by `TimeShift(1m) -> TimeRange(5m)`: 360,000. pub(super) fn read_back_ms(&self, node: PhysicalASAPNodeId) -> Option { - let root = |id: PhysicalASAPNodeId| self.dag.roots.contains(&id); - if root(node) { + if self.dag.roots.contains(&node) { return None; } - let windows = time_window::windows_read_above( - node, - |id| self.consumers.get(&id).into_iter().flatten().copied(), - |id| match TimeIndexedRead::of( - &self.nodes[&id].payload, - self.nodes[&id].output_state.timing, - self.nodes[&id].lifetime, - ) { - // A root's output is read at evaluation time, so a root - // read through is read whole. A materialized shift that is a - // root still reads its input's newest output: its readers do - // not change that, so it is not mapped to `Whole` (the - // executor refuses its refresh over a chain for now). - TimeIndexedRead::Through { .. } if root(id) => TimeIndexedRead::Whole, - read => read, - }, - is_raw_data_scan(self.nodes[&node]), - )?; + let windows = time_window::windows_readers_need(self.dag, node)?; Some( windows .iter() - .map(|(_, window)| window.read_back_ms()) + .map(|(_, window)| window.reaches_back_ms()) .max() .unwrap_or(0), ) diff --git a/crates/types/src/plan/time_window.rs b/crates/types/src/plan/time_window.rs index 60fd0babf..55c30e861 100644 --- a/crates/types/src/plan/time_window.rs +++ b/crates/types/src/plan/time_window.rs @@ -1,20 +1,27 @@ -//! Time windows relative to an evaluation time, and the two walks over a -//! plan that find them, shared by the cost model (retention and a scan's -//! read), Stage 3, the installer's checks and the executor: +//! Time windows relative to an evaluation time, and the walks over a plan +//! that find them, shared by the cost model (retention and a scan's read), +//! Stage 3, the installer's checks and the executor. Four layers: //! -//! - up from a time-indexed producer (raw data, a chain, results stored -//! per evaluation time) through its readers, the window each reads -//! ([`windows_read_above`]); -//! - down from a node, the windows its rows were read through -//! ([`windows_below`], [`end_offset_below`]). -//! -//! The walks are generic over the node type `N`: a plan node, a Stage 3 -//! candidate or an executor node, with closures giving each node's -//! operator, inputs or readers. +//! 1. [`TimeWindow`]: the rows `(t - ends_before_evaluation_ms - +//! length_ms, t - ends_before_evaluation_ms]` at evaluation time t. +//! 2. [`InputTimeAccess`]: how one node reads its time-indexed input, +//! which rows ([`InputRows`]) and how much earlier its output rows are +//! than those ([`InputTimeAccess::shifts_back_ms`]). +//! 3. [`TimeIndexedPlan`]: a plan the walks run over (plan nodes, Stage 3 +//! candidates or executor nodes): each node's readers, inputs, input +//! time access and operator, and which nodes are raw data. +//! 4. The walks: up from a time-indexed producer (raw data, a chain, +//! results stored per evaluation time) through its readers, the window +//! each needs ([`windows_readers_need`]); down from a node, the windows +//! its result is computed from ([`windows_computed_from`], +//! [`shift_back_ms_below`]). //! //! Example: `sum_over_time(m[5m] offset 1m)` reads `m` through -//! `TimeShift(1m) -> TimeRange(5m)`: `TimeWindow { end_offset_ms: 60_000, -//! width_ms: 300_000 }`, `(t - 6m, t - 1m]`, read back 360,000 ms. +//! `TimeShift(1m) -> TimeRange(5m)`. The shift reads what its reader +//! reads, one minute earlier (`WhatItsReadersRead`, `shifts_back_ms` +//! 60,000); the range reads `LastWindow { length_ms: 300_000 }`. So `m` is +//! read through `TimeWindow { ends_before_evaluation_ms: 60_000, +//! length_ms: 300_000 }`, `(t - 6m, t - 1m]`, reaching back 360,000 ms. use serde::{Deserialize, Serialize}; @@ -22,34 +29,40 @@ use crate::ir::operator::asap::ASAPOp; use crate::ir::operator::node::Operator; use crate::ir::operator::non_asap::NonASAPOp; use crate::ir::properties::execution_data_state::ExecutionTiming; +use crate::plan::physical_asap_dag::{PhysicalASAPDAG, PhysicalASAPNodeId}; use crate::stages::Lifetime; -/// A time window relative to an evaluation time t: (t - end_offset_ms - width_ms, t - end_offset_ms]. +/// The rows `(t - ends_before_evaluation_ms - length_ms, t - +/// ends_before_evaluation_ms]` at evaluation time t. /// -/// E.g. a 5m read with offset 1m = `TimeWindow { end_offset_ms: 60_000, -/// width_ms: 300_000 }`; its `read_back_ms` is 360,000. +/// Example: a 5-minute read with offset 1m is `TimeWindow { +/// ends_before_evaluation_ms: 60_000, length_ms: 300_000 }`; it reaches +/// back 360,000 ms. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] pub struct TimeWindow { - pub end_offset_ms: u64, - pub width_ms: u64, + pub ends_before_evaluation_ms: u64, + pub length_ms: u64, } impl TimeWindow { - /// How far back from t the window reaches: `end_offset_ms + width_ms` - /// (saturating). - pub fn read_back_ms(&self) -> u64 { - self.end_offset_ms.saturating_add(self.width_ms) + /// How far back from t the window reaches: `ends_before_evaluation_ms + /// + length_ms` (saturating). + pub fn reaches_back_ms(&self) -> u64 { + self.ends_before_evaluation_ms + .saturating_add(self.length_ms) } /// The smallest window holding both. pub fn union(self, other: Self) -> Self { - let end_offset_ms = self.end_offset_ms.min(other.end_offset_ms); + let ends_before_evaluation_ms = self + .ends_before_evaluation_ms + .min(other.ends_before_evaluation_ms); Self { - end_offset_ms, - width_ms: self - .read_back_ms() - .max(other.read_back_ms()) - .saturating_sub(end_offset_ms), + ends_before_evaluation_ms, + length_ms: self + .reaches_back_ms() + .max(other.reaches_back_ms()) + .saturating_sub(ends_before_evaluation_ms), } } @@ -57,138 +70,221 @@ impl TimeWindow { /// in ms (saturating). pub fn bounds_ms(&self, evaluation_time_ms: i64) -> (i64, i64) { let millis = |ms: u64| i64::try_from(ms).unwrap_or(i64::MAX); - let end = evaluation_time_ms.saturating_sub(millis(self.end_offset_ms)); - (end.saturating_sub(millis(self.width_ms)), end) + let end = evaluation_time_ms.saturating_sub(millis(self.ends_before_evaluation_ms)); + (end.saturating_sub(millis(self.length_ms)), end) } } -/// How a reader of a time-indexed producer reads its rows, relative to the -/// reader's own evaluation time ([`Self::of`]). +/// How one node reads its time-indexed input at its evaluation time t +/// ([`Self::of`]). +/// +/// | Node | `reads` | `shifts_back_ms` | +/// |---|---|---| +/// | query-time `TimeShift(1h)` | `WhatItsReadersRead` | 3,600,000 | +/// | `TimeShift(1h)` stored at ingestion time | `CurrentTimeOnly` | 3,600,000 | +/// | query-time chain | `WhatItsReadersRead` | 0 | +/// | ingestion-time chain of width w | `LastWindow { length_ms: w }` | 0 | +/// | `TimeRange(5m)` | `LastWindow { length_ms: 300_000 }` | 0 | +/// | any other operator | `CurrentTimeOnly` | 0 | +/// | `TimeShift` with `@` | `AllRows` | 0 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct InputTimeAccess { + /// Which input rows it reads. + pub reads: InputRows, + /// How much earlier its output rows are than the rows it read: a + /// `TimeShift`'s offset, else 0. + pub shifts_back_ms: i64, +} + +/// Which input rows a node reads at its evaluation time t +/// ([`InputTimeAccess::reads`]). #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum TimeIndexedRead { - /// Its own readers read the rows `offset_ms` earlier: a `TimeShift` - /// without `@`, or, with offset 0, a chain built at query time, which - /// builds at each evaluation the windows its readers read. - Through { offset_ms: i64 }, - /// A `TimeShift` without `@` materialized across evaluations at - /// ingestion time: it keeps its rows, which its own readers read - /// `offset_ms` later (as through `Through`), and each refresh reads the - /// newest output of its input, so its input is read at its evaluation - /// time and does not keep the offset too. - StoredThrough { offset_ms: i64 }, - /// The window ending at its evaluation time, `width_ms` wide: a - /// `TimeRange`'s range, or a chain built at ingestion time's tumbling - /// window (it builds its newest window from it). - Window { width_ms: u64 }, - /// The rows of its own evaluation time: any other operator. - AtEvaluationTime, - /// Every row: a `TimeShift` anchored with `@`, which reads at a time - /// that is not relative to the evaluation time. - Whole, +pub enum InputRows { + /// Stores nothing itself: it reads exactly what its readers read + /// (shifted back). A query-time `TimeShift` without `@`, or a chain + /// built at query time, which builds the windows its readers read. + WhatItsReadersRead, + /// The window `(t - length_ms, t]`: a `TimeRange`'s range, or an + /// ingestion-time chain's tumbling window (it builds its newest window + /// from it). + LastWindow { length_ms: u64 }, + /// Only the rows at t: any other operator, and a `TimeShift` stored at + /// ingestion time, whose refresh reads its input's newest output. + CurrentTimeOnly, + /// Every row, because the time it reads does not depend on t: a + /// `TimeShift` anchored with `@`. + AllRows, } -impl TimeIndexedRead { +impl InputTimeAccess { /// How `operator`, run at `timing` with `lifetime`, reads its input. /// - /// Example: `TimeRange(5m)` is `Window { width_ms: 300_000 }`; a - /// query-time chain `Through { offset_ms: 0 }`; `TimeShift(1h)` at - /// query time `Through { offset_ms: 3_600_000 }`, stored at ingestion - /// time `StoredThrough { offset_ms: 3_600_000 }`. + /// Example: `TimeRange(5m)` reads `LastWindow { length_ms: 300_000 }`, + /// shifting back 0; `TimeShift(1h)` at query time reads + /// `WhatItsReadersRead`, shifting back 3,600,000, and stored at + /// ingestion time reads `CurrentTimeOnly`, shifting back 3,600,000. pub fn of(operator: &Operator, timing: ExecutionTiming, lifetime: Lifetime) -> Self { + let reads = |reads| Self { + reads, + shifts_back_ms: 0, + }; match operator { Operator::NonASAP(NonASAPOp::TimeShift { shift, .. }) => match shift.at { - None if timing == ExecutionTiming::IngestionTime - && matches!(lifetime, Lifetime::AcrossEvaluations(_)) => - { - Self::StoredThrough { - offset_ms: shift.offset_ms, - } - } - None => Self::Through { - offset_ms: shift.offset_ms, + None => Self { + reads: match timing == ExecutionTiming::IngestionTime + && matches!(lifetime, Lifetime::AcrossEvaluations(_)) + { + true => InputRows::CurrentTimeOnly, + false => InputRows::WhatItsReadersRead, + }, + shifts_back_ms: shift.offset_ms, }, - Some(_) => Self::Whole, - }, - Operator::NonASAP(NonASAPOp::TimeRange { range, .. }) => Self::Window { - width_ms: u64::try_from(range.as_millis()).unwrap_or(u64::MAX), + Some(_) => reads(InputRows::AllRows), }, + Operator::NonASAP(NonASAPOp::TimeRange { range, .. }) => reads(InputRows::LastWindow { + length_ms: u64::try_from(range.as_millis()).unwrap_or(u64::MAX), + }), Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { tumbling_window_width_ms, .. }) => match timing { - ExecutionTiming::IngestionTime => Self::Window { - width_ms: *tumbling_window_width_ms, - }, - ExecutionTiming::QueryTime => Self::Through { offset_ms: 0 }, + ExecutionTiming::IngestionTime => reads(InputRows::LastWindow { + length_ms: *tumbling_window_width_ms, + }), + ExecutionTiming::QueryTime => reads(InputRows::WhatItsReadersRead), + }, + _ => reads(InputRows::CurrentTimeOnly), + } + } +} + +/// A plan the time walks run over: plan nodes, Stage 3 candidates or +/// executor nodes. +pub trait TimeIndexedPlan { + type Node: Copy + Ord; + /// The child type of the operators it holds. + type Child; + /// The nodes that read `node`. + fn readers_of(&self, node: Self::Node) -> Vec; + /// The nodes `node` reads, in input order. + fn inputs_of(&self, node: Self::Node) -> Vec; + /// How `node` reads its time-indexed input ([`InputTimeAccess::of`]). + fn input_time_access(&self, node: Self::Node) -> InputTimeAccess; + /// Raw data is not stored by evaluation time: a `CurrentTimeOnly` + /// read of it reads every row. + fn is_raw_data(&self, node: Self::Node) -> bool; + /// `node`'s operator, which the walks down read: a `TimeRange`'s range, + /// a chain's tumbling window, a `TimeShift`'s offset. + fn operator_of(&self, node: Self::Node) -> &Operator; +} + +/// The plan as installed and costed: its edges give the readers and +/// inputs, each node's operator, timing and lifetime its input time access +/// ([`InputTimeAccess::of`]), and a `Scan` is raw data. A root reached +/// through a node reading `WhatItsReadersRead` reads `AllRows`: the +/// query reads the root's output at the evaluation time, so nothing above +/// bounds what it reads. O(edges) per node asked. +impl TimeIndexedPlan for PhysicalASAPDAG { + type Node = PhysicalASAPNodeId; + type Child = PhysicalASAPNodeId; + + fn readers_of(&self, node: PhysicalASAPNodeId) -> Vec { + let edges = self.edges.iter().filter(|edge| edge.producer == node); + edges.map(|edge| edge.consumer).collect() + } + + fn inputs_of(&self, node: PhysicalASAPNodeId) -> Vec { + let edges = self.edges.iter().filter(|edge| edge.consumer == node); + edges.map(|edge| edge.producer).collect() + } + + fn input_time_access(&self, node: PhysicalASAPNodeId) -> InputTimeAccess { + let plan_node = plan_node(self, node); + let access = InputTimeAccess::of( + &plan_node.payload, + plan_node.output_state.timing, + plan_node.lifetime, + ); + match access.reads { + InputRows::WhatItsReadersRead if self.roots.contains(&node) => InputTimeAccess { + reads: InputRows::AllRows, + shifts_back_ms: 0, }, - _ => Self::AtEvaluationTime, + _ => access, } } + + fn is_raw_data(&self, node: PhysicalASAPNodeId) -> bool { + matches!( + plan_node(self, node).payload, + Operator::NonASAP(NonASAPOp::Scan { .. }) + ) + } + + fn operator_of(&self, node: PhysicalASAPNodeId) -> &Operator { + &plan_node(self, node).payload + } } -/// The windows the readers of the time-indexed `producer` read it through: -/// walking up from it through `Through` readers (`readers_of` gives a -/// node's readers, `read_of` how a reader reads), each reader that ends a -/// path with the window it reads back from the evaluation time, the -/// offsets of the shifts walked through included, starting with the -/// producer's own when it is a `Through` or `StoredThrough` reader itself -/// (a stored `TimeShift(1h)` keeps each row an hour before its readers -/// read it): a `Window` reader its window, an `AtEvaluationTime` or -/// `StoredThrough` reader the empty window at its offset (a stored shift -/// keeps the offset in its own rows, not its input's). -/// `None`, unbounded, when one reads it `Whole`, an `AtEvaluationTime` or -/// `StoredThrough` reader reads it and `at_evaluation_time_reads_whole` -/// (raw data, which is not stored by evaluation time, so has no newest -/// output), or a path's offsets are negative or overflow. +/// The node of `dag` with id `id`. +fn plan_node( + dag: &PhysicalASAPDAG, + id: PhysicalASAPNodeId, +) -> &crate::plan::physical_asap_dag::PhysicalASAPDAGNode { + dag.nodes + .iter() + .find(|node| node.id == id) + .expect("a node of the plan") +} + +/// Up: the window each reader of `producer` needs from its rows, walking +/// up through the readers that read `WhatItsReadersRead`. `None`, +/// unbounded, when one reads `AllRows`, a `CurrentTimeOnly` reader reads +/// raw data ([`TimeIndexedPlan::is_raw_data`]), or a path's offsets are +/// negative or overflow. The retention `producer` needs is the largest +/// [`TimeWindow::reaches_back_ms`]. +/// +/// The walk starts at the producer's own `shifts_back_ms` (a stored +/// `TimeShift(1h)` keeps each row an hour before its readers read it). +/// Each reader that reads `WhatItsReadersRead` adds its `shifts_back_ms` +/// and the walk goes on above it; a `LastWindow` reader ends the path with +/// `{offset, length}`; a `CurrentTimeOnly` reader with `{offset, 0}` (raw +/// data: unbounded). /// /// Example: a `Scan` read by `TimeRange(5m)` and by `TimeShift(1h) -> /// TimeRange(5m)` is read through `{0, 5m}` and `{1h, 5m}`; the -/// `TimeShift(1h)` itself through `{1h, 5m}`. A chain read by a stored -/// `TimeShift(1h)` is read through `{0, 0}`, its newest window. -pub fn windows_read_above>( - producer: N, - readers_of: impl Fn(N) -> R, - read_of: impl Fn(N) -> TimeIndexedRead, - at_evaluation_time_reads_whole: bool, -) -> Option> { +/// `TimeShift(1h)` itself through `{1h, 5m}`. A chain read by a +/// `TimeShift(1h)` stored at ingestion time is read through `{0, 0}`, its +/// newest window. +pub fn windows_readers_need( + plan: &P, + producer: P::Node, +) -> Option> { + let reads_raw_data = plan.is_raw_data(producer); let mut windows = Vec::new(); // (node whose readers are next, the offsets walked through below them) - let own_offset_ms = match read_of(producer) { - TimeIndexedRead::Through { offset_ms } | TimeIndexedRead::StoredThrough { offset_ms } => { - offset_ms - } - _ => 0, - }; - let mut pending: Vec<(N, i64)> = vec![(producer, own_offset_ms)]; + let mut pending = vec![(producer, plan.input_time_access(producer).shifts_back_ms)]; let mut seen = std::collections::BTreeSet::new(); while let Some((node, offset_ms)) = pending.pop() { if !seen.insert((node, offset_ms)) { continue; } - for reader in readers_of(node) { - let width_ms = match read_of(reader) { - TimeIndexedRead::Through { - offset_ms: shift_ms, - } => { - pending.push((reader, offset_ms.checked_add(shift_ms)?)); + for reader in plan.readers_of(node) { + let access = plan.input_time_access(reader); + let length_ms = match access.reads { + InputRows::WhatItsReadersRead => { + pending.push((reader, offset_ms.checked_add(access.shifts_back_ms)?)); continue; } - TimeIndexedRead::Window { width_ms } => width_ms, - TimeIndexedRead::AtEvaluationTime | TimeIndexedRead::StoredThrough { .. } - if !at_evaluation_time_reads_whole => - { - 0 - } - TimeIndexedRead::AtEvaluationTime - | TimeIndexedRead::StoredThrough { .. } - | TimeIndexedRead::Whole => return None, + InputRows::LastWindow { length_ms } => length_ms, + InputRows::CurrentTimeOnly if !reads_raw_data => 0, + InputRows::CurrentTimeOnly | InputRows::AllRows => return None, }; - let end_offset_ms = u64::try_from(offset_ms).ok()?; windows.push(( reader, TimeWindow { - end_offset_ms, - width_ms, + ends_before_evaluation_ms: u64::try_from(offset_ms).ok()?, + length_ms, }, )); } @@ -196,48 +292,43 @@ pub fn windows_read_above>( Some(windows) } -/// How far before its evaluation time `node` reads its input: the sum of -/// the offsets of the `TimeShift`s right below it (`input_of` gives a -/// node's only input, `operator_of` its operator). `None` when one has an -/// `@` anchor, or the sum is negative or overflows. +/// The sum of the offsets of the `TimeShift`s directly below `node` (each +/// the only input of the node above it). `None` when one has an `@` +/// anchor, or the sum is negative or overflows. /// /// Example: `TimeRange(5m)` over `TimeShift(1m)` over `TimeShift(1h)`: /// 3,660,000. -pub fn end_offset_below<'a, N: Copy, C: 'a>( - node: N, - input_of: impl Fn(N) -> Option, - operator_of: impl Fn(N) -> &'a Operator, -) -> Option { - let mut end_offset_ms = 0i64; +pub fn shift_back_ms_below(plan: &P, node: P::Node) -> Option { + let only_input = |node| match plan.inputs_of(node).as_slice() { + [input] => Some(*input), + _ => None, + }; + let mut shift_back_ms = 0i64; let mut current = node; - while let Some(input) = input_of(current) { - let Operator::NonASAP(NonASAPOp::TimeShift { shift, .. }) = operator_of(input) else { + while let Some(input) = only_input(current) { + let Operator::NonASAP(NonASAPOp::TimeShift { shift, .. }) = plan.operator_of(input) else { break; }; if shift.at.is_some() { return None; } - end_offset_ms = end_offset_ms.checked_add(shift.offset_ms)?; + shift_back_ms = shift_back_ms.checked_add(shift.offset_ms)?; current = input; } - u64::try_from(end_offset_ms).ok() + u64::try_from(shift_back_ms).ok() } -/// The windows back from the evaluation time that `node` reads its rows -/// through: each `TimeRange` at or below it, and each -/// `SummaryTumblingWindowChain` at or below it reached without passing -/// one (its tumbling window), each over the shifts right below it -/// ([`end_offset_below`]). `inputs_of` gives a node's inputs and -/// `input_of` its only input. `None` when one is read through an `@` -/// anchor or a negative offset. +/// Down: the windows of input rows that `node`'s result is computed from: +/// each `TimeRange` at or below it, and each `SummaryTumblingWindowChain` +/// at or below it reached without passing one (its tumbling window), each +/// over the shifts directly below it ([`shift_back_ms_below`]). `None` +/// when one is read through an `@` anchor or a negative offset. /// /// Example: a merge of `TimeRange(5m)` over a scan and of `TimeShift(1h) /// -> TimeRange(5m)` over it: `{0, 5m}` and `{1h, 5m}`. -pub fn windows_below<'a, N: Copy + Ord + 'a, C: 'a, I: IntoIterator>( - node: N, - inputs_of: impl Fn(N) -> I, - input_of: impl Fn(N) -> Option, - operator_of: impl Fn(N) -> &'a Operator, +pub fn windows_computed_from( + plan: &P, + node: P::Node, ) -> Option> { let mut windows = Vec::new(); let mut seen = std::collections::BTreeSet::new(); @@ -246,7 +337,7 @@ pub fn windows_below<'a, N: Copy + Ord + 'a, C: 'a, I: IntoIterator>( if !seen.insert(id) { continue; } - let width_ms = match operator_of(id) { + let length_ms = match plan.operator_of(id) { Operator::NonASAP(NonASAPOp::TimeRange { range, .. }) => { u64::try_from(range.as_millis()).ok()? } @@ -255,13 +346,13 @@ pub fn windows_below<'a, N: Copy + Ord + 'a, C: 'a, I: IntoIterator>( .. }) => *tumbling_window_width_ms, _ => { - pending.extend(inputs_of(id)); + pending.extend(plan.inputs_of(id)); continue; } }; windows.push(TimeWindow { - end_offset_ms: end_offset_below(id, &input_of, &operator_of)?, - width_ms, + ends_before_evaluation_ms: shift_back_ms_below(plan, id)?, + length_ms, }); } Some(windows) @@ -272,33 +363,77 @@ mod tests { use super::*; const FIVE_MINUTES: TimeWindow = TimeWindow { - end_offset_ms: 0, - width_ms: 300_000, + ends_before_evaluation_ms: 0, + length_ms: 300_000, }; /// `(t - 6m, t - 1m]` and `(t - 5m, t]` are both held by `(t - 6m, t]`. #[test] fn the_union_of_two_windows_holds_both() { let shifted = TimeWindow { - end_offset_ms: 60_000, - width_ms: 300_000, + ends_before_evaluation_ms: 60_000, + length_ms: 300_000, }; - assert_eq!(shifted.read_back_ms(), 360_000); + assert_eq!(shifted.reaches_back_ms(), 360_000); assert_eq!( shifted.union(FIVE_MINUTES), TimeWindow { - end_offset_ms: 0, - width_ms: 360_000 + ends_before_evaluation_ms: 0, + length_ms: 360_000 } ); assert_eq!(shifted.bounds_ms(1_000_000), (640_000, 940_000)); } + /// A hand-built plan: each node's readers and input time access, and + /// whether node 0 is raw data. The walks up read only these. + struct Plan { + readers: R, + access: A, + raw_data: bool, + operator: Operator, + } + + impl Vec, A: Fn(u32) -> InputTimeAccess> TimeIndexedPlan for Plan { + type Node = u32; + type Child = u32; + fn readers_of(&self, node: u32) -> Vec { + (self.readers)(node) + } + fn inputs_of(&self, _: u32) -> Vec { + Vec::new() + } + fn input_time_access(&self, node: u32) -> InputTimeAccess { + (self.access)(node) + } + fn is_raw_data(&self, node: u32) -> bool { + self.raw_data && node == 0 + } + fn operator_of(&self, _: u32) -> &Operator { + &self.operator + } + } + + /// The plan of `readers` and `access`, node 0 raw data when `raw_data`. + fn plan(readers: R, access: A, raw_data: bool) -> Plan { + Plan { + readers, + access, + raw_data, + // Read only by the walks down, which these tests do not run. + operator: Operator::NonASAP(NonASAPOp::TimeRange { + range: std::time::Duration::from_secs(300), + kind: crate::ir::TimeRangeKind::Range, + child: 0, + }), + } + } + /// Nodes 0 (a scan) ← 1 `TimeRange(5m)`, and 0 ← 2 `TimeShift(1h)` ← 3 /// `TimeRange(5m)`; node 4, a reader at the evaluation time, reads 0 /// too. The scan is read through `{0, 5m}` and `{1h, 5m}`, the shift /// through `{1h, 5m}`; the reader at the evaluation time reads raw data - /// whole. A shift stored at ingestion time (`TimeIndexedRead::of`) is + /// whole. A shift stored at ingestion time ([`InputTimeAccess::of`]) is /// read through `{1h, 5m}` too, and reads its input through `{0, 0}`, /// its newest output, or, raw data, unbounded. #[test] @@ -310,23 +445,28 @@ mod tests { _ => vec![], } }; - let read_of = |node: u32| match node { - 1 | 3 => TimeIndexedRead::Window { width_ms: 300_000 }, - 2 => TimeIndexedRead::Through { - offset_ms: 3_600_000, + let reads = |reads| InputTimeAccess { + reads, + shifts_back_ms: 0, + }; + let access = |node: u32| match node { + 1 | 3 => reads(InputRows::LastWindow { length_ms: 300_000 }), + 2 => InputTimeAccess { + reads: InputRows::WhatItsReadersRead, + shifts_back_ms: 3_600_000, }, - _ => TimeIndexedRead::AtEvaluationTime, + _ => reads(InputRows::CurrentTimeOnly), }; - let mut windows = windows_read_above(0, readers, read_of, true).unwrap(); + let mut windows = windows_readers_need(&plan(readers, access, true), 0).unwrap(); // The shift itself is read an hour back too: what it stores is read // an hour after it is stored. assert_eq!( - windows_read_above(2, readers, read_of, false), + windows_readers_need(&plan(readers, access, false), 2), Some(vec![( 3, TimeWindow { - end_offset_ms: 3_600_000, - width_ms: 300_000 + ends_before_evaluation_ms: 3_600_000, + length_ms: 300_000 } )]) ); @@ -338,8 +478,8 @@ mod tests { ( 3, TimeWindow { - end_offset_ms: 3_600_000, - width_ms: 300_000 + ends_before_evaluation_ms: 3_600_000, + length_ms: 300_000 } ) ] @@ -354,28 +494,29 @@ mod tests { }, child: 0, }); - let stored_shift = TimeIndexedRead::of( + let stored_shift = InputTimeAccess::of( &shift, ExecutionTiming::IngestionTime, Lifetime::AcrossEvaluations(crate::stages::StorageMedium::Memory), ); assert_eq!( stored_shift, - TimeIndexedRead::StoredThrough { - offset_ms: 3_600_000 + InputTimeAccess { + reads: InputRows::CurrentTimeOnly, + shifts_back_ms: 3_600_000 } ); let stored = |node: u32| match node { 2 => stored_shift, - node => read_of(node), + node => access(node), }; assert_eq!( - windows_read_above(2, readers, stored, false), - windows_read_above(2, readers, read_of, false) + windows_readers_need(&plan(readers, stored, false), 2), + windows_readers_need(&plan(readers, access, false), 2) ); // Over raw data, which has no newest output, it reads unbounded. - assert_eq!(windows_read_above(0, readers, stored, true), None); - let mut windows = windows_read_above(0, readers, stored, false).unwrap(); + assert_eq!(windows_readers_need(&plan(readers, stored, true), 0), None); + let mut windows = windows_readers_need(&plan(readers, stored, false), 0).unwrap(); windows.sort(); assert_eq!( windows, @@ -384,8 +525,8 @@ mod tests { ( 2, TimeWindow { - end_offset_ms: 0, - width_ms: 0 + ends_before_evaluation_ms: 0, + length_ms: 0 } ) ] @@ -395,11 +536,11 @@ mod tests { _ => vec![], }; assert_eq!( - windows_read_above(0, with_reader_at_evaluation, read_of, true), + windows_readers_need(&plan(with_reader_at_evaluation, access, true), 0), None ); assert_eq!( - windows_read_above(0, with_reader_at_evaluation, read_of, false) + windows_readers_need(&plan(with_reader_at_evaluation, access, false), 0) .map(|windows| windows.len()), Some(2) ); diff --git a/crates/types/tests/physical_asap_dag.rs b/crates/types/tests/physical_asap_dag.rs index 0e20aa9c6..70c0e1668 100644 --- a/crates/types/tests/physical_asap_dag.rs +++ b/crates/types/tests/physical_asap_dag.rs @@ -263,8 +263,8 @@ fn physical_node_ids_serialize_in_deterministic_binding_order() { #[test] fn a_time_window_ends_its_offset_before_the_evaluation() { let window = asap_types::plan::TimeWindow { - end_offset_ms: 300_000, - width_ms: 600_000, + ends_before_evaluation_ms: 300_000, + length_ms: 600_000, }; assert_eq!(window.bounds_ms(3_600_000), (2_700_000, 3_300_000)); } From ea48d5c89f069eefc3cfbb263e6df128610517c8 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Sat, 14 Nov 2026 17:13:05 +0000 Subject: [PATCH 31/31] refactor(types,stage3,executor): current_time_read_needs_every_row; the root rule is the installer's (#1207 PR 6) TimeIndexedPlan::is_raw_data is current_time_read_needs_every_row: it answers whether a reader of only the current time in fact needs every row, true for raw data. PhysicalASAPDAG and Stage 3's Candidates say so for a Scan; the executor's CompileGraph for every node, since today's executor store reads every slot whole outside a window. PhysicalASAPDAG's input time access is plain InputTimeAccess::of again, so asap-cost reads a plan as Stage 3 reads its candidates, as before the redesign. The installer applies its root rule itself (ViewIndex::read_through_a_root): a node a query root reads through nodes that read what their readers read is read unbounded. New test: a root TimeShift(5m) over a query-time Scan is read back 0 by asap-cost and kept unbounded by the installer (it fails without the installer's check). --- crates/cost/src/node/usage/tests.rs | 2 +- .../src/physical_planner/time_window.rs | 10 ++-- crates/planner/src/installable/tests.rs | 46 +++++++++++++++++++ crates/stage3-plan-selection/src/retention.rs | 6 +-- .../types/src/plan/materialized_view/views.rs | 44 ++++++++++++++---- crates/types/src/plan/time_window.rs | 37 ++++++--------- 6 files changed, 105 insertions(+), 40 deletions(-) diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index 0725bb642..1955c1e6b 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -202,7 +202,7 @@ fn a_query_time_scan_reads_what_its_consumers_read_and_keeps_its_retention() { let node = self.0[node]; InputTimeAccess::of(&node.payload, ExecutionTiming::QueryTime, node.lifetime) } - fn is_raw_data(&self, node: usize) -> bool { + fn current_time_read_needs_every_row(&self, node: usize) -> bool { node == 0 } fn operator_of(&self, node: usize) -> &Payload { diff --git a/crates/executor/src/physical_planner/time_window.rs b/crates/executor/src/physical_planner/time_window.rs index 972f5e650..9826d33be 100644 --- a/crates/executor/src/physical_planner/time_window.rs +++ b/crates/executor/src/physical_planner/time_window.rs @@ -15,10 +15,10 @@ use planner_types::plan::time_window::{ /// inputs (`dependencies`), the compiled nodes reading it (`consumers`; /// a materialized view's input is not compiled, so it has none), and the /// compile roots. A root reached through a node reading -/// `WhatItsReadersRead` reads `AllRows`. Every node counts as raw data -/// ([`TimeIndexedPlan::is_raw_data`]): the executor reads a slot only -/// through the windows above it, so a consumer reading only its current -/// time reads it unbounded. +/// `WhatItsReadersRead` reads `AllRows`. A current-time read of any node +/// needs every row ([`TimeIndexedPlan::current_time_read_needs_every_row`]): +/// today's executor store reads a slot only through the windows above it, +/// and reads every slot whole otherwise (#1291). pub(super) struct CompileGraph<'a> { pub(super) nodes: &'a BTreeMap, pub(super) dependencies: &'a BTreeMap>, @@ -54,7 +54,7 @@ impl TimeIndexedPlan for CompileGraph<'_> { } } - fn is_raw_data(&self, _: NodeId) -> bool { + fn current_time_read_needs_every_row(&self, _: NodeId) -> bool { true } diff --git a/crates/planner/src/installable/tests.rs b/crates/planner/src/installable/tests.rs index e545d244f..5e5025c40 100644 --- a/crates/planner/src/installable/tests.rs +++ b/crates/planner/src/installable/tests.rs @@ -317,3 +317,49 @@ fn unrestricted_summaries_default_to_the_executors_families() { }; assert_eq!(executor_summaries_when_unrestricted(&kll_only), kll_only); } + +/// `TimeShift(5m)` as a query's root over a query-time `Scan`: no frontend +/// builds this shape (a selector's shift sits below its `TimeRange`), so +/// the DAG is built by hand. asap-cost reads the plan as Stage 3 reads its +/// candidates, plainly: the shift reads what its readers read and nothing +/// reads the root, so the scan is read back 0. The installer reads a root +/// reached only through such nodes as reading every row: the scan's raw +/// data is kept unbounded, so the plan installs without a retention on it. +#[test] +fn a_root_time_shift_is_read_whole_by_the_installer_not_by_the_cost() { + use asap_types::ir::operator::Source; + use asap_types::ir::schema::{DataType, Field, Schema}; + use asap_types::ir::{NonASAPOp, Operator, OperatorNode, QueryRoot}; + let node = |operator| OperatorNode::new_shared(operator).unwrap(); + let scan = node(Operator::NonASAP(NonASAPOp::Scan { + source: Source::TimeSeries { + metric: "latency".into(), + }, + predicates: vec![], + schema: Schema::with_time_index( + vec![ + Field::plain("ts", DataType::Timestamp, false), + Field::plain("value", DataType::Float64, false), + ], + 0, + vec![], + ), + })); + let root = node(Operator::NonASAP(NonASAPOp::TimeShift { + shift: asap_types::ir::operator::arguments::TimeShift { + offset_ms: 300_000, + at: None, + }, + child: scan.clone(), + })); + let (dag, nodes) = + asap_types::test_support::forwarders::physical_asap_dag_at_query_time_with_node_ids(&[ + QueryRoot::Operator(root), + ]) + .unwrap(); + let scan_id = nodes.iter().position(|n| Rc::ptr_eq(n, &scan)).unwrap(); + assert_eq!(asap_cost::read_back_ms(&dag, scan_id, |_| 0), Some(0)); + let retention = dag.raw_data_retention().unwrap(); + assert_eq!(retention.len(), 1); + assert_eq!(retention[0].lookback_ms(), None); +} diff --git a/crates/stage3-plan-selection/src/retention.rs b/crates/stage3-plan-selection/src/retention.rs index 1c8cd02ee..0e19a2bd6 100644 --- a/crates/stage3-plan-selection/src/retention.rs +++ b/crates/stage3-plan-selection/src/retention.rs @@ -63,8 +63,8 @@ fn read_back>( /// Stage 3's candidates as the time walks see them: the readers a caller /// gives (those that may read a candidate, or the selected ones), each /// candidate's admissible producers as its inputs, its operator at its -/// timing and lifetime ([`InputTimeAccess::of`]), and a `Scan` as raw -/// data. +/// timing and lifetime ([`InputTimeAccess::of`]), and a current-time read +/// of a `Scan` (raw data) needing every row. struct Candidates<'a> { physical: &'a PhysicalASAPMemoCandidates, readers_of: &'a dyn Fn(PhysicalCandidateId) -> Vec, @@ -94,7 +94,7 @@ impl TimeIndexedPlan for Candidates<'_> { ) } - fn is_raw_data(&self, v: PhysicalCandidateId) -> bool { + fn current_time_read_needs_every_row(&self, v: PhysicalCandidateId) -> bool { matches!( self.operator_of(v), Operator::NonASAP(NonASAPOp::Scan { .. }) diff --git a/crates/types/src/plan/materialized_view/views.rs b/crates/types/src/plan/materialized_view/views.rs index 9dc811b4c..5c765e11b 100644 --- a/crates/types/src/plan/materialized_view/views.rs +++ b/crates/types/src/plan/materialized_view/views.rs @@ -15,7 +15,7 @@ use crate::ir::operator::node::Operator; use crate::ir::operator::non_asap::NonASAPOp; use crate::ir::properties::execution_data_state::ExecutionTiming; use crate::plan::physical_asap_dag::{PhysicalASAPDAG, PhysicalASAPDAGNode, PhysicalASAPNodeId}; -use crate::plan::time_window::{self, TimeWindow}; +use crate::plan::time_window::{self, InputRows, TimeIndexedPlan, TimeWindow}; impl PhysicalASAPDAG { /// Every materialized view, in dependency order (each after the views @@ -337,17 +337,15 @@ impl<'a> ViewIndex<'a> { /// How far back from the evaluation time the readers of `node` read it /// ([`time_window::windows_readers_need`]): the most any reaches back. - /// `None`, unbounded, when `node` is a root, a root or a reader at a time - /// not relative to the evaluation (`@`) reads it, or, for a raw-data - /// `Scan`, a reader other than a window (a `TimeRange` or an - /// ingestion-time chain). A materialized shift that is a root still - /// reads its input's newest output: its readers do not change that, so - /// it is not read whole (the executor refuses its refresh over a chain - /// for now). + /// `None`, unbounded, when `node` is a root, a root reads it through + /// nodes that read what their readers read ([`Self::read_through_a_root`]), + /// a reader at a time not relative to the evaluation (`@`) reads it, or, + /// for a raw-data `Scan`, a reader other than a window (a `TimeRange` or + /// an ingestion-time chain). /// /// Example: a `Scan` read by `TimeShift(1m) -> TimeRange(5m)`: 360,000. pub(super) fn read_back_ms(&self, node: PhysicalASAPNodeId) -> Option { - if self.dag.roots.contains(&node) { + if self.dag.roots.contains(&node) || self.read_through_a_root(node) { return None; } let windows = time_window::windows_readers_need(self.dag, node)?; @@ -360,6 +358,34 @@ impl<'a> ViewIndex<'a> { ) } + /// Whether a query root reads `node` through nodes that each read what + /// their readers read (`WhatItsReadersRead`: a query-time `TimeShift` or + /// chain), the root among them. The query reads the root's output at the + /// evaluation time, so nothing above it bounds what it reads: `node` is + /// read unbounded. A shift stored at ingestion time reads its input's + /// newest output whatever reads it, so a path stops there. + /// + /// Example: a query-time `Scan` read by `TimeShift(5m)`, the query's + /// root: true. + fn read_through_a_root(&self, node: PhysicalASAPNodeId) -> bool { + let mut pending = vec![node]; + let mut seen = HashSet::new(); + while let Some(id) = pending.pop() { + if !seen.insert(id) { + continue; + } + for reader in self.dag.readers_of(id) { + if self.dag.input_time_access(reader).reads == InputRows::WhatItsReadersRead { + if self.dag.roots.contains(&reader) { + return true; + } + pending.push(reader); + } + } + } + false + } + /// What view `node`'s refresh reads: below it, each view (`views`) it /// reaches without passing another, in node order; a chain reading raw /// data none. Checks the shape of each raw-data window it reads (its diff --git a/crates/types/src/plan/time_window.rs b/crates/types/src/plan/time_window.rs index 55c30e861..4100c5463 100644 --- a/crates/types/src/plan/time_window.rs +++ b/crates/types/src/plan/time_window.rs @@ -170,9 +170,10 @@ pub trait TimeIndexedPlan { fn inputs_of(&self, node: Self::Node) -> Vec; /// How `node` reads its time-indexed input ([`InputTimeAccess::of`]). fn input_time_access(&self, node: Self::Node) -> InputTimeAccess; - /// Raw data is not stored by evaluation time: a `CurrentTimeOnly` - /// read of it reads every row. - fn is_raw_data(&self, node: Self::Node) -> bool; + /// Whether a reader of `node` that reads only the rows at its + /// evaluation time (`CurrentTimeOnly`) in fact needs every row of + /// `node`: true for raw data, which is not stored by evaluation time. + fn current_time_read_needs_every_row(&self, node: Self::Node) -> bool; /// `node`'s operator, which the walks down read: a `TimeRange`'s range, /// a chain's tumbling window, a `TimeShift`'s offset. fn operator_of(&self, node: Self::Node) -> &Operator; @@ -180,10 +181,8 @@ pub trait TimeIndexedPlan { /// The plan as installed and costed: its edges give the readers and /// inputs, each node's operator, timing and lifetime its input time access -/// ([`InputTimeAccess::of`]), and a `Scan` is raw data. A root reached -/// through a node reading `WhatItsReadersRead` reads `AllRows`: the -/// query reads the root's output at the evaluation time, so nothing above -/// bounds what it reads. O(edges) per node asked. +/// ([`InputTimeAccess::of`]), and a current-time read of a `Scan` (raw +/// data) needs every row. O(edges) per node asked. impl TimeIndexedPlan for PhysicalASAPDAG { type Node = PhysicalASAPNodeId; type Child = PhysicalASAPNodeId; @@ -200,21 +199,14 @@ impl TimeIndexedPlan for PhysicalASAPDAG { fn input_time_access(&self, node: PhysicalASAPNodeId) -> InputTimeAccess { let plan_node = plan_node(self, node); - let access = InputTimeAccess::of( + InputTimeAccess::of( &plan_node.payload, plan_node.output_state.timing, plan_node.lifetime, - ); - match access.reads { - InputRows::WhatItsReadersRead if self.roots.contains(&node) => InputTimeAccess { - reads: InputRows::AllRows, - shifts_back_ms: 0, - }, - _ => access, - } + ) } - fn is_raw_data(&self, node: PhysicalASAPNodeId) -> bool { + fn current_time_read_needs_every_row(&self, node: PhysicalASAPNodeId) -> bool { matches!( plan_node(self, node).payload, Operator::NonASAP(NonASAPOp::Scan { .. }) @@ -240,8 +232,9 @@ fn plan_node( /// Up: the window each reader of `producer` needs from its rows, walking /// up through the readers that read `WhatItsReadersRead`. `None`, /// unbounded, when one reads `AllRows`, a `CurrentTimeOnly` reader reads -/// raw data ([`TimeIndexedPlan::is_raw_data`]), or a path's offsets are -/// negative or overflow. The retention `producer` needs is the largest +/// a producer whose current-time read needs every row (raw data, +/// [`TimeIndexedPlan::current_time_read_needs_every_row`]), or a path's +/// offsets are negative or overflow. The retention `producer` needs is the largest /// [`TimeWindow::reaches_back_ms`]. /// /// The walk starts at the producer's own `shifts_back_ms` (a stored @@ -260,7 +253,7 @@ pub fn windows_readers_need( plan: &P, producer: P::Node, ) -> Option> { - let reads_raw_data = plan.is_raw_data(producer); + let needs_every_row = plan.current_time_read_needs_every_row(producer); let mut windows = Vec::new(); // (node whose readers are next, the offsets walked through below them) let mut pending = vec![(producer, plan.input_time_access(producer).shifts_back_ms)]; @@ -277,7 +270,7 @@ pub fn windows_readers_need( continue; } InputRows::LastWindow { length_ms } => length_ms, - InputRows::CurrentTimeOnly if !reads_raw_data => 0, + InputRows::CurrentTimeOnly if !needs_every_row => 0, InputRows::CurrentTimeOnly | InputRows::AllRows => return None, }; windows.push(( @@ -406,7 +399,7 @@ mod tests { fn input_time_access(&self, node: u32) -> InputTimeAccess { (self.access)(node) } - fn is_raw_data(&self, node: u32) -> bool { + fn current_time_read_needs_every_row(&self, node: u32) -> bool { self.raw_data && node == 0 } fn operator_of(&self, _: u32) -> &Operator {