From 16f0bb517248a1a31b5e7b11cc83de33eeabbd1a Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 20:44:58 +0000 Subject: [PATCH 1/3] test(cost): node_cost_terms tests: formula CPU per node as named per-unit constants times units (tests only) Adds the twelve tests of node_cost_terms and CostConstant, and the declarations they compile against. The bodies are todo!() until the tests are approved, so eleven tests fail; the JSON-name test passes on the derived serde of the declared enum. --- crates/cost/src/lib.rs | 1 + crates/cost/src/node/cost_terms.rs | 122 ++++ crates/cost/src/node/cost_terms/tests.rs | 122 ++++ crates/cost/src/node/mod.rs | 4 +- crates/cost/src/node/usage/tests.rs | 861 +++++++++++++++++++++++ 5 files changed, 1109 insertions(+), 1 deletion(-) create mode 100644 crates/cost/src/node/cost_terms.rs create mode 100644 crates/cost/src/node/cost_terms/tests.rs diff --git a/crates/cost/src/lib.rs b/crates/cost/src/lib.rs index 4ea46e909..0d60737b0 100644 --- a/crates/cost/src/lib.rs +++ b/crates/cost/src/lib.rs @@ -38,6 +38,7 @@ pub use model::{ CalibrationFit, ClusterLimits, ClusterObjective, CostInputs, CostModel, ResourceSpeed, ResourceUnitCost, COST_SOURCE, MEASURED_COST_SOURCE, MIXED_COST_SOURCE, }; +pub use node::cost_terms::{node_cost_terms, CostConstant, NodeCostTerms}; pub use node::operator_statistics::EdgeStatistics; pub use node::usage::{ node_resource_usage_and_cost, output_interval_ms, reads_one_batch_of, summary_type_name, diff --git a/crates/cost/src/node/cost_terms.rs b/crates/cost/src/node/cost_terms.rs new file mode 100644 index 000000000..afae2c3d2 --- /dev/null +++ b/crates/cost/src/node/cost_terms.rs @@ -0,0 +1,122 @@ +//! One node's formula CPU operations per execution, split into named +//! per-unit constants times the units of work each multiplies, plus the +//! operations no named constant charges. +//! +//! Example: a per-series `max_over_time(latency[5m])` hash aggregate over +//! 300,000 rows of 1,000 series at 1 Hz visits each series' group once per +//! 10 s raw batch: 30,000 units of `OperationsPerGroupPerRawBatch` (170 +//! operations each), beside 300,000 units of `HashAggregateOperationsPerRow` +//! (16), 300,000 of `AggregateMeasureOperationsPerRow` (4) and 1,000 of +//! `OutputRowOperationsPerGroup` (2,500): 13.6 M operations, the formula's +//! charge. A refit of one constant changes the charge by its units times the +//! change. +//! +//! The terms describe the formulas only: they do not depend on +//! `CostInputs::measured_operation_costs` or `frozen_calibration_factor`. + +// Tests-only stage: the bodies below are `todo!()` until the tests are +// approved; the declarations are the interface the tests check. + +use std::collections::BTreeMap; + +use asap_types::plan::physical_asap_dag::PhysicalASAPDAGNode; +use asap_types::workload::DataWorkload; +use serde::{Deserialize, Serialize}; + +use crate::node::analytical::AnalyticalCostError; +use crate::{CostInputs, ProducerOutput}; + +/// A named per-unit constant of the cost formulas: CPU operations per unit +/// of work, e.g. `OperationsPerGroupPerRawBatch` (170 per group visit). +/// JSON: snake_case, e.g. `"operations_per_group_per_raw_batch"`. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum CostConstant { + TimeRangeOperationsPerRow, + OperationsPerGroupPerRawBatch, + ChainOperationsPerArrivingRow, + HashAggregateOperationsPerRow, + HashAggregateOperationsPerFurtherKey, + AggregateMeasureOperationsPerRow, + SortOperationsPerRowPerLevel, + DdsketchUpdateOperationsPerRow, + KllUpdateOperationsPerRow, + ExactUpdateOperationsPerRow, + CounterUpdateOperationsPerRow, + CarriedLabelOperationsPerRowPerLabel, + KmvUpdateOperationsPerRow, + KmvOperationsPerComparedHash, + ExactDistinctOperationsPerRow, + ExactDistinctOperationsPerValueDoubling, + ExactDistinctOperationsPerRowAtFullValueMiss, + ExactDistinctOperationsPerRowAtFullPairMiss, + ExactDistinctOperationsPerInsertedValue, + ExactDistinctOperationsPerInsertedPair, + SketchUpdateOperationsPerCounter, + ExactMergeOperationsPerState, + ExtremumMergeOperationsPerState, + CounterMergeOperationsPerState, + DdsketchMergeOperationsPerState, + KllMergeOperationsPerState, + KllMergeOperationsPerInputByte, + KllMergeOperationsPerMergedByte, + MergeOperationsPerOutputGroup, + SketchMergeOperationsPerByte, + KllEstimateOperationsPerByte, + KllEstimateOperationsPerUnsortedItem, + SketchEstimateOperationsPerGroup, + OutputRowOperationsPerGroup, + SketchChainEmitOperationsPerState, + ExactChainEmitOperationsPerState, + KllMergeOperationsPerOutputGroup, +} + +impl CostConstant { + /// Every constant. Empty in this tests-only stage. + pub const ALL: &'static [CostConstant] = &[]; + + /// Its built-in value, e.g. 170.0 for `OperationsPerGroupPerRawBatch`. + pub fn operations_per_unit(self) -> f64 { + todo!("CostConstant::operations_per_unit: implemented after the tests are approved") + } +} + +/// One node's formula CPU operations per execution, as units of work per +/// named constant plus the operations no named constant charges. +#[derive(Debug, Clone, Default, PartialEq)] +pub struct NodeCostTerms { + /// Per constant, the units it multiplies in one execution, e.g. 30,000 + /// group visits for `OperationsPerGroupPerRawBatch`. + pub units_per_constant: BTreeMap, + /// Operations charged without a named constant: one per row a `Scan` + /// reads, a predicate's operations per row, the rounding a hash + /// aggregate applies to its per-row operations. + pub operations_without_constant: f64, +} + +impl NodeCostTerms { + /// `operations_without_constant + Σ units × value(constant)`; with + /// [`CostConstant::operations_per_unit`] it is the formula's charge. + pub fn operations_with(&self, value: impl Fn(CostConstant) -> f64) -> f64 { + let _ = value; + todo!("NodeCostTerms::operations_with: implemented after the tests are approved") + } +} + +/// The terms of `node`'s formula CPU per execution, from the inputs +/// `node_resource_usage_and_cost` takes except the rate (which scales +/// executions, not units). Independent of +/// `settings.measured_operation_costs` and `frozen_calibration_factor`. +pub fn node_cost_terms<'a>( + node: &'a PhysicalASAPDAGNode, + producers: &[ProducerOutput<'a>], + window_ms: Option, + data: &DataWorkload, + settings: &'a CostInputs, +) -> Result { + let _ = (node, producers, window_ms, data, settings); + todo!("node_cost_terms: implemented after the tests are approved") +} + +#[cfg(test)] +mod tests; diff --git a/crates/cost/src/node/cost_terms/tests.rs b/crates/cost/src/node/cost_terms/tests.rs new file mode 100644 index 000000000..aceb9e667 --- /dev/null +++ b/crates/cost/src/node/cost_terms/tests.rs @@ -0,0 +1,122 @@ +//! Tests of [`CostConstant`]: its values, its coverage of the per-unit +//! constants and its JSON names. + +use std::collections::BTreeSet; + +use super::*; + +/// Each constant's value is the per-unit constant of the same name in +/// `operations_per_unit.rs`, which P2 does not change. Example: +/// `OperationsPerGroupPerRawBatch` is 170 operations per group visit. +#[test] +fn cost_constants_hold_the_built_in_values() { + for (constant, operations_per_unit) in [ + (CostConstant::OperationsPerGroupPerRawBatch, 170.0), + (CostConstant::KllUpdateOperationsPerRow, 84.0), + (CostConstant::TimeRangeOperationsPerRow, 11.0), + (CostConstant::OutputRowOperationsPerGroup, 2_500.0), + (CostConstant::KllMergeOperationsPerOutputGroup, 19_000.0), + ] { + assert_eq!( + constant.operations_per_unit(), + operations_per_unit, + "{constant:?}" + ); + } + // Every constant, against the file's text. + let defined = defined_constants(); + assert_eq!(CostConstant::ALL.len(), defined.len()); + for constant in CostConstant::ALL { + let name = format!("{constant:?}"); + let (_, value) = defined + .iter() + .find(|(defined_name, _)| *defined_name == name) + .unwrap_or_else(|| panic!("{name} has no constant in operations_per_unit.rs")); + assert_eq!(constant.operations_per_unit(), *value, "{name}"); + } +} + +/// Every `const NAME: f64` of `operations_per_unit.rs`, except the four +/// exact-distinct shape parameters (which shape units, not charge them), +/// has a `CostConstant` named in UpperCamelCase, and every `CostConstant` +/// has such a constant: adding a constant without a variant fails here. +#[test] +fn every_per_unit_constant_has_a_cost_constant() { + let defined: BTreeSet = defined_constants() + .into_iter() + .map(|(name, _)| name) + .collect(); + let variants: BTreeSet = CostConstant::ALL + .iter() + .map(|constant| format!("{constant:?}")) + .collect(); + assert_eq!(defined.len(), 37, "{defined:?}"); + assert_eq!( + defined.difference(&variants).collect::>(), + Vec::<&String>::new(), + "constants without a CostConstant" + ); + assert_eq!( + variants.difference(&defined).collect::>(), + Vec::<&String>::new(), + "CostConstants without a constant" + ); +} + +/// A constant's JSON name is its snake_case name; a name that is not a +/// constant is refused. +#[test] +fn cost_constant_json_names_are_snake_case() { + for (json, constant) in [ + ( + "\"operations_per_group_per_raw_batch\"", + CostConstant::OperationsPerGroupPerRawBatch, + ), + ( + "\"kll_update_operations_per_row\"", + CostConstant::KllUpdateOperationsPerRow, + ), + ] { + assert_eq!( + serde_json::from_str::(json).unwrap(), + constant + ); + assert_eq!(serde_json::to_string(&constant).unwrap(), json); + } + assert!(serde_json::from_str::("\"operations_per_group\"").is_err()); +} + +/// The exact-distinct cache and size parameters: not operations per unit. +const SHAPE_PARAMETERS: [&str; 4] = [ + "EXACT_DISTINCT_FEWEST_VALUES", + "EXACT_DISTINCT_MOST_VALUES", + "EXACT_DISTINCT_VALUES_IN_CACHE", + "EXACT_DISTINCT_PAIRS_IN_CACHE", +]; + +/// Each `const NAME: f64 = value;` of `operations_per_unit.rs` but the +/// shape parameters, as (UpperCamelCase name, value). +fn defined_constants() -> Vec<(String, f64)> { + include_str!("../operations_per_unit.rs") + .lines() + .filter_map(|line| { + let (_, declaration) = line.split_once("const ")?; + let (name, value) = declaration.split_once(": f64 = ")?; + let value: f64 = value.trim_end_matches(';').replace('_', "").parse().ok()?; + Some((name.trim().to_string(), value)) + }) + .filter(|(name, _)| !SHAPE_PARAMETERS.contains(&name.as_str())) + .map(|(name, value)| { + let upper_camel = name + .split('_') + .map(|word| { + let mut letters = word.chars(); + letters.next().map_or(String::new(), |first| { + first.to_string() + &letters.as_str().to_lowercase() + }) + }) + .collect(); + (upper_camel, value) + }) + .collect() +} diff --git a/crates/cost/src/node/mod.rs b/crates/cost/src/node/mod.rs index 97ad25baf..fa8f8a3af 100644 --- a/crates/cost/src/node/mod.rs +++ b/crates/cost/src/node/mod.rs @@ -1,10 +1,12 @@ //! One node of a physical plan DAG: its resource usage and cost -//! (`usage`), from the analytical per-operator resource model +//! (`usage`) and its formula CPU split into named per-unit constants +//! (`cost_terms`), from the analytical per-operator resource model //! ([`analytical`], `operator_statistics`), the measured CPU operations per //! unit of work (`operations_per_unit`) and the bytes a state holds //! ([`state_bytes`]). pub mod analytical; +pub(crate) mod cost_terms; pub(crate) mod operations_per_unit; pub(crate) mod operator_statistics; pub mod state_bytes; diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index 16bf32548..da5ca611f 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -2500,3 +2500,864 @@ fn a_kll_estimate_over_a_state_built_by_updates_charges_its_measured_time() { "3,000 rows charged {many:.1} µs, 300 rows {few:.1}; measured 1.4x" ); } + +// --------------------------------------------------------------------- +// `node_cost_terms`: the formula CPU split into named per-unit constants. +// "Formula CPU per execution" is `node_resource_usage_and_cost`'s +// `cpu_ops_per_second` at one execution per second. +// --------------------------------------------------------------------- + +/// The formula CPU per execution of `node` under `settings`. +fn formula_cpu_per_execution<'a>( + node: &'a PhysicalASAPDAGNode, + producers: &[ProducerOutput<'a>], + window_ms: Option, + data: &DataWorkload, + settings: &'a CostInputs, +) -> f64 { + node_resource_usage_and_cost(node, producers, (1, 1.0), window_ms, data, settings) + .unwrap() + .cpu_ops_per_second +} + +/// The terms of `node` at the built-in constant values. +fn charged_by_terms(terms: &crate::NodeCostTerms) -> f64 { + terms.operations_with(crate::CostConstant::operations_per_unit) +} + +/// `a`, `b` equal within `relative` of the larger. +fn assert_close(a: f64, b: f64, relative: f64, what: &str) { + assert!( + (a - b).abs() <= relative * a.abs().max(b.abs()), + "{what}: {a} against {b}" + ); +} + +/// [`a_grouping_visits_each_group_once_per_raw_batch_of_the_declared_width`]'s +/// query-time per-series aggregate of `measure` over 5 min of `latency`. +fn per_series_aggregate(measure: asap_types::ir::operator::AggIntent) -> PhysicalASAPDAGNode { + node(Payload::NonASAP(NonASAPOp::Aggregate { + reduction: Reduction::PerEntity, + measures: vec![measure], + output_names: vec![String::new()], + filters: vec![], + having: None, + child: 0, + })) +} + +/// A 5 min `TimeRange` (its producer in [`raw_rows`]). +fn five_minute_range() -> PhysicalASAPDAGNode { + node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(300), + kind: TimeRangeKind::Range, + child: 0, + })) +} + +/// A per-series `max_over_time(latency[5m])` at query time over 1,000 +/// series at 1 Hz (300,000 rows) is, by hand, one hash-aggregate key and +/// one measure per row (300,000 each), 1,000 groups visited in each of 30 +/// raw batches of 10 s (30,000), and 1,000 output groups; 16 + 4 = 20 per +/// row needs no rounding, and the output schema carries no label. At the +/// built-in values: 4.8 M + 1.2 M + 5.1 M + 2.5 M = 13.6 M, the formula +/// CPU (observed: 13,600,000, "20 ops per row, 30000 group visits"). +#[test] +fn a_per_series_max_aggregate_is_rows_visits_and_output_groups() { + use crate::CostConstant::*; + use asap_types::ir::operator::AggIntent; + let settings = CostInputs::default(); + let data = thousand_series_sampled_every(1_000); + let range = five_minute_range(); + let aggregate = per_series_aggregate(AggIntent::Max { col: None }); + let rows = || [raw_rows(&range, "latency", 300_000, 300_000)]; + let terms = crate::node_cost_terms(&aggregate, &rows(), None, &data, &settings).unwrap(); + assert_eq!( + terms.units_per_constant, + BTreeMap::from([ + (HashAggregateOperationsPerRow, 300_000.0), + (AggregateMeasureOperationsPerRow, 300_000.0), + (OperationsPerGroupPerRawBatch, 30_000.0), + (OutputRowOperationsPerGroup, 1_000.0), + ]) + ); + assert_eq!(terms.operations_without_constant, 0.0); + assert_close(charged_by_terms(&terms), 13_600_000.0, 1e-12, "by hand"); + assert_close( + charged_by_terms(&terms), + formula_cpu_per_execution(&aggregate, &rows(), None, &data, &settings), + 1e-9, + "the formula", + ); +} + +/// An ingestion-time per-series KLL chain (k = 547) of 60 s windows +/// carrying `job`, over one second of 1,000 series at 1 Hz (1,000 rows): +/// 1,000 KLL updates, 1,000 arriving rows routed to their window, 100 +/// group visits (1,000 groups × 1 s of a 10 s batch, at most one per +/// row), 1,000 × 1,000 / 60,000 = 16.67 states emitted per second, and +/// one carried label per row. At the built-in values: 84,000 + 14,000 + +/// 17,000 + 41,667 + 37,000 = 193,667, the formula CPU (observed: +/// 193,666.67, "1000 rows x 121 ops, 100 group visits, 16.7 states +/// emitted"). +#[test] +fn an_ingestion_time_kll_chain_is_updates_routing_visits_emits_and_a_carried_label() { + use crate::CostConstant::*; + use asap_types::summary_type::SketchAlgorithm; + let settings = CostInputs::default(); + let data = thousand_series_sampled_every(1_000); + let kll = FieldDataType::Sketch(SketchAlgorithm::Kll); + let scan = node(metric_scan("latency")); + let chain = PhysicalASAPDAGNode { + retention_ms: Some(300_000), + output_state: ExecutionDataState::INGESTION_SUMMARY, + output_schema: per_series_carrying_job(Some(kll.clone())), + parameters: Some(SketchParams::Kll { k: 547 }), + ..node(Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { + child: 0, + summary_type: kll, + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::PerEntity, + filter: None, + tumbling_window_width_ms: 60_000, + })) + }; + let rows = || [raw_rows(&scan, "latency", 1_000, 1_000)]; + let terms = crate::node_cost_terms(&chain, &rows(), None, &data, &settings).unwrap(); + let units = &terms.units_per_constant; + assert_eq!(units.len(), 5, "{units:?}"); + assert_eq!(units[&KllUpdateOperationsPerRow], 1_000.0); + assert_eq!(units[&ChainOperationsPerArrivingRow], 1_000.0); + assert_eq!(units[&OperationsPerGroupPerRawBatch], 100.0); + assert_close( + units[&SketchChainEmitOperationsPerState], + 1_000.0 * 1_000.0 / 60_000.0, + 1e-12, + "states emitted", + ); + assert_eq!(units[&CarriedLabelOperationsPerRowPerLabel], 1_000.0); + assert_eq!(terms.operations_without_constant, 0.0); + assert_close( + charged_by_terms(&terms), + 84_000.0 + 14_000.0 + 17_000.0 + 2_500.0 * 1_000.0 / 60.0 + 37_000.0, + 1e-12, + "by hand", + ); + assert_close( + charged_by_terms(&terms), + formula_cpu_per_execution(&chain, &rows(), None, &data, &settings), + 1e-9, + "the formula", + ); +} + +/// The per-series aggregate of `quantile_over_time(0.99, latency[5m])` +/// (300 rows per group) sorts each group: 300,000 × log2(300) = +/// 2,468,646 units of `SortOperationsPerRowPerLevel`, beside one key and +/// one measure per row, 30,000 visits and 1,000 output groups. The +/// formula rounds its per-row operations up, 16 + 4 + 5.5 × log2(300) = +/// 65.2585 to 66; the 0.7415 × 300,000 ≈ 222,450 operations of that +/// rounding are charged without a constant, so no charge changes. Sum: +/// 300,000 × 66 + 5.1 M + 2.5 M = 27.4 M, the formula CPU (observed). +#[test] +fn a_quantile_aggregate_keeps_its_rounding_outside_the_constants() { + use crate::CostConstant::*; + use asap_types::ir::operator::AggIntent; + let settings = CostInputs::default(); + let data = thousand_series_sampled_every(1_000); + let range = five_minute_range(); + let aggregate = per_series_aggregate(AggIntent::Quantile { q: 0.99, col: None }); + let rows = || [raw_rows(&range, "latency", 300_000, 300_000)]; + let terms = crate::node_cost_terms(&aggregate, &rows(), None, &data, &settings).unwrap(); + let units = &terms.units_per_constant; + assert_eq!(units.len(), 5, "{units:?}"); + let levels = 300f64.log2(); + assert_close( + units[&SortOperationsPerRowPerLevel], + 300_000.0 * levels, + 1e-12, + "sort units", + ); + assert_eq!(units[&HashAggregateOperationsPerRow], 300_000.0); + assert_eq!(units[&AggregateMeasureOperationsPerRow], 300_000.0); + assert_eq!(units[&OperationsPerGroupPerRawBatch], 30_000.0); + assert_eq!(units[&OutputRowOperationsPerGroup], 1_000.0); + let rounding = (66.0 - (16.0 + 4.0 + 5.5 * levels)) * 300_000.0; + assert_close( + terms.operations_without_constant, + rounding, + 1e-9, + "rounding", + ); + assert!((222_000.0..223_000.0).contains(&rounding), "{rounding}"); + assert_close( + charged_by_terms(&terms), + 300_000.0 * 66.0 + 5_100_000.0 + 2_500_000.0, + 1e-9, + "by hand", + ); + assert_close( + charged_by_terms(&terms), + formula_cpu_per_execution(&aggregate, &rows(), None, &data, &settings), + 1e-9, + "the formula", + ); +} + +/// A query-time scan of 5 min of `latency` (1,000 series at 1 Hz) charges +/// one operation per row read, which no named constant multiplies: no +/// units, 300,000 operations without a constant (observed: 300,000). +#[test] +fn a_scan_has_no_constant_terms() { + let settings = CostInputs::default(); + let data = thousand_series_sampled_every(1_000); + let scan = node(metric_scan("latency")); + let terms = crate::node_cost_terms(&scan, &[], Some(300_000), &data, &settings).unwrap(); + assert!( + terms.units_per_constant.is_empty(), + "{:?}", + terms.units_per_constant + ); + assert_eq!(terms.operations_without_constant, 300_000.0); + assert_close( + charged_by_terms(&terms), + formula_cpu_per_execution(&scan, &[], Some(300_000), &data, &settings), + 1e-9, + "the formula", + ); +} + +/// The invariant: for every node these tests cost, the terms at the +/// built-in values are the formula CPU per execution, so the formula and +/// its terms cannot drift. Each case is a fixture of a test above. +#[test] +fn terms_reproduce_the_formula_cpu_of_every_costed_node() { + use asap_types::ir::operator::AggIntent; + use asap_types::ir::scalar::{ScalarExpr, ScalarValue, SortKey}; + use asap_types::ir::schema::{DataType, PROMQL_SERIES_IDENTITY}; + use asap_types::summary_type::{SketchAlgorithm, SketchStatistic}; + let settings = CostInputs::default(); + fn check<'a>( + what: &str, + node: &'a PhysicalASAPDAGNode, + producers: &[ProducerOutput<'a>], + window_ms: Option, + data: &DataWorkload, + ) { + let settings = CostInputs::default(); + let formula = formula_cpu_per_execution(node, producers, window_ms, data, &settings); + let terms = crate::node_cost_terms(node, producers, window_ms, data, &settings) + .unwrap_or_else(|error| panic!("{what}: {error}")); + assert_close(charged_by_terms(&terms), formula, 1e-9, what); + } + let gauge = thousand_series_sampled_every(1_000); + let none = DataWorkload::default(); + + // Rate chains, at ingestion time and at query time. + let rate = FieldDataType::ExactAggregate(ExactKind::Rate, ExactParams::Rate); + let counter_scan = node(metric_scan("requests_total")); + let rate_chain = |output_state| PhysicalASAPDAGNode { + retention_ms: Some(300_000), + output_state, + output_schema: per_series_carrying_job(Some(rate.clone())), + ..node(Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { + child: 0, + summary_type: rate.clone(), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::PerEntity, + filter: None, + tumbling_window_width_ms: 300_000, + })) + }; + for (what, output_state, rows, span_ms) in [ + ( + "ingestion-time Rate chain", + ExecutionDataState::INGESTION_SUMMARY, + 1_000, + 1_000, + ), + ( + "query-time Rate chain", + ExecutionDataState::QUERY_ROWS, + 300_000, + 300_000, + ), + ] { + let chain = rate_chain(output_state); + check( + what, + &chain, + &[raw_rows(&counter_scan, "requests_total", rows, span_ms)], + None, + &gauge, + ); + } + + // Per-series Max and quantile aggregates; a query-time Rate build. + let range = five_minute_range(); + for measure in [ + AggIntent::Max { col: None }, + AggIntent::Quantile { q: 0.99, col: None }, + AggIntent::Sum { col: None }, + ] { + let aggregate = per_series_aggregate(measure.clone()); + check( + &format!("per-series {measure:?}"), + &aggregate, + &[raw_rows(&range, "latency", 300_000, 300_000)], + None, + &gauge, + ); + } + let rate_build = PhysicalASAPDAGNode { + output_schema: per_series_carrying_job(Some(rate.clone())), + ..node(Payload::ASAP(ASAPOp::SummaryAgg { + child: 0, + summary_type: rate.clone(), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::PerEntity, + filter: None, + })) + }; + check( + "query-time Rate build", + &rate_build, + &[raw_rows(&range, "requests_total", 300_000, 300_000)], + None, + &gauge, + ); + + // A `TimeRange` over a chain's windows, and merges over them: exact + // Sum, Rate and Max, KLL at k = 547 and 1,404. + let kll = |k| SketchParams::Kll { k }; + let (kll_547, kll_1404) = (kll(547), kll(1_404)); + let merged_families: [(&str, FieldDataType, StateFamily<'_>, Option); 5] = [ + ( + "Sum", + FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum), + StateFamily::Exact(&ExactKind::Sum), + None, + ), + ( + "Rate", + rate.clone(), + StateFamily::Exact(&ExactKind::Rate), + None, + ), + ( + "Max", + FieldDataType::ExactAggregate(ExactKind::Max, ExactParams::Max), + StateFamily::Exact(&ExactKind::Max), + None, + ), + ( + "KLL k = 547", + FieldDataType::Sketch(SketchAlgorithm::Kll), + StateFamily::Sketch(&kll_547), + Some(kll_547.clone()), + ), + ( + "KLL k = 1,404", + FieldDataType::Sketch(SketchAlgorithm::Kll), + StateFamily::Sketch(&kll_1404), + Some(kll_1404.clone()), + ), + ]; + for (what, state_type, family, parameters) in merged_families { + let chain = PhysicalASAPDAGNode { + parameters: parameters.clone(), + output_schema: state_schema(state_type.clone(), &[PROMQL_SERIES_IDENTITY]), + ..node(Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { + child: 0, + summary_type: state_type, + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::PerEntity, + filter: None, + tumbling_window_width_ms: 60_000, + })) + }; + let rows_per_state = StateInputRows { + rows_per_state: Some(60), + non_negative: true, + value_magnitude_range: None, + }; + // A KLL state's bytes grow with its `k`; an exact state is 56 bytes. + let state_bytes = parameters.as_ref().map_or(56, |parameters| { + crate::node::state_bytes::sketch_state_bytes(parameters, rows_per_state) + }); + let chain_output = ProducerOutput { + payload: &chain.payload, + output_schema: &chain.output_schema, + output: EdgeStatistics { + rows: 1_000, + bytes: 1_000 * state_bytes, + }, + sources: BTreeSet::from(["latency"]), + family, + unchanged_columns: BTreeSet::new(), + tumbling_windows: 1, + chain_tumbling_window_width_ms: Some(60_000), + raw_rows_span_ms: None, + state_input_rows: Some(rows_per_state), + }; + let windows = PhysicalASAPDAGNode { + output_schema: chain.output_schema.clone(), + ..five_minute_range() + }; + check( + &format!("TimeRange over a {what} chain"), + &windows, + std::slice::from_ref(&chain_output), + None, + &gauge, + ); + let windows_usage = node_resource_usage_and_cost( + &windows, + &[chain_output], + (1, 1.0), + None, + &gauge, + &settings, + ) + .unwrap(); + let merge = PhysicalASAPDAGNode { + parameters, + output_schema: chain.output_schema.clone(), + ..node(Payload::ASAP(ASAPOp::SummaryMerge { + children: vec![0], + group_by: None, + })) + }; + check( + &format!("merge over a {what} chain's windows"), + &merge, + &[produced(&windows, &windows_usage)], + None, + &gauge, + ); + } + + // A KMV build, ungrouped and by `region`, and exact distinct counts, + // ungrouped and by `job`. + let (distinct_data, distinct_schema) = sql_rows_of_t(100_000); + let sql_range = PhysicalASAPDAGNode { + output_schema: distinct_schema, + ..five_minute_range() + }; + let sql_rows = || { + let mut rows = raw_rows(&sql_range, "t", 300_000, 300_000); + rows.output_schema = &sql_range.output_schema; + rows.unchanged_columns = ["job", "region", "instance", "user_id", "value"].into(); + rows + }; + for (keys, k) in [(vec![], 4_323), (vec![2], 26_656)] { + let build = PhysicalASAPDAGNode { + parameters: Some(SketchParams::Kmv { k }), + ..node(Payload::ASAP(ASAPOp::SummaryAgg { + child: 0, + summary_type: FieldDataType::Sketch(SketchAlgorithm::Kmv), + input: SummaryUpdate::column(ColumnRef::Named("user_id".into())), + reduction: Reduction::by(keys.clone()), + filter: None, + })) + }; + check( + &format!("KMV build by {keys:?}"), + &build, + &[sql_rows()], + None, + &distinct_data, + ); + } + for keys in [vec![], vec![1]] { + let count = node(Payload::NonASAP(NonASAPOp::Aggregate { + reduction: Reduction::by(keys.clone()), + measures: vec![AggIntent::Cardinality { cols: vec![4] }], + output_names: vec![String::new()], + filters: vec![], + having: None, + child: 0, + })); + check( + &format!("exact distinct count by {keys:?}"), + &count, + &[sql_rows()], + None, + &distinct_data, + ); + } + + // A KMV rollup merge (from `a_kmv_rollup_merge_costs_per_hash_it_inserts`). + let rollup_data: DataWorkload = serde_json::from_value(serde_json::json!({ + "arrival": {"ingesting": {"rows_per_second": 2_000_000.0}}, + "sources": {"http": {"distinct_values_per_label": {"service": 100, "region": 10}}} + })) + .unwrap(); + let kmv = SketchParams::Kmv { k: 106_263 }; + let kmv_rows = StateInputRows { + rows_per_state: Some(12_000), + non_negative: false, + value_magnitude_range: None, + }; + let kmv_build = PhysicalASAPDAGNode { + parameters: Some(kmv.clone()), + output_schema: Schema::new(vec![ + Field::new("region", FieldDataType::Plain(DataType::Utf8), false), + Field::new("service", FieldDataType::Plain(DataType::Utf8), false), + Field::new("state", FieldDataType::Sketch(SketchAlgorithm::Kmv), false), + ]), + ..node(Payload::ASAP(ASAPOp::SummaryAgg { + child: 0, + summary_type: FieldDataType::Sketch(SketchAlgorithm::Kmv), + input: SummaryUpdate::column(ColumnRef::Named("user_id".into())), + reduction: Reduction::by(vec![0, 1]), + filter: None, + })) + }; + let kmv_rollup = PhysicalASAPDAGNode { + parameters: Some(kmv.clone()), + ..node(Payload::ASAP(ASAPOp::SummaryMerge { + children: vec![0], + group_by: Some(vec![1]), + })) + }; + let kmv_state_bytes = crate::node::state_bytes::sketch_state_bytes(&kmv, kmv_rows); + check( + "KMV rollup merge", + &kmv_rollup, + &[ProducerOutput { + payload: &kmv_build.payload, + output_schema: &kmv_build.output_schema, + output: EdgeStatistics { + rows: 1_000, + bytes: 1_000 * (kmv_state_bytes + 32), + }, + sources: BTreeSet::from(["http"]), + family: StateFamily::Sketch(&kmv), + unchanged_columns: ["region", "service"].into(), + tumbling_windows: 1, + chain_tumbling_window_width_ms: None, + raw_rows_span_ms: None, + state_input_rows: Some(kmv_rows), + }], + None, + &rollup_data, + ); + + // A DDSketch build at α = 0.01 over 600 rows of a scan. + let latency_scan = PhysicalASAPDAGNode { + output_schema: Schema::new(vec![ + Field::plain("ts", DataType::Timestamp, false), + Field::plain("value", DataType::Float64, false), + ]), + ..node(metric_scan("latency")) + }; + let ddsketch_build = PhysicalASAPDAGNode { + parameters: Some(SketchParams::DDSketch { alpha: 0.01 }), + ..node(Payload::ASAP(ASAPOp::SummaryAgg { + child: 0, + summary_type: FieldDataType::Sketch(SketchAlgorithm::DDSketch), + input: SummaryUpdate::column(ColumnRef::Named("value".to_string())), + reduction: Reduction::Reduce(GroupKeys::default()), + filter: None, + })) + }; + let mut six_hundred_rows = raw_rows(&latency_scan, "latency", 600, 60_000); + six_hundred_rows.raw_rows_span_ms = None; + check( + "DDSketch build", + &ddsketch_build, + &[six_hundred_rows], + None, + &gauge, + ); + + // KLL estimates (k = 547) after updates and after a merge. + let estimate = node(Payload::ASAP(ASAPOp::SummaryEstimate { + summary_input: 0, + query: SketchStatistic::Quantile { q: 0.99 }, + })); + let kll_updates = node(Payload::ASAP(ASAPOp::SummaryAgg { + child: 0, + summary_type: FieldDataType::Sketch(SketchAlgorithm::Kll), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::PerEntity, + filter: None, + })); + let kll_merge = node(Payload::ASAP(ASAPOp::SummaryMerge { + children: vec![0], + group_by: None, + })); + for (what, states, rows_per_state, state_bytes) in [ + ("KLL estimate after updates", &kll_updates, 3_000, 17_000), + ("KLL estimate after a merge", &kll_merge, 300, 4_844), + ] { + check( + what, + &estimate, + &[ProducerOutput { + payload: &states.payload, + output_schema: &states.output_schema, + output: EdgeStatistics { + rows: 1_000, + bytes: 1_000 * state_bytes, + }, + sources: BTreeSet::new(), + family: StateFamily::Sketch(&kll_547), + unchanged_columns: BTreeSet::new(), + tumbling_windows: 1, + chain_tumbling_window_width_ms: None, + raw_rows_span_ms: None, + state_input_rows: Some(StateInputRows { + rows_per_state: Some(rows_per_state), + non_negative: true, + value_magnitude_range: None, + }), + }], + None, + &none, + ); + } + + // `FinalizeExactAccumulator` of 1,000 merged Sum states. + let finalize = node(Payload::ASAP(ASAPOp::FinalizeExactAccumulator { child: 0 })); + check( + "FinalizeExactAccumulator", + &finalize, + &[ProducerOutput { + payload: &kll_merge.payload, + output_schema: &kll_merge.output_schema, + output: EdgeStatistics { + rows: 1_000, + bytes: 56_000, + }, + sources: BTreeSet::new(), + family: StateFamily::Exact(&ExactKind::Sum), + unchanged_columns: BTreeSet::new(), + tumbling_windows: 1, + chain_tumbling_window_width_ms: None, + raw_rows_span_ms: None, + state_input_rows: None, + }], + None, + &none, + ); + + // `Sort`, `Limit`, `Dedup` and `Concat` over a 5 min scan. + let scan = node(metric_scan("latency")); + let scan_usage = + node_resource_usage_and_cost(&scan, &[], (1, 1.0), Some(300_000), &gauge, &settings) + .unwrap(); + for payload in [ + Payload::NonASAP(NonASAPOp::Sort { + keys: vec![SortKey { + expr: ScalarExpr::Literal(ScalarValue::Float64(1.0)), + ascending: true, + nulls_first: false, + }], + partition_by: GroupKeys::default(), + child: 0, + }), + Payload::NonASAP(NonASAPOp::Limit { + n: Some(10), + offset: 0, + partition_by: GroupKeys::default(), + child: 0, + }), + Payload::NonASAP(NonASAPOp::Dedup { + cols: vec![], + child: 0, + }), + ] { + let operator = node(payload); + check( + &format!("{:?}", operator.payload), + &operator, + &[produced(&scan, &scan_usage)], + None, + &gauge, + ); + } + let concat = node(Payload::NonASAP(NonASAPOp::Concat { + children: vec![0, 1], + })); + check( + "Concat", + &concat, + &[produced(&scan, &scan_usage), produced(&scan, &scan_usage)], + None, + &gauge, + ); + + // An ingestion-time view of raw rows, kept for 390 s. + let mut view_data = + DataWorkload::sampled_every(["m".to_string()], asap_types::workload::DurationMs(1_000)); + for facts in view_data.sources.values_mut() { + facts.series_count = Some(1_000); + } + let m_scan = node(metric_scan("m")); + let view = PhysicalASAPDAGNode { + output_state: ExecutionDataState::INGESTION_ROWS, + lifetime: Lifetime::AcrossEvaluations(StorageMedium::Memory), + retention_ms: Some(390_000), + output_schema: Schema::new(vec![ + Field::plain("ts", DataType::Timestamp, false), + Field::plain("value", DataType::Float64, false), + ]), + ..node(Payload::NonASAP(NonASAPOp::TimeShift { + shift: asap_types::ir::operator::arguments::TimeShift { + offset_ms: 90_000, + at: None, + }, + child: 0, + })) + }; + let mut one_second_of_rows = raw_rows(&m_scan, "m", 1_000, 1_000); + one_second_of_rows.output.bytes = 64_000; + one_second_of_rows.raw_rows_span_ms = None; + check( + "ingestion-time view of raw rows", + &view, + &[one_second_of_rows], + None, + &view_data, + ); +} + +/// A refit of one constant changes the charge by its units times the +/// change, which is `fitlib.charge`'s rule: the per-series Max aggregate's +/// 13.6 M with 140 operations per group visit instead of 170 is +/// 13.6 M − 30 × 30,000 = 12.7 M. +#[test] +fn replacing_a_constant_changes_the_charge_by_its_units() { + use crate::CostConstant; + use asap_types::ir::operator::AggIntent; + let settings = CostInputs::default(); + let data = thousand_series_sampled_every(1_000); + let range = five_minute_range(); + let aggregate = per_series_aggregate(AggIntent::Max { col: None }); + let terms = crate::node_cost_terms( + &aggregate, + &[raw_rows(&range, "latency", 300_000, 300_000)], + None, + &data, + &settings, + ) + .unwrap(); + let refit = terms.operations_with(|constant| { + if constant == CostConstant::OperationsPerGroupPerRawBatch { + 140.0 + } else { + constant.operations_per_unit() + } + }); + assert_close(refit, 12_700_000.0, 1e-12, "refit charge"); +} + +/// The terms describe the formulas: a measured table entry for the node +/// and a frozen calibration factor change its price (`cost_source` becomes +/// `Measured`) but not its terms. Node: a query-time KLL build at k = 547 +/// over 600 rows. +#[test] +fn terms_ignore_the_measured_table_and_the_calibration_factor() { + use crate::{MeasuredOperationCosts, OperatorCost, OperatorKind}; + use asap_types::summary_type::SketchAlgorithm; + let data = thousand_series_sampled_every(1_000); + let range = five_minute_range(); + let build = PhysicalASAPDAGNode { + parameters: Some(SketchParams::Kll { k: 547 }), + ..node(Payload::ASAP(ASAPOp::SummaryAgg { + child: 0, + summary_type: FieldDataType::Sketch(SketchAlgorithm::Kll), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::PerEntity, + filter: None, + })) + }; + let rows = || [raw_rows(&range, "latency", 600, 300_000)]; + let formulas = CostInputs::default(); + let measured = CostInputs { + measured_operation_costs: MeasuredOperationCosts { + operators: vec![OperatorCost { + operator: OperatorKind::SummaryAgg, + phase: None, + sketch: Some(SketchParams::Kll { k: 547 }), + exact: None, + measure: None, + ns_per_unit: 10.0, + ns_per_invocation: 0.0, + memory_bytes_per_state: None, + }], + ..MeasuredOperationCosts::default() + }, + frozen_calibration_factor: Some(2.0), + ..CostInputs::default() + }; + let usage = + node_resource_usage_and_cost(&build, &rows(), (1, 1.0), None, &data, &measured).unwrap(); + assert_eq!(usage.cost.cost_source, NodeCostSource::Measured); + assert_eq!( + crate::node_cost_terms(&build, &rows(), None, &data, &measured).unwrap(), + crate::node_cost_terms(&build, &rows(), None, &data, &formulas).unwrap() + ); +} + +/// A node `node_resource_usage_and_cost` cannot cost has no terms either, +/// with the same error: a view materialized across evaluations without a +/// retention (`a_view_without_retention_is_not_costed`). +#[test] +fn terms_fail_where_the_node_cannot_be_costed() { + let settings = CostInputs::default(); + let data = DataWorkload::default(); + let scan = node(metric_scan("m")); + let mut one_second_of_rows = raw_rows(&scan, "m", 1_000, 1_000); + one_second_of_rows.output.bytes = 64_000; + one_second_of_rows.sources = BTreeSet::new(); + one_second_of_rows.raw_rows_span_ms = None; + let view = 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, + })) + }; + assert!(matches!( + crate::node_cost_terms(&view, &[one_second_of_rows], None, &data, &settings), + Err(AnalyticalCostError::MissingOrZero("retention_ms")) + )); +} + +/// The group-visit units follow the deployment's raw batch width: the +/// per-series Max aggregate over 5 min of 1,000 series at 1 Hz visits +/// each series 300 times in 1 s batches (300,000, one per row) and 30 +/// times in 10 s batches (30,000). The 270,000 visits between them times +/// 170 are `a_grouping_visits_each_group_once_per_raw_batch_of_the_declared_width`'s +/// difference. +#[test] +fn group_visit_units_follow_the_raw_batch_width() { + use crate::CostConstant::OperationsPerGroupPerRawBatch; + use asap_types::ir::operator::AggIntent; + let data = thousand_series_sampled_every(1_000); + let range = five_minute_range(); + let aggregate = per_series_aggregate(AggIntent::Max { col: None }); + let visits = |raw_batch_width_ms: u64| { + let mut settings = CostInputs::default(); + settings.resource_speed.raw_batch_width_ms = raw_batch_width_ms; + crate::node_cost_terms( + &aggregate, + &[raw_rows(&range, "latency", 300_000, 300_000)], + None, + &data, + &settings, + ) + .unwrap() + .units_per_constant[&OperationsPerGroupPerRawBatch] + }; + assert_eq!(visits(1_000), 300_000.0); + assert_eq!(visits(10_000), 30_000.0); +} From 32aac84e5066ecc91705c8ff3b6b642aedd63628 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:48:07 +0000 Subject: [PATCH 2/3] feat(cost): node_cost_terms: a node's formula CPU as named per-unit constants times units, the charge computed from those terms CostConstant names each per-unit constant of operations_per_unit.rs (values unchanged). The per-unit helpers and estimate_operator now return NodeCostTerms, and node_resource_usage_and_cost computes its formula CPU as those terms at the built-in values, so the charge and its terms cannot drift. A hash aggregate's rounding of its per-row operations stays, charged without a constant. node_cost_terms returns the terms taken before a measured entry or the calibration factor applies. --- crates/cost/src/node/analytical.rs | 91 +++++-- crates/cost/src/node/cost_terms.rs | 215 +++++++++++++++- crates/cost/src/node/cost_terms/tests.rs | 57 ++++- crates/cost/src/node/operations_per_unit.rs | 268 +++++++++++++------- crates/cost/src/node/usage/mod.rs | 213 ++++++++++------ crates/cost/src/node/usage/tests.rs | 4 +- 6 files changed, 635 insertions(+), 213 deletions(-) diff --git a/crates/cost/src/node/analytical.rs b/crates/cost/src/node/analytical.rs index d711bc438..5b7a96e42 100644 --- a/crates/cost/src/node/analytical.rs +++ b/crates/cost/src/node/analytical.rs @@ -4,9 +4,7 @@ //! separate. Plan selection (Stage 3) turns them into one objective with //! [`crate::ResourceUnitCost`]. -use crate::node::operations_per_unit::{ - OUTPUT_ROW_OPERATIONS_PER_GROUP, TIME_RANGE_OPERATIONS_PER_ROW, -}; +use crate::node::cost_terms::{CostConstant, NodeCostTerms}; use crate::resources::ModeledCpu; use asap_types::summary_type::{SketchAlgorithm, SketchParams}; use serde::{Deserialize, Serialize}; @@ -373,14 +371,31 @@ fn validate_partitioning( Ok(()) } -/// Estimate one physical operator. Child costs are deliberately excluded; -/// a DAG walker sums CPU/disk once per node and combines simultaneously -/// retained state separately. +/// [`estimate_operator_with_terms`] without its terms. +#[cfg(test)] pub(crate) fn estimate_operator( operator: PhysicalOperator, statistics: OperatorStatistics, ) -> Result { + estimate_operator_with_terms(operator, statistics).map(|(estimate, _)| estimate) +} + +/// Estimate one physical operator. Child costs are deliberately excluded; +/// a DAG walker sums CPU/disk once per node and combines simultaneously +/// retained state separately. Its CPU operations are also returned as +/// terms: units of the +/// named constants it charges (rows a time range keeps, groups a hash +/// aggregate outputs), the rest without a constant. Example: a time +/// range keeping 200 rows is 200 units of `TimeRangeOperationsPerRow` +/// (2,200 operations); a sort's comparisons are charged without one. +pub(crate) fn estimate_operator_with_terms( + operator: PhysicalOperator, + statistics: OperatorStatistics, +) -> Result<(ResourceEstimate, NodeCostTerms), AnalyticalCostError> { validate_operator_semantics(operator, &statistics)?; + // The terms of an operator charging a named constant; any other's CPU + // is charged without one. + let mut named_terms = None; let per_row_width = |rows: u64, bytes: u64| -> Result { match (rows, bytes) { (0, 0) => return Ok(0), @@ -410,10 +425,14 @@ pub(crate) fn estimate_operator( "PromQL scalar leaf must emit one scalar row per evaluation step", )); } - return Ok(ResourceEstimate::new( + let estimate = ResourceEstimate::new( output.rows as f64, per_row_width(output.rows, output.bytes)?, 0, + ); + return Ok(( + estimate, + NodeCostTerms::without_constant(output.rows as f64), )); } let left = input(0)?; @@ -449,11 +468,17 @@ pub(crate) fn estimate_operator( 0, ), (PhysicalOperator::TimeShift, _) => ResourceEstimate::new(0.0, 0, 0), - (PhysicalOperator::TimeRange, _) => ResourceEstimate::new( - output.rows as f64 * TIME_RANGE_OPERATIONS_PER_ROW, - per_row_width(output.rows, output.bytes)?, - 0, - ), + (PhysicalOperator::TimeRange, _) => { + let terms = + NodeCostTerms::of(CostConstant::TimeRangeOperationsPerRow, output.rows as f64); + let estimate = ResourceEstimate::new( + terms.operations(), + per_row_width(output.rows, output.bytes)?, + 0, + ); + named_terms = Some(terms); + estimate + } (PhysicalOperator::PassThrough, _) => ResourceEstimate::new( left.rows as f64, per_row_width(output.rows, output.bytes)?, @@ -469,18 +494,29 @@ pub(crate) fn estimate_operator( accumulator_bytes_per_group, .. }, - ) => ResourceEstimate::new( - checked_cpu_product(left.rows, operations_per_row)? - + *group_count as f64 * OUTPUT_ROW_OPERATIONS_PER_GROUP, - checked_bytes(&[ - *group_count, - key_bytes - .checked_add(*accumulator_bytes_per_group) - .and_then(|bytes| bytes.checked_add(16)) - .ok_or(AnalyticalCostError::Overflow)?, - ])?, - 0, - ), + ) => { + let terms = NodeCostTerms::without_constant(checked_cpu_product( + left.rows, + operations_per_row, + )?) + .plus( + CostConstant::OutputRowOperationsPerGroup, + *group_count as f64, + ); + let estimate = ResourceEstimate::new( + terms.operations(), + checked_bytes(&[ + *group_count, + key_bytes + .checked_add(*accumulator_bytes_per_group) + .and_then(|bytes| bytes.checked_add(16)) + .ok_or(AnalyticalCostError::Overflow)?, + ])?, + 0, + ); + named_terms = Some(terms); + estimate + } ( PhysicalOperator::HashDeduplicate { key_count }, OperatorStatistics::HashDeduplicate { @@ -743,7 +779,9 @@ pub(crate) fn estimate_operator( } }; if estimate.cpu_ops().is_finite() { - Ok(estimate) + let terms = + named_terms.unwrap_or_else(|| NodeCostTerms::without_constant(estimate.cpu_ops())); + Ok((estimate, terms)) } else { Err(AnalyticalCostError::Overflow) } @@ -1538,6 +1576,9 @@ fn checked_bytes(parts: &[u64]) -> Result { #[cfg(test)] mod tests { use super::*; + use crate::node::operations_per_unit::{ + OUTPUT_ROW_OPERATIONS_PER_GROUP, TIME_RANGE_OPERATIONS_PER_ROW, + }; use crate::node::operator_statistics::{ BinaryEdgeStatistics, EdgeStatistics, OperatorStatistics, PartitionStatistics, PromqlBinaryEdgeStatistics, PromqlEdgeStatistics, PromqlUnaryEdgeStatistics, diff --git a/crates/cost/src/node/cost_terms.rs b/crates/cost/src/node/cost_terms.rs index afae2c3d2..902767bed 100644 --- a/crates/cost/src/node/cost_terms.rs +++ b/crates/cost/src/node/cost_terms.rs @@ -14,9 +14,6 @@ //! The terms describe the formulas only: they do not depend on //! `CostInputs::measured_operation_costs` or `frozen_calibration_factor`. -// Tests-only stage: the bodies below are `todo!()` until the tests are -// approved; the declarations are the interface the tests check. - use std::collections::BTreeMap; use asap_types::plan::physical_asap_dag::PhysicalASAPDAGNode; @@ -24,6 +21,8 @@ use asap_types::workload::DataWorkload; use serde::{Deserialize, Serialize}; use crate::node::analytical::AnalyticalCostError; +use crate::node::operations_per_unit as built_in; +use crate::node::usage::node_usage_and_cost_terms; use crate::{CostInputs, ProducerOutput}; /// A named per-unit constant of the cost formulas: CPU operations per unit @@ -72,12 +71,151 @@ pub enum CostConstant { } impl CostConstant { - /// Every constant. Empty in this tests-only stage. - pub const ALL: &'static [CostConstant] = &[]; + /// Every constant, in declaration order. + pub const ALL: &'static [CostConstant] = &[ + CostConstant::TimeRangeOperationsPerRow, + CostConstant::OperationsPerGroupPerRawBatch, + CostConstant::ChainOperationsPerArrivingRow, + CostConstant::HashAggregateOperationsPerRow, + CostConstant::HashAggregateOperationsPerFurtherKey, + CostConstant::AggregateMeasureOperationsPerRow, + CostConstant::SortOperationsPerRowPerLevel, + CostConstant::DdsketchUpdateOperationsPerRow, + CostConstant::KllUpdateOperationsPerRow, + CostConstant::ExactUpdateOperationsPerRow, + CostConstant::CounterUpdateOperationsPerRow, + CostConstant::CarriedLabelOperationsPerRowPerLabel, + CostConstant::KmvUpdateOperationsPerRow, + CostConstant::KmvOperationsPerComparedHash, + CostConstant::ExactDistinctOperationsPerRow, + CostConstant::ExactDistinctOperationsPerValueDoubling, + CostConstant::ExactDistinctOperationsPerRowAtFullValueMiss, + CostConstant::ExactDistinctOperationsPerRowAtFullPairMiss, + CostConstant::ExactDistinctOperationsPerInsertedValue, + CostConstant::ExactDistinctOperationsPerInsertedPair, + CostConstant::SketchUpdateOperationsPerCounter, + CostConstant::ExactMergeOperationsPerState, + CostConstant::ExtremumMergeOperationsPerState, + CostConstant::CounterMergeOperationsPerState, + CostConstant::DdsketchMergeOperationsPerState, + CostConstant::KllMergeOperationsPerState, + CostConstant::KllMergeOperationsPerInputByte, + CostConstant::KllMergeOperationsPerMergedByte, + CostConstant::MergeOperationsPerOutputGroup, + CostConstant::SketchMergeOperationsPerByte, + CostConstant::KllEstimateOperationsPerByte, + CostConstant::KllEstimateOperationsPerUnsortedItem, + CostConstant::SketchEstimateOperationsPerGroup, + CostConstant::OutputRowOperationsPerGroup, + CostConstant::SketchChainEmitOperationsPerState, + CostConstant::ExactChainEmitOperationsPerState, + CostConstant::KllMergeOperationsPerOutputGroup, + ]; - /// Its built-in value, e.g. 170.0 for `OperationsPerGroupPerRawBatch`. + /// Its built-in value, e.g. 170.0 for `OperationsPerGroupPerRawBatch`: + /// the constant of the same name in `operations_per_unit`. pub fn operations_per_unit(self) -> f64 { - todo!("CostConstant::operations_per_unit: implemented after the tests are approved") + match self { + CostConstant::TimeRangeOperationsPerRow => built_in::TIME_RANGE_OPERATIONS_PER_ROW, + CostConstant::OperationsPerGroupPerRawBatch => { + built_in::OPERATIONS_PER_GROUP_PER_RAW_BATCH + } + CostConstant::ChainOperationsPerArrivingRow => { + built_in::CHAIN_OPERATIONS_PER_ARRIVING_ROW + } + CostConstant::HashAggregateOperationsPerRow => { + built_in::HASH_AGGREGATE_OPERATIONS_PER_ROW + } + CostConstant::HashAggregateOperationsPerFurtherKey => { + built_in::HASH_AGGREGATE_OPERATIONS_PER_FURTHER_KEY + } + CostConstant::AggregateMeasureOperationsPerRow => { + built_in::AGGREGATE_MEASURE_OPERATIONS_PER_ROW + } + CostConstant::SortOperationsPerRowPerLevel => { + built_in::SORT_OPERATIONS_PER_ROW_PER_LEVEL + } + CostConstant::DdsketchUpdateOperationsPerRow => { + built_in::DDSKETCH_UPDATE_OPERATIONS_PER_ROW + } + CostConstant::KllUpdateOperationsPerRow => built_in::KLL_UPDATE_OPERATIONS_PER_ROW, + CostConstant::ExactUpdateOperationsPerRow => built_in::EXACT_UPDATE_OPERATIONS_PER_ROW, + CostConstant::CounterUpdateOperationsPerRow => { + built_in::COUNTER_UPDATE_OPERATIONS_PER_ROW + } + CostConstant::CarriedLabelOperationsPerRowPerLabel => { + built_in::CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL + } + CostConstant::KmvUpdateOperationsPerRow => built_in::KMV_UPDATE_OPERATIONS_PER_ROW, + CostConstant::KmvOperationsPerComparedHash => { + built_in::KMV_OPERATIONS_PER_COMPARED_HASH + } + CostConstant::ExactDistinctOperationsPerRow => { + built_in::EXACT_DISTINCT_OPERATIONS_PER_ROW + } + CostConstant::ExactDistinctOperationsPerValueDoubling => { + built_in::EXACT_DISTINCT_OPERATIONS_PER_VALUE_DOUBLING + } + CostConstant::ExactDistinctOperationsPerRowAtFullValueMiss => { + built_in::EXACT_DISTINCT_OPERATIONS_PER_ROW_AT_FULL_VALUE_MISS + } + CostConstant::ExactDistinctOperationsPerRowAtFullPairMiss => { + built_in::EXACT_DISTINCT_OPERATIONS_PER_ROW_AT_FULL_PAIR_MISS + } + CostConstant::ExactDistinctOperationsPerInsertedValue => { + built_in::EXACT_DISTINCT_OPERATIONS_PER_INSERTED_VALUE + } + CostConstant::ExactDistinctOperationsPerInsertedPair => { + built_in::EXACT_DISTINCT_OPERATIONS_PER_INSERTED_PAIR + } + CostConstant::SketchUpdateOperationsPerCounter => { + built_in::SKETCH_UPDATE_OPERATIONS_PER_COUNTER + } + CostConstant::ExactMergeOperationsPerState => { + built_in::EXACT_MERGE_OPERATIONS_PER_STATE + } + CostConstant::ExtremumMergeOperationsPerState => { + built_in::EXTREMUM_MERGE_OPERATIONS_PER_STATE + } + CostConstant::CounterMergeOperationsPerState => { + built_in::COUNTER_MERGE_OPERATIONS_PER_STATE + } + CostConstant::DdsketchMergeOperationsPerState => { + built_in::DDSKETCH_MERGE_OPERATIONS_PER_STATE + } + CostConstant::KllMergeOperationsPerState => built_in::KLL_MERGE_OPERATIONS_PER_STATE, + CostConstant::KllMergeOperationsPerInputByte => { + built_in::KLL_MERGE_OPERATIONS_PER_INPUT_BYTE + } + CostConstant::KllMergeOperationsPerMergedByte => { + built_in::KLL_MERGE_OPERATIONS_PER_MERGED_BYTE + } + CostConstant::MergeOperationsPerOutputGroup => { + built_in::MERGE_OPERATIONS_PER_OUTPUT_GROUP + } + CostConstant::SketchMergeOperationsPerByte => { + built_in::SKETCH_MERGE_OPERATIONS_PER_BYTE + } + CostConstant::KllEstimateOperationsPerByte => { + built_in::KLL_ESTIMATE_OPERATIONS_PER_BYTE + } + CostConstant::KllEstimateOperationsPerUnsortedItem => { + built_in::KLL_ESTIMATE_OPERATIONS_PER_UNSORTED_ITEM + } + CostConstant::SketchEstimateOperationsPerGroup => { + built_in::SKETCH_ESTIMATE_OPERATIONS_PER_GROUP + } + CostConstant::OutputRowOperationsPerGroup => built_in::OUTPUT_ROW_OPERATIONS_PER_GROUP, + CostConstant::SketchChainEmitOperationsPerState => { + built_in::SKETCH_CHAIN_EMIT_OPERATIONS_PER_STATE + } + CostConstant::ExactChainEmitOperationsPerState => { + built_in::EXACT_CHAIN_EMIT_OPERATIONS_PER_STATE + } + CostConstant::KllMergeOperationsPerOutputGroup => { + built_in::KLL_MERGE_OPERATIONS_PER_OUTPUT_GROUP + } + } } } @@ -98,8 +236,61 @@ impl NodeCostTerms { /// `operations_without_constant + Σ units × value(constant)`; with /// [`CostConstant::operations_per_unit`] it is the formula's charge. pub fn operations_with(&self, value: impl Fn(CostConstant) -> f64) -> f64 { - let _ = value; - todo!("NodeCostTerms::operations_with: implemented after the tests are approved") + self.operations_without_constant + + self + .units_per_constant + .iter() + .map(|(constant, units)| units * value(*constant)) + .sum::() + } + + /// The operations at the built-in values: the formula's charge. + pub(crate) fn operations(&self) -> f64 { + self.operations_with(CostConstant::operations_per_unit) + } + + /// `units` of `constant` alone. Example: `of(TimeRangeOperationsPerRow, + /// 300.0)` is a time range keeping 300 rows. + pub(crate) fn of(constant: CostConstant, units: f64) -> Self { + Self::default().plus(constant, units) + } + + /// `operations` charged without a named constant alone, e.g. one per + /// row a scan reads. + pub(crate) fn without_constant(operations: f64) -> Self { + Self { + units_per_constant: BTreeMap::new(), + operations_without_constant: operations, + } + } + + /// These terms and `units` more of `constant`; zero units add no entry. + pub(crate) fn plus(mut self, constant: CostConstant, units: f64) -> Self { + if units != 0.0 { + *self.units_per_constant.entry(constant).or_insert(0.0) += units; + } + self + } + + /// These terms and `other`'s. + pub(crate) fn plus_terms(mut self, other: &NodeCostTerms) -> Self { + for (constant, units) in &other.units_per_constant { + self = self.plus(*constant, *units); + } + self.operations_without_constant += other.operations_without_constant; + self + } + + /// These terms `times` over: the terms per row times the rows. + /// Example: one KLL update per row, times 1,000 rows, is 1,000 units + /// of `KllUpdateOperationsPerRow`. + pub(crate) fn times(mut self, times: f64) -> Self { + for units in self.units_per_constant.values_mut() { + *units *= times; + } + self.units_per_constant.retain(|_, units| *units != 0.0); + self.operations_without_constant *= times; + self } } @@ -114,8 +305,10 @@ pub fn node_cost_terms<'a>( data: &DataWorkload, settings: &'a CostInputs, ) -> Result { - let _ = (node, producers, window_ms, data, settings); - todo!("node_cost_terms: implemented after the tests are approved") + // Its usage at one execution per second: the terms are computed before + // the measured table or the calibration factor apply. + node_usage_and_cost_terms(node, producers, (1, 1.0), window_ms, data, settings) + .map(|(_, terms)| terms) } #[cfg(test)] diff --git a/crates/cost/src/node/cost_terms/tests.rs b/crates/cost/src/node/cost_terms/tests.rs index aceb9e667..93efa8da8 100644 --- a/crates/cost/src/node/cost_terms/tests.rs +++ b/crates/cost/src/node/cost_terms/tests.rs @@ -6,7 +6,7 @@ use std::collections::BTreeSet; use super::*; /// Each constant's value is the per-unit constant of the same name in -/// `operations_per_unit.rs`, which P2 does not change. Example: +/// `operations_per_unit.rs`. Example: /// `OperationsPerGroupPerRawBatch` is 170 operations per group visit. #[test] fn cost_constants_hold_the_built_in_values() { @@ -86,6 +86,61 @@ fn cost_constant_json_names_are_snake_case() { assert!(serde_json::from_str::("\"operations_per_group\"").is_err()); } +/// The JSON name of every constant, in declaration order: the approx-bench +/// side reads these keys, so renaming a variant must fail here. +#[test] +fn cost_constant_json_names_are_the_full_snake_case_list() { + let expected: [&str; 37] = [ + "time_range_operations_per_row", + "operations_per_group_per_raw_batch", + "chain_operations_per_arriving_row", + "hash_aggregate_operations_per_row", + "hash_aggregate_operations_per_further_key", + "aggregate_measure_operations_per_row", + "sort_operations_per_row_per_level", + "ddsketch_update_operations_per_row", + "kll_update_operations_per_row", + "exact_update_operations_per_row", + "counter_update_operations_per_row", + "carried_label_operations_per_row_per_label", + "kmv_update_operations_per_row", + "kmv_operations_per_compared_hash", + "exact_distinct_operations_per_row", + "exact_distinct_operations_per_value_doubling", + "exact_distinct_operations_per_row_at_full_value_miss", + "exact_distinct_operations_per_row_at_full_pair_miss", + "exact_distinct_operations_per_inserted_value", + "exact_distinct_operations_per_inserted_pair", + "sketch_update_operations_per_counter", + "exact_merge_operations_per_state", + "extremum_merge_operations_per_state", + "counter_merge_operations_per_state", + "ddsketch_merge_operations_per_state", + "kll_merge_operations_per_state", + "kll_merge_operations_per_input_byte", + "kll_merge_operations_per_merged_byte", + "merge_operations_per_output_group", + "sketch_merge_operations_per_byte", + "kll_estimate_operations_per_byte", + "kll_estimate_operations_per_unsorted_item", + "sketch_estimate_operations_per_group", + "output_row_operations_per_group", + "sketch_chain_emit_operations_per_state", + "exact_chain_emit_operations_per_state", + "kll_merge_operations_per_output_group", + ]; + let actual: Vec = CostConstant::ALL + .iter() + .map(|constant| { + serde_json::to_string(constant) + .unwrap() + .trim_matches('"') + .to_string() + }) + .collect(); + assert_eq!(actual, expected); +} + /// The exact-distinct cache and size parameters: not operations per unit. const SHAPE_PARAMETERS: [&str; 4] = [ "EXACT_DISTINCT_FEWEST_VALUES", diff --git a/crates/cost/src/node/operations_per_unit.rs b/crates/cost/src/node/operations_per_unit.rs index 52002aaf3..b81157dea 100644 --- a/crates/cost/src/node/operations_per_unit.rs +++ b/crates/cost/src/node/operations_per_unit.rs @@ -59,6 +59,7 @@ use asap_types::ir::operator::AggIntent; use asap_types::ir::schema::FieldDataType; use asap_types::summary_type::{ExactKind, SketchAlgorithm, SketchParams}; +use crate::node::cost_terms::{CostConstant, NodeCostTerms}; use crate::StateFamily; /// A row kept by a time range over raw rows, or a state read by a time @@ -202,7 +203,7 @@ pub(crate) const CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL: f64 = 37.0; /// squared log error, chosen by leave-one-out error over the distinct /// counts, 0.107 against 0.200 for the absolute): 63.8 per row and 0.51 /// per compared hash, charged 65 and 0.50. -const KMV_UPDATE_OPERATIONS_PER_ROW: f64 = 65.0; +pub(crate) const KMV_UPDATE_OPERATIONS_PER_ROW: f64 = 65.0; /// Per retained hash a KMV update compares the row's hash with /// ([`kmv_compared_hashes_per_row`]): `asap_sketchlib` checks a new hash @@ -212,7 +213,7 @@ const KMV_UPDATE_OPERATIONS_PER_ROW: f64 = 65.0; /// 13,820 hashes per row, 6,975 ns charged, 7,410 measured; one state at /// k = 4,323 over all 300,000 rows fills early and compares 314, 222 ns /// charged, 191 measured. -const KMV_OPERATIONS_PER_COMPARED_HASH: f64 = 0.5; +pub(crate) const KMV_OPERATIONS_PER_COMPARED_HASH: f64 = 0.5; /// Per input row of an exact distinct count or frequency statistic, beyond /// its lookups and inserts below, the measure's own @@ -241,14 +242,14 @@ const KMV_OPERATIONS_PER_COMPARED_HASH: f64 = 0.5; /// where a constant, a term per square root of the values and one per /// inserted entry gave 0.61–1.58 (0.224). Fitted on one grid alone, the /// form predicts the other within 0.65–1.40. -const EXACT_DISTINCT_OPERATIONS_PER_ROW: f64 = 59.0; +pub(crate) const EXACT_DISTINCT_OPERATIONS_PER_ROW: f64 = 59.0; /// Per doubling of the distinct values an exact distinct count's value /// table holds past `EXACT_DISTINCT_FEWEST_VALUES` (at most /// `EXACT_DISTINCT_MOST_VALUES`): a larger table spreads its lookups over /// more cache lines (fitted 12.9). Example: 100,000 values are 6.6 /// doublings past 1,000, 86 per row. -const EXACT_DISTINCT_OPERATIONS_PER_VALUE_DOUBLING: f64 = 13.0; +pub(crate) const EXACT_DISTINCT_OPERATIONS_PER_VALUE_DOUBLING: f64 = 13.0; /// The fewest distinct values on the 10,000-series grid: fewer are charged as /// 1,000 (the earlier grid measured 60–92 ns per row over 100 values and @@ -265,7 +266,7 @@ const EXACT_DISTINCT_MOST_VALUES: f64 = 10_000_000.0; /// `EXACT_DISTINCT_VALUES_IN_CACHE`): a lookup in a value table that has /// outgrown the cache reads memory (fitted 207). Example: 1,000,000 values /// miss 0.8 of their lookups, 166 per row. -const EXACT_DISTINCT_OPERATIONS_PER_ROW_AT_FULL_VALUE_MISS: f64 = 207.0; +pub(crate) const EXACT_DISTINCT_OPERATIONS_PER_ROW_AT_FULL_VALUE_MISS: f64 = 207.0; /// The distinct values whose table still fits in the cache: the best by /// leave-one-setting-out error of 100,000, 200,000, 300,000, 500,000 and @@ -278,7 +279,7 @@ const EXACT_DISTINCT_VALUES_IN_CACHE: f64 = 200_000.0; /// so charged times the fraction that miss ([`cache_miss_fraction`] of the /// pairs over /// `EXACT_DISTINCT_PAIRS_IN_CACHE`, fitted 79.2). -const EXACT_DISTINCT_OPERATIONS_PER_ROW_AT_FULL_PAIR_MISS: f64 = 79.0; +pub(crate) const EXACT_DISTINCT_OPERATIONS_PER_ROW_AT_FULL_PAIR_MISS: f64 = 79.0; /// The (group, value) pairs whose set still fits in the cache: the best by /// leave-one-setting-out error of 500,000, 1,000,000, 3,000,000 and @@ -293,11 +294,11 @@ const EXACT_DISTINCT_PAIRS_IN_CACHE: f64 = 500_000.0; /// table, shared over its input rows: storing the value and growing the /// table (fitted 356). Example: 300,000 rows of 259,000 distinct values /// pay 307 per row for them. -const EXACT_DISTINCT_OPERATIONS_PER_INSERTED_VALUE: f64 = 356.0; +pub(crate) const EXACT_DISTINCT_OPERATIONS_PER_INSERTED_VALUE: f64 = 356.0; /// Per (group, value) pair an exact distinct count inserts into its set of /// pairs, shared over its input rows (fitted 13.3). -const EXACT_DISTINCT_OPERATIONS_PER_INSERTED_PAIR: f64 = 13.0; +pub(crate) const EXACT_DISTINCT_OPERATIONS_PER_INSERTED_PAIR: f64 = 13.0; /// The fraction of random lookups into a table of `entries` that miss a /// cache holding `entries_in_cache` of them: none while the table fits, @@ -324,22 +325,33 @@ pub(crate) fn exact_distinct_value_operations_per_row( distinct_values: u64, group_count: u64, rows: u64, -) -> f64 { +) -> NodeCostTerms { let values = distinct_values.max(1) as f64; let rows = rows.max(1) as f64; let groups = group_count.max(1) as f64; let pairs = groups * -values * (-(rows / groups) / values).exp_m1(); let table_values = values.clamp(EXACT_DISTINCT_FEWEST_VALUES, EXACT_DISTINCT_MOST_VALUES); - EXACT_DISTINCT_OPERATIONS_PER_ROW - + EXACT_DISTINCT_OPERATIONS_PER_VALUE_DOUBLING - * (table_values / EXACT_DISTINCT_FEWEST_VALUES).log2() - + EXACT_DISTINCT_OPERATIONS_PER_ROW_AT_FULL_VALUE_MISS - * cache_miss_fraction(table_values, EXACT_DISTINCT_VALUES_IN_CACHE) - + EXACT_DISTINCT_OPERATIONS_PER_ROW_AT_FULL_PAIR_MISS - * cache_miss_fraction(pairs, EXACT_DISTINCT_PAIRS_IN_CACHE) - + (EXACT_DISTINCT_OPERATIONS_PER_INSERTED_VALUE * values.min(rows) - + EXACT_DISTINCT_OPERATIONS_PER_INSERTED_PAIR * pairs) - / rows + NodeCostTerms::of(CostConstant::ExactDistinctOperationsPerRow, 1.0) + .plus( + CostConstant::ExactDistinctOperationsPerValueDoubling, + (table_values / EXACT_DISTINCT_FEWEST_VALUES).log2(), + ) + .plus( + CostConstant::ExactDistinctOperationsPerRowAtFullValueMiss, + cache_miss_fraction(table_values, EXACT_DISTINCT_VALUES_IN_CACHE), + ) + .plus( + CostConstant::ExactDistinctOperationsPerRowAtFullPairMiss, + cache_miss_fraction(pairs, EXACT_DISTINCT_PAIRS_IN_CACHE), + ) + .plus( + CostConstant::ExactDistinctOperationsPerInsertedValue, + values.min(rows) / rows, + ) + .plus( + CostConstant::ExactDistinctOperationsPerInsertedPair, + pairs / rows, + ) } /// One counter update of any other sketch, per counter its row updates @@ -358,7 +370,7 @@ pub(crate) const EXACT_MERGE_OPERATIONS_PER_STATE: f64 = 230.0; /// state over 100, 1,000 and 10,000 groups (a fit of 125 per state and /// 0.86 µs per group, the group's part in `MERGE_OPERATIONS_PER_OUTPUT_GROUP`), /// where a Sum or Count state measures about 230. -const EXTREMUM_MERGE_OPERATIONS_PER_STATE: f64 = 125.0; +pub(crate) const EXTREMUM_MERGE_OPERATIONS_PER_STATE: f64 = 125.0; /// One counter-increase accumulator state merged (rate, increase, irate): /// 346 ns per state over 360,000 states. @@ -412,7 +424,7 @@ pub(crate) const KLL_MERGE_OPERATIONS_PER_MERGED_BYTE: f64 = 0.43; /// output row (`OUTPUT_ROW_OPERATIONS_PER_GROUP`, 2,500): a merge's row /// takes its group's key columns and merged state as they are, where a /// finalize or an estimate also reads each state into a new value column. -const MERGE_OPERATIONS_PER_OUTPUT_GROUP: f64 = 1_100.0; +pub(crate) const MERGE_OPERATIONS_PER_OUTPUT_GROUP: f64 = 1_100.0; /// Per byte of any other sketch state merged, above /// `EXACT_MERGE_OPERATIONS_PER_STATE`: merging adds every counter of the @@ -471,14 +483,14 @@ pub(crate) const OUTPUT_ROW_OPERATIONS_PER_GROUP: f64 = 2_500.0; /// `OUTPUT_ROW_OPERATIONS_PER_GROUP` and equal to it: both build one row per /// group with its labels and series identity, here around a state, there /// around a value. -const SKETCH_CHAIN_EMIT_OPERATIONS_PER_STATE: f64 = 2_500.0; +pub(crate) const SKETCH_CHAIN_EMIT_OPERATIONS_PER_STATE: f64 = 2_500.0; /// Per fixed-size exact state a tumbling-window chain emits (sum, count, /// min, max, rate): 1.9 µs per Rate state, and at ingestion time /// 1.3–2.1 µs per Max state over 100 to 10,000 series. Less than a sketch /// state's (`SKETCH_CHAIN_EMIT_OPERATIONS_PER_STATE`): its state is a /// fixed record, not a sketch to box. -const EXACT_CHAIN_EMIT_OPERATIONS_PER_STATE: f64 = 1_500.0; +pub(crate) const EXACT_CHAIN_EMIT_OPERATIONS_PER_STATE: f64 = 1_500.0; /// Per output group of a KLL `SummaryMerge`, beyond /// `MERGE_OPERATIONS_PER_OUTPUT_GROUP` and its states' merges: creating the @@ -486,7 +498,7 @@ const EXACT_CHAIN_EMIT_OPERATIONS_PER_STATE: f64 = 1_500.0; /// of 5 KLL states of 60 rows per series (1,000 and 10,000 series) measures /// 32–33 µs per series where its states' merges are charged 14; over 60 /// states per series this adds 4% to the charge, measured 0.8x. -const KLL_MERGE_OPERATIONS_PER_OUTPUT_GROUP: f64 = 19_000.0; +pub(crate) const KLL_MERGE_OPERATIONS_PER_OUTPUT_GROUP: f64 = 19_000.0; /// The operations one input row costs folded into a state of `summary_type` /// with `parameters`, which folds `rows_per_state` rows (`None`: unknown) @@ -498,18 +510,25 @@ pub(crate) fn update_operations_per_row( parameters: Option<&SketchParams>, rows_per_state: Option, distinct_values: Option, -) -> f64 { +) -> NodeCostTerms { match (summary_type, parameters) { (FieldDataType::ExactAggregate(kind, _), _) if kind.tracks_counter_increase() => { - COUNTER_UPDATE_OPERATIONS_PER_ROW + NodeCostTerms::of(CostConstant::CounterUpdateOperationsPerRow, 1.0) + } + (FieldDataType::ExactAggregate(..), _) => { + NodeCostTerms::of(CostConstant::ExactUpdateOperationsPerRow, 1.0) + } + (_, Some(SketchParams::DDSketch { .. })) => { + NodeCostTerms::of(CostConstant::DdsketchUpdateOperationsPerRow, 1.0) + } + (_, Some(SketchParams::Kll { .. })) => { + NodeCostTerms::of(CostConstant::KllUpdateOperationsPerRow, 1.0) } - (FieldDataType::ExactAggregate(..), _) => EXACT_UPDATE_OPERATIONS_PER_ROW, - (_, Some(SketchParams::DDSketch { .. })) => DDSKETCH_UPDATE_OPERATIONS_PER_ROW, - (_, Some(SketchParams::Kll { .. })) => KLL_UPDATE_OPERATIONS_PER_ROW, (_, Some(SketchParams::Kmv { k })) => { - KMV_UPDATE_OPERATIONS_PER_ROW - + KMV_OPERATIONS_PER_COMPARED_HASH - * kmv_compared_hashes_per_row(*k, rows_per_state, distinct_values) + NodeCostTerms::of(CostConstant::KmvUpdateOperationsPerRow, 1.0).plus( + CostConstant::KmvOperationsPerComparedHash, + kmv_compared_hashes_per_row(*k, rows_per_state, distinct_values), + ) } ( _, @@ -522,12 +541,12 @@ pub(crate) fn update_operations_per_row( | SketchParams::UnivMon { .. } | SketchParams::Hll { .. }), ), - ) => { - EXACT_UPDATE_OPERATIONS_PER_ROW - + SKETCH_UPDATE_OPERATIONS_PER_COUNTER * counters_updated_per_row(parameters) - } - // A summary without sketch parameters (a sample): one counter. - (_, None) => EXACT_UPDATE_OPERATIONS_PER_ROW + SKETCH_UPDATE_OPERATIONS_PER_COUNTER, + ) => NodeCostTerms::of(CostConstant::ExactUpdateOperationsPerRow, 1.0).plus( + CostConstant::SketchUpdateOperationsPerCounter, + counters_updated_per_row(parameters), + ), + (_, None) => NodeCostTerms::of(CostConstant::ExactUpdateOperationsPerRow, 1.0) + .plus(CostConstant::SketchUpdateOperationsPerCounter, 1.0), } } @@ -625,29 +644,60 @@ pub(crate) fn merge_operations_per_state( rows_per_input_state: Option, input_states_per_group: u64, ) -> f64 { + merge_terms_per_state( + family, + state_bytes, + merged_state_bytes, + rows_per_input_state, + input_states_per_group, + ) + .operations() +} + +/// [`merge_operations_per_state`] as terms: the units of each constant one +/// input state's merge charges. +pub(crate) fn merge_terms_per_state( + family: StateFamily<'_>, + state_bytes: u64, + merged_state_bytes: u64, + rows_per_input_state: Option, + input_states_per_group: u64, +) -> NodeCostTerms { match family { StateFamily::Exact(kind) if kind.tracks_counter_increase() => { - COUNTER_MERGE_OPERATIONS_PER_STATE + NodeCostTerms::of(CostConstant::CounterMergeOperationsPerState, 1.0) + } + StateFamily::Exact(ExactKind::Min | ExactKind::Max) => { + NodeCostTerms::of(CostConstant::ExtremumMergeOperationsPerState, 1.0) + } + StateFamily::Exact(_) | StateFamily::None => { + NodeCostTerms::of(CostConstant::ExactMergeOperationsPerState, 1.0) + } + StateFamily::Sketch(SketchParams::DDSketch { .. }) => { + NodeCostTerms::of(CostConstant::DdsketchMergeOperationsPerState, 1.0) } - StateFamily::Exact(ExactKind::Min | ExactKind::Max) => EXTREMUM_MERGE_OPERATIONS_PER_STATE, - StateFamily::Exact(_) | StateFamily::None => EXACT_MERGE_OPERATIONS_PER_STATE, - StateFamily::Sketch(SketchParams::DDSketch { .. }) => DDSKETCH_MERGE_OPERATIONS_PER_STATE, StateFamily::Sketch(SketchParams::Kll { .. }) => { - KLL_MERGE_OPERATIONS_PER_STATE - + KLL_MERGE_OPERATIONS_PER_INPUT_BYTE * state_bytes as f64 - + KLL_MERGE_OPERATIONS_PER_MERGED_BYTE * merged_state_bytes as f64 + NodeCostTerms::of(CostConstant::KllMergeOperationsPerState, 1.0) + .plus( + CostConstant::KllMergeOperationsPerInputByte, + state_bytes as f64, + ) + .plus( + CostConstant::KllMergeOperationsPerMergedByte, + merged_state_bytes as f64, + ) } StateFamily::Sketch(SketchParams::Kmv { k }) => { let retained_hashes = rows_per_input_state.map_or(u64::from(*k), |rows| rows.min(u64::from(*k))); let hashes_per_group = retained_hashes.saturating_mul(input_states_per_group.max(1)); - retained_hashes as f64 - * update_operations_per_row( - &FieldDataType::Sketch(SketchAlgorithm::Kmv), - Some(&SketchParams::Kmv { k: *k }), - Some(hashes_per_group), - None, - ) + update_operations_per_row( + &FieldDataType::Sketch(SketchAlgorithm::Kmv), + Some(&SketchParams::Kmv { k: *k }), + Some(hashes_per_group), + None, + ) + .times(retained_hashes as f64) } StateFamily::Sketch( SketchParams::Hydra(_) @@ -657,9 +707,10 @@ pub(crate) fn merge_operations_per_state( | SketchParams::CountSketchWithHeap { .. } | SketchParams::UnivMon { .. } | SketchParams::Hll { .. }, - ) => { - EXACT_MERGE_OPERATIONS_PER_STATE + SKETCH_MERGE_OPERATIONS_PER_BYTE * state_bytes as f64 - } + ) => NodeCostTerms::of(CostConstant::ExactMergeOperationsPerState, 1.0).plus( + CostConstant::SketchMergeOperationsPerByte, + state_bytes as f64, + ), } } @@ -667,10 +718,17 @@ pub(crate) fn merge_operations_per_state( /// costs beyond its input states' merges ([`merge_operations_per_state`]): /// each group's state and output row. Example: 1,000 groups cost /// 1,100,000, of KLL states 20,100,000. +#[cfg(test)] pub(crate) fn merge_output_operations(family: StateFamily<'_>, output_groups: u64) -> f64 { + merge_output_terms(family, output_groups).operations() +} + +/// [`merge_output_operations`] as terms: units per output group. +pub(crate) fn merge_output_terms(family: StateFamily<'_>, output_groups: u64) -> NodeCostTerms { + let per_group = NodeCostTerms::of(CostConstant::MergeOperationsPerOutputGroup, 1.0); let per_group = match family { StateFamily::Sketch(SketchParams::Kll { .. }) => { - MERGE_OPERATIONS_PER_OUTPUT_GROUP + KLL_MERGE_OPERATIONS_PER_OUTPUT_GROUP + per_group.plus(CostConstant::KllMergeOperationsPerOutputGroup, 1.0) } StateFamily::Sketch( SketchParams::DDSketch { .. } @@ -684,9 +742,9 @@ pub(crate) fn merge_output_operations(family: StateFamily<'_>, output_groups: u6 | SketchParams::Kmv { .. }, ) | StateFamily::Exact(_) - | StateFamily::None => MERGE_OPERATIONS_PER_OUTPUT_GROUP, + | StateFamily::None => per_group, }; - output_groups as f64 * per_group + per_group.times(output_groups as f64) } /// The operations a `SummaryMerge` that reads one state per group costs, @@ -701,21 +759,28 @@ pub(crate) fn merge_output_operations(family: StateFamily<'_>, output_groups: u6 /// median 1.38–1.62x their charge (up to 2.29x for KLL states), at 100 and /// 1,000 groups 0.96–1.20x (`MERGE_OPERATIONS_PER_OUTPUT_GROUP`). Example: /// 1,000 groups cost 1,100,000. -pub(crate) fn pass_through_merge_operations(output_groups: u64) -> f64 { - output_groups as f64 * MERGE_OPERATIONS_PER_OUTPUT_GROUP +pub(crate) fn pass_through_merge_operations(output_groups: u64) -> NodeCostTerms { + NodeCostTerms::of( + CostConstant::MergeOperationsPerOutputGroup, + output_groups as f64, + ) } /// The operations per state a tumbling-window chain of `summary_type` /// costs to complete and emit each window's state: 1,500 for an exact /// accumulator, 2,500 for a sketch or any other summary. -pub(crate) fn chain_emit_operations_per_state(summary_type: &FieldDataType) -> f64 { +pub(crate) fn chain_emit_operations_per_state(summary_type: &FieldDataType) -> NodeCostTerms { match summary_type { - FieldDataType::ExactAggregate(..) => EXACT_CHAIN_EMIT_OPERATIONS_PER_STATE, + FieldDataType::ExactAggregate(..) => { + NodeCostTerms::of(CostConstant::ExactChainEmitOperationsPerState, 1.0) + } FieldDataType::Sketch(..) | FieldDataType::Plain(..) | FieldDataType::SummarySample(..) | FieldDataType::Wavelet(..) - | FieldDataType::StatModel(..) => SKETCH_CHAIN_EMIT_OPERATIONS_PER_STATE, + | FieldDataType::StatModel(..) => { + NodeCostTerms::of(CostConstant::SketchChainEmitOperationsPerState, 1.0) + } } } @@ -741,14 +806,21 @@ pub(crate) fn estimate_operations_per_group( family: StateFamily<'_>, state_bytes: u64, written_by: StateLastWrittenBy, -) -> f64 { - let read = match family { +) -> NodeCostTerms { + let output_row = NodeCostTerms::of(CostConstant::OutputRowOperationsPerGroup, 1.0); + match family { StateFamily::Sketch(SketchParams::Kll { k }) => match written_by { - StateLastWrittenBy::Merge => KLL_ESTIMATE_OPERATIONS_PER_BYTE * state_bytes as f64, + StateLastWrittenBy::Merge => output_row.plus( + CostConstant::KllEstimateOperationsPerByte, + state_bytes as f64, + ), StateLastWrittenBy::Updates { rows_per_state } => { let k = u64::from(*k); let unsorted_items = rows_per_state.map_or(k, |rows| rows.min(k)); - KLL_ESTIMATE_OPERATIONS_PER_UNSORTED_ITEM * unsorted_items as f64 + output_row.plus( + CostConstant::KllEstimateOperationsPerUnsortedItem, + unsorted_items as f64, + ) } }, StateFamily::Sketch( @@ -761,10 +833,9 @@ pub(crate) fn estimate_operations_per_group( | SketchParams::UnivMon { .. } | SketchParams::Hll { .. } | SketchParams::Kmv { .. }, - ) => SKETCH_ESTIMATE_OPERATIONS_PER_GROUP, - StateFamily::Exact(_) | StateFamily::None => 0.0, - }; - read + OUTPUT_ROW_OPERATIONS_PER_GROUP + ) => output_row.plus(CostConstant::SketchEstimateOperationsPerGroup, 1.0), + StateFamily::Exact(_) | StateFamily::None => output_row, + } } /// The operations one input row costs a hash aggregate with @@ -782,28 +853,31 @@ pub(crate) fn hash_aggregate_operations_per_row( rows: u64, group_count: u64, distinct_values: u64, -) -> f64 { +) -> NodeCostTerms { let rows_per_group = rows.div_ceil(group_count.max(1)); let keys = match grouping_key_count { - 0 => 0.0, - keys => { - HASH_AGGREGATE_OPERATIONS_PER_ROW - + (keys - 1) as f64 * HASH_AGGREGATE_OPERATIONS_PER_FURTHER_KEY - } + 0 => NodeCostTerms::default(), + keys => NodeCostTerms::of(CostConstant::HashAggregateOperationsPerRow, 1.0).plus( + CostConstant::HashAggregateOperationsPerFurtherKey, + (keys - 1) as f64, + ), }; - keys + measures + let measures = measures .iter() - .map(|measure| { - aggregate_measure_operations_per_row( + .fold(NodeCostTerms::default(), |terms, measure| { + terms.plus_terms(&aggregate_measure_operations_per_row( measure, rows, group_count, rows_per_group, distinct_values, - ) - }) - .sum::() - .max(1.0) + )) + }); + // At least one operation per row for the measures: an aggregate + // without measures charges it without a constant. + let missing = (1.0 - measures.operations()).max(0.0); + keys.plus_terms(&measures) + .plus_terms(&NodeCostTerms::without_constant(missing)) } /// The operations one input row costs a hash aggregate's `measure` over @@ -831,26 +905,28 @@ fn aggregate_measure_operations_per_row( group_count: u64, rows_per_group: u64, distinct_values: u64, -) -> f64 { +) -> NodeCostTerms { + let measure_row = NodeCostTerms::of(CostConstant::AggregateMeasureOperationsPerRow, 1.0); match measure { - AggIntent::Quantile { .. } => { - AGGREGATE_MEASURE_OPERATIONS_PER_ROW - + (rows_per_group.max(2) as f64).log2() * SORT_OPERATIONS_PER_ROW_PER_LEVEL - } + AggIntent::Quantile { .. } => measure_row.plus( + CostConstant::SortOperationsPerRowPerLevel, + (rows_per_group.max(2) as f64).log2(), + ), AggIntent::Cardinality { .. } | AggIntent::FrequencyL2 { .. } - | AggIntent::FrequencyEntropy { .. } => { - AGGREGATE_MEASURE_OPERATIONS_PER_ROW - + exact_distinct_value_operations_per_row(distinct_values, group_count, rows) - } + | AggIntent::FrequencyEntropy { .. } => measure_row.plus_terms( + &exact_distinct_value_operations_per_row(distinct_values, group_count, rows), + ), AggIntent::TopK { .. } => { - AGGREGATE_MEASURE_OPERATIONS_PER_ROW + HASH_AGGREGATE_OPERATIONS_PER_FURTHER_KEY + measure_row.plus(CostConstant::HashAggregateOperationsPerFurtherKey, 1.0) } AggIntent::Rate | AggIntent::IRate | AggIntent::Increase | AggIntent::Deriv - | AggIntent::PredictLinear { .. } => COUNTER_UPDATE_OPERATIONS_PER_ROW, + | AggIntent::PredictLinear { .. } => { + NodeCostTerms::of(CostConstant::CounterUpdateOperationsPerRow, 1.0) + } AggIntent::Count | AggIntent::Sum { .. } | AggIntent::Min { .. } @@ -885,6 +961,6 @@ fn aggregate_measure_operations_per_row( | AggIntent::TsOfMaxOverTime | AggIntent::TsOfFirstOverTime | AggIntent::TsOfLastOverTime - | AggIntent::Extension { .. } => AGGREGATE_MEASURE_OPERATIONS_PER_ROW, + | AggIntent::Extension { .. } => measure_row, } } diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index 768e7d8b1..a7f8aa1fe 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -20,15 +20,13 @@ use std::collections::{BTreeMap, BTreeSet}; use crate::node::analytical::{ - estimate_operator, AnalyticalCostError, PhysicalOperator, ResourceEstimate, + estimate_operator_with_terms, AnalyticalCostError, PhysicalOperator, ResourceEstimate, }; +use crate::node::cost_terms::{CostConstant, NodeCostTerms}; use crate::node::operations_per_unit::{ chain_emit_operations_per_state, estimate_operations_per_group, - hash_aggregate_operations_per_row, merge_operations_per_state, merge_output_operations, + hash_aggregate_operations_per_row, merge_output_terms, merge_terms_per_state, pass_through_merge_operations, update_operations_per_row, StateLastWrittenBy, - CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL, CHAIN_OPERATIONS_PER_ARRIVING_ROW, - OPERATIONS_PER_GROUP_PER_RAW_BATCH, OUTPUT_ROW_OPERATIONS_PER_GROUP, - TIME_RANGE_OPERATIONS_PER_ROW, }; use crate::node::operator_statistics::{ EdgeStatistics, OperatorStatistics, PartitionStatistics, UnaryEdgeStatistics, @@ -391,6 +389,22 @@ pub fn node_resource_usage_and_cost<'a>( data: &DataWorkload, settings: &'a CostInputs, ) -> Result, AnalyticalCostError> { + node_usage_and_cost_terms(node, producers, rate, window_ms, data, settings) + .map(|(usage, _)| usage) +} + +/// [`node_resource_usage_and_cost`] and the terms of its formula CPU per +/// execution ([`crate::node_cost_terms`]). The formula CPU is computed from +/// those terms, so the two cannot drift; the terms are taken before a +/// measured entry or the calibration factor replaces or scales it. +pub(crate) fn node_usage_and_cost_terms<'a>( + node: &'a asap_types::plan::physical_asap_dag::PhysicalASAPDAGNode, + producers: &[ProducerOutput<'a>], + rate: (u64, f64), + window_ms: Option, + data: &DataWorkload, + settings: &'a CostInputs, +) -> Result<(NodeResourceUsageAndCost<'a>, NodeCostTerms), 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; @@ -775,7 +789,7 @@ pub fn node_resource_usage_and_cost<'a>( })?, }; let out = edge(rows.max(1))?; - let estimate = estimate_operator( + let estimate = estimate_operator_with_terms( PhysicalOperator::Scan, OperatorStatistics::Scan { edges: UnaryEdgeStatistics { @@ -854,16 +868,20 @@ pub fn node_resource_usage_and_cost<'a>( .chain([input.rows]) .min() .unwrap_or(input.rows); - let operations_per_row = - hash_aggregate_operations_per_row( - keys, - measures, - input.rows, - group_count, - distinct_values, - ) + carried_label_operations_per_row(&node.output_schema, reduction); + let terms_per_row = hash_aggregate_operations_per_row( + keys, + measures, + input.rows, + group_count, + distinct_values, + ) + .plus_terms(&carried_label_operations_per_row( + &node.output_schema, + reduction, + )); + let operations_per_row = terms_per_row.operations(); let group_visits = visits(group_count); - let estimate = estimate_operator( + let estimate = estimate_operator_with_terms( PhysicalOperator::HashAggregate { grouping_key_count: keys, operations_per_row: operations_per_row.ceil() as u64, @@ -887,12 +905,20 @@ pub fn node_resource_usage_and_cost<'a>( .max(8), }, ) - .map(|estimate| { - ResourceEstimate::new( - estimate.cpu_ops() + group_visits * OPERATIONS_PER_GROUP_PER_RAW_BATCH, - estimate.peak_memory_bytes(), - estimate.scan_bytes(), - ) + .map(|(estimate, operator_terms)| { + // The operator charges its rows times the per-row + // operations rounded up, without a constant: split + // into the per-row terms and the rounding. + let rounding = operator_terms.operations_without_constant + - input.rows as f64 * operations_per_row; + let terms = NodeCostTerms { + operations_without_constant: 0.0, + ..operator_terms + } + .plus_terms(&terms_per_row.times(input.rows as f64)) + .plus_terms(&NodeCostTerms::without_constant(rounding)) + .plus(CostConstant::OperationsPerGroupPerRawBatch, group_visits); + charged(terms, estimate.peak_memory_bytes(), estimate.scan_bytes()) }); ( out, @@ -912,7 +938,7 @@ pub fn node_resource_usage_and_cost<'a>( } else { estimated_groups(partition_by)? }; - let estimate = estimate_operator( + let estimate = estimate_operator_with_terms( PhysicalOperator::InMemoryComparisonSort { ordering_key_count: keys.len() as u64, partitioned: partitions > 1, @@ -942,7 +968,7 @@ pub fn node_resource_usage_and_cost<'a>( let limit = n.map_or(u64::MAX, |n| (n as u64).saturating_mul(partitions)); let offset = (*offset as u64).saturating_mul(partitions); let out = edge(input.rows.saturating_sub(offset).min(limit))?; - let estimate = estimate_operator( + let estimate = estimate_operator_with_terms( PhysicalOperator::Limit { limit, offset }, OperatorStatistics::Limit { edges: unary(out) }, ); @@ -964,7 +990,7 @@ pub fn node_resource_usage_and_cost<'a>( }; ( out, - estimate_operator( + estimate_operator_with_terms( PhysicalOperator::TimeShift, OperatorStatistics::PassThrough { edges: unary(input), @@ -990,7 +1016,7 @@ pub fn node_resource_usage_and_cost<'a>( .ok_or(AnalyticalCostError::Overflow)?, }; // It reads those windows' states from the chain's store. - let estimate = estimate_operator( + let estimate = estimate_operator_with_terms( PhysicalOperator::TimeRange, OperatorStatistics::PassThrough { edges: UnaryEdgeStatistics { @@ -1026,7 +1052,7 @@ pub fn node_resource_usage_and_cost<'a>( read_raw_rows_span_ms = Some( input_raw_rows_span_ms.map_or(range_ms, |input_ms| input_ms.min(range_ms)), ); - let estimate = estimate_operator( + let estimate = estimate_operator_with_terms( PhysicalOperator::TimeRange, OperatorStatistics::PassThrough { edges: unary(out) }, ); @@ -1053,7 +1079,7 @@ pub fn node_resource_usage_and_cost<'a>( | NonASAPOp::PromqlInfoEnrich { .. } | NonASAPOp::PromqlSeriesSample { .. } | NonASAPOp::PromqlSubquery { .. } => { - let estimate = estimate_operator( + let estimate = estimate_operator_with_terms( PhysicalOperator::PassThrough, OperatorStatistics::PassThrough { edges: unary(input), @@ -1089,12 +1115,17 @@ pub fn node_resource_usage_and_cost<'a>( input_rows, )?); assumed_data_facts.extend(assumed_magnitude_range_fact(column, input_rows)); - let update_ops = update_operations_per_row( + let update_terms = update_operations_per_row( family, node.parameters.as_ref(), input_rows.rows_per_state, folded_distinct_values(update), - ) + carried_label_operations_per_row(&node.output_schema, reduction); + ) + .plus_terms(&carried_label_operations_per_row( + &node.output_schema, + reduction, + )); + let update_ops = update_terms.operations(); let out = EdgeStatistics { rows: group_count, bytes: group_count @@ -1102,14 +1133,12 @@ pub fn node_resource_usage_and_cost<'a>( .ok_or(AnalyticalCostError::Overflow)?, }; let group_visits = visits(group_count); + let terms = update_terms + .times(input.rows as f64) + .plus(CostConstant::OperationsPerGroupPerRawBatch, group_visits); ( out, - Ok(ResourceEstimate::new( - input.rows as f64 * update_ops - + group_visits * OPERATIONS_PER_GROUP_PER_RAW_BATCH, - out.bytes, - 0, - )), + Ok(charged(terms, out.bytes, 0)), format!( "build {} into {group_count} states: {} rows x {update_ops} ops, \ {group_visits:.0} group visits", @@ -1159,12 +1188,17 @@ pub fn node_resource_usage_and_cost<'a>( input_rows, )?); assumed_data_facts.extend(assumed_magnitude_range_fact(column, input_rows)); - let update_ops = update_operations_per_row( + let update_terms = update_operations_per_row( family, node.parameters.as_ref(), input_rows.rows_per_state, folded_distinct_values(update), - ) + carried_label_operations_per_row(&node.output_schema, reduction); + ) + .plus_terms(&carried_label_operations_per_row( + &node.output_schema, + reduction, + )); + let update_ops = update_terms.operations(); // The states it completes in one execution: at ingestion time // one second's share of each group's window (a 10 s window: a // tenth of a state per group); at query time every retained @@ -1179,11 +1213,10 @@ pub fn node_resource_usage_and_cost<'a>( // window's range, the work a `TimeRange` over the same rows does // (11 per row); at ingestion time each arriving row is routed to // its window's state (14 per row). - let range_ops = input.rows as f64 - * match ingestion { - true => CHAIN_OPERATIONS_PER_ARRIVING_ROW, - false => TIME_RANGE_OPERATIONS_PER_ROW, - }; + let range_constant = match ingestion { + true => CostConstant::ChainOperationsPerArrivingRow, + false => CostConstant::TimeRangeOperationsPerRow, + }; // Each group's state found once per raw batch. let group_visits = visits(group_count); let out = EdgeStatistics { @@ -1194,11 +1227,12 @@ pub fn node_resource_usage_and_cost<'a>( }; ( out, - Ok(ResourceEstimate::new( - input.rows as f64 * update_ops - + range_ops - + group_visits * OPERATIONS_PER_GROUP_PER_RAW_BATCH - + emitted_states * chain_emit_operations_per_state(family), + Ok(charged( + update_terms + .times(input.rows as f64) + .plus(range_constant, input.rows as f64) + .plus(CostConstant::OperationsPerGroupPerRawBatch, group_visits) + .plus_terms(&chain_emit_operations_per_state(family).times(emitted_states)), out.bytes, 0, )), @@ -1265,26 +1299,25 @@ pub fn node_resource_usage_and_cost<'a>( .iter() .all(|producer| keys_are_input_keys(keys, producer.output_schema)) }); - let merge_ops: f64 = match keeps_input_groups && states <= group_count { + let merge_terms = match keeps_input_groups && states <= group_count { true => pass_through_merge_operations(group_count), - false => { - producers - .iter() - .map(|producer| { - producer.output.rows as f64 - * merge_operations_per_state( - family, - input_state_bytes(producer), - merged_state_bytes, - producer - .state_input_rows - .and_then(|rows| rows.rows_per_state), - states.div_ceil(group_count.max(1)), - ) - }) - .sum::() - + merge_output_operations(family, group_count) - } + false => producers + .iter() + .fold(NodeCostTerms::default(), |terms, producer| { + terms.plus_terms( + &merge_terms_per_state( + family, + input_state_bytes(producer), + merged_state_bytes, + producer + .state_input_rows + .and_then(|rows| rows.rows_per_state), + states.div_ceil(group_count.max(1)), + ) + .times(producer.output.rows as f64), + ) + }) + .plus_terms(&merge_output_terms(family, group_count)), }; let out = EdgeStatistics { rows: group_count, @@ -1294,7 +1327,7 @@ pub fn node_resource_usage_and_cost<'a>( }; ( out, - Ok(ResourceEstimate::new(merge_ops, out.bytes, 0)), + Ok(charged(merge_terms, out.bytes, 0)), format!("merge {states} states into {group_count}"), ) } @@ -1329,9 +1362,9 @@ pub fn node_resource_usage_and_cost<'a>( )?; ( out, - Ok(ResourceEstimate::new( - input.rows as f64 - * estimate_operations_per_group(family, input_state_bytes(), written_by), + Ok(charged( + estimate_operations_per_group(family, input_state_bytes(), written_by) + .times(input.rows as f64), 0, 0, )), @@ -1341,8 +1374,8 @@ pub fn node_resource_usage_and_cost<'a>( Payload::ASAP(ASAPOp::FinalizeExactAccumulator { .. }) => ( edge(input.rows)?, // An exact accumulator's read is its output row alone. - Ok(ResourceEstimate::new( - input.rows as f64 * OUTPUT_ROW_OPERATIONS_PER_GROUP, + Ok(charged( + NodeCostTerms::of(CostConstant::OutputRowOperationsPerGroup, input.rows as f64), 0, 0, )), @@ -1357,11 +1390,15 @@ pub fn node_resource_usage_and_cost<'a>( | ASAPOp::Extension { .. }, ) => ( edge(input.rows)?, - Ok(ResourceEstimate::new(input.rows as f64, 0, 0)), + Ok(charged( + NodeCostTerms::without_constant(input.rows as f64), + 0, + 0, + )), format!("{} rows", input.rows), ), }; - let estimate = estimate?; + let (estimate, terms) = estimate?; 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; @@ -1610,7 +1647,7 @@ pub fn node_resource_usage_and_cost<'a>( .then_some(input_raw_rows_span_ms) .flatten() }); - Ok(NodeResourceUsageAndCost { + let usage = NodeResourceUsageAndCost { output: out, sources, family, @@ -1638,7 +1675,21 @@ pub fn node_resource_usage_and_cost<'a>( cost_source, assumed_data_facts, }, - }) + }; + Ok((usage, terms)) +} + +/// A formula estimate whose CPU operations are `terms` at the built-in +/// values, holding `peak_memory_bytes` and scanning `scan_bytes`. +fn charged( + terms: NodeCostTerms, + peak_memory_bytes: u64, + scan_bytes: u64, +) -> (ResourceEstimate, NodeCostTerms) { + ( + ResourceEstimate::new(terms.operations(), peak_memory_bytes, scan_bytes), + terms, + ) } /// [`PlanCost::source`] for nodes costed as `per_node` says. @@ -1724,14 +1775,20 @@ fn grouping_key_count(reduction: &Reduction) -> u64 { /// the per-series sum under `sum by (job) (sum_over_time(latency[5m]))` /// outputs the series identity (its key) and `job`, so it carries one /// label: 37 operations per row. -fn carried_label_operations_per_row(output_schema: &Schema, reduction: &Reduction) -> f64 { +fn carried_label_operations_per_row( + output_schema: &Schema, + reduction: &Reduction, +) -> NodeCostTerms { let label_columns = output_schema .fields .iter() .filter(|field| field.dtype == FieldDataType::Plain(asap_types::ir::schema::DataType::Utf8)) .count() as u64; let carried = label_columns.saturating_sub(grouping_key_count(reduction)); - carried as f64 * CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL + NodeCostTerms::of( + CostConstant::CarriedLabelOperationsPerRowPerLabel, + carried as f64, + ) } /// The input states a `SummaryMerge` over `producers` reads: every row of diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index da5ca611f..d19c368c6 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -3225,8 +3225,8 @@ fn terms_reproduce_the_formula_cpu_of_every_costed_node() { } /// A refit of one constant changes the charge by its units times the -/// change, which is `fitlib.charge`'s rule: the per-series Max aggregate's -/// 13.6 M with 140 operations per group visit instead of 170 is +/// change, i.e. the new charge is the old charge plus the change times the +/// units: the per-series Max aggregate's 13.6 M with 140 operations per group visit instead of 170 is /// 13.6 M − 30 × 30,000 = 12.7 M. #[test] fn replacing_a_constant_changes_the_charge_by_its_units() { From 5f080872bc417213080df5c3cd3027c6be54fe7a Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Tue, 17 Nov 2026 06:40:03 +0000 Subject: [PATCH 3/3] fix(cost): node_cost_terms review fixes: the charge keeps the formula's summation order; terms-named helpers; no test-only wrappers NodeCostTerms carries the charge at the built-in values, added in the order each formula adds its terms, so every node's CPU is the same f64 as before the terms existed and tied plans no longer flip on summation order. The exact-distinct insert terms are divided by the rows as a whole (divided_by), and the hash aggregate attributes its per-row terms to their constants (attributing_to_constants) instead of back-solving the rounding. Helpers returning terms are named *_terms*; estimate_operator, merge_operations_per_state, merge_output_operations and CostConstant::ALL are deleted (tests enumerate the constants from their JSON names). The extension guide says a new per-unit constant needs a CostConstant variant. --- crates/cost/src/node/analytical.rs | 115 ++++++------- crates/cost/src/node/cost_terms.rs | 87 +++++----- crates/cost/src/node/cost_terms/tests.rs | 153 ++++++++++++------ crates/cost/src/node/operations_per_unit.rs | 63 +++----- crates/cost/src/node/usage/mod.rs | 54 +++---- crates/cost/src/node/usage/tests.rs | 13 +- crates/cost/src/plan/cost.rs | 6 +- .../architecture/physical-plan-integration.md | 2 +- .../analytical-resource-cost.md | 2 +- .../develop_docs/extend-asap-aware-mapping.md | 10 +- 10 files changed, 262 insertions(+), 243 deletions(-) diff --git a/crates/cost/src/node/analytical.rs b/crates/cost/src/node/analytical.rs index 5b7a96e42..047b2eadb 100644 --- a/crates/cost/src/node/analytical.rs +++ b/crates/cost/src/node/analytical.rs @@ -99,7 +99,7 @@ pub(crate) enum PhysicalOperator { grouping_key_count: u64, /// CPU operations one input row costs, its grouping keys and /// measures together - /// (`operations_per_unit::hash_aggregate_operations_per_row`): + /// (`operations_per_unit::hash_aggregate_terms_per_row`): /// 27 for a per-series `max_over_time` (one key, one measure). operations_per_row: u64, }, @@ -371,15 +371,6 @@ fn validate_partitioning( Ok(()) } -/// [`estimate_operator_with_terms`] without its terms. -#[cfg(test)] -pub(crate) fn estimate_operator( - operator: PhysicalOperator, - statistics: OperatorStatistics, -) -> Result { - estimate_operator_with_terms(operator, statistics).map(|(estimate, _)| estimate) -} - /// Estimate one physical operator. Child costs are deliberately excluded; /// a DAG walker sums CPU/disk once per node and combines simultaneously /// retained state separately. Its CPU operations are also returned as @@ -1625,31 +1616,33 @@ mod tests { /// operations each), and keeps no more than it reads. #[test] fn time_shift_is_free_and_time_range_is_priced_by_the_rows_it_keeps() { - let shift = estimate_operator( + let shift = estimate_operator_with_terms( PhysicalOperator::TimeShift, OperatorStatistics::PassThrough { edges: unary_row_edges(1_000, 1_000), }, ) - .unwrap(); + .unwrap() + .0; assert_eq!(shift.cpu_ops(), 0.0); assert_eq!(shift.scan_bytes(), 0); - let range = estimate_operator( + let range = estimate_operator_with_terms( PhysicalOperator::TimeRange, OperatorStatistics::PassThrough { edges: unary_row_edges(1_000, 200), }, ) - .unwrap(); + .unwrap() + .0; assert_eq!(range.cpu_ops(), 200.0 * TIME_RANGE_OPERATIONS_PER_ROW); - assert!(estimate_operator( + assert!(estimate_operator_with_terms( PhysicalOperator::TimeRange, OperatorStatistics::PassThrough { edges: unary_row_edges(200, 1_000), }, ) .is_err()); - assert!(estimate_operator( + assert!(estimate_operator_with_terms( PhysicalOperator::TimeShift, OperatorStatistics::PassThrough { edges: unary_row_edges(1_000, 200), @@ -1741,20 +1734,20 @@ mod tests { build_side: Some(HashJoinBuildSide::Right), }; - assert!(estimate_operator( + assert!(estimate_operator_with_terms( operator(PromqlBinaryOperation::Or), promql_binary_statistics(150, 15), ) .is_ok()); assert!(matches!( - estimate_operator( + estimate_operator_with_terms( operator(PromqlBinaryOperation::ArithmeticOrComparison), promql_binary_statistics(60, 5), ), Err(AnalyticalCostError::InconsistentOperatorStatistics(_)) )); assert!(matches!( - estimate_operator( + estimate_operator_with_terms( operator(PromqlBinaryOperation::And), promql_binary_statistics(101, 10), ), @@ -1777,7 +1770,7 @@ mod tests { }, promql: Some(PromqlUnaryEdgeStatistics { input, output }), }; - assert!(estimate_operator( + assert!(estimate_operator_with_terms( PhysicalOperator::PromqlScalarToVector, OperatorStatistics::PromqlScalarToVector { edges: edges(scalar, vector), @@ -1785,7 +1778,7 @@ mod tests { ) .is_ok()); assert!(matches!( - estimate_operator( + estimate_operator_with_terms( PhysicalOperator::PromqlScalarToVector, OperatorStatistics::PromqlScalarToVector { edges: edges(vector, scalar), @@ -1804,7 +1797,9 @@ mod tests { }, promql_output: promql_edge(0, 10, PromqlValueKind::Scalar), }; - let estimate = estimate_operator(PhysicalOperator::PromqlScalarLeaf, statistics).unwrap(); + let estimate = estimate_operator_with_terms(PhysicalOperator::PromqlScalarLeaf, statistics) + .unwrap() + .0; assert_eq!(estimate.cpu_ops(), 10.0); assert_eq!(estimate.peak_memory_bytes(), 8); @@ -1832,7 +1827,7 @@ mod tests { }; assert_eq!( - estimate_operator( + estimate_operator_with_terms( PhysicalOperator::HashAggregate { grouping_key_count: 0, operations_per_row: 1, @@ -1866,7 +1861,7 @@ mod tests { }, }; - assert!(estimate_operator( + assert!(estimate_operator_with_terms( PhysicalOperator::PromqlPresence { kind: PromqlPresenceKind::PresentPerSeries, operations_per_row: 1, @@ -1875,7 +1870,7 @@ mod tests { ) .is_ok()); assert!(matches!( - estimate_operator( + estimate_operator_with_terms( PhysicalOperator::PromqlPresence { kind: PromqlPresenceKind::Absent, operations_per_row: 1, @@ -1889,7 +1884,7 @@ mod tests { #[test] fn operator_estimator_rejects_contradictory_cardinality_evidence() { assert!(matches!( - estimate_operator( + estimate_operator_with_terms( filter_operator(), OperatorStatistics::Filter { edges: unary_row_edges(10, 11), @@ -1899,7 +1894,7 @@ mod tests { Err(AnalyticalCostError::InconsistentOperatorStatistics(_)) )); assert!(matches!( - estimate_operator( + estimate_operator_with_terms( project_operator(), OperatorStatistics::Project { edges: unary_row_edges(10, 9), @@ -1910,7 +1905,7 @@ mod tests { )); assert!(matches!( - estimate_operator( + estimate_operator_with_terms( aggregate_operator(), OperatorStatistics::HashAggregate { edges: unary_row_edges(10, 3), @@ -1923,7 +1918,7 @@ mod tests { )); assert!(matches!( - estimate_operator( + estimate_operator_with_terms( PhysicalOperator::TopK { limit: 3, offset: 0, @@ -1939,7 +1934,7 @@ mod tests { #[test] fn physical_operator_formulas_keep_disk_at_scan_and_require_join_stats() { - let scan = estimate_operator( + let scan = estimate_operator_with_terms( PhysicalOperator::Scan, OperatorStatistics::Scan { source_read_bytes: 64_000, @@ -1956,10 +1951,11 @@ mod tests { }, }, ) - .unwrap(); + .unwrap() + .0; assert_eq!(scan.scan_bytes(), 64_000); - let topk = estimate_operator( + let topk = estimate_operator_with_terms( PhysicalOperator::TopK { limit: 10, offset: 0, @@ -1979,12 +1975,13 @@ mod tests { }, }, ) - .unwrap(); + .unwrap() + .0; assert_eq!(topk.scan_bytes(), 0); assert_eq!(topk.cpu_ops(), 4_000.0); assert_eq!(topk.peak_memory_bytes(), 400); - let mismatched_join_statistics = estimate_operator( + let mismatched_join_statistics = estimate_operator_with_terms( PhysicalOperator::HashJoin { build_side: HashJoinBuildSide::Left, equality_key_count: 1, @@ -2001,7 +1998,7 @@ mod tests { )) ); - let build_left = estimate_operator( + let build_left = estimate_operator_with_terms( PhysicalOperator::HashJoin { build_side: HashJoinBuildSide::Left, equality_key_count: 1, @@ -2026,10 +2023,11 @@ mod tests { }, }, ) - .unwrap(); + .unwrap() + .0; assert_eq!(build_left.peak_memory_bytes(), 80_000); - let aggregate = estimate_operator( + let aggregate = estimate_operator_with_terms( aggregate_operator(), OperatorStatistics::HashAggregate { group_count: 100, @@ -2048,10 +2046,11 @@ mod tests { }, }, ) - .unwrap(); + .unwrap() + .0; assert_eq!(aggregate.peak_memory_bytes(), 5_600); - let oversized_topk = estimate_operator( + let oversized_topk = estimate_operator_with_terms( PhysicalOperator::TopK { limit: 1_000, offset: 0, @@ -2071,10 +2070,11 @@ mod tests { }, }, ) - .unwrap(); + .unwrap() + .0; assert_eq!(oversized_topk.cpu_ops(), 8.0); - let offset_limit = estimate_operator( + let offset_limit = estimate_operator_with_terms( PhysicalOperator::Limit { limit: 10, offset: 900_000, @@ -2093,7 +2093,8 @@ mod tests { }, }, ) - .unwrap(); + .unwrap() + .0; assert_eq!(offset_limit.cpu_ops(), 900_010.0); } @@ -2111,14 +2112,15 @@ mod tests { rows: 100, bytes: 10_000, }; - let estimate = estimate_operator( + let estimate = estimate_operator_with_terms( PhysicalOperator::Scan, OperatorStatistics::Scan { edges: unary_edges(logical, logical), source_read_bytes: 2_500, }, ) - .unwrap(); + .unwrap() + .0; assert_eq!(estimate.scan_bytes(), 2_500); assert_eq!(estimate.peak_memory_bytes(), 100); @@ -2136,8 +2138,9 @@ mod tests { }; assert_eq!( - estimate_operator(filter_operator(), filter) + estimate_operator_with_terms(filter_operator(), filter) .unwrap() + .0 .scan_bytes(), 0 ); @@ -2150,7 +2153,7 @@ mod tests { bytes: 80, }; assert_eq!( - estimate_operator( + estimate_operator_with_terms( PhysicalOperator::Scan, OperatorStatistics::Scan { edges: unary_edges(logical, logical), @@ -2166,7 +2169,7 @@ mod tests { #[test] fn hash_aggregate_handles_empty_grouped_and_ungrouped_inputs() { let empty = EdgeStatistics { rows: 0, bytes: 0 }; - let ungrouped = estimate_operator( + let ungrouped = estimate_operator_with_terms( PhysicalOperator::HashAggregate { grouping_key_count: 0, operations_per_row: 1, @@ -2178,12 +2181,13 @@ mod tests { accumulator_bytes_per_group: 8, }, ) - .unwrap(); + .unwrap() + .0; // No row to fold; its one output row is still built. assert_eq!(ungrouped.cpu_ops(), OUTPUT_ROW_OPERATIONS_PER_GROUP); assert_eq!(ungrouped.peak_memory_bytes(), 24); - let grouped = estimate_operator( + let grouped = estimate_operator_with_terms( PhysicalOperator::HashAggregate { grouping_key_count: 1, operations_per_row: 1, @@ -2195,7 +2199,8 @@ mod tests { accumulator_bytes_per_group: 8, }, ) - .unwrap(); + .unwrap() + .0; assert_eq!(grouped.peak_memory_bytes(), 0); } @@ -2210,18 +2215,19 @@ mod tests { scalar_inputs: vec![], }; assert_eq!( - estimate_operator( + estimate_operator_with_terms( PhysicalOperator::Filter { predicate_operations_per_row: 3, }, filter, ) .unwrap() + .0 .cpu_ops(), 300.0 ); - let sort = estimate_operator( + let sort = estimate_operator_with_terms( PhysicalOperator::InMemoryComparisonSort { ordering_key_count: 1, partitioned: true, @@ -2242,7 +2248,8 @@ mod tests { }, }, ) - .unwrap(); + .unwrap() + .0; assert_eq!(sort.cpu_ops(), 600.0); assert_eq!(sort.peak_memory_bytes(), 400); } @@ -2250,7 +2257,7 @@ mod tests { #[test] fn concat_requires_at_least_one_physical_input() { assert_eq!( - estimate_operator( + estimate_operator_with_terms( PhysicalOperator::Concat, OperatorStatistics::Concat { inputs: vec![], diff --git a/crates/cost/src/node/cost_terms.rs b/crates/cost/src/node/cost_terms.rs index 902767bed..dbdef16b7 100644 --- a/crates/cost/src/node/cost_terms.rs +++ b/crates/cost/src/node/cost_terms.rs @@ -71,47 +71,6 @@ pub enum CostConstant { } impl CostConstant { - /// Every constant, in declaration order. - pub const ALL: &'static [CostConstant] = &[ - CostConstant::TimeRangeOperationsPerRow, - CostConstant::OperationsPerGroupPerRawBatch, - CostConstant::ChainOperationsPerArrivingRow, - CostConstant::HashAggregateOperationsPerRow, - CostConstant::HashAggregateOperationsPerFurtherKey, - CostConstant::AggregateMeasureOperationsPerRow, - CostConstant::SortOperationsPerRowPerLevel, - CostConstant::DdsketchUpdateOperationsPerRow, - CostConstant::KllUpdateOperationsPerRow, - CostConstant::ExactUpdateOperationsPerRow, - CostConstant::CounterUpdateOperationsPerRow, - CostConstant::CarriedLabelOperationsPerRowPerLabel, - CostConstant::KmvUpdateOperationsPerRow, - CostConstant::KmvOperationsPerComparedHash, - CostConstant::ExactDistinctOperationsPerRow, - CostConstant::ExactDistinctOperationsPerValueDoubling, - CostConstant::ExactDistinctOperationsPerRowAtFullValueMiss, - CostConstant::ExactDistinctOperationsPerRowAtFullPairMiss, - CostConstant::ExactDistinctOperationsPerInsertedValue, - CostConstant::ExactDistinctOperationsPerInsertedPair, - CostConstant::SketchUpdateOperationsPerCounter, - CostConstant::ExactMergeOperationsPerState, - CostConstant::ExtremumMergeOperationsPerState, - CostConstant::CounterMergeOperationsPerState, - CostConstant::DdsketchMergeOperationsPerState, - CostConstant::KllMergeOperationsPerState, - CostConstant::KllMergeOperationsPerInputByte, - CostConstant::KllMergeOperationsPerMergedByte, - CostConstant::MergeOperationsPerOutputGroup, - CostConstant::SketchMergeOperationsPerByte, - CostConstant::KllEstimateOperationsPerByte, - CostConstant::KllEstimateOperationsPerUnsortedItem, - CostConstant::SketchEstimateOperationsPerGroup, - CostConstant::OutputRowOperationsPerGroup, - CostConstant::SketchChainEmitOperationsPerState, - CostConstant::ExactChainEmitOperationsPerState, - CostConstant::KllMergeOperationsPerOutputGroup, - ]; - /// Its built-in value, e.g. 170.0 for `OperationsPerGroupPerRawBatch`: /// the constant of the same name in `operations_per_unit`. pub fn operations_per_unit(self) -> f64 { @@ -230,11 +189,19 @@ pub struct NodeCostTerms { /// reads, a predicate's operations per row, the rounding a hash /// aggregate applies to its per-row operations. pub operations_without_constant: f64, + /// The formula's charge: these terms at the built-in values, added in + /// the order the formula adds them. Floating-point addition depends on + /// order, so this is the exact value the formula charges, where + /// re-adding `units_per_constant` in key order may differ in the last + /// bits. Example: `rows × (update + carried label) + visits × 170` adds + /// the per-row operations before multiplying by the rows. + operations_at_built_in_values: f64, } impl NodeCostTerms { /// `operations_without_constant + Σ units × value(constant)`; with - /// [`CostConstant::operations_per_unit`] it is the formula's charge. + /// [`CostConstant::operations_per_unit`] it is the formula's charge up + /// to floating-point summation order. pub fn operations_with(&self, value: impl Fn(CostConstant) -> f64) -> f64 { self.operations_without_constant + self @@ -246,7 +213,7 @@ impl NodeCostTerms { /// The operations at the built-in values: the formula's charge. pub(crate) fn operations(&self) -> f64 { - self.operations_with(CostConstant::operations_per_unit) + self.operations_at_built_in_values } /// `units` of `constant` alone. Example: `of(TimeRangeOperationsPerRow, @@ -261,6 +228,7 @@ impl NodeCostTerms { Self { units_per_constant: BTreeMap::new(), operations_without_constant: operations, + operations_at_built_in_values: operations, } } @@ -268,6 +236,7 @@ impl NodeCostTerms { pub(crate) fn plus(mut self, constant: CostConstant, units: f64) -> Self { if units != 0.0 { *self.units_per_constant.entry(constant).or_insert(0.0) += units; + self.operations_at_built_in_values += units * constant.operations_per_unit(); } self } @@ -275,9 +244,26 @@ impl NodeCostTerms { /// These terms and `other`'s. pub(crate) fn plus_terms(mut self, other: &NodeCostTerms) -> Self { for (constant, units) in &other.units_per_constant { - self = self.plus(*constant, *units); + *self.units_per_constant.entry(*constant).or_insert(0.0) += units; } self.operations_without_constant += other.operations_without_constant; + self.operations_at_built_in_values += other.operations_at_built_in_values; + self + } + + /// These terms with `part`'s constants charging some of their + /// operations without a constant: `part`'s units are added and the + /// operations without a constant shrink by what those units charge, so + /// the charge is unchanged. Example: a hash aggregate charges 300,000 + /// rows × 27 operations without a constant, its per-row operations + /// 26.4 rounded up; attributing the 26.4 per row to their constants + /// leaves the 180,000 operations of rounding without one. + pub(crate) fn attributing_to_constants(mut self, part: &NodeCostTerms) -> Self { + for (constant, units) in &part.units_per_constant { + *self.units_per_constant.entry(*constant).or_insert(0.0) += units; + } + self.operations_without_constant -= + part.operations_at_built_in_values - part.operations_without_constant; self } @@ -290,6 +276,19 @@ impl NodeCostTerms { } self.units_per_constant.retain(|_, units| *units != 0.0); self.operations_without_constant *= times; + self.operations_at_built_in_values *= times; + self + } + + /// These terms shared over `parts`: the terms of a whole input spread + /// over its rows. Example: 259,000 inserted values over 300,000 rows + /// is 0.86 units of `ExactDistinctOperationsPerInsertedValue` per row. + pub(crate) fn divided_by(mut self, parts: f64) -> Self { + for units in self.units_per_constant.values_mut() { + *units /= parts; + } + self.operations_without_constant /= parts; + self.operations_at_built_in_values /= parts; self } } diff --git a/crates/cost/src/node/cost_terms/tests.rs b/crates/cost/src/node/cost_terms/tests.rs index 93efa8da8..00cbf015b 100644 --- a/crates/cost/src/node/cost_terms/tests.rs +++ b/crates/cost/src/node/cost_terms/tests.rs @@ -25,8 +25,8 @@ fn cost_constants_hold_the_built_in_values() { } // Every constant, against the file's text. let defined = defined_constants(); - assert_eq!(CostConstant::ALL.len(), defined.len()); - for constant in CostConstant::ALL { + assert_eq!(every_constant().len(), defined.len()); + for constant in every_constant() { let name = format!("{constant:?}"); let (_, value) = defined .iter() @@ -46,7 +46,7 @@ fn every_per_unit_constant_has_a_cost_constant() { .into_iter() .map(|(name, _)| name) .collect(); - let variants: BTreeSet = CostConstant::ALL + let variants: BTreeSet = every_constant() .iter() .map(|constant| format!("{constant:?}")) .collect(); @@ -87,58 +87,70 @@ fn cost_constant_json_names_are_snake_case() { } /// The JSON name of every constant, in declaration order: the approx-bench -/// side reads these keys, so renaming a variant must fail here. +/// side reads these keys, so renaming a variant must fail here. Each +/// parses to a distinct constant that prints it back. #[test] fn cost_constant_json_names_are_the_full_snake_case_list() { - let expected: [&str; 37] = [ - "time_range_operations_per_row", - "operations_per_group_per_raw_batch", - "chain_operations_per_arriving_row", - "hash_aggregate_operations_per_row", - "hash_aggregate_operations_per_further_key", - "aggregate_measure_operations_per_row", - "sort_operations_per_row_per_level", - "ddsketch_update_operations_per_row", - "kll_update_operations_per_row", - "exact_update_operations_per_row", - "counter_update_operations_per_row", - "carried_label_operations_per_row_per_label", - "kmv_update_operations_per_row", - "kmv_operations_per_compared_hash", - "exact_distinct_operations_per_row", - "exact_distinct_operations_per_value_doubling", - "exact_distinct_operations_per_row_at_full_value_miss", - "exact_distinct_operations_per_row_at_full_pair_miss", - "exact_distinct_operations_per_inserted_value", - "exact_distinct_operations_per_inserted_pair", - "sketch_update_operations_per_counter", - "exact_merge_operations_per_state", - "extremum_merge_operations_per_state", - "counter_merge_operations_per_state", - "ddsketch_merge_operations_per_state", - "kll_merge_operations_per_state", - "kll_merge_operations_per_input_byte", - "kll_merge_operations_per_merged_byte", - "merge_operations_per_output_group", - "sketch_merge_operations_per_byte", - "kll_estimate_operations_per_byte", - "kll_estimate_operations_per_unsorted_item", - "sketch_estimate_operations_per_group", - "output_row_operations_per_group", - "sketch_chain_emit_operations_per_state", - "exact_chain_emit_operations_per_state", - "kll_merge_operations_per_output_group", - ]; - let actual: Vec = CostConstant::ALL + let constants = every_constant(); + let distinct: BTreeSet = constants.iter().copied().collect(); + assert_eq!(distinct.len(), JSON_NAMES.len()); + for (constant, json_name) in constants.iter().zip(JSON_NAMES) { + assert_eq!( + serde_json::to_string(constant).unwrap(), + format!("\"{json_name}\"") + ); + } +} + +/// Every constant's JSON name, in declaration order. +const JSON_NAMES: [&str; 37] = [ + "time_range_operations_per_row", + "operations_per_group_per_raw_batch", + "chain_operations_per_arriving_row", + "hash_aggregate_operations_per_row", + "hash_aggregate_operations_per_further_key", + "aggregate_measure_operations_per_row", + "sort_operations_per_row_per_level", + "ddsketch_update_operations_per_row", + "kll_update_operations_per_row", + "exact_update_operations_per_row", + "counter_update_operations_per_row", + "carried_label_operations_per_row_per_label", + "kmv_update_operations_per_row", + "kmv_operations_per_compared_hash", + "exact_distinct_operations_per_row", + "exact_distinct_operations_per_value_doubling", + "exact_distinct_operations_per_row_at_full_value_miss", + "exact_distinct_operations_per_row_at_full_pair_miss", + "exact_distinct_operations_per_inserted_value", + "exact_distinct_operations_per_inserted_pair", + "sketch_update_operations_per_counter", + "exact_merge_operations_per_state", + "extremum_merge_operations_per_state", + "counter_merge_operations_per_state", + "ddsketch_merge_operations_per_state", + "kll_merge_operations_per_state", + "kll_merge_operations_per_input_byte", + "kll_merge_operations_per_merged_byte", + "merge_operations_per_output_group", + "sketch_merge_operations_per_byte", + "kll_estimate_operations_per_byte", + "kll_estimate_operations_per_unsorted_item", + "sketch_estimate_operations_per_group", + "output_row_operations_per_group", + "sketch_chain_emit_operations_per_state", + "exact_chain_emit_operations_per_state", + "kll_merge_operations_per_output_group", +]; + +/// Every constant, parsed from [`JSON_NAMES`]: with the count of the +/// constants of `operations_per_unit.rs`, a variant missing from the list +/// fails the tests above. +fn every_constant() -> Vec { + JSON_NAMES .iter() - .map(|constant| { - serde_json::to_string(constant) - .unwrap() - .trim_matches('"') - .to_string() - }) - .collect(); - assert_eq!(actual, expected); + .map(|name| serde_json::from_value(serde_json::Value::from(*name)).unwrap()) + .collect() } /// The exact-distinct cache and size parameters: not operations per unit. @@ -175,3 +187,40 @@ fn defined_constants() -> Vec<(String, f64)> { }) .collect() } + +/// The charge is the formula's own floating-point value, its terms added +/// in the order the formula adds them, so it does not move when the +/// terms are kept per constant: 0.1 compared hashes (0.5 each) and 0.3 +/// merged bytes (0.43 each) per row, times 7 rows, charge +/// (0.1 × 0.5 + 0.3 × 0.43) × 7 = 1.253, where adding the per-constant +/// products 0.7 × 0.5 + 2.1 × 0.43 gives 1.2530000000000001. +#[test] +fn the_charge_adds_terms_in_the_formula_s_order() { + let terms = NodeCostTerms::of(CostConstant::KmvOperationsPerComparedHash, 0.1) + .plus(CostConstant::KllMergeOperationsPerMergedByte, 0.3) + .times(7.0); + assert_eq!(terms.operations(), (0.1 * 0.5 + 0.3 * 0.43) * 7.0); + let by_constant = terms.operations_with(CostConstant::operations_per_unit); + assert_ne!(by_constant, terms.operations()); + assert!((by_constant - terms.operations()).abs() <= 1e-12 * terms.operations()); +} + +/// Attributing per-row terms to their constants keeps the charge and +/// leaves the rounding without a constant: 1,000 rows × 27 operations +/// charged without a constant, 26.4 of them per row named, leave 600. +#[test] +fn attributing_to_constants_keeps_the_charge() { + let per_row = NodeCostTerms::of(CostConstant::HashAggregateOperationsPerRow, 1.0) + .plus(CostConstant::KmvOperationsPerComparedHash, 20.8); + let operator = NodeCostTerms::without_constant(27_000.0) + .plus(CostConstant::OutputRowOperationsPerGroup, 10.0); + let terms = operator + .clone() + .attributing_to_constants(&per_row.times(1_000.0)); + assert_eq!(terms.operations(), operator.operations()); + assert!((terms.operations_without_constant - 600.0).abs() < 1e-9); + assert_eq!( + terms.units_per_constant[&CostConstant::HashAggregateOperationsPerRow], + 1_000.0 + ); +} diff --git a/crates/cost/src/node/operations_per_unit.rs b/crates/cost/src/node/operations_per_unit.rs index b81157dea..7344882c1 100644 --- a/crates/cost/src/node/operations_per_unit.rs +++ b/crates/cost/src/node/operations_per_unit.rs @@ -321,7 +321,7 @@ fn cache_miss_fraction(entries: f64, entries_in_cache: f64) -> f64 { /// pairs) cost 59 + 13 × 10.0 + 207 × 0.8 + 79 × 0.96 + 356 × 1 M ÷ 12 M + /// 13 × 11.3 M ÷ 12 M = 472 (measured 488 ns per row with the key and the /// measure, charged 492). -pub(crate) fn exact_distinct_value_operations_per_row( +pub(crate) fn exact_distinct_value_terms_per_row( distinct_values: u64, group_count: u64, rows: u64, @@ -344,13 +344,13 @@ pub(crate) fn exact_distinct_value_operations_per_row( CostConstant::ExactDistinctOperationsPerRowAtFullPairMiss, cache_miss_fraction(pairs, EXACT_DISTINCT_PAIRS_IN_CACHE), ) - .plus( - CostConstant::ExactDistinctOperationsPerInsertedValue, - values.min(rows) / rows, - ) - .plus( - CostConstant::ExactDistinctOperationsPerInsertedPair, - pairs / rows, + .plus_terms( + &NodeCostTerms::of( + CostConstant::ExactDistinctOperationsPerInsertedValue, + values.min(rows), + ) + .plus(CostConstant::ExactDistinctOperationsPerInsertedPair, pairs) + .divided_by(rows), ) } @@ -505,7 +505,7 @@ pub(crate) const KLL_MERGE_OPERATIONS_PER_OUTPUT_GROUP: f64 = 19_000.0; /// of a column taking `distinct_values` values (`None`: every row's /// differs). Only a KMV's depends on them /// ([`kmv_compared_hashes_per_row`]). -pub(crate) fn update_operations_per_row( +pub(crate) fn update_terms_per_row( summary_type: &FieldDataType, parameters: Option<&SketchParams>, rows_per_state: Option, @@ -637,25 +637,6 @@ fn univmon_expected_layers(layers: u8) -> f64 { /// 12,000 rows each into 100 groups (10 states per group) inserts 119,300 /// hashes per group, 58,278 compared per hash: 348 s, measured 352 on the /// executor. -pub(crate) fn merge_operations_per_state( - family: StateFamily<'_>, - state_bytes: u64, - merged_state_bytes: u64, - rows_per_input_state: Option, - input_states_per_group: u64, -) -> f64 { - merge_terms_per_state( - family, - state_bytes, - merged_state_bytes, - rows_per_input_state, - input_states_per_group, - ) - .operations() -} - -/// [`merge_operations_per_state`] as terms: the units of each constant one -/// input state's merge charges. pub(crate) fn merge_terms_per_state( family: StateFamily<'_>, state_bytes: u64, @@ -691,7 +672,7 @@ pub(crate) fn merge_terms_per_state( let retained_hashes = rows_per_input_state.map_or(u64::from(*k), |rows| rows.min(u64::from(*k))); let hashes_per_group = retained_hashes.saturating_mul(input_states_per_group.max(1)); - update_operations_per_row( + update_terms_per_row( &FieldDataType::Sketch(SketchAlgorithm::Kmv), Some(&SketchParams::Kmv { k: *k }), Some(hashes_per_group), @@ -715,15 +696,9 @@ pub(crate) fn merge_terms_per_state( } /// The operations a `SummaryMerge` of `family` with `output_groups` groups -/// costs beyond its input states' merges ([`merge_operations_per_state`]): +/// costs beyond its input states' merges ([`merge_terms_per_state`]): /// each group's state and output row. Example: 1,000 groups cost /// 1,100,000, of KLL states 20,100,000. -#[cfg(test)] -pub(crate) fn merge_output_operations(family: StateFamily<'_>, output_groups: u64) -> f64 { - merge_output_terms(family, output_groups).operations() -} - -/// [`merge_output_operations`] as terms: units per output group. pub(crate) fn merge_output_terms(family: StateFamily<'_>, output_groups: u64) -> NodeCostTerms { let per_group = NodeCostTerms::of(CostConstant::MergeOperationsPerOutputGroup, 1.0); let per_group = match family { @@ -759,7 +734,7 @@ pub(crate) fn merge_output_terms(family: StateFamily<'_>, output_groups: u64) -> /// median 1.38–1.62x their charge (up to 2.29x for KLL states), at 100 and /// 1,000 groups 0.96–1.20x (`MERGE_OPERATIONS_PER_OUTPUT_GROUP`). Example: /// 1,000 groups cost 1,100,000. -pub(crate) fn pass_through_merge_operations(output_groups: u64) -> NodeCostTerms { +pub(crate) fn pass_through_merge_terms(output_groups: u64) -> NodeCostTerms { NodeCostTerms::of( CostConstant::MergeOperationsPerOutputGroup, output_groups as f64, @@ -769,7 +744,7 @@ pub(crate) fn pass_through_merge_operations(output_groups: u64) -> NodeCostTerms /// The operations per state a tumbling-window chain of `summary_type` /// costs to complete and emit each window's state: 1,500 for an exact /// accumulator, 2,500 for a sketch or any other summary. -pub(crate) fn chain_emit_operations_per_state(summary_type: &FieldDataType) -> NodeCostTerms { +pub(crate) fn chain_emit_terms_per_state(summary_type: &FieldDataType) -> NodeCostTerms { match summary_type { FieldDataType::ExactAggregate(..) => { NodeCostTerms::of(CostConstant::ExactChainEmitOperationsPerState, 1.0) @@ -802,7 +777,7 @@ pub(crate) enum StateLastWrittenBy { /// rows are unknown). Example: a DDSketch quantile costs 1,000 + 2,500 = /// 3,500 per group; a KLL at k = 547 of 4,844 bytes 6,860 after a merge /// and, built from 300 rows, 2,500 + 40 × 300 = 14,500 after updates. -pub(crate) fn estimate_operations_per_group( +pub(crate) fn estimate_terms_per_group( family: StateFamily<'_>, state_bytes: u64, written_by: StateLastWrittenBy, @@ -847,7 +822,7 @@ pub(crate) fn estimate_operations_per_group( /// without measures). Example: a per-series `max_over_time` (one key, one /// measure) costs 16 + 4 = 20; `GROUP BY job, instance` with one measure /// 28 more. -pub(crate) fn hash_aggregate_operations_per_row( +pub(crate) fn hash_aggregate_terms_per_row( grouping_key_count: u64, measures: &[AggIntent], rows: u64, @@ -865,7 +840,7 @@ pub(crate) fn hash_aggregate_operations_per_row( let measures = measures .iter() .fold(NodeCostTerms::default(), |terms, measure| { - terms.plus_terms(&aggregate_measure_operations_per_row( + terms.plus_terms(&aggregate_measure_terms_per_row( measure, rows, group_count, @@ -887,7 +862,7 @@ pub(crate) fn hash_aggregate_operations_per_row( /// (`log2(rows_per_group)` levels); /// - an exact distinct count or frequency statistic interns the row's value /// in a table of every distinct value read and keeps its group's -/// (group, value) pairs ([`exact_distinct_value_operations_per_row`] of +/// (group, value) pairs ([`exact_distinct_value_terms_per_row`] of /// the `distinct_values` the column takes); a top-k hashes the row's /// value into its group's map, the work a further grouping key does /// (`HASH_AGGREGATE_OPERATIONS_PER_FURTHER_KEY`, not measured); @@ -899,7 +874,7 @@ pub(crate) fn hash_aggregate_operations_per_row( /// including `changes`, `delta`, `idelta` and `resets`, which compare /// neighbouring samples without tracking a counter's increase (not /// measured). -fn aggregate_measure_operations_per_row( +fn aggregate_measure_terms_per_row( measure: &AggIntent, rows: u64, group_count: u64, @@ -915,7 +890,7 @@ fn aggregate_measure_operations_per_row( AggIntent::Cardinality { .. } | AggIntent::FrequencyL2 { .. } | AggIntent::FrequencyEntropy { .. } => measure_row.plus_terms( - &exact_distinct_value_operations_per_row(distinct_values, group_count, rows), + &exact_distinct_value_terms_per_row(distinct_values, group_count, rows), ), AggIntent::TopK { .. } => { measure_row.plus(CostConstant::HashAggregateOperationsPerFurtherKey, 1.0) diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index a7f8aa1fe..093fc44ec 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -24,9 +24,9 @@ use crate::node::analytical::{ }; use crate::node::cost_terms::{CostConstant, NodeCostTerms}; use crate::node::operations_per_unit::{ - chain_emit_operations_per_state, estimate_operations_per_group, - hash_aggregate_operations_per_row, merge_output_terms, merge_terms_per_state, - pass_through_merge_operations, update_operations_per_row, StateLastWrittenBy, + chain_emit_terms_per_state, estimate_terms_per_group, hash_aggregate_terms_per_row, + merge_output_terms, merge_terms_per_state, pass_through_merge_terms, update_terms_per_row, + StateLastWrittenBy, }; use crate::node::operator_statistics::{ EdgeStatistics, OperatorStatistics, PartitionStatistics, UnaryEdgeStatistics, @@ -868,17 +868,14 @@ pub(crate) fn node_usage_and_cost_terms<'a>( .chain([input.rows]) .min() .unwrap_or(input.rows); - let terms_per_row = hash_aggregate_operations_per_row( + let terms_per_row = hash_aggregate_terms_per_row( keys, measures, input.rows, group_count, distinct_values, ) - .plus_terms(&carried_label_operations_per_row( - &node.output_schema, - reduction, - )); + .plus_terms(&carried_label_terms_per_row(&node.output_schema, reduction)); let operations_per_row = terms_per_row.operations(); let group_visits = visits(group_count); let estimate = estimate_operator_with_terms( @@ -907,17 +904,11 @@ pub(crate) fn node_usage_and_cost_terms<'a>( ) .map(|(estimate, operator_terms)| { // The operator charges its rows times the per-row - // operations rounded up, without a constant: split - // into the per-row terms and the rounding. - let rounding = operator_terms.operations_without_constant - - input.rows as f64 * operations_per_row; - let terms = NodeCostTerms { - operations_without_constant: 0.0, - ..operator_terms - } - .plus_terms(&terms_per_row.times(input.rows as f64)) - .plus_terms(&NodeCostTerms::without_constant(rounding)) - .plus(CostConstant::OperationsPerGroupPerRawBatch, group_visits); + // operations rounded up, without a constant: those + // per-row terms charge all but the rounding. + let terms = operator_terms + .attributing_to_constants(&terms_per_row.times(input.rows as f64)) + .plus(CostConstant::OperationsPerGroupPerRawBatch, group_visits); charged(terms, estimate.peak_memory_bytes(), estimate.scan_bytes()) }); ( @@ -1115,16 +1106,13 @@ pub(crate) fn node_usage_and_cost_terms<'a>( input_rows, )?); assumed_data_facts.extend(assumed_magnitude_range_fact(column, input_rows)); - let update_terms = update_operations_per_row( + let update_terms = update_terms_per_row( family, node.parameters.as_ref(), input_rows.rows_per_state, folded_distinct_values(update), ) - .plus_terms(&carried_label_operations_per_row( - &node.output_schema, - reduction, - )); + .plus_terms(&carried_label_terms_per_row(&node.output_schema, reduction)); let update_ops = update_terms.operations(); let out = EdgeStatistics { rows: group_count, @@ -1188,16 +1176,13 @@ pub(crate) fn node_usage_and_cost_terms<'a>( input_rows, )?); assumed_data_facts.extend(assumed_magnitude_range_fact(column, input_rows)); - let update_terms = update_operations_per_row( + let update_terms = update_terms_per_row( family, node.parameters.as_ref(), input_rows.rows_per_state, folded_distinct_values(update), ) - .plus_terms(&carried_label_operations_per_row( - &node.output_schema, - reduction, - )); + .plus_terms(&carried_label_terms_per_row(&node.output_schema, reduction)); let update_ops = update_terms.operations(); // The states it completes in one execution: at ingestion time // one second's share of each group's window (a 10 s window: a @@ -1232,7 +1217,7 @@ pub(crate) fn node_usage_and_cost_terms<'a>( .times(input.rows as f64) .plus(range_constant, input.rows as f64) .plus(CostConstant::OperationsPerGroupPerRawBatch, group_visits) - .plus_terms(&chain_emit_operations_per_state(family).times(emitted_states)), + .plus_terms(&chain_emit_terms_per_state(family).times(emitted_states)), out.bytes, 0, )), @@ -1300,7 +1285,7 @@ pub(crate) fn node_usage_and_cost_terms<'a>( .all(|producer| keys_are_input_keys(keys, producer.output_schema)) }); let merge_terms = match keeps_input_groups && states <= group_count { - true => pass_through_merge_operations(group_count), + true => pass_through_merge_terms(group_count), false => producers .iter() .fold(NodeCostTerms::default(), |terms, producer| { @@ -1363,7 +1348,7 @@ pub(crate) fn node_usage_and_cost_terms<'a>( ( out, Ok(charged( - estimate_operations_per_group(family, input_state_bytes(), written_by) + estimate_terms_per_group(family, input_state_bytes(), written_by) .times(input.rows as f64), 0, 0, @@ -1775,10 +1760,7 @@ fn grouping_key_count(reduction: &Reduction) -> u64 { /// the per-series sum under `sum by (job) (sum_over_time(latency[5m]))` /// outputs the series identity (its key) and `job`, so it carries one /// label: 37 operations per row. -fn carried_label_operations_per_row( - output_schema: &Schema, - reduction: &Reduction, -) -> NodeCostTerms { +fn carried_label_terms_per_row(output_schema: &Schema, reduction: &Reduction) -> NodeCostTerms { let label_columns = output_schema .fields .iter() diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index d19c368c6..407ddb326 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -231,7 +231,8 @@ fn a_merge_over_a_chain_s_windows_is_costed_by_the_chain_s_family() { assert_eq!( merge_usage.cpu_ops_per_second, 5_000.0 * crate::node::operations_per_unit::DDSKETCH_MERGE_OPERATIONS_PER_STATE - + crate::node::operations_per_unit::merge_output_operations(merge_usage.family, 1_000) + + crate::node::operations_per_unit::merge_output_terms(merge_usage.family, 1_000) + .operations() ); } @@ -280,7 +281,8 @@ fn a_merge_over_a_rate_chain_s_windows_is_costed_by_the_rate_accumulator() { assert_eq!( merge_usage.cpu_ops_per_second, 5_000.0 * crate::node::operations_per_unit::COUNTER_MERGE_OPERATIONS_PER_STATE - + crate::node::operations_per_unit::merge_output_operations(merge_usage.family, 1_000) + + crate::node::operations_per_unit::merge_output_terms(merge_usage.family, 1_000) + .operations() ); } @@ -2316,12 +2318,13 @@ fn a_kll_merge_of_few_states_charges_each_group_its_measured_time() { /// Sum and Count states measure about 230. #[test] fn a_max_merge_charges_its_measured_time_per_state() { - use crate::node::operations_per_unit::merge_operations_per_state; + use crate::node::operations_per_unit::merge_terms_per_state; for kind in [ExactKind::Max, ExactKind::Min] { - let max = merge_operations_per_state(StateFamily::Exact(&kind), 56, 56, None, 1); + let max = merge_terms_per_state(StateFamily::Exact(&kind), 56, 56, None, 1).operations(); assert!((100.0..=150.0).contains(&max), "{kind:?}: {max} per state"); } - let sum = merge_operations_per_state(StateFamily::Exact(&ExactKind::Sum), 56, 56, None, 1); + let sum = + merge_terms_per_state(StateFamily::Exact(&ExactKind::Sum), 56, 56, None, 1).operations(); assert!((200.0..=260.0).contains(&sum), "Sum: {sum} per state"); } diff --git a/crates/cost/src/plan/cost.rs b/crates/cost/src/plan/cost.rs index 9d6c5de9c..f1358b642 100644 --- a/crates/cost/src/plan/cost.rs +++ b/crates/cost/src/plan/cost.rs @@ -9,7 +9,7 @@ use std::collections::{BTreeMap, HashMap, HashSet}; use crate::node::analytical::AnalyticalCostError; -use crate::node::operations_per_unit::merge_operations_per_state; +use crate::node::operations_per_unit::merge_terms_per_state; use crate::node::operator_statistics::EdgeStatistics; use crate::node::state_bytes::{sketch_state_bytes, state_bytes_per_row, StateInputRows}; use crate::node::usage::{cost_source, reads_one_batch_of}; @@ -325,7 +325,7 @@ pub fn plan_cost( // such as the raw rows a `Filter` or `TimeRange` keeps or an exact // `Aggregate`'s values: the executor merges ingest shards only of // summary state columns, and partial copies of rows are concatenated. - // No per-group term (`merge_output_operations`, 19,000 per KLL group): + // No per-group term (`merge_output_terms`, 19,000 per KLL group): // the executor merges a shard's states into the existing states in // place, group by group, and creates no group's state or output row. let merge_ms = |id: PhysicalASAPNodeId| { @@ -343,7 +343,7 @@ pub fn plan_cost( || { // A partial copy merges into a state of the same window. let state_bytes = state_bytes_per_row(output[&id], &nodes[&id].output_schema); - merge_operations_per_state(family, state_bytes, state_bytes, None, 1) + merge_terms_per_state(family, state_bytes, state_bytes, None, 1).operations() * speed.ms_per_cpu_op * formula_scale }, diff --git a/docs/design_docs/architecture/physical-plan-integration.md b/docs/design_docs/architecture/physical-plan-integration.md index 4d42aaab9..f54e8d97a 100644 --- a/docs/design_docs/architecture/physical-plan-integration.md +++ b/docs/design_docs/architecture/physical-plan-integration.md @@ -26,7 +26,7 @@ logical variant. Current state: no code lowers a whole candidate to a `PhysicalOperator` DAG. The recursive lowering and the physical-DAG estimator that priced its output were deleted with no remaining reader. Stage 3 prices each node of a physical -candidate with `estimate_operator` (`asap-cost`, `analytical.rs`) over the +candidate with `estimate_operator_with_terms` (`asap-cost`, `analytical.rs`) over the `OperatorStatistics` it derives for that node. The lowering obligations and comparison rules below are the contract a whole-DAG lowering must meet. diff --git a/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md b/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md index 753bfd4c7..696a579c1 100644 --- a/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md +++ b/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md @@ -9,7 +9,7 @@ > stage read it: physical query lowering (`query_physical_lowering`), > `EvidenceBackedPhysicalDAG`, `estimate_physical_dag` and its comparison and > cache variants, `ComparisonScope`, `CacheProfile`, and the storage and -> handoff estimators. Stage 3 prices each node with `estimate_operator` over +> handoff estimators. Stage 3 prices each node with `estimate_operator_with_terms` over > the `OperatorStatistics` it derives for that node. The sections on those > deleted parts record the design, not current code. > Unsupported operators, arrival modes and missing evidence remain unavailable; diff --git a/docs/develop_docs/extend-asap-aware-mapping.md b/docs/develop_docs/extend-asap-aware-mapping.md index d0ff341ed..a5ee18277 100644 --- a/docs/develop_docs/extend-asap-aware-mapping.md +++ b/docs/develop_docs/extend-asap-aware-mapping.md @@ -38,9 +38,13 @@ A new sketch that Pass 1 can offer for an intent. in `sketch_state_bytes` (`crates/cost/src/node/state_bytes/mod.rs`), its per-row and per-merge CPU in `crates/cost/src/node/operations_per_unit.rs` - (`update_operations_per_row`, `counters_updated_per_row`, - `merge_operations_per_state`), and accept its parameters in - `crates/cost/src/measured.rs` if deployments measure it. + (`update_terms_per_row`, `counters_updated_per_row`, + `merge_terms_per_state`), and accept its parameters in + `crates/cost/src/measured.rs` if deployments measure it. A new per-unit + constant there also needs a `CostConstant` variant of the same name in + `crates/cost/src/node/cost_terms.rs` and its arm in + `CostConstant::operations_per_unit`, so `node_cost_terms` can name it; + the `cost_terms` tests fail until it has one. 6. **Executor.** Wrap the `asap_sketchlib` implementation in `crates/executor/src/summary_kernels/` and register it in `factory.rs` and `capability.rs`. Gaps in the library are fixed upstream,