From 296621fd1f1a17bf5f5c563703fa0a248eed3184 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:35:55 +0000 Subject: [PATCH 01/15] test(cost): default unit costs rank exact against sketch plans as the executor measures Selection tests under the default CostInputs: with raw data kept at 1 Hz every 10 s, quantile_over_time(0.99, latency[1h]) selects the exact plan and max_over_time does not build at query time; sum(rate) at 1 Hz every 300 s keeps stored Rate states; stored exact accumulators and stored DDSketch windows stay selected where they measured faster. A ranking test checks that predicted CPU orders the exact plan and each measured alternative as the executor measured them, with the measured CPU per second as fixed expectations and the measured query-time plans as fixtures. Four of the six fail on main. --- .../tests/default_cost_plan_ranking.rs | 444 ++++++++++++++++++ .../max_1_hz_every_10_s_query_time_max.json | 1 + ..._10_hz_every_60_s_query_time_ddsketch.json | 1 + ...e_1_hz_every_10_s_query_time_ddsketch.json | 1 + ...e_rank_1_hz_every_10_s_query_time_kll.json | 1 + .../sum_rate_query_time_rate.json | 1 + crates/integration-tests/tests/main.rs | 1 + 7 files changed, 450 insertions(+) create mode 100644 crates/integration-tests/tests/default_cost_plan_ranking.rs create mode 100644 crates/integration-tests/tests/default_cost_plan_ranking_fixtures/max_1_hz_every_10_s_query_time_max.json create mode 100644 crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_10_hz_every_60_s_query_time_ddsketch.json create mode 100644 crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_1_hz_every_10_s_query_time_ddsketch.json create mode 100644 crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_rank_1_hz_every_10_s_query_time_kll.json create mode 100644 crates/integration-tests/tests/default_cost_plan_ranking_fixtures/sum_rate_query_time_rate.json diff --git a/crates/integration-tests/tests/default_cost_plan_ranking.rs b/crates/integration-tests/tests/default_cost_plan_ranking.rs new file mode 100644 index 000000000..d67c560c0 --- /dev/null +++ b/crates/integration-tests/tests/default_cost_plan_ranking.rs @@ -0,0 +1,444 @@ +//! Stage 3 under the default `CostInputs` (no measured table) ranks the +//! exact plan against the alternatives as the executor measures them. +//! +//! The expectations are the executor's measured total CPU (query plus +//! ingestion) per second of wall time, for each plan kind of each workload +//! below: measured on one core of an idle server, in a release build, +//! serially, with the in-repository executor over the synthetic data the +//! workloads declare (`latency` a non-negative gauge, `requests_total` a +//! counter, 10 jobs), every accuracy target 1% relative at δ = 0.01. +//! +//! Example: `quantile_over_time(0.99, latency[1h])` over 1,000 series +//! sampled at 1 Hz, every 10 s, raw data kept. The exact plan (scan, keep +//! every value per series, sort) measures 39.0 CPU-ms per second; a +//! DDSketch built from the raw rows at query time measures 77.8, twice as +//! much, because inserting a row into a DDSketch costs more than keeping it +//! and sorting it. The planner must not predict the sketch cheaper. + +use asap_cost::CostInputs; +use asap_devtools::planning_stages::{lower_promql_roots, run_planning_stages_over}; +use asap_planner::{DeploymentCapabilities, PlanningModels}; +use asap_types::ir::properties::ExecutionTiming; +use asap_types::ir::schema::FieldDataType; +use asap_types::ir::ASAPOp; +use asap_types::plan::physical_asap_dag::{PhysicalASAPDAG, PhysicalASAPOperatorPayload}; +use asap_types::workload::PlanningWorkload; + +/// Data rate and evaluation interval of one measured setting, over 1,000 +/// series of each source. +#[derive(Debug, Clone, Copy)] +struct Setting { + /// Milliseconds between two samples of one series. + sample_interval_ms: u32, + /// Milliseconds between two evaluations of the query. + evaluation_interval_ms: u32, +} + +/// 1 Hz, every 10 s. +const ONE_HZ_EVERY_10_S: Setting = Setting { + sample_interval_ms: 1_000, + evaluation_interval_ms: 10_000, +}; +/// 10 Hz, every 60 s. +const TEN_HZ_EVERY_60_S: Setting = Setting { + sample_interval_ms: 100, + evaluation_interval_ms: 60_000, +}; +/// 1 Hz, every 300 s. +const ONE_HZ_EVERY_300_S: Setting = Setting { + sample_interval_ms: 1_000, + evaluation_interval_ms: 300_000, +}; + +/// A measured PromQL query and the metric of its 1% accuracy target. +#[derive(Debug, Clone, Copy)] +struct Query { + promql: &'static str, + /// `relative` or `rank`. + accuracy_metric: &'static str, +} + +/// `query` every `setting.evaluation_interval_ms` with its 1% target at +/// δ = 0.01, over 1,000 series each of `latency` and `requests_total`. +fn workload(query: Query, setting: Setting) -> PlanningWorkload { + let Setting { + sample_interval_ms, + evaluation_interval_ms, + } = setting; + let Query { + promql, + accuracy_metric, + } = query; + let requirements = format!( + r#""accuracy": {{"max_error": 0.01, "failure_probability": 0.01, "accuracy_scope": "per_value", "accuracy_metric": "{accuracy_metric}"}}"# + ); + let rows_per_second = 2.0 * 1_000.0 * 1_000.0 / f64::from(sample_interval_ms); + serde_json::from_str(&format!( + r#"{{"query_workload": {{"language": "promql", "repeating_queries": [ + {{"query": {promql:?}, "demand": {{"fixed_interval_at": {{"interval": {evaluation_interval_ms}, "evaluation_phase": 0}}}}, + "requirements": {{{requirements}}}, "predictability": {{"predictable": {{"known_at": null}}}}}}]}}, + "data_workload": {{"arrival": {{"ingesting": {{"rows_per_second": {rows_per_second}}}}}, + "sources": {{ + "latency": {{"series_count": 1000, "metric_type": "gauge", "column_signs": {{"value": "non_negative"}}, "sample_interval": {sample_interval_ms}}}, + "requests_total": {{"series_count": 1000, "metric_type": "counter", "sample_interval": {sample_interval_ms}}}}}}}}}"# + )) + .unwrap() +} + +/// The plan Stage 3 selects for `workload` under the default cost inputs, +/// with raw data kept or not, and its predicted CPU core-seconds per second. +fn selected(workload: &PlanningWorkload, raw_data_retained: bool) -> (PhysicalASAPDAG, f64) { + let settings = CostInputs::default(); + let capabilities = DeploymentCapabilities { + raw_data_retained, + ..Default::default() + }; + let models = PlanningModels::builtin() + .with_cost_inputs(&settings) + .with_deployment_capabilities(&capabilities); + let entries: Vec<_> = workload.query_workload.entries().collect(); + let data = workload.data_workload.clone().unwrap(); + let roots = lower_promql_roots(workload).unwrap(); + let run = run_planning_stages_over(roots, &entries, &data, models).unwrap(); + let (plan, _) = run.run.selection.as_ref().expect("a plan is selected"); + let cpu = predicted_cpu(plan, workload); + (plan.clone(), cpu) +} + +/// The exact plan the executor measured as the baseline: the query-time +/// lowering of `query` (scan raw data, compute exactly), predicted CPU +/// core-seconds per second. +fn exact_query_time_cpu(query: Query, setting: Setting) -> f64 { + let w = workload(query, setting); + let roots = lower_promql_roots(&w).unwrap(); + let (plan, _) = + asap_types::test_support::forwarders::physical_asap_dag_at_query_time_with_node_ids(&roots) + .unwrap(); + predicted_cpu(&plan, &w) +} + +/// `plan`'s predicted CPU core-seconds per second under the default cost +/// inputs: query and ingestion time. +fn predicted_cpu(plan: &PhysicalASAPDAG, workload: &PlanningWorkload) -> f64 { + let entries: Vec<_> = workload.query_workload.entries().collect(); + let data = workload.data_workload.clone().unwrap(); + let (cost, _) = asap_cost::plan_cost(plan, &entries, &data, &CostInputs::default()).unwrap(); + cost.per_node + .values() + .map(|node| node.cpu_core_seconds_per_second) + .sum() +} + +/// What a plan computes its answer from, as the measurements name it. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum PlanKind { + /// Raw data scanned at query time and computed exactly (no summary). + ExactScan, + /// An exact accumulator built from raw data at query time. + QueryTimeExactAccumulator, + /// A sketch built from raw data at query time. + QueryTimeSketch, + /// States maintained at ingestion time in tumbling windows and merged + /// at query time. + StoredStates, +} + +fn plan_kind(plan: &PhysicalASAPDAG) -> PlanKind { + let mut kind = PlanKind::ExactScan; + for node in &plan.nodes { + match (&node.payload, node.output_state.timing) { + ( + PhysicalASAPOperatorPayload::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }), + ExecutionTiming::IngestionTime, + ) => return PlanKind::StoredStates, + ( + PhysicalASAPOperatorPayload::ASAP(ASAPOp::SummaryAgg { summary_type, .. }), + ExecutionTiming::QueryTime, + ) if kind != PlanKind::QueryTimeSketch => { + kind = match summary_type { + FieldDataType::Sketch(..) => PlanKind::QueryTimeSketch, + _ => PlanKind::QueryTimeExactAccumulator, + } + } + _ => {} + } + } + kind +} + +/// Whether `plan` keeps or builds a sketch anywhere. +fn has_sketch(plan: &PhysicalASAPDAG) -> bool { + plan.nodes.iter().any(|node| { + matches!( + &node.payload, + PhysicalASAPOperatorPayload::ASAP( + ASAPOp::SummaryAgg { + summary_type: FieldDataType::Sketch(..), + .. + } | ASAPOp::SummaryTumblingWindowChain { + summary_type: FieldDataType::Sketch(..), + .. + } + ) + ) + }) +} + +const fn relative(promql: &'static str) -> Query { + Query { + promql, + accuracy_metric: "relative", + } +} + +const QUANTILE: Query = relative("quantile_over_time(0.99, latency[1h])"); +/// The same quantile with a 1% rank target: a KLL instead of a DDSketch. +const QUANTILE_RANK: Query = Query { + promql: "quantile_over_time(0.99, latency[1h])", + accuracy_metric: "rank", +}; +const MAX: Query = relative("max_over_time(latency[1h])"); +const SUM_OVER_TIME: Query = relative("sum by (job) (sum_over_time(latency[1h]))"); +const COUNT_OVER_TIME: Query = relative("sum by (job) (count_over_time(latency[1h]))"); +const SUM_RATE: Query = relative("sum by (job) (rate(requests_total[1h]))"); + +/// With raw data kept at 1 Hz every 10 s, a DDSketch built at query time +/// measures 77.8 CPU-ms/s against the exact plan's 39.0, and stored DDSketch +/// windows 69.7: the exact plan is selected. +#[test] +fn quantile_at_1_hz_every_10_s_with_raw_data_selects_the_exact_plan() { + let (plan, _) = selected(&workload(QUANTILE, ONE_HZ_EVERY_10_S), true); + assert!( + !has_sketch(&plan), + "selected a sketch plan ({:?}), measured 2x slower than exact", + plan_kind(&plan) + ); +} + +/// With raw data kept at 1 Hz every 10 s, a Max accumulator built at query +/// time measures 19.1 CPU-ms/s, slower than the exact plan's 14.6; stored +/// Max windows measure 5.6. Either of the two measured no slower than +/// exact may be selected, not the query-time build. +#[test] +fn max_at_1_hz_every_10_s_with_raw_data_does_not_build_at_query_time() { + let (plan, _) = selected(&workload(MAX, ONE_HZ_EVERY_10_S), true); + let kind = plan_kind(&plan); + assert!( + matches!(kind, PlanKind::ExactScan | PlanKind::StoredStates), + "selected {kind:?}, measured slower than exact" + ); +} + +/// With no raw data at 10 Hz every 60 s, stored DDSketch windows measure +/// 6.1 CPU-ms/s against the exact plan's 76.9 (with raw data): the +/// quantile still keeps a stored sketch. +#[test] +fn quantile_at_10_hz_every_60_s_without_raw_data_keeps_a_stored_sketch() { + let (plan, _) = selected(&workload(QUANTILE, TEN_HZ_EVERY_60_S), false); + assert_eq!(plan_kind(&plan), PlanKind::StoredStates); + assert!(has_sketch(&plan)); +} + +/// Stored exact accumulators measure 3.3–4.0x cheaper than exact at 1 Hz +/// every 10 s and 37–41x at 10 Hz every 60 s, for `sum(rate)` and +/// `sum_over_time`: both keep them, with raw data kept. +#[test] +fn sum_rate_and_sum_over_time_keep_stored_exact_accumulators() { + for setting in [ONE_HZ_EVERY_10_S, TEN_HZ_EVERY_60_S] { + for query in [SUM_RATE, SUM_OVER_TIME] { + let (plan, _) = selected(&workload(query, setting), true); + assert_eq!( + plan_kind(&plan), + PlanKind::StoredStates, + "{query:?} at {setting:?}" + ); + assert!(!has_sketch(&plan), "{query:?} at {setting:?}"); + } + } +} + +/// `sum by (job) (rate(requests_total[1h]))` at 1 Hz every 300 s, raw data +/// kept: the query-time Rate over raw data measures 1.12 CPU-ms/s, stored +/// 300 s Rate windows 0.137, 8x less work and 45x lower latency. The +/// stored windows are selected. +#[test] +fn sum_rate_at_1_hz_every_300_s_keeps_stored_rate_states() { + let (plan, _) = selected(&workload(SUM_RATE, ONE_HZ_EVERY_300_S), true); + assert_eq!(plan_kind(&plan), PlanKind::StoredStates); +} + +/// A plan measured against the exact plan: the stored-state plan (Stage 3's +/// selection without raw data), or a plan built at query time from raw data +/// that Stage 3 selected before the measured unit costs, kept as a fixture +/// (`default_cost_plan_ranking_fixtures/.json`). +#[derive(Debug, Clone, Copy)] +enum Alternative { + Stored, + QueryTimeFixture(&'static str), +} + +/// Measured total CPU, in CPU-ms per second of wall time, of the exact +/// plan and of an alternative, for each query and setting. Rate's exact +/// plan is the query-time Rate accumulator over raw data, which the +/// measurement used because the executor cannot run rate's query-time +/// lowering. +const MEASURED_CPU_MS_PER_S: [(Query, Setting, Alternative, f64, f64); 15] = [ + ( + QUANTILE, + ONE_HZ_EVERY_10_S, + Alternative::Stored, + 39.047, + 69.718, + ), + ( + QUANTILE, + TEN_HZ_EVERY_60_S, + Alternative::Stored, + 76.912, + 6.077, + ), + (MAX, ONE_HZ_EVERY_10_S, Alternative::Stored, 14.557, 5.572), + (MAX, TEN_HZ_EVERY_60_S, Alternative::Stored, 17.763, 0.620), + ( + SUM_OVER_TIME, + ONE_HZ_EVERY_10_S, + Alternative::Stored, + 30.101, + 8.758, + ), + ( + SUM_OVER_TIME, + TEN_HZ_EVERY_60_S, + Alternative::Stored, + 39.236, + 1.055, + ), + ( + COUNT_OVER_TIME, + ONE_HZ_EVERY_10_S, + Alternative::Stored, + 28.740, + 8.746, + ), + ( + COUNT_OVER_TIME, + TEN_HZ_EVERY_60_S, + Alternative::Stored, + 37.644, + 1.028, + ), + ( + SUM_RATE, + ONE_HZ_EVERY_10_S, + Alternative::Stored, + 35.126, + 13.079, + ), + ( + SUM_RATE, + TEN_HZ_EVERY_60_S, + Alternative::Stored, + 48.367, + 1.227, + ), + ( + SUM_RATE, + ONE_HZ_EVERY_300_S, + Alternative::Stored, + 1.118, + 0.137, + ), + ( + QUANTILE, + ONE_HZ_EVERY_10_S, + Alternative::QueryTimeFixture("quantile_1_hz_every_10_s_query_time_ddsketch"), + 39.047, + 77.786, + ), + ( + QUANTILE, + TEN_HZ_EVERY_60_S, + Alternative::QueryTimeFixture("quantile_10_hz_every_60_s_query_time_ddsketch"), + 76.912, + 140.022, + ), + ( + QUANTILE_RANK, + ONE_HZ_EVERY_10_S, + Alternative::QueryTimeFixture("quantile_rank_1_hz_every_10_s_query_time_kll"), + 39.118, + 43.483, + ), + ( + MAX, + ONE_HZ_EVERY_10_S, + Alternative::QueryTimeFixture("max_1_hz_every_10_s_query_time_max"), + 14.557, + 19.130, + ), +]; + +/// The plan kept as fixture `name` (`default_cost_plan_ranking_fixtures/.json`). +fn fixture_plan(name: &str) -> PhysicalASAPDAG { + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("tests/default_cost_plan_ranking_fixtures") + .join(format!("{name}.json")); + serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap() +} + +/// The exact plan's predicted CPU for `query`: its query-time lowering, or +/// for a rate the query-time Rate accumulator over raw data the +/// measurement ran (fixture `sum_rate_query_time_rate`, Stage 3's exact +/// plan over data at rest before the measured unit costs). +fn exact_cpu(query: Query, setting: Setting) -> f64 { + if !query.promql.contains("rate(") { + return exact_query_time_cpu(query, setting); + } + let plan = fixture_plan("sum_rate_query_time_rate"); + assert_eq!(plan_kind(&plan), PlanKind::QueryTimeExactAccumulator); + predicted_cpu(&plan, &workload(query, setting)) +} + +/// The alternative's predicted CPU for `query` at `setting`. +fn alternative_cpu(query: Query, setting: Setting, alternative: Alternative) -> f64 { + let w = workload(query, setting); + match alternative { + Alternative::Stored => { + let (plan, cpu) = selected(&w, false); + assert_eq!(plan_kind(&plan), PlanKind::StoredStates, "{query:?}"); + cpu + } + Alternative::QueryTimeFixture(name) => { + let plan = fixture_plan(name); + assert!( + matches!( + plan_kind(&plan), + PlanKind::QueryTimeSketch | PlanKind::QueryTimeExactAccumulator + ), + "{name}" + ); + predicted_cpu(&plan, &w) + } + } +} + +/// For every measured query, setting and alternative, the predicted CPU +/// orders the exact plan and the alternative as the measurement does. +#[test] +fn predicted_cpu_orders_exact_and_alternative_plans_as_measured() { + let mut wrong = Vec::new(); + for (query, setting, alternative, measured_exact, measured_alternative) in MEASURED_CPU_MS_PER_S + { + let exact = exact_cpu(query, setting); + let predicted = alternative_cpu(query, setting, alternative); + if (exact > predicted) != (measured_exact > measured_alternative) { + wrong.push(format!( + "{query:?} at {setting:?}, {alternative:?}: predicted exact {:.3} vs {:.3} \ + CPU-ms/s, measured {measured_exact} vs {measured_alternative}", + exact * 1e3, + predicted * 1e3 + )); + } + } + assert!(wrong.is_empty(), "misranked:\n{}", wrong.join("\n")); +} diff --git a/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/max_1_hz_every_10_s_query_time_max.json b/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/max_1_hz_every_10_s_query_time_max.json new file mode 100644 index 000000000..235458873 --- /dev/null +++ b/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/max_1_hz_every_10_s_query_time_max.json @@ -0,0 +1 @@ +{"nodes":[{"id":0,"payload":{"NonASAP":{"Scan":{"source":{"TimeSeries":{"metric":"latency"}},"predicates":[],"schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"retention_ms":3600000},{"id":1,"payload":{"NonASAP":{"TimeRange":{"range":{"secs":3600,"nanos":0},"kind":"range","child":0}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":2,"payload":{"ASAP":{"SummaryAgg":{"child":1,"summary_type":{"ExactAggregate":["Max","Max"]},"input":{"item":null,"weight":{"Column":"SampleValue"}},"reduction":"PerEntity","filter":null}}},"output_state":{"timing":"query_time","primitive":"SummaryState"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"state","dtype":{"ExactAggregate":["Max","Max"]},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":3,"payload":{"ASAP":{"FinalizeExactAccumulator":{"child":2}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}}],"edges":[{"producer":0,"consumer":1,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":1,"consumer":2,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":2,"consumer":3,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"state","dtype":{"ExactAggregate":["Max","Max"]},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"SummaryState"},"grouping":"NotApplicable","window":"NotApplicable"}],"roots":[3],"scalar_roots":[]} diff --git a/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_10_hz_every_60_s_query_time_ddsketch.json b/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_10_hz_every_60_s_query_time_ddsketch.json new file mode 100644 index 000000000..77a14e899 --- /dev/null +++ b/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_10_hz_every_60_s_query_time_ddsketch.json @@ -0,0 +1 @@ +{"nodes":[{"id":0,"payload":{"NonASAP":{"Scan":{"source":{"TimeSeries":{"metric":"latency"}},"predicates":[],"schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"retention_ms":3600000},{"id":1,"payload":{"NonASAP":{"TimeRange":{"range":{"secs":3600,"nanos":0},"kind":"range","child":0}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":2,"payload":{"ASAP":{"SummaryAgg":{"child":1,"summary_type":{"Sketch":"DDSketch"},"input":{"item":null,"weight":{"Column":"SampleValue"}},"reduction":"PerEntity","filter":null}}},"output_state":{"timing":"query_time","primitive":"SummaryState"},"lifetime":{"OneEvaluation":"Memory"},"parameters":{"DDSketch":{"alpha":0.01}},"output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"state","dtype":{"Sketch":"DDSketch"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":3,"payload":{"ASAP":{"SummaryEstimate":{"summary_input":2,"query":{"Quantile":{"q":0.99}}}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"quantile","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":4,"payload":{"NonASAP":{"Project":{"cols":[{"alias":"ts","expr":{"Column":0}},{"alias":"value","expr":{"Column":1}},{"alias":"$promql_series_identity","expr":{"Column":2}}],"child":3}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}}],"edges":[{"producer":0,"consumer":1,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":1,"consumer":2,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":2,"consumer":3,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"state","dtype":{"Sketch":"DDSketch"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"SummaryState"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":3,"consumer":4,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"quantile","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"}],"roots":[4],"scalar_roots":[]} diff --git a/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_1_hz_every_10_s_query_time_ddsketch.json b/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_1_hz_every_10_s_query_time_ddsketch.json new file mode 100644 index 000000000..0a3062ca8 --- /dev/null +++ b/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_1_hz_every_10_s_query_time_ddsketch.json @@ -0,0 +1 @@ +{"nodes":[{"id":0,"payload":{"NonASAP":{"Scan":{"source":{"TimeSeries":{"metric":"latency"}},"predicates":[],"schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"retention_ms":3600000},{"id":1,"payload":{"NonASAP":{"TimeRange":{"range":{"secs":3600,"nanos":0},"kind":"range","child":0}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":2,"payload":{"ASAP":{"SummaryAgg":{"child":1,"summary_type":{"Sketch":"DDSketch"},"input":{"item":null,"weight":{"Column":"SampleValue"}},"reduction":"PerEntity","filter":null}}},"output_state":{"timing":"query_time","primitive":"SummaryState"},"lifetime":"None","parameters":{"DDSketch":{"alpha":0.01}},"output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"state","dtype":{"Sketch":"DDSketch"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":3,"payload":{"ASAP":{"SummaryEstimate":{"summary_input":2,"query":{"Quantile":{"q":0.99}}}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"quantile","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":4,"payload":{"NonASAP":{"Project":{"cols":[{"alias":"ts","expr":{"Column":0}},{"alias":"value","expr":{"Column":1}},{"alias":"$promql_series_identity","expr":{"Column":2}}],"child":3}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}}],"edges":[{"producer":0,"consumer":1,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":1,"consumer":2,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":2,"consumer":3,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"state","dtype":{"Sketch":"DDSketch"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"SummaryState"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":3,"consumer":4,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"quantile","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"}],"roots":[4],"scalar_roots":[]} diff --git a/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_rank_1_hz_every_10_s_query_time_kll.json b/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_rank_1_hz_every_10_s_query_time_kll.json new file mode 100644 index 000000000..755f5c035 --- /dev/null +++ b/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_rank_1_hz_every_10_s_query_time_kll.json @@ -0,0 +1 @@ +{"nodes":[{"id":0,"payload":{"NonASAP":{"Scan":{"source":{"TimeSeries":{"metric":"latency"}},"predicates":[],"schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"retention_ms":3600000},{"id":1,"payload":{"NonASAP":{"TimeRange":{"range":{"secs":3600,"nanos":0},"kind":"range","child":0}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":2,"payload":{"ASAP":{"SummaryAgg":{"child":1,"summary_type":{"Sketch":"Kll"},"input":{"item":null,"weight":{"Column":"SampleValue"}},"reduction":"PerEntity","filter":null}}},"output_state":{"timing":"query_time","primitive":"SummaryState"},"lifetime":"None","parameters":{"Kll":{"k":547}},"output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"state","dtype":{"Sketch":"Kll"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":3,"payload":{"ASAP":{"SummaryEstimate":{"summary_input":2,"query":{"Quantile":{"q":0.99}}}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"quantile","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":4,"payload":{"NonASAP":{"Project":{"cols":[{"alias":"ts","expr":{"Column":0}},{"alias":"value","expr":{"Column":1}},{"alias":"$promql_series_identity","expr":{"Column":2}}],"child":3}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}}],"edges":[{"producer":0,"consumer":1,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":1,"consumer":2,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":2,"consumer":3,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"state","dtype":{"Sketch":"Kll"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"SummaryState"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":3,"consumer":4,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"quantile","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"}],"roots":[4],"scalar_roots":[]} diff --git a/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/sum_rate_query_time_rate.json b/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/sum_rate_query_time_rate.json new file mode 100644 index 000000000..67e59c809 --- /dev/null +++ b/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/sum_rate_query_time_rate.json @@ -0,0 +1 @@ +{"nodes":[{"id":0,"payload":{"NonASAP":{"Scan":{"source":{"TimeSeries":{"metric":"requests_total"}},"predicates":[],"schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[3,0]],"closed":true}}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[3,0]],"closed":true},"retention_ms":3600000},{"id":1,"payload":{"NonASAP":{"TimeRange":{"range":{"secs":3600,"nanos":0},"kind":"range","child":0}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[3,0]],"closed":true}},{"id":2,"payload":{"ASAP":{"SummaryAgg":{"child":1,"summary_type":{"ExactAggregate":["Rate","Rate"]},"input":{"item":null,"weight":{"Column":"SampleValue"}},"reduction":"PerEntity","filter":null}}},"output_state":{"timing":"query_time","primitive":"SummaryState"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"state","dtype":{"ExactAggregate":["Rate","Rate"]},"nullable":false},{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[3,0]],"closed":true}},{"id":3,"payload":{"ASAP":{"FinalizeExactAccumulator":{"child":2}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[3,0]],"closed":true}},{"id":4,"payload":{"ASAP":{"SummaryAgg":{"child":3,"summary_type":{"ExactAggregate":["Sum","Sum"]},"input":{"item":null,"weight":{"Column":"SampleValue"}},"reduction":{"Reduce":[2]},"filter":null}}},"output_state":{"timing":"query_time","primitive":"SummaryState"},"lifetime":"None","output_schema":{"fields":[{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"state","dtype":{"ExactAggregate":["Sum","Sum"]},"nullable":false}],"time_index":null,"unique_keys":[[0]],"closed":true}},{"id":5,"payload":{"ASAP":{"FinalizeExactAccumulator":{"child":4}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"sum","dtype":{"Plain":"float64"},"nullable":false}],"time_index":null,"unique_keys":[[0]],"closed":true}}],"edges":[{"producer":0,"consumer":1,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[3,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":1,"consumer":2,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[3,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":2,"consumer":3,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"state","dtype":{"ExactAggregate":["Rate","Rate"]},"nullable":false},{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[3,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"SummaryState"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":3,"consumer":4,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[3,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":4,"consumer":5,"role":"Input","intermediate_schema":{"fields":[{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"state","dtype":{"ExactAggregate":["Sum","Sum"]},"nullable":false}],"time_index":null,"unique_keys":[[0]],"closed":true},"data_state":{"timing":"query_time","primitive":"SummaryState"},"grouping":"NotApplicable","window":"NotApplicable"}],"roots":[5],"scalar_roots":[]} diff --git a/crates/integration-tests/tests/main.rs b/crates/integration-tests/tests/main.rs index bebcbab67..9ee627c25 100644 --- a/crates/integration-tests/tests/main.rs +++ b/crates/integration-tests/tests/main.rs @@ -9,6 +9,7 @@ mod planning_common; mod aggregate; mod binary_op; mod crate_graph; +mod default_cost_plan_ranking; mod exact_reduce_execution; mod filtered_aggregates; mod finer_aggregate_reuse; From 466d497d8ba5cae720ce9e120bfc47c93e244171 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:56:46 +0000 Subject: [PATCH 02/15] fix(cost): charge each operation the executor's measured time per unit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The built-in formulas charged about one CPU operation (1 ns) per row for every operator, so a DDSketch built from raw rows at query time looked cheaper than the exact plan that keeps and sorts the same rows, while the executor runs it twice as slowly (217 ns against 111 ns per row). `node::operation_counts` holds the executor's measured time per unit, as CPU operations at the default 1 ns per operation: a row kept by a time range (12), a row grouped by a hash aggregate per key (15) and per measure (12, a counter-increase measure 84, an exact quantile plus 5.5 per sort level), a row folded into a state per family (DDSketch 210, KLL 95, exact 40, counter increase 84), a state merged per family (exact 230, counter increase 350, DDSketch 2,500, KLL 7,000), a group read out (KLL 35,000, other sketches 1,000, an exact finalize 40) and a state a chain emits (2,500). A merge over a time range of a chain's windows now takes the states' family from its θ or its output column, so it is charged (and looked up in a measured table) as that family. Tests that used Stage 3's selection to obtain a summary plan now cost summaries free (`summary_work_free`); pinned costs and the disk-tolerance scenario are re-derived at the new scale. --- crates/cost/src/node/analytical.rs | 35 +-- crates/cost/src/node/mod.rs | 4 +- crates/cost/src/node/operation_counts.rs | 213 ++++++++++++++++++ crates/cost/src/node/usage/mod.rs | 104 +++++++-- crates/cost/src/node/usage/tests.rs | 44 ++++ crates/integration-tests/src/lib.rs | 85 ++++++- .../integration-tests/tests/plan_sharing.rs | 32 ++- .../tests/planning_common/mod.rs | 10 +- .../tests/rollup_controlled.rs | 11 +- .../tests/stage3_objective.rs | 14 +- crates/planner/tests/runtime_install.rs | 26 ++- .../analytical-resource-cost.md | 8 +- 12 files changed, 526 insertions(+), 60 deletions(-) create mode 100644 crates/cost/src/node/operation_counts.rs diff --git a/crates/cost/src/node/analytical.rs b/crates/cost/src/node/analytical.rs index e476dea25..788d29137 100644 --- a/crates/cost/src/node/analytical.rs +++ b/crates/cost/src/node/analytical.rs @@ -4,6 +4,9 @@ //! separate. Plan selection (Stage 3) turns them into one objective with //! [`crate::ResourceUnitCost`]. +use crate::node::operation_counts::{ + HASH_AGGREGATE_OPERATIONS_PER_KEY, TIME_RANGE_OPERATIONS_PER_ROW, +}; use crate::resources::ModeledCpu; use asap_types::summary_type::{SketchAlgorithm, SketchParams}; use serde::{Deserialize, Serialize}; @@ -96,7 +99,9 @@ pub(crate) enum PhysicalOperator { }, HashAggregate { grouping_key_count: u64, - accumulator_count: u64, + /// CPU operations its measures cost per input row, all together + /// (`operation_counts::aggregate_measure_operations_per_row`). + accumulator_operations_per_row: u64, }, InMemoryComparisonSort { ordering_key_count: u64, @@ -443,7 +448,7 @@ pub(crate) fn estimate_operator( ), (PhysicalOperator::TimeShift, _) => ResourceEstimate::new(0.0, 0, 0), (PhysicalOperator::TimeRange, _) => ResourceEstimate::new( - output.rows as f64, + checked_cpu_product(output.rows, TIME_RANGE_OPERATIONS_PER_ROW)?, per_row_width(output.rows, output.bytes)?, 0, ), @@ -455,7 +460,7 @@ pub(crate) fn estimate_operator( ( PhysicalOperator::HashAggregate { grouping_key_count, - accumulator_count, + accumulator_operations_per_row, }, OperatorStatistics::HashAggregate { group_count, @@ -467,7 +472,8 @@ pub(crate) fn estimate_operator( checked_cpu_product( left.rows, grouping_key_count - .checked_add(accumulator_count) + .checked_mul(HASH_AGGREGATE_OPERATIONS_PER_KEY) + .and_then(|keys| keys.checked_add(accumulator_operations_per_row)) .ok_or(AnalyticalCostError::Overflow)?, )?, checked_bytes(&[ @@ -842,7 +848,7 @@ pub(crate) fn validate_operator_semantics( ( PhysicalOperator::HashAggregate { grouping_key_count, - accumulator_count, + accumulator_operations_per_row, }, OperatorStatistics::HashAggregate { group_count, @@ -856,7 +862,7 @@ pub(crate) fn validate_operator_semantics( "PromQL cross-series aggregation needs an explicit physical operator", ); } - if accumulator_count == 0 || *accumulator_bytes_per_group == 0 { + if accumulator_operations_per_row == 0 || *accumulator_bytes_per_group == 0 { return inconsistent("HashAggregate accumulator work and width must be positive"); } if grouping_key_count == 0 { @@ -1578,8 +1584,8 @@ mod tests { } /// A time shift only moves when its input is evaluated: it costs - /// nothing. A time range is priced by the rows it keeps, and keeps no - /// more than it reads. + /// nothing. A time range is priced by the rows it keeps (12 measured + /// 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( @@ -1598,7 +1604,10 @@ mod tests { }, ) .unwrap(); - assert_eq!(range.cpu_ops(), 200.0); + assert_eq!( + range.cpu_ops(), + 200.0 * TIME_RANGE_OPERATIONS_PER_ROW as f64 + ); assert!(estimate_operator( PhysicalOperator::TimeRange, OperatorStatistics::PassThrough { @@ -1630,7 +1639,7 @@ mod tests { fn aggregate_operator() -> PhysicalOperator { PhysicalOperator::HashAggregate { grouping_key_count: 1, - accumulator_count: 1, + accumulator_operations_per_row: 1, } } @@ -1792,7 +1801,7 @@ mod tests { estimate_operator( PhysicalOperator::HashAggregate { grouping_key_count: 0, - accumulator_count: 1, + accumulator_operations_per_row: 1, }, statistics, ), @@ -2126,7 +2135,7 @@ mod tests { let ungrouped = estimate_operator( PhysicalOperator::HashAggregate { grouping_key_count: 0, - accumulator_count: 1, + accumulator_operations_per_row: 1, }, OperatorStatistics::HashAggregate { edges: unary_edges(empty, EdgeStatistics { rows: 1, bytes: 8 }), @@ -2142,7 +2151,7 @@ mod tests { let grouped = estimate_operator( PhysicalOperator::HashAggregate { grouping_key_count: 1, - accumulator_count: 1, + accumulator_operations_per_row: 1, }, OperatorStatistics::HashAggregate { edges: unary_edges(empty, empty), diff --git a/crates/cost/src/node/mod.rs b/crates/cost/src/node/mod.rs index 1ab5a2605..d0feceb97 100644 --- a/crates/cost/src/node/mod.rs +++ b/crates/cost/src/node/mod.rs @@ -1,9 +1,11 @@ //! One node of a physical plan DAG: its resource usage and cost //! (`usage`), from the analytical per-operator resource model -//! ([`analytical`], `operator_statistics`) and the bytes a state holds +//! ([`analytical`], `operator_statistics`), the measured CPU operations per +//! unit of work (`operation_counts`) and the bytes a state holds //! ([`state_bytes`]). pub mod analytical; +pub(crate) mod operation_counts; pub(crate) mod operator_statistics; pub mod state_bytes; pub(crate) mod usage; diff --git a/crates/cost/src/node/operation_counts.rs b/crates/cost/src/node/operation_counts.rs new file mode 100644 index 000000000..93d1dc166 --- /dev/null +++ b/crates/cost/src/node/operation_counts.rs @@ -0,0 +1,213 @@ +//! The CPU operations the formulas charge per unit of work: per row a time +//! range keeps, per row a hash aggregate groups, per row folded into a +//! summary state, per state merged, per group read out. +//! +//! One operation is one nanosecond on one core at the default +//! [`ResourceSpeed`](crate::ResourceSpeed) (`ms_per_cpu_op` = 1e-6), so each +//! count below is the executor's measured time for that unit, in ns. What +//! plan selection needs is their ratios: a sketch built at query time is +//! cheaper than the exact plan only when its update is cheaper than +//! keeping and sorting the same rows. A slower or faster machine scales +//! them all through `ms_per_cpu_op`; a deployment that measures its own +//! (`CostInputs::measured_operation_costs`) replaces them node by node. +//! +//! Measured on one core of an idle server with the executor at commit +//! 59365ce80, in a release build: each query-time node of a plan run as the +//! only root over warm in-memory data, its own time the sub-plan's time +//! minus its inputs', median of 5 runs. Data: 1,000 series of a +//! non-negative gauge (`ts`, `value`, the series identity) and of a counter +//! (with a `job` label), 1 h windows at 1 Hz (3,600 rows per series, +//! tumbling windows of 10 s) and at 10 Hz (36,000 rows per series, windows +//! of 60 s). Each constant's own measurement is in its doc. +//! +//! Example: `quantile_over_time(0.99, latency[1h])` at 1 Hz reads 3.6 M +//! rows. The exact plan keeps them (`TIME_RANGE_OPERATIONS_PER_ROW`, 12), +//! groups them per series (`HASH_AGGREGATE_OPERATIONS_PER_KEY` + +//! `AGGREGATE_MEASURE_OPERATIONS_PER_ROW`, 27) and sorts each series' 3,600 +//! values (11.8 levels × `SORT_OPERATIONS_PER_ROW_PER_LEVEL`, 65): 104 ns per +//! row. A DDSketch built +//! from the same rows takes 12 + `DDSKETCH_UPDATE_OPERATIONS_PER_ROW` (210) +//! = 222 ns per row, about twice the exact plan, as measured (111 and 229). + +use asap_types::ir::operator::AggIntent; +use asap_types::ir::schema::FieldDataType; +use asap_types::summary_type::{ExactKind, SketchParams}; + +use crate::StateFamily; + +/// A row kept by a time range over raw rows, or a state read by a time +/// range over a chain's tumbling windows: 11–12 ns per row over 3.6 M and +/// 36 M rows, 8–18 ns per state over 360,000 states. +pub(crate) const TIME_RANGE_OPERATIONS_PER_ROW: u64 = 12; + +/// Per input row, for each grouping key of a hash aggregate: hashing the +/// key and finding its group. With `AGGREGATE_MEASURE_OPERATIONS_PER_ROW`, +/// a per-series `max_over_time` (one key, one measure) groups a row in +/// 27 ns. +pub(crate) const HASH_AGGREGATE_OPERATIONS_PER_KEY: u64 = 15; + +/// Per input row, for each plain measure of a hash aggregate (sum, count, +/// min, max, ...): folding the row's value into its group's. +pub(crate) const AGGREGATE_MEASURE_OPERATIONS_PER_ROW: f64 = 12.0; + +/// Per input row, per level of the sort that an exact quantile runs over +/// its group's values (`log2` of the rows per group levels): a per-series +/// exact quantile costs 72 ns per row more than a per-series max at 3,600 +/// rows per series (11.8 levels, 6.1 ns per level) and 69–97 ns more at +/// 36,000 (15.1 levels, 4.5–6.4 ns per level). +pub(crate) const SORT_OPERATIONS_PER_ROW_PER_LEVEL: f64 = 5.5; + +/// One row folded into a DDSketch state: 217 ns per row at 1 Hz and 203 at +/// 10 Hz at query time, 211 at ingestion time. +pub(crate) const DDSKETCH_UPDATE_OPERATIONS_PER_ROW: f64 = 210.0; + +/// One row folded into a KLL state: 106 ns per row at 1 Hz and 84 at +/// 10 Hz at query time, 94 at ingestion time. +pub(crate) const KLL_UPDATE_OPERATIONS_PER_ROW: f64 = 95.0; + +/// One row folded into an exact accumulator that keeps one value (sum, +/// count, min, max): an exact Max accumulator built per series at query +/// time takes 40 ns per row. +pub(crate) const EXACT_UPDATE_OPERATIONS_PER_ROW: f64 = 40.0; + +/// One row folded into an exact accumulator of a counter's increase (rate, +/// increase, irate), which also tracks resets and the first and last +/// samples: 84 ns per row at query time, 84–89 at ingestion time. A hash +/// aggregate's measure over a series' samples in time order (rate, delta, +/// deriv, ...) is charged the same: it does the same work per row, and the +/// executor implements no such measure to time. +pub(crate) const COUNTER_UPDATE_OPERATIONS_PER_ROW: f64 = 84.0; + +/// One counter update of any other sketch, per counter its row updates +/// (`state_bytes::summary_shape`: Count-Min and Count Sketch one per row of +/// depth, plus the heap; one for HyperLogLog and KMV), above +/// `EXACT_UPDATE_OPERATIONS_PER_ROW`, the executor's cost of handing a row +/// to its group's state. Not measured on the executor: `asap_sketchlib`'s +/// own Count-Min Sketch of depth 3 with a top-k heap updates in about +/// 72 ns, 18 ns per counter. +pub(crate) const SKETCH_COUNTER_UPDATE_OPERATIONS: f64 = 18.0; + +/// One exact accumulator state merged into its group's (sum, count, min, +/// max): 128–265 ns per state over 360,000 states. +pub(crate) const EXACT_MERGE_OPERATIONS_PER_STATE: f64 = 230.0; + +/// One counter-increase accumulator state merged (rate, increase, irate): +/// 346 ns per state over 360,000 states. +pub(crate) const COUNTER_MERGE_OPERATIONS_PER_STATE: f64 = 350.0; + +/// One DDSketch state merged: 1.8 µs per state of 10 rows, 2.5 µs per +/// state of 600 rows. +pub(crate) const DDSKETCH_MERGE_OPERATIONS_PER_STATE: f64 = 2_500.0; + +/// One KLL state merged: 5.4 µs per state of 10 rows, 8.8 µs per state of +/// 600 rows. +pub(crate) const KLL_MERGE_OPERATIONS_PER_STATE: f64 = 7_000.0; + +/// Per byte of any other sketch state merged, above +/// `EXACT_MERGE_OPERATIONS_PER_STATE`: merging adds every counter of the +/// input state. Not measured on the executor: `asap_sketchlib` merges a +/// Count-Min Sketch of 16 KB in about 6 µs and a HyperLogLog of 16 KB in +/// about 13 µs, 0.4–0.8 ns per byte. +pub(crate) const SKETCH_MERGE_OPERATIONS_PER_BYTE: f64 = 1.0; + +/// One group's answer read from a KLL state: 19–79 µs per group of states +/// folding 3,600 rows, 35 µs folding 36,000. +pub(crate) const KLL_ESTIMATE_OPERATIONS_PER_GROUP: f64 = 35_000.0; + +/// One group's answer read from any other sketch state: 0–2.6 µs per +/// DDSketch quantile, within the measurement's resolution (±3 ms per 1,000 +/// groups); `asap_sketchlib`'s own DDSketch quantile takes 0.15 µs and a +/// Count-Min Sketch top-k query 0.5–8 µs. +pub(crate) const SKETCH_ESTIMATE_OPERATIONS_PER_GROUP: f64 = 1_000.0; + +/// One exact accumulator finalized into its group's value: reading one +/// state, charged as folding one row into it +/// (`EXACT_UPDATE_OPERATIONS_PER_ROW`); measured within the resolution +/// above, so it costs no more than a hash aggregate's measure producing the +/// same value. +pub(crate) const FINALIZE_OPERATIONS_PER_GROUP: f64 = EXACT_UPDATE_OPERATIONS_PER_ROW; + +/// Per state a tumbling-window chain completes and emits at the end of each +/// window, beyond its rows' updates: fitting each window's time to its rows +/// and its 1,000 states at 1 Hz (10 s windows of 10,000 rows) and at +/// 10 Hz (60 s windows of 600,000 rows), 1.9 µs per exact Rate state, +/// 2.8 µs per KLL state and 3.4 µs per DDSketch state. +pub(crate) const CHAIN_EMIT_OPERATIONS_PER_STATE: f64 = 2_500.0; + +/// The operations one input row costs folded into a state of `summary_type` +/// with `parameters`, whose kernel updates `counter_updates` counters per +/// row (`state_bytes::summary_shape`'s first value). +pub(crate) fn update_operations_per_row( + summary_type: &FieldDataType, + parameters: Option<&SketchParams>, + counter_updates: f64, +) -> f64 { + match (summary_type, parameters) { + (FieldDataType::ExactAggregate(kind, _), _) if is_counter_increase(kind) => { + COUNTER_UPDATE_OPERATIONS_PER_ROW + } + (FieldDataType::ExactAggregate(..), _) => EXACT_UPDATE_OPERATIONS_PER_ROW, + (_, Some(SketchParams::DDSketch { .. })) => DDSKETCH_UPDATE_OPERATIONS_PER_ROW, + (_, Some(SketchParams::Kll { .. })) => KLL_UPDATE_OPERATIONS_PER_ROW, + _ => EXACT_UPDATE_OPERATIONS_PER_ROW + SKETCH_COUNTER_UPDATE_OPERATIONS * counter_updates, + } +} + +/// The operations one input state of `family` holding `state_bytes` costs +/// merged into its group's state. +pub(crate) fn merge_operations_per_state(family: StateFamily<'_>, state_bytes: u64) -> f64 { + match family { + StateFamily::Exact(kind) if is_counter_increase(kind) => COUNTER_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, + StateFamily::Sketch(_) => { + EXACT_MERGE_OPERATIONS_PER_STATE + SKETCH_MERGE_OPERATIONS_PER_BYTE * state_bytes as f64 + } + } +} + +/// The operations reading one group's answer from a state of `family` +/// costs (an exact accumulator's: finalizing it). +pub(crate) fn estimate_operations_per_group(family: StateFamily<'_>) -> f64 { + match family { + StateFamily::Sketch(SketchParams::Kll { .. }) => KLL_ESTIMATE_OPERATIONS_PER_GROUP, + StateFamily::Sketch(_) => SKETCH_ESTIMATE_OPERATIONS_PER_GROUP, + StateFamily::Exact(_) | StateFamily::None => FINALIZE_OPERATIONS_PER_GROUP, + } +} + +/// The operations one input row costs a hash aggregate's `measure` over +/// groups of `rows_per_group` rows: an exact quantile keeps the row and +/// sorts its group (`log2(rows_per_group)` levels); a measure over a +/// series' samples in time order costs a counter-increase update; any +/// other folds the value. +pub(crate) fn aggregate_measure_operations_per_row( + measure: &AggIntent, + rows_per_group: u64, +) -> f64 { + match measure { + AggIntent::Quantile { .. } => { + AGGREGATE_MEASURE_OPERATIONS_PER_ROW + + (rows_per_group.max(2) as f64).log2() * SORT_OPERATIONS_PER_ROW_PER_LEVEL + } + AggIntent::Rate + | AggIntent::IRate + | AggIntent::Increase + | AggIntent::Changes + | AggIntent::Delta + | AggIntent::IDelta + | AggIntent::Deriv + | AggIntent::Resets + | AggIntent::PredictLinear { .. } => COUNTER_UPDATE_OPERATIONS_PER_ROW, + _ => AGGREGATE_MEASURE_OPERATIONS_PER_ROW, + } +} + +/// Whether an exact accumulator of `kind` tracks a counter's increase. +fn is_counter_increase(kind: &ExactKind) -> bool { + matches!( + kind, + ExactKind::Rate | ExactKind::Increase | ExactKind::IRate + ) +} diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index efc2bd383..e65da88d1 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -22,6 +22,10 @@ use std::collections::{BTreeMap, BTreeSet}; use crate::node::analytical::{ estimate_operator, AnalyticalCostError, PhysicalOperator, ResourceEstimate, }; +use crate::node::operation_counts::{ + aggregate_measure_operations_per_row, estimate_operations_per_group, + merge_operations_per_state, update_operations_per_row, CHAIN_EMIT_OPERATIONS_PER_STATE, +}; use crate::node::operator_statistics::{ EdgeStatistics, OperatorStatistics, PartitionStatistics, UnaryEdgeStatistics, }; @@ -115,11 +119,16 @@ fn state_family<'a>( | FieldDataType::Wavelet(..) | FieldDataType::StatModel(..) => StateFamily::None, }, - Payload::ASAP( - ASAPOp::SummaryMerge { .. } - | ASAPOp::SummaryEstimate { .. } - | ASAPOp::FinalizeExactAccumulator { .. }, - ) => producer_family.unwrap_or(StateFamily::None), + // A merge over a time range of a chain's windows reads states whose + // producer (the time range) has no family: they are its own θ's, or + // the exact accumulator its output column holds. + Payload::ASAP(ASAPOp::SummaryMerge { .. }) => match producer_family { + None | Some(StateFamily::None) => merged_state_family(node), + Some(family) => family, + }, + Payload::ASAP(ASAPOp::SummaryEstimate { .. } | ASAPOp::FinalizeExactAccumulator { .. }) => { + producer_family.unwrap_or(StateFamily::None) + } Payload::NonASAP( NonASAPOp::Scan { .. } | NonASAPOp::Values { .. } @@ -153,6 +162,28 @@ fn state_family<'a>( }) } +/// The family of the states merge `node` outputs: its θ for a sketch, else +/// the exact accumulator of its output's state column, else none. +fn merged_state_family( + node: &asap_types::plan::physical_asap_dag::PhysicalASAPDAGNode, +) -> StateFamily<'_> { + if let Some(parameters) = &node.parameters { + return StateFamily::Sketch(parameters); + } + node.output_schema + .fields + .iter() + .find_map(|field| match &field.dtype { + FieldDataType::ExactAggregate(kind, _) => Some(StateFamily::Exact(kind)), + FieldDataType::Plain(_) + | FieldDataType::Sketch(..) + | FieldDataType::SummarySample(..) + | FieldDataType::Wavelet(..) + | FieldDataType::StatModel(..) => None, + }) + .unwrap_or(StateFamily::None) +} + /// One producer of a node being costed ([`node_resource_usage_and_cost`]): its payload and /// schema, its output statistics, the sources it reads and its state /// family. @@ -637,10 +668,16 @@ pub fn node_resource_usage_and_cost<'a>( }; let out = edge(group_count)?; let held = input.rows.div_ceil(group_count.max(1)); + // Its measures' work per row, by kind (an exact quantile + // also sorts its group of `held` rows); at least one. + let measure_ops: f64 = measures + .iter() + .map(|measure| aggregate_measure_operations_per_row(measure, held)) + .sum(); let estimate = estimate_operator( PhysicalOperator::HashAggregate { grouping_key_count: keys, - accumulator_count: measures.len().max(1) as u64, + accumulator_operations_per_row: (measure_ops.ceil() as u64).max(1), }, OperatorStatistics::HashAggregate { edges: unary(out), @@ -665,7 +702,8 @@ pub fn node_resource_usage_and_cost<'a>( out, estimate, format!( - "hash aggregate {} rows into {group_count} groups", + "hash aggregate {} rows into {group_count} groups: {measure_ops:.0} \ + measure ops per row", input.rows ), ) @@ -842,8 +880,11 @@ pub fn node_resource_usage_and_cost<'a>( non_negative, value_magnitude_range, }; - let (depth, state_bytes) = summary_shape(family, node.parameters.as_ref(), input_rows)?; + let (counter_updates, state_bytes) = + summary_shape(family, node.parameters.as_ref(), input_rows)?; let state_bytes = measured_state_bytes.unwrap_or(state_bytes); + let update_ops = + update_operations_per_row(family, node.parameters.as_ref(), counter_updates); let out = EdgeStatistics { rows: group_count, bytes: group_count @@ -853,12 +894,12 @@ pub fn node_resource_usage_and_cost<'a>( ( out, Ok(ResourceEstimate::new( - input.rows as f64 * depth, + input.rows as f64 * update_ops, out.bytes, 0, )), format!( - "build {} into {group_count} states: {} rows x depth {depth}", + "build {} into {group_count} states: {} rows x {update_ops} ops", summary_type_name(family), input.rows ), @@ -890,8 +931,21 @@ pub fn node_resource_usage_and_cost<'a>( non_negative, value_magnitude_range, }; - let (depth, state_bytes) = summary_shape(family, node.parameters.as_ref(), input_rows)?; + let (counter_updates, state_bytes) = + summary_shape(family, node.parameters.as_ref(), input_rows)?; let state_bytes = measured_state_bytes.unwrap_or(state_bytes); + let update_ops = + update_operations_per_row(family, node.parameters.as_ref(), counter_updates); + // 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 + // window's. + let emitted_states = match ingestion { + true => { + group_count as f64 * ONE_SECOND_MS as f64 / *tumbling_window_width_ms as f64 + } + false => group_count as f64 * retained_output_count as f64, + }; let out = EdgeStatistics { rows: group_count, bytes: group_count @@ -901,13 +955,15 @@ pub fn node_resource_usage_and_cost<'a>( ( out, Ok(ResourceEstimate::new( - input.rows as f64 * (depth + 1.0), + input.rows as f64 * update_ops + + emitted_states * CHAIN_EMIT_OPERATIONS_PER_STATE, out.bytes, 0, )), format!( "build {} in tumbling windows of {tumbling_window_width_ms} ms for \ - {group_count} groups: {} rows x (1 + depth {depth})", + {group_count} groups: {} rows x {update_ops} ops, {emitted_states:.1} \ + states emitted", summary_type_name(family), input.rows ), @@ -929,6 +985,14 @@ pub fn node_resource_usage_and_cost<'a>( ); } let states = merged_states(producers)?; + // Each input state's merge, by the states' family and bytes. + let merge_ops: f64 = producers + .iter() + .map(|producer| { + let state_bytes = producer.output.bytes.div_ceil(producer.output.rows.max(1)); + producer.output.rows as f64 * merge_operations_per_state(family, state_bytes) + }) + .sum(); if let Some(keys) = group_by { group_count = group_count.min(groups(&Reduction::by(keys.clone()))?); } @@ -942,7 +1006,7 @@ pub fn node_resource_usage_and_cost<'a>( }; ( out, - Ok(ResourceEstimate::new(states as f64, out.bytes, 0)), + Ok(ResourceEstimate::new(merge_ops, out.bytes, 0)), format!("merge {states} states into {group_count}"), ) } @@ -963,13 +1027,21 @@ pub fn node_resource_usage_and_cost<'a>( )?; ( out, - Ok(ResourceEstimate::new(out.rows as f64, 0, 0)), + Ok(ResourceEstimate::new( + input.rows as f64 * estimate_operations_per_group(family), + 0, + 0, + )), format!("estimate {} groups x {per_group}", input.rows), ) } Payload::ASAP(ASAPOp::FinalizeExactAccumulator { .. }) => ( edge(input.rows)?, - Ok(ResourceEstimate::new(input.rows as f64, 0, 0)), + Ok(ResourceEstimate::new( + input.rows as f64 * estimate_operations_per_group(family), + 0, + 0, + )), format!("finalize {} accumulators", input.rows), ), Payload::ASAP( diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index efd664bf0..c674eadeb 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -183,6 +183,50 @@ fn an_operator_keeping_its_input_window_over_a_chain_or_its_windows_selects_what } } +/// A merge over a time range of a DDSketch chain's windows reads DDSketch +/// states, though the time range itself has no family: the merge's θ names +/// it, so each of the 5,000 states read costs a DDSketch merge (2,500 +/// operations), not an exact accumulator's (230). +#[test] +fn a_merge_over_a_chain_s_windows_is_costed_by_the_chain_s_family() { + let settings = CostInputs::default(); + let data = DataWorkload::default(); + let windows = node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(300), + kind: TimeRangeKind::Range, + child: 0, + })); + let windows_output = ProducerOutput { + payload: &windows.payload, + output_schema: &windows.output_schema, + output: EdgeStatistics { + rows: 5_000, + bytes: 5_000 * 4_096, + }, + sources: BTreeSet::new(), + family: StateFamily::None, + unchanged_columns: BTreeSet::new(), + tumbling_windows: 5, + chain_tumbling_window_width_ms: None, + }; + let mut merge = node(Payload::ASAP(ASAPOp::SummaryMerge { + children: vec![0], + group_by: None, + })); + merge.parameters = Some(SketchParams::DDSketch { alpha: 0.01 }); + let merge_usage = + node_resource_usage_and_cost(&merge, &[windows_output], (1, 1.0), None, &data, &settings) + .unwrap(); + assert!(matches!( + merge_usage.family, + StateFamily::Sketch(SketchParams::DDSketch { .. }) + )); + assert_eq!( + merge_usage.cpu_ops_per_second, + 5_000.0 * crate::node::operation_counts::DDSKETCH_MERGE_OPERATIONS_PER_STATE + ); +} + /// A query-time node holds its state only /// while it computes: bytes × its work per execution × its executions /// per second, with no query latency. A KLL state of 2 MB whose own diff --git a/crates/integration-tests/src/lib.rs b/crates/integration-tests/src/lib.rs index 813deaa4e..cbe44839a 100644 --- a/crates/integration-tests/src/lib.rs +++ b/crates/integration-tests/src/lib.rs @@ -245,14 +245,97 @@ pub mod planning { /// For tests of a rewrite's structure or execution over a summary whose /// output has no guarantee that meets `accuracy` (e.g. a value filter /// over an estimate), not of accuracy acceptance. + /// + /// Summaries are costed free ([`summary_work_free`]), so the plan reads + /// the summary candidate wherever Stage 1 offers one, whether or not the + /// default unit costs make it cheaper than the exact plan on these + /// tests' small data. pub fn logical_candidates_ignoring_guarantees( root: Rc, accuracy: AccuracyTarget, ) -> Vec> { - let models = asap_planner::PlanningModels::builtin().with_ignore_accuracy_guarantees(true); + let settings = summary_work_free(); + let models = asap_planner::PlanningModels::builtin() + .with_ignore_accuracy_guarantees(true) + .with_cost_inputs(&settings); selected_operator_roots(&run_all_stages_with(root, accuracy, models)) } + /// Default cost inputs with a measured table that prices every summary + /// operator (build, chain, merge, estimate, finalize) of every sketch + /// and exact accumulator at 0 ns: for tests of a summary plan's shape, + /// execution or reuse, which need Stage 3 to select the summary. + pub fn summary_work_free() -> asap_cost::CostInputs { + use asap_cost::{OperatorCost, OperatorKind}; + use asap_types::summary_type::{ExactKind, SketchParams}; + let sketches = [ + SketchParams::Kll { k: 200 }, + SketchParams::DDSketch { alpha: 0.01 }, + SketchParams::Cms { + width: 1024, + depth: 3, + }, + SketchParams::CmsWithHeap { + width: 1024, + depth: 3, + heap_size: 32, + }, + SketchParams::CountSketch { + width: 1024, + depth: 3, + }, + SketchParams::CountSketchWithHeap { + width: 1024, + depth: 3, + heap_size: 32, + }, + SketchParams::Hll { precision: 12 }, + SketchParams::Kmv { k: 1024 }, + SketchParams::UnivMon { + heap_size: 32, + sketch_rows: 3, + sketch_cols: 1024, + layers: 8, + }, + ]; + let exact = [ + ExactKind::Sum, + ExactKind::Count, + ExactKind::Min, + ExactKind::Max, + ExactKind::Increase, + ExactKind::Rate, + ExactKind::IRate, + ]; + let families = sketches + .into_iter() + .map(|sketch| (Some(sketch), None)) + .chain(exact.into_iter().map(|kind| (None, Some(kind)))); + let mut settings = asap_cost::CostInputs::default(); + settings.measured_operation_costs.operators = families + .flat_map(|(sketch, exact)| { + [ + OperatorKind::SummaryAgg, + OperatorKind::SummaryTumblingWindowChain, + OperatorKind::SummaryMerge, + OperatorKind::SummaryEstimate, + OperatorKind::FinalizeExactAccumulator, + ] + .map(|operator| OperatorCost { + operator, + phase: None, + sketch: sketch.clone(), + exact, + measure: None, + ns_per_unit: 0.0, + ns_per_invocation: 0.0, + memory_bytes_per_state: None, + }) + }) + .collect(); + settings + } + /// Whether `node` builds a summary. fn is_build(node: &Rc) -> bool { matches!( diff --git a/crates/integration-tests/tests/plan_sharing.rs b/crates/integration-tests/tests/plan_sharing.rs index f113ca7e5..0cf1db234 100644 --- a/crates/integration-tests/tests/plan_sharing.rs +++ b/crates/integration-tests/tests/plan_sharing.rs @@ -1,7 +1,7 @@ //! Workloads whose queries share computation, planned through the production //! stages: adding a query that can read an existing result must not cost //! more than planning the first query alone. -use crate::planning_common::{cost, fuzz_workload}; +use crate::planning_common::{cost, cost_with, fuzz_workload}; use serde_json::json; /// Deriving the coarse sum costs only a sum over the 150 fine groups. @@ -49,12 +49,25 @@ fn slower_repeat_reuses_faster_sum() { ); } -/// The filtered query avoids its independent scan and aggregation. +/// The filtered query reads the by-city quantile's summary states instead +/// of its own scan and aggregation: with summaries costed free (so the +/// sketch plans are selected, as under the earlier unit costs), it adds +/// at most 0.1% to the by-city quantile's cost. Under the default unit +/// costs both are exact plans, and no rule reads the filtered quantile +/// from the by-city exact values. #[test] fn grouped_filtered_quantile_plans() { + let settings = asap_integration_tests::planning::summary_work_free(); + let w = fuzz_workload("percentile_filtered_and_by_city", true); + let mut grouped = w.clone(); + grouped.queries = vec![w.queries[2].clone()]; + let grouped_cost = cost_with(&grouped, &settings); + let mut filtered_and_grouped = w.clone(); + filtered_and_grouped.queries.remove(0); + let together = cost_with(&filtered_and_grouped, &settings); assert!( - cost(&fuzz_workload("percentile_filtered_and_by_city", true)) < 0.25, - "filtered query must reuse the grouped result" + together <= grouped_cost * 1.001, + "filtered query must reuse the grouped result: {together} vs {grouped_cost}" ); } @@ -117,11 +130,18 @@ fn identical_exact_percentiles_share_work() { ); } -/// Different ranks share the source window but still compute their own values. +/// Different ranks share the source window but still compute their own +/// values: together they cost less than twice one of them. #[test] fn different_exact_percentiles_share_the_input_window() { let mut related = fuzz_workload("identical_percentiles", false); related.queries[1].entry.query.0 = related.queries[1].entry.query.0.replace("0.99", "0.5"); let related_cost = cost(&related); - assert!(related_cost <= 0.0883, "{related_cost}"); + let mut base = related.clone(); + base.queries.truncate(1); + let base_cost = cost(&base); + assert!( + related_cost < 2.0 * base_cost * (1.0 - 1e-6), + "{related_cost} vs {base_cost}" + ); } diff --git a/crates/integration-tests/tests/planning_common/mod.rs b/crates/integration-tests/tests/planning_common/mod.rs index 809bcbc80..3d0f2de6a 100644 --- a/crates/integration-tests/tests/planning_common/mod.rs +++ b/crates/integration-tests/tests/planning_common/mod.rs @@ -48,13 +48,17 @@ pub fn with_queries(mut w: FuzzWorkload, queries: &[String]) -> FuzzWorkload { /// The selected plan's cost per second for `w`. pub fn cost(w: &FuzzWorkload) -> f64 { + cost_with(w, &CostInputs::default()) +} + +/// [`cost`] under `settings`. +pub fn cost_with(w: &FuzzWorkload, settings: &CostInputs) -> f64 { let roots = Planner::new(FuzzSettings::default()).lower(w).unwrap(); let planning = w.planning_workload(); let entries: Vec<_> = planning.query_workload.entries().collect(); - let settings = CostInputs::default(); let deployment = w.deployment(); let models = PlanningModels::builtin() - .with_cost_inputs(&settings) + .with_cost_inputs(settings) .with_deployment_capabilities(&deployment); let run = run_search_over( roots, @@ -66,7 +70,7 @@ pub fn cost(w: &FuzzWorkload) -> f64 { .unwrap(); let (plan, _) = run.run.selection.as_ref().unwrap(); let value = - asap_integration_tests::planning::plan_cost_per_second(plan, &entries, &w.data, &settings); + asap_integration_tests::planning::plan_cost_per_second(plan, &entries, &w.data, settings); println!("cost={value}"); value } diff --git a/crates/integration-tests/tests/rollup_controlled.rs b/crates/integration-tests/tests/rollup_controlled.rs index 7f05e3911..086fe1e74 100644 --- a/crates/integration-tests/tests/rollup_controlled.rs +++ b/crates/integration-tests/tests/rollup_controlled.rs @@ -41,7 +41,9 @@ fn nodes(roots: &[QueryRoot]) -> Vec> { .collect() } -// The production entrypoint must find a sketch rollup under the viewer budget. +// The production entrypoint must find a sketch rollup under the viewer +// budget: 2e-5 US dollars per second at AWS Fargate prices, about 1.8 times +// the selected KMV rollup's 1.13e-5 under the measured unit costs. #[test] fn production_distinct_rollup_finds_shared_kmv() { let (workload, roots) = fixture(); @@ -68,10 +70,9 @@ fn production_distinct_rollup_finds_shared_kmv() { assert!(accepted .iter() .all(|accuracy| accuracy.sketches.iter().any(|sketch| sketch == "Kmv"))); - assert!( - asap_integration_tests::planning::plan_cost_per_second(plan, &entries, data, &settings) - < 2.0e-6 - ); + let cost_per_second = + asap_integration_tests::planning::plan_cost_per_second(plan, &entries, data, &settings); + assert!(cost_per_second < 2.0e-5, "{cost_per_second}"); let roots = plan.query_roots().unwrap(); let builds: HashSet<_> = nodes(&roots) .iter() diff --git a/crates/integration-tests/tests/stage3_objective.rs b/crates/integration-tests/tests/stage3_objective.rs index 51100e33f..fb8494984 100644 --- a/crates/integration-tests/tests/stage3_objective.rs +++ b/crates/integration-tests/tests/stage3_objective.rs @@ -193,12 +193,14 @@ fn offset_windows_without_raw_data_select_installable_plans_costed_for_their_ret /// Unit costs with a disk price like a cloud block volume's (3.1e-17 per /// byte-second): what keeping raw data on disk costs per second is below -/// 1e-9, where the solver drops a constraint coefficient as zero. +/// 1e-9, where the solver drops a constraint coefficient as zero. CPU is +/// priced at half AWS Fargate's vCPU-second (`cost_per_cpu_op` 5.622e-15), +/// so that disk decides between the plans below. fn small_disk_costs() -> CostInputs { CostInputs { cost_model: CostModel::UsageOverTime(ResourceUnitCost { - cost_per_cpu_op: 1.1244e-14, - cost_per_scanned_byte: 1.1244e-15, + cost_per_cpu_op: 5.622e-15, + cost_per_scanned_byte: 5.622e-16, cost_per_memory_byte_second: 1.150183379650116e-15, cost_per_disk_byte_second: 3.1e-17, ..ResourceUnitCost::illustrative() @@ -212,9 +214,9 @@ fn small_disk_costs() -> CostInputs { /// charged at 3.1e-17 per byte-second: raw data on disk costs about 2e-9 /// per second. Its row carries the objective's scale, so the solver keeps /// it, and the plan selected costs less than the one selected when disk -/// is free, costed at the disk price. With the scale on the raw-data -/// variable's objective coefficient instead, the solver dropped the -/// unscaled coefficients and selected that plan, 57% dearer. +/// is free, costed at the disk price (that one is 50% dearer). With the +/// scale on the raw-data variable's objective coefficient instead, the +/// solver dropped the unscaled coefficients and selected that plan. #[test] fn raw_data_on_disk_costing_less_than_the_solver_tolerance_still_steers_selection() { let w = workload(&[ diff --git a/crates/planner/tests/runtime_install.rs b/crates/planner/tests/runtime_install.rs index 4fba7b99c..3821a31e5 100644 --- a/crates/planner/tests/runtime_install.rs +++ b/crates/planner/tests/runtime_install.rs @@ -468,17 +468,29 @@ fn query_answers(plan: &InstallablePlan, samples: &[RawDataSample]) -> Vec Date: Sun, 15 Nov 2026 16:19:31 +0000 Subject: [PATCH 03/15] test(cost): name the executor commit the ranking expectations were measured at --- crates/integration-tests/tests/default_cost_plan_ranking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/integration-tests/tests/default_cost_plan_ranking.rs b/crates/integration-tests/tests/default_cost_plan_ranking.rs index d67c560c0..3b7fd0d39 100644 --- a/crates/integration-tests/tests/default_cost_plan_ranking.rs +++ b/crates/integration-tests/tests/default_cost_plan_ranking.rs @@ -4,7 +4,7 @@ //! The expectations are the executor's measured total CPU (query plus //! ingestion) per second of wall time, for each plan kind of each workload //! below: measured on one core of an idle server, in a release build, -//! serially, with the in-repository executor over the synthetic data the +//! serially, with the executor at commit 59365ce80 over the synthetic data the //! workloads declare (`latency` a non-negative gauge, `requests_total` a //! counter, 10 jobs), every accuracy target 1% relative at δ = 0.01. //! From fb65262fa876a5466363199c4a90265fecfc3f4a Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:01:13 +0000 Subject: [PATCH 04/15] test(cost): compaction latency charges each merge its family's merge time; Rate chain merge family The compaction test fails before the fix: the newest window's compaction of stored DDSketch windows over 100,000 series is predicted at 0.2 ms (1 operation per merge) against at least 180 ms of measured merges. --- crates/cost/src/node/usage/tests.rs | 46 +++++++++++++++++++ .../tests/default_cost_plan_ranking.rs | 35 ++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index c674eadeb..8ad547328 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -4,6 +4,7 @@ use std::time::Duration; use asap_types::ir::properties::ExecutionDataState; use asap_types::ir::scalar::ColumnRef; +use asap_types::ir::schema::Field; use asap_types::plan::physical_asap_dag::PhysicalASAPDAGNode; use asap_types::plan::time_window::{InputTimeAccess, TimeIndexedPlan}; use asap_types::summary_type::{ExactKind, ExactParams, SummaryUpdate}; @@ -227,6 +228,51 @@ fn a_merge_over_a_chain_s_windows_is_costed_by_the_chain_s_family() { ); } +/// A merge over a time range of a Rate chain's windows reads Rate +/// accumulator states, though the time range has no family and the merge +/// no θ: its output's state column names the family, so each of the 5,000 +/// states read costs a counter-increase merge (350 operations), not a +/// plain exact accumulator's (230). +#[test] +fn a_merge_over_a_rate_chain_s_windows_is_costed_by_the_rate_accumulator() { + let settings = CostInputs::default(); + let data = DataWorkload::default(); + let windows = node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(300), + kind: TimeRangeKind::Range, + child: 0, + })); + let windows_output = ProducerOutput { + payload: &windows.payload, + output_schema: &windows.output_schema, + output: EdgeStatistics { + rows: 5_000, + bytes: 5_000 * asap_types::summary_type::EXACT_ACCUMULATOR_STATE_BYTES, + }, + sources: BTreeSet::new(), + family: StateFamily::None, + unchanged_columns: BTreeSet::new(), + tumbling_windows: 5, + }; + let mut merge = node(Payload::ASAP(ASAPOp::SummaryMerge { + children: vec![0], + group_by: None, + })); + merge.output_schema = Schema::new(vec![Field::new( + "state", + FieldDataType::ExactAggregate(ExactKind::Rate, ExactParams::Rate), + false, + )]); + let merge_usage = + node_resource_usage_and_cost(&merge, &[windows_output], (1, 1.0), None, &data, &settings) + .unwrap(); + assert_eq!(merge_usage.family, StateFamily::Exact(&ExactKind::Rate)); + assert_eq!( + merge_usage.cpu_ops_per_second, + 5_000.0 * crate::node::operation_counts::COUNTER_MERGE_OPERATIONS_PER_STATE + ); +} + /// A query-time node holds its state only /// while it computes: bytes × its work per execution × its executions /// per second, with no query latency. A KLL state of 2 MB whose own diff --git a/crates/integration-tests/tests/default_cost_plan_ranking.rs b/crates/integration-tests/tests/default_cost_plan_ranking.rs index 3b7fd0d39..119c5db33 100644 --- a/crates/integration-tests/tests/default_cost_plan_ranking.rs +++ b/crates/integration-tests/tests/default_cost_plan_ranking.rs @@ -442,3 +442,38 @@ fn predicted_cpu_orders_exact_and_alternative_plans_as_measured() { } assert!(wrong.is_empty(), "misranked:\n{}", wrong.join("\n")); } + +/// A query reading stored windows waits for the newest window's +/// compaction: each ingest worker beyond the first builds its own partial +/// copy of that window, merged at query time, one merge per state per extra +/// worker. Example: the stored DDSketch windows of the quantile over +/// 100,000 series sampled every 10 ms (10 M rows per second, at 210 ns per +/// row more than one core of ingestion work) merge at least one extra copy +/// of each of the window's 100,000 states, and a DDSketch merge measures +/// 1.8–2.5 µs: at least 180 ms. One operation (1 ns) per merge would +/// predict 0.2 ms. +#[test] +fn compaction_latency_charges_each_merge_its_family_s_merge_time() { + let (plan, _) = selected(&workload(QUANTILE, TEN_HZ_EVERY_60_S), false); + assert_eq!(plan_kind(&plan), PlanKind::StoredStates); + assert!(has_sketch(&plan)); + let mut heavy = serde_json::to_value(workload(QUANTILE, TEN_HZ_EVERY_60_S)).unwrap(); + let data = &mut heavy["data_workload"]; + data["arrival"]["ingesting"]["rows_per_second"] = serde_json::json!(10_000_000.0); + data["sources"]["latency"]["series_count"] = serde_json::json!(100_000); + data["sources"]["latency"]["sample_interval"] = serde_json::json!(10); + let heavy: PlanningWorkload = serde_json::from_value(heavy).unwrap(); + let entries: Vec<_> = heavy.query_workload.entries().collect(); + let (cost, _) = asap_cost::plan_cost( + &plan, + &entries, + heavy.data_workload.as_ref().unwrap(), + &CostInputs::default(), + ) + .unwrap(); + let compaction_ms = cost.usage.latency_per_query[0].compaction_ms; + assert!( + compaction_ms >= 100_000.0 * 1.8e-3, + "predicted compaction {compaction_ms} ms, measured merges take at least 180 ms" + ); +} From 213a14c1aa4de7ba6bff7d21f10282ecd79d85f2 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:01:40 +0000 Subject: [PATCH 05/15] fix(cost): per-family compaction merges, one CPU-operation unit, review cleanups - Compaction latency charges each merge its state's family merge time (the measured entry, else the merge formula at its bytes per state), not one operation; the formula-merge parameter is gone. - operation_counts is now operations_per_unit; its doc and ResourceSpeed::ms_per_cpu_op define one CPU operation as one ns of the reference executor's work and list the operators still at 1 per unit. measured.rs says the formulas are the built-in fallback a measured table overrides. - rate, irate, increase, deriv and predict_linear measures in a hash aggregate are charged the Rate accumulator's per-row work as an estimate; changes, delta, idelta and resets as plain measures. - ExactKind::tracks_counter_increase replaces the copies in the cost model and Stage 2. - The counters a sketch row updates move to operations_per_unit; summary_state_bytes / sketch_state_bytes return bytes only. - HashAggregate carries its whole per-row cost (keys and measures), computed in one function; all unit constants are f64. - summary_cpu_free (was summary_work_free) lists every sketch family, exact kind and no family, checked by exhaustive matches. - Tests: rollup_controlled compares the KMV rollup with the exact plan's cost; different_exact_percentiles_share_the_input_window checks the shared input node; grouped_filtered_quantile_plans pins today's no-reuse under the default costs (#1358). --- crates/cost/src/measured.rs | 6 + crates/cost/src/model.rs | 9 +- crates/cost/src/node/analytical.rs | 42 ++---- crates/cost/src/node/mod.rs | 4 +- ...ation_counts.rs => operations_per_unit.rs} | 141 ++++++++++++------ crates/cost/src/node/state_bytes/mod.rs | 122 ++++++--------- crates/cost/src/node/state_bytes/tests.rs | 15 +- crates/cost/src/node/usage/mod.rs | 48 +++--- crates/cost/src/node/usage/tests.rs | 4 +- crates/cost/src/plan/cost.rs | 52 ++++--- crates/integration-tests/src/lib.rs | 53 ++++++- .../integration-tests/tests/plan_sharing.rs | 62 ++++++-- .../tests/planning_common/mod.rs | 17 ++- .../tests/rollup_controlled.rs | 19 ++- .../src/candidate.rs | 4 +- crates/types/src/summary_type/parameters.rs | 13 ++ .../analytical-resource-cost.md | 2 +- 17 files changed, 366 insertions(+), 247 deletions(-) rename crates/cost/src/node/{operation_counts.rs => operations_per_unit.rs} (62%) diff --git a/crates/cost/src/measured.rs b/crates/cost/src/measured.rs index c587526a6..3d2c368cd 100644 --- a/crates/cost/src/measured.rs +++ b/crates/cost/src/measured.rs @@ -22,6 +22,12 @@ //! covering every operator kind the plans use makes the choice the //! measured one (plan selection's module doc, "Measured operation costs"). //! +//! Without a table, the formulas are not unit-free guesses: they carry the +//! reference executor's measured time per unit of work +//! (`node::operations_per_unit`), the built-in fallback. A table measured +//! on the deployment's own machine (by a calibration run of its executor) +//! overrides them node by node. +//! //! A measured time enters [`ResourceUsage`] as CPU operations: `t` ns is //! `t · 10⁻⁶ / ms_per_cpu_op` operations of [`ResourceSpeed`], so latency //! (operations × `ms_per_cpu_op`) is the measured time, and every cost model diff --git a/crates/cost/src/model.rs b/crates/cost/src/model.rs index 355867a13..a68cb02dd 100644 --- a/crates/cost/src/model.rs +++ b/crates/cost/src/model.rs @@ -44,7 +44,8 @@ pub struct ResourceUnitCost { impl ResourceUnitCost { /// The prices plan selection uses until a deployment is calibrated: - /// 1 ns of CPU per operation, 0.1 ns per scanned byte, 1 GB retained for a + /// 1 ns of CPU per operation (one operation is one ns of the reference + /// executor's work, [`ResourceSpeed::ms_per_cpu_op`]), 0.1 ns per scanned byte, 1 GB retained for a /// second costs 1/8 vCPU (125 CPU-ms), held or provisioned alike; one /// provisioned core-second is 1,000 CPU-ms. Disk is not priced (0). pub fn illustrative() -> Self { @@ -117,7 +118,11 @@ impl ResourceUnitCost { #[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)] #[serde(deny_unknown_fields)] pub struct ResourceSpeed { - /// Milliseconds one CPU operation takes on one core. + /// Milliseconds one CPU operation takes on one core. One CPU operation + /// is one nanosecond of the reference executor's work + /// (`node::operations_per_unit`), so the default is 1e-6; for another + /// machine set this to 1e-6 × its time ÷ the reference time (a core + /// half as fast: 2e-6). pub ms_per_cpu_op: f64, /// Milliseconds scanning one byte takes on one core. pub ms_per_scanned_byte: f64, diff --git a/crates/cost/src/node/analytical.rs b/crates/cost/src/node/analytical.rs index 788d29137..3be752eac 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::operation_counts::{ - HASH_AGGREGATE_OPERATIONS_PER_KEY, TIME_RANGE_OPERATIONS_PER_ROW, -}; +use crate::node::operations_per_unit::TIME_RANGE_OPERATIONS_PER_ROW; use crate::resources::ModeledCpu; use asap_types::summary_type::{SketchAlgorithm, SketchParams}; use serde::{Deserialize, Serialize}; @@ -99,9 +97,11 @@ pub(crate) enum PhysicalOperator { }, HashAggregate { grouping_key_count: u64, - /// CPU operations its measures cost per input row, all together - /// (`operation_counts::aggregate_measure_operations_per_row`). - accumulator_operations_per_row: u64, + /// CPU operations one input row costs, its grouping keys and + /// measures together + /// (`operations_per_unit::hash_aggregate_operations_per_row`): + /// 27 for a per-series `max_over_time` (one key, one measure). + operations_per_row: u64, }, InMemoryComparisonSort { ordering_key_count: u64, @@ -448,7 +448,7 @@ pub(crate) fn estimate_operator( ), (PhysicalOperator::TimeShift, _) => ResourceEstimate::new(0.0, 0, 0), (PhysicalOperator::TimeRange, _) => ResourceEstimate::new( - checked_cpu_product(output.rows, TIME_RANGE_OPERATIONS_PER_ROW)?, + output.rows as f64 * TIME_RANGE_OPERATIONS_PER_ROW, per_row_width(output.rows, output.bytes)?, 0, ), @@ -459,8 +459,7 @@ pub(crate) fn estimate_operator( ), ( PhysicalOperator::HashAggregate { - grouping_key_count, - accumulator_operations_per_row, + operations_per_row, .. }, OperatorStatistics::HashAggregate { group_count, @@ -469,13 +468,7 @@ pub(crate) fn estimate_operator( .. }, ) => ResourceEstimate::new( - checked_cpu_product( - left.rows, - grouping_key_count - .checked_mul(HASH_AGGREGATE_OPERATIONS_PER_KEY) - .and_then(|keys| keys.checked_add(accumulator_operations_per_row)) - .ok_or(AnalyticalCostError::Overflow)?, - )?, + checked_cpu_product(left.rows, operations_per_row)?, checked_bytes(&[ *group_count, key_bytes @@ -848,7 +841,7 @@ pub(crate) fn validate_operator_semantics( ( PhysicalOperator::HashAggregate { grouping_key_count, - accumulator_operations_per_row, + operations_per_row, }, OperatorStatistics::HashAggregate { group_count, @@ -862,7 +855,7 @@ pub(crate) fn validate_operator_semantics( "PromQL cross-series aggregation needs an explicit physical operator", ); } - if accumulator_operations_per_row == 0 || *accumulator_bytes_per_group == 0 { + if operations_per_row == 0 || *accumulator_bytes_per_group == 0 { return inconsistent("HashAggregate accumulator work and width must be positive"); } if grouping_key_count == 0 { @@ -1604,10 +1597,7 @@ mod tests { }, ) .unwrap(); - assert_eq!( - range.cpu_ops(), - 200.0 * TIME_RANGE_OPERATIONS_PER_ROW as f64 - ); + assert_eq!(range.cpu_ops(), 200.0 * TIME_RANGE_OPERATIONS_PER_ROW); assert!(estimate_operator( PhysicalOperator::TimeRange, OperatorStatistics::PassThrough { @@ -1639,7 +1629,7 @@ mod tests { fn aggregate_operator() -> PhysicalOperator { PhysicalOperator::HashAggregate { grouping_key_count: 1, - accumulator_operations_per_row: 1, + operations_per_row: 1, } } @@ -1801,7 +1791,7 @@ mod tests { estimate_operator( PhysicalOperator::HashAggregate { grouping_key_count: 0, - accumulator_operations_per_row: 1, + operations_per_row: 1, }, statistics, ), @@ -2135,7 +2125,7 @@ mod tests { let ungrouped = estimate_operator( PhysicalOperator::HashAggregate { grouping_key_count: 0, - accumulator_operations_per_row: 1, + operations_per_row: 1, }, OperatorStatistics::HashAggregate { edges: unary_edges(empty, EdgeStatistics { rows: 1, bytes: 8 }), @@ -2151,7 +2141,7 @@ mod tests { let grouped = estimate_operator( PhysicalOperator::HashAggregate { grouping_key_count: 1, - accumulator_operations_per_row: 1, + operations_per_row: 1, }, OperatorStatistics::HashAggregate { edges: unary_edges(empty, empty), diff --git a/crates/cost/src/node/mod.rs b/crates/cost/src/node/mod.rs index d0feceb97..97ad25baf 100644 --- a/crates/cost/src/node/mod.rs +++ b/crates/cost/src/node/mod.rs @@ -1,11 +1,11 @@ //! One node of a physical plan DAG: its resource usage and cost //! (`usage`), from the analytical per-operator resource model //! ([`analytical`], `operator_statistics`), the measured CPU operations per -//! unit of work (`operation_counts`) and the bytes a state holds +//! unit of work (`operations_per_unit`) and the bytes a state holds //! ([`state_bytes`]). pub mod analytical; -pub(crate) mod operation_counts; +pub(crate) mod operations_per_unit; pub(crate) mod operator_statistics; pub mod state_bytes; pub(crate) mod usage; diff --git a/crates/cost/src/node/operation_counts.rs b/crates/cost/src/node/operations_per_unit.rs similarity index 62% rename from crates/cost/src/node/operation_counts.rs rename to crates/cost/src/node/operations_per_unit.rs index 93d1dc166..e04a6403b 100644 --- a/crates/cost/src/node/operation_counts.rs +++ b/crates/cost/src/node/operations_per_unit.rs @@ -2,15 +2,23 @@ //! range keeps, per row a hash aggregate groups, per row folded into a //! summary state, per state merged, per group read out. //! -//! One operation is one nanosecond on one core at the default -//! [`ResourceSpeed`](crate::ResourceSpeed) (`ms_per_cpu_op` = 1e-6), so each -//! count below is the executor's measured time for that unit, in ns. What -//! plan selection needs is their ratios: a sketch built at query time is -//! cheaper than the exact plan only when its update is cheaper than -//! keeping and sorting the same rows. A slower or faster machine scales -//! them all through `ms_per_cpu_op`; a deployment that measures its own +//! One CPU operation is one nanosecond of the reference executor's work on +//! one core: each count below is the executor's measured time for that +//! unit, in ns, and [`ResourceSpeed::ms_per_cpu_op`](crate::ResourceSpeed) +//! (1e-6 by default) turns operations into milliseconds on the deployment's +//! machine. What plan selection needs is their ratios: a sketch built at +//! query time is cheaper than the exact plan only when its update is +//! cheaper than keeping and sorting the same rows. These are the built-in +//! fallback; a table measured on the deployment //! (`CostInputs::measured_operation_costs`) replaces them node by node. //! +//! Operators not measured here still charge an abstract 1 operation per +//! unit, unscaled: the source scan (1 per sample plus 0.1 per byte), a +//! filter or projection (1 per row per predicate or expression), sorts, +//! joins, deduplication, set operations and the other non-summary +//! operators (`analytical`), and every node `usage` prices by its input +//! rows alone. +//! //! Measured on one core of an idle server with the executor at commit //! 59365ce80, in a release build: each query-time node of a plan run as the //! only root over warm in-memory data, its own time the sub-plan's time @@ -25,26 +33,26 @@ //! groups them per series (`HASH_AGGREGATE_OPERATIONS_PER_KEY` + //! `AGGREGATE_MEASURE_OPERATIONS_PER_ROW`, 27) and sorts each series' 3,600 //! values (11.8 levels × `SORT_OPERATIONS_PER_ROW_PER_LEVEL`, 65): 104 ns per -//! row. A DDSketch built -//! from the same rows takes 12 + `DDSKETCH_UPDATE_OPERATIONS_PER_ROW` (210) -//! = 222 ns per row, about twice the exact plan, as measured (111 and 229). +//! row. A DDSketch built from the same rows takes 12 + +//! `DDSKETCH_UPDATE_OPERATIONS_PER_ROW` (210) = 222 ns per row, about twice +//! the exact plan, as measured (111 and 229). use asap_types::ir::operator::AggIntent; use asap_types::ir::schema::FieldDataType; -use asap_types::summary_type::{ExactKind, SketchParams}; +use asap_types::summary_type::SketchParams; use crate::StateFamily; /// A row kept by a time range over raw rows, or a state read by a time /// range over a chain's tumbling windows: 11–12 ns per row over 3.6 M and /// 36 M rows, 8–18 ns per state over 360,000 states. -pub(crate) const TIME_RANGE_OPERATIONS_PER_ROW: u64 = 12; +pub(crate) const TIME_RANGE_OPERATIONS_PER_ROW: f64 = 12.0; /// Per input row, for each grouping key of a hash aggregate: hashing the /// key and finding its group. With `AGGREGATE_MEASURE_OPERATIONS_PER_ROW`, /// a per-series `max_over_time` (one key, one measure) groups a row in /// 27 ns. -pub(crate) const HASH_AGGREGATE_OPERATIONS_PER_KEY: u64 = 15; +pub(crate) const HASH_AGGREGATE_OPERATIONS_PER_KEY: f64 = 15.0; /// Per input row, for each plain measure of a hash aggregate (sum, count, /// min, max, ...): folding the row's value into its group's. @@ -72,15 +80,11 @@ pub(crate) const EXACT_UPDATE_OPERATIONS_PER_ROW: f64 = 40.0; /// One row folded into an exact accumulator of a counter's increase (rate, /// increase, irate), which also tracks resets and the first and last -/// samples: 84 ns per row at query time, 84–89 at ingestion time. A hash -/// aggregate's measure over a series' samples in time order (rate, delta, -/// deriv, ...) is charged the same: it does the same work per row, and the -/// executor implements no such measure to time. +/// samples: 84 ns per row at query time, 84–89 at ingestion time. pub(crate) const COUNTER_UPDATE_OPERATIONS_PER_ROW: f64 = 84.0; /// One counter update of any other sketch, per counter its row updates -/// (`state_bytes::summary_shape`: Count-Min and Count Sketch one per row of -/// depth, plus the heap; one for HyperLogLog and KMV), above +/// ([`counters_updated_per_row`]), above /// `EXACT_UPDATE_OPERATIONS_PER_ROW`, the executor's cost of handing a row /// to its group's state. Not measured on the executor: `asap_sketchlib`'s /// own Count-Min Sketch of depth 3 with a top-k heap updates in about @@ -135,29 +139,68 @@ pub(crate) const FINALIZE_OPERATIONS_PER_GROUP: f64 = EXACT_UPDATE_OPERATIONS_PE pub(crate) const CHAIN_EMIT_OPERATIONS_PER_STATE: f64 = 2_500.0; /// The operations one input row costs folded into a state of `summary_type` -/// with `parameters`, whose kernel updates `counter_updates` counters per -/// row (`state_bytes::summary_shape`'s first value). +/// with `parameters`. pub(crate) fn update_operations_per_row( summary_type: &FieldDataType, parameters: Option<&SketchParams>, - counter_updates: f64, ) -> f64 { match (summary_type, parameters) { - (FieldDataType::ExactAggregate(kind, _), _) if is_counter_increase(kind) => { + (FieldDataType::ExactAggregate(kind, _), _) if kind.tracks_counter_increase() => { COUNTER_UPDATE_OPERATIONS_PER_ROW } (FieldDataType::ExactAggregate(..), _) => EXACT_UPDATE_OPERATIONS_PER_ROW, (_, Some(SketchParams::DDSketch { .. })) => DDSKETCH_UPDATE_OPERATIONS_PER_ROW, (_, Some(SketchParams::Kll { .. })) => KLL_UPDATE_OPERATIONS_PER_ROW, - _ => EXACT_UPDATE_OPERATIONS_PER_ROW + SKETCH_COUNTER_UPDATE_OPERATIONS * counter_updates, + (_, Some(parameters)) => { + EXACT_UPDATE_OPERATIONS_PER_ROW + + SKETCH_COUNTER_UPDATE_OPERATIONS * counters_updated_per_row(parameters) + } + // A summary without sketch parameters (a sample): one counter. + (_, None) => EXACT_UPDATE_OPERATIONS_PER_ROW + SKETCH_COUNTER_UPDATE_OPERATIONS, + } +} + +/// The counters one input row updates in a sketch with `parameters`: one +/// per row of depth for Count-Min and Count Sketch, plus one heap update for +/// their top-k variants; for UnivMon, the same per layer it updates +/// (`univmon_expected_layers`); one for every other sketch. Example: a +/// Count-Min Sketch of depth 3 with a heap updates 4. +pub(crate) fn counters_updated_per_row(parameters: &SketchParams) -> f64 { + match parameters { + SketchParams::Hydra(hydra) => counters_updated_per_row(&hydra.per_subpopulation()), + SketchParams::Cms { depth, .. } | SketchParams::CountSketch { depth, .. } => { + f64::from(*depth) + } + SketchParams::CmsWithHeap { depth, .. } + | SketchParams::CountSketchWithHeap { depth, .. } => f64::from(*depth) + 1.0, + SketchParams::UnivMon { + sketch_rows, + layers, + .. + } => univmon_expected_layers(*layers) * (f64::from(*sketch_rows) + 1.0), + SketchParams::Kll { .. } + | SketchParams::DDSketch { .. } + | SketchParams::Hll { .. } + | SketchParams::Kmv { .. } => 1.0, } } +/// Layers a UnivMon row updates in expectation. `asap_sketchlib` updates +/// layers `0..=b`, with bottom layer `b` the index of the first zero among +/// hash bits `1..layers`, capped at `layers - 1`; so it reaches layer `j` +/// with probability `2^-j`, and the expectation is +/// `sum_{j < layers} 2^-j = 2 - 2^(1 - layers)`. +fn univmon_expected_layers(layers: u8) -> f64 { + 2.0 - 2f64.powi(1 - i32::from(layers.max(1))) +} + /// The operations one input state of `family` holding `state_bytes` costs /// merged into its group's state. pub(crate) fn merge_operations_per_state(family: StateFamily<'_>, state_bytes: u64) -> f64 { match family { - StateFamily::Exact(kind) if is_counter_increase(kind) => COUNTER_MERGE_OPERATIONS_PER_STATE, + StateFamily::Exact(kind) if kind.tracks_counter_increase() => { + COUNTER_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, @@ -177,15 +220,37 @@ pub(crate) fn estimate_operations_per_group(family: StateFamily<'_>) -> f64 { } } -/// The operations one input row costs a hash aggregate's `measure` over -/// groups of `rows_per_group` rows: an exact quantile keeps the row and -/// sorts its group (`log2(rows_per_group)` levels); a measure over a -/// series' samples in time order costs a counter-increase update; any -/// other folds the value. -pub(crate) fn aggregate_measure_operations_per_row( - measure: &AggIntent, +/// The operations one input row costs a hash aggregate with +/// `grouping_key_count` keys and `measures` over groups of `rows_per_group` +/// rows: each key hashed and looked up, each measure folded (at least one +/// operation for an aggregate without measures). Example: a per-series +/// `max_over_time` (one key, one measure) costs 15 + 12 = 27. +pub(crate) fn hash_aggregate_operations_per_row( + grouping_key_count: u64, + measures: &[AggIntent], rows_per_group: u64, ) -> f64 { + grouping_key_count as f64 * HASH_AGGREGATE_OPERATIONS_PER_KEY + + measures + .iter() + .map(|measure| aggregate_measure_operations_per_row(measure, rows_per_group)) + .sum::() + .max(1.0) +} + +/// The operations one input row costs a hash aggregate's `measure` over +/// groups of `rows_per_group` rows, an estimate of its work: +/// - an exact quantile keeps the row and sorts its group +/// (`log2(rows_per_group)` levels); +/// - a counter's rate or increase, or a regression over a series' samples +/// (`deriv`, `predict_linear`), does about what the Rate accumulator does +/// per row (`COUNTER_UPDATE_OPERATIONS_PER_ROW`): it reads each sample's +/// time and value in order and keeps running terms; +/// - any other folds the value (`AGGREGATE_MEASURE_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(measure: &AggIntent, rows_per_group: u64) -> f64 { match measure { AggIntent::Quantile { .. } => { AGGREGATE_MEASURE_OPERATIONS_PER_ROW @@ -194,20 +259,8 @@ pub(crate) fn aggregate_measure_operations_per_row( AggIntent::Rate | AggIntent::IRate | AggIntent::Increase - | AggIntent::Changes - | AggIntent::Delta - | AggIntent::IDelta | AggIntent::Deriv - | AggIntent::Resets | AggIntent::PredictLinear { .. } => COUNTER_UPDATE_OPERATIONS_PER_ROW, _ => AGGREGATE_MEASURE_OPERATIONS_PER_ROW, } } - -/// Whether an exact accumulator of `kind` tracks a counter's increase. -fn is_counter_increase(kind: &ExactKind) -> bool { - matches!( - kind, - ExactKind::Rate | ExactKind::Increase | ExactKind::IRate - ) -} diff --git a/crates/cost/src/node/state_bytes/mod.rs b/crates/cost/src/node/state_bytes/mod.rs index fac0cc11e..d6ba178a8 100644 --- a/crates/cost/src/node/state_bytes/mod.rs +++ b/crates/cost/src/node/state_bytes/mod.rs @@ -34,7 +34,7 @@ pub fn keeps_every_value(measure: &AggIntent) -> bool { ) } -/// Update operations per input row and bytes per state, from the sketch's +/// Bytes per state, from the summary's family, its /// parameters and `input_rows` (the rows one state folds). The bytes are /// `asap_sketchlib`'s `estimated_memory_bytes` for the type the executor /// builds (Count-Min and Count Sketch over `f64` counters), given what the @@ -59,24 +59,19 @@ pub fn keeps_every_value(measure: &AggIntent) -> bool { /// An exact accumulator's state is the executor's per-group state, /// [`EXACT_ACCUMULATOR_STATE_BYTES`] for every kind. /// -/// Update operations per input row: one counter per row of depth for -/// Count-Min and Count-Sketch, plus one heap update for their top-k variants; -/// for UnivMon, the same per layer it updates (`univmon_expected_layers`); -/// 1 for every other sketch. -/// /// `parameters` is the build's θ (`PhysicalASAPDAGNode::parameters`). Err /// [`AnalyticalCostError::UnsupportedCandidate`] for a sketch without θ: a /// planner bug, since every sketch build carries its θ. -pub fn summary_shape( +pub fn summary_state_bytes( family: &FieldDataType, parameters: Option<&SketchParams>, input_rows: StateInputRows, -) -> Result<(f64, u64), AnalyticalCostError> { +) -> Result { Ok(match (family, parameters) { - (FieldDataType::Sketch(..), Some(parameters)) => sketch_shape(parameters, input_rows), + (FieldDataType::Sketch(..), Some(parameters)) => sketch_state_bytes(parameters, input_rows), (FieldDataType::Sketch(..), None) => return Err(AnalyticalCostError::UnsupportedCandidate), - (FieldDataType::ExactAggregate(..), _) => (1.0, EXACT_ACCUMULATOR_STATE_BYTES), - _ => (1.0, 8), + (FieldDataType::ExactAggregate(..), _) => EXACT_ACCUMULATOR_STATE_BYTES, + _ => 8, }) } @@ -111,11 +106,10 @@ impl StateInputRows { }; } -/// [`summary_shape`] of one sketch: its update operations per row and its -/// bytes per state. With [`StateInputRows::UNKNOWN`], the bytes are also the +/// [`summary_state_bytes`] of one sketch. With [`StateInputRows::UNKNOWN`], the bytes are also the /// size the measured table interpolates between /// (`crate::measured`). -pub fn sketch_shape(params: &SketchParams, input_rows: StateInputRows) -> (f64, u64) { +pub fn sketch_state_bytes(params: &SketchParams, input_rows: StateInputRows) -> u64 { use asap_sketchlib::{ Classic, CountMinF64, CountMinSketchWithHeap, CountSketch, CountSketchWithHeap, DataInput, DdSketch, HyperLogLog, Kmv, RegularPath, UnivMon, Vector2D, @@ -125,89 +119,66 @@ pub fn sketch_shape(params: &SketchParams, input_rows: StateInputRows) -> (f64, usize::try_from(rows.max(1)).unwrap_or(usize::MAX) }); match params { - SketchParams::Hydra(hydra) => sketch_shape(&hydra.per_subpopulation(), input_rows), - SketchParams::Cms { width, depth } => ( - f64::from(*depth), - CountMinF64::estimated_memory_bytes(*depth as usize, *width as usize) as u64, - ), - SketchParams::CountSketch { width, depth } => ( - f64::from(*depth), + SketchParams::Hydra(hydra) => sketch_state_bytes(&hydra.per_subpopulation(), input_rows), + SketchParams::Cms { width, depth } => { + CountMinF64::estimated_memory_bytes(*depth as usize, *width as usize) as u64 + } + SketchParams::CountSketch { width, depth } => { CountSketch::, RegularPath>::estimated_memory_bytes( *depth as usize, *width as usize, - ) as u64, - ), + ) as u64 + } SketchParams::CmsWithHeap { width, depth, heap_size, - } => ( - f64::from(*depth) + 1.0, - CountMinSketchWithHeap::, RegularPath>::estimated_memory_bytes( - *depth as usize, - *width as usize, - *heap_size as usize, - &heap_key, - ) as u64, - ), + } => CountMinSketchWithHeap::, RegularPath>::estimated_memory_bytes( + *depth as usize, + *width as usize, + *heap_size as usize, + &heap_key, + ) as u64, SketchParams::CountSketchWithHeap { width, depth, heap_size, - } => ( - f64::from(*depth) + 1.0, - CountSketchWithHeap::, RegularPath>::estimated_memory_bytes( - *depth as usize, - *width as usize, - *heap_size as usize, - &heap_key, - ) as u64, - ), - SketchParams::Kll { k, .. } => ( - 1.0, - kll_fixed_bytes() + 8 * kll_buffer_items(*k, input_rows), - ), + } => CountSketchWithHeap::, RegularPath>::estimated_memory_bytes( + *depth as usize, + *width as usize, + *heap_size as usize, + &heap_key, + ) as u64, + SketchParams::Kll { k, .. } => kll_fixed_bytes() + 8 * kll_buffer_items(*k, input_rows), // The executor builds no DDSketch outside α in (0, 1), where // sketchlib's estimate panics: priced as an empty one. SketchParams::DDSketch { alpha } if !(*alpha > 0.0 && *alpha < 1.0) => { - (1.0, std::mem::size_of::() as u64) + std::mem::size_of::() as u64 } SketchParams::DDSketch { alpha } => { let ValueMagnitudeRange { min_abs, max_abs } = input_rows .value_magnitude_range .unwrap_or(DDSKETCH_ASSUMED_MAGNITUDE_RANGE); - ( - 1.0, - DdSketch::estimated_memory_bytes( - *alpha, - rows, - min_abs, - max_abs, - !input_rows.non_negative, - ) as u64, - ) + DdSketch::estimated_memory_bytes(*alpha, rows, min_abs, max_abs, !input_rows.non_negative) + as u64 + } + SketchParams::Hll { precision, .. } => { + HyperLogLog::::estimated_memory_bytes(usize::from(*precision)) as u64 } - SketchParams::Hll { precision, .. } => ( - 1.0, - HyperLogLog::::estimated_memory_bytes(usize::from(*precision)) as u64, - ), - SketchParams::Kmv { k, .. } => (1.0, Kmv::estimated_memory_bytes(*k as usize, rows) as u64), + SketchParams::Kmv { k, .. } => Kmv::estimated_memory_bytes(*k as usize, rows) as u64, SketchParams::UnivMon { heap_size, sketch_rows, sketch_cols, layers, .. - } => ( - univmon_expected_layers(*layers) * (f64::from(*sketch_rows) + 1.0), - UnivMon::estimated_memory_bytes( - *heap_size as usize, - *sketch_rows as usize, - *sketch_cols as usize, - usize::from(*layers), - &DataInput::Bytes(&[0; ASSUMED_VALUE_KEY_BYTES]), - ) as u64, - ), + } => UnivMon::estimated_memory_bytes( + *heap_size as usize, + *sketch_rows as usize, + *sketch_cols as usize, + usize::from(*layers), + &DataInput::Bytes(&[0; ASSUMED_VALUE_KEY_BYTES]), + ) as u64, } } @@ -278,15 +249,6 @@ const ASSUMED_HEAP_KEY_BYTES: usize = 3 * (1 + 4 + 16); /// A sample value's key owns no bytes, so this bounds it too. const ASSUMED_VALUE_KEY_BYTES: usize = 1 + 16; -/// Layers a UnivMon row updates in expectation. `asap_sketchlib` updates -/// layers `0..=b`, with bottom layer `b` the index of the first zero among -/// hash bits `1..layers`, capped at `layers - 1`; so it reaches layer `j` -/// with probability `2^-j`, and the expectation is -/// `sum_{j < layers} 2^-j = 2 - 2^(1 - layers)`. -fn univmon_expected_layers(layers: u8) -> f64 { - 2.0 - 2f64.powi(1 - i32::from(layers.max(1))) -} - /// The range of value magnitudes a DDSketch is assumed to see when its /// column declares no `column_magnitudes`: nine decades, e.g. latencies from 1 ns /// to 1 s, costed as magnitudes in `[1, 1e9]` (its bins depend on the ratio diff --git a/crates/cost/src/node/state_bytes/tests.rs b/crates/cost/src/node/state_bytes/tests.rs index f9fd83bce..4a2f58e3f 100644 --- a/crates/cost/src/node/state_bytes/tests.rs +++ b/crates/cost/src/node/state_bytes/tests.rs @@ -13,12 +13,12 @@ fn exact_state_is_priced_at_the_executors_state_size() { (ExactKind::Rate, ExactParams::Rate), ] { assert_eq!( - summary_shape( + summary_state_bytes( &FieldDataType::ExactAggregate(kind, params), None, StateInputRows::UNKNOWN ), - Ok((1.0, 56)) + Ok(56) ); } assert_eq!(asap_types::summary_type::EXACT_ACCUMULATOR_STATE_BYTES, 56); @@ -103,7 +103,7 @@ fn library_ddsketch_bytes(alpha: f64, values: &[f64]) -> u64 { fn ddsketch_state_bytes_bound_the_library() { let alpha = 0.05; let priced = |rows_per_state, non_negative| { - sketch_shape( + sketch_state_bytes( &SketchParams::DDSketch { alpha }, StateInputRows { rows_per_state, @@ -111,7 +111,6 @@ fn ddsketch_state_bytes_bound_the_library() { value_magnitude_range: None, }, ) - .1 }; // Ten Pareto(2) samples (scale 1), one 1 s tumbling window of a 10 Hz series. let pareto: Vec = (0..10) @@ -199,7 +198,7 @@ fn kll_state_bytes_grow_with_the_samples_it_sees() { sketch.memory_bytes() as u64 }; let model_bytes = |rows_per_state| { - sketch_shape( + sketch_state_bytes( &SketchParams::Kll { k }, StateInputRows { rows_per_state, @@ -207,7 +206,6 @@ fn kll_state_bytes_grow_with_the_samples_it_sees() { value_magnitude_range: None, }, ) - .1 }; // An empty sketch holds its fixed bytes; one sample adds 16 items. assert_eq!(library_bytes(0), kll_fixed_bytes()); @@ -416,15 +414,14 @@ fn state_bytes_bound_the_executors_charge() { let state = updater.into_accumulator(); let charged = state.approx_memory_bytes(); let priced = fields - + sketch_shape( + + sketch_state_bytes( params, StateInputRows { rows_per_state: Some(u64::from(n)), non_negative: false, value_magnitude_range: None, }, - ) - .1 as usize; + ) as usize; let context = format!("{algorithm:?} after {n} rows: {priced} priced"); match priced_as { Priced::Exact => assert_eq!(charged, priced, "{context}"), diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index e65da88d1..8bcf96f14 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -22,15 +22,15 @@ use std::collections::{BTreeMap, BTreeSet}; use crate::node::analytical::{ estimate_operator, AnalyticalCostError, PhysicalOperator, ResourceEstimate, }; -use crate::node::operation_counts::{ - aggregate_measure_operations_per_row, estimate_operations_per_group, - merge_operations_per_state, update_operations_per_row, CHAIN_EMIT_OPERATIONS_PER_STATE, +use crate::node::operations_per_unit::{ + estimate_operations_per_group, hash_aggregate_operations_per_row, merge_operations_per_state, + update_operations_per_row, CHAIN_EMIT_OPERATIONS_PER_STATE, }; use crate::node::operator_statistics::{ EdgeStatistics, OperatorStatistics, PartitionStatistics, UnaryEdgeStatistics, }; use crate::node::state_bytes::{ - keeps_every_value, row_bytes, sketch_shape, summary_shape, StateInputRows, + keeps_every_value, row_bytes, sketch_state_bytes, summary_state_bytes, StateInputRows, }; use crate::{aggregate_lookup, lookup, StateFamily}; use crate::{ @@ -362,7 +362,7 @@ pub fn node_resource_usage_and_cost<'a>( let speed = settings.resource_speed; let unit_costs = settings.cost_model.per_node_unit_costs(); let sketch_size = - |params: &SketchParams| sketch_shape(params, StateInputRows::UNKNOWN).1 as f64; + |params: &SketchParams| sketch_state_bytes(params, StateInputRows::UNKNOWN) as f64; let mut fit = CalibrationFit::default(); // Query-time memory held while the node computes. let mut held_bytes = None; @@ -668,16 +668,11 @@ pub fn node_resource_usage_and_cost<'a>( }; let out = edge(group_count)?; let held = input.rows.div_ceil(group_count.max(1)); - // Its measures' work per row, by kind (an exact quantile - // also sorts its group of `held` rows); at least one. - let measure_ops: f64 = measures - .iter() - .map(|measure| aggregate_measure_operations_per_row(measure, held)) - .sum(); + let operations_per_row = hash_aggregate_operations_per_row(keys, measures, held); let estimate = estimate_operator( PhysicalOperator::HashAggregate { grouping_key_count: keys, - accumulator_operations_per_row: (measure_ops.ceil() as u64).max(1), + operations_per_row: operations_per_row.ceil() as u64, }, OperatorStatistics::HashAggregate { edges: unary(out), @@ -702,8 +697,8 @@ pub fn node_resource_usage_and_cost<'a>( out, estimate, format!( - "hash aggregate {} rows into {group_count} groups: {measure_ops:.0} \ - measure ops per row", + "hash aggregate {} rows into {group_count} groups: {operations_per_row:.0} \ + ops per row", input.rows ), ) @@ -880,11 +875,12 @@ pub fn node_resource_usage_and_cost<'a>( non_negative, value_magnitude_range, }; - let (counter_updates, state_bytes) = - summary_shape(family, node.parameters.as_ref(), input_rows)?; - let state_bytes = measured_state_bytes.unwrap_or(state_bytes); - let update_ops = - update_operations_per_row(family, node.parameters.as_ref(), counter_updates); + let state_bytes = measured_state_bytes.unwrap_or(summary_state_bytes( + family, + node.parameters.as_ref(), + input_rows, + )?); + let update_ops = update_operations_per_row(family, node.parameters.as_ref()); let out = EdgeStatistics { rows: group_count, bytes: group_count @@ -931,11 +927,12 @@ pub fn node_resource_usage_and_cost<'a>( non_negative, value_magnitude_range, }; - let (counter_updates, state_bytes) = - summary_shape(family, node.parameters.as_ref(), input_rows)?; - let state_bytes = measured_state_bytes.unwrap_or(state_bytes); - let update_ops = - update_operations_per_row(family, node.parameters.as_ref(), counter_updates); + let state_bytes = measured_state_bytes.unwrap_or(summary_state_bytes( + family, + node.parameters.as_ref(), + input_rows, + )?); + let update_ops = update_operations_per_row(family, node.parameters.as_ref()); // 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 @@ -970,7 +967,8 @@ pub fn node_resource_usage_and_cost<'a>( ) } Payload::ASAP(ASAPOp::SummaryMerge { group_by, .. }) => { - // One operation per input state read; one state per group + // Each input state read is merged into its group's (by the + // states' family, `merge_ops` below); one state per group // out: an input's groups are its states of one window (all // its states unless it selects a chain's windows). A // rollup's groups are those of its keys, at most its input diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index 8ad547328..d33df35c5 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -224,7 +224,7 @@ 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::operation_counts::DDSKETCH_MERGE_OPERATIONS_PER_STATE + 5_000.0 * crate::node::operations_per_unit::DDSKETCH_MERGE_OPERATIONS_PER_STATE ); } @@ -269,7 +269,7 @@ fn a_merge_over_a_rate_chain_s_windows_is_costed_by_the_rate_accumulator() { assert_eq!(merge_usage.family, StateFamily::Exact(&ExactKind::Rate)); assert_eq!( merge_usage.cpu_ops_per_second, - 5_000.0 * crate::node::operation_counts::COUNTER_MERGE_OPERATIONS_PER_STATE + 5_000.0 * crate::node::operations_per_unit::COUNTER_MERGE_OPERATIONS_PER_STATE ); } diff --git a/crates/cost/src/plan/cost.rs b/crates/cost/src/plan/cost.rs index 41253cf88..b692a429f 100644 --- a/crates/cost/src/plan/cost.rs +++ b/crates/cost/src/plan/cost.rs @@ -9,8 +9,9 @@ use std::collections::{BTreeMap, HashMap, HashSet}; use crate::node::analytical::AnalyticalCostError; +use crate::node::operations_per_unit::merge_operations_per_state; use crate::node::operator_statistics::EdgeStatistics; -use crate::node::state_bytes::{sketch_shape, StateInputRows}; +use crate::node::state_bytes::{sketch_state_bytes, StateInputRows}; use crate::node::usage::{cost_source, reads_one_batch_of}; use crate::plan::execution_demand::{computations, evaluation_rate, query_starts}; use crate::plan::read_back::read_back_ms; @@ -190,7 +191,7 @@ pub fn plan_cost( // finalizations over it. let mut family_of: HashMap> = HashMap::new(); let sketch_size = - |params: &SketchParams| sketch_shape(params, StateInputRows::UNKNOWN).1 as f64; + |params: &SketchParams| sketch_state_bytes(params, StateInputRows::UNKNOWN) as f64; for node in &dag.nodes { let node_costed = &costed[&node.id]; usage.cpu_ops_per_second += node_costed.cpu_ops_per_second; @@ -294,29 +295,38 @@ pub fn plan_cost( measured.detail += &format!("; {copies} partial copies of its newest window, {bytes} bytes"); } - // Milliseconds of one merge of each state with a measured query-time - // merge entry (measured ns · 10⁻⁶), for the newest window's compaction. - let merge_ms: HashMap = family_of + // Milliseconds of one merge of each node's state, for the newest + // window's compaction: its measured query-time merge entry (ns · 10⁻⁶) + // when the table has one, else the merge formula of its family and + // bytes per state, as a `SummaryMerge` over it is costed (a DDSketch + // state: 2,500 operations). + let merge_ms: HashMap = dag + .nodes .iter() - .filter_map(|(id, family)| { - lookup( + .map(|node| { + let family = family_of + .get(&node.id) + .copied() + .unwrap_or(StateFamily::None); + let measured = lookup( table, OperatorKind::SummaryMerge, ExecutionTiming::QueryTime, - *family, + family, sketch_size, ) - .map(|unit| (*id, (unit.ns_per_unit + unit.ns_per_invocation) * 1e-6)) + .map(|unit| (unit.ns_per_unit + unit.ns_per_invocation) * 1e-6); + let per_merge_ms = measured.unwrap_or_else(|| { + let states = &output[&node.id]; + let state_bytes = states.bytes.div_ceil(states.rows.max(1)); + merge_operations_per_state(family, state_bytes) + * speed.ms_per_cpu_op + * formula_scale + }); + (node.id, per_merge_ms) }) .collect(); - let mut latency = query_latencies( - dag, - nodes, - &output, - &work_ms, - &merge_ms, - speed.ms_per_cpu_op * formula_scale, - ); + let mut latency = query_latencies(dag, nodes, &output, &work_ms, &merge_ms); for (latency, evaluation_ms) in latency.iter_mut().zip(evaluation_ms_per_query) { latency.query_ms += evaluation_ms; } @@ -472,15 +482,14 @@ fn peak_cpu_cores( /// state's newest window merges, for each of its `I` states per window, /// the `k` ingest workers' partial copies, `(k − 1)·I` merges, where `k` /// is the cores its ingestion work takes, at least one. Each merge takes -/// the state's measured merge time (`merge_ms`) when the measured table has -/// one, and one operation otherwise. +/// the state's merge time (`merge_ms`: measured, or its family's merge +/// formula). fn query_latencies( dag: &PhysicalASAPDAG, nodes: &HashMap, output: &HashMap, work_ms: &HashMap, merge_ms: &HashMap, - formula_merge_ms: f64, ) -> Vec { let inputs = |id: PhysicalASAPNodeId| { dag.edges @@ -493,8 +502,7 @@ fn query_latencies( let compaction_ms = |state: PhysicalASAPNodeId| { let workers = ingest_workers(dag, work_ms, state) as f64; let states = output[&state].rows as f64; - let per_merge_ms = merge_ms.get(&state).copied().unwrap_or(formula_merge_ms); - (workers - 1.0) * states * per_merge_ms + (workers - 1.0) * states * merge_ms[&state] }; query_starts(dag) .into_iter() diff --git a/crates/integration-tests/src/lib.rs b/crates/integration-tests/src/lib.rs index cbe44839a..bb030ae00 100644 --- a/crates/integration-tests/src/lib.rs +++ b/crates/integration-tests/src/lib.rs @@ -246,7 +246,7 @@ pub mod planning { /// output has no guarantee that meets `accuracy` (e.g. a value filter /// over an estimate), not of accuracy acceptance. /// - /// Summaries are costed free ([`summary_work_free`]), so the plan reads + /// Summaries' CPU is costed free ([`summary_cpu_free`]), so the plan reads /// the summary candidate wherever Stage 1 offers one, whether or not the /// default unit costs make it cheaper than the exact plan on these /// tests' small data. @@ -254,20 +254,24 @@ pub mod planning { root: Rc, accuracy: AccuracyTarget, ) -> Vec> { - let settings = summary_work_free(); + let settings = summary_cpu_free(); let models = asap_planner::PlanningModels::builtin() .with_ignore_accuracy_guarantees(true) .with_cost_inputs(&settings); selected_operator_roots(&run_all_stages_with(root, accuracy, models)) } - /// Default cost inputs with a measured table that prices every summary - /// operator (build, chain, merge, estimate, finalize) of every sketch - /// and exact accumulator at 0 ns: for tests of a summary plan's shape, + /// Default cost inputs with a measured table that prices the CPU of + /// every summary operator (build, chain, merge, estimate, finalize) of + /// every sketch family, every exact accumulator and no family at 0 ns; + /// memory is still charged. For tests of a summary plan's shape, /// execution or reuse, which need Stage 3 to select the summary. - pub fn summary_work_free() -> asap_cost::CostInputs { + pub fn summary_cpu_free() -> asap_cost::CostInputs { use asap_cost::{OperatorCost, OperatorKind}; - use asap_types::summary_type::{ExactKind, SketchParams}; + use asap_types::summary_type::{ExactKind, HydraParams, SketchParams}; + // One instance of each sketch family: the table matches an entry by + // family. The match below lists every family, so adding one fails + // to compile until it is listed here too. let sketches = [ SketchParams::Kll { k: 200 }, SketchParams::DDSketch { alpha: 0.01 }, @@ -297,7 +301,27 @@ pub mod planning { sketch_cols: 1024, layers: 8, }, + SketchParams::Hydra(HydraParams::HydraCms { + width: 1024, + depth: 3, + shared_rows: 3, + shared_columns: 1024, + }), ]; + for sketch in &sketches { + match sketch { + SketchParams::Kll { .. } + | SketchParams::DDSketch { .. } + | SketchParams::Cms { .. } + | SketchParams::CmsWithHeap { .. } + | SketchParams::CountSketch { .. } + | SketchParams::CountSketchWithHeap { .. } + | SketchParams::Hll { .. } + | SketchParams::Kmv { .. } + | SketchParams::UnivMon { .. } + | SketchParams::Hydra(_) => {} + } + } let exact = [ ExactKind::Sum, ExactKind::Count, @@ -307,10 +331,23 @@ pub mod planning { ExactKind::Rate, ExactKind::IRate, ]; + for kind in &exact { + match kind { + ExactKind::Sum + | ExactKind::Count + | ExactKind::Min + | ExactKind::Max + | ExactKind::Increase + | ExactKind::Rate + | ExactKind::IRate => {} + } + } + // Every family, and no family (`sketch` and `exact` both `None`). let families = sketches .into_iter() .map(|sketch| (Some(sketch), None)) - .chain(exact.into_iter().map(|kind| (None, Some(kind)))); + .chain(exact.into_iter().map(|kind| (None, Some(kind)))) + .chain([(None, None)]); let mut settings = asap_cost::CostInputs::default(); settings.measured_operation_costs.operators = families .flat_map(|(sketch, exact)| { diff --git a/crates/integration-tests/tests/plan_sharing.rs b/crates/integration-tests/tests/plan_sharing.rs index 0cf1db234..1c1508c64 100644 --- a/crates/integration-tests/tests/plan_sharing.rs +++ b/crates/integration-tests/tests/plan_sharing.rs @@ -1,7 +1,10 @@ //! Workloads whose queries share computation, planned through the production //! stages: adding a query that can read an existing result must not cost //! more than planning the first query alone. -use crate::planning_common::{cost, cost_with, fuzz_workload}; +use crate::planning_common::{cost, cost_with, fuzz_workload, selected_plan}; +use asap_cost::CostInputs; +use asap_types::ir::NonASAPOp; +use asap_types::plan::physical_asap_dag::PhysicalASAPOperatorPayload as Payload; use serde_json::json; /// Deriving the coarse sum costs only a sum over the 150 fine groups. @@ -50,14 +53,18 @@ fn slower_repeat_reuses_faster_sum() { } /// The filtered query reads the by-city quantile's summary states instead -/// of its own scan and aggregation: with summaries costed free (so the -/// sketch plans are selected, as under the earlier unit costs), it adds -/// at most 0.1% to the by-city quantile's cost. Under the default unit -/// costs both are exact plans, and no rule reads the filtered quantile -/// from the by-city exact values. +/// of its own scan and aggregation: with summaries' CPU costed free (so +/// the sketch plans are selected, as under the earlier unit costs), it +/// adds at most 0.1% to the by-city quantile's cost. +/// +/// Under the default unit costs both quantiles are exact plans, and no +/// rule reads the filtered quantile from the by-city exact values +/// (#1358): the filtered query pays its own scan and sort. The last +/// assertion pins that; when the rule exists it fails, and should then +/// expect the reuse. #[test] fn grouped_filtered_quantile_plans() { - let settings = asap_integration_tests::planning::summary_work_free(); + let settings = asap_integration_tests::planning::summary_cpu_free(); let w = fuzz_workload("percentile_filtered_and_by_city", true); let mut grouped = w.clone(); grouped.queries = vec![w.queries[2].clone()]; @@ -69,6 +76,14 @@ fn grouped_filtered_quantile_plans() { together <= grouped_cost * 1.001, "filtered query must reuse the grouped result: {together} vs {grouped_cost}" ); + let defaults = CostInputs::default(); + let grouped_default = cost_with(&grouped, &defaults); + let together_default = cost_with(&filtered_and_grouped, &defaults); + assert!( + together_default > grouped_default * 1.001, + "under the default costs the filtered query now reuses the grouped exact result \ + ({together_default} vs {grouped_default}): update this test" + ); } /// Duplicate exact TopK queries share their result and inner window work. @@ -131,17 +146,32 @@ fn identical_exact_percentiles_share_work() { } /// Different ranks share the source window but still compute their own -/// values: together they cost less than twice one of them. +/// values: the plan scans the table and selects the window once, and both +/// percentiles read that one node. #[test] fn different_exact_percentiles_share_the_input_window() { let mut related = fuzz_workload("identical_percentiles", false); related.queries[1].entry.query.0 = related.queries[1].entry.query.0.replace("0.99", "0.5"); - let related_cost = cost(&related); - let mut base = related.clone(); - base.queries.truncate(1); - let base_cost = cost(&base); - assert!( - related_cost < 2.0 * base_cost * (1.0 - 1e-6), - "{related_cost} vs {base_cost}" - ); + let plan = selected_plan(&related, &CostInputs::default()); + let scans: Vec<_> = plan + .nodes + .iter() + .filter(|node| matches!(node.payload, Payload::NonASAP(NonASAPOp::Scan { .. }))) + .collect(); + assert_eq!(scans.len(), 1, "one scan of the table: {plan:#?}"); + let windows: Vec<_> = plan + .nodes + .iter() + .filter(|node| matches!(node.payload, Payload::NonASAP(NonASAPOp::TimeRange { .. }))) + .collect(); + assert!(windows.len() <= 1, "one input window: {plan:#?}"); + // The node both percentiles read: the window, or the scan if the + // window is a filter over it. + let shared = windows.first().unwrap_or(&scans[0]).id; + let readers = plan + .edges + .iter() + .filter(|edge| edge.producer == shared) + .count(); + assert_eq!(readers, 2, "both percentiles read node {shared}: {plan:#?}"); } diff --git a/crates/integration-tests/tests/planning_common/mod.rs b/crates/integration-tests/tests/planning_common/mod.rs index 3d0f2de6a..fe0ae4df0 100644 --- a/crates/integration-tests/tests/planning_common/mod.rs +++ b/crates/integration-tests/tests/planning_common/mod.rs @@ -7,6 +7,7 @@ use asap_devtools::planning_stages::{ lower_promql_roots, lower_sql_roots, run_search_over, run_stages_until, sql_catalog, RunUntil, }; use asap_planner::{PlanSearchSettings, PlanningModels}; +use asap_types::plan::physical_asap_dag::PhysicalASAPDAG; use asap_types::stages::LogicalASAPMemoCandidates; use asap_types::workload::PlanningWorkload; use serde_json::{json, Value}; @@ -53,6 +54,17 @@ pub fn cost(w: &FuzzWorkload) -> f64 { /// [`cost`] under `settings`. pub fn cost_with(w: &FuzzWorkload, settings: &CostInputs) -> f64 { + let plan = selected_plan(w, settings); + let planning = w.planning_workload(); + let entries: Vec<_> = planning.query_workload.entries().collect(); + let value = + asap_integration_tests::planning::plan_cost_per_second(&plan, &entries, &w.data, settings); + println!("cost={value}"); + value +} + +/// The plan the production stages select for `w` under `settings`. +pub fn selected_plan(w: &FuzzWorkload, settings: &CostInputs) -> PhysicalASAPDAG { let roots = Planner::new(FuzzSettings::default()).lower(w).unwrap(); let planning = w.planning_workload(); let entries: Vec<_> = planning.query_workload.entries().collect(); @@ -69,10 +81,7 @@ pub fn cost_with(w: &FuzzWorkload, settings: &CostInputs) -> f64 { ) .unwrap(); let (plan, _) = run.run.selection.as_ref().unwrap(); - let value = - asap_integration_tests::planning::plan_cost_per_second(plan, &entries, &w.data, settings); - println!("cost={value}"); - value + plan.clone() } /// Pass 2's output for PromQL `queries`, each with its accuracy target, diff --git a/crates/integration-tests/tests/rollup_controlled.rs b/crates/integration-tests/tests/rollup_controlled.rs index 086fe1e74..aa4696abb 100644 --- a/crates/integration-tests/tests/rollup_controlled.rs +++ b/crates/integration-tests/tests/rollup_controlled.rs @@ -41,12 +41,14 @@ fn nodes(roots: &[QueryRoot]) -> Vec> { .collect() } -// The production entrypoint must find a sketch rollup under the viewer -// budget: 2e-5 US dollars per second at AWS Fargate prices, about 1.8 times -// the selected KMV rollup's 1.13e-5 under the measured unit costs. +// The production entrypoint must find a sketch rollup cheaper than +// computing both distinct counts exactly from raw data at query time. #[test] fn production_distinct_rollup_finds_shared_kmv() { let (workload, roots) = fixture(); + let (exact_plan, _) = + asap_types::test_support::forwarders::physical_asap_dag_at_query_time_with_node_ids(&roots) + .unwrap(); let entries: Vec<_> = workload.query_workload.entries().collect(); let data = workload.data_workload.as_ref().unwrap(); let mut settings = CostInputs::default(); @@ -72,7 +74,16 @@ fn production_distinct_rollup_finds_shared_kmv() { .all(|accuracy| accuracy.sketches.iter().any(|sketch| sketch == "Kmv"))); let cost_per_second = asap_integration_tests::planning::plan_cost_per_second(plan, &entries, data, &settings); - assert!(cost_per_second < 2.0e-5, "{cost_per_second}"); + let exact_cost_per_second = asap_integration_tests::planning::plan_cost_per_second( + &exact_plan, + &entries, + data, + &settings, + ); + assert!( + cost_per_second < exact_cost_per_second, + "KMV rollup {cost_per_second} vs exact {exact_cost_per_second}" + ); let roots = plan.query_roots().unwrap(); let builds: HashSet<_> = nodes(&roots) .iter() diff --git a/crates/stage2-physical-optimization/src/candidate.rs b/crates/stage2-physical-optimization/src/candidate.rs index effe6d523..f8e6aef59 100644 --- a/crates/stage2-physical-optimization/src/candidate.rs +++ b/crates/stage2-physical-optimization/src/candidate.rs @@ -14,7 +14,7 @@ use asap_types::stages::{ CandidateForMemoGroupId, Lifetime, Memo, MemoGroupId, PhysicalProperties, PruneReason, StorageMedium, ValueSign, }; -use asap_types::summary_type::{ExactKind, SketchAlgorithm}; +use asap_types::summary_type::SketchAlgorithm; use asap_types::workload::{DataArrival, DataWorkload}; /// Level 3's values, $\tau$. @@ -153,7 +153,7 @@ fn counter_state_rows_ordered( ) -> bool { let counter_state = matches!( summary_type, - FieldDataType::ExactAggregate(ExactKind::Rate | ExactKind::Increase | ExactKind::IRate, _) + FieldDataType::ExactAggregate(kind, _) if kind.tracks_counter_increase() ); let out_of_order_bound_known = matches!( data.arrival, diff --git a/crates/types/src/summary_type/parameters.rs b/crates/types/src/summary_type/parameters.rs index 339fa9b83..21cce4b0b 100644 --- a/crates/types/src/summary_type/parameters.rs +++ b/crates/types/src/summary_type/parameters.rs @@ -52,6 +52,19 @@ impl ExactKind { ExactKind::Increase => AggIntent::Increase, } } + + /// Whether this accumulator tracks a counter's increase (`Rate`, + /// `Increase`, `IRate`): besides its value it keeps the window's first + /// and last samples and the resets between them, so it needs its rows + /// in time order and does more work per row than a `Sum`. Example: + /// `rate(requests_total[5m])`'s accumulator does; `sum_over_time`'s + /// does not. + pub fn tracks_counter_increase(&self) -> bool { + match self { + Self::Rate | Self::Increase | Self::IRate => true, + Self::Sum | Self::Min | Self::Max | Self::Count => false, + } + } } /// Parameters for an [`ExactKind`] accumulator. All exact accumulators have 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 f3a8a3ef3..dac353488 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 @@ -768,7 +768,7 @@ they are never folded into the simple formula implicitly. Concrete accuracy-sized parameters determine state and work. The table counts each kernel's counter updates and reads; `asap-cost` charges a row, a merged state and a read group the executor's measured time for that unit -(`node::operation_counts`, one operation per nanosecond at the default speed), +(`node::operations_per_unit`, one operation per nanosecond at the default speed), so a DDSketch or KLL row costs what the executor measured, not one operation: | Summary | Update operations per row | Read operations | State bytes per physical instance | From b973faf3f25b8b9f36e4ba0c1330183769122ebb Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:05:23 +0000 Subject: [PATCH 06/15] test(planner): sum by (job) (rate) at 10 Hz with unknown arrival installs and answers Fails before the fix: the query-time Rate chain Stage 3 selects fails every evaluation (counter samples must have strictly increasing timestamps within each group). --- crates/planner/tests/runtime_install.rs | 43 +++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/crates/planner/tests/runtime_install.rs b/crates/planner/tests/runtime_install.rs index 3821a31e5..bd9c4c66c 100644 --- a/crates/planner/tests/runtime_install.rs +++ b/crates/planner/tests/runtime_install.rs @@ -854,3 +854,46 @@ async fn a_global_promql_count_over_an_empty_aggregate_answers_no_sample() { assert_eq!(values(&plan, &m_samples()), with_samples, "{query}"); } } + +/// `sum by (job) (rate(requests_total[5m]))` every minute over 1,000 +/// counter series sampled every 100 ms, raw data kept, with the arrival +/// left unknown (the default): the plan installs and answers. Four series +/// (jobs `x` and `y`) each grow by 1 every 100 ms, so each series' rate is +/// 10 per second and each job's sum 20. +#[tokio::test] +async fn sum_rate_at_10_hz_with_unknown_arrival_installs_and_answers() { + let workload: PlanningWorkload = serde_json::from_str( + r#"{"query_workload": {"language": "promql", "repeating_queries": [ + {"query": "sum by (job) (rate(requests_total[5m]))", + "demand": {"fixed_interval_at": {"interval": 60000, "evaluation_phase": 0}}, + "requirements": {}, "predictability": {"predictable": {"known_at": null}}}]}, + "data_workload": {"arrival": "unknown", "sources": {"requests_total": { + "series_count": 1000, "metric_type": "counter", "sample_interval": 100}}}}"#, + ) + .unwrap(); + let plan = plan_installable(UserInput::new( + &workload, + FrontendInput::Promql, + PlanningModels::builtin(), + )) + .await + .unwrap() + .0; + let series = [("a", "x"), ("b", "x"), ("c", "y"), ("d", "y")]; + let mut samples = Vec::new(); + for (instance, job) in series { + for step in 0..=3_600i64 { + samples.push(sample( + "requests_total", + &[("instance", instance), ("job", job)], + EVALUATION_MS - 360_000 + step * 100, + step as f64, + )); + } + } + let answered = query_answers(&plan, &samples); + assert_eq!(answered[0].len(), 2, "{answered:?}"); + for (labels, rate) in &answered[0] { + assert!((rate - 20.0).abs() < 0.01, "{labels:?}: {rate}"); + } +} From ddc8b288f6dff2a70de91e699e419a00406c3169 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:08:30 +0000 Subject: [PATCH 07/15] fix(cost): a query-time chain pays the time range over the rows it reads At query time a tumbling-window chain reads the scan's lookback and keeps each row in its window's range, the work a TimeRange over the same rows does; it was charged nothing for it, so at 10 Hz with the arrival unknown or at rest Stage 3 preferred a query-time Rate chain over the Rate accumulator. Also note in operations_per_unit that sketch constants measure about twice as high under memory and cache contention. --- crates/cost/src/node/operations_per_unit.rs | 7 ++++++- crates/cost/src/node/usage/mod.rs | 11 ++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/crates/cost/src/node/operations_per_unit.rs b/crates/cost/src/node/operations_per_unit.rs index e04a6403b..d2c5612ce 100644 --- a/crates/cost/src/node/operations_per_unit.rs +++ b/crates/cost/src/node/operations_per_unit.rs @@ -26,7 +26,12 @@ //! non-negative gauge (`ts`, `value`, the series identity) and of a counter //! (with a `job` label), 1 h windows at 1 Hz (3,600 rows per series, //! tumbling windows of 10 s) and at 10 Hz (36,000 rows per series, windows -//! of 60 s). Each constant's own measurement is in its doc. +//! of 60 s). Each constant's own measurement is in its doc. On a host +//! with most cores busy, the sketch constants measure about twice as high +//! (DDSketch update 325–403 ns per row, merge 3.4–3.9 µs per state) while +//! the row-streaming ones (time range, hash aggregate, exact accumulators) +//! barely move: memory and cache contention slows sketch kernels most, which +//! only favours the exact plan further. //! //! Example: `quantile_over_time(0.99, latency[1h])` at 1 Hz reads 3.6 M //! rows. The exact plan keeps them (`TIME_RANGE_OPERATIONS_PER_ROW`, 12), diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index 8bcf96f14..c9ae77773 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -24,7 +24,7 @@ use crate::node::analytical::{ }; use crate::node::operations_per_unit::{ estimate_operations_per_group, hash_aggregate_operations_per_row, merge_operations_per_state, - update_operations_per_row, CHAIN_EMIT_OPERATIONS_PER_STATE, + update_operations_per_row, CHAIN_EMIT_OPERATIONS_PER_STATE, TIME_RANGE_OPERATIONS_PER_ROW, }; use crate::node::operator_statistics::{ EdgeStatistics, OperatorStatistics, PartitionStatistics, UnaryEdgeStatistics, @@ -943,6 +943,14 @@ pub fn node_resource_usage_and_cost<'a>( } false => group_count as f64 * retained_output_count as f64, }; + // At query time it reads the scan's lookback and keeps each + // row in its window's range, the work a `TimeRange` over the + // same rows does (12 per row); at ingestion time each row + // arrives once, in its own window. + let range_ops = match ingestion { + true => 0.0, + false => input.rows as f64 * TIME_RANGE_OPERATIONS_PER_ROW, + }; let out = EdgeStatistics { rows: group_count, bytes: group_count @@ -953,6 +961,7 @@ pub fn node_resource_usage_and_cost<'a>( out, Ok(ResourceEstimate::new( input.rows as f64 * update_ops + + range_ops + emitted_states * CHAIN_EMIT_OPERATIONS_PER_STATE, out.bytes, 0, From afd149592387294268db7f1cd251fb85d8f0cdcc Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:48:11 +0000 Subject: [PATCH 08/15] test(cost): no compaction for nodes holding no states; a query-time chain's range term The compaction test fails before the fix: max_over_time(latency[5m]) at 100 M rows per second with its 5m time range kept at ingestion time is predicted 23,000 ms of compaction, an exact accumulator's merge (230 ns) per raw row, though partial copies of rows are concatenated, not merged. The unit test pins a query-time chain's CPU at the cost level: rows x (update + 12 range) + emitted states x 2,500, so it keeps guarding the range term once the executor runs query-time Rate chains. --- crates/cost/src/node/usage/tests.rs | 52 +++++++++++ .../tests/default_cost_plan_ranking.rs | 91 +++++++++++++++++++ 2 files changed, 143 insertions(+) diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index d33df35c5..3bde77bd8 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -273,6 +273,58 @@ fn a_merge_over_a_rate_chain_s_windows_is_costed_by_the_rate_accumulator() { ); } +/// A query-time chain reads its scan's lookback and keeps each row in its +/// window's range, as a `TimeRange` over the same rows does, besides +/// updating the row's state. Example: a sum in 60 s windows over 36,000 +/// raw rows, retaining five windows of one group, costs 36,000 × (40 +/// update + 12 range) operations plus five emitted states × 2,500. +#[test] +fn a_query_time_chain_pays_the_time_range_over_the_rows_it_reads() { + use crate::node::operations_per_unit::{ + CHAIN_EMIT_OPERATIONS_PER_STATE, EXACT_UPDATE_OPERATIONS_PER_ROW, + TIME_RANGE_OPERATIONS_PER_ROW, + }; + let settings = CostInputs::default(); + let data = DataWorkload::default(); + let scan = node(Payload::NonASAP(NonASAPOp::Scan { + source: Source::TimeSeries { + metric: "latency".to_string(), + }, + predicates: vec![], + schema: Schema::default(), + })); + let rows = ProducerOutput { + payload: &scan.payload, + output_schema: &scan.output_schema, + output: EdgeStatistics { + rows: 36_000, + bytes: 36_000 * 16, + }, + sources: BTreeSet::new(), + family: StateFamily::None, + unchanged_columns: BTreeSet::new(), + tumbling_windows: 1, + }; + let chain = PhysicalASAPDAGNode { + retention_ms: Some(300_000), + ..node(Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { + child: 0, + summary_type: FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::by(vec![]), + filter: None, + tumbling_window_width_ms: 60_000, + })) + }; + let usage = + node_resource_usage_and_cost(&chain, &[rows], (1, 1.0), None, &data, &settings).unwrap(); + assert_eq!( + usage.cpu_ops_per_second, + 36_000.0 * (EXACT_UPDATE_OPERATIONS_PER_ROW + TIME_RANGE_OPERATIONS_PER_ROW) + + 5.0 * CHAIN_EMIT_OPERATIONS_PER_STATE + ); +} + /// A query-time node holds its state only /// while it computes: bytes × its work per execution × its executions /// per second, with no query latency. A KLL state of 2 MB whose own diff --git a/crates/integration-tests/tests/default_cost_plan_ranking.rs b/crates/integration-tests/tests/default_cost_plan_ranking.rs index 119c5db33..c6c8da501 100644 --- a/crates/integration-tests/tests/default_cost_plan_ranking.rs +++ b/crates/integration-tests/tests/default_cost_plan_ranking.rs @@ -477,3 +477,94 @@ fn compaction_latency_charges_each_merge_its_family_s_merge_time() { "predicted compaction {compaction_ms} ms, measured merges take at least 180 ms" ); } + +/// Partial copies of an ingestion-time node that holds no states (raw rows +/// a time range keeps, or the values of an exact `Aggregate`) are +/// concatenated, not merged: the executor merges ingest shards only of +/// summary state columns. So reading one adds no compaction latency. +/// Example: `max_over_time(latency[5m])` over 100,000 series sampled every +/// 1 ms (100 M rows per second, more than one core of ingestion work) with +/// its 5m time range, and then also its per-series max, kept at ingestion +/// time: an exact accumulator's merge (230 ns) per row of the 5m window +/// read would predict seconds of compaction for no merge work. +#[test] +fn compaction_latency_is_zero_for_ingestion_time_nodes_holding_no_states() { + use asap_types::ir::properties::ExecutionDataState; + use asap_types::ir::NonASAPOp; + use asap_types::stages::{Lifetime, StorageMedium}; + let query = Query { + promql: "max_over_time(latency[5m])", + accuracy_metric: "relative", + }; + let mut heavy = serde_json::to_value(workload(query, TEN_HZ_EVERY_60_S)).unwrap(); + let data = &mut heavy["data_workload"]; + data["arrival"]["ingesting"]["rows_per_second"] = serde_json::json!(100_000_000.0); + data["sources"]["latency"]["series_count"] = serde_json::json!(100_000); + data["sources"]["latency"]["sample_interval"] = serde_json::json!(1); + let heavy: PlanningWorkload = serde_json::from_value(heavy).unwrap(); + let roots = lower_promql_roots(&heavy).unwrap(); + let (exact, _) = + asap_types::test_support::forwarders::physical_asap_dag_at_query_time_with_node_ids(&roots) + .unwrap(); + let entries: Vec<_> = heavy.query_workload.entries().collect(); + let data = heavy.data_workload.as_ref().unwrap(); + // Scan, then the 5m `TimeRange`, then the per-series max `Aggregate`. + let is_time_range = |payload: &PhysicalASAPOperatorPayload| { + matches!( + payload, + PhysicalASAPOperatorPayload::NonASAP(NonASAPOp::TimeRange { .. }) + ) + }; + let is_aggregate = |payload: &PhysicalASAPOperatorPayload| { + matches!( + payload, + PhysicalASAPOperatorPayload::NonASAP(NonASAPOp::Aggregate { .. }) + ) + }; + let kept_at_ingestion = |up_to_aggregate: bool| { + let mut plan = exact.clone(); + let ingested: Vec<_> = plan + .nodes + .iter() + .filter(|node| !is_aggregate(&node.payload) || up_to_aggregate) + .map(|node| node.id) + .collect(); + for node in &mut plan.nodes { + if !ingested.contains(&node.id) { + continue; + } + node.output_state = ExecutionDataState::INGESTION_ROWS; + if is_time_range(&node.payload) || is_aggregate(&node.payload) { + node.lifetime = Lifetime::AcrossEvaluations(StorageMedium::Memory); + node.retention_ms = Some(300_000); + } + } + for edge in &mut plan.edges { + if ingested.contains(&edge.producer) { + *edge.data_state_mut() = ExecutionDataState::INGESTION_ROWS; + } + } + plan + }; + for (kept, plan) in [ + ("time range", kept_at_ingestion(false)), + ("time range and max", kept_at_ingestion(true)), + ] { + let (cost, _) = + asap_cost::plan_cost(&plan, &entries, data, &CostInputs::default()).unwrap(); + let ingest_ms_per_second: f64 = cost + .per_node + .values() + .filter(|node| node.timing == ExecutionTiming::IngestionTime) + .map(|node| node.cpu_core_seconds_per_second * 1_000.0) + .sum(); + assert!( + ingest_ms_per_second > 1_000.0, + "{kept}: ingestion takes {ingest_ms_per_second} ms per second, one core" + ); + assert_eq!( + cost.usage.latency_per_query[0].compaction_ms, 0.0, + "{kept} kept at ingestion time" + ); + } +} From a9c9cc4f3d797627c2ede98d06e86300d7e84dfe Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:48:13 +0000 Subject: [PATCH 09/15] fix(cost): compaction merges only nodes holding states; review round 2 cleanups - Compaction latency charges 0 per merge for an ingestion-time node with no state family (raw rows of a Filter or TimeRange, an exact Aggregate's values): the executor merges ingest shards only of summary state columns. The merge time is now a closure, evaluated only for the ingestion-time states a query reads. - EdgeStatistics::bytes_per_row replaces three copies of bytes / rows. - SKETCH_COUNTER_UPDATE_OPERATIONS renamed SKETCH_UPDATE_OPERATIONS_PER_COUNTER; TIME_RANGE_OPERATIONS_PER_ROW's doc names the query-time chain. - Docs: stale sketch_shape references in the extension guide and tutorial; the merge cost wording in the analytical cost design doc; the summary_cpu_free comment says what the exhaustive match enforces. --- crates/cost/src/node/operations_per_unit.rs | 10 +-- crates/cost/src/node/operator_statistics.rs | 6 ++ crates/cost/src/node/usage/mod.rs | 7 +- crates/cost/src/plan/cost.rs | 68 +++++++++---------- crates/integration-tests/src/lib.rs | 6 +- crates/planner/src/tutorials/_06_extending.md | 5 +- .../analytical-resource-cost.md | 3 +- .../develop_docs/extend-asap-aware-mapping.md | 7 +- 8 files changed, 62 insertions(+), 50 deletions(-) diff --git a/crates/cost/src/node/operations_per_unit.rs b/crates/cost/src/node/operations_per_unit.rs index d2c5612ce..d54e36602 100644 --- a/crates/cost/src/node/operations_per_unit.rs +++ b/crates/cost/src/node/operations_per_unit.rs @@ -50,7 +50,9 @@ use crate::StateFamily; /// A row kept by a time range over raw rows, or a state read by a time /// range over a chain's tumbling windows: 11–12 ns per row over 3.6 M and -/// 36 M rows, 8–18 ns per state over 360,000 states. +/// 36 M rows, 8–18 ns per state over 360,000 states. A query-time tumbling +/// window chain pays it too, per raw row it reads: it keeps each row of +/// its scan's lookback in its window's range, as a time range does. pub(crate) const TIME_RANGE_OPERATIONS_PER_ROW: f64 = 12.0; /// Per input row, for each grouping key of a hash aggregate: hashing the @@ -94,7 +96,7 @@ pub(crate) const COUNTER_UPDATE_OPERATIONS_PER_ROW: f64 = 84.0; /// to its group's state. Not measured on the executor: `asap_sketchlib`'s /// own Count-Min Sketch of depth 3 with a top-k heap updates in about /// 72 ns, 18 ns per counter. -pub(crate) const SKETCH_COUNTER_UPDATE_OPERATIONS: f64 = 18.0; +pub(crate) const SKETCH_UPDATE_OPERATIONS_PER_COUNTER: f64 = 18.0; /// One exact accumulator state merged into its group's (sum, count, min, /// max): 128–265 ns per state over 360,000 states. @@ -158,10 +160,10 @@ pub(crate) fn update_operations_per_row( (_, Some(SketchParams::Kll { .. })) => KLL_UPDATE_OPERATIONS_PER_ROW, (_, Some(parameters)) => { EXACT_UPDATE_OPERATIONS_PER_ROW - + SKETCH_COUNTER_UPDATE_OPERATIONS * counters_updated_per_row(parameters) + + 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_COUNTER_UPDATE_OPERATIONS, + (_, None) => EXACT_UPDATE_OPERATIONS_PER_ROW + SKETCH_UPDATE_OPERATIONS_PER_COUNTER, } } diff --git a/crates/cost/src/node/operator_statistics.rs b/crates/cost/src/node/operator_statistics.rs index f382dec0f..d24c8a50e 100644 --- a/crates/cost/src/node/operator_statistics.rs +++ b/crates/cost/src/node/operator_statistics.rs @@ -18,6 +18,12 @@ impl EdgeStatistics { pub(crate) fn is_consistent(self) -> bool { matches!((self.rows, self.bytes), (0, 0) | (1.., 1..)) } + + /// The bytes of one row, rounded up: one state's bytes on an edge of + /// states (1,000 states of 16,000 bytes: 16), 0 on an empty edge. + pub(crate) fn bytes_per_row(self) -> u64 { + self.bytes.div_ceil(self.rows.max(1)) + } } /// Input and output evidence for a unary physical operator. diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index c9ae77773..589a0edd2 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -996,8 +996,8 @@ pub fn node_resource_usage_and_cost<'a>( let merge_ops: f64 = producers .iter() .map(|producer| { - let state_bytes = producer.output.bytes.div_ceil(producer.output.rows.max(1)); - producer.output.rows as f64 * merge_operations_per_state(family, state_bytes) + producer.output.rows as f64 + * merge_operations_per_state(family, producer.output.bytes_per_row()) }) .sum(); if let Some(keys) = group_by { @@ -1006,8 +1006,7 @@ pub fn node_resource_usage_and_cost<'a>( let out = EdgeStatistics { rows: group_count, bytes: input - .bytes - .div_ceil(input.rows.max(1)) + .bytes_per_row() .checked_mul(group_count) .ok_or(AnalyticalCostError::Overflow)?, }; diff --git a/crates/cost/src/plan/cost.rs b/crates/cost/src/plan/cost.rs index b692a429f..91edcde02 100644 --- a/crates/cost/src/plan/cost.rs +++ b/crates/cost/src/plan/cost.rs @@ -58,7 +58,8 @@ pub struct PlanCost { pub struct QueryLatency { /// Milliseconds to merge the ingest workers' partial copies of the /// newest window of the largest ingestion-time state the query reads; - /// 0 when ingestion fits on one core or the query reads none. + /// 0 when ingestion fits on one core or the query reads no state (only + /// raw rows or an exact `Aggregate`'s values kept at ingestion time). pub compaction_ms: f64, /// Milliseconds of the query-time work the query reaches, each node /// once. @@ -295,38 +296,35 @@ pub fn plan_cost( measured.detail += &format!("; {copies} partial copies of its newest window, {bytes} bytes"); } - // Milliseconds of one merge of each node's state, for the newest - // window's compaction: its measured query-time merge entry (ns · 10⁻⁶) - // when the table has one, else the merge formula of its family and - // bytes per state, as a `SummaryMerge` over it is costed (a DDSketch - // state: 2,500 operations). - let merge_ms: HashMap = dag - .nodes - .iter() - .map(|node| { - let family = family_of - .get(&node.id) - .copied() - .unwrap_or(StateFamily::None); - let measured = lookup( - table, - OperatorKind::SummaryMerge, - ExecutionTiming::QueryTime, - family, - sketch_size, - ) - .map(|unit| (unit.ns_per_unit + unit.ns_per_invocation) * 1e-6); - let per_merge_ms = measured.unwrap_or_else(|| { - let states = &output[&node.id]; - let state_bytes = states.bytes.div_ceil(states.rows.max(1)); - merge_operations_per_state(family, state_bytes) + // Milliseconds of one merge of a node's state when the newest window's + // partial copies are compacted: its measured query-time merge entry + // (ns · 10⁻⁶) when the table has one, else the merge formula of its + // family and bytes per state, as a `SummaryMerge` over it is costed (a + // DDSketch state: 2,500 operations). 0 for a node holding no states, + // 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. + let merge_ms = |id: PhysicalASAPNodeId| { + let Some(&family) = family_of.get(&id) else { + return 0.0; + }; + lookup( + table, + OperatorKind::SummaryMerge, + ExecutionTiming::QueryTime, + family, + sketch_size, + ) + .map_or_else( + || { + merge_operations_per_state(family, output[&id].bytes_per_row()) * speed.ms_per_cpu_op * formula_scale - }); - (node.id, per_merge_ms) - }) - .collect(); - let mut latency = query_latencies(dag, nodes, &output, &work_ms, &merge_ms); + }, + |unit| (unit.ns_per_unit + unit.ns_per_invocation) * 1e-6, + ) + }; + let mut latency = query_latencies(dag, nodes, &output, &work_ms, merge_ms); for (latency, evaluation_ms) in latency.iter_mut().zip(evaluation_ms_per_query) { latency.query_ms += evaluation_ms; } @@ -482,14 +480,14 @@ fn peak_cpu_cores( /// state's newest window merges, for each of its `I` states per window, /// the `k` ingest workers' partial copies, `(k − 1)·I` merges, where `k` /// is the cores its ingestion work takes, at least one. Each merge takes -/// the state's merge time (`merge_ms`: measured, or its family's merge -/// formula). +/// `merge_ms` of the state: measured, or its family's merge formula, 0 for +/// a node holding no states (its partial copies are concatenated). fn query_latencies( dag: &PhysicalASAPDAG, nodes: &HashMap, output: &HashMap, work_ms: &HashMap, - merge_ms: &HashMap, + merge_ms: impl Fn(PhysicalASAPNodeId) -> f64, ) -> Vec { let inputs = |id: PhysicalASAPNodeId| { dag.edges @@ -502,7 +500,7 @@ fn query_latencies( let compaction_ms = |state: PhysicalASAPNodeId| { let workers = ingest_workers(dag, work_ms, state) as f64; let states = output[&state].rows as f64; - (workers - 1.0) * states * merge_ms[&state] + (workers - 1.0) * states * merge_ms(state) }; query_starts(dag) .into_iter() diff --git a/crates/integration-tests/src/lib.rs b/crates/integration-tests/src/lib.rs index bb030ae00..8a327f2a2 100644 --- a/crates/integration-tests/src/lib.rs +++ b/crates/integration-tests/src/lib.rs @@ -270,8 +270,8 @@ pub mod planning { use asap_cost::{OperatorCost, OperatorKind}; use asap_types::summary_type::{ExactKind, HydraParams, SketchParams}; // One instance of each sketch family: the table matches an entry by - // family. The match below lists every family, so adding one fails - // to compile until it is listed here too. + // family. A new family fails to compile in the exhaustive match + // below; add an instance of it to this array too. let sketches = [ SketchParams::Kll { k: 200 }, SketchParams::DDSketch { alpha: 0.01 }, @@ -322,6 +322,8 @@ pub mod planning { | SketchParams::Hydra(_) => {} } } + // Every exact accumulator; as above, a new kind fails to compile in + // the match below, and needs adding to this array too. let exact = [ ExactKind::Sum, ExactKind::Count, diff --git a/crates/planner/src/tutorials/_06_extending.md b/crates/planner/src/tutorials/_06_extending.md index 618c958c9..419571e84 100644 --- a/crates/planner/src/tutorials/_06_extending.md +++ b/crates/planner/src/tutorials/_06_extending.md @@ -11,8 +11,9 @@ recipes in [extend ASAP-aware mapping](https://github.com/ProjectASAP/ASAPPlanne `parameter_list`) and its capability row (`SketchAlgorithm::capability`, read by `SummaryTypeCapability`); - offering it in Pass 1 (`ALGORITHM_PREFERENCE`, `summary_candidates`); - - its error model (`theoretical_guarantee`) and its cost - (`sketch_shape`); + - its error model (`theoretical_guarantee`), its state bytes + (`sketch_state_bytes`, `crates/cost/src/node/state_bytes/mod.rs`) and + its per-row and per-merge CPU (`crates/cost/src/node/operations_per_unit.rs`); - its executor kernel (`summary_kernels`, `EVALUATED_READOUTS`). - Adding a reuse rule: a memo rule under Pass 2's `rules` that adds groups (`Memo::find_or_add_group`) or candidates (`Memo::add_candidate`), called 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 dac353488..2d595c8cc 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 @@ -789,7 +789,8 @@ from logical group count alone. Summary merge, subtract, delete, and readout are separate physical operators. Their CPU and memory use the concrete summary state size and number of input -states. A merge costs its family's measured operations per input state, and outputs one state per +states. A merge costs its family's operations per input state (per byte for unmeasured +sketches), and outputs one state per group: the input's groups, or for a rollup (`group_by`) the groups of its keys, estimated like any `by (keys)` and at most the input's groups. A plan using one of these operations is unavailable until the corresponding formula diff --git a/docs/develop_docs/extend-asap-aware-mapping.md b/docs/develop_docs/extend-asap-aware-mapping.md index af00cceaa..d0ff341ed 100644 --- a/docs/develop_docs/extend-asap-aware-mapping.md +++ b/docs/develop_docs/extend-asap-aware-mapping.md @@ -35,8 +35,11 @@ A new sketch that Pass 1 can offer for an intent. rejected. 5. **Cost.** Give it a state size (the library's `estimated_memory_bytes` for the type the executor builds; KLL keeps the planner's own formula) - and per-operation CPU in `sketch_shape` - (`crates/cost/src/node/state_bytes.rs`), and accept its parameters in + 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. 6. **Executor.** Wrap the `asap_sketchlib` implementation in `crates/executor/src/summary_kernels/` and register it in From 6f36a6357a0f21f0c2a7aa3d68523024d6135895 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:57:46 +0000 Subject: [PATCH 10/15] test(cost): rebase onto main: the new ProducerOutputs in the unit-cost tests carry no chain width Main now gives ProducerOutput a chain_tumbling_window_width_ms field. A TimeRange's windows and a scan's rows are read from no chain (a TimeRange does not keep its input's window), so None. --- crates/cost/src/node/usage/tests.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index 3bde77bd8..2daf3f44d 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -253,6 +253,7 @@ fn a_merge_over_a_rate_chain_s_windows_is_costed_by_the_rate_accumulator() { family: StateFamily::None, unchanged_columns: BTreeSet::new(), tumbling_windows: 5, + chain_tumbling_window_width_ms: None, }; let mut merge = node(Payload::ASAP(ASAPOp::SummaryMerge { children: vec![0], @@ -304,6 +305,7 @@ fn a_query_time_chain_pays_the_time_range_over_the_rows_it_reads() { family: StateFamily::None, unchanged_columns: BTreeSet::new(), tumbling_windows: 1, + chain_tumbling_window_width_ms: None, }; let chain = PhysicalASAPDAGNode { retention_ms: Some(300_000), From c1b6a0e2cd78fd38ccabe72435292872ae8edc9b Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:44:09 +0000 Subject: [PATCH 11/15] test: rebase onto main: main's Stage 3 tests under the measured unit costs; KLL tests on a KLL-only executor Main added three stage3_objective tests whose absolute cost bounds were set under the old one-operation unit costs. The bounds are now relative, computed in the test: the shared scan and the @ end() input each cost less than the queries planned alone and have one Scan read by both queries; the one-off queries over years cost less than the same queries answered by ingestion-time views only. With main's Stage 3, summary_cpu_free leaves memory to choose between KLL and DDSketch for the global quantile, and DDSketch now wins; kll_candidates now plans on an executor that builds only KLL, through logical_candidates_ignoring_guarantees_on. --- crates/integration-tests/src/lib.rs | 20 +- .../tests/filtered_aggregates.rs | 18 +- .../tests/stage3_objective.rs | 180 +++++++++++++++--- 3 files changed, 186 insertions(+), 32 deletions(-) diff --git a/crates/integration-tests/src/lib.rs b/crates/integration-tests/src/lib.rs index 8a327f2a2..7d2ff54e4 100644 --- a/crates/integration-tests/src/lib.rs +++ b/crates/integration-tests/src/lib.rs @@ -253,11 +253,29 @@ pub mod planning { pub fn logical_candidates_ignoring_guarantees( root: Rc, accuracy: AccuracyTarget, + ) -> Vec> { + logical_candidates_ignoring_guarantees_on( + root, + accuracy, + &asap_types::deployment_capabilities::DeploymentCapabilities::default(), + ) + } + + /// [`logical_candidates_ignoring_guarantees`] on a deployment with + /// `capabilities`. For a test of one sketch's execution: with every + /// summary's CPU free, Stage 3 picks among sketches serving the same + /// readout by memory alone, e.g. a DDSketch over a KLL for a global + /// quantile, so a KLL test declares an executor that builds only KLL. + pub fn logical_candidates_ignoring_guarantees_on( + root: Rc, + accuracy: AccuracyTarget, + capabilities: &asap_types::deployment_capabilities::DeploymentCapabilities, ) -> Vec> { let settings = summary_cpu_free(); let models = asap_planner::PlanningModels::builtin() .with_ignore_accuracy_guarantees(true) - .with_cost_inputs(&settings); + .with_cost_inputs(&settings) + .with_deployment_capabilities(capabilities); selected_operator_roots(&run_all_stages_with(root, accuracy, models)) } diff --git a/crates/integration-tests/tests/filtered_aggregates.rs b/crates/integration-tests/tests/filtered_aggregates.rs index eaa96aa4f..c8f6cfadd 100644 --- a/crates/integration-tests/tests/filtered_aggregates.rs +++ b/crates/integration-tests/tests/filtered_aggregates.rs @@ -12,8 +12,11 @@ use asap_executor::{ values::{Batch, Value}, }; use asap_frontend_sql::{lower_sql, SqlCatalog}; -use asap_integration_tests::planning::{logical_candidates_ignoring_guarantees, selected_dag}; +use asap_integration_tests::planning::{ + logical_candidates_ignoring_guarantees, logical_candidates_ignoring_guarantees_on, selected_dag, +}; use asap_types::accuracy::AccuracyTarget; +use asap_types::deployment_capabilities::DeploymentCapabilities; use asap_types::ir::schema::FieldDataType; use asap_types::ir::schema::{DataType, Field, Schema}; use asap_types::ir::{ASAPOp, NonASAPOp, Operator, OperatorNode, Predicate}; @@ -267,11 +270,20 @@ async fn hand_filtered_kll_compiles_and_executes() { } /// The KLL candidates of `sql`, unchanged, every guarantee accepted (as -/// [`hand_filtered`]). +/// [`hand_filtered`]), on an executor that builds only KLL. async fn kll_candidates(sql: &str) -> Vec> { let target = AccuracyTarget::max_error(0.01).failure_probability(0.01); + let kll_only = DeploymentCapabilities { + supported_summaries: Some( + asap_executor::supported_summary_families() + .into_iter() + .filter(|family| family.summary_type == SketchAlgorithm::Kll) + .collect(), + ), + ..DeploymentCapabilities::default() + }; let roots: Vec<_> = - logical_candidates_ignoring_guarantees(lower(sql).await, target) + logical_candidates_ignoring_guarantees_on(lower(sql).await, target, &kll_only) .into_iter() .filter(|root| { OperatorNode::reachable(root).iter().any(|n| { diff --git a/crates/integration-tests/tests/stage3_objective.rs b/crates/integration-tests/tests/stage3_objective.rs index fb8494984..016ca54d1 100644 --- a/crates/integration-tests/tests/stage3_objective.rs +++ b/crates/integration-tests/tests/stage3_objective.rs @@ -8,7 +8,9 @@ use asap_devtools::planning_stages::{lower_promql_roots, run_planning_stages_ove use asap_planner::{DeploymentCapabilities, PlanningModels}; use asap_types::ir::properties::ExecutionTiming; use asap_types::ir::NonASAPOp; -use asap_types::plan::physical_asap_dag::{PhysicalASAPDAG, PhysicalASAPOperatorPayload}; +use asap_types::plan::physical_asap_dag::{ + PhysicalASAPDAG, PhysicalASAPNodeId, PhysicalASAPOperatorPayload, +}; use asap_types::stages::Lifetime; use asap_types::workload::PlanningWorkload; @@ -89,6 +91,77 @@ fn assert_equal(name: &str, (objective, plan_cost): (f64, f64)) { ); } +/// The ids of the plan's `Scan` nodes, each with how many of the plan's +/// roots (queries) read it, directly or through other nodes: a `Scan` read +/// by 2 of 2 queries is one scan they share. +fn scans_and_their_reading_query_counts( + plan: &PhysicalASAPDAG, +) -> Vec<(PhysicalASAPNodeId, usize)> { + let reads = |root: PhysicalASAPNodeId, target: PhysicalASAPNodeId| { + let mut pending = vec![root]; + let mut seen = vec![root]; + while let Some(node) = pending.pop() { + if node == target { + return true; + } + for edge in plan.edges.iter().filter(|edge| edge.consumer == node) { + if !seen.contains(&edge.producer) { + seen.push(edge.producer); + pending.push(edge.producer); + } + } + } + false + }; + plan.nodes + .iter() + .filter(|node| { + matches!( + node.payload, + PhysicalASAPOperatorPayload::NonASAP(NonASAPOp::Scan { .. }) + ) + }) + .map(|node| { + let readers = plan + .roots + .iter() + .filter(|&&root| reads(root, node.id)) + .count(); + (node.id, readers) + }) + .collect() +} + +/// Asserts that `plan` has one `Scan`, read by all `query_count` queries. +fn assert_one_scan_read_by_every_query(plan: &PhysicalASAPDAG, query_count: usize) { + assert_eq!( + scans_and_their_reading_query_counts(plan) + .iter() + .map(|&(_, readers)| readers) + .collect::>(), + vec![query_count], + "expected one Scan read by all {query_count} queries" + ); +} + +/// `plan_cost` per second of the plan Stage 3 selects for each of the +/// `queries` planned alone. +fn costs_of_queries_planned_alone( + queries: &[serde_json::Value], + workload_of: impl Fn(&[serde_json::Value]) -> PlanningWorkload, + models: impl Fn() -> PlanningModels<'static>, +) -> Vec { + queries + .iter() + .map(|query| { + let alone = workload_of(std::slice::from_ref(query)); + selected_plan_objective_and_plan_cost_with(&alone, models(), &CostInputs::default()) + .1 + .1 + }) + .collect() +} + /// Unit costs that charge what the default ones leave at 0: 50 µs of /// runtime overhead per query request and 1e-9 per byte-second on disk. fn overhead_and_disk_costs() -> CostInputs { @@ -246,29 +319,43 @@ fn raw_data_on_disk_costing_less_than_the_solver_tolerance_still_steers_selectio /// Two windowed quantiles over one metric (1 m and 5 m, every 10 s; 10,000 /// series every 15 s), accuracy not enforced: the selected plan shares one /// query-time scan, whose execution reads the 5 m window once and retains -/// it ($h_v$, constraint (R)), at most 0.144/s. +/// it ($h_v$, constraint (R)). It costs less than the two queries planned +/// alone, which each read the metric once. #[test] fn the_objective_is_the_plan_cost_on_a_shared_scan() { - let w: PlanningWorkload = serde_json::from_value(serde_json::json!({ - "query_workload": { - "language": "promql", - "query_batch": null, - "repeating_queries": [ - every_10s("quantile_over_time(0.5, data[1m])"), - every_10s("quantile_over_time(0.9, data[5m])"), - ], - }, - "data_workload": { - "arrival": {"ingesting": {"rows_per_second": 666.67}}, - "sources": {"data": {"series_count": 10000, "metric_type": "gauge", "sample_interval": 15000}}, - }, - })) - .unwrap(); - let models = PlanningModels::builtin().with_ignore_accuracy_guarantees(true); - let (_, (objective, plan_cost)) = - selected_plan_objective_and_plan_cost_with(&w, models, &CostInputs::default()); + let queries = [ + every_10s("quantile_over_time(0.5, data[1m])"), + every_10s("quantile_over_time(0.9, data[5m])"), + ]; + let workload_of = |queries: &[serde_json::Value]| -> PlanningWorkload { + serde_json::from_value(serde_json::json!({ + "query_workload": { + "language": "promql", + "query_batch": null, + "repeating_queries": queries, + }, + "data_workload": { + "arrival": {"ingesting": {"rows_per_second": 666.67}}, + "sources": {"data": {"series_count": 10000, "metric_type": "gauge", "sample_interval": 15000}}, + }, + })) + .unwrap() + }; + let models = || PlanningModels::builtin().with_ignore_accuracy_guarantees(true); + let (plan, (objective, plan_cost)) = selected_plan_objective_and_plan_cost_with( + &workload_of(&queries), + models(), + &CostInputs::default(), + ); assert_equal("shared scan", (objective, plan_cost)); - assert!(plan_cost <= 0.144, "{plan_cost}"); + assert_one_scan_read_by_every_query(&plan, 2); + let alone: f64 = costs_of_queries_planned_alone(&queries, workload_of, models) + .iter() + .sum(); + assert!( + plan_cost < alone, + "shared {plan_cost} is not below the two queries planned alone, {alone}" + ); } /// The o11y benchmark's q21: `sum(rate(..[5m] offset 1h))`, a 1 h range @@ -505,26 +592,63 @@ fn the_objective_is_the_plan_cost_on_one_off_queries_over_years() { let (objective, plan_cost) = selected_plan_objective_and_plan_cost_with(&w, models, &CostInputs::default()).1; assert_equal("one-off queries over years", (objective, plan_cost)); - assert!(objective < 30_000.0, "objective {objective}"); + // The same queries over arriving rows only, with no raw data kept (a + // deployment that stores nothing, as `mixed` arrival does): only + // ingestion-time views over the arriving rows can answer them, the + // kind of plan the objective's scale once led the solver to. Reading + // raw data at query time is cheaper. + let capabilities = DeploymentCapabilities { + raw_data_retained: false, + ..Default::default() + }; + let views_only = PlanningModels::builtin() + .with_ignore_accuracy_guarantees(true) + .with_deployment_capabilities(&capabilities); + let mut ingesting = serde_json::to_value(&w).unwrap(); + ingesting["data_workload"]["arrival"] = + serde_json::json!({"ingesting": {"rows_per_second": 1_000_000.0 / 15.0}}); + let ingesting: PlanningWorkload = serde_json::from_value(ingesting).unwrap(); + let (_, (_, views_only_plan_cost)) = + selected_plan_objective_and_plan_cost_with(&ingesting, views_only, &CostInputs::default()); + assert!( + plan_cost < views_only_plan_cost, + "selected {plan_cost} is not below the ingestion-time views plan, {views_only_plan_cost}" + ); } /// `sum_over_time(m[5m] @ end())` and `sum_over_time(m[5m])`, both instant /// every 60 s: an instant query's `@ end()` is its evaluation time, so the /// anchored input runs in the evaluation context and shares `m[5m]` with -/// the plain query, in the MILP and in `plan_cost` alike (0.0360/s; in a -/// request context of its own it cost 0.0620/s). +/// the plain query, in the MILP and in `plan_cost` alike. The two cost less +/// than planned alone, which is what any plan that does not share `m[5m]` +/// (such as one in a request context of its own) costs. #[test] fn an_instant_at_end_input_shares_the_evaluation_context() { - let w = over_m(vec![ + let queries = [ requested("sum_over_time(m[5m] @ end())", 60_000, None), requested("sum_over_time(m[5m])", 60_000, None), - ]); - let (objective, plan_cost) = objective_and_plan_cost(&w, &CostInputs::default(), true); + ]; + let (plan, (objective, plan_cost)) = selected_plan_objective_and_plan_cost_with( + &over_m(queries.to_vec()), + PlanningModels::builtin(), + &CostInputs::default(), + ); assert_equal( "instant @ end() beside a plain query", (objective, plan_cost), ); - assert!(objective < 0.04, "objective {objective}"); + assert_one_scan_read_by_every_query(&plan, 2); + let alone: f64 = costs_of_queries_planned_alone( + &queries, + |queries| over_m(queries.to_vec()), + PlanningModels::builtin, + ) + .iter() + .sum(); + assert!( + plan_cost < alone, + "shared {plan_cost} is not below the two queries planned alone, {alone}" + ); } /// `sum_over_time(m[1m])` every 20 s at phase 10 s and From 5caac0697d51dff3505c5bf4c0ebaf2c1f788da7 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 04:42:20 +0000 Subject: [PATCH 12/15] refactor(cost): rebase onto main: operations_per_unit lists every variant under the wildcard lint Each per-unit rule lists every aggregate measure and sketch, so a new one fails to build instead of getting a default cost. Formatting of the DDSketch arm on the declared-magnitude path. --- crates/cost/src/node/operations_per_unit.rs | 85 ++++++++++++++++++++- crates/cost/src/node/state_bytes/mod.rs | 9 ++- 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/crates/cost/src/node/operations_per_unit.rs b/crates/cost/src/node/operations_per_unit.rs index d54e36602..fbde9c04c 100644 --- a/crates/cost/src/node/operations_per_unit.rs +++ b/crates/cost/src/node/operations_per_unit.rs @@ -42,6 +42,14 @@ //! `DDSKETCH_UPDATE_OPERATIONS_PER_ROW` (210) = 222 ns per row, about twice //! the exact plan, as measured (111 and 229). +// Each rule lists every variant, as in `usage`: a wildcard arm would give a +// new operator, summary type or sketch a default cost instead of failing +// the build (under `-D warnings`). +#![warn( + clippy::wildcard_enum_match_arm, + clippy::match_wildcard_for_single_variants +)] + use asap_types::ir::operator::AggIntent; use asap_types::ir::schema::FieldDataType; use asap_types::summary_type::SketchParams; @@ -158,7 +166,19 @@ pub(crate) fn update_operations_per_row( (FieldDataType::ExactAggregate(..), _) => EXACT_UPDATE_OPERATIONS_PER_ROW, (_, Some(SketchParams::DDSketch { .. })) => DDSKETCH_UPDATE_OPERATIONS_PER_ROW, (_, Some(SketchParams::Kll { .. })) => KLL_UPDATE_OPERATIONS_PER_ROW, - (_, Some(parameters)) => { + ( + _, + Some( + parameters @ (SketchParams::Hydra(_) + | SketchParams::Cms { .. } + | SketchParams::CountSketch { .. } + | SketchParams::CmsWithHeap { .. } + | SketchParams::CountSketchWithHeap { .. } + | SketchParams::UnivMon { .. } + | SketchParams::Hll { .. } + | SketchParams::Kmv { .. }), + ), + ) => { EXACT_UPDATE_OPERATIONS_PER_ROW + SKETCH_UPDATE_OPERATIONS_PER_COUNTER * counters_updated_per_row(parameters) } @@ -211,7 +231,16 @@ pub(crate) fn merge_operations_per_state(family: StateFamily<'_>, state_bytes: u 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, - StateFamily::Sketch(_) => { + StateFamily::Sketch( + SketchParams::Hydra(_) + | SketchParams::Cms { .. } + | SketchParams::CountSketch { .. } + | SketchParams::CmsWithHeap { .. } + | SketchParams::CountSketchWithHeap { .. } + | SketchParams::UnivMon { .. } + | SketchParams::Hll { .. } + | SketchParams::Kmv { .. }, + ) => { EXACT_MERGE_OPERATIONS_PER_STATE + SKETCH_MERGE_OPERATIONS_PER_BYTE * state_bytes as f64 } } @@ -222,7 +251,17 @@ pub(crate) fn merge_operations_per_state(family: StateFamily<'_>, state_bytes: u pub(crate) fn estimate_operations_per_group(family: StateFamily<'_>) -> f64 { match family { StateFamily::Sketch(SketchParams::Kll { .. }) => KLL_ESTIMATE_OPERATIONS_PER_GROUP, - StateFamily::Sketch(_) => SKETCH_ESTIMATE_OPERATIONS_PER_GROUP, + StateFamily::Sketch( + SketchParams::DDSketch { .. } + | SketchParams::Hydra(_) + | SketchParams::Cms { .. } + | SketchParams::CountSketch { .. } + | SketchParams::CmsWithHeap { .. } + | SketchParams::CountSketchWithHeap { .. } + | SketchParams::UnivMon { .. } + | SketchParams::Hll { .. } + | SketchParams::Kmv { .. }, + ) => SKETCH_ESTIMATE_OPERATIONS_PER_GROUP, StateFamily::Exact(_) | StateFamily::None => FINALIZE_OPERATIONS_PER_GROUP, } } @@ -268,6 +307,44 @@ fn aggregate_measure_operations_per_row(measure: &AggIntent, rows_per_group: u64 | AggIntent::Increase | AggIntent::Deriv | AggIntent::PredictLinear { .. } => COUNTER_UPDATE_OPERATIONS_PER_ROW, - _ => AGGREGATE_MEASURE_OPERATIONS_PER_ROW, + AggIntent::Count + | AggIntent::Sum { .. } + | AggIntent::Min { .. } + | AggIntent::Max { .. } + | AggIntent::Avg { .. } + | AggIntent::StdDev { .. } + | AggIntent::Variance { .. } + | AggIntent::PearsonCorr { .. } + | AggIntent::TopK { .. } + | AggIntent::Cardinality { .. } + | AggIntent::FrequencyL2 { .. } + | AggIntent::FrequencyEntropy { .. } + | AggIntent::Changes + | AggIntent::Delta + | AggIntent::IDelta + | AggIntent::Resets + | AggIntent::DoubleExpSmoothing { .. } + | AggIntent::HistogramCount + | AggIntent::HistogramSum + | AggIntent::HistogramAvg + | AggIntent::HistogramStdDev + | AggIntent::HistogramStdVar + | AggIntent::HistogramFraction { .. } + | AggIntent::HistogramQuantile { .. } + | AggIntent::Math(_) + | AggIntent::Absent { .. } + | AggIntent::AbsentOverTime + | AggIntent::PresentOverTime + | AggIntent::TimeFn(_) + | AggIntent::Group + | AggIntent::CountValues { .. } + | AggIntent::LastOverTime + | AggIntent::FirstOverTime + | AggIntent::MadOverTime + | AggIntent::TsOfMinOverTime + | AggIntent::TsOfMaxOverTime + | AggIntent::TsOfFirstOverTime + | AggIntent::TsOfLastOverTime + | AggIntent::Extension { .. } => AGGREGATE_MEASURE_OPERATIONS_PER_ROW, } } diff --git a/crates/cost/src/node/state_bytes/mod.rs b/crates/cost/src/node/state_bytes/mod.rs index d6ba178a8..8d67db565 100644 --- a/crates/cost/src/node/state_bytes/mod.rs +++ b/crates/cost/src/node/state_bytes/mod.rs @@ -159,8 +159,13 @@ pub fn sketch_state_bytes(params: &SketchParams, input_rows: StateInputRows) -> let ValueMagnitudeRange { min_abs, max_abs } = input_rows .value_magnitude_range .unwrap_or(DDSKETCH_ASSUMED_MAGNITUDE_RANGE); - DdSketch::estimated_memory_bytes(*alpha, rows, min_abs, max_abs, !input_rows.non_negative) - as u64 + DdSketch::estimated_memory_bytes( + *alpha, + rows, + min_abs, + max_abs, + !input_rows.non_negative, + ) as u64 } SketchParams::Hll { precision, .. } => { HyperLogLog::::estimated_memory_bytes(usize::from(*precision)) as u64 From ec15a375fc1aff46b9b4898ea98eb5b43b43bb9f Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 04:45:56 +0000 Subject: [PATCH 13/15] test: rebase onto main: main's DDSketch byte tests call sketch_state_bytes; the 10 Hz rate test uses the shared sample helper --- crates/cost/src/node/state_bytes/tests.rs | 11 ++++------- crates/cost/src/node/usage/tests.rs | 7 +++---- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/crates/cost/src/node/state_bytes/tests.rs b/crates/cost/src/node/state_bytes/tests.rs index 4a2f58e3f..131088675 100644 --- a/crates/cost/src/node/state_bytes/tests.rs +++ b/crates/cost/src/node/state_bytes/tests.rs @@ -155,7 +155,7 @@ fn ddsketch_state_bytes_bound_the_library() { fn ddsketch_state_bytes_follow_the_declared_value_magnitude_range() { let alpha = 0.01; let costed = |value_magnitude_range| { - sketch_shape( + sketch_state_bytes( &SketchParams::DDSketch { alpha }, StateInputRows { rows_per_state: Some(600), @@ -163,7 +163,6 @@ fn ddsketch_state_bytes_follow_the_declared_value_magnitude_range() { value_magnitude_range, }, ) - .1 }; let struct_bytes = std::mem::size_of::() as u64; let bytes_for_bins = |bins: u64| struct_bytes + 8 * 2 * (bins + 126); @@ -496,7 +495,7 @@ fn next_uniform(state: &mut u64) -> f64 { /// non-negative), or 1,000 rows of 1,000 distinct label values or label /// sets. Its charge /// (`approx_memory_bytes`; for HydraCms its shared grid over its 10 -/// groups) is compared with `summary_shape` for the same rows. Priced ÷ +/// groups) is compared with `summary_state_bytes` for the same rows. Priced ÷ /// charged lies within [`BYTES_AGREEMENT_FACTOR`], or the family's row /// carries its [`KnownBytesGap`]. /// @@ -654,9 +653,7 @@ fn priced_bytes_agree_with_the_executor() { } } let charged = updater.into_accumulator().approx_memory_bytes() as u64; - let priced = summary_shape(&family, params.as_ref(), input_rows(rows_fed)) - .unwrap() - .1; + let priced = summary_state_bytes(&family, params.as_ref(), input_rows(rows_fed)).unwrap(); measured.push((family, priced, charged, known_gap)); } // HydraCms: one grid for every group, priced as one Count-Min per @@ -679,7 +676,7 @@ fn priced_bytes_agree_with_the_executor() { } measured.push(( hydra_family, - sketch_shape(&hydra, input_rows(hydra_rows_fed / HYDRA_GROUPS)).1, + sketch_state_bytes(&hydra, input_rows(hydra_rows_fed / HYDRA_GROUPS)), (grid.approx_memory_bytes() / HYDRA_GROUPS as usize) as u64, gap(947, 0.104), )); diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index 2daf3f44d..95e2c5e67 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -680,14 +680,14 @@ fn ddsketch_state_bytes( .bytes } -/// The bytes `sketch_shape` gives a DDSketch state at α = 0.01 over 600 +/// The bytes `sketch_state_bytes` gives a DDSketch state at α = 0.01 over 600 /// rows of the given sign and magnitude range. fn ddsketch_costed_bytes( non_negative: bool, value_magnitude_range: Option, ) -> u64 { - use crate::node::state_bytes::{sketch_shape, StateInputRows}; - sketch_shape( + use crate::node::state_bytes::{sketch_state_bytes, StateInputRows}; + sketch_state_bytes( &SketchParams::DDSketch { alpha: 0.01 }, StateInputRows { rows_per_state: Some(600), @@ -695,7 +695,6 @@ fn ddsketch_costed_bytes( value_magnitude_range, }, ) - .1 } /// A scan of PromQL metric `metric`. From 91913fef19ed1ca25fa48f0996e95bff32abd8d2 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 05:02:49 +0000 Subject: [PATCH 14/15] test(planner): rebase onto main: MetricsQL answers are checked on data at rest With data arriving, increase(requests_total[5m]) now selects windows maintained at ingestion time, and the test answers each root at query time from raw samples only, so it answered nothing. The plan and install tests keep the arriving data. --- crates/planner/tests/metricsql_workloads.rs | 45 +++++++++++++-------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/crates/planner/tests/metricsql_workloads.rs b/crates/planner/tests/metricsql_workloads.rs index 2d2dd1707..c0197efbe 100644 --- a/crates/planner/tests/metricsql_workloads.rs +++ b/crates/planner/tests/metricsql_workloads.rs @@ -34,17 +34,28 @@ const QUERIES: [&str; 4] = [ r#"sum(latency{job="j1"})"#, ]; +/// Data arriving at 2,000 rows per second: the plans the Planner selects +/// for these queries in production. +const INGESTING: &str = r#"{"ingesting": {"rows_per_second": 2000.0}}"#; + +/// Data at rest: what [`query_answers`] runs over, since it answers each +/// root at query time from the raw samples alone. With data arriving, +/// `increase` selects windows maintained at ingestion time, which this +/// harness never fills. +const AT_REST: &str = r#""at_rest""#; + /// `query` written in `language` ("promql" or "metricsql"), every 10 s /// within relative error 0.01, over 1,000 `requests_total` counter series -/// and 1,000 `latency` gauge series, each sampled every second. -fn workload(language: &str, query: &str) -> PlanningWorkload { +/// and 1,000 `latency` gauge series, each sampled every second, with the +/// data `arrival` ([`INGESTING`] or [`AT_REST`]). +fn workload(language: &str, query: &str, arrival: &str) -> PlanningWorkload { serde_json::from_str(&format!( r#"{{"query_workload": {{"language": "{language}", "repeating_queries": [{{ "query": {query:?}, "demand": {{"fixed_interval_at": {{"interval": 10000, "evaluation_phase": 0}}}}, "requirements": {{"accuracy": {{"max_error": 0.01}}}}, "predictability": {{"predictable": {{"known_at": null}}}}}}]}}, - "data_workload": {{"arrival": {{"ingesting": {{"rows_per_second": 2000.0}}}}, + "data_workload": {{"arrival": {arrival}, "sources": {{ "requests_total": {{"series_count": 1000, "metric_type": "counter", "sample_interval": 1000}}, @@ -54,10 +65,10 @@ fn workload(language: &str, query: &str) -> PlanningWorkload { .unwrap() } -/// The built-in plan of `query` written in MetricsQL, or the error the -/// Planner returned. -async fn plan_metricsql(query: &str) -> Result { - let workload = workload("metricsql", query); +/// The built-in plan of `query` written in MetricsQL, with the data +/// `arrival`, or the error the Planner returned. +async fn plan_metricsql(query: &str, arrival: &str) -> Result { + let workload = workload("metricsql", query, arrival); plan_installable(UserInput::new( &workload, FrontendInput::Metricsql, @@ -68,9 +79,9 @@ async fn plan_metricsql(query: &str) -> Result { .map_err(|e| e.to_string()) } -/// The built-in plan of `query` written in PromQL. -async fn plan_promql(query: &str) -> InstallablePlan { - let workload = workload("promql", query); +/// The built-in plan of `query` written in PromQL, with the data `arrival`. +async fn plan_promql(query: &str, arrival: &str) -> InstallablePlan { + let workload = workload("promql", query, arrival); plan_installable(UserInput::new( &workload, FrontendInput::Promql, @@ -118,7 +129,7 @@ fn query_answers(plan: &InstallablePlan, samples: &[RawDataSample]) -> Vec Date: Mon, 16 Nov 2026 13:52:27 +0000 Subject: [PATCH 15/15] test(planner): force the shared query-time rate chain by data at rest; assert the ingestion-time chain separately The new cost model stores the two rates' shared tumbling-window chain at ingestion time when the samples arrive, so the query-time chain test now plans with the samples at rest, where nothing can be built at ingestion time. A sibling test plans the same queries with arriving samples and asserts the chain is built at ingestion time and answers the same rates. --- crates/planner/tests/runtime_install.rs | 70 ++++++++++++++++++------- 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/crates/planner/tests/runtime_install.rs b/crates/planner/tests/runtime_install.rs index bd9c4c66c..1f95f8b03 100644 --- a/crates/planner/tests/runtime_install.rs +++ b/crates/planner/tests/runtime_install.rs @@ -11,8 +11,8 @@ use crate::time_series; use asap_executor::runtime::Scope; use asap_executor::values::{Batch, Value}; use asap_planner::{ - plan_installable, CostInputs, DeploymentCapabilities, FrontendInput, InstallablePlan, - OperatorKind, PlanningModels, PlanningWorkload, SelectedPlanReport, UserInput, + plan_installable, CostInputs, DeploymentCapabilities, ExecutionTiming, FrontendInput, + InstallablePlan, OperatorKind, PlanningModels, PlanningWorkload, SelectedPlanReport, UserInput, }; use asap_types::ir::{ASAPOp, NonASAPOp, Operator}; @@ -562,13 +562,21 @@ async fn grouped_and_per_series_counts_over_a_shared_query_time_chain_install_an // ── Counter rate and increase, per series and per group ───────────────────── +/// Counter samples arriving at 1,000 rows per second while the queries run. +const COUNTERS_INGESTING: &str = r#"{"ingesting": {"rows_per_second": 1000.0}}"#; +/// Counter samples all stored already: nothing arrives, so no state can be +/// built at ingestion time and every chain is built at query time. +const COUNTERS_AT_REST: &str = r#""at_rest""#; + /// `queries`, each every `interval_ms` within relative error 0.01, over -/// 1,000 `requests_total` counter series sampled every second, planned +/// 1,000 `requests_total` counter series sampled every second with data +/// `arrival` ([`COUNTERS_INGESTING`] or [`COUNTERS_AT_REST`]), planned /// together with the built-in models on a deployment that keeps raw data /// or not. async fn plan_counter_queries( queries: &[&str], interval_ms: u64, + arrival: &str, raw_data_retained: bool, ) -> InstallablePlan { let entries: Vec = queries @@ -584,7 +592,7 @@ async fn plan_counter_queries( .collect(); let workload: PlanningWorkload = serde_json::from_str(&format!( r#"{{"query_workload": {{"language": "promql", "repeating_queries": [{}]}}, - "data_workload": {{"arrival": {{"ingesting": {{"rows_per_second": 1000.0}}}}, + "data_workload": {{"arrival": {arrival}, "sources": {{"requests_total": {{"series_count": 1000, "metric_type": "counter", "sample_interval": 1000}}}}}}}}"#, entries.join(",") @@ -724,7 +732,8 @@ async fn counter_query_mismatches( let mut mismatches = Vec::new(); for raw_data_retained in [true, false] { for (query, expected) in counter_queries() { - let plan = plan_counter_queries(&[query], 10_000, raw_data_retained).await; + let plan = + plan_counter_queries(&[query], 10_000, COUNTERS_INGESTING, raw_data_retained).await; let answered = answer(&plan, &samples).remove(0); let matches = answered.len() == expected.len() && answered.iter().zip(&expected).all(|(answer, expected)| { @@ -763,36 +772,37 @@ async fn counter_rates_and_increases_answer_at_query_time_from_raw_samples() { } /// `rate(requests_total[5m] offset 1m)` and `rate(requests_total[5m])`, -/// each every minute, share a tumbling-window chain of 1-minute windows -/// that the plan builds at query time. Installed, the query run builds it -/// from the raw samples, each update at its sample's own time: over -/// [`counter_samples`] (four samples per window) both queries answer the -/// hand-worked rates `(i + 1) * 20 / 300` per series. `(T - 6m, T - 1m]` -/// holds steps 9..=28, 20 samples as `(T - 5m, T]` does. -#[tokio::test] -async fn counter_rates_over_a_shared_query_time_chain_answer_as_installed() { +/// each every minute, planned together with data `arrival`: the plan with +/// its shared tumbling-window chain (1-minute windows) built at `timing`. +async fn plan_shared_rate_chain(arrival: &str, timing: ExecutionTiming) -> InstallablePlan { let plan = plan_counter_queries( &[ "rate(requests_total[5m] offset 1m)", "rate(requests_total[5m])", ], 60_000, + arrival, true, ) .await; assert!( plan.dag.nodes.iter().any(|node| matches!( node.payload, - asap_types::ir::Operator::ASAP( - asap_types::ir::ASAPOp::SummaryTumblingWindowChain { .. } - ) - ) && node.output_state.timing - == asap_types::ir::properties::ExecutionTiming::QueryTime), - "the plan builds a chain at query time: {:#?}", + Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) + ) && node.output_state.timing == timing), + "the plan builds a chain at {timing:?}: {:#?}", plan.dag.nodes ); + plan +} + +/// The answers of [`plan_shared_rate_chain`]'s plan as installed, over +/// [`counter_samples`] (four samples per window): both queries answer the +/// hand-worked rates `(i + 1) * 20 / 300` per series. `(T - 6m, T - 1m]` +/// holds steps 9..=28, 20 samples as `(T - 5m, T]` does. +fn assert_shared_rate_chain_answers(plan: &InstallablePlan) { let rates: Vec = (1..=4).map(|i| i as f64 * 20.0 / 300.0).collect(); - for answered in installed_answers(&plan, &counter_samples()) { + for answered in installed_answers(plan, &counter_samples()) { let values: Vec = answered.iter().map(|(_, value)| *value).collect(); assert_eq!(values.len(), rates.len(), "{answered:?}"); for (value, rate) in values.iter().zip(&rates) { @@ -801,6 +811,26 @@ async fn counter_rates_over_a_shared_query_time_chain_answer_as_installed() { } } +/// With the counter samples at rest nothing arrives, so the two rates' +/// shared tumbling-window chain is built at query time. Installed, the +/// query run builds it from the raw samples, each update at its sample's +/// own time, and both queries answer the hand-worked rates. +#[tokio::test] +async fn counter_rates_over_a_shared_query_time_chain_answer_as_installed() { + let plan = plan_shared_rate_chain(COUNTERS_AT_REST, ExecutionTiming::QueryTime).await; + assert_shared_rate_chain_answers(&plan); +} + +/// With the counter samples arriving, the two rates' shared +/// tumbling-window chain is cheaper stored: the plan builds it at +/// ingestion time into a materialized view, and both queries, reading the +/// view, answer the same hand-worked rates. +#[tokio::test] +async fn counter_rates_over_a_shared_ingestion_time_chain_answer_as_installed() { + let plan = plan_shared_rate_chain(COUNTERS_INGESTING, ExecutionTiming::IngestionTime).await; + assert_shared_rate_chain_answers(&plan); +} + // ── Global aggregates over an aggregate's output ───────────────────────────── /// A PromQL aggregate with no grouping keys over another aggregate's output