From 6c30b92d97939cd59f9438d32246f341d0b03a7e Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:58:33 +0000 Subject: [PATCH 01/24] test(accuracy): failure probability splits over the readout values the covered output values depend on Cases: per-value and whole-result targets without a reduction, a global max, a grouped sum per value and over the whole result, nested reductions, `or` per value and over the whole result, unknown readout values, a record-share scope and a per-value ratio. Seven fail on the current split, which divides by max(values covered, readout values). --- crates/accuracy/src/readout_caps/tests.rs | 299 +++++++++++++++++++++- 1 file changed, 293 insertions(+), 6 deletions(-) diff --git a/crates/accuracy/src/readout_caps/tests.rs b/crates/accuracy/src/readout_caps/tests.rs index cb19c80e4..5fb1127e4 100644 --- a/crates/accuracy/src/readout_caps/tests.rs +++ b/crates/accuracy/src/readout_caps/tests.rs @@ -1,9 +1,11 @@ use super::*; +use asap_types::accuracy::AccuracyScope; use asap_types::ir::operator::arguments::{PromQLVectorSetOpKind, Reduction}; use asap_types::ir::operator::AggIntent; use asap_types::ir::scalar::expr_ir::ArithmeticOpKind; use asap_types::ir::QueryRoot; -use asap_types::stages::MemoQuery; +use asap_types::stages::{MemoQuery, ValueSign}; +use asap_types::workload::DataWorkload; use asap_types::summary_type::{SketchAlgorithm, SketchStatistic}; use asap_types::test_support::memo_builders::{ estimate, group, own_candidate, quantile_over_time, query, scan_group, sketch_state, @@ -192,10 +194,10 @@ fn an_and_combines_no_readout_errors() { /// `quantile_over_time(0.9, m[5m]) or quantile_over_time(0.5, m[5m])` /// copies each output value from one side, so its error is the larger of /// the two readouts' errors, not their sum: each readout gets the single -/// level of its cap, 0.05. Its whole result still fails when any of the -/// 200 readout values it can return does, so δ = 0.01 splits over all of -/// them: 5e-5 per value, not 1e-4 over its 100 output values. A ratio of -/// the same readouts combines both errors and keeps the composed grid. +/// level of its cap, 0.05. Its whole result covers its 100 output values, +/// each copied from one of the 200 readout values below it, so δ = 0.01 +/// splits over 100 of them: 1e-4 per value (min(200, 100 × 1)). A ratio +/// of the same readouts combines both errors and keeps the composed grid. #[test] fn an_or_takes_each_value_from_one_readout() { let target = AccuracyTarget::max_error(0.05) @@ -219,7 +221,7 @@ fn an_or_takes_each_value_from_one_readout() { }; assert!((required.max_error - 0.05).abs() < 1e-12, "{required:?}"); assert!( - (required.failure_probability - 5e-5).abs() < 1e-12, + (required.failure_probability - 1e-4).abs() < 1e-12, "{required:?}" ); } @@ -304,3 +306,288 @@ fn an_operator_above_a_count_of_readouts_combines_one_readout_error() { "{required:?}" ); } + +// The failure-probability split: δ splits over the readout values the +// output values one (ε, δ) statement covers depend on, +// min(R, covered × fan-in), where R is the readout values one evaluation +// reads, covered the output values the scope covers and fan-in the most +// readout values one output value depends on. + +/// The failure probability of the single level `readout` gets from the one +/// query of `memo`, or `None` when the query is listed without a per-value +/// failure probability. +fn failure_probability_of(memo: &Memo, readout: MemoGroupId) -> Option { + let order = memo.topological_order().expect("acyclic"); + let required = readout_levels(memo, &ErrorMetricsOfMemo::of(memo, &order), &order) + .remove(&own_candidate(readout)) + .expect("a sketch readout"); + if !required.queries_without_per_value_failure_probability.is_empty() { + assert_eq!(required.levels, vec![], "a listed query gives no level"); + return None; + } + let [level] = required.levels[..] else { + panic!("one level: {:?}", required.levels); + }; + Some(level.failure_probability) +} + +/// Asserts that `actual` is `Some(expected)`, up to rounding. +fn assert_failure_probability(actual: Option, expected: f64) { + let actual = actual.expect("a per-value failure probability"); + assert!( + (actual - expected).abs() <= expected * 1e-9, + "{actual} != {expected}" + ); +} + +/// A relative-error target at δ = 0.01 over `scope`. +fn relative_target(scope: AccuracyScope) -> AccuracyTarget { + AccuracyTarget::max_error(0.05) + .failure_probability(0.01) + .accuracy_metric(AccuracyMetric::Relative) + .accuracy_scope(scope) +} + +/// `memo` with one query at `root` with `target`, returning `n_q` values. +fn with_query(memo: &mut Memo, root: MemoGroupId, target: AccuracyTarget, n_q: Option) { + memo.add_query(MemoQuery { + root: QueryRoot::Operator(root), + accuracy_target: Some(target), + number_of_values_per_query_evaluation: n_q, + }); +} + +/// A data workload where `m` holds `series` non-negative series over +/// `jobs` jobs (a sum carries a relative error over values of one sign). +fn with_series_over_jobs(series: u64, jobs: u64) -> DataWorkload { + let mut data = with_series("m", series); + let facts = data.sources.get_mut("m").expect("m"); + facts.distinct_values_per_label.insert("job".into(), jobs); + facts + .column_signs + .insert("value".into(), ValueSign::NonNegative); + data +} + +/// `quantile_over_time(0.99, m[5m])` over 8 series with a per-value rank +/// target of 0.0025 at δ = 0.01: each output value is one series' readout, +/// so the readout keeps δ = 0.01 (KLL k = 1404), not 0.01 / 8 (k = 2863). +/// Divisor min(8, 1 × 1) = 1. +#[test] +fn a_per_value_target_without_a_reduction_keeps_the_whole_failure_probability() { + let mut memo = Memo::new(&with_series("m", 8)); + let (_, _, readout) = quantile_over_time(&mut memo, SketchAlgorithm::Kll, 0.99, 300); + let target = AccuracyTarget::max_error(0.0025) + .failure_probability(0.01) + .accuracy_metric(AccuracyMetric::Rank) + .accuracy_scope(AccuracyScope::PerValue); + with_query(&mut memo, readout, target, Some(8)); + assert_failure_probability(failure_probability_of(&memo, readout), 0.01); +} + +/// The same query over its whole result covers all 8 output values, each +/// one readout value: divisor min(8, 8 × 1) = 8. +#[test] +fn a_whole_result_target_without_a_reduction_splits_over_the_values_returned() { + let mut memo = Memo::new(&with_series("m", 8)); + let (_, _, readout) = quantile_over_time(&mut memo, SketchAlgorithm::Kll, 0.99, 300); + let target = AccuracyTarget::max_error(0.0025) + .failure_probability(0.01) + .accuracy_metric(AccuracyMetric::Rank); + with_query(&mut memo, readout, target, Some(8)); + assert_failure_probability(failure_probability_of(&memo, readout), 0.01 / 8.0); +} + +/// A global `max` over 1000 series: its one output value depends on all +/// 1000 readout values, whatever the scope: divisor min(1000, 1 × 1000). +#[test] +fn a_global_max_splits_over_every_readout_value_in_any_scope() { + for scope in [ + AccuracyScope::PerValue, + AccuracyScope::WholeResult, + AccuracyScope::GroupsWithRecordShareAtLeast(0.5), + ] { + let mut memo = Memo::new(&with_series("m", 1000)); + let (_, _, readout) = quantile_over_time(&mut memo, SketchAlgorithm::DDSketch, 0.9, 300); + let max = group( + &mut memo, + aggregate(Reduction::by(vec![]), AggIntent::Max { col: None }, readout), + ); + with_query(&mut memo, max, relative_target(scope), Some(1)); + assert_failure_probability(failure_probability_of(&memo, readout), 0.01 / 1000.0); + } +} + +/// `sum by (job)` over 1000 series in 10 jobs, per value: one output value +/// depends on the series of its job. The declared facts bound the jobs +/// (`distinct_values_per_label` is an upper bound), not how many series +/// one job holds: the largest job may hold 991 of them. So the fan-in is +/// unknown and the split falls back to R = 1000. +#[test] +fn a_grouped_sum_per_value_splits_over_every_readout_value_when_group_sizes_are_unbound() { + let mut memo = Memo::new(&with_series_over_jobs(1000, 10)); + let (_, _, readout) = quantile_over_time(&mut memo, SketchAlgorithm::DDSketch, 0.9, 300); + let sum = group( + &mut memo, + aggregate(Reduction::by(vec![0]), AggIntent::Sum { col: None }, readout), + ); + with_query( + &mut memo, + sum, + relative_target(AccuracyScope::PerValue), + Some(10), + ); + assert_failure_probability(failure_probability_of(&memo, readout), 0.01 / 1000.0); +} + +/// `sum by (job)` over 1000 series in 10 jobs, whole result: the 10 output +/// values together depend on all 1000 readout values: divisor 1000. +#[test] +fn a_grouped_sum_over_its_whole_result_splits_over_every_readout_value() { + let mut memo = Memo::new(&with_series_over_jobs(1000, 10)); + let (_, _, readout) = quantile_over_time(&mut memo, SketchAlgorithm::DDSketch, 0.9, 300); + let sum = group( + &mut memo, + aggregate(Reduction::by(vec![0]), AggIntent::Sum { col: None }, readout), + ); + with_query( + &mut memo, + sum, + relative_target(AccuracyScope::WholeResult), + Some(10), + ); + assert_failure_probability(failure_probability_of(&memo, readout), 0.01 / 1000.0); +} + +/// `q or r` copies each output value from one readout value: per value, +/// each readout keeps δ = 0.01 (divisor min(200, 1 × 1) = 1), not +/// δ / 200 over both sides' readout values. +#[test] +fn an_or_per_value_keeps_the_whole_failure_probability() { + let mut memo = Memo::new(&with_series("m", 100)); + let (_, state, p90) = quantile_over_time(&mut memo, SketchAlgorithm::DDSketch, 0.9, 300); + let p50 = estimate(&mut memo, state, SketchStatistic::Quantile { q: 0.5 }); + let either = group(&mut memo, set_operator(PromQLVectorSetOpKind::Or, p90, p50)); + with_query( + &mut memo, + either, + relative_target(AccuracyScope::PerValue), + Some(100), + ); + for readout in [p90, p50] { + assert_failure_probability(failure_probability_of(&memo, readout), 0.01); + } +} + +/// Nested reductions: `max(sum by (job) (q))` over 1000 series in 10 jobs. +/// Its one output value depends on every readout value through both +/// reductions: divisor 1000 per value. +#[test] +fn nested_reductions_split_over_every_readout_value_below_them() { + let mut memo = Memo::new(&with_series_over_jobs(1000, 10)); + let (_, _, readout) = quantile_over_time(&mut memo, SketchAlgorithm::DDSketch, 0.9, 300); + let sum = group( + &mut memo, + aggregate(Reduction::by(vec![0]), AggIntent::Sum { col: None }, readout), + ); + let max = group( + &mut memo, + aggregate(Reduction::by(vec![]), AggIntent::Max { col: None }, sum), + ); + with_query( + &mut memo, + max, + relative_target(AccuracyScope::PerValue), + Some(1), + ); + assert_failure_probability(failure_probability_of(&memo, readout), 0.01 / 1000.0); +} + +/// Unknown readout values (no `series_count`): without a reduction, a +/// per-value target still splits over the one readout value each output +/// value depends on (divisor min(unknown, 1 × 1) = 1); under a global `max` +/// the fan-in is the unknown R, so no per-value failure probability follows +/// and the query is listed. +#[test] +fn unknown_readout_values_split_by_the_fan_in_when_it_is_known() { + let mut memo = Memo::new(&DataWorkload::default()); + let (_, _, readout) = quantile_over_time(&mut memo, SketchAlgorithm::DDSketch, 0.9, 300); + with_query( + &mut memo, + readout, + relative_target(AccuracyScope::PerValue), + None, + ); + assert_failure_probability(failure_probability_of(&memo, readout), 0.01); + + let mut memo = Memo::new(&DataWorkload::default()); + let (_, _, readout) = quantile_over_time(&mut memo, SketchAlgorithm::DDSketch, 0.9, 300); + let max = group( + &mut memo, + aggregate(Reduction::by(vec![]), AggIntent::Max { col: None }, readout), + ); + with_query( + &mut memo, + max, + relative_target(AccuracyScope::PerValue), + Some(1), + ); + assert_eq!(failure_probability_of(&memo, readout), None); +} + +/// A whole-result target with unknown `n_q` but known readout values: the +/// union bound over all R = 100 readout values holds: δ / 100. +#[test] +fn a_whole_result_with_unknown_values_returned_splits_over_the_readout_values() { + let mut memo = Memo::new(&with_series("m", 100)); + let (_, _, readout) = quantile_over_time(&mut memo, SketchAlgorithm::DDSketch, 0.9, 300); + with_query( + &mut memo, + readout, + relative_target(AccuracyScope::WholeResult), + None, + ); + assert_failure_probability(failure_probability_of(&memo, readout), 0.01 / 100.0); +} + +/// `GroupsWithRecordShareAtLeast(0.25)` over 8 series without a reduction +/// covers at most ⌊1 / 0.25⌋ = 4 output values, one readout value each: +/// divisor min(8, 4 × 1) = 4, not 8. +#[test] +fn a_record_share_scope_splits_over_the_readout_values_of_the_groups_it_covers() { + let mut memo = Memo::new(&with_series("m", 8)); + let (_, _, readout) = quantile_over_time(&mut memo, SketchAlgorithm::DDSketch, 0.9, 300); + with_query( + &mut memo, + readout, + relative_target(AccuracyScope::GroupsWithRecordShareAtLeast(0.25)), + Some(8), + ); + assert_failure_probability(failure_probability_of(&memo, readout), 0.01 / 4.0); +} + +/// A ratio of two readouts over 8 series, per value: one output value +/// depends on one readout value of each side, fan-in 2, so the two +/// readouts' failure probabilities sum within δ = 0.01: divisor +/// min(16, 1 × 2) = 2, and the loosest grid level is 1e-3 (≤ 0.005), +/// not 1e-4 (≤ 0.01 / 16). +#[test] +fn a_per_value_ratio_splits_over_the_two_readout_values_of_each_output_value() { + let mut memo = Memo::new(&with_series("m", 8)); + let (_, state, p90) = quantile_over_time(&mut memo, SketchAlgorithm::DDSketch, 0.9, 300); + let p50 = estimate(&mut memo, state, SketchStatistic::Quantile { q: 0.5 }); + let ratio = group(&mut memo, arithmetic(ArithmeticOpKind::Div, p90, p50)); + with_query( + &mut memo, + ratio, + relative_target(AccuracyScope::PerValue), + Some(8), + ); + for readout in [p90, p50] { + let loosest = levels_of(&memo, readout) + .into_iter() + .map(|required| required.failure_probability) + .fold(0.0, f64::max); + assert!((loosest - 1e-3).abs() < 1e-15, "{loosest}"); + } +} From 45d5133a731047bef7b35df6a5d2e35c7f455eba Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:03:45 +0000 Subject: [PATCH 02/24] =?UTF-8?q?fix(accuracy):=20split=20=CE=B4=20over=20?= =?UTF-8?q?the=20readout=20values=20the=20covered=20output=20values=20depe?= =?UTF-8?q?nd=20on?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A query's failure probability was split per readout value by max(values covered, readout values per evaluation): a per-value target on `quantile_over_time(0.99, m[5m])` over 8 series got δ / 8 although each output value is one readout value, doubling KLL's k (1404 -> 2863). The union bound needs only the readout values the output values one statement covers depend on: min(R, V * fan_in), with R the readout values per evaluation, V the values the scope covers and fan_in the most readout values one output value depends on (1 at a readout, carried value by value through filters, projections, binary operators and `or`, all of the input's values through a reduction). One function states it, `AccuracyTarget::failure_probability_per_readout_value` over `ReadoutValuesRead`; the readout walk computes both counts. An unknown term drops out of the min, so a per-value target over series of unknown count keeps δ, and a whole-result target with unknown n_q splits over R. --- crates/accuracy/src/readout_caps/mod.rs | 165 +++++++++++++------ crates/accuracy/src/readout_caps/tests.rs | 25 ++- crates/types/src/accuracy/mod.rs | 4 +- crates/types/src/accuracy/target/mod.rs | 67 +++++++- crates/types/src/accuracy/target/tests.rs | 48 ++++++ docs/design_docs/concepts/accuracy-models.md | 40 +++-- 6 files changed, 272 insertions(+), 77 deletions(-) diff --git a/crates/accuracy/src/readout_caps/mod.rs b/crates/accuracy/src/readout_caps/mod.rs index 3d3016bf3..100ccbf27 100644 --- a/crates/accuracy/src/readout_caps/mod.rs +++ b/crates/accuracy/src/readout_caps/mod.rs @@ -5,7 +5,7 @@ use std::collections::{BTreeMap, BTreeSet, HashMap}; -use asap_types::accuracy::{AccuracyMetric, AccuracyTarget}; +use asap_types::accuracy::{AccuracyMetric, AccuracyTarget, ReadoutValuesRead}; use asap_types::ir::operator::arguments::BinaryOpKind; use asap_types::ir::{BinaryOperator, NonASAPOp, Operator}; use asap_types::stages::{CandidateForMemoGroupId, Memo, MemoGroupId, RequiredAccuracy}; @@ -48,8 +48,9 @@ pub struct ReadoutRequiredAccuracy { /// a copy each further one. pub levels: Vec, /// The queries, by index, whose target sets a failure probability - /// without a per-value one at the readout (the values it covers or the - /// values per evaluation of a readout group below it unknown). + /// without a per-value one at the readout: no bound on the readout + /// values its covered output values depend on (the readout values per + /// evaluation unknown, and so is the fan-in or the values covered). pub queries_without_per_value_failure_probability: Vec, } @@ -60,7 +61,7 @@ pub struct ReadoutRequiredAccuracy { /// - A query whose path reads one readout group gives one level: its target /// divided by the path's factor, the readout group's cap, at the target's /// failure probability per readout value ([`query_levels`]: split over -/// every readout value the query's output can depend on). +/// the readout values the output values its scope covers depend on). /// - A composed query (several readouts whose errors reach its output, /// β > 0, such as a ratio; `q and r` and `q or r` combine none: /// [`readouts_read_by_queries`]) gives every level of the grid @@ -143,7 +144,7 @@ fn group_levels( let Some(query_levels) = query_levels( target, memo.queries()[query].number_of_values_per_query_evaluation, - readouts_read.readout_values.get(&query).copied().flatten(), + readouts_read.readout_values[&query], metric, cap, readouts_read.composed.contains(&query), @@ -165,37 +166,29 @@ fn group_levels( /// The requirement levels one query gives a readout in `metric` whose cap /// for it is `cap`; `None` when its target's failure probability has no -/// per-value one. The query's result fails when any readout value it can -/// depend on does, so by the union bound δ splits over all of them, -/// `readout_values` ([`ReadoutsReadByQueries::readout_values`], summed over -/// every readout group below the query), and over the values its scope -/// covers, $V$ (`AccuracyTarget::failure_probability_per_output_value`, -/// $\delta / V$ with $V = n_q$ for a whole result): the per-value failure -/// probability is $\min(\delta / V, \delta / \text{readout values})$, and -/// needs both known when the target sets a δ. +/// per-value one. The query's statement fails only when a readout value +/// one of the output values it covers depends on does, so δ splits over +/// those (`AccuracyTarget::failure_probability_per_readout_value`, over +/// `readout_values` of [`ReadoutsReadByQueries::readout_values`]). /// - `max(quantile_over_time(0.9, m[5m]))` over 1000 series at δ = 0.01 /// returns one value but reads 1000 readout values: 1e-5. /// - `max by (job) (q{host="h0"} or q{host="h1"})` with 3 series per side -/// reads 6 readout values: δ / 6, not one side's δ / 3. -/// - A bare `q or r` copies each output value from one readout value, yet -/// a whole result fails when any of them does: δ over the readout values -/// of `q` and `r` together, at least $n_q$. A per-value scope ($V = 1$) -/// also splits over all readout values, which is conservative here. +/// reads 6 readout values, and a job's value may depend on all: δ / 6. +/// - A bare `q or r` copies each output value from one readout value: +/// per value δ, over a whole result of $n_q$ values δ / $n_q$. /// - `SELECT count(DISTINCT dst)` reads one readout value: 0.01. fn query_levels( target: &AccuracyTarget, number_of_values_per_query_evaluation: Option, - readout_values: Option, + readout_values: ReadoutValuesRead, metric: AccuracyMetric, cap: f64, composed: bool, ) -> Option> { - let per_value_failure_probability = match target.failure_probability { - None => 1.0, - Some(delta) => target - .failure_probability_per_output_value(number_of_values_per_query_evaluation)? - .min(delta / readout_values?.max(1) as f64), - }; + let per_value_failure_probability = target.failure_probability_per_readout_value( + number_of_values_per_query_evaluation, + readout_values, + )?; let level = |max_error, failure_probability| RequiredAccuracy { metric, max_error, @@ -308,15 +301,19 @@ struct ReadoutsReadByQueries { /// single level of its cap. Readouts of one value by different summary /// types are alternatives in one group's candidates, not a combination. composed: BTreeSet, - /// Per query, by index: the readout values per evaluation its output - /// can depend on through β > 0, summed over the readout groups below it - /// (`Memo::max_values_per_evaluation` each) and, among alternative - /// candidates of a group, the largest; `None` when one of them is - /// unknown. `max by (job) (q{host="h0"} or q{host="h1"})` with 3 series - /// per side: 6, since `or` reads both sides; `q / count(r)`: `q`'s alone. - /// A readout group reached on two paths (`q or q`) counts twice, which - /// only tightens the split. - readout_values: BTreeMap>, + /// Per query, by index: the readout values its output can depend on + /// through β > 0, among alternative candidates of a group the largest. + /// Per evaluation, summed over the readout groups below it + /// (`Memo::max_values_per_evaluation` each); per output value, 1 at a + /// readout, carried value by value through + /// [`inputs_read_value_by_value`] operators (summed over inputs, or the + /// largest for `or`) and all of the inputs' per-evaluation values + /// through any other operator (a reduction). `max by (job) + /// (q{host="h0"} or q{host="h1"})` with 3 series per side: 6 and 6; + /// `q or r` alone: 6 and 1; `q / count(r)`: `q`'s alone. A readout + /// group reached on two paths (`q or q`) counts twice, which only + /// tightens the split. + readout_values: BTreeMap, } /// The [`ReadoutsReadByQueries`] of `memo`, in one walk up `order` (the @@ -329,19 +326,23 @@ fn readouts_read_by_queries( ) -> ReadoutsReadByQueries { // Per group: the readout values its output can depend on (`Some(0)` // when none, so it carries no readout error; `None` unknown). - let mut readout_values_per_group: Vec> = vec![Some(0); memo.groups().len()]; + let none_read = ReadoutValuesRead { + per_evaluation: Some(0), + per_output_value: Some(0), + }; + let mut readout_values_per_group = vec![none_read; memo.groups().len()]; let mut composed = BTreeSet::new(); for &group in order { let candidates = &memo.groups()[group.0 as usize].candidates; let mut most_inputs_reading_with_error = 0; - let mut most_readout_values = Some(0); + let mut most_readout_values = none_read; for (candidate, contract_per_metric) in candidates.iter().zip(&error_contract[group.0 as usize]) { let operator = &candidate.expression.operator; let children = operator.children(); let reads_with_error = |input: &usize| { - readout_values_per_group[children[*input].0 as usize] != Some(0) + readout_values_per_group[children[*input].0 as usize].per_evaluation != Some(0) && contract_per_metric .iter() .flatten() @@ -357,15 +358,36 @@ fn readouts_read_by_queries( inputs_reading_with_error }; most_inputs_reading_with_error = most_inputs_reading_with_error.max(inputs_combined); - let readout_values = - (0..children.len()) - .filter(reads_with_error) - .try_fold(0u64, |total, input| { - total.checked_add(readout_values_per_group[children[input].0 as usize]?) + let read_of = |input: usize| readout_values_per_group[children[input].0 as usize]; + let per_evaluation = (0..children.len()) + .filter(reads_with_error) + .try_fold(0u64, |total, input| { + total.checked_add(read_of(input).per_evaluation?) + }); + // One output value reads one value of each of the first + // `value_by_value` inputs, and every value of any other input. + let per_output_value = + inputs_read_value_by_value(operator).map_or(per_evaluation, |value_by_value| { + let mut per_input = (0..children.len()).filter(reads_with_error).map(|input| { + if input < value_by_value { + read_of(input).per_output_value + } else { + read_of(input).per_evaluation + } }); - most_readout_values = most_readout_values - .zip(readout_values) - .map(|(most, values)| most.max(values)); + if takes_each_value_from_one_input(operator) { + per_input.try_fold(0u64, |most, values| Some(most.max(values?))) + } else { + per_input.try_fold(0u64, |total, values| total.checked_add(values?)) + } + }); + let largest = |most: Option, values: Option| { + most.zip(values).map(|(most, values)| most.max(values)) + }; + most_readout_values = ReadoutValuesRead { + per_evaluation: largest(most_readout_values.per_evaluation, per_evaluation), + per_output_value: largest(most_readout_values.per_output_value, per_output_value), + }; } if most_inputs_reading_with_error > 1 { composed.extend( @@ -376,17 +398,29 @@ fn readouts_read_by_queries( ); } readout_values_per_group[group.0 as usize] = if readout_groups.contains(&group) { - memo.max_values_per_evaluation(group) + ReadoutValuesRead { + per_evaluation: memo.max_values_per_evaluation(group), + per_output_value: Some(1), + } } else { most_readout_values }; } - let mut readout_values: BTreeMap> = BTreeMap::new(); + // A query of several roots (a scalar expression over them) reads every + // root's values, one output value each root's output value. + let mut readout_values: BTreeMap = BTreeMap::new(); for (query, root, _, _) in query_roots_with_target(memo) { - let total = readout_values.entry(query).or_insert(Some(0)); - *total = total - .zip(readout_values_per_group[root.0 as usize]) - .and_then(|(total, values)| total.checked_add(values)); + let total = readout_values.entry(query).or_insert(none_read); + let root = readout_values_per_group[root.0 as usize]; + let add = |total: Option, values: Option| { + total + .zip(values) + .and_then(|(total, values)| total.checked_add(values)) + }; + *total = ReadoutValuesRead { + per_evaluation: add(total.per_evaluation, root.per_evaluation), + per_output_value: add(total.per_output_value, root.per_output_value), + }; } ReadoutsReadByQueries { composed, @@ -394,6 +428,35 @@ fn readouts_read_by_queries( } } +/// How many direct inputs `operator` reads value by value: each output +/// value from one value (row or series) of each of them; `None` for an +/// operator one of whose output values may read many values of an input +/// (an aggregate, a window function, a time range or subquery, a `limit` +/// choosing rows by value, a deduplication). A filter, projection, sort, +/// time shift, relabel, label enrichment or series sample: 1, each output +/// value one input value. A binary operator, join or relational set +/// operator: 2, one value of each side; a concatenation: each of its +/// inputs. Operators its scalar expressions reference (`scalar(r)`) follow +/// the direct inputs and are read whole. +fn inputs_read_value_by_value(operator: &Operator) -> Option { + match operator { + Operator::NonASAP( + NonASAPOp::Filter { .. } + | NonASAPOp::Project { .. } + | NonASAPOp::Sort { .. } + | NonASAPOp::TimeShift { .. } + | NonASAPOp::PromqlRelabel { .. } + | NonASAPOp::PromqlInfoEnrich { .. } + | NonASAPOp::PromqlSeriesSample { .. }, + ) => Some(1), + Operator::NonASAP( + NonASAPOp::BinaryOp { .. } | NonASAPOp::Join { .. } | NonASAPOp::SetOp { .. }, + ) => Some(2), + Operator::NonASAP(NonASAPOp::Concat { children }) => Some(children.len()), + _ => None, + } +} + /// Whether `operator` copies each output value from exactly one of its /// inputs: a PromQL set operator (`q or r` takes a series' value from `q` /// when `q` has the series, else from `r`; `and` and `unless` only from diff --git a/crates/accuracy/src/readout_caps/tests.rs b/crates/accuracy/src/readout_caps/tests.rs index 5fb1127e4..ed2d74495 100644 --- a/crates/accuracy/src/readout_caps/tests.rs +++ b/crates/accuracy/src/readout_caps/tests.rs @@ -5,13 +5,13 @@ use asap_types::ir::operator::AggIntent; use asap_types::ir::scalar::expr_ir::ArithmeticOpKind; use asap_types::ir::QueryRoot; use asap_types::stages::{MemoQuery, ValueSign}; -use asap_types::workload::DataWorkload; use asap_types::summary_type::{SketchAlgorithm, SketchStatistic}; use asap_types::test_support::memo_builders::{ estimate, group, own_candidate, quantile_over_time, query, scan_group, sketch_state, with_series, }; use asap_types::test_support::memo_fixtures::{aggregate, arithmetic, set_operator}; +use asap_types::workload::DataWorkload; /// The levels [`readout_levels`] gives the readout candidate `readout`. fn levels_of(memo: &Memo, readout: MemoGroupId) -> Vec { @@ -321,7 +321,10 @@ fn failure_probability_of(memo: &Memo, readout: MemoGroupId) -> Option { let required = readout_levels(memo, &ErrorMetricsOfMemo::of(memo, &order), &order) .remove(&own_candidate(readout)) .expect("a sketch readout"); - if !required.queries_without_per_value_failure_probability.is_empty() { + if !required + .queries_without_per_value_failure_probability + .is_empty() + { assert_eq!(required.levels, vec![], "a listed query gives no level"); return None; } @@ -429,7 +432,11 @@ fn a_grouped_sum_per_value_splits_over_every_readout_value_when_group_sizes_are_ let (_, _, readout) = quantile_over_time(&mut memo, SketchAlgorithm::DDSketch, 0.9, 300); let sum = group( &mut memo, - aggregate(Reduction::by(vec![0]), AggIntent::Sum { col: None }, readout), + aggregate( + Reduction::by(vec![0]), + AggIntent::Sum { col: None }, + readout, + ), ); with_query( &mut memo, @@ -448,7 +455,11 @@ fn a_grouped_sum_over_its_whole_result_splits_over_every_readout_value() { let (_, _, readout) = quantile_over_time(&mut memo, SketchAlgorithm::DDSketch, 0.9, 300); let sum = group( &mut memo, - aggregate(Reduction::by(vec![0]), AggIntent::Sum { col: None }, readout), + aggregate( + Reduction::by(vec![0]), + AggIntent::Sum { col: None }, + readout, + ), ); with_query( &mut memo, @@ -488,7 +499,11 @@ fn nested_reductions_split_over_every_readout_value_below_them() { let (_, _, readout) = quantile_over_time(&mut memo, SketchAlgorithm::DDSketch, 0.9, 300); let sum = group( &mut memo, - aggregate(Reduction::by(vec![0]), AggIntent::Sum { col: None }, readout), + aggregate( + Reduction::by(vec![0]), + AggIntent::Sum { col: None }, + readout, + ), ); let max = group( &mut memo, diff --git a/crates/types/src/accuracy/mod.rs b/crates/types/src/accuracy/mod.rs index 6780f97d5..f97dc28e9 100644 --- a/crates/types/src/accuracy/mod.rs +++ b/crates/types/src/accuracy/mod.rs @@ -10,4 +10,6 @@ mod target; pub use default_metric::default_metric_for_root; pub use guarantee::{AccuracyGuarantee, BoundExpr, ProbabilityExpr}; -pub use target::{AccuracyMetric, AccuracyScope, AccuracySpec, AccuracyTarget, NormScale}; +pub use target::{ + AccuracyMetric, AccuracyScope, AccuracySpec, AccuracyTarget, NormScale, ReadoutValuesRead, +}; diff --git a/crates/types/src/accuracy/target/mod.rs b/crates/types/src/accuracy/target/mod.rs index 5ed6aa715..d3a46d24b 100644 --- a/crates/types/src/accuracy/target/mod.rs +++ b/crates/types/src/accuracy/target/mod.rs @@ -167,9 +167,10 @@ impl AccuracyScope { /// when unbound: one for [`Self::PerValue`], `G` for /// [`Self::WholeResult`], and `min(⌊1 / share⌋, G)` for /// [`Self::GroupsWithRecordShareAtLeast`], which is bound even when `G` - /// is not. Sizing divides a target's failure probability by it, and the - /// guarantee check multiplies a per-value one by it, so the strictest of - /// several scopes is the one that covers the most values. + /// is not. A target's failure probability splits over the readout values + /// these values depend on + /// ([`AccuracyTarget::failure_probability_per_readout_value`]), so the + /// strictest of several scopes is the one that covers the most values. pub fn max_values_covered(self, values_per_result: Option) -> Option { match self { Self::PerValue => Some(1), @@ -303,8 +304,8 @@ impl AccuracyTarget { /// `values_per_evaluation` ($n_q$; [`AccuracyScope::max_values_covered`]). /// `None` when the target sets no δ, and when that count is not known /// for a δ over the whole result. A readout value's failure probability - /// splits δ further over the readout values an output value depends - /// on, and is built on this one. + /// splits δ over the readout values the covered output values depend + /// on instead ([`Self::failure_probability_per_readout_value`]). /// /// Example: δ = 0.01 over the whole result of a query returning 100 /// series gives 0.0001 per output value; with the number of series @@ -329,6 +330,62 @@ impl AccuracyTarget { .filter(|&metric| self.accuracy_metric.is_none_or(|named| named == metric)) .collect() } + + /// The failure probability each readout value below the query must + /// meet: δ split by the union bound over the readout values the output + /// values one statement covers depend on, `readout_values`, when one + /// evaluation returns `values_per_evaluation` ($n_q$). 1 when the + /// target sets no δ; `None` when no bound on that count is known. + /// + /// The statement covers at most `V` output values + /// ([`AccuracyScope::max_values_covered`]), each depending on at most + /// `fan_in` readout values, and the query reads `R` in all, so it fails + /// only when one of at most `min(R, V · fan_in)` readout values does, + /// whatever their dependence. An unknown term drops out of the `min`. + /// + /// Examples at δ = 0.01: + /// - `quantile_over_time(0.99, m[5m])` over 8 series per value: + /// min(8, 1 · 1) = 1, so 0.01; over the whole result min(8, 8 · 1) = 8. + /// - `max(quantile_over_time(0.9, m[5m]))` over 1000 series, any scope: + /// min(1000, 1 · 1000) = 1000, so 1e-5. + /// - Per value over series of unknown count without a reduction: + /// min(unknown, 1 · 1) = 1. + pub fn failure_probability_per_readout_value( + &self, + values_per_evaluation: Option, + readout_values: ReadoutValuesRead, + ) -> Option { + let Some(delta) = self.failure_probability else { + return Some(1.0); + }; + let readout_values_covered = self + .accuracy_scope + .max_values_covered(values_per_evaluation) + .zip(readout_values.per_output_value) + .map(|(values, fan_in)| values.saturating_mul(fan_in)); + let split_over = match (readout_values.per_evaluation, readout_values_covered) { + (Some(all), Some(covered)) => all.min(covered), + (all, covered) => all.or(covered)?, + }; + Some(delta / split_over.max(1) as f64) + } +} + +/// The readout values (sketch estimates) a query's output depends on, per +/// evaluation: what its failure probability splits over +/// ([`AccuracyTarget::failure_probability_per_readout_value`]). `None` is +/// unknown. +/// +/// Example: `sum by (job) (quantile_over_time(0.9, m[5m]))` over 1000 +/// series reads 1000 readout values per evaluation, and one output value +/// may depend on all of them (a job's series are not bounded), so both are +/// 1000; without the `sum`, 1000 and 1. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct ReadoutValuesRead { + /// `R`: every readout value one evaluation reads. + pub per_evaluation: Option, + /// The fan-in: the most readout values one output value depends on. + pub per_output_value: Option, } #[cfg(test)] diff --git a/crates/types/src/accuracy/target/tests.rs b/crates/types/src/accuracy/target/tests.rs index 290b10a52..5bfd5b255 100644 --- a/crates/types/src/accuracy/target/tests.rs +++ b/crates/types/src/accuracy/target/tests.rs @@ -354,3 +354,51 @@ fn a_whole_result_failure_probability_splits_over_the_output_values() { None ); } + +/// δ = 0.01 splits over min(R, V · fan-in) readout values; an unknown term +/// drops out of the `min`. +#[test] +fn failure_probability_splits_over_the_readout_values_covered() { + let read = |per_evaluation, per_output_value| ReadoutValuesRead { + per_evaluation, + per_output_value, + }; + let per_value = AccuracyTarget::max_error(0.05) + .failure_probability(0.01) + .accuracy_scope(AccuracyScope::PerValue); + let whole = per_value.clone().accuracy_scope(AccuracyScope::WholeResult); + // (target, n_q, readout values, divisor) + let cases = [ + // 8 series, no reduction. + (&per_value, Some(8), read(Some(8), Some(1)), 1), + (&whole, Some(8), read(Some(8), Some(1)), 8), + // A global `max` over 1000 series. + (&per_value, Some(1), read(Some(1000), Some(1000)), 1000), + (&whole, Some(1), read(Some(1000), Some(1000)), 1000), + // A grouped sum whose groups are known to hold 100 series each. + (&per_value, Some(10), read(Some(1000), Some(100)), 100), + (&whole, Some(10), read(Some(1000), Some(100)), 1000), + // Unknown terms. + (&per_value, None, read(None, Some(1)), 1), + (&whole, None, read(Some(100), Some(1)), 100), + (&whole, Some(8), read(None, Some(1)), 8), + (&per_value, Some(1), read(Some(1000), None), 1000), + ]; + for (target, n_q, readout_values, divisor) in cases { + let split = target.failure_probability_per_readout_value(n_q, readout_values); + assert_eq!( + split, + Some(0.01 / divisor as f64), + "{target:?} {n_q:?} {readout_values:?}" + ); + } + assert_eq!( + whole.failure_probability_per_readout_value(None, read(None, Some(1))), + None + ); + assert_eq!( + AccuracyTarget::max_error(0.05) + .failure_probability_per_readout_value(None, read(None, None)), + Some(1.0) + ); +} diff --git a/docs/design_docs/concepts/accuracy-models.md b/docs/design_docs/concepts/accuracy-models.md index c698b99cf..92a4aa653 100644 --- a/docs/design_docs/concepts/accuracy-models.md +++ b/docs/design_docs/concepts/accuracy-models.md @@ -78,12 +78,22 @@ unknown (HLL without a distinct bound) serves no query with an accuracy target heap size and its value guarantee alone (the contracts table). Pass 2 gives each sketch readout the error and per-value failure probability -`δ / max(V, R)` it must meet, with `R` the readout values the query's output -can depend on (the values per evaluation of every readout group below it, -summed: the series their scans read): a `max` over 1000 readouts returns -one value yet fails when any readout does, so the union bound gives δ over -every readout read, and `max by (job) (q or r)` reads those of both sides; -Pass 1's candidate takes the first requirement level and Pass 2 adds a copy +`δ / min(R, V · fan_in)` it must meet (`AccuracyTarget::failure_probability_per_readout_value`): +the statement fails only when a readout value one of its `V` covered output +values depends on does. `R` is the readout values the query reads per +evaluation (the values per evaluation of every readout group below it, +summed: the series their scans read), and `fan_in` the most readout values +one output value depends on: 1 when no reduction lies above the readout, +the reduction's input values under one (all of `R` for a global `max`; for +`sum by (job)` too, since the declared facts bound how many jobs there are, +not how many series one job holds), the sum over the sides of a binary +operator, the larger side of `or`. An unknown term drops out of the `min`; +with both unknown no per-value failure probability follows. At δ = 0.01, +`quantile_over_time(0.99, m[5m])` over 8 series per value keeps 0.01 +(min(8, 1 · 1)), and over its whole result gets 0.01 / 8; a `max` over 1000 +readouts returns one value yet fails when any readout does, so it gets +0.01 / 1000 in any scope; and `max by (job) (q or r)` splits over the +readouts of both sides. Pass 1's candidate takes the first requirement level and Pass 2 adds a copy per further level. A readout several queries reach gets the union of their levels. Pass 2 sizes nothing: a state has one build per listed θ, and each edge checks the guarantee of its producer's θ at the required failure @@ -91,8 +101,8 @@ probability, so a build several readers share serves the readers whose levels its θ meets. Stage 3 only checks the guarantee; it never resizes. At equal δ, the scope covering the most values is the strictest: every group (`G`, when `G` is above 100), then a 1% share (100), then a 5% share (20), then a -single value (1). A whole-result target with an unbound `G` -enlarges nothing and stays unmet. A grouped result's `G` is bound by the +single value (1). A whole-result target with an unbound `G` splits +over `R` alone, and stays unmet when `R` is unbound too. A grouped result's `G` is bound by the distinct values its sources declare for its group keys (`SourceFacts.distinct_values_per_label`, and `distinct_values_per_label_set` for labels that depend on each other): the @@ -297,17 +307,17 @@ largest error the readout's output may have for that query. - A query whose path reads one readout gives one requirement level: the target divided by the path's factor, at the target's failure probability per - value: a whole-result δ split over the larger of `n_q` and the readout - values the query's output can depend on, summed over every readout group - below it (`Memo::max_values_per_evaluation` each: at most the series + value: δ split over `min(R, V · fan_in)`, the readout values the output + values the scope covers depend on (`R` summed over every readout group + below it, `Memo::max_values_per_evaluation` each: at most the series scanned below it, the declared distinct values of its state's keys, one without keys), since a reduction such as `max` fails when any readout it reads does. `SELECT count(DISTINCT dst) FROM flows` at δ = 0.01 reads one distinct count, so its readout keeps 0.01. `max by (job) (q or r)` - with 3 series per side reads 6 readout values, so each gets δ / 6; a - bare `q or r` copies each output value from one readout value, but its - whole result still fails when any of them does, so it too splits δ over - the readout values of both sides. + with 3 series per side reads 6 readout values, and one job's value may + depend on all, so each gets δ / 6; a bare `q or r` copies each output + value from one readout value, so per value it keeps δ and over a whole + result of `n_q` values gets δ / `n_q`. - A composed query (several readouts whose errors reach its output, `β > 0`, such as a ratio; `q and r` reads one, since `r`'s values are not read, and `q or r` combines none, since each output value is copied from one side From ad81ca8686b80cb6733c98eed5e7ed2673055f30 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:32:32 +0000 Subject: [PATCH 03/24] test(accuracy): readout groups holding reductions, top-k readouts, concatenations and redundant levels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A readout group that also holds a reduction over other readouts, a top-k readout, and a concatenation each split δ over the readout values their output values depend on; a level the same listed builds meet as a stricter one is dropped. --- crates/accuracy/src/readout_caps/tests.rs | 146 +++++++++++++++++++++- 1 file changed, 143 insertions(+), 3 deletions(-) diff --git a/crates/accuracy/src/readout_caps/tests.rs b/crates/accuracy/src/readout_caps/tests.rs index ed2d74495..3b47319d1 100644 --- a/crates/accuracy/src/readout_caps/tests.rs +++ b/crates/accuracy/src/readout_caps/tests.rs @@ -7,10 +7,10 @@ use asap_types::ir::QueryRoot; use asap_types::stages::{MemoQuery, ValueSign}; use asap_types::summary_type::{SketchAlgorithm, SketchStatistic}; use asap_types::test_support::memo_builders::{ - estimate, group, own_candidate, quantile_over_time, query, scan_group, sketch_state, - with_series, + candidate, estimate, group, own_candidate, quantile_over_time, query, scan_group, + sketch_state, with_series, }; -use asap_types::test_support::memo_fixtures::{aggregate, arithmetic, set_operator}; +use asap_types::test_support::memo_fixtures::{aggregate, arithmetic, range, set_operator}; use asap_types::workload::DataWorkload; /// The levels [`readout_levels`] gives the readout candidate `readout`. @@ -606,3 +606,143 @@ fn a_per_value_ratio_splits_over_the_two_readout_values_of_each_output_value() { assert!((loosest - 1e-3).abs() < 1e-15, "{loosest}"); } } + +/// A readout group whose candidates are a readout and a reduction over +/// other readouts: `quantile(0.9)` over every sample of 1000 series, read +/// from one global sketch, or summed over the 1000 per-series readouts +/// below it (an alternative with the same output). A per-value target on +/// it covers one output value, which the reduction makes depend on all +/// 1000 per-series readout values: each of them gets δ / 1000, not the δ +/// the global readout's own fan-in of 1 would give. +#[test] +fn a_readout_group_holding_a_reduction_splits_over_the_readout_values_below_it() { + let mut memo = Memo::new(&with_series_over_jobs(1000, 10)); + let (window, _, per_series) = + quantile_over_time(&mut memo, SketchAlgorithm::DDSketch, 0.9, 300); + let global_state = sketch_state( + &mut memo, + window, + SketchAlgorithm::DDSketch, + Reduction::by(vec![]), + ); + let global = estimate(&mut memo, global_state, SketchStatistic::Quantile { q: 0.9 }); + let sum_of_readouts = Operator::NonASAP(NonASAPOp::Aggregate { + child: per_series, + reduction: Reduction::by(vec![]), + measures: vec![AggIntent::Sum { col: None }], + output_names: vec!["quantile".into()], + filters: vec![], + having: None, + }); + candidate(&mut memo, global, sum_of_readouts, None); + with_query( + &mut memo, + global, + relative_target(AccuracyScope::PerValue), + Some(1), + ); + assert_failure_probability(failure_probability_of(&memo, per_series), 0.01 / 1000.0); +} + +/// A top-k readout chooses its output keys by their estimated values, so +/// one output value (a key in the top 10) depends on the estimates of +/// every key it was ranked against: CMS-with-heap top-10 over 1000 series +/// gets δ / 1000 per value, not δ. +#[test] +fn a_topk_readout_splits_over_every_value_it_ranks() { + let mut memo = Memo::new(&with_series("m", 1000)); + let input = scan_group(&mut memo, "m", &["job"], vec![]); + let window = group(&mut memo, range(300_000, input)); + let state = sketch_state( + &mut memo, + window, + SketchAlgorithm::CmsWithHeap, + Reduction::PerEntity, + ); + let topk = estimate(&mut memo, state, SketchStatistic::TopK { k: 10 }); + with_query( + &mut memo, + topk, + AccuracyTarget::max_error(0.05) + .failure_probability(0.01) + .accuracy_scope(AccuracyScope::PerValue), + Some(10), + ); + assert_failure_probability(failure_probability_of(&memo, topk), 0.01 / 1000.0); +} + +/// A concatenation copies each output row from one of its inputs, as `or` +/// does: per value, each of two quantile readouts over 100 series keeps +/// the single level of its cap at δ = 0.01, neither the composed grid nor +/// δ / 2. +#[test] +fn a_concatenation_takes_each_value_from_one_readout() { + let mut memo = Memo::new(&with_series("m", 100)); + let (_, state, p90) = quantile_over_time(&mut memo, SketchAlgorithm::DDSketch, 0.9, 300); + let p50 = estimate(&mut memo, state, SketchStatistic::Quantile { q: 0.5 }); + let both = group( + &mut memo, + Operator::NonASAP(NonASAPOp::Concat { + children: vec![p90, p50], + }), + ); + with_query( + &mut memo, + both, + relative_target(AccuracyScope::PerValue), + Some(200), + ); + for readout in [p90, p50] { + assert_failure_probability(failure_probability_of(&memo, readout), 0.01); + } +} + +/// Two queries on one DDSketch quantile readout over 100 series, one per +/// value (δ = 0.01) and one over the whole result (δ / 100 = 1e-4), both +/// at relative error 0.05. DDSketch's relative error does not depend on +/// δ, so the same listed builds meet both levels: the looser one is +/// redundant and only the stricter, (0.05, 1e-4), is kept. +#[test] +fn a_level_met_by_the_same_builds_as_a_stricter_one_is_dropped() { + let mut memo = Memo::new(&with_series("m", 100)); + let (_, _, readout) = quantile_over_time(&mut memo, SketchAlgorithm::DDSketch, 0.9, 300); + with_query( + &mut memo, + readout, + relative_target(AccuracyScope::PerValue), + Some(100), + ); + with_query( + &mut memo, + readout, + relative_target(AccuracyScope::WholeResult), + Some(100), + ); + let [level] = levels_of(&memo, readout)[..] else { + panic!("one level: {:?}", levels_of(&memo, readout)); + }; + assert!((level.max_error - 0.05).abs() < 1e-12, "{level:?}"); + assert_failure_probability(Some(level.failure_probability), 1e-4); +} + +/// Levels that different builds meet are both kept: a KLL rank error of +/// 0.0025 over 8 series at δ = 0.01 per value needs k = 1404 and over the +/// whole result (δ / 8) k = 2863, so k = 1404 meets only the first. +#[test] +fn levels_met_by_different_builds_are_both_kept() { + let mut memo = Memo::new(&with_series("m", 8)); + let (_, _, readout) = quantile_over_time(&mut memo, SketchAlgorithm::Kll, 0.99, 300); + let rank = |scope| { + AccuracyTarget::max_error(0.0025) + .failure_probability(0.01) + .accuracy_metric(AccuracyMetric::Rank) + .accuracy_scope(scope) + }; + with_query(&mut memo, readout, rank(AccuracyScope::PerValue), Some(8)); + with_query(&mut memo, readout, rank(AccuracyScope::WholeResult), Some(8)); + let failure_probabilities: Vec = levels_of(&memo, readout) + .into_iter() + .map(|level| level.failure_probability) + .collect(); + assert_eq!(failure_probabilities, vec![0.01, 0.01 / 8.0]); +} From e9d30085d51f1d2a4b33965a286ba129f8972bc3 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:39:29 +0000 Subject: [PATCH 04/24] fix(accuracy): count a readout group's other candidates, rank top-k over all values, drop redundant levels - A readout group takes the larger of its own readout counts and its other candidates' (a reduction over readouts below it), field by field. - A top-k readout's output value depends on every value it ranks. - A concatenation takes each value from one input, like `or`. - A level the same listed builds meet as a stricter level of the group is dropped: a copy at it accepts the same producers. - ReadoutValuesRead is renamed ReadoutValueCounts, with larger and plus. --- crates/accuracy/src/logical_edge/mod.rs | 64 ++++-- crates/accuracy/src/readout_caps/mod.rs | 194 ++++++++++++++---- crates/accuracy/src/readout_caps/tests.rs | 90 +++++--- .../stage1-pass2-reuse-candidates/src/lib.rs | 2 +- .../src/readout_requirements.rs | 9 +- crates/types/src/accuracy/mod.rs | 2 +- crates/types/src/accuracy/target/mod.rs | 28 ++- crates/types/src/accuracy/target/tests.rs | 22 +- docs/design_docs/concepts/accuracy-models.md | 63 +++--- 9 files changed, 360 insertions(+), 114 deletions(-) diff --git a/crates/accuracy/src/logical_edge/mod.rs b/crates/accuracy/src/logical_edge/mod.rs index f2f8fe857..30229470f 100644 --- a/crates/accuracy/src/logical_edge/mod.rs +++ b/crates/accuracy/src/logical_edge/mod.rs @@ -3,12 +3,15 @@ //! comparison of an evaluated bound with a required limit //! ([`within_limit`]). -use asap_types::accuracy::AccuracyMetric; +use asap_types::accuracy::{AccuracyGuarantee, AccuracyMetric}; +use asap_types::ir::schema::FieldDataType; use asap_types::ir::{ASAPOp, Operator}; -use asap_types::stages::{CandidateForMemoGroupId, Memo, RootPruneReason}; +use asap_types::stages::{CandidateForMemoGroupId, Memo, RequiredAccuracy, RootPruneReason}; use asap_types::summary_type::{MergedError, SketchParams, SketchStatistic, SummaryTypeCapability}; -use crate::guarantee::{guarantee_before_failure_probability_check, holds_at_failure_probability}; +use crate::guarantee::{ + guarantee_before_failure_probability_check, holds_at_failure_probability, theoretical_guarantee, +}; use crate::readout::sketch_readout; use crate::{AccuracyEvidence, ErrorMetricsOfMemo}; @@ -169,15 +172,7 @@ impl ErrorMetricsOfMemo { holds_at_failure_probability(guarantee, required.failure_probability) }) }); - let meets = guarantee.is_some_and(|guarantee| { - guarantee.spec.accuracy_metric == required.metric - && guarantee - .spec - .error - .evaluate() - .is_some_and(|error| within_limit(error, required.max_error)) - }); - if keeps && meets { + if keeps && guarantee_meets(guarantee.as_ref(), required) { Ok(()) } else { Err(RootPruneReason::BelowRequiredAccuracy { @@ -189,6 +184,51 @@ impl ErrorMetricsOfMemo { } } +/// Whether a state of `summary_type` built with `parameters` meets readout +/// level `required` for `statistic`, as the edge from the build checks it +/// ([`ErrorMetricsOfMemo::edge_meets_logical_requirements`]): its guarantee +/// at the required failure probability (`evidence`'s measurement, else the +/// theoretical one) is in the required metric and within the required +/// error. A KLL build of k = 1404 meets rank error 0.0025 at δ = 0.01, not +/// at δ = 0.00125. +pub(crate) fn build_meets_level( + summary_type: &FieldDataType, + parameters: &SketchParams, + statistic: &SketchStatistic, + required: RequiredAccuracy, + evidence: &dyn AccuracyEvidence, +) -> bool { + let guarantee = evidence + .measured_guarantee( + summary_type, + parameters, + statistic, + required.failure_probability, + ) + .or_else(|| { + theoretical_guarantee( + summary_type, + parameters, + statistic, + required.failure_probability, + ) + }); + guarantee_meets(guarantee.as_ref(), required) +} + +/// Whether `guarantee`, a θ's at the required failure probability, meets +/// `required`: in its metric, with an error within its error. +fn guarantee_meets(guarantee: Option<&AccuracyGuarantee>, required: RequiredAccuracy) -> bool { + guarantee.is_some_and(|guarantee| { + guarantee.spec.accuracy_metric == required.metric + && guarantee + .spec + .error + .evaluate() + .is_some_and(|error| within_limit(error, required.max_error)) + }) +} + /// A heap variant's heap size. fn heap_size(parameters: &SketchParams) -> Option { match parameters { diff --git a/crates/accuracy/src/readout_caps/mod.rs b/crates/accuracy/src/readout_caps/mod.rs index 100ccbf27..f76cd3de1 100644 --- a/crates/accuracy/src/readout_caps/mod.rs +++ b/crates/accuracy/src/readout_caps/mod.rs @@ -5,13 +5,17 @@ use std::collections::{BTreeMap, BTreeSet, HashMap}; -use asap_types::accuracy::{AccuracyMetric, AccuracyTarget, ReadoutValuesRead}; +use asap_types::accuracy::{AccuracyMetric, AccuracyTarget, ReadoutValueCounts}; use asap_types::ir::operator::arguments::BinaryOpKind; -use asap_types::ir::{BinaryOperator, NonASAPOp, Operator}; +use asap_types::ir::schema::FieldDataType; +use asap_types::ir::{ASAPOp, BinaryOperator, NonASAPOp, Operator}; use asap_types::stages::{CandidateForMemoGroupId, Memo, MemoGroupId, RequiredAccuracy}; +use asap_types::summary_type::{parameter_list, SketchStatistic}; +use crate::logical_edge::build_meets_level; use crate::metric_rule::{query_roots_with_target, ErrorContractPerMetric}; -use crate::ErrorMetricsOfMemo; +use crate::readout::sketch_readout; +use crate::{AccuracyEvidence, ErrorMetricsOfMemo}; /// The error grid of the readouts of a query that combines several /// ([`query_levels`]), loosest first. @@ -57,7 +61,8 @@ pub struct ReadoutRequiredAccuracy { /// The required-accuracy levels of the sketch readouts without a /// requirement yet, per readout ([`ReadoutRequiredAccuracy`]), from /// `error_metrics` of `memo` ([`ErrorMetricsOfMemo::of`]). `order` is the -/// memo's topological order (`Memo::topological_order`). +/// memo's topological order (`Memo::topological_order`); `evidence` is the +/// deployment's measured accuracy the edges check builds against. /// - A query whose path reads one readout group gives one level: its target /// divided by the path's factor, the readout group's cap, at the target's /// failure probability per readout value ([`query_levels`]: split over @@ -73,6 +78,10 @@ pub struct ReadoutRequiredAccuracy { /// levels, in query order; one no query with a target reaches in the /// readout's metric gets none. Readouts of one group in one metric get /// the same levels. +/// - A level is dropped when the listed builds that meet it are exactly +/// those that meet a stricter level of the group ([`builds_meeting`]): +/// any producer a copy at the looser level accepts, the stricter copy +/// accepts too, so the looser copy only adds a choice of equal cost. /// /// A query whose target sets a failure probability that has no per-value /// one at the readout gives no level and is listed instead: Pass 2 prunes @@ -83,6 +92,7 @@ pub fn readout_levels( memo: &Memo, error_metrics: &ErrorMetricsOfMemo, order: &[MemoGroupId], + evidence: &dyn AccuracyEvidence, ) -> BTreeMap { let caps = caps(memo, &error_metrics.error_contract, order); let mut readouts: Vec<(CandidateForMemoGroupId, AccuracyMetric)> = Vec::new(); @@ -107,6 +117,16 @@ pub fn readout_levels( readouts.iter().map(|(readout, _)| readout.group).collect(); let readouts_read = readouts_read_by_queries(memo, &error_metrics.error_contract, order, &readout_groups); + let mut readouts_of_group: HashMap< + (MemoGroupId, AccuracyMetric), + Vec, + > = HashMap::new(); + for &(readout, metric) in &readouts { + readouts_of_group + .entry((readout.group, metric)) + .or_default() + .push(readout); + } let mut levels_of_group: HashMap<(MemoGroupId, AccuracyMetric), ReadoutRequiredAccuracy> = HashMap::new(); readouts @@ -115,7 +135,16 @@ pub fn readout_levels( let group = readout.group; let levels = levels_of_group .entry((group, metric)) - .or_insert_with(|| group_levels(memo, &caps, &readouts_read, group, metric)) + .or_insert_with(|| { + let mut required = group_levels(memo, &caps, &readouts_read, group, metric); + required.levels = without_redundant_levels( + memo, + &readouts_of_group[&(group, metric)], + required.levels, + evidence, + ); + required + }) .clone(); (readout, levels) }) @@ -164,6 +193,68 @@ fn group_levels( } } +/// `levels` of the readouts `readouts` of one group, without each level +/// the listed builds that meet it ([`builds_meeting`]) are exactly those +/// that meet a stricter level of `levels` (at most its error and failure +/// probability): DDSketch's relative error does not depend on δ, so of +/// (0.05, 0.01) and (0.05, 1e-4) only the second is kept; KLL's does, so +/// both are kept when a build meets the first only. Order is kept, and so +/// is every strictest level, so each dropped level's builds stay those of a +/// kept one. +fn without_redundant_levels( + memo: &Memo, + readouts: &[CandidateForMemoGroupId], + levels: Vec, + evidence: &dyn AccuracyEvidence, +) -> Vec { + if levels.len() < 2 { + return levels; + } + let builds: Vec> = levels + .iter() + .map(|&level| builds_meeting(memo, readouts, level, evidence)) + .collect(); + let stricter = |strict: &RequiredAccuracy, loose: &RequiredAccuracy| { + strict != loose + && strict.max_error <= loose.max_error + && strict.failure_probability <= loose.failure_probability + }; + levels + .iter() + .enumerate() + .filter(|&(index, level)| { + !levels.iter().enumerate().any(|(other, stricter_level)| { + stricter(stricter_level, level) && builds[other] == builds[index] + }) + }) + .map(|(_, &level)| level) + .collect() +} + +/// Per readout of `readouts` and listed θ of the summary type it reads +/// (`parameter_list`, the only θ a state candidate may have), in that +/// order: whether a state built with θ meets `level` for the readout's +/// statistic, as the logical edge checks it ([`build_meets_level`]). +fn builds_meeting( + memo: &Memo, + readouts: &[CandidateForMemoGroupId], + level: RequiredAccuracy, + evidence: &dyn AccuracyEvidence, +) -> Vec { + readouts + .iter() + .filter_map(|&readout| sketch_readout(memo, memo.candidate(readout))) + .flat_map(|(summary_type, statistic)| { + let FieldDataType::Sketch(algorithm) = summary_type else { + unreachable!("a sketch readout reads a sketch state") + }; + parameter_list(algorithm).iter().map(move |parameters| { + build_meets_level(&summary_type, parameters, statistic, level, evidence) + }) + }) + .collect() +} + /// The requirement levels one query gives a readout in `metric` whose cap /// for it is `cap`; `None` when its target's failure probability has no /// per-value one. The query's statement fails only when a readout value @@ -180,7 +271,7 @@ fn group_levels( fn query_levels( target: &AccuracyTarget, number_of_values_per_query_evaluation: Option, - readout_values: ReadoutValuesRead, + readout_values: ReadoutValueCounts, metric: AccuracyMetric, cap: f64, composed: bool, @@ -305,15 +396,17 @@ struct ReadoutsReadByQueries { /// through β > 0, among alternative candidates of a group the largest. /// Per evaluation, summed over the readout groups below it /// (`Memo::max_values_per_evaluation` each); per output value, 1 at a - /// readout, carried value by value through - /// [`inputs_read_value_by_value`] operators (summed over inputs, or the - /// largest for `or`) and all of the inputs' per-evaluation values - /// through any other operator (a reduction). `max by (job) + /// readout (all of its values for a top-k, which ranks them), carried + /// value by value through [`inputs_read_value_by_value`] operators + /// (summed over inputs, or the largest for `or` and a concatenation) + /// and all of the inputs' per-evaluation values through any other + /// operator (a reduction); a readout group also counts its other + /// candidates (a reduction over readouts below it). `max by (job) /// (q{host="h0"} or q{host="h1"})` with 3 series per side: 6 and 6; /// `q or r` alone: 6 and 1; `q / count(r)`: `q`'s alone. A readout /// group reached on two paths (`q or q`) counts twice, which only /// tightens the split. - readout_values: BTreeMap, + readout_values: BTreeMap, } /// The [`ReadoutsReadByQueries`] of `memo`, in one walk up `order` (the @@ -326,7 +419,7 @@ fn readouts_read_by_queries( ) -> ReadoutsReadByQueries { // Per group: the readout values its output can depend on (`Some(0)` // when none, so it carries no readout error; `None` unknown). - let none_read = ReadoutValuesRead { + let none_read = ReadoutValueCounts { per_evaluation: Some(0), per_output_value: Some(0), }; @@ -341,6 +434,7 @@ fn readouts_read_by_queries( { let operator = &candidate.expression.operator; let children = operator.children(); + let takes_each_value_from_one_input = takes_each_value_from_one_input(operator); let reads_with_error = |input: &usize| { readout_values_per_group[children[*input].0 as usize].per_evaluation != Some(0) && contract_per_metric @@ -352,7 +446,7 @@ fn readouts_read_by_queries( // Each output value comes from one input, so the output's error // is one input's, never a sum of several; its values still // depend on the readouts of every input. - let inputs_combined = if takes_each_value_from_one_input(operator) { + let inputs_combined = if takes_each_value_from_one_input { inputs_reading_with_error.min(1) } else { inputs_reading_with_error @@ -375,19 +469,16 @@ fn readouts_read_by_queries( read_of(input).per_evaluation } }); - if takes_each_value_from_one_input(operator) { + if takes_each_value_from_one_input { per_input.try_fold(0u64, |most, values| Some(most.max(values?))) } else { per_input.try_fold(0u64, |total, values| total.checked_add(values?)) } }); - let largest = |most: Option, values: Option| { - most.zip(values).map(|(most, values)| most.max(values)) - }; - most_readout_values = ReadoutValuesRead { - per_evaluation: largest(most_readout_values.per_evaluation, per_evaluation), - per_output_value: largest(most_readout_values.per_output_value, per_output_value), - }; + most_readout_values = most_readout_values.larger(ReadoutValueCounts { + per_evaluation, + per_output_value, + }); } if most_inputs_reading_with_error > 1 { composed.extend( @@ -397,30 +488,40 @@ fn readouts_read_by_queries( .copied(), ); } + // A readout group's own readouts read its values per evaluation, + // each output value one of them, or all of them for a top-k, whose + // output keys are chosen by their estimates; any other candidate of + // the group (a reduction over readouts below it) still counts. readout_values_per_group[group.0 as usize] = if readout_groups.contains(&group) { - ReadoutValuesRead { - per_evaluation: memo.max_values_per_evaluation(group), - per_output_value: Some(1), - } + let per_evaluation = memo.max_values_per_evaluation(group); + let ranks_by_estimate = candidates.iter().any(|candidate| { + matches!( + candidate.expression.operator, + Operator::ASAP(ASAPOp::SummaryEstimate { + query: SketchStatistic::TopK { .. }, + .. + }) + ) + }); + let own = ReadoutValueCounts { + per_evaluation, + per_output_value: if ranks_by_estimate { + per_evaluation + } else { + Some(1) + }, + }; + own.larger(most_readout_values) } else { most_readout_values }; } // A query of several roots (a scalar expression over them) reads every // root's values, one output value each root's output value. - let mut readout_values: BTreeMap = BTreeMap::new(); + let mut readout_values: BTreeMap = BTreeMap::new(); for (query, root, _, _) in query_roots_with_target(memo) { let total = readout_values.entry(query).or_insert(none_read); - let root = readout_values_per_group[root.0 as usize]; - let add = |total: Option, values: Option| { - total - .zip(values) - .and_then(|(total, values)| total.checked_add(values)) - }; - *total = ReadoutValuesRead { - per_evaluation: add(total.per_evaluation, root.per_evaluation), - per_output_value: add(total.per_output_value, root.per_output_value), - }; + *total = total.plus(readout_values_per_group[root.0 as usize]); } ReadoutsReadByQueries { composed, @@ -437,7 +538,10 @@ fn readouts_read_by_queries( /// value one input value. A binary operator, join or relational set /// operator: 2, one value of each side; a concatenation: each of its /// inputs. Operators its scalar expressions reference (`scalar(r)`) follow -/// the direct inputs and are read whole. +/// the direct inputs and are read whole. Both counts are per evaluation, +/// not per subquery step: `max_over_time(q[1h:1m])` reads each readout +/// value of `q` at 60 steps, which they do not multiply in (no cap passes +/// through a subquery to the readouts below it). fn inputs_read_value_by_value(operator: &Operator) -> Option { match operator { Operator::NonASAP( @@ -460,17 +564,19 @@ fn inputs_read_value_by_value(operator: &Operator) -> Option /// Whether `operator` copies each output value from exactly one of its /// inputs: a PromQL set operator (`q or r` takes a series' value from `q` /// when `q` has the series, else from `r`; `and` and `unless` only from -/// the left). +/// the left) or a concatenation (each row from one of its inputs). fn takes_each_value_from_one_input(operator: &Operator) -> bool { matches!( operator, - Operator::NonASAP(NonASAPOp::BinaryOp { - operator: BinaryOperator { - kind: BinaryOpKind::Set(_), + Operator::NonASAP( + NonASAPOp::BinaryOp { + operator: BinaryOperator { + kind: BinaryOpKind::Set(_), + .. + }, .. - }, - .. - }) + } | NonASAPOp::Concat { .. } + ) ) } diff --git a/crates/accuracy/src/readout_caps/tests.rs b/crates/accuracy/src/readout_caps/tests.rs index 3b47319d1..478cdcab3 100644 --- a/crates/accuracy/src/readout_caps/tests.rs +++ b/crates/accuracy/src/readout_caps/tests.rs @@ -1,4 +1,5 @@ use super::*; +use crate::NoMeasuredGuarantees; use asap_types::accuracy::AccuracyScope; use asap_types::ir::operator::arguments::{PromQLVectorSetOpKind, Reduction}; use asap_types::ir::operator::AggIntent; @@ -7,8 +8,8 @@ use asap_types::ir::QueryRoot; use asap_types::stages::{MemoQuery, ValueSign}; use asap_types::summary_type::{SketchAlgorithm, SketchStatistic}; use asap_types::test_support::memo_builders::{ - candidate, estimate, group, own_candidate, quantile_over_time, query, scan_group, - sketch_state, with_series, + candidate, estimate, group, own_candidate, quantile_over_time, query, scan_group, sketch_state, + with_series, }; use asap_types::test_support::memo_fixtures::{aggregate, arithmetic, range, set_operator}; use asap_types::workload::DataWorkload; @@ -16,10 +17,15 @@ use asap_types::workload::DataWorkload; /// The levels [`readout_levels`] gives the readout candidate `readout`. fn levels_of(memo: &Memo, readout: MemoGroupId) -> Vec { let order = memo.topological_order().expect("acyclic"); - readout_levels(memo, &ErrorMetricsOfMemo::of(memo, &order), &order) - .remove(&own_candidate(readout)) - .expect("a sketch readout") - .levels + readout_levels( + memo, + &ErrorMetricsOfMemo::of(memo, &order), + &order, + &NoMeasuredGuarantees, + ) + .remove(&own_candidate(readout)) + .expect("a sketch readout") + .levels } /// A whole-result failure probability is split over the values one @@ -318,9 +324,14 @@ fn an_operator_above_a_count_of_readouts_combines_one_readout_error() { /// failure probability. fn failure_probability_of(memo: &Memo, readout: MemoGroupId) -> Option { let order = memo.topological_order().expect("acyclic"); - let required = readout_levels(memo, &ErrorMetricsOfMemo::of(memo, &order), &order) - .remove(&own_candidate(readout)) - .expect("a sketch readout"); + let required = readout_levels( + memo, + &ErrorMetricsOfMemo::of(memo, &order), + &order, + &NoMeasuredGuarantees, + ) + .remove(&own_candidate(readout)) + .expect("a sketch readout"); if !required .queries_without_per_value_failure_probability .is_empty() @@ -585,26 +596,48 @@ fn a_record_share_scope_splits_over_the_readout_values_of_the_groups_it_covers() /// depends on one readout value of each side, fan-in 2, so the two /// readouts' failure probabilities sum within δ = 0.01: divisor /// min(16, 1 × 2) = 2, and the loosest grid level is 1e-3 (≤ 0.005), -/// not 1e-4 (≤ 0.01 / 16). +/// not 1e-4 (≤ 0.01 / 16). Checked on the counts and the query's grid, +/// since DDSketch's error does not depend on δ, so the readout keeps only +/// the strictest δ of each error level. #[test] fn a_per_value_ratio_splits_over_the_two_readout_values_of_each_output_value() { let mut memo = Memo::new(&with_series("m", 8)); let (_, state, p90) = quantile_over_time(&mut memo, SketchAlgorithm::DDSketch, 0.9, 300); let p50 = estimate(&mut memo, state, SketchStatistic::Quantile { q: 0.5 }); let ratio = group(&mut memo, arithmetic(ArithmeticOpKind::Div, p90, p50)); - with_query( - &mut memo, - ratio, - relative_target(AccuracyScope::PerValue), + let target = relative_target(AccuracyScope::PerValue); + with_query(&mut memo, ratio, target.clone(), Some(8)); + let order = memo.topological_order().expect("acyclic"); + let error_metrics = ErrorMetricsOfMemo::of(&memo, &order); + let read = readouts_read_by_queries( + &memo, + &error_metrics.error_contract, + &order, + &[p90, p50].into(), + ); + assert!(read.composed.contains(&0)); + let counts = read.readout_values[&0]; + assert_eq!( + counts, + ReadoutValueCounts { + per_evaluation: Some(16), + per_output_value: Some(2), + } + ); + let levels = query_levels( + &target, Some(8), - ); - for readout in [p90, p50] { - let loosest = levels_of(&memo, readout) - .into_iter() - .map(|required| required.failure_probability) - .fold(0.0, f64::max); - assert!((loosest - 1e-3).abs() < 1e-15, "{loosest}"); - } + counts, + AccuracyMetric::Relative, + 0.05, + true, + ) + .expect("a per-value failure probability"); + let loosest = levels + .into_iter() + .map(|required| required.failure_probability) + .fold(0.0, f64::max); + assert!((loosest - 1e-3).abs() < 1e-15, "{loosest}"); } /// A readout group whose candidates are a readout and a reduction over @@ -625,7 +658,11 @@ fn a_readout_group_holding_a_reduction_splits_over_the_readout_values_below_it() SketchAlgorithm::DDSketch, Reduction::by(vec![]), ); - let global = estimate(&mut memo, global_state, SketchStatistic::Quantile { q: 0.9 }); + let global = estimate( + &mut memo, + global_state, + SketchStatistic::Quantile { q: 0.9 }, + ); let sum_of_readouts = Operator::NonASAP(NonASAPOp::Aggregate { child: per_series, reduction: Reduction::by(vec![]), @@ -739,7 +776,12 @@ fn levels_met_by_different_builds_are_both_kept() { .accuracy_scope(scope) }; with_query(&mut memo, readout, rank(AccuracyScope::PerValue), Some(8)); - with_query(&mut memo, readout, rank(AccuracyScope::WholeResult), Some(8)); + with_query( + &mut memo, + readout, + rank(AccuracyScope::WholeResult), + Some(8), + ); let failure_probabilities: Vec = levels_of(&memo, readout) .into_iter() .map(|level| level.failure_probability) diff --git a/crates/stage1-pass2-reuse-candidates/src/lib.rs b/crates/stage1-pass2-reuse-candidates/src/lib.rs index 6e1703380..618059393 100644 --- a/crates/stage1-pass2-reuse-candidates/src/lib.rs +++ b/crates/stage1-pass2-reuse-candidates/src/lib.rs @@ -86,7 +86,7 @@ pub fn generate_logical_asap_memo_candidates( // Readout copies add candidates, not groups: one order serves steps 3 // and 4. let order = topological_order(&memo)?; - let readout_requirements = set_readout_requirements(&mut memo, &order)?; + let readout_requirements = set_readout_requirements(&mut memo, &order, evidence)?; let candidates_of = |pruned: &[PrunedLogicalCandidate]| -> BTreeSet { pruned.iter().map(|pruned| pruned.candidate).collect() }; diff --git a/crates/stage1-pass2-reuse-candidates/src/readout_requirements.rs b/crates/stage1-pass2-reuse-candidates/src/readout_requirements.rs index 83ece4d84..256ffbb5e 100644 --- a/crates/stage1-pass2-reuse-candidates/src/readout_requirements.rs +++ b/crates/stage1-pass2-reuse-candidates/src/readout_requirements.rs @@ -6,7 +6,7 @@ use std::collections::{BTreeMap, BTreeSet}; use crate::Pass2RuleError; -use asap_accuracy::{readout_levels, ErrorMetricsOfMemo}; +use asap_accuracy::{readout_levels, AccuracyEvidence, ErrorMetricsOfMemo}; use asap_types::stages::{ CandidateForMemoGroupId, LogicalRequirements, Memo, MemoGroupId, RequiredAccuracy, }; @@ -29,17 +29,20 @@ pub(crate) struct ReadoutRequirementsOutcome { /// required accuracy, one readout candidate per requirement level /// ([`readout_levels`]): Pass 1's candidate takes the first level and a /// copy is added per further level. A readout without a level keeps no -/// requirement. `order` is the memo's topological order. +/// requirement. `order` is the memo's topological order; `evidence` the +/// deployment's measured accuracy, which decides the builds that meet a +/// level. pub(crate) fn set_readout_requirements( memo: &mut Memo, order: &[MemoGroupId], + evidence: &dyn AccuracyEvidence, ) -> Result { let mut outcome = ReadoutRequirementsOutcome { readouts_without_per_value_failure_probability: BTreeMap::new(), exact_only_readout_groups_of_query: BTreeMap::new(), }; let error_metrics = ErrorMetricsOfMemo::of(memo, order); - for (readout, required) in readout_levels(memo, &error_metrics, order) { + for (readout, required) in readout_levels(memo, &error_metrics, order, evidence) { for &query in &required.queries_without_per_value_failure_probability { outcome .exact_only_readout_groups_of_query diff --git a/crates/types/src/accuracy/mod.rs b/crates/types/src/accuracy/mod.rs index f97dc28e9..67546063e 100644 --- a/crates/types/src/accuracy/mod.rs +++ b/crates/types/src/accuracy/mod.rs @@ -11,5 +11,5 @@ mod target; pub use default_metric::default_metric_for_root; pub use guarantee::{AccuracyGuarantee, BoundExpr, ProbabilityExpr}; pub use target::{ - AccuracyMetric, AccuracyScope, AccuracySpec, AccuracyTarget, NormScale, ReadoutValuesRead, + AccuracyMetric, AccuracyScope, AccuracySpec, AccuracyTarget, NormScale, ReadoutValueCounts, }; diff --git a/crates/types/src/accuracy/target/mod.rs b/crates/types/src/accuracy/target/mod.rs index d3a46d24b..42efa60f2 100644 --- a/crates/types/src/accuracy/target/mod.rs +++ b/crates/types/src/accuracy/target/mod.rs @@ -353,7 +353,7 @@ impl AccuracyTarget { pub fn failure_probability_per_readout_value( &self, values_per_evaluation: Option, - readout_values: ReadoutValuesRead, + readout_values: ReadoutValueCounts, ) -> Option { let Some(delta) = self.failure_probability else { return Some(1.0); @@ -381,12 +381,36 @@ impl AccuracyTarget { /// may depend on all of them (a job's series are not bounded), so both are /// 1000; without the `sum`, 1000 and 1. #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct ReadoutValuesRead { +pub struct ReadoutValueCounts { /// `R`: every readout value one evaluation reads. pub per_evaluation: Option, /// The fan-in: the most readout values one output value depends on. pub per_output_value: Option, } +impl ReadoutValueCounts { + /// The larger of `self` and `other`, count by count, as among + /// alternative candidates of one group: (1000, 1) and (8, 8) give + /// (1000, 8). Unknown when either is. + pub fn larger(self, other: Self) -> Self { + let larger = |a: Option, b: Option| a.zip(b).map(|(a, b)| a.max(b)); + Self { + per_evaluation: larger(self.per_evaluation, other.per_evaluation), + per_output_value: larger(self.per_output_value, other.per_output_value), + } + } + + /// `self` and `other` added count by count, as the roots of one query + /// read together: (1000, 1) and (8, 8) give (1008, 9). Unknown when + /// either is or the sum overflows. + pub fn plus(self, other: Self) -> Self { + let plus = |a: Option, b: Option| a.zip(b).and_then(|(a, b)| a.checked_add(b)); + Self { + per_evaluation: plus(self.per_evaluation, other.per_evaluation), + per_output_value: plus(self.per_output_value, other.per_output_value), + } + } +} + #[cfg(test)] mod tests; diff --git a/crates/types/src/accuracy/target/tests.rs b/crates/types/src/accuracy/target/tests.rs index 5bfd5b255..56b6af163 100644 --- a/crates/types/src/accuracy/target/tests.rs +++ b/crates/types/src/accuracy/target/tests.rs @@ -359,7 +359,7 @@ fn a_whole_result_failure_probability_splits_over_the_output_values() { /// drops out of the `min`. #[test] fn failure_probability_splits_over_the_readout_values_covered() { - let read = |per_evaluation, per_output_value| ReadoutValuesRead { + let read = |per_evaluation, per_output_value| ReadoutValueCounts { per_evaluation, per_output_value, }; @@ -402,3 +402,23 @@ fn failure_probability_splits_over_the_readout_values_covered() { Some(1.0) ); } + +/// `larger` and `plus` combine counts field by field; an unknown count +/// stays unknown. +#[test] +fn readout_value_counts_combine_count_by_count() { + let counts = |per_evaluation, per_output_value| ReadoutValueCounts { + per_evaluation, + per_output_value, + }; + let (a, b) = (counts(Some(1000), Some(1)), counts(Some(8), Some(8))); + assert_eq!(a.larger(b), counts(Some(1000), Some(8))); + assert_eq!(a.plus(b), counts(Some(1008), Some(9))); + let unknown_fan_in = counts(Some(8), None); + assert_eq!(a.larger(unknown_fan_in), counts(Some(1000), None)); + assert_eq!(a.plus(unknown_fan_in), counts(Some(1008), None)); + assert_eq!( + counts(Some(u64::MAX), Some(1)).plus(a), + counts(None, Some(2)) + ); +} diff --git a/docs/design_docs/concepts/accuracy-models.md b/docs/design_docs/concepts/accuracy-models.md index 92a4aa653..c8a3993b0 100644 --- a/docs/design_docs/concepts/accuracy-models.md +++ b/docs/design_docs/concepts/accuracy-models.md @@ -77,33 +77,42 @@ unknown (HLL without a distinct bound) serves no query with an accuracy target (`RootPruneReason::NoGuarantee`), and a top-k readout is admitted on its heap size and its value guarantee alone (the contracts table). -Pass 2 gives each sketch readout the error and per-value failure probability -`δ / min(R, V · fan_in)` it must meet (`AccuracyTarget::failure_probability_per_readout_value`): -the statement fails only when a readout value one of its `V` covered output -values depends on does. `R` is the readout values the query reads per -evaluation (the values per evaluation of every readout group below it, -summed: the series their scans read), and `fan_in` the most readout values -one output value depends on: 1 when no reduction lies above the readout, -the reduction's input values under one (all of `R` for a global `max`; for +Pass 2 gives each sketch readout the error and per-value failure +probability `δ / min(R, V · fan_in)` it must meet +(`AccuracyTarget::failure_probability_per_readout_value`): the statement +fails only when a readout value one of its `V` covered output values +depends on does. `R` is the readout values the query reads per evaluation +(the values per evaluation of every readout group below it, summed: the +series their scans read), and `fan_in` the most readout values one output +value depends on: 1 when no reduction lies above the readout, the +reduction's input values under one (all of `R` for a global `max`; for `sum by (job)` too, since the declared facts bound how many jobs there are, not how many series one job holds), the sum over the sides of a binary -operator, the larger side of `or`. An unknown term drops out of the `min`; -with both unknown no per-value failure probability follows. At δ = 0.01, -`quantile_over_time(0.99, m[5m])` over 8 series per value keeps 0.01 -(min(8, 1 · 1)), and over its whole result gets 0.01 / 8; a `max` over 1000 -readouts returns one value yet fails when any readout does, so it gets -0.01 / 1000 in any scope; and `max by (job) (q or r)` splits over the -readouts of both sides. Pass 1's candidate takes the first requirement level and Pass 2 adds a copy -per further level. A readout several queries reach gets the union of their -levels. Pass 2 sizes nothing: a state has one build per listed θ, and each -edge checks the guarantee of its producer's θ at the required failure -probability, so a build several readers share serves the readers whose -levels its θ meets. Stage 3 only checks the guarantee; it never resizes. At equal -δ, the scope covering the most values is the strictest: every group (`G`, -when `G` is above 100), then a 1% share (100), then a 5% share (20), then a -single value (1). A whole-result target with an unbound `G` splits -over `R` alone, and stays unmet when `R` is unbound too. A grouped result's `G` is bound by the -distinct values its sources declare for its group keys +operator, the larger side of `or` or input of a concatenation. A top-k +readout chooses its output keys by their estimates, so each of its output +values depends on every value it ranks. A readout group whose candidates +include a reduction over readouts below it takes the larger of the two +counts. Both counts are per evaluation, not per subquery step. An unknown +term drops out of the `min`; with both unknown no per-value failure +probability follows. At δ = 0.01, `quantile_over_time(0.99, m[5m])` over 8 +series per value keeps 0.01 (min(8, 1 · 1)), and over its whole result +gets 0.01 / 8; a `max` over 1000 readouts returns one value yet fails when +any readout does, so it gets 0.01 / 1000 in any scope; and +`max by (job) (q or r)` splits over the readouts of both sides. Pass 1's +candidate takes the first requirement level and Pass 2 adds a copy per +further level. A readout several queries reach gets the union of their +levels, less each level the listed builds that meet it are exactly those +that meet a stricter one: DDSketch's relative error does not depend on δ, +so of (5%, 0.01) and (5%, 1e-4) only the second is kept. Pass 2 sizes +nothing: a state has one build per listed θ, and each edge checks the +guarantee of its producer's θ at the required failure probability, so a +build several readers share serves the readers whose levels its θ meets. +Stage 3 only checks the guarantee; it never resizes. At equal δ, the scope +covering the most values is the strictest: every group (`G`, when `G` is +above 100), then a 1% share (100), then a 5% share (20), then a single +value (1). A whole-result target with an unbound `G` splits over `R` alone, +and stays unmet when `R` is unbound too. A grouped result's `G` is bound by +the distinct values its sources declare for its group keys (`SourceFacts.distinct_values_per_label`, and `distinct_values_per_label_set` for labels that depend on each other): the smallest product of declared label and label-set counts covering the keys, at @@ -326,7 +335,9 @@ largest error the readout's output may have for that query. ten from 1e-2 down to 1e-5, continued down to the first one at or below a tenth of the query's per-value failure probability) not looser than the cap. The logical-edge check then keeps the levels a producer's θ meets. -- A readout reached by several queries gets the union of their levels. +- A readout reached by several queries gets the union of their levels, + less each level the listed builds that meet it are exactly those that + meet a stricter one (a copy at it would accept the same producers). - A count reads no value (`β = 0`), and neither does the right input of `and` and `unless`: the readouts below it get no level through it, and an operator above it combines no error of them (`q / count(r)` reads From 441b156ee7ca2b0389406be086234c63dd51999f Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 03:39:52 +0000 Subject: [PATCH 05/24] test(accuracy): a readout grouped by a time bucket has no bound from the declared groups of rows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A whole-result quantile grouped by date_trunc('minute', ts) over a table declaring 6 groups of rows returns about 16 values per evaluation, so its readout must not split δ over 6. Grouped by src alone, the split over the 6 groups of rows still holds. --- crates/integration-tests/tests/pass2_sql.rs | 105 +++++++++++++++++- .../tests/planning_common/mod.rs | 7 +- crates/types/src/stages/logical/memo/tests.rs | 79 +++++++++++++ 3 files changed, 188 insertions(+), 3 deletions(-) diff --git a/crates/integration-tests/tests/pass2_sql.rs b/crates/integration-tests/tests/pass2_sql.rs index 8b3f226fa..37d7d084d 100644 --- a/crates/integration-tests/tests/pass2_sql.rs +++ b/crates/integration-tests/tests/pass2_sql.rs @@ -1,8 +1,8 @@ //! SQL → Pass 2's readout requirements, through production lowering. -use crate::planning_common::sql_pass2; +use crate::planning_common::{sql_pass2, sql_pass2_over}; use asap_types::ir::schema::FieldDataType; use asap_types::ir::{ASAPOp, Operator}; -use asap_types::stages::{CandidateForMemoGroupId, MemoGroupId}; +use asap_types::stages::{CandidateForMemoGroupId, LogicalASAPMemoCandidates, MemoGroupId}; use asap_types::summary_type::{SketchAlgorithm, SketchParams}; use serde_json::json; @@ -59,3 +59,104 @@ fn a_global_distinct_count_keeps_its_whole_failure_probability() { assert!(kmv_readouts > 0); assert!(live_kmv_builds.contains(&4323), "{live_kmv_builds:?}"); } + +/// Each KLL readout candidate of `logical`: whether Pass 2 keeps it, and +/// the failure probability its state must meet, if it is given one. +fn kll_readouts(logical: &LogicalASAPMemoCandidates) -> Vec<(bool, Option)> { + let memo = &logical.memo; + let mut readouts = Vec::new(); + for (group, memo_group) in memo.groups().iter().enumerate() { + for (index_in_group, candidate) in memo_group.candidates.iter().enumerate() { + let Operator::ASAP(ASAPOp::SummaryEstimate { summary_input, .. }) = + &candidate.expression.operator + else { + continue; + }; + if memo.groups()[summary_input.0 as usize] + .group_properties + .sketch_algorithm_of_state() + != Some(SketchAlgorithm::Kll) + { + continue; + } + let id = CandidateForMemoGroupId { + group: MemoGroupId(group as u32), + index_in_group, + }; + let live = logical.pruned.iter().all(|pruned| pruned.candidate != id); + let failure_probability = candidate.input_requirements[0] + .required_accuracy + .map(|required| required.failure_probability); + readouts.push((live, failure_probability)); + } + } + assert!(!readouts.is_empty(), "a KLL readout"); + readouts +} + +/// A 0.9-quantile of `bytes` at rank error 0.01 and δ = 0.01 over the +/// whole result, as `SELECT {keys}, approx_percentile_cont(bytes, 0.9)` +/// over the last 15 minutes grouped by `keys`, where `flows` declares 6 +/// groups of rows, and `src_values` distinct `src` values when given. +fn whole_result_quantile_grouped_by( + keys: &str, + src_values: Option, +) -> LogicalASAPMemoCandidates { + let query = format!( + "SELECT {keys}, approx_percentile_cont(bytes, 0.9) AS q FROM flows \ + WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY {keys}" + ); + sql_pass2_over( + &[( + &query, + json!({"max_error": 0.01, "failure_probability": 0.01, "accuracy_metric": "rank"}), + )], + json!({"flows": { + "series_count": 6, + "distinct_values_per_label": src_values.map_or(json!({}), |src| json!({"src": src})), + }}), + ) +} + +/// `GROUP BY date_trunc('minute', ts)` returns one quantile per minute +/// bucket: about 16 over the last 15 minutes, not at most the 6 groups of +/// rows `flows` declares. The readout values per evaluation are unknown +/// (the bucket is a computed key), and so is the query's value count, so +/// no per-value failure probability follows and the readout is read +/// exactly, rather than at δ / 6 (KLL k = 547, whose guarantee at δ / 16 +/// is above the target). +#[test] +fn a_whole_result_grouped_by_a_time_bucket_gives_its_readout_no_level() { + let logical = whole_result_quantile_grouped_by("date_trunc('minute', ts)", Some(6)); + for (live, failure_probability) in kll_readouts(&logical) { + assert_eq!((live, failure_probability), (false, None)); + } +} + +/// Grouped by `src` and the minute bucket, the result holds up to 6 × 16 +/// values; the bucket key still leaves them unbounded, so no level. +#[test] +fn a_whole_result_grouped_by_a_source_column_and_a_time_bucket_gives_its_readout_no_level() { + let logical = whole_result_quantile_grouped_by("src, date_trunc('minute', ts)", Some(6)); + for (live, failure_probability) in kll_readouts(&logical) { + assert_eq!((live, failure_probability), (false, None)); + } +} + +/// Grouped by the source column `src` alone, the result holds at most the +/// 6 groups of rows `flows` declares. With no `src` count declared, the +/// query's value count is unknown, but the readout's δ still splits over +/// R = 6: 0.01 / 6. +#[test] +fn a_whole_result_grouped_by_a_source_column_splits_over_the_groups_of_rows() { + let logical = whole_result_quantile_grouped_by("src", None); + let readouts = kll_readouts(&logical); + assert!(readouts.iter().any(|(live, _)| *live), "{readouts:?}"); + for (_, failure_probability) in readouts { + let failure_probability = failure_probability.expect("a level"); + assert!( + (failure_probability - 0.01 / 6.0).abs() < 1e-12, + "{failure_probability}" + ); + } +} diff --git a/crates/integration-tests/tests/planning_common/mod.rs b/crates/integration-tests/tests/planning_common/mod.rs index e962f2d52..6b553c06e 100644 --- a/crates/integration-tests/tests/planning_common/mod.rs +++ b/crates/integration-tests/tests/planning_common/mod.rs @@ -106,6 +106,11 @@ pub fn promql_pass2(queries: &[(&str, Value)]) -> LogicalASAPMemoCandidates { /// `flows(ts, src, dst, bytes)` of 1000 groups of rows, each with its /// accuracy target, repeating every minute. pub fn sql_pass2(queries: &[(&str, Value)]) -> LogicalASAPMemoCandidates { + sql_pass2_over(queries, json!({"flows": {"series_count": 1000}})) +} + +/// [`sql_pass2`] with the facts `sources` declares for `flows`. +pub fn sql_pass2_over(queries: &[(&str, Value)], sources: Value) -> LogicalASAPMemoCandidates { let flows = json!({"name": "flows", "time_index": 0, "columns": [ {"name": "ts", "type": "timestamp"}, {"name": "src", "type": "utf8"}, @@ -115,7 +120,7 @@ pub fn sql_pass2(queries: &[(&str, Value)]) -> LogicalASAPMemoCandidates { pass2_of( json!({"sql": "datafusion_sql"}), queries, - json!({"flows": {"series_count": 1000}}), + sources, Some(&[flows.to_string()]), ) } diff --git a/crates/types/src/stages/logical/memo/tests.rs b/crates/types/src/stages/logical/memo/tests.rs index 0a40e109f..f3d0173af 100644 --- a/crates/types/src/stages/logical/memo/tests.rs +++ b/crates/types/src/stages/logical/memo/tests.rs @@ -499,3 +499,82 @@ fn max_distinct_per_evaluation_sums_the_scanned_sources_bounds() { .unwrap(); assert_eq!(memo.max_distinct_per_evaluation(both), Some(10_000)); } + +/// A readout of a state grouped by a computed key or a time has no bound +/// from the facts: over `latency` with 1000 series and 20 declared jobs, +/// grouped by `date_trunc('minute', ts)` it returns one value per minute +/// bucket of the range it reads, however many series there are; by `job` +/// and the bucket, up to 20 per bucket; by the time `ts` itself, one per +/// timestamp. Grouped by `job` through the same projection, still 20. +#[test] +fn max_values_per_evaluation_is_unbounded_for_a_computed_or_time_key() { + use crate::ir::scalar::ScalarValue; + use crate::ir::{ProjectItem, ScalarExpr}; + let data = crate::workload::DataWorkload { + sources: [( + "latency".to_string(), + crate::workload::SourceFacts { + series_count: Some(1000), + distinct_values_per_label: [("job".to_string(), 20)].into(), + ..Default::default() + }, + )] + .into(), + ..Default::default() + }; + let mut memo = Memo::new(&data); + let input = memo + .find_or_add_group(scan( + "latency", + &[("job", crate::ir::schema::DataType::Utf8)], + vec![], + )) + .unwrap(); + // `[ts, value, job, date_trunc('minute', ts)]`. + let column = |index| ProjectItem { + alias: None, + expr: ScalarExpr::Column(index), + }; + let bucketed = memo + .find_or_add_group(Operator::NonASAP(NonASAPOp::Project { + cols: vec![ + column(0), + column(1), + column(2), + ProjectItem { + alias: Some("minute".into()), + expr: ScalarExpr::FunctionCall { + name: "date_trunc".into(), + args: vec![ + ScalarExpr::Literal(ScalarValue::Utf8("minute".into())), + ScalarExpr::Column(0), + ], + }, + }, + ], + child: input, + })) + .unwrap(); + for (keys, values) in [ + (vec![3], None), + (vec![2, 3], None), + (vec![0], None), + (vec![2], Some(20)), + ] { + let state = memo + .find_or_add_group(state_grouped_by( + FieldDataType::Sketch(SketchAlgorithm::Kll), + Reduction::by(keys.clone()), + bucketed, + )) + .unwrap(); + let quantile = memo + .find_or_add_group(readout(SketchStatistic::Quantile { q: 0.9 }, state)) + .unwrap(); + assert_eq!( + memo.max_values_per_evaluation(quantile), + values, + "by {keys:?}" + ); + } +} From 65398824e9fbecffcc026c7548b4b09b705c929f Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 03:41:39 +0000 Subject: [PATCH 06/24] fix(accuracy): bound a readout's values by the groups of rows only when it is keyed by source labels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Memo::max_values_per_evaluation bounded every readout by the series (groups of rows) scanned below it. A readout grouped by a computed key or a time, such as date_trunc('minute', ts), returns one value per bucket, which that count does not bound: a whole-result target with an unknown value count split δ over 6 for about 16 values. The bound now applies only when every key of the readout's state (or aggregate) is a source label or column copied unchanged and not a time; otherwise R is unknown and, with n_q unknown too, no per-value failure probability follows, as on the base. --- crates/types/src/stages/logical/memo/mod.rs | 167 +++++++++++++------ docs/design_docs/concepts/accuracy-models.md | 13 +- 2 files changed, 129 insertions(+), 51 deletions(-) diff --git a/crates/types/src/stages/logical/memo/mod.rs b/crates/types/src/stages/logical/memo/mod.rs index 3637934d5..012461573 100644 --- a/crates/types/src/stages/logical/memo/mod.rs +++ b/crates/types/src/stages/logical/memo/mod.rs @@ -14,9 +14,10 @@ use super::group::{ use crate::accuracy::AccuracyTarget; use crate::ir::operator::arguments::Reduction; use crate::ir::operator::input::InputOrTemporaryBuild; -use crate::ir::schema::Schema; +use crate::ir::operator::AggIntent; +use crate::ir::schema::{DataType, FieldDataType, Schema}; use crate::ir::QueryRoot; -use crate::ir::{ASAPOp, Operator}; +use crate::ir::{ASAPOp, NonASAPOp, Operator}; use crate::stages::{ CandidateError, CandidateForMemoGroup, CandidateForMemoGroupId, ColumnLineage, LogicalRequirements, MemoGroup, MemoGroupId, @@ -92,25 +93,33 @@ impl Memo { &self.groups } - /// The most values one evaluation of `group` returns, from the facts - /// the data workload declares; `None` when they bound it in no way. - /// The smaller of: + /// The most values one evaluation of `group`, a readout or a state, + /// returns, from the facts the data workload declares; `None` when they + /// bound it in no way. Only a result keyed by the sources' labels (or + /// SQL columns copied unchanged, not times) is bounded + /// ([`Self::keys_of_values`]), by the smaller of: /// - the series (PromQL) or groups of rows (SQL) of the sources scanned /// below it (`GroupProperties::scanned_sources`, each source's - /// `SourceFacts::series_count`, when every one declares it): every - /// value of a per-series or grouped result over them comes from at - /// least one of them; - /// - for a readout (`SummaryEstimate`, not a top-k) of a state, or a - /// state, grouped `by` keys, the groups those keys form: 1 for no - /// key, else the distinct combinations the source declares for keys - /// copied from one source (`SourceFacts::distinct_values_per_label` - /// and `distinct_values_per_label_set`). + /// `SourceFacts::series_count`, when every one declares it): each + /// value of a per-series result, or of a result grouped by their + /// labels, comes from at least one of them; + /// - for keys copied from one source, the distinct combinations it + /// declares for them (`SourceFacts::distinct_values_per_label` and + /// `distinct_values_per_label_set`); 1 for no key. + /// + /// A result grouped by a computed key or a time is not bounded: one + /// value per bucket of `GROUP BY date_trunc('minute', ts)` over the last + /// 15 minutes is about 16 values, however few groups of rows the table + /// holds. /// /// Examples: a readout of `quantile_over_time(0.9, m[5m])` over `m` /// with 1000 series returns at most 1000 values, which `max(...)` over /// it reads at once; `SELECT count(DISTINCT dst) FROM flows` reads one /// distinct count per evaluation. pub fn max_values_per_evaluation(&self, group: MemoGroupId) -> Option { + let KeysOfValues::Labels { declared_groups } = self.keys_of_values(group) else { + return None; + }; let series_scanned = self.groups[group.0 as usize] .group_properties .scanned_sources @@ -119,7 +128,7 @@ impl Memo { let (facts, _) = series_facts(&self.source_facts, source_name(source))?; total.checked_add(facts.series_count?) }); - match (series_scanned, self.declared_groups_of_state(group)) { + match (series_scanned, declared_groups) { (Some(series), Some(groups)) => Some(series.min(groups)), (series, groups) => series.or(groups), } @@ -145,50 +154,87 @@ impl Memo { ) } - /// The groups the `by` keys of the state `group` is, or reads, form - /// ([`Self::max_values_per_evaluation`]). - fn declared_groups_of_state(&self, group: MemoGroupId) -> Option { - match &self.groups[group.0 as usize].expression { + /// What the values of `group` are keyed by + /// ([`Self::max_values_per_evaluation`]): the keys of the state it is, + /// or reads, or of the aggregate it is. A top-k readout or aggregate + /// returns several rows per group, so its declared groups are unknown. + fn keys_of_values(&self, group: MemoGroupId) -> KeysOfValues { + let (reduction, child, one_row_per_group) = match &self.groups[group.0 as usize].expression + { Operator::ASAP(ASAPOp::SummaryEstimate { summary_input, query, - }) if !matches!(query, SketchStatistic::TopK { .. }) => { - self.declared_groups_of_state(*summary_input) + }) => { + let keys = self.keys_of_values(*summary_input); + return if matches!(query, SketchStatistic::TopK { .. }) { + keys.without_declared_groups() + } else { + keys + }; } Operator::ASAP( ASAPOp::SummaryAgg { - reduction: Reduction::Reduce(by), - child, - .. + reduction, child, .. } | ASAPOp::SummaryTumblingWindowChain { - reduction: Reduction::Reduce(by), - child, - .. + reduction, child, .. }, - ) if !by.is_without() => { - let lineage = &self.groups[child.0 as usize] - .group_properties - .column_lineage; - let mut source = None; - let mut labels = Vec::new(); - for key in by.keys() { - let Some(ColumnLineage::SourceColumn(column)) = lineage.get(*key) else { - return None; - }; - if source.is_some_and(|source| source != &column.source) { - return None; - } - source = Some(&column.source); - labels.push(column.column.as_str()); + ) => (reduction, *child, true), + Operator::NonASAP(NonASAPOp::Aggregate { + reduction, + measures, + child, + .. + }) => ( + reduction, + *child, + !measures + .iter() + .any(|m| matches!(m, AggIntent::TopK { .. } | AggIntent::CountValues { .. })), + ), + _ => return KeysOfValues::ComputedOrTime, + }; + let keys = match reduction { + Reduction::Reduce(by) if !by.is_without() => by.keys(), + // One value per series, or per group of every label but some. + _ => { + return KeysOfValues::Labels { + declared_groups: None, } - let Some(source) = source else { - return Some(1); - }; - let (facts, _) = series_facts(&self.source_facts, source_name(source))?; - facts.declared_distinct_combinations(&labels) } - _ => None, + }; + let input = &self.groups[child.0 as usize].group_properties; + let mut source = None; + let mut labels = Vec::new(); + let mut one_source = true; + for key in keys { + let Some(ColumnLineage::SourceColumn(column)) = input.column_lineage.get(*key) else { + return KeysOfValues::ComputedOrTime; + }; + let time_valued = input.schema.fields.get(*key).is_some_and(|field| { + matches!( + field.dtype, + FieldDataType::Plain(DataType::Timestamp | DataType::Date) + ) + }); + if time_valued { + return KeysOfValues::ComputedOrTime; + } + one_source &= source.is_none_or(|source| source == &column.source); + source = Some(&column.source); + labels.push(column.column.as_str()); + } + let declared_groups = match source { + None => Some(1), + Some(source) if one_source => series_facts(&self.source_facts, source_name(source)) + .and_then(|(facts, _)| facts.declared_distinct_combinations(&labels)), + Some(_) => None, + }; + let keys = KeysOfValues::Labels { declared_groups }; + if one_row_per_group { + keys + } else { + keys.without_declared_groups() } } @@ -551,5 +597,32 @@ fn add_parent(group: &mut MemoGroup, parent: MemoGroupId) { } } +/// What the values one evaluation of a readout or state returns are keyed +/// by ([`Memo::max_values_per_evaluation`]). +enum KeysOfValues { + /// Each value is one series, or one group of labels (SQL columns) + /// copied unchanged from the sources: at most the series (groups of + /// rows) scanned, and at most `declared_groups`, the combinations the + /// source declares for the keys, when known (`Some(20)` for `by (job)` + /// over 20 declared jobs, `Some(1)` for no key). + Labels { declared_groups: Option }, + /// A key is computed (`date_trunc('minute', ts)`, an expression) or a + /// time: one value per bucket or time, which no fact bounds. + ComputedOrTime, +} + +impl KeysOfValues { + /// These keys, with no declared groups: a top-k returns several rows + /// per group. + fn without_declared_groups(self) -> Self { + match self { + Self::Labels { .. } => Self::Labels { + declared_groups: None, + }, + Self::ComputedOrTime => Self::ComputedOrTime, + } + } +} + #[cfg(test)] mod tests; diff --git a/docs/design_docs/concepts/accuracy-models.md b/docs/design_docs/concepts/accuracy-models.md index c8a3993b0..c04dff61d 100644 --- a/docs/design_docs/concepts/accuracy-models.md +++ b/docs/design_docs/concepts/accuracy-models.md @@ -83,7 +83,7 @@ probability `δ / min(R, V · fan_in)` it must meet fails only when a readout value one of its `V` covered output values depends on does. `R` is the readout values the query reads per evaluation (the values per evaluation of every readout group below it, summed: the -series their scans read), and `fan_in` the most readout values one output +series their scans read, for readouts keyed by the sources' labels), and `fan_in` the most readout values one output value depends on: 1 when no reduction lies above the readout, the reduction's input values under one (all of `R` for a global `max`; for `sum by (job)` too, since the declared facts bound how many jobs there are, @@ -110,8 +110,12 @@ build several readers share serves the readers whose levels its θ meets. Stage 3 only checks the guarantee; it never resizes. At equal δ, the scope covering the most values is the strictest: every group (`G`, when `G` is above 100), then a 1% share (100), then a 5% share (20), then a single -value (1). A whole-result target with an unbound `G` splits over `R` alone, -and stays unmet when `R` is unbound too. A grouped result's `G` is bound by +value (1). A whole-result target with an unbound `G` splits over `R`, which +bounds every readout value read only when each readout is keyed by the +sources' labels or columns: grouped by a computed key or a time, one value +per bucket of `GROUP BY date_trunc('minute', ts)` is about 16 values over the +last 15 minutes however few groups of rows the table declares, so `R` is +unbound there. With both unbound the target stays unmet. A grouped result's `G` is bound by the distinct values its sources declare for its group keys (`SourceFacts.distinct_values_per_label`, and `distinct_values_per_label_set` for labels that depend on each other): the @@ -320,7 +324,8 @@ largest error the readout's output may have for that query. values the scope covers depend on (`R` summed over every readout group below it, `Memo::max_values_per_evaluation` each: at most the series scanned below it, the declared distinct values of its state's keys, one - without keys), since a reduction such as `max` fails when any readout it + without keys, when the keys are the sources' labels or columns; unbound + for a computed key or a time), since a reduction such as `max` fails when any readout it reads does. `SELECT count(DISTINCT dst) FROM flows` at δ = 0.01 reads one distinct count, so its readout keeps 0.01. `max by (job) (q or r)` with 3 series per side reads 6 readout values, and one job's value may From 3fdb75a1b53cb57ba560fd9e2ed9bf8cb0d992f3 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 04:15:47 +0000 Subject: [PATCH 07/24] test(accuracy): a readout's values multiply over joined scans and are clamped to the rows read MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - `flows JOIN hosts ... GROUP BY f.src, h.region` splits δ over 6 × 6 = 36 readout values, or over the rows the join reads when fewer. - `GROUP BY bytes`, a measure key with no declared count, splits over the rows read (96 for 6 groups of rows sampled every minute over 15 minutes), and gets no level when they are unknown. - A declared SQL key uses its declared distinct values. - A self-join's readout keyed by both sides' `job` returns up to 20 × 20. --- crates/integration-tests/tests/pass2_sql.rs | 113 +++++++++++++++--- .../tests/planning_common/mod.rs | 10 +- crates/types/src/stages/logical/memo/tests.rs | 70 +++++++++++ 3 files changed, 173 insertions(+), 20 deletions(-) diff --git a/crates/integration-tests/tests/pass2_sql.rs b/crates/integration-tests/tests/pass2_sql.rs index 37d7d084d..84207086a 100644 --- a/crates/integration-tests/tests/pass2_sql.rs +++ b/crates/integration-tests/tests/pass2_sql.rs @@ -106,11 +106,8 @@ fn whole_result_quantile_grouped_by( "SELECT {keys}, approx_percentile_cont(bytes, 0.9) AS q FROM flows \ WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY {keys}" ); - sql_pass2_over( - &[( - &query, - json!({"max_error": 0.01, "failure_probability": 0.01, "accuracy_metric": "rank"}), - )], + whole_result_quantile( + &query, json!({"flows": { "series_count": 6, "distinct_values_per_label": src_values.map_or(json!({}), |src| json!({"src": src})), @@ -118,6 +115,18 @@ fn whole_result_quantile_grouped_by( ) } +/// `query`, a 0.9-quantile at rank error 0.01 and δ = 0.01 over the whole +/// result, over the facts `sources` declares. +fn whole_result_quantile(query: &str, sources: serde_json::Value) -> LogicalASAPMemoCandidates { + sql_pass2_over( + &[( + query, + json!({"max_error": 0.01, "failure_probability": 0.01, "accuracy_metric": "rank"}), + )], + sources, + ) +} + /// `GROUP BY date_trunc('minute', ts)` returns one quantile per minute /// bucket: about 16 over the last 15 minutes, not at most the 6 groups of /// rows `flows` declares. The readout values per evaluation are unknown @@ -143,20 +152,88 @@ fn a_whole_result_grouped_by_a_source_column_and_a_time_bucket_gives_its_readout } } -/// Grouped by the source column `src` alone, the result holds at most the -/// 6 groups of rows `flows` declares. With no `src` count declared, the -/// query's value count is unknown, but the readout's δ still splits over -/// R = 6: 0.01 / 6. +/// Grouped by the source column `src`, with 4 distinct `src` values +/// declared, the result holds at most 4 values: the readout's δ splits +/// over them, 0.01 / 4. #[test] -fn a_whole_result_grouped_by_a_source_column_splits_over_the_groups_of_rows() { - let logical = whole_result_quantile_grouped_by("src", None); - let readouts = kll_readouts(&logical); +fn a_whole_result_grouped_by_a_declared_source_column_splits_over_its_distinct_values() { + let logical = whole_result_quantile_grouped_by("src", Some(4)); + assert_levels(&logical, &[0.01 / 4.0]); +} + +/// `GROUP BY bytes` keys each quantile by a measure value: one group per +/// distinct `bytes`, and no count of distinct `bytes` is declared. The 6 +/// groups of rows `flows` declares do not bound it (nothing says which +/// columns form them); the rows the query reads do, as there are never +/// more groups than rows. With no sampling interval declared, the rows +/// are unknown too, so the readout gets no per-value failure probability, +/// rather than δ / 6. Sampled every minute, the last 15 minutes hold at +/// most 6 × 16 = 96 rows: δ splits over 96. +#[test] +fn a_whole_result_grouped_by_an_undeclared_measure_column_splits_over_the_rows_read() { + let by_bytes = |sample_interval: Option| { + whole_result_quantile( + "SELECT bytes, approx_percentile_cont(bytes, 0.9) AS q FROM flows \ + WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY bytes", + json!({"flows": { + "series_count": 6, + "sample_interval": sample_interval, + "distinct_values_per_label": {"src": 6}, + }}), + ) + }; + for (live, failure_probability) in kll_readouts(&by_bytes(None)) { + assert_eq!((live, failure_probability), (false, None)); + } + assert_levels(&by_bytes(Some(60_000)), &[0.01 / 96.0]); +} + +/// `flows JOIN hosts` grouped by `f.src` and `h.region`: with 6 groups of +/// rows and 6 `src` values in `flows`, and 6 groups of rows, 6 hosts and +/// 6 regions in `hosts`, the result can hold 6 × 6 = 36 values (6 flows +/// to one host, and 6 host rows, one per region, for that host), not +/// 6 + 6 = 12: the readout's δ splits over 36. When the join reads fewer rows, they bound +/// the values instead: with `flows` holding 6 rows and `hosts` 5, the join +/// reads at most 30, and δ splits over 30. +#[test] +fn a_whole_result_over_a_join_splits_over_the_product_of_its_sides_values() { + let join = |flows_rows: Option, hosts_rows: Option| { + whole_result_quantile( + "SELECT f.src, h.region, approx_percentile_cont(f.bytes, 0.9) AS q \ + FROM flows f JOIN hosts h ON f.dst = h.host \ + WHERE f.ts >= now() - INTERVAL '15 minutes' GROUP BY f.src, h.region", + json!({ + "flows": { + "series_count": 6, + "rows_held": flows_rows, + "distinct_values_per_label": {"src": 6, "dst": 300}, + }, + "hosts": { + "series_count": 6, + "rows_held": hosts_rows, + "distinct_values_per_label": {"host": 6, "region": 6}, + }, + }), + ) + }; + assert_levels(&join(None, None), &[0.01 / 36.0]); + assert_levels(&join(Some(6), Some(5)), &[0.01 / 30.0]); +} + +/// Asserts that the KLL readout candidates of `logical` keep at least +/// one, and that the failure probabilities they must meet are `expected` +/// (distinct, ascending), each candidate having one. +fn assert_levels(logical: &LogicalASAPMemoCandidates, expected: &[f64]) { + let readouts = kll_readouts(logical); assert!(readouts.iter().any(|(live, _)| *live), "{readouts:?}"); - for (_, failure_probability) in readouts { - let failure_probability = failure_probability.expect("a level"); - assert!( - (failure_probability - 0.01 / 6.0).abs() < 1e-12, - "{failure_probability}" - ); + let mut levels: Vec = readouts + .iter() + .map(|(_, failure_probability)| failure_probability.expect("a level")) + .collect(); + levels.sort_by(f64::total_cmp); + levels.dedup_by(|a, b| (*a - *b).abs() < 1e-15); + assert_eq!(levels.len(), expected.len(), "{levels:?}"); + for (level, expected) in levels.iter().zip(expected) { + assert!((level - expected).abs() < 1e-12, "{levels:?}"); } } diff --git a/crates/integration-tests/tests/planning_common/mod.rs b/crates/integration-tests/tests/planning_common/mod.rs index 6b553c06e..3fc33b103 100644 --- a/crates/integration-tests/tests/planning_common/mod.rs +++ b/crates/integration-tests/tests/planning_common/mod.rs @@ -109,7 +109,8 @@ pub fn sql_pass2(queries: &[(&str, Value)]) -> LogicalASAPMemoCandidates { sql_pass2_over(queries, json!({"flows": {"series_count": 1000}})) } -/// [`sql_pass2`] with the facts `sources` declares for `flows`. +/// [`sql_pass2`] with the facts `sources` declares for `flows` and for +/// `hosts(ts, host, region)`, which a query may join with `flows`. pub fn sql_pass2_over(queries: &[(&str, Value)], sources: Value) -> LogicalASAPMemoCandidates { let flows = json!({"name": "flows", "time_index": 0, "columns": [ {"name": "ts", "type": "timestamp"}, @@ -117,11 +118,16 @@ pub fn sql_pass2_over(queries: &[(&str, Value)], sources: Value) -> LogicalASAPM {"name": "dst", "type": "utf8"}, {"name": "bytes", "type": "float64"}, ]}); + let hosts = json!({"name": "hosts", "time_index": 0, "columns": [ + {"name": "ts", "type": "timestamp"}, + {"name": "host", "type": "utf8"}, + {"name": "region", "type": "utf8"}, + ]}); pass2_of( json!({"sql": "datafusion_sql"}), queries, sources, - Some(&[flows.to_string()]), + Some(&[flows.to_string(), hosts.to_string()]), ) } diff --git a/crates/types/src/stages/logical/memo/tests.rs b/crates/types/src/stages/logical/memo/tests.rs index f3d0173af..1ba98db1d 100644 --- a/crates/types/src/stages/logical/memo/tests.rs +++ b/crates/types/src/stages/logical/memo/tests.rs @@ -578,3 +578,73 @@ fn max_values_per_evaluation_is_unbounded_for_a_computed_or_time_key() { ); } } + +/// Keys copied from two scans of one source bound the values by the +/// product of each scan's combinations, not by the source's alone: over +/// the table `flows` with 20 declared `src` values, a self-join's readout +/// grouped by both sides' `src` returns up to 20 × 20 = 400 values, and +/// one grouped by the left side's `src` alone up to 20. +#[test] +fn max_values_per_evaluation_multiplies_the_keys_of_different_scans() { + use crate::ir::operator::arguments::{JoinKind, Source}; + use crate::ir::scalar::ScalarValue; + use crate::ir::schema::{DataType, Field}; + use crate::ir::{Predicate, ScalarExpr}; + let data = crate::workload::DataWorkload { + sources: [( + "flows".to_string(), + crate::workload::SourceFacts { + series_count: Some(1000), + distinct_values_per_label: [("src".to_string(), 20)].into(), + ..Default::default() + }, + )] + .into(), + ..Default::default() + }; + let mut memo = Memo::new(&data); + // `flows(ts, bytes, src)`. + let flows = memo + .find_or_add_group(Operator::NonASAP(NonASAPOp::Scan { + source: Source::Table { + table_ref: "flows".into(), + }, + predicates: vec![], + schema: Schema::with_time_index( + vec![ + Field::plain("ts", DataType::Timestamp, false), + Field::plain("value", DataType::Float64, false), + Field::plain("src", DataType::Utf8, false), + ], + 0, + vec![], + ), + })) + .unwrap(); + // `[ts, value, src, ts, value, src]`. + let joined = memo + .find_or_add_group(Operator::NonASAP(NonASAPOp::Join { + kind: JoinKind::Cross, + pred: Predicate(ScalarExpr::Literal(ScalarValue::Boolean(true))), + left: flows, + right: flows, + })) + .unwrap(); + for (keys, values) in [(vec![2, 5], 400), (vec![2], 20)] { + let state = memo + .find_or_add_group(state_grouped_by( + FieldDataType::Sketch(SketchAlgorithm::Kll), + Reduction::by(keys.clone()), + joined, + )) + .unwrap(); + let quantile = memo + .find_or_add_group(readout(SketchStatistic::Quantile { q: 0.9 }, state)) + .unwrap(); + assert_eq!( + memo.max_values_per_evaluation(quantile), + Some(values), + "by {keys:?}" + ); + } +} From 7597f6965c06acd3227b51f5e6d16822d5b12528 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 04:15:50 +0000 Subject: [PATCH 08/24] fix(accuracy): bound a readout's values by its keys' declared distinct values per scan, clamped to the rows read As SQL optimizers estimate a GROUP BY's groups (the product of the key columns' distinct counts, clamped to the input rows), but as an upper bound from declared facts only: - keys from different scans (a join, or a self-join's two sides) multiply; - a SQL key bounds the values only through its declared distinct values, not through the groups of rows the table declares, which do not say which columns form them; - PromQL labels keep their series bound; - every readout's values are at most the rows its input reads per evaluation: each source's series times the samples its window holds at its sample interval, or the rows it holds. --- crates/types/src/stages/logical/memo/mod.rs | 270 +++++++++++++------ crates/types/src/workload/data.rs | 20 ++ docs/design_docs/concepts/accuracy-models.md | 124 +++++---- 3 files changed, 278 insertions(+), 136 deletions(-) diff --git a/crates/types/src/stages/logical/memo/mod.rs b/crates/types/src/stages/logical/memo/mod.rs index 012461573..d1ee35d28 100644 --- a/crates/types/src/stages/logical/memo/mod.rs +++ b/crates/types/src/stages/logical/memo/mod.rs @@ -12,12 +12,12 @@ use super::group::{ MemoGroupError, }; use crate::accuracy::AccuracyTarget; -use crate::ir::operator::arguments::Reduction; +use crate::ir::operator::arguments::{JoinKind, Reduction, Source, SourceColumn}; use crate::ir::operator::input::InputOrTemporaryBuild; use crate::ir::operator::AggIntent; use crate::ir::schema::{DataType, FieldDataType, Schema}; use crate::ir::QueryRoot; -use crate::ir::{ASAPOp, NonASAPOp, Operator}; +use crate::ir::{ASAPOp, NonASAPOp, Operator, TimeRangeKind}; use crate::stages::{ CandidateError, CandidateForMemoGroup, CandidateForMemoGroupId, ColumnLineage, LogicalRequirements, MemoGroup, MemoGroupId, @@ -95,42 +95,155 @@ impl Memo { /// The most values one evaluation of `group`, a readout or a state, /// returns, from the facts the data workload declares; `None` when they - /// bound it in no way. Only a result keyed by the sources' labels (or - /// SQL columns copied unchanged, not times) is bounded - /// ([`Self::keys_of_values`]), by the smaller of: - /// - the series (PromQL) or groups of rows (SQL) of the sources scanned - /// below it (`GroupProperties::scanned_sources`, each source's - /// `SourceFacts::series_count`, when every one declares it): each - /// value of a per-series result, or of a result grouped by their - /// labels, comes from at least one of them; - /// - for keys copied from one source, the distinct combinations it - /// declares for them (`SourceFacts::distinct_values_per_label` and - /// `distinct_values_per_label_set`); 1 for no key. + /// bound it in no way. As SQL optimizers estimate the groups of a + /// `GROUP BY` (the product of the key columns' distinct counts, clamped + /// to the input rows), but as an upper bound, it is the smaller of: + /// - the values its keys can take ([`Self::keys_of_values`]): for keys + /// copied unchanged from source columns, the product, over the scans + /// they come from, of the combinations each scan's source declares + /// for its keys (`SourceFacts::distinct_values_per_label` and + /// `distinct_values_per_label_set`), at most its series for a PromQL + /// source, each series having one label set; 1 for no key; for a + /// per-series result (or several rows per group, as a top-k), the + /// series of the PromQL sources scanned below it, summed + /// (`SourceFacts::series_count`). A SQL key with no declared count, + /// such as a measure column, or a computed key or a time, bounds + /// nothing: the groups of rows a SQL source declares do not say which + /// columns form them; + /// - the rows its input reads per evaluation + /// ([`Self::max_rows_per_evaluation`]): there are never more groups + /// than input rows. /// - /// A result grouped by a computed key or a time is not bounded: one - /// value per bucket of `GROUP BY date_trunc('minute', ts)` over the last - /// 15 minutes is about 16 values, however few groups of rows the table - /// holds. + /// Keys from different scans multiply, for a join pairs their values: + /// `flows JOIN hosts ... GROUP BY f.src, h.region`, with 6 `src` and 6 + /// `region` values declared, returns up to 36 values, not 6 + 6. /// /// Examples: a readout of `quantile_over_time(0.9, m[5m])` over `m` /// with 1000 series returns at most 1000 values, which `max(...)` over /// it reads at once; `SELECT count(DISTINCT dst) FROM flows` reads one - /// distinct count per evaluation. + /// distinct count per evaluation; `GROUP BY bytes` over the last 15 + /// minutes of `flows`, 6 groups of rows sampled every minute, returns + /// at most the 6 × 16 = 96 rows it reads. pub fn max_values_per_evaluation(&self, group: MemoGroupId) -> Option { - let KeysOfValues::Labels { declared_groups } = self.keys_of_values(group) else { + let (keys, input) = self.keys_of_values(group)?; + let by_keys = match keys { + // PromQL has no join: a vector binary operator returns at most + // one series per series of one side, `or` both sides', so the + // series of the sources scanned add up. + KeysOfValues::PerSeries => self.groups[input.0 as usize] + .group_properties + .scanned_sources + .iter() + .try_fold(0u64, |total, source| { + if !source.is_time_series() { + return None; + } + let (facts, _) = series_facts(&self.source_facts, source_name(source))?; + total.checked_add(facts.series_count?) + }), + KeysOfValues::SourceColumns(columns) => { + let mut columns_per_scan: BTreeMap<(&Source, u32), Vec<&str>> = BTreeMap::new(); + for column in &columns { + columns_per_scan + .entry((&column.source, column.scan_ordinal)) + .or_default() + .push(column.column.as_str()); + } + columns_per_scan + .into_iter() + .try_fold(1u64, |product, ((source, _), columns)| { + let (facts, _) = series_facts(&self.source_facts, source_name(source))?; + let declared = facts.declared_distinct_combinations(&columns); + let series = facts.series_count.filter(|_| source.is_time_series()); + product.checked_mul(smaller_known(declared, series)?) + }) + } + KeysOfValues::ComputedOrTime => None, + }; + smaller_known(by_keys, self.max_rows_per_evaluation(input)) + } + + /// The most rows one evaluation of `group` returns, from the facts the + /// data workload declares; `None` when they bound it in no way. A `Scan` + /// returns the rows its source holds (`SourceFacts::rows_held`), and a + /// `TimeRange` over it the rows of its window + /// (`SourceFacts::max_rows_in_window`; for an instant selector, one per + /// series at most); an operator that keeps, drops or groups its input's + /// rows at most its input's; a join at most the product of its inputs' + /// (plus each side's own for an outer join), a union their sum. + /// Example: `flows WHERE ts >= now() - INTERVAL '15 minutes'`, 6 groups + /// of rows sampled every minute, returns at most 6 × 16 = 96 rows. + fn max_rows_per_evaluation(&self, group: MemoGroupId) -> Option { + let rows = |input: &MemoGroupId| self.max_rows_per_evaluation(*input); + let facts = |source: &Source| { + series_facts(&self.source_facts, source_name(source)).map(|(facts, _)| facts) + }; + let Operator::NonASAP(op) = &self.groups[group.0 as usize].expression else { + // A state, readout or merge: its rows are not its input's. return None; }; - let series_scanned = self.groups[group.0 as usize] - .group_properties - .scanned_sources - .iter() - .try_fold(0u64, |total, source| { - let (facts, _) = series_facts(&self.source_facts, source_name(source))?; - total.checked_add(facts.series_count?) - }); - match (series_scanned, declared_groups) { - (Some(series), Some(groups)) => Some(series.min(groups)), - (series, groups) => series.or(groups), + match op { + NonASAPOp::Scan { source, .. } => facts(source)?.rows_held, + NonASAPOp::Values { rows, .. } => u64::try_from(rows.len()).ok(), + NonASAPOp::TimeRange { range, kind, child } => { + let window_rows = match &self.groups[child.0 as usize].expression { + Operator::NonASAP(NonASAPOp::Scan { source, .. }) => { + facts(source).and_then(|facts| { + let window_ms = u64::try_from(range.as_millis()).ok()?; + let in_window = facts.max_rows_in_window(window_ms); + match kind { + TimeRangeKind::Range => in_window, + TimeRangeKind::Instant => { + smaller_known(in_window, facts.series_count) + } + } + }) + } + Operator::NonASAP(_) | Operator::ASAP(_) => None, + }; + smaller_known(window_rows, rows(child)) + } + NonASAPOp::Filter { child, .. } + | NonASAPOp::Project { child, .. } + | NonASAPOp::Dedup { child, .. } + | NonASAPOp::Sort { child, .. } + | NonASAPOp::Limit { child, .. } + | NonASAPOp::SQLWindowFunc { child, .. } + | NonASAPOp::TimeShift { child, .. } + | NonASAPOp::PromqlRelabel { child, .. } + | NonASAPOp::PromqlInfoEnrich { child, .. } + | NonASAPOp::PromqlSeriesSample { child, .. } => rows(child), + // A global aggregate returns one row even over no rows. + NonASAPOp::Aggregate { child, .. } => rows(child).map(|rows| rows.max(1)), + NonASAPOp::Join { + kind, left, right, .. + } => { + let (left, right) = (rows(left)?, rows(right)?); + let pairs = left.checked_mul(right)?; + match kind { + JoinKind::Inner | JoinKind::Cross => Some(pairs), + JoinKind::Semi | JoinKind::Anti => Some(left), + JoinKind::Left | JoinKind::Right | JoinKind::Full => { + pairs.checked_add(left)?.checked_add(right) + } + } + } + // A vector binary operator returns at most one row per row of + // one side (`or` keeps both sides' rows); a set operator at most + // both sides' rows. + NonASAPOp::BinaryOp { lhs, rhs, .. } + | NonASAPOp::SetOp { + left: lhs, + right: rhs, + .. + } => rows(lhs)?.checked_add(rows(rhs)?), + NonASAPOp::Concat { children } => children + .iter() + .try_fold(0u64, |total, child| total.checked_add(rows(child)?)), + NonASAPOp::PromqlVectorFromScalar(_) => Some(1), + // Its child's rows at each step of its range, which no declared + // fact bounds here. + NonASAPOp::PromqlSubquery { .. } => None, } } @@ -154,23 +267,25 @@ impl Memo { ) } - /// What the values of `group` are keyed by - /// ([`Self::max_values_per_evaluation`]): the keys of the state it is, - /// or reads, or of the aggregate it is. A top-k readout or aggregate - /// returns several rows per group, so its declared groups are unknown. - fn keys_of_values(&self, group: MemoGroupId) -> KeysOfValues { + /// What the values of `group` are keyed by, and the group of rows they + /// are computed from ([`Self::max_values_per_evaluation`]): the keys of + /// the state it is, or reads, or of the aggregate it is, and that + /// state's or aggregate's input. A top-k readout or aggregate returns + /// several rows per group, so it counts as per series. `None` for any + /// other group. + fn keys_of_values(&self, group: MemoGroupId) -> Option<(KeysOfValues, MemoGroupId)> { let (reduction, child, one_row_per_group) = match &self.groups[group.0 as usize].expression { Operator::ASAP(ASAPOp::SummaryEstimate { summary_input, query, }) => { - let keys = self.keys_of_values(*summary_input); - return if matches!(query, SketchStatistic::TopK { .. }) { - keys.without_declared_groups() + let (keys, input) = self.keys_of_values(*summary_input)?; + return Some(if matches!(query, SketchStatistic::TopK { .. }) { + (keys.several_rows_per_group(), input) } else { - keys - }; + (keys, input) + }); } Operator::ASAP( ASAPOp::SummaryAgg { @@ -192,24 +307,18 @@ impl Memo { .iter() .any(|m| matches!(m, AggIntent::TopK { .. } | AggIntent::CountValues { .. })), ), - _ => return KeysOfValues::ComputedOrTime, + _ => return None, }; let keys = match reduction { Reduction::Reduce(by) if !by.is_without() => by.keys(), // One value per series, or per group of every label but some. - _ => { - return KeysOfValues::Labels { - declared_groups: None, - } - } + _ => return Some((KeysOfValues::PerSeries, child)), }; let input = &self.groups[child.0 as usize].group_properties; - let mut source = None; - let mut labels = Vec::new(); - let mut one_source = true; + let mut columns = Vec::new(); for key in keys { let Some(ColumnLineage::SourceColumn(column)) = input.column_lineage.get(*key) else { - return KeysOfValues::ComputedOrTime; + return Some((KeysOfValues::ComputedOrTime, child)); }; let time_valued = input.schema.fields.get(*key).is_some_and(|field| { matches!( @@ -218,24 +327,16 @@ impl Memo { ) }); if time_valued { - return KeysOfValues::ComputedOrTime; + return Some((KeysOfValues::ComputedOrTime, child)); } - one_source &= source.is_none_or(|source| source == &column.source); - source = Some(&column.source); - labels.push(column.column.as_str()); + columns.push(column.clone()); } - let declared_groups = match source { - None => Some(1), - Some(source) if one_source => series_facts(&self.source_facts, source_name(source)) - .and_then(|(facts, _)| facts.declared_distinct_combinations(&labels)), - Some(_) => None, - }; - let keys = KeysOfValues::Labels { declared_groups }; - if one_row_per_group { - keys + let keys = KeysOfValues::SourceColumns(columns); + Some(if one_row_per_group { + (keys, child) } else { - keys.without_declared_groups() - } + (keys.several_rows_per_group(), child) + }) } /// The queries, by index in the workload. @@ -597,32 +698,41 @@ fn add_parent(group: &mut MemoGroup, parent: MemoGroupId) { } } -/// What the values one evaluation of a readout or state returns are keyed -/// by ([`Memo::max_values_per_evaluation`]). +/// What the values of a readout, state or aggregate are keyed by +/// ([`Memo::keys_of_values`]). enum KeysOfValues { - /// Each value is one series, or one group of labels (SQL columns) - /// copied unchanged from the sources: at most the series (groups of - /// rows) scanned, and at most `declared_groups`, the combinations the - /// source declares for the keys, when known (`Some(20)` for `by (job)` - /// over 20 declared jobs, `Some(1)` for no key). - Labels { declared_groups: Option }, + /// One value per series (or per group of every label but some), or + /// several per group (a top-k): at most the series of the PromQL + /// sources scanned. + PerSeries, + /// One value per combination of these columns, each copied unchanged + /// from a column of one scan of a source (`by (job)`, `GROUP BY src`); + /// none for one value. + SourceColumns(Vec), /// A key is computed (`date_trunc('minute', ts)`, an expression) or a - /// time: one value per bucket or time, which no fact bounds. + /// time: one value per bucket or time, which no declared count bounds. ComputedOrTime, } impl KeysOfValues { - /// These keys, with no declared groups: a top-k returns several rows - /// per group. - fn without_declared_groups(self) -> Self { + /// These keys for a result with several rows per group (a top-k): its + /// values are not bounded by its groups. + fn several_rows_per_group(self) -> Self { match self { - Self::Labels { .. } => Self::Labels { - declared_groups: None, - }, + Self::PerSeries | Self::SourceColumns(_) => Self::PerSeries, Self::ComputedOrTime => Self::ComputedOrTime, } } } +/// The smaller of two upper bounds, either alone when the other is +/// unknown. +fn smaller_known(a: Option, b: Option) -> Option { + match (a, b) { + (Some(a), Some(b)) => Some(a.min(b)), + (a, b) => a.or(b), + } +} + #[cfg(test)] mod tests; diff --git a/crates/types/src/workload/data.rs b/crates/types/src/workload/data.rs index f23fb5384..75c6e509d 100644 --- a/crates/types/src/workload/data.rs +++ b/crates/types/src/workload/data.rs @@ -268,6 +268,26 @@ pub struct LabelSetDistinctValues { const MAX_KEYS_COVERED_BY_LABEL_SETS: usize = 16; impl SourceFacts { + /// The most rows (PromQL samples) a window of `window_ms` over the + /// source holds, from declared facts only: each of its `series_count` + /// series (groups of rows) receives one sample per `sample_interval`, + /// so at most `window_ms / sample_interval + 1` fall in the window, and + /// the source holds `rows_held` rows in all. The smaller of the two, + /// either alone when the other is unknown; `None` when neither is + /// declared. Example: 6 groups of rows sampled every minute hold at + /// most 6 × 16 = 96 rows in 15 minutes. + pub(crate) fn max_rows_in_window(&self, window_ms: u64) -> Option { + let by_rate = self + .series_count + .zip(self.sample_interval) + .filter(|(_, interval)| interval.0 > 0) + .and_then(|(series, interval)| series.checked_mul(window_ms / interval.0 + 1)); + match (by_rate, self.rows_held) { + (Some(by_rate), Some(rows_held)) => Some(by_rate.min(rows_held)), + (by_rate, rows_held) => by_rate.or(rows_held), + } + } + /// An upper bound on the distinct combinations of values `labels` take /// together in this source, from declared counts only: the smallest /// product of counts, of declared label sets diff --git a/docs/design_docs/concepts/accuracy-models.md b/docs/design_docs/concepts/accuracy-models.md index c04dff61d..b78148d9f 100644 --- a/docs/design_docs/concepts/accuracy-models.md +++ b/docs/design_docs/concepts/accuracy-models.md @@ -77,55 +77,67 @@ unknown (HLL without a distinct bound) serves no query with an accuracy target (`RootPruneReason::NoGuarantee`), and a top-k readout is admitted on its heap size and its value guarantee alone (the contracts table). -Pass 2 gives each sketch readout the error and per-value failure -probability `δ / min(R, V · fan_in)` it must meet +Pass 2 gives each sketch readout the error and per-value failure probability +`δ / min(R, V · fan_in)` it must meet (`AccuracyTarget::failure_probability_per_readout_value`): the statement -fails only when a readout value one of its `V` covered output values -depends on does. `R` is the readout values the query reads per evaluation -(the values per evaluation of every readout group below it, summed: the -series their scans read, for readouts keyed by the sources' labels), and `fan_in` the most readout values one output -value depends on: 1 when no reduction lies above the readout, the -reduction's input values under one (all of `R` for a global `max`; for -`sum by (job)` too, since the declared facts bound how many jobs there are, -not how many series one job holds), the sum over the sides of a binary -operator, the larger side of `or` or input of a concatenation. A top-k -readout chooses its output keys by their estimates, so each of its output -values depends on every value it ranks. A readout group whose candidates -include a reduction over readouts below it takes the larger of the two -counts. Both counts are per evaluation, not per subquery step. An unknown -term drops out of the `min`; with both unknown no per-value failure -probability follows. At δ = 0.01, `quantile_over_time(0.99, m[5m])` over 8 -series per value keeps 0.01 (min(8, 1 · 1)), and over its whole result -gets 0.01 / 8; a `max` over 1000 readouts returns one value yet fails when -any readout does, so it gets 0.01 / 1000 in any scope; and -`max by (job) (q or r)` splits over the readouts of both sides. Pass 1's -candidate takes the first requirement level and Pass 2 adds a copy per -further level. A readout several queries reach gets the union of their -levels, less each level the listed builds that meet it are exactly those -that meet a stricter one: DDSketch's relative error does not depend on δ, -so of (5%, 0.01) and (5%, 1e-4) only the second is kept. Pass 2 sizes -nothing: a state has one build per listed θ, and each edge checks the -guarantee of its producer's θ at the required failure probability, so a -build several readers share serves the readers whose levels its θ meets. -Stage 3 only checks the guarantee; it never resizes. At equal δ, the scope -covering the most values is the strictest: every group (`G`, when `G` is -above 100), then a 1% share (100), then a 5% share (20), then a single -value (1). A whole-result target with an unbound `G` splits over `R`, which -bounds every readout value read only when each readout is keyed by the -sources' labels or columns: grouped by a computed key or a time, one value -per bucket of `GROUP BY date_trunc('minute', ts)` is about 16 values over the -last 15 minutes however few groups of rows the table declares, so `R` is -unbound there. With both unbound the target stays unmet. A grouped result's `G` is bound by +fails only when a readout value one of its `V` covered output values depends +on does. `R` is the readout values the query reads per evaluation (the +values per evaluation of every readout group below it, summed), and `fan_in` +the most readout values one output value depends on: 1 when no reduction +lies above the readout, the reduction's input values under one (all of `R` +for a global `max`; for `sum by (job)` too, since the declared facts bound +how many jobs there are, not how many series one job holds), the sum over +the sides of a binary operator, the larger side of `or` or input of a +concatenation. A top-k readout chooses its output keys by their estimates, +so each of its output values depends on every value it ranks. A readout +group whose candidates include a reduction over readouts below it takes the +larger of the two counts. Both counts are per evaluation, not per subquery +step. An unknown term drops out of the `min`; with both unknown no per-value +failure probability follows. At δ = 0.01, `quantile_over_time(0.99, m[5m])` +over 8 series per value keeps 0.01 (min(8, 1 · 1)), and over its whole +result gets 0.01 / 8; a `max` over 1000 readouts returns one value yet fails +when any readout does, so it gets 0.01 / 1000 in any scope; and `max by +(job) (q or r)` splits over the readouts of both sides. Pass 1's candidate +takes the first requirement level and Pass 2 adds a copy per further level. +A readout several queries reach gets the union of their levels, less each +level the listed builds that meet it are exactly those that meet a stricter +one: DDSketch's relative error does not depend on δ, so of (5%, 0.01) and +(5%, 1e-4) only the second is kept. Pass 2 sizes nothing: a state has one +build per listed θ, and each edge checks the guarantee of its producer's θ +at the required failure probability, so a build several readers share serves +the readers whose levels its θ meets. Stage 3 only checks the guarantee; it +never resizes. At equal δ, the scope covering the most values is the +strictest: every group (`G`, when `G` is above 100), then a 1% share (100), +then a 5% share (20), then a single value (1). A whole-result target with an +unbound `G` splits over `R`, so `R` must be an upper bound. A readout +group's values are bounded as SQL optimizers estimate the groups of a `GROUP +BY` (the product of the key columns' distinct counts, clamped to the input +rows), but from declared facts only: by the smaller of the product, over the +scans its keys come from, of the distinct combinations each scan's source +declares for them (at most its series for a PromQL source; 1 without keys), +and the rows its input reads per evaluation (each source's `series_count` +times the samples its window holds at its `sample_interval`, or its +`rows_held`), since there are never more groups than input rows. A key with +no declared count bounds nothing by itself: a SQL measure key (`GROUP BY +bytes`), a computed key or a time. The groups of rows a SQL table declares +do not say which columns form them, so they bound no key; `GROUP BY bytes` +over the last 15 minutes of 6 groups of rows sampled every minute reads at +most 6 x 16 = 96 rows, so `R` = 96, and without a sampling interval or +`rows_held` `R` is unbound. Keys from different scans multiply, since a join +pairs their values: `flows JOIN hosts ... GROUP BY f.src, h.region` with 6 +`src` and 6 `region` values declared returns up to 36 values, not 6 + 6. +With both unbound the target stays unmet. A grouped result's `G` is bound by the distinct values its sources declare for its group keys (`SourceFacts.distinct_values_per_label`, and `distinct_values_per_label_set` for labels that depend on each other): the -smallest product of declared label and label-set counts covering the keys, at -most the series read. With each of 625 endpoints in one of 25 services and -`(service, endpoint)` declared at 625, `by (service, endpoint)` returns at -most 625 values, not 25 x 625. A key counts only when it is a source column passed through -unchanged; a relabeled or computed key leaves `G` unbound. So a sketch meets a whole-result target over SQL groups when -every grouping column's distinct values are declared, and over 25 services -and 25 endpoints requires each group's sketch to meet `δ / 625`. Nothing declared +smallest product of declared label and label-set counts covering the keys, +at most the series read. With each of 625 endpoints in one of 25 services +and `(service, endpoint)` declared at 625, `by (service, endpoint)` returns +at most 625 values, not 25 x 625. A key counts only when it is a source +column passed through unchanged; a relabeled or computed key leaves `G` +unbound. So a sketch meets a whole-result target over SQL groups when every +grouping column's distinct values are declared, and over 25 services and 25 +endpoints requires each group's sketch to meet `δ / 625`. Nothing declared leaves `G` unbound; no default stands in for it. An `AccuracyGuarantee` contains an error metric, an error bound and a @@ -319,17 +331,17 @@ the readout to the query root, and capped by the validity caps on the way: the largest error the readout's output may have for that query. - A query whose path reads one readout gives one requirement level: the - target divided by the path's factor, at the target's failure probability per - value: δ split over `min(R, V · fan_in)`, the readout values the output - values the scope covers depend on (`R` summed over every readout group - below it, `Memo::max_values_per_evaluation` each: at most the series - scanned below it, the declared distinct values of its state's keys, one - without keys, when the keys are the sources' labels or columns; unbound - for a computed key or a time), since a reduction such as `max` fails when any readout it - reads does. `SELECT count(DISTINCT dst) FROM flows` at δ = 0.01 reads - one distinct count, so its readout keeps 0.01. `max by (job) (q or r)` - with 3 series per side reads 6 readout values, and one job's value may - depend on all, so each gets δ / 6; a bare `q or r` copies each output + target divided by the path's factor, at the target's failure probability + per value: δ split over `min(R, V · fan_in)`, the readout values the + output values the scope covers depend on (`R` summed over every readout + group below it, `Memo::max_values_per_evaluation` each: the product of the + distinct values its keys' scans declare for them, at most the series for + PromQL labels and one without keys, clamped to the rows its input reads; + unbound when neither is known), since a reduction such as `max` fails when + any readout it reads does. `SELECT count(DISTINCT dst) FROM flows` at δ = + 0.01 reads one distinct count, so its readout keeps 0.01. `max by (job) (q + or r)` with 3 series per side reads 6 readout values, and one job's value + may depend on all, so each gets δ / 6; a bare `q or r` copies each output value from one readout value, so per value it keeps δ and over a whole result of `n_q` values gets δ / `n_q`. - A composed query (several readouts whose errors reach its output, `β > 0`, From eb639c9f463dcb29e7d20d59a2717d2732d99ecf Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 04:56:48 +0000 Subject: [PATCH 09/24] test(accuracy): a grouped SQL result's values are its keys' declared values per scan, clamped to the rows read GROUP BY dst with 300 dst values declared returns up to 300 values, not the 6 groups of rows the table declares; an undeclared key is bounded by the rows read; keys of joined scans multiply. A PromQL result keeps its series bound. --- .../tests/pass2_promql/accuracy_and_grids.rs | 32 +++++++- crates/integration-tests/tests/pass2_sql.rs | 82 +++++++++++++++++++ .../tests/planning_common/mod.rs | 9 +- 3 files changed, 119 insertions(+), 4 deletions(-) diff --git a/crates/integration-tests/tests/pass2_promql/accuracy_and_grids.rs b/crates/integration-tests/tests/pass2_promql/accuracy_and_grids.rs index c84f116b1..bd7e3b6db 100644 --- a/crates/integration-tests/tests/pass2_promql/accuracy_and_grids.rs +++ b/crates/integration-tests/tests/pass2_promql/accuracy_and_grids.rs @@ -2,7 +2,7 @@ //! production lowering: the frontend's `Project`s of a function of the //! value keep a readout's error bounded, and a subquery's resolution is the //! evaluation grid of the groups below it. -use crate::planning_common::promql_pass2; +use crate::planning_common::{promql_pass2, promql_pass2_over}; use asap_types::ir::operator::asap::summary_type_of_operator; use asap_types::ir::schema::FieldDataType; use asap_types::ir::{ASAPOp, NonASAPOp, Operator}; @@ -113,3 +113,33 @@ fn a_subquery_s_resolution_is_the_grid_of_the_groups_below_it() { let without_resolution = pass2("max_over_time(rate(c[2m])[10m:])", rank); assert_eq!(chain_widths(&without_resolution), Vec::::new()); } + +/// $n_q$ of a PromQL result is the series bound, tightened by the declared +/// values of its `by` keys: `sum by (job) (rate(m[5m]))` returns one value +/// per `job`, the 20 `m` declares, or, undeclared, at most `m`'s 1000 +/// series; `rate(m[5m])` one per series, 1000. +#[test] +fn a_promql_result_returns_at_most_its_keys_declared_values_or_its_series() { + let values_per_evaluation = |query: &str, job_values: Option| { + let distinct_values = job_values.map_or(json!({}), |job| json!({"job": job})); + promql_pass2_over( + &[( + query, + json!({"max_error": 0.05, "accuracy_metric": "relative"}), + )], + json!({"m": { + "series_count": 1000, + "metric_type": "gauge", + "sample_interval": 1000, + "distinct_values_per_label": distinct_values, + }}), + ) + .memo + .queries()[0] + .number_of_values_per_query_evaluation + }; + let by_job = "sum by (job) (rate(m[5m]))"; + assert_eq!(values_per_evaluation(by_job, Some(20)), Some(20)); + assert_eq!(values_per_evaluation(by_job, None), Some(1000)); + assert_eq!(values_per_evaluation("rate(m[5m])", None), Some(1000)); +} diff --git a/crates/integration-tests/tests/pass2_sql.rs b/crates/integration-tests/tests/pass2_sql.rs index 84207086a..ea132076f 100644 --- a/crates/integration-tests/tests/pass2_sql.rs +++ b/crates/integration-tests/tests/pass2_sql.rs @@ -237,3 +237,85 @@ fn assert_levels(logical: &LogicalASAPMemoCandidates, expected: &[f64]) { assert!((level - expected).abs() < 1e-12, "{levels:?}"); } } + +/// $n_q$, the values one evaluation of the only query of `logical` +/// returns at most. +fn values_per_evaluation(logical: &LogicalASAPMemoCandidates) -> Option { + logical.memo.queries()[0].number_of_values_per_query_evaluation +} + +/// `GROUP BY dst`, with 300 distinct `dst` values declared, returns up to +/// 300 values: the key's declared values, not the 6 groups of rows +/// `flows` declares (nothing says which columns form them). The readout's +/// δ splits over all 300. Sampled every minute, the last 15 minutes hold +/// at most 6 × 16 = 96 rows, which bound the groups instead: δ / 96. +#[test] +fn a_result_grouped_by_a_declared_key_returns_up_to_its_declared_values() { + let by_dst = |sample_interval: Option| { + whole_result_quantile( + "SELECT dst, approx_percentile_cont(bytes, 0.9) AS q FROM flows \ + WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY dst", + json!({"flows": { + "series_count": 6, + "sample_interval": sample_interval, + "distinct_values_per_label": {"dst": 300}, + }}), + ) + }; + let unsampled = by_dst(None); + assert_eq!(values_per_evaluation(&unsampled), Some(300)); + assert_levels(&unsampled, &[0.01 / 300.0]); + let sampled = by_dst(Some(60_000)); + assert_eq!(values_per_evaluation(&sampled), Some(96)); + assert_levels(&sampled, &[0.01 / 96.0]); +} + +/// `GROUP BY bytes` keys each value by a measure with no declared count of +/// distinct values: only the rows read bound the result. Unknown without a +/// sampling interval; sampled every minute, at most 6 × 16 = 96 rows over +/// the last 15 minutes. +#[test] +fn a_result_grouped_by_an_undeclared_key_returns_at_most_the_rows_read() { + let by_bytes = |sample_interval: Option| { + whole_result_quantile( + "SELECT bytes, approx_percentile_cont(bytes, 0.9) AS q FROM flows \ + WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY bytes", + json!({"flows": { + "series_count": 6, + "sample_interval": sample_interval, + "distinct_values_per_label": {"src": 6}, + }}), + ) + }; + assert_eq!(values_per_evaluation(&by_bytes(None)), None); + assert_eq!(values_per_evaluation(&by_bytes(Some(60_000))), Some(96)); +} + +/// `flows JOIN hosts` grouped by `f.src` (6 declared) and `h.region` (6 +/// declared) returns up to 6 × 6 = 36 values, the keys of different scans +/// multiplying; with `flows` holding 6 rows and `hosts` 5, the join reads +/// at most 30 rows, which bound it instead. +#[test] +fn a_result_grouped_by_keys_of_joined_scans_returns_up_to_their_product() { + let join = |flows_rows: Option, hosts_rows: Option| { + whole_result_quantile( + "SELECT f.src, h.region, approx_percentile_cont(f.bytes, 0.9) AS q \ + FROM flows f JOIN hosts h ON f.dst = h.host \ + WHERE f.ts >= now() - INTERVAL '15 minutes' GROUP BY f.src, h.region", + json!({ + "flows": { + "series_count": 6, + "rows_held": flows_rows, + "distinct_values_per_label": {"src": 6, "dst": 300}, + }, + "hosts": { + "series_count": 6, + "rows_held": hosts_rows, + "distinct_values_per_label": {"host": 6, "region": 6}, + }, + }), + ) + }; + assert_eq!(values_per_evaluation(&join(None, None)), Some(36)); + assert_eq!(values_per_evaluation(&join(Some(6), Some(5))), Some(30)); +} diff --git a/crates/integration-tests/tests/planning_common/mod.rs b/crates/integration-tests/tests/planning_common/mod.rs index 3fc33b103..906e52d7a 100644 --- a/crates/integration-tests/tests/planning_common/mod.rs +++ b/crates/integration-tests/tests/planning_common/mod.rs @@ -89,8 +89,7 @@ pub fn selected_plan(w: &FuzzWorkload, settings: &CostInputs) -> PhysicalASAPDAG /// gauge `latency` whose values are declared non-negative (so DDSketch's /// quantile guarantee holds over it), 1000 series each. pub fn promql_pass2(queries: &[(&str, Value)]) -> LogicalASAPMemoCandidates { - pass2_of( - json!("promql"), + promql_pass2_over( queries, json!({ "m": {"series_count": 1000, "metric_type": "gauge", "sample_interval": 1000}, @@ -98,10 +97,14 @@ pub fn promql_pass2(queries: &[(&str, Value)]) -> LogicalASAPMemoCandidates { "latency": {"series_count": 1000, "metric_type": "gauge", "sample_interval": 1000, "column_signs": {"value": "non_negative"}}, }), - None, ) } +/// [`promql_pass2`] with the facts `sources` declares. +pub fn promql_pass2_over(queries: &[(&str, Value)], sources: Value) -> LogicalASAPMemoCandidates { + pass2_of(json!("promql"), queries, sources, None) +} + /// Pass 2's output for SQL `queries` over the table /// `flows(ts, src, dst, bytes)` of 1000 groups of rows, each with its /// accuracy target, repeating every minute. From ddb77f84dd917f0a33317bb416995e9e0214d747 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 05:02:23 +0000 Subject: [PATCH 10/24] fix(accuracy): bound a query's values by its keys' declared values per scan, clamped to the rows read A grouped SQL result's n_q was the groups its keys declare, at most the groups of rows the table declares: GROUP BY dst with 300 dst values over a table of 6 groups of rows gave 6, so a whole-result target split delta over 6 instead of 300. n_q now comes from the memo's bound on the root group (Memo::max_values_per_evaluation), the rule R uses: the product, per scan, of the keys' declared distinct values, clamped to the rows the grouping reads. A PromQL instant vector keeps its series bound. The IR-side copy of the group rule in bounds is deleted. --- crates/stage0-memo/src/lib.rs | 17 +- crates/types/src/stages/logical/memo/mod.rs | 43 ++-- crates/types/src/workload/bounds.rs | 211 ++----------------- crates/types/src/workload/data.rs | 5 +- docs/design_docs/concepts/accuracy-models.md | 30 +-- 5 files changed, 80 insertions(+), 226 deletions(-) diff --git a/crates/stage0-memo/src/lib.rs b/crates/stage0-memo/src/lib.rs index b72691b35..70b6424fb 100644 --- a/crates/stage0-memo/src/lib.rs +++ b/crates/stage0-memo/src/lib.rs @@ -21,11 +21,12 @@ use asap_types::workload::DataWorkload; /// being `roots[i]` with accuracy target `accuracy_targets[i]` (`None`: /// none), every group without candidates. Each query is added with its /// $n_q$, the number of values one evaluation returns, which `data` bounds -/// ([`values_per_result`]); a `Scan`'s column signs are the ones `data` -/// declares, which the memo holds (`Memo::new`). Fails with the first node -/// whose expression is not valid over its children's groups -/// ([`Memo::find_or_add_group`]); the facade validates the roots first, so -/// that is a frontend bug. +/// ([`values_per_result`], from the memo's bound on the root group's +/// values, `Memo::max_values_per_evaluation`); a `Scan`'s column signs are +/// the ones `data` declares, which the memo holds (`Memo::new`). Fails +/// with the first node whose expression is not valid over its children's +/// groups ([`Memo::find_or_add_group`]); the facade validates the roots +/// first, so that is a frontend bug. pub fn memo_of_queries( roots: &[QueryRoot], accuracy_targets: &[Option], @@ -45,6 +46,10 @@ pub fn memo_of_queries( } let group = |node: &Rc| group_of_node[&Rc::as_ptr(node)]; for (root, accuracy_target) in roots.iter().zip(accuracy_targets) { + let values_by_keys = match root { + QueryRoot::Operator(node) => memo.max_values_per_evaluation(group(node)), + QueryRoot::Scalar(_) => None, + }; memo.add_query(MemoQuery { root: match root { QueryRoot::Operator(node) => QueryRoot::Operator(group(node)), @@ -56,7 +61,7 @@ pub fn memo_of_queries( number_of_values_per_query_evaluation: values_per_result( root, series_read(root, data), - data, + values_by_keys, ), }); } diff --git a/crates/types/src/stages/logical/memo/mod.rs b/crates/types/src/stages/logical/memo/mod.rs index d1ee35d28..f6be90406 100644 --- a/crates/types/src/stages/logical/memo/mod.rs +++ b/crates/types/src/stages/logical/memo/mod.rs @@ -23,7 +23,7 @@ use crate::stages::{ LogicalRequirements, MemoGroup, MemoGroupId, }; use crate::summary_type::SketchStatistic; -use crate::workload::bounds::{declared_max_distinct_per_evaluation, source_name}; +use crate::workload::bounds::{declared_max_distinct_per_evaluation, smaller_known, source_name}; use crate::workload::{series_facts, DataArrival, DataWorkload, SourceFacts}; use hash_consing::{ expression_hash, same_candidate, same_serialized_expression, serialized_expression, @@ -93,8 +93,8 @@ impl Memo { &self.groups } - /// The most values one evaluation of `group`, a readout or a state, - /// returns, from the facts the data workload declares; `None` when they + /// The most values one evaluation of `group`, a readout, a state, a + /// grouped aggregate or a query result over one, returns, from the facts the data workload declares; `None` when they /// bound it in no way. As SQL optimizers estimate the groups of a /// `GROUP BY` (the product of the key columns' distinct counts, clamped /// to the input rows), but as an upper bound, it is the smaller of: @@ -123,7 +123,10 @@ impl Memo { /// it reads at once; `SELECT count(DISTINCT dst) FROM flows` reads one /// distinct count per evaluation; `GROUP BY bytes` over the last 15 /// minutes of `flows`, 6 groups of rows sampled every minute, returns - /// at most the 6 × 16 = 96 rows it reads. + /// at most the 6 × 16 = 96 rows it reads. It is the readout values `R` + /// a query's failure probability splits over, and Stage 0's bound on a + /// query's values `n_q` (`workload::bounds::values_per_result`): `GROUP + /// BY dst` with 300 `dst` values declared returns up to 300. pub fn max_values_per_evaluation(&self, group: MemoGroupId) -> Option { let (keys, input) = self.keys_of_values(group)?; let by_keys = match keys { @@ -271,11 +274,30 @@ impl Memo { /// are computed from ([`Self::max_values_per_evaluation`]): the keys of /// the state it is, or reads, or of the aggregate it is, and that /// state's or aggregate's input. A top-k readout or aggregate returns - /// several rows per group, so it counts as per series. `None` for any - /// other group. + /// several rows per group, so it counts as per series. Above them, an + /// operator that keeps, drops or reorders their rows (the projection, + /// `HAVING`, `ORDER BY` and `LIMIT` of `SELECT src, count(*) ... GROUP + /// BY src`), a finalization, or a merge keeping its states' groups has + /// their keys. `None` for any other group. fn keys_of_values(&self, group: MemoGroupId) -> Option<(KeysOfValues, MemoGroupId)> { let (reduction, child, one_row_per_group) = match &self.groups[group.0 as usize].expression { + Operator::NonASAP( + NonASAPOp::Project { child, .. } + | NonASAPOp::Filter { child, .. } + | NonASAPOp::Sort { child, .. } + | NonASAPOp::Limit { child, .. } + | NonASAPOp::Dedup { child, .. }, + ) + | Operator::ASAP(ASAPOp::FinalizeExactAccumulator { child }) => { + return self.keys_of_values(*child); + } + // A merge's children are states of one definition: its first + // build's keys stand for all of them. + Operator::ASAP(ASAPOp::SummaryMerge { + children, + group_by: None, + }) => return self.keys_of_values(*children.first()?), Operator::ASAP(ASAPOp::SummaryEstimate { summary_input, query, @@ -725,14 +747,5 @@ impl KeysOfValues { } } -/// The smaller of two upper bounds, either alone when the other is -/// unknown. -fn smaller_known(a: Option, b: Option) -> Option { - match (a, b) { - (Some(a), Some(b)) => Some(a.min(b)), - (a, b) => a.or(b), - } -} - #[cfg(test)] mod tests; diff --git a/crates/types/src/workload/bounds.rs b/crates/types/src/workload/bounds.rs index d43517f9c..4bc04f6e0 100644 --- a/crates/types/src/workload/bounds.rs +++ b/crates/types/src/workload/bounds.rs @@ -137,20 +137,25 @@ pub fn series_read(root: &QueryRoot, data: &DataWorkload) -> Option { Some(rows) } -/// How many values one evaluation of `root` returns at most (`G`): one for -/// a scalar root and for a root of at most one row (`at_most_one_row`), -/// provable from the query. Otherwise the smaller of two bounds, either -/// alone when the other is unknown: `series_read` ([`series_read`]) for a -/// PromQL instant vector, which bounds the values it can return (each -/// branch of an `or` adds its own), and, for a grouped root of any -/// language, the groups its keys' declared distinct values allow -/// (`declared_group_bound`), at most `series_read`. `None` when neither -/// applies: an ungrouped SQL result, a grouping with an undeclared key, a -/// range vector. +/// How many values one evaluation of `root` returns at most (`G`, $n_q$): +/// one for a scalar root and for a root of at most one row +/// (`at_most_one_row`), provable from the query. Otherwise the smaller of +/// two bounds, either alone when the other is unknown ([`smaller_known`]): +/// - `values_by_keys`, the memo's bound for the root's group +/// (`Memo::max_values_per_evaluation`): the product, over the scans its +/// group keys come from, of each key set's declared distinct values, +/// clamped to the rows its grouping reads; the rule that also bounds the +/// readout values a query reads, so `GROUP BY dst` with 300 `dst` values +/// declared gives 300, whatever groups of rows the table declares; +/// - for a PromQL instant vector, `series_read` ([`series_read`]), which +/// bounds the values it can return (each branch of an `or` adds its own). +/// +/// `None` when neither applies: an ungrouped SQL result, a grouping by an +/// undeclared key over rows of unknown count, a range vector. pub fn values_per_result( root: &QueryRoot, series_read: Option, - data: &DataWorkload, + values_by_keys: Option, ) -> Option { let node = match root { QueryRoot::Scalar(_) => return Some(1), @@ -160,185 +165,15 @@ pub fn values_per_result( let by_series = (node.result_kind == OperatorResultKind::InstantVector) .then_some(series_read) .flatten(); - // Each group holds at least one row read, so the series read also - // bound the groups. - let by_groups = declared_group_bound(node, data) - .map(|groups| series_read.map_or(groups, |series| groups.min(series))); - by_groups.or(by_series).map(|values| values.max(1)) -} - -/// An upper bound on the groups `node` returns, from the distinct values -/// the sources it reads declare for its group keys -/// (`SourceFacts::distinct_values_per_label` and -/// `distinct_values_per_label_set`): the smallest product of declared counts -/// covering the keys ([`declared_distinct_combinations`]). The grouping is -/// found below operators that only keep, drop or reorder rows, and through -/// a summary's estimate (not a top-k, which returns several rows per -/// group), merge and finalization. `None` when no such grouping is found, -/// it is a `without (...)`, or a key is not a source column passed through -/// unchanged covered by declared counts: the declared counts are upper -/// bounds, never defaults. -fn declared_group_bound(node: &OperatorNode, data: &DataWorkload) -> Option { - let grouped = |reduction: &Reduction, input: &OperatorNode| -> Option { - let Reduction::Reduce(keys) = reduction else { - return None; - }; - if keys.is_without() { - return None; - } - declared_distinct_combinations(input, keys.keys(), data) - }; - match &node.operator { - Operator::NonASAP(NonASAPOp::Aggregate { - reduction, - measures, - child, - .. - }) if !measures - .iter() - .any(|m| matches!(m, AggIntent::TopK { .. } | AggIntent::CountValues { .. })) => - { - grouped(reduction, child) - } - Operator::ASAP( - ASAPOp::SummaryAgg { - reduction, child, .. - } - | ASAPOp::SummaryTumblingWindowChain { - reduction, child, .. - }, - ) => grouped(reduction, child), - // A merge's children are states of one definition: its first - // build's grouping stands for all of them. - Operator::ASAP(ASAPOp::SummaryMerge { - children, - group_by: None, - .. - }) => match children.first().and_then(|first| first.summary_build()) { - Some( - ASAPOp::SummaryAgg { - reduction, child, .. - } - | ASAPOp::SummaryTumblingWindowChain { - reduction, child, .. - }, - ) => grouped(&reduction, &child), - _ => None, - }, - Operator::ASAP(ASAPOp::SummaryEstimate { - summary_input, - query, - }) if !matches!(query, SketchStatistic::TopK { .. }) => { - declared_group_bound(summary_input, data) - } - Operator::ASAP(ASAPOp::FinalizeExactAccumulator { child }) - | Operator::NonASAP( - NonASAPOp::Project { child, .. } - | NonASAPOp::Filter { child, .. } - | NonASAPOp::Sort { child, .. } - | NonASAPOp::Limit { child, .. } - | NonASAPOp::Dedup { child, .. }, - ) => declared_group_bound(child, data), - _ => None, - } + smaller_known(values_by_keys, by_series).map(|values| values.max(1)) } -/// The distinct combinations of values columns `columns` of `node` take -/// together at most, when each is a source column passed through -/// unchanged: traced down through operators that keep their input's -/// columns (filter, sort, limit, time range and shift), a projection that -/// selects them, a grouped aggregate's keys, a per-series reduction's other -/// columns, and each branch of a union (whose combinations add), to the -/// scans, where the source's declared label and label-set counts bound them -/// ([`SourceFacts::declared_distinct_combinations`]). A join is not traced: -/// it can pair values of different rows. `None` when a column is computed -/// or relabeled on the way (its values are no longer the source's), or a -/// source's declared counts do not cover the columns. -/// -/// [`SourceFacts::declared_distinct_combinations`]: super::SourceFacts::declared_distinct_combinations -fn declared_distinct_combinations( - node: &OperatorNode, - columns: &[usize], - data: &DataWorkload, -) -> Option { - match node.non_asap()? { - NonASAPOp::Scan { source, .. } => { - let labels = columns - .iter() - .map(|&column| Some(node.schema.fields.get(column)?.name.as_str())) - .collect::>>()?; - data.sources - .get(source_name(source))? - .declared_distinct_combinations(&labels) - } - NonASAPOp::Filter { child, .. } - | NonASAPOp::Sort { child, .. } - | NonASAPOp::Limit { child, .. } - | NonASAPOp::TimeRange { child, .. } - | NonASAPOp::TimeShift { child, .. } => { - declared_distinct_combinations(child, columns, data) - } - // A grouped aggregate returns its keys first, unchanged; but a - // `count_values` label named like a key takes that key's place - // with the stringified sample values. - NonASAPOp::Aggregate { - reduction: Reduction::Reduce(keys), - measures, - child, - .. - } if !keys.is_without() - && !measures - .iter() - .any(|m| matches!(m, AggIntent::CountValues { .. })) => - { - let inputs = columns - .iter() - .map(|&column| keys.keys().get(column).copied()) - .collect::>>()?; - declared_distinct_combinations(child, &inputs, data) - } - // A per-series reduction keeps every column but the one it reduces. - NonASAPOp::Aggregate { - reduction: Reduction::PerEntity, - measures, - child, - .. - } => { - let reduced = match measures.first()?.input_cols().first() { - Some(&input) => input, - None => crate::ir::scalar::column_resolution::resolve_column_ref( - &crate::ir::scalar::ColumnRef::SampleValue, - &child.schema, - ) - .ok()?, - }; - (!columns.contains(&reduced)) - .then(|| declared_distinct_combinations(child, columns, data)) - .flatten() - } - NonASAPOp::Project { cols, child, .. } => { - let inputs = columns - .iter() - .map(|&column| match cols.get(column)?.expr { - ScalarExpr::Column(input) => Some(input), - _ => None, - }) - .collect::>>()?; - declared_distinct_combinations(child, &inputs, data) - } - NonASAPOp::SetOp { - kind: RelationalSetOpKind::Union, - left, - right, - .. - } => Some( - declared_distinct_combinations(left, columns, data)? - .saturating_add(declared_distinct_combinations(right, columns, data)?), - ), - NonASAPOp::Concat { children, .. } => children.iter().try_fold(0u64, |total, child| { - Some(total.saturating_add(declared_distinct_combinations(child, columns, data)?)) - }), - _ => None, +/// The smaller of two upper bounds, either alone when the other is +/// unknown: `(Some(300), Some(96))` gives 96, `(None, Some(96))` 96. +pub(crate) fn smaller_known(a: Option, b: Option) -> Option { + match (a, b) { + (Some(a), Some(b)) => Some(a.min(b)), + (a, b) => a.or(b), } } diff --git a/crates/types/src/workload/data.rs b/crates/types/src/workload/data.rs index 75c6e509d..fe13441fd 100644 --- a/crates/types/src/workload/data.rs +++ b/crates/types/src/workload/data.rs @@ -282,10 +282,7 @@ impl SourceFacts { .zip(self.sample_interval) .filter(|(_, interval)| interval.0 > 0) .and_then(|(series, interval)| series.checked_mul(window_ms / interval.0 + 1)); - match (by_rate, self.rows_held) { - (Some(by_rate), Some(rows_held)) => Some(by_rate.min(rows_held)), - (by_rate, rows_held) => by_rate.or(rows_held), - } + super::bounds::smaller_known(by_rate, self.rows_held) } /// An upper bound on the distinct combinations of values `labels` take diff --git a/docs/design_docs/concepts/accuracy-models.md b/docs/design_docs/concepts/accuracy-models.md index b78148d9f..397465708 100644 --- a/docs/design_docs/concepts/accuracy-models.md +++ b/docs/design_docs/concepts/accuracy-models.md @@ -126,19 +126,23 @@ most 6 x 16 = 96 rows, so `R` = 96, and without a sampling interval or `rows_held` `R` is unbound. Keys from different scans multiply, since a join pairs their values: `flows JOIN hosts ... GROUP BY f.src, h.region` with 6 `src` and 6 `region` values declared returns up to 36 values, not 6 + 6. -With both unbound the target stays unmet. A grouped result's `G` is bound by -the distinct values its sources declare for its group keys -(`SourceFacts.distinct_values_per_label`, and -`distinct_values_per_label_set` for labels that depend on each other): the -smallest product of declared label and label-set counts covering the keys, -at most the series read. With each of 625 endpoints in one of 25 services -and `(service, endpoint)` declared at 625, `by (service, endpoint)` returns -at most 625 values, not 25 x 625. A key counts only when it is a source -column passed through unchanged; a relabeled or computed key leaves `G` -unbound. So a sketch meets a whole-result target over SQL groups when every -grouping column's distinct values are declared, and over 25 services and 25 -endpoints requires each group's sketch to meet `δ / 625`. Nothing declared -leaves `G` unbound; no default stands in for it. +With both unbound the target stays unmet. A grouped result's `G` follows +the same rule as `R` (`Memo::max_values_per_evaluation` of the root group): +the product, over the scans its group keys come from, of the distinct values +each scan's source declares for them (`SourceFacts.distinct_values_per_label`, +and `distinct_values_per_label_set` for labels that depend on each other: the +smallest product of declared label and label-set counts covering the keys), +clamped to the rows its grouping reads; a PromQL instant vector is also at +most the series read. With each of 625 endpoints in one of 25 services and +`(service, endpoint)` declared at 625, `by (service, endpoint)` returns at +most 625 values, not 25 x 625. `GROUP BY dst` over a table declaring 6 +groups of rows and 300 `dst` values returns up to 300 values, not 6. A key +counts only when it is a source column passed through unchanged; a relabeled +or computed key, or an undeclared one, leaves only the rows read to bound +`G`. So a sketch meets a whole-result target over SQL groups when every +grouping column's distinct values, or the rows read, are declared, and over +25 services and 25 endpoints requires each group's sketch to meet +`δ / 625`. Nothing declared leaves `G` unbound; no default stands in for it. An `AccuracyGuarantee` contains an error metric, an error bound and a failure probability (possibly unknown, as HLL's). The metric is essential: From b71e0533824118c21f72d954342b64aec2fd0c57 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 05:40:24 +0000 Subject: [PATCH 11/24] test(accuracy): a many-to-one match pairs two series' labels, an outer join adds a NULL, a LIMIT caps the rows m{side="l"} * on(x) group_left(instance) m{side="r"}, grouped by job and instance, pairs a left series' job with a right series' instance: up to 10 x 10 values, not the 10 pairs one series holds (also group_right and an offset side). flows LEFT JOIN hosts grouped by h.region returns the 6 regions and a NULL group, so the declared 6 do not bound it (also FULL JOIN and two keys). A grouping over LIMIT 10 returns at most 10 values. --- .../tests/pass2_promql/accuracy_and_grids.rs | 45 ++++++++++++++ crates/integration-tests/tests/pass2_sql.rs | 62 +++++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/crates/integration-tests/tests/pass2_promql/accuracy_and_grids.rs b/crates/integration-tests/tests/pass2_promql/accuracy_and_grids.rs index bd7e3b6db..aa63d1de8 100644 --- a/crates/integration-tests/tests/pass2_promql/accuracy_and_grids.rs +++ b/crates/integration-tests/tests/pass2_promql/accuracy_and_grids.rs @@ -143,3 +143,48 @@ fn a_promql_result_returns_at_most_its_keys_declared_values_or_its_series() { assert_eq!(values_per_evaluation(by_job, None), Some(1000)); assert_eq!(values_per_evaluation("rate(m[5m])", None), Some(1000)); } + +/// A many-to-one match between two selectors of one metric pairs labels of +/// different series: `m{side="l"} * on(x) group_left(instance) +/// m{side="r"}` keeps each left series' `job` and copies `instance` from +/// the right series with the same `x`. `m` declares 200 series, 10 `job` +/// and 10 `instance` values, and 10 (`job`, `instance`) pairs; the left +/// series `(job_a, instance_a, x = 10a + b)` and the right series +/// `(job_b, instance_b, x = 10a + b)` give the output pairs +/// `(job_a, instance_b)`: 100, not the 10 pairs one series holds. +/// `group_right(job)` and an `offset` on one side pair them the same way. +#[test] +fn a_many_to_one_match_within_one_metric_returns_up_to_the_product_of_both_sides_labels() { + let values_per_evaluation = |query: &str| { + promql_pass2_over( + &[( + query, + json!({"max_error": 0.05, "failure_probability": 0.01, "accuracy_metric": "relative"}), + )], + json!({"m": { + "series_count": 200, + "metric_type": "gauge", + "sample_interval": 15_000, + "distinct_values_per_label": {"job": 10, "instance": 10}, + "distinct_values_per_label_set": [ + {"labels": ["job", "instance"], "distinct_values": 10}, + ], + }}), + ) + .memo + .queries()[0] + .number_of_values_per_query_evaluation + }; + for query in [ + r#"quantile by (job, instance) (0.5, m{side="l"} * on(x) group_left(instance) m{side="r"})"#, + r#"quantile by (job, instance) (0.5, m{side="l"} * on(x) group_right(job) m{side="r"})"#, + r#"quantile by (job, instance) (0.5, m * on(x) group_left(instance) m offset 1h)"#, + ] { + assert_eq!(values_per_evaluation(query), Some(100), "{query}"); + } + // One series' own labels still count once: 10 pairs. + assert_eq!( + values_per_evaluation("quantile by (job, instance) (0.5, m)"), + Some(10) + ); +} diff --git a/crates/integration-tests/tests/pass2_sql.rs b/crates/integration-tests/tests/pass2_sql.rs index ea132076f..6d68372b5 100644 --- a/crates/integration-tests/tests/pass2_sql.rs +++ b/crates/integration-tests/tests/pass2_sql.rs @@ -319,3 +319,65 @@ fn a_result_grouped_by_keys_of_joined_scans_returns_up_to_their_product() { assert_eq!(values_per_evaluation(&join(None, None)), Some(36)); assert_eq!(values_per_evaluation(&join(Some(6), Some(5))), Some(30)); } + +/// `flows` holding 96 rows, sampled every minute (6 groups of rows, 300 +/// `dst` values), and `hosts` holding 6 rows (6 hosts, 6 regions), as +/// [`sql_pass2_over`] declares them, for the outer-join and `LIMIT` tests. +fn flows_and_six_hosts() -> serde_json::Value { + json!({ + "flows": { + "series_count": 6, + "rows_held": 96, + "sample_interval": 60_000, + "distinct_values_per_label": {"src": 6, "dst": 300}, + }, + "hosts": { + "series_count": 6, + "rows_held": 6, + "distinct_values_per_label": {"host": 6, "region": 6}, + }, + }) +} + +/// An outer join adds a NULL to the columns of its nullable side: `flows f +/// LEFT JOIN hosts h ... GROUP BY h.region` returns the 6 declared regions +/// and a NULL group for the flows no host matches, 7 values, so the +/// declared 6 do not bound it. The key counts as computed and the rows the +/// grouping reads bound it: 96 `flows` rows and 6 `hosts` rows, at most +/// 96 × 6 pairs plus each side's own rows, 678. A +/// `FULL JOIN` makes both sides nullable, so `GROUP BY f.src, h.region` is +/// bounded the same way, not by 6 × 6. +#[test] +fn a_key_from_the_nullable_side_of_an_outer_join_is_bounded_by_the_rows_read() { + for join in ["LEFT JOIN", "FULL JOIN"] { + for keys in ["h.region", "f.src, h.region"] { + let query = format!( + "SELECT {keys}, approx_percentile_cont(f.bytes, 0.9) AS q \ + FROM flows f {join} hosts h ON f.dst = h.host \ + WHERE f.ts >= now() - INTERVAL '15 minutes' GROUP BY {keys}" + ); + let logical = whole_result_quantile(&query, flows_and_six_hosts()); + assert_eq!(values_per_evaluation(&logical), Some(678), "{query}"); + } + } + // An inner join adds no NULL: 6 × 6 pairs. + let inner = whole_result_quantile( + "SELECT f.src, h.region, approx_percentile_cont(f.bytes, 0.9) AS q \ + FROM flows f JOIN hosts h ON f.dst = h.host \ + WHERE f.ts >= now() - INTERVAL '15 minutes' GROUP BY f.src, h.region", + flows_and_six_hosts(), + ); + assert_eq!(values_per_evaluation(&inner), Some(36)); +} + +/// A `LIMIT 10` below the grouping keeps at most 10 rows, so the result +/// holds at most 10 `dst` values, not the 96 rows the window holds. +#[test] +fn a_grouping_over_a_limit_returns_at_most_its_rows() { + let logical = whole_result_quantile( + "SELECT dst, approx_percentile_cont(b, 0.9) AS q FROM (SELECT dst, bytes AS b \ + FROM flows WHERE ts >= now() - INTERVAL '15 minutes' LIMIT 10) t GROUP BY dst", + flows_and_six_hosts(), + ); + assert_eq!(values_per_evaluation(&logical), Some(10)); +} From f3462087ae850ad144790d04afca7cfea29f3268 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 05:40:30 +0000 Subject: [PATCH 12/24] fix(accuracy): shift a many-to-one match's other side past the output side's scans; an outer join's nullable side is computed A group_left/group_right match pairs each output series with one series of the other side, as a join pairs rows: the labels it copies from that side now move past the output side's scan ordinals (after_left_scans, as for a join), so job and instance of two series of one metric multiply instead of sharing one label-set count. The columns of an outer join's nullable side (Left: right, Right: left, Full: both) gain a NULL their source may not hold, so their lineage is computed by the join; a key from them is bounded by the rows read. LIMIT n over the whole input keeps at most n rows. column_properties now lints wildcard enum arms: every lineage and sign rule lists each variant, so a new operator states its rule. --- .../stages/logical/group/column_properties.rs | 144 +++++++++++++++--- crates/types/src/stages/logical/memo/mod.rs | 32 +++- 2 files changed, 150 insertions(+), 26 deletions(-) diff --git a/crates/types/src/stages/logical/group/column_properties.rs b/crates/types/src/stages/logical/group/column_properties.rs index 30115e8d2..69fa305f5 100644 --- a/crates/types/src/stages/logical/group/column_properties.rs +++ b/crates/types/src/stages/logical/group/column_properties.rs @@ -8,6 +8,16 @@ //! contracts that hold only for values of one sign read the sign; //! aggregate coverage names a selection's columns by the lineage. +// Each rule here answers per operator (or per expression, measure or +// operator kind) and lists every variant: a wildcard arm would give a new +// operator a default lineage, which bounds and coverage then trust, +// instead of failing the build (under `-D warnings`). `matches!` and `let +// ... else` stay only for a check of one variant's identity. +#![warn( + clippy::wildcard_enum_match_arm, + clippy::match_wildcard_for_single_variants +)] + use std::collections::BTreeMap; use super::{MemoGroupAsInput, MemoGroupId}; @@ -74,9 +84,10 @@ pub enum ColumnLineage { /// Copied unchanged from a column of one scan of a source. SourceColumn(SourceColumn), /// Column `column` of memo group `group`, where it was computed: a - /// measure, an expression, a summary state or a readout, or a column + /// measure, an expression, a summary state or a readout, a column /// the inputs of a union-like operator (`SetOp`, `Concat`, - /// `SummaryMerge`) disagree on. + /// `SummaryMerge`) disagree on, or a column of an outer join's nullable + /// side, which holds a NULL its source may not. Computed { group: MemoGroupId, column: ColumnId, @@ -176,11 +187,14 @@ fn columns_of(input: &Input<'_>) -> Vec { .collect() } -/// `right`, the right side's columns of a `Join`, which concatenates them -/// after `left`'s: a source column of a source `left` also -/// scans moves to the scans after `left`'s (its scan ordinal shifted by -/// `left`'s largest for that source, plus one), so a self-join's two sides -/// name different scans. +/// `right`, the columns of an input whose rows pair with `left`'s (a +/// `Join`'s right side, or the "one" side of a many-to-one vector match), +/// moved to the scans after `left`'s: a source column of a source `left` +/// also scans gets its scan ordinal shifted by `left`'s largest for that +/// source, plus one. Paired rows can be any two rows of the source, so the +/// two sides name different scans: a self-join's `a.src` and `b.src`, or +/// `job` and `instance` of `m{side="l"} * on(x) group_left(instance) +/// m{side="r"}`, each pair taken from a different series. fn after_left_scans( left: &[ColumnProperties], mut right: Vec, @@ -273,12 +287,32 @@ fn non_asap_columns( } aggregate_columns(child, reduction, measures, output) } + // The left side's columns, then the right side's after the left's + // scans. An outer join's nullable side holds a NULL its source may + // not: `flows f LEFT JOIN hosts h` gives `h.region` NULL for a flow + // no host matches. Its columns no longer hold their source column's + // values, so they are computed by the join. Join { kind, left, right, .. } => { + let (left_nullable, right_nullable, right_kept) = match kind { + JoinKind::Inner | JoinKind::Cross => (false, false, true), + JoinKind::Left => (false, true, true), + JoinKind::Right => (true, false, true), + JoinKind::Full => (true, true, true), + JoinKind::Semi | JoinKind::Anti => (false, false, false), + }; let mut columns = columns_of(left); - if !matches!(kind, JoinKind::Semi | JoinKind::Anti) { - let right = after_left_scans(&columns, columns_of(right)); + let mut right = after_left_scans(&columns, columns_of(right)); + for (columns, nullable) in [(&mut columns, left_nullable), (&mut right, right_nullable)] + { + if nullable { + for column in columns.iter_mut() { + column.lineage = None; + } + } + } + if right_kept { columns.extend(right); } columns @@ -298,13 +332,30 @@ fn non_asap_columns( // one side lacks is computed). A label // `group_left(...)` names comes from the right ("one") side and one // `group_right(...)` names from the left, overwriting a column of - // that name. The output does not concatenate both sides' columns, - // so no scan ordinal shifts. + // that name. Each output series pairs a series of the output side + // with one of the "one" side, as a join pairs rows, so the "one" + // side's columns move past the output side's scans + // ([`after_left_scans`]): a label of each side, of one metric, can + // pair values no one series holds. BinaryOp { operator, lhs, rhs, .. } => { - let left = columns_of(lhs); - let right = columns_of(rhs); + let grouping = operator + .vector_match + .as_ref() + .and_then(|m| m.grouping.as_ref()); + let (left, right) = match grouping.map(|grouping| &grouping.side) { + None => (columns_of(lhs), columns_of(rhs)), + Some(GroupSide::Left) => { + let left = columns_of(lhs); + let right = after_left_scans(&left, columns_of(rhs)); + (left, right) + } + Some(GroupSide::Right) => { + let right = columns_of(rhs); + (after_left_scans(&right, columns_of(lhs)), right) + } + }; let named = |input: &Input<'_>, columns: &[ColumnProperties], name: &str| { input .schema() @@ -312,10 +363,6 @@ fn non_asap_columns( .and_then(|column| columns.get(column).cloned()) .unwrap_or_default() }; - let grouping = operator - .vector_match - .as_ref() - .and_then(|m| m.grouping.as_ref()); let output_side_is_right = grouping.is_some_and(|grouping| grouping.side == GroupSide::Right); let is_or = matches!(operator.kind, BinaryOpKind::Set(PromQLVectorSetOpKind::Or)); @@ -512,7 +559,25 @@ pub(super) fn project_lineage( cols.iter() .map(|item| match item.expr { ScalarExpr::Column(column) => input.get(column).cloned().flatten(), - _ => None, + ScalarExpr::Literal(_) + | ScalarExpr::Negative { .. } + | ScalarExpr::Compare { .. } + | ScalarExpr::BoolAnd(_) + | ScalarExpr::BoolOr(_) + | ScalarExpr::Not(_) + | ScalarExpr::IsNull(_) + | ScalarExpr::IsNotNull(_) + | ScalarExpr::Cast { .. } + | ScalarExpr::InList { .. } + | ScalarExpr::FunctionCall { .. } + | ScalarExpr::Arithmetic { .. } + | ScalarExpr::Case { .. } + | ScalarExpr::CurrentTimestamp + | ScalarExpr::EvalTimestamp + | ScalarExpr::PromqlScalarFromVector(_) + | ScalarExpr::ScalarSubquery(_) + | ScalarExpr::Exists { .. } + | ScalarExpr::InSubquery { .. } => None, }) .collect() } @@ -540,7 +605,22 @@ fn expression_sign(expr: &ScalarExpr, input: &[ValueSign]) -> ValueSign { { NonNegative } - _ => Unknown, + ScalarExpr::Literal(_) + | ScalarExpr::Compare { .. } + | ScalarExpr::BoolAnd(_) + | ScalarExpr::BoolOr(_) + | ScalarExpr::Not(_) + | ScalarExpr::IsNull(_) + | ScalarExpr::IsNotNull(_) + | ScalarExpr::InList { .. } + | ScalarExpr::FunctionCall { .. } + | ScalarExpr::Case { .. } + | ScalarExpr::CurrentTimestamp + | ScalarExpr::EvalTimestamp + | ScalarExpr::PromqlScalarFromVector(_) + | ScalarExpr::ScalarSubquery(_) + | ScalarExpr::Exists { .. } + | ScalarExpr::InSubquery { .. } => Unknown, } } @@ -612,7 +692,31 @@ fn measure_sign(measure: &AggIntent, input: ValueSign) -> ValueSign { // A counter's rate and increase are non-negative; over a value of // another sign a decrease reads as a reset, so no sign is known. Rate | IRate | Increase if input == NonNegative => NonNegative, - _ => Unknown, + PearsonCorr { .. } + | TopK { .. } + | Rate + | IRate + | Increase + | Delta + | IDelta + | Deriv + | PredictLinear { .. } + | DoubleExpSmoothing { .. } + | HistogramCount + | HistogramSum + | HistogramAvg + | HistogramStdDev + | HistogramStdVar + | HistogramFraction { .. } + | HistogramQuantile { .. } + | Math(_) + | TimeFn(_) + | MadOverTime + | TsOfMinOverTime + | TsOfMaxOverTime + | TsOfFirstOverTime + | TsOfLastOverTime + | Extension { .. } => Unknown, } } diff --git a/crates/types/src/stages/logical/memo/mod.rs b/crates/types/src/stages/logical/memo/mod.rs index f6be90406..86037c453 100644 --- a/crates/types/src/stages/logical/memo/mod.rs +++ b/crates/types/src/stages/logical/memo/mod.rs @@ -94,8 +94,9 @@ impl Memo { } /// The most values one evaluation of `group`, a readout, a state, a - /// grouped aggregate or a query result over one, returns, from the facts the data workload declares; `None` when they - /// bound it in no way. As SQL optimizers estimate the groups of a + /// grouped aggregate or a query result over one, returns, from the + /// facts the data workload declares; `None` when they bound it in no + /// way. As SQL optimizers estimate the groups of a /// `GROUP BY` (the product of the key columns' distinct counts, clamped /// to the input rows), but as an upper bound, it is the smaller of: /// - the values its keys can take ([`Self::keys_of_values`]): for keys @@ -116,7 +117,12 @@ impl Memo { /// /// Keys from different scans multiply, for a join pairs their values: /// `flows JOIN hosts ... GROUP BY f.src, h.region`, with 6 `src` and 6 - /// `region` values declared, returns up to 36 values, not 6 + 6. + /// `region` values declared, returns up to 36 values, not 6 + 6. The + /// "one" side of a many-to-one vector match is another scan in the same + /// way (`m{side="l"} * on(x) group_left(instance) m{side="r"}` by + /// `job, instance` pairs labels of two series), and a key from an outer + /// join's nullable side, which may be NULL, is computed + /// (`ColumnLineage`). /// /// Examples: a readout of `quantile_over_time(0.9, m[5m])` over `m` /// with 1000 series returns at most 1000 values, which `max(...)` over @@ -172,8 +178,9 @@ impl Memo { /// `TimeRange` over it the rows of its window /// (`SourceFacts::max_rows_in_window`; for an instant selector, one per /// series at most); an operator that keeps, drops or groups its input's - /// rows at most its input's; a join at most the product of its inputs' - /// (plus each side's own for an outer join), a union their sum. + /// rows at most its input's, and `LIMIT n` over the whole input at most + /// n; a join at most the product of its inputs' (plus each side's own + /// for an outer join), a union their sum. /// Example: `flows WHERE ts >= now() - INTERVAL '15 minutes'`, 6 groups /// of rows sampled every minute, returns at most 6 × 16 = 96 rows. fn max_rows_per_evaluation(&self, group: MemoGroupId) -> Option { @@ -210,12 +217,25 @@ impl Memo { | NonASAPOp::Project { child, .. } | NonASAPOp::Dedup { child, .. } | NonASAPOp::Sort { child, .. } - | NonASAPOp::Limit { child, .. } | NonASAPOp::SQLWindowFunc { child, .. } | NonASAPOp::TimeShift { child, .. } | NonASAPOp::PromqlRelabel { child, .. } | NonASAPOp::PromqlInfoEnrich { child, .. } | NonASAPOp::PromqlSeriesSample { child, .. } => rows(child), + // `LIMIT n` over the whole input keeps at most n rows; per + // partition (`topk by (job)`), n per partition. + NonASAPOp::Limit { + n, + partition_by, + child, + .. + } => { + let whole_input = !partition_by.is_without() && partition_by.keys().is_empty(); + let kept = n + .filter(|_| whole_input) + .and_then(|n| u64::try_from(n).ok()); + smaller_known(kept, rows(child)) + } // A global aggregate returns one row even over no rows. NonASAPOp::Aggregate { child, .. } => rows(child).map(|rows| rows.max(1)), NonASAPOp::Join { From bc2a1b5e7779129321f11e8df9002b85c859e63d Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 05:58:49 +0000 Subject: [PATCH 13/24] test(accuracy): check the memo's value bounds against execution on data the declared facts allow Each review round found another shape where Memo::max_values_per_evaluation (the readout values R and a query's n_q) fell below the values a query can return. This test checks the bounds against data directly: each query of a corpus is lowered as planning lowers it, its Stage 0 memo built, and the query run by the executor over datasets that keep to the declared source facts (series, distinct values per label and label set, sample interval, rows held). Worst-case seekers make the labels the facts bound only together independent, crossed or aligned across series and rows, replace series between the times a query reads, and split series into blocks by literal labels so matches pair them one to one; random datasets add more. Every bounded group's result rows, and the query's, must be at most its bound. The default corpus is the reviewed probe shapes (85 queries, 9 s); an ignored variant runs larger datasets over the dag-viewer workloads and a manifest of workloads. Cases the executor or the frontend cannot run yet are listed by name, so a case joins the check once it can run. --- crates/integration-tests/tests/main.rs | 1 + .../tests/memo_value_bounds.rs | 1260 +++++++++++++++++ .../memo_value_bounds_fixtures/cases.json | 835 +++++++++++ 3 files changed, 2096 insertions(+) create mode 100644 crates/integration-tests/tests/memo_value_bounds.rs create mode 100644 crates/integration-tests/tests/memo_value_bounds_fixtures/cases.json diff --git a/crates/integration-tests/tests/main.rs b/crates/integration-tests/tests/main.rs index 768f0c362..43b720fd7 100644 --- a/crates/integration-tests/tests/main.rs +++ b/crates/integration-tests/tests/main.rs @@ -19,6 +19,7 @@ mod independent_plan_seed; mod joint_search_equivalence; mod kll_tumbling_window_execution; mod materialized_views; +mod memo_value_bounds; mod nested; mod operator_design_examples; mod operator_sharing; diff --git a/crates/integration-tests/tests/memo_value_bounds.rs b/crates/integration-tests/tests/memo_value_bounds.rs new file mode 100644 index 000000000..9d01b4908 --- /dev/null +++ b/crates/integration-tests/tests/memo_value_bounds.rs @@ -0,0 +1,1260 @@ +//! The memo's value bounds against execution: every bound the memo gives +//! on the values one evaluation of a group returns +//! (`Memo::max_values_per_evaluation`: the readout values R a whole-result +//! failure probability splits over, and the bound on any grouped result) +//! and on a query's values (n_q) must hold on every dataset the declared +//! source facts allow. Each query of a corpus is lowered as planning lowers +//! it, its Stage 0 memo built, and the query run by the executor over +//! small datasets that keep to the facts (`series_count`, the distinct +//! values per label and per label set, `sample_interval`, `rows_held`); +//! each group's result rows must be at most its bound. The datasets seek +//! the worst case: the labels and columns the facts bound only together +//! take independent, crossed or aligned values across series and rows, +//! series can be replaced between the times a query reads (`offset`), and +//! labels compared with literals split the series into blocks that a match +//! pairs one to one. So a bound that takes two scans for one (a join, or a +//! many-to-one match of one metric), misses a NULL an outer join adds, or +//! reads a declared count where none applies fails here, whichever +//! operator it comes from. +//! +//! Example: `quantile by (job, instance) (0.5, m{side="l"} * on(x) +//! group_left(instance) m{side="r"})` over `m` declaring 200 series and 10 +//! (`job`, `instance`) pairs: the left series of a block take random pairs +//! and the right series of the same `x` other random pairs, so the result +//! holds up to 100 (`job`, `instance`) values; a bound of 10 fails. +//! +//! A readout's R is its group's bound; the readout reads the values the +//! group's exact aggregate returns, which the executor counts here. + +use std::collections::{BTreeMap, BTreeSet, HashMap}; +use std::panic::{catch_unwind, AssertUnwindSafe}; +use std::rc::Rc; +use std::sync::Arc; + +use asap_devtools::planning_stages::{lower_promql_roots, lower_sql_roots, sql_catalog}; +use asap_executor::physical_planner::{compile, promql_rows}; +use asap_executor::runtime::{Limits, RunContext, Scope}; +use asap_executor::sources::InMemoryDataStore; +use asap_executor::values::{Batch, SchemaRef, Value}; +use asap_types::ir::operator::Source; +use asap_types::ir::schema::{DataType, FieldDataType}; +use asap_types::ir::{NonASAPOp, Operator, OperatorNode, QueryRoot}; +use asap_types::plan::physical_asap_dag::PhysicalASAPOperatorPayload; +use asap_types::stages::{Memo, MemoGroupId}; +use asap_types::workload::{DataWorkload, PlanningWorkload, SourceFacts}; +use futures::executor::block_on; +use futures::TryStreamExt; +use serde_json::{json, Value as Json}; + +/// When every query is evaluated: late enough for any lookback. +const EVALUATION_MS: i64 = 30 * 86_400_000; + +/// The default corpus: the query shapes each bound rule was reviewed +/// against, over the facts they were reviewed with. +const CASES: &str = include_str!("memo_value_bounds_fixtures/cases.json"); + +/// How large and how many datasets each query is run over. +#[derive(Clone, Copy)] +struct DatasetSizes { + /// The most series (PromQL) or groups of rows (SQL) a source gets; a + /// source declaring more gets this many, which its facts allow. + max_units: u64, + /// The most samples per series, or rows per group of rows. + max_samples_per_unit: u64, + /// Datasets per query beyond the fixed worst-case seekers. + random_datasets: u64, +} + +const DEFAULT_SIZES: DatasetSizes = DatasetSizes { + max_units: 240, + max_samples_per_unit: 20, + random_datasets: 6, +}; + +const LARGE_SIZES: DatasetSizes = DatasetSizes { + max_units: 1200, + max_samples_per_unit: 40, + random_datasets: 40, +}; + +/// The default corpus's cases the executor cannot run yet, each with the +/// operator it lacks. A case leaves this list once the executor runs it, +/// so the corpus checks every shape the executor can run. +const EXECUTOR_GAPS: &[(&str, &str)] = &[ + ("c_cast_num", "CAST to BIGINT"), + ("c_cast_ts", "CAST to BIGINT"), + ("c_sub_alias", "date_trunc"), + ("c_sub_upper", "upper"), + ("p_hist", "rate"), + ("p_hist_qot", "rate"), + ("p_hist_whole", "rate"), + ("p_labelrep_other", "label_replace"), + ("p_labelrep_same", "label_replace"), + ("p_labelrep_whole", "label_replace"), + ("p_subq", "a subquery"), + ("s1_bucket_nosc", "date_trunc"), + ("s1_bucket_sc", "date_trunc"), + ("s2_src_bucket_sc", "date_trunc"), + ("u_union_mixed", "UNION ALL below a grouping"), + ("u_union_same", "UNION ALL below a grouping"), + ("w_window_key", "row_number() as a key"), +]; + +/// The default corpus's cases the frontend does not lower. +const FRONTEND_GAPS: &[&str] = &[ + // `src || dst`: string concatenation. + "g_expr", + // `offset` on a function of a range selector. + "p9_offset_or", +]; + +/// Every memo bound of the default corpus holds on every dataset; every +/// case runs on at least one dataset, but for the executor's and the +/// frontend's known gaps. +#[test] +fn memo_value_bounds_hold_on_data_the_declared_facts_allow() { + let report = check_corpus(&default_corpus(), DEFAULT_SIZES); + report.assert_sound(); + let never_run: Vec<&str> = report + .never_executed + .iter() + .map(|case| case.split(':').next().unwrap()) + .collect(); + let gaps: Vec<&str> = EXECUTOR_GAPS.iter().map(|(case, _)| *case).collect(); + assert_eq!(never_run, gaps, "{:#?}", report.never_executed); + assert_eq!(report.not_lowered, FRONTEND_GAPS); +} + +/// [`memo_value_bounds_hold_on_data_the_declared_facts_allow`] over larger +/// and more datasets, and also over the checked-in dag-viewer workloads +/// and the workloads `ASAP_MEMO_VALUE_BOUNDS_WORKLOADS` lists (lines of +/// `name|workload.json|tables.json`, the tables optional). +#[test] +#[ignore = "minutes: larger datasets over every corpus"] +fn memo_value_bounds_hold_on_data_the_declared_facts_allow_over_every_corpus() { + let mut corpus = default_corpus(); + corpus.extend(dag_viewer_corpus()); + if let Ok(manifest) = std::env::var("ASAP_MEMO_VALUE_BOUNDS_WORKLOADS") { + corpus.extend(manifest_corpus(&manifest)); + } + let report = check_corpus(&corpus, LARGE_SIZES); + report.assert_sound(); +} + +/// One workload of the corpus and the SQL tables it reads. +struct Case { + name: String, + workload: PlanningWorkload, + tables: Vec, +} + +fn default_corpus() -> Vec { + let corpus: Json = serde_json::from_str(CASES).unwrap(); + let tables: Vec = corpus["tables"] + .as_array() + .unwrap() + .iter() + .map(Json::to_string) + .collect(); + corpus["cases"] + .as_array() + .unwrap() + .iter() + .map(|case| { + let language = match case["language"].as_str().unwrap() { + "promql" => json!("promql"), + _ => json!({"sql": "datafusion_sql"}), + }; + let workload = json!({ + "query_workload": { + "language": language, + "repeating_queries": [{ + "query": case["query"], + "demand": {"fixed_interval_at": {"interval": 60_000, "evaluation_phase": 0}}, + "requirements": {"max_latency_ms": null, "accuracy": { + "max_error": 0.05, "failure_probability": 0.01, + "accuracy_metric": "relative", "accuracy_scope": "whole_result", + }}, + "predictability": {"predictable": {"known_at": null}}, + }], + }, + "data_workload": { + "arrival": {"ingesting": {"rows_per_second": 1000.0}}, + "sources": corpus["facts"][case["facts"].as_str().unwrap()], + }, + }); + Case { + name: case["name"].as_str().unwrap().to_string(), + workload: serde_json::from_value(workload).unwrap(), + tables: tables.clone(), + } + }) + .collect() +} + +/// The dag-viewer's checked-in workloads, each with the tables beside it. +fn dag_viewer_corpus() -> Vec { + let root = concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../tools/dag-viewer/workloads" + ); + let mut cases = Vec::new(); + for set in std::fs::read_dir(root).unwrap().flatten() { + if !set.path().is_dir() { + continue; + } + let tables = std::fs::read_to_string(set.path().join("tables.json")) + .ok() + .map(|text| table_strings(&text)) + .unwrap_or_default(); + let mut files: Vec<_> = std::fs::read_dir(set.path()) + .unwrap() + .flatten() + .map(|file| file.path()) + .filter(|path| path.file_name().is_some_and(|name| name != "tables.json")) + .collect(); + files.sort(); + for file in files { + let Ok(workload) = serde_json::from_str(&std::fs::read_to_string(&file).unwrap()) + else { + continue; + }; + cases.push(Case { + name: file.display().to_string(), + workload, + tables: tables.clone(), + }); + } + } + cases +} + +/// The workloads a manifest of `name|workload.json|tables.json` lines lists. +fn manifest_corpus(manifest: &str) -> Vec { + std::fs::read_to_string(manifest) + .unwrap() + .lines() + .filter(|line| !line.trim().is_empty()) + .filter_map(|line| { + let fields: Vec<&str> = line.split('|').collect(); + let workload = serde_json::from_str(&std::fs::read_to_string(fields[1]).ok()?).ok()?; + let tables = fields + .get(2) + .filter(|path| !path.is_empty()) + .and_then(|path| std::fs::read_to_string(path).ok()) + .map(|text| table_strings(&text)) + .unwrap_or_default(); + Some(Case { + name: fields[0].to_string(), + workload, + tables, + }) + }) + .collect() +} + +/// Each table of a JSON array of tables, as `sql_catalog` reads it. +fn table_strings(text: &str) -> Vec { + let tables: Json = serde_json::from_str(text).unwrap(); + tables + .as_array() + .unwrap() + .iter() + .map(Json::to_string) + .collect() +} + +/// A bound one dataset broke: the case, the dataset, the group (or the +/// query result), its bound and the values the executor returned. +struct Violation { + case: String, + query: usize, + dataset: String, + what: String, + bound: u64, + observed: u64, +} + +#[derive(Default)] +struct Report { + cases: usize, + bounds_checked: usize, + executions: usize, + rejected_datasets: usize, + never_executed: Vec, + not_lowered: Vec, + violations: Vec, +} + +impl Report { + fn assert_sound(&self) { + println!( + "memo value bounds: {} cases, {} bounds, {} executions, {} datasets the executor \ + rejected, {} cases never run, not lowered: {:?}, {} violations", + self.cases, + self.bounds_checked, + self.executions, + self.rejected_datasets, + self.never_executed.len(), + self.not_lowered, + self.violations.len() + ); + let mut first_per_bound: BTreeMap<(String, usize, String), &Violation> = BTreeMap::new(); + for violation in &self.violations { + first_per_bound + .entry(( + violation.case.clone(), + violation.query, + violation.what.clone(), + )) + .or_insert(violation); + } + for violation in first_per_bound.values() { + println!( + "VIOLATION {} query {}: {} bounded by {}, {} on dataset {}", + violation.case, + violation.query, + violation.what, + violation.bound, + violation.observed, + violation.dataset + ); + } + assert!( + first_per_bound.is_empty(), + "{} memo bounds fall below the values the executor returned", + first_per_bound.len() + ); + } +} + +/// Every case of `corpus` checked, the cases spread over threads. +fn check_corpus(corpus: &[Case], sizes: DatasetSizes) -> Report { + let threads = std::thread::available_parallelism().map_or(4, |n| n.get().min(8)); + let reports: Vec = std::thread::scope(|scope| { + let workers: Vec<_> = (0..threads) + .map(|thread| { + scope.spawn(move || { + let mut report = Report::default(); + for case in corpus.iter().skip(thread).step_by(threads) { + report.cases += 1; + check_case(case, sizes, &mut report); + } + report + }) + }) + .collect(); + workers + .into_iter() + .map(|worker| worker.join().unwrap()) + .collect() + }); + let mut report = Report::default(); + for part in reports { + report.cases += part.cases; + report.bounds_checked += part.bounds_checked; + report.executions += part.executions; + report.rejected_datasets += part.rejected_datasets; + report.never_executed.extend(part.never_executed); + report.not_lowered.extend(part.not_lowered); + report.violations.extend(part.violations); + } + report.never_executed.sort(); + report.not_lowered.sort(); + report +} + +/// One query's bounds: each group of its memo with a bound, as the node it +/// lowered to, and its own n_q. +struct QueryBounds { + root: Rc, + /// (what is bounded, the node computing it, its bound) + bounds: Vec<(String, Rc, u64)>, +} + +fn check_case(case: &Case, sizes: DatasetSizes, report: &mut Report) { + let data = case.workload.data_workload.clone().unwrap_or_default(); + let roots = match lower(case) { + Ok(roots) => roots, + Err(_) => { + report.not_lowered.push(case.name.clone()); + return; + } + }; + let Ok(query_memo) = asap_stage0_memo::memo_of_queries(&roots, &vec![None; roots.len()], &data) + else { + report.not_lowered.push(case.name.clone()); + return; + }; + let mut memo = Memo::new(&data); + let mut group_of_node = HashMap::new(); + let mut ran_once = false; + let mut bounded = false; + let mut last_rejection = String::new(); + for (index, root) in roots.iter().enumerate() { + let QueryRoot::Operator(root) = root else { + continue; + }; + let mut bounds = Vec::new(); + let mut seen = BTreeSet::new(); + collect_bounds(root, &mut memo, &mut group_of_node, &mut seen, &mut bounds); + if let Some(values) = query_memo.queries()[index].number_of_values_per_query_evaluation { + bounds.push(("n_q".to_string(), root.clone(), values)); + } + if bounds.is_empty() { + continue; + } + bounded = true; + let query = QueryBounds { + root: root.clone(), + bounds, + }; + report.bounds_checked += query.bounds.len(); + // The bounds whose nodes the executor compiles: all together, else + // each alone. + let all: Vec = (0..query.bounds.len()).collect(); + let runnable: Vec = match compiles(&query, &all) { + Ok(()) => all, + Err(reason) => { + last_rejection = reason; + all.into_iter() + .filter(|&bound| compiles(&query, &[bound]).is_ok()) + .collect() + } + }; + if runnable.is_empty() { + continue; + } + for dataset in datasets(case, &query.root, &data, sizes) { + match execute_counts(&query, &runnable, &dataset) { + Ok(counts) => { + ran_once = true; + report.executions += 1; + for (&bound_index, observed) in runnable.iter().zip(counts) { + let (what, _, bound) = &query.bounds[bound_index]; + if observed > *bound { + report.violations.push(Violation { + case: case.name.clone(), + query: index, + dataset: dataset.name.clone(), + what: what.clone(), + bound: *bound, + observed, + }); + } + } + } + Err(reason) => { + report.rejected_datasets += 1; + last_rejection = reason; + } + } + } + } + if bounded && !ran_once { + report + .never_executed + .push(format!("{}: {last_rejection}", case.name)); + } +} + +fn lower(case: &Case) -> Result, String> { + match &case.workload.query_workload.language { + asap_types::workload::QueryLanguage::SQL(_) => { + let (catalog, _) = sql_catalog(&case.tables)?; + lower_sql_roots(&case.workload, &catalog) + } + _ => lower_promql_roots(&case.workload), + } +} + +/// Each node below `node` (itself included) whose group has a bound, with +/// its group, as Stage 0 adds them ([`Memo::find_or_add_group`]); the +/// group of `node`. +fn collect_bounds( + node: &Rc, + memo: &mut Memo, + group_of_node: &mut HashMap<*const OperatorNode, MemoGroupId>, + seen: &mut BTreeSet<*const OperatorNode>, + bounds: &mut Vec<(String, Rc, u64)>, +) -> MemoGroupId { + for child in node.children() { + collect_bounds(child, memo, group_of_node, seen, bounds); + } + let group = match group_of_node.get(&Rc::as_ptr(node)) { + Some(&group) => group, + None => { + let expression = node + .operator + .map_children(|child| group_of_node[&Rc::as_ptr(child)]); + let group = memo + .find_or_add_group(expression) + .expect("Stage 0 adds every node of a lowered query"); + group_of_node.insert(Rc::as_ptr(node), group); + group + } + }; + if seen.insert(Rc::as_ptr(node)) { + if let Some(bound) = memo.max_values_per_evaluation(group) { + let what = format!("g{} {}", group.0, node.operator.kind_name()); + bounds.push((what, node.clone(), bound)); + } + } + group +} + +/// The result rows of each bounded node of `query` over `dataset`, in +/// `query.bounds` order; why not when the executor rejects the query or +/// the dataset (a many-to-many match, an unsupported operator). +fn execute_counts( + query: &QueryBounds, + bounds: &[usize], + dataset: &Dataset, +) -> Result, String> { + let run = || -> Result, String> { + let dag = query_time_dag(query, bounds)?; + // One schema per source: every column any of its scans reads. + let mut schemas: Vec<(Source, asap_types::ir::schema::Schema)> = Vec::new(); + for node in &dag.nodes { + let PhysicalASAPOperatorPayload::NonASAP(NonASAPOp::Scan { source, .. }) = + &node.payload + else { + continue; + }; + match schemas.iter_mut().find(|(bound, _)| bound == source) { + Some((_, schema)) => { + for field in &node.output_schema.fields { + if schema.column_id(&field.name).is_none() { + schema.fields.push(field.clone()); + } + } + } + None => schemas.push((source.clone(), node.output_schema.clone())), + } + } + let mut store = InMemoryDataStore::default(); + for (source, schema) in schemas { + let source = &source; + let schema: SchemaRef = Arc::new(schema); + let rows = dataset + .rows(source, &schema) + .ok_or_else(|| format!("no rows for {source:?}"))?; + let batch = Batch::try_new(schema.clone(), rows).map_err(|e| format!("batch: {e}"))?; + store = store + .with_raw_data(source.clone(), schema, vec![batch]) + .map_err(|e| format!("bind: {e}"))?; + } + let root_ids: Vec = dag.roots.iter().map(|&root| root as u64).collect(); + let plan = compile(&dag, &[], &root_ids) + .map_err(|e| format!("compile: {e}"))? + .instantiate(Arc::new(store)) + .map_err(|e| format!("instantiate: {e}"))?; + let context = RunContext::new( + Scope::Query { + evaluation_time_ms: EVALUATION_MS, + }, + Limits::default(), + ) + .map_err(|e| format!("context: {e}"))?; + let outputs = plan + .execute(&root_ids, context) + .map_err(|e| format!("execute: {e}"))?; + outputs + .into_iter() + .map(|stream| { + let batches = + block_on(stream.try_collect::>()).map_err(|e| format!("run: {e}"))?; + Ok(batches.iter().map(|batch| batch.num_rows() as u64).sum()) + }) + .collect() + }; + without_panics(run) +} + +/// The nodes of `bounds` exported as one DAG at query time, one root each. +fn query_time_dag( + query: &QueryBounds, + bounds: &[usize], +) -> Result { + let roots: Vec = bounds + .iter() + .map(|&bound| QueryRoot::Operator(query.bounds[bound].1.clone())) + .collect(); + asap_types::test_support::forwarders::physical_asap_dag_at_query_time_with_node_ids(&roots) + .map(|(dag, _)| dag) + .map_err(|error| format!("export: {error:?}")) +} + +/// Whether the executor compiles the nodes of `bounds` together. +fn compiles(query: &QueryBounds, bounds: &[usize]) -> Result<(), String> { + without_panics(|| { + let dag = query_time_dag(query, bounds)?; + let root_ids: Vec = dag.roots.iter().map(|&root| root as u64).collect(); + compile(&dag, &[], &root_ids) + .map(|_| ()) + .map_err(|e| format!("compile: {e}")) + }) +} + +/// `run`'s result, or its panic message as an error. +fn without_panics(run: impl FnOnce() -> Result) -> Result { + catch_unwind(AssertUnwindSafe(run)).unwrap_or_else(|panic| { + let message = panic + .downcast_ref::() + .cloned() + .or_else(|| panic.downcast_ref::<&str>().map(|text| text.to_string())) + .unwrap_or_default(); + Err(format!("panic: {message}")) + }) +} + +/// A tiny deterministic generator (SplitMix64). +struct Rng(u64); + +impl Rng { + fn next(&mut self) -> u64 { + self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15); + let mut z = self.0; + z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); + z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); + z ^ (z >> 31) + } + + fn below(&mut self, n: u64) -> u64 { + if n == 0 { + 0 + } else { + self.next() % n + } + } +} + +/// How one component (a label, a column, or labels declared together) +/// takes its value index over the units (series or rows) of a source, +/// by the unit's index `u`, out of `n` values. +#[derive(Clone, Copy, Debug)] +enum Pattern { + /// `u mod n`: consecutive units differ. + Cycle, + /// `(u / width) mod n`: runs of `width` units share a value. + Blocks(u64), + /// Uniform at random. + Random, +} + +/// Where a component's values come from. +#[derive(Clone, Debug)] +enum ValueSet { + /// Labels whose combinations the facts bound by `combinations`: each + /// label takes value `c mod count` of combination `c`. + Declared { + labels: Vec<(String, u64)>, + combinations: u64, + }, + /// An undeclared label the query compares with literals: those values. + Literals { label: String, values: Vec }, + /// An undeclared label or column: any value; `n` of them in this + /// dataset. + Free { label: String }, +} + +#[derive(Clone, Debug)] +struct Component { + values: ValueSet, + /// How many values the component takes in this dataset. + n: u64, + pattern: Pattern, + /// SQL: the value is the group of rows' (constant within a group) + /// rather than each row's. + per_group: bool, +} + +/// Each source's generated data: its units, samples and components. +struct SourceData { + units: u64, + samples_per_unit: u64, + /// The time between a unit's samples. + spacing_ms: i64, + /// The newest sample's age at evaluation time, before an anchor. + newest_age_ms: i64, + /// PromQL: how long before the evaluation each run of samples ends, + /// newest first: 0, then each `offset` the query reads at. + anchor_ages_ms: Vec, + /// PromQL: the first half of the series exists only at the older + /// anchors and the second half only at the newest, as when series are + /// replaced (a restarted job). + replaced: bool, + components: Vec, + counter: bool, + non_positive_values: bool, + is_table: bool, +} + +struct Dataset { + name: String, + seed: u64, + sources: BTreeMap, + /// Each table column's or label's literal values, by name. + literals: BTreeMap>, +} + +/// The value of index `index` of label or column `name`: its literals +/// first, then values named by index alone, so columns of different +/// tables (a join's `dst` and `host`) share values; `le` takes bucket +/// bounds. +fn value_name(name: &str, index: u64, literals: &BTreeMap>) -> String { + if let Some(literal) = literals + .get(name) + .and_then(|values| values.get(index as usize)) + { + return literal.clone(); + } + if name == "le" { + return format!("{}", 1u64 << index.min(60)); + } + format!("v{index}") +} + +impl Pattern { + fn index(self, u: u64, n: u64, rng: &mut Rng) -> u64 { + let n = n.max(1); + match self { + Pattern::Cycle => u % n, + Pattern::Blocks(width) => (u / width.max(1)) % n, + Pattern::Random => rng.below(n), + } + } +} + +impl Dataset { + /// The rows of `source` in `schema`'s layout; `None` when the dataset + /// does not describe it. + fn rows(&self, source: &Source, schema: &SchemaRef) -> Option>> { + let name = match source { + Source::TimeSeries { metric } => metric.clone(), + Source::Table { table_ref } => table_ref.clone(), + }; + let data = self.sources.get(&name)?; + let mut rng = Rng(self.seed ^ hash(&name)); + let mut rows = Vec::new(); + if data.is_table { + for unit_group in 0..data.units { + for sample in 0..data.samples_per_unit { + let row_index = sample * data.units + unit_group; + let mut cells: BTreeMap = BTreeMap::new(); + for component in &data.components { + let u = if component.per_group { + unit_group + } else { + row_index + }; + let index = component.pattern.index(u, component.n, &mut rng); + for (label, value) in self.component_values(component, index) { + cells.insert(label, value); + } + } + let time = EVALUATION_MS - data.newest_age_ms - sample as i64 * data.spacing_ms; + rows.push( + schema + .fields + .iter() + .enumerate() + .map(|(column, field)| { + if Some(column) == schema.time_index { + return Value::Timestamp(time); + } + let text = cells.get(&field.name); + table_cell(&field.dtype, text, &mut rng) + }) + .collect(), + ); + } + } + } else { + for series in 0..data.units { + let mut labels = BTreeMap::new(); + labels.insert("__name__".to_string(), name.clone()); + labels.insert("series_index".to_string(), series.to_string()); + for component in &data.components { + let index = component.pattern.index(series, component.n, &mut rng); + for (label, value) in self.component_values(component, index) { + if let Value::Utf8(text) = value { + labels.insert(label, text.to_string()); + } + } + } + // A replaced series exists only before the newest anchor, its + // replacement only at it. + let anchors = match (data.replaced, data.anchor_ages_ms.split_first()) { + (true, Some((newest, older))) if !older.is_empty() => { + if series < data.units / 2 { + older + } else { + std::slice::from_ref(newest) + } + } + _ => data.anchor_ages_ms.as_slice(), + }; + let mut value = rng.below(1000) as f64 + 1.0; + let times = anchors.iter().rev().flat_map(|anchor| { + (0..data.samples_per_unit).rev().map(move |sample| { + EVALUATION_MS + - anchor + - data.newest_age_ms + - sample as i64 * data.spacing_ms + }) + }); + for time in times { + value = if data.counter { + value + rng.below(100) as f64 + } else { + rng.below(1000) as f64 + 1.0 + }; + let signed = if data.non_positive_values { + -value + } else { + value + }; + rows.push(promql_rows::series_row(schema, &labels, time, signed).ok()?); + } + } + } + Some(rows) + } + + /// Each label or column `component` sets, at value index `index`. + fn component_values(&self, component: &Component, index: u64) -> Vec<(String, Value)> { + match &component.values { + ValueSet::Declared { labels, .. } => labels + .iter() + .map(|(label, count)| { + let text = value_name(label, index % count.max(&1), &self.literals); + (label.clone(), Value::Utf8(text.into())) + }) + .collect(), + ValueSet::Literals { label, values } => vec![( + label.clone(), + Value::Utf8(values[index as usize % values.len()].clone().into()), + )], + ValueSet::Free { label } => { + vec![( + label.clone(), + Value::Utf8(value_name(label, index, &self.literals).into()), + )] + } + } + } +} + +/// A table cell of type `dtype` from its generated text (`v{i}` or a +/// literal); a numeric cell takes the index as its number, so a declared +/// count bounds it too; with no text, a random number. +fn table_cell(dtype: &FieldDataType, text: Option<&Value>, rng: &mut Rng) -> Value { + let index = text.and_then(|value| match value { + Value::Utf8(text) => text.strip_prefix('v').unwrap_or(text).parse::().ok(), + _ => None, + }); + let number = index.unwrap_or_else(|| rng.below(1_000_000) as f64 / 7.0); + match dtype { + FieldDataType::Plain(DataType::Utf8) => match text { + Some(value) => value.clone(), + None => Value::Utf8(format!("v{}", rng.below(1_000_000)).into()), + }, + FieldDataType::Plain(DataType::Int64) => Value::Int64(number as i64), + FieldDataType::Plain(DataType::Float64) => Value::Float64(number), + FieldDataType::Plain(DataType::Bool) => Value::Bool((number as u64).is_multiple_of(2)), + FieldDataType::Plain(DataType::Timestamp) => Value::Timestamp(number as i64), + _ => Value::Null, + } +} + +fn hash(text: &str) -> u64 { + text.bytes().fold(0xcbf2_9ce4_8422_2325, |hash, byte| { + (hash ^ u64::from(byte)).wrapping_mul(0x100_0000_01b3) + }) +} + +/// The labels (or columns) a query names, by source kind: every label or +/// column it compares with a literal (`m{side="l"}`, `WHERE proto = +/// 'tcp'`), with the literals, and every label its groupings and matches +/// name (`by (job)`, `on(x)`, `group_left(instance)`). +fn literals_and_named_labels(query: &str) -> (BTreeMap>, BTreeSet) { + let chars: Vec = query.chars().collect(); + let mut literals: BTreeMap> = BTreeMap::new(); + let mut named = BTreeSet::new(); + let is_ident = |c: char| c.is_alphanumeric() || c == '_'; + let mut i = 0; + while i < chars.len() { + if !is_ident(chars[i]) || (i > 0 && is_ident(chars[i - 1])) { + i += 1; + continue; + } + let start = i; + while i < chars.len() && is_ident(chars[i]) { + i += 1; + } + let word: String = chars[start..i].iter().collect(); + let mut j = i; + while j < chars.len() && chars[j].is_whitespace() { + j += 1; + } + let rest: String = chars[j..chars.len().min(j + 2)].iter().collect(); + let operator = ["=~", "!~", "!=", "<>", "="] + .into_iter() + .find(|op| rest.starts_with(op)); + if let Some(op) = operator { + let mut k = j + op.len(); + while k < chars.len() && chars[k].is_whitespace() { + k += 1; + } + if let Some("e) = chars.get(k).filter(|c| **c == '"' || **c == '\'') { + let end = chars[k + 1..] + .iter() + .position(|c| *c == quote) + .map_or(chars.len(), |p| k + 1 + p); + let literal: String = chars[k + 1..end].iter().collect(); + if word != "__name__" { + let values = literals.entry(word.clone()).or_default(); + for value in literal.split('|') { + if !value.is_empty() && !values.iter().any(|known| known == value) { + values.push(value.to_string()); + } + } + named.insert(word.clone()); + } + } + } + let grouping = [ + "by", + "without", + "on", + "ignoring", + "group_left", + "group_right", + ]; + if grouping.contains(&word.as_str()) && chars.get(j) == Some(&'(') { + let end = chars[j..] + .iter() + .position(|c| *c == ')') + .map_or(chars.len(), |p| j + p); + let inside: String = chars[j + 1..end].iter().collect(); + for label in inside.split(',').map(str::trim) { + if !label.is_empty() && label.chars().all(is_ident) { + named.insert(label.to_string()); + } + } + } + } + (literals, named) +} + +/// When a query reads its sources, relative to its evaluation. +struct QueryTimes { + /// The longest window it reads back from one time: its windows and + /// subquery ranges along the longest path, plus a 5-minute lookback. + window_ms: i64, + /// Each time it reads at, as an age: 0, then each `offset` along a + /// path (`m offset 1h` reads at 1 h). + anchor_ages_ms: BTreeSet, +} + +fn query_times(root: &OperatorNode) -> QueryTimes { + fn walk(node: &OperatorNode, age_ms: i64, anchors: &mut BTreeSet) -> i64 { + let (own_window, age_ms) = match &node.operator { + Operator::NonASAP(NonASAPOp::TimeRange { range, .. }) + | Operator::NonASAP(NonASAPOp::PromqlSubquery { range, .. }) => { + (range.as_millis() as i64, age_ms) + } + Operator::NonASAP(NonASAPOp::TimeShift { shift, .. }) => { + (0, age_ms + shift.offset_ms.max(0)) + } + _ => (0, age_ms), + }; + anchors.insert(age_ms); + own_window + + node + .children() + .iter() + .map(|child| walk(child, age_ms, anchors)) + .max() + .unwrap_or(300_000) + } + let mut anchor_ages_ms = BTreeSet::new(); + let window_ms = walk(root, 0, &mut anchor_ages_ms); + QueryTimes { + window_ms, + anchor_ages_ms, + } +} + +/// The columns each scanned source has in `root`, by source name, and +/// whether it is a table. +fn scanned_columns(root: &OperatorNode, out: &mut BTreeMap)>) { + if let Operator::NonASAP(NonASAPOp::Scan { source, schema, .. }) = &root.operator { + let (name, is_table) = match source { + Source::TimeSeries { metric } => (metric.clone(), false), + Source::Table { table_ref } => (table_ref.clone(), true), + }; + let entry = out.entry(name).or_insert((is_table, BTreeSet::new())); + for (column, field) in schema.fields.iter().enumerate() { + let label = matches!(field.dtype, FieldDataType::Plain(DataType::Utf8)) + && field.name != asap_types::ir::schema::PROMQL_SERIES_IDENTITY; + if is_table && Some(column) != schema.time_index || label { + entry.1.insert(field.name.clone()); + } + } + } + for child in root.children() { + scanned_columns(child, out); + } +} + +/// The datasets `query` runs over: three that seek the worst case and +/// `sizes.random_datasets` random ones, each within the facts `data` +/// declares. +fn datasets( + case: &Case, + root: &Rc, + data: &DataWorkload, + sizes: DatasetSizes, +) -> Vec { + let query_text: String = case + .workload + .query_workload + .entries() + .map(|entry| entry.query.0.clone()) + .collect::>() + .join("\n"); + let (literals, named) = literals_and_named_labels(&query_text); + let mut scanned = BTreeMap::new(); + scanned_columns(root, &mut scanned); + let times = query_times(root); + // Fixed worst-case seekers: declared labels random per unit, in a mixed + // radix (every combination of the components), or all cycling + // together; and series replaced between the times the query reads. + let strategies = ["independent", "crossed", "aligned", "replaced"] + .into_iter() + .map(str::to_string) + .chain((0..sizes.random_datasets).map(|seed| format!("random-{seed}"))); + strategies + .enumerate() + .map(|(index, strategy)| { + let seed = hash(&case.name) ^ (index as u64).wrapping_mul(0x9E37_79B9); + let mut rng = Rng(seed); + let sources = scanned + .iter() + .map(|(name, (is_table, columns))| { + let default_facts = SourceFacts::default(); + let facts = data.sources.get(name).unwrap_or(&default_facts); + let source = source_data( + *is_table, columns, &named, &literals, facts, ×, &strategy, sizes, + &mut rng, + ); + (name.clone(), source) + }) + .collect(); + Dataset { + name: strategy, + seed, + sources, + literals: literals.clone(), + } + }) + .collect() +} + +#[allow(clippy::too_many_arguments)] +fn source_data( + is_table: bool, + columns: &BTreeSet, + named: &BTreeSet, + literals: &BTreeMap>, + facts: &SourceFacts, + times: &QueryTimes, + strategy: &str, + sizes: DatasetSizes, + rng: &mut Rng, +) -> SourceData { + let random = strategy.starts_with("random"); + // Units: the declared series (groups of rows), at most `max_units`. + let mut units = facts + .series_count + .unwrap_or(if random { 1 + rng.below(64) } else { 24 }) + .clamp(1, sizes.max_units); + let interval_ms = facts + .sample_interval + .map_or(if is_table { 60_000 } else { 15_000 }, |interval| { + interval.0.max(1) as i64 + }); + let mut samples_per_unit = sizes.max_samples_per_unit; + let spacing_ms = if is_table { + interval_ms + } else { + interval_ms.max(times.window_ms / (samples_per_unit as i64 - 2).max(1)) + }; + let anchor_ages_ms: Vec = if is_table { + vec![0] + } else { + times.anchor_ages_ms.iter().copied().collect() + }; + if let Some(held) = facts.rows_held { + let runs = anchor_ages_ms.len() as u64; + units = units.min(held / runs).max(1); + samples_per_unit = samples_per_unit.min(held / units / runs).max(1); + } + let newest_age_ms = if is_table { 0 } else { interval_ms / 2 }; + // Components: labels declared together, each other declared label, + // undeclared labels compared with literals, and the other undeclared + // labels the query reads or names. + let mut labels: BTreeSet = columns.clone(); + if !is_table { + labels.extend(named.iter().cloned()); + labels.extend(facts.distinct_values_per_label.keys().cloned()); + for set in &facts.distinct_values_per_label_set { + labels.extend(set.labels.iter().cloned()); + } + } + let mut components = Vec::new(); + let mut in_a_set: BTreeSet = BTreeSet::new(); + let mut sets: Vec<(BTreeSet, u64)> = Vec::new(); + for set in &facts.distinct_values_per_label_set { + let mut members: BTreeSet = set.labels.iter().cloned().collect(); + let mut combinations = set.distinct_values; + sets.retain(|(other, count)| { + if other.is_disjoint(&members) { + true + } else { + members.extend(other.iter().cloned()); + combinations = combinations.min(*count); + false + } + }); + sets.push((members, combinations)); + } + for (members, combinations) in sets { + let labels = members + .iter() + .filter(|label| labels.contains(*label)) + .map(|label| { + let count = facts + .distinct_values_per_label + .get(label) + .copied() + .unwrap_or(combinations); + (label.clone(), count.min(combinations).max(1)) + }) + .collect::>(); + in_a_set.extend(members); + if !labels.is_empty() { + components.push(ValueSet::Declared { + labels, + combinations: combinations.max(1), + }); + } + } + for label in &labels { + if in_a_set.contains(label) { + continue; + } + components.push(match facts.distinct_values_per_label.get(label) { + Some(&count) => ValueSet::Declared { + labels: vec![(label.clone(), count.max(1))], + combinations: count.max(1), + }, + None => match literals.get(label) { + Some(values) if !values.is_empty() => ValueSet::Literals { + label: label.clone(), + values: values.clone(), + }, + _ => ValueSet::Free { + label: label.clone(), + }, + }, + }); + } + let total_units = if is_table { + units * samples_per_unit + } else { + units + }; + // Literal labels split the units into blocks; within a block, free + // labels are unique, so matches across blocks pair units one to one. + let blocks: u64 = components + .iter() + .map(|values| match values { + ValueSet::Literals { values, .. } => values.len() as u64, + ValueSet::Declared { .. } | ValueSet::Free { .. } => 1, + }) + .product::() + .max(1); + let per_block = total_units.div_ceil(blocks).max(1); + let mut block_width = total_units; + let mut radix = 1u64; + let components = components + .into_iter() + .map(|values| { + let (n, pattern) = match (&values, strategy) { + (ValueSet::Literals { values, .. }, _) if !random => { + let count = values.len() as u64; + block_width = (block_width / count).max(1); + (count, Pattern::Blocks(block_width)) + } + // A replaced series and its replacement share their free + // labels. + (ValueSet::Free { .. }, "replaced") => ((per_block / 2).max(1), Pattern::Cycle), + (ValueSet::Free { .. }, _) if !random => (per_block, Pattern::Cycle), + (ValueSet::Declared { combinations, .. }, "replaced") => { + (*combinations, Pattern::Random) + } + (ValueSet::Declared { combinations, .. }, "independent") => { + (*combinations, Pattern::Random) + } + (ValueSet::Declared { combinations, .. }, "crossed") => { + let pattern = Pattern::Blocks(radix); + radix = radix.saturating_mul(*combinations); + (*combinations, pattern) + } + (ValueSet::Declared { combinations, .. }, _) if !random => { + (*combinations, Pattern::Cycle) + } + (values, _) => { + let n = match values { + ValueSet::Declared { combinations, .. } => *combinations, + ValueSet::Literals { values, .. } => values.len() as u64, + ValueSet::Free { .. } => { + [1, 2, 3, 10, per_block, total_units][rng.below(6) as usize] + } + }; + let pattern = match rng.below(3) { + 0 => Pattern::Cycle, + 1 => Pattern::Blocks(1 + rng.below(total_units)), + _ => Pattern::Random, + }; + (n, pattern) + } + }; + Component { + values, + n: n.max(1), + pattern, + per_group: is_table && random && rng.below(3) == 0, + } + }) + .collect(); + let value_sign = facts.column_signs.get("value"); + SourceData { + units, + samples_per_unit, + spacing_ms, + newest_age_ms, + anchor_ages_ms, + replaced: strategy == "replaced", + components, + counter: matches!( + facts.metric_type, + Some(asap_types::workload::PrometheusMetricType::Counter) + ), + non_positive_values: matches!(value_sign, Some(asap_types::stages::ValueSign::NonPositive)), + is_table, + } +} diff --git a/crates/integration-tests/tests/memo_value_bounds_fixtures/cases.json b/crates/integration-tests/tests/memo_value_bounds_fixtures/cases.json new file mode 100644 index 000000000..da505bd63 --- /dev/null +++ b/crates/integration-tests/tests/memo_value_bounds_fixtures/cases.json @@ -0,0 +1,835 @@ +{ + "tables": [ + { + "name": "flows", + "time_index": 0, + "columns": [ + { + "name": "ts", + "type": "timestamp", + "nullable": false + }, + { + "name": "src", + "type": "utf8", + "nullable": false + }, + { + "name": "dst", + "type": "utf8", + "nullable": false + }, + { + "name": "bytes", + "type": "float64", + "nullable": false + }, + { + "name": "p", + "type": "float64", + "nullable": false + } + ] + }, + { + "name": "hosts", + "time_index": 0, + "columns": [ + { + "name": "ts", + "type": "timestamp", + "nullable": false + }, + { + "name": "host", + "type": "utf8", + "nullable": false + }, + { + "name": "region", + "type": "utf8", + "nullable": false + } + ] + } + ], + "facts": { + "f01": { + "m": { + "column_signs": { + "value": "non_negative" + }, + "distinct_values_per_label": { + "job": 10 + }, + "metric_type": "gauge", + "sample_interval": 5, + "series_count": 100 + } + }, + "f02": { + "flows": { + "distinct_values_per_label": { + "dst": 300, + "src": 6 + }, + "rows_held": 3600000 + } + }, + "f03": { + "flows": { + "distinct_values_per_label": { + "dst": 300, + "src": 6 + }, + "rows_held": 3600000, + "series_count": 6 + } + }, + "f04": { + "flows": { + "rows_held": 3600000, + "series_count": 6 + } + }, + "f05": { + "flows": { + "distinct_values_per_label": { + "dst": 300, + "src": 6 + }, + "rows_held": 3600000, + "series_count": 1800 + } + }, + "f06": { + "flows": { + "column_signs": { + "bytes": "non_negative" + }, + "distinct_values_per_label": { + "dst": 300, + "src": 6 + }, + "rows_held": 3600000, + "series_count": 6 + } + }, + "f07": { + "flows": { + "distinct_values_per_label": { + "dst": 300, + "src": 6 + }, + "rows_held": 3600000, + "series_count": 6 + }, + "hosts": { + "distinct_values_per_label": { + "host": 6, + "region": 6 + }, + "rows_held": 3600000, + "series_count": 6 + } + }, + "f08": { + "m_bucket": { + "distinct_values_per_label": { + "le": 12 + }, + "metric_type": "counter", + "sample_interval": 5, + "series_count": 120 + } + }, + "f09": { + "m": { + "column_signs": { + "value": "non_negative" + }, + "metric_type": "gauge", + "sample_interval": 5, + "series_count": 100 + } + }, + "f10": { + "m": { + "column_signs": { + "value": "non_negative" + }, + "distinct_values_per_label": { + "job": 10, + "le": 12 + }, + "metric_type": "gauge", + "sample_interval": 5, + "series_count": 100 + } + }, + "f11": { + "flows": { + "distinct_values_per_label": { + "dst": 300, + "src": 6 + }, + "rows_held": 3600000, + "sample_interval": 60000, + "series_count": 6 + } + }, + "f12": { + "flows": { + "distinct_values_per_label": { + "dst": 300, + "src": 6 + }, + "rows_held": 3600000, + "sample_interval": 60000, + "series_count": 6 + }, + "hosts": { + "distinct_values_per_label": { + "host": 6, + "region": 6 + }, + "rows_held": 3600000, + "sample_interval": 60000, + "series_count": 6 + } + }, + "f13": { + "a": { + "column_signs": { + "value": "non_negative" + }, + "distinct_values_per_label": { + "instance": 10, + "job": 10 + }, + "distinct_values_per_label_set": [ + { + "distinct_values": 10, + "labels": [ + "job", + "instance" + ] + } + ], + "metric_type": "gauge", + "sample_interval": 15000, + "series_count": 200 + }, + "b": { + "column_signs": { + "value": "non_negative" + }, + "distinct_values_per_label": { + "instance": 10, + "job": 10 + }, + "distinct_values_per_label_set": [ + { + "distinct_values": 10, + "labels": [ + "job", + "instance" + ] + } + ], + "metric_type": "gauge", + "sample_interval": 15000, + "series_count": 200 + } + }, + "f14": { + "m": { + "column_signs": { + "value": "non_negative" + }, + "distinct_values_per_label": { + "instance": 10, + "job": 10 + }, + "distinct_values_per_label_set": [ + { + "distinct_values": 10, + "labels": [ + "job", + "instance" + ] + } + ], + "metric_type": "gauge", + "sample_interval": 15000, + "series_count": 200 + } + }, + "f15": { + "m": { + "column_signs": { + "value": "non_negative" + }, + "distinct_values_per_label": { + "instance": 10, + "job": 10 + }, + "distinct_values_per_label_set": [ + { + "distinct_values": 10, + "labels": [ + "job", + "instance" + ] + } + ], + "metric_type": "gauge", + "sample_interval": 15000, + "series_count": 5 + } + }, + "f16": { + "m_bucket": { + "distinct_values_per_label": { + "job": 10, + "le": 12 + }, + "metric_type": "counter", + "sample_interval": 15000, + "series_count": 1200 + } + }, + "f17": { + "flows": { + "distinct_values_per_label": { + "dst": 300, + "src": 6 + }, + "series_count": 6 + } + }, + "f18": { + "flows": { + "distinct_values_per_label": { + "dst": 300, + "src": 6 + }, + "rows_held": 3600000, + "sample_interval": 600000, + "series_count": 6 + } + } + }, + "cases": [ + { + "name": "p10_ratio_pv", + "language": "promql", + "facts": "f01", + "query": "quantile_over_time(0.9, m[5m]) / quantile_over_time(0.5, m[5m])" + }, + { + "name": "p11_scalar_pv", + "language": "promql", + "facts": "f01", + "query": "quantile_over_time(0.9, m[5m]) / scalar(max(quantile_over_time(0.5, m[5m])))" + }, + { + "name": "p12_vector_or", + "language": "promql", + "facts": "f01", + "query": "vector(1) or quantile_over_time(0.9, m[5m])" + }, + { + "name": "p13_unless", + "language": "promql", + "facts": "f01", + "query": "quantile_over_time(0.9, m[5m]) unless quantile_over_time(0.5, m[5m])" + }, + { + "name": "p14_share", + "language": "promql", + "facts": "f01", + "query": "quantile_over_time(0.9, m[5m])" + }, + { + "name": "p15_topk_by_job_or", + "language": "promql", + "facts": "f01", + "query": "topk by (job) (1, quantile_over_time(0.9, m[5m])) or quantile_over_time(0.5, m[5m])" + }, + { + "name": "p1_or_pv", + "language": "promql", + "facts": "f01", + "query": "quantile_over_time(0.9, m[5m]) or quantile_over_time(0.5, m[5m])" + }, + { + "name": "p2_or_on_job", + "language": "promql", + "facts": "f01", + "query": "quantile_over_time(0.9, m[5m]) or on(job) quantile_over_time(0.5, m[5m])" + }, + { + "name": "p3_topk_pv", + "language": "promql", + "facts": "f01", + "query": "topk(5, quantile_over_time(0.9, m[5m]))" + }, + { + "name": "p4_group_left_pv", + "language": "promql", + "facts": "f01", + "query": "quantile_over_time(0.9, m[5m]) * on(job) group_left max by (job) (quantile_over_time(0.5, m[5m]))" + }, + { + "name": "p6_sum_by_job_pv", + "language": "promql", + "facts": "f01", + "query": "sum by (job) (quantile_over_time(0.9, m[5m]))" + }, + { + "name": "p7_quantile_by_job_pv", + "language": "promql", + "facts": "f01", + "query": "quantile by (job) (0.5, m)" + }, + { + "name": "p9_offset_or", + "language": "promql", + "facts": "f01", + "query": "quantile_over_time(0.9, m[5m]) offset 5m or quantile_over_time(0.9, m[5m])" + }, + { + "name": "s1_bucket_nosc", + "language": "sql", + "facts": "f02", + "query": "SELECT date_trunc('minute', ts) AS m, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY date_trunc('minute', ts)" + }, + { + "name": "s1_bucket_sc", + "language": "sql", + "facts": "f03", + "query": "SELECT date_trunc('minute', ts) AS m, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY date_trunc('minute', ts)" + }, + { + "name": "s2_src_bucket_sc", + "language": "sql", + "facts": "f03", + "query": "SELECT src, date_trunc('minute', ts) AS m, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY src, date_trunc('minute', ts)" + }, + { + "name": "s3_src_undeclared", + "language": "sql", + "facts": "f04", + "query": "SELECT src, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY src" + }, + { + "name": "s4_rollup", + "language": "sql", + "facts": "f05", + "query": "SELECT src, dst, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY ROLLUP(src, dst)" + }, + { + "name": "s5_unionall", + "language": "sql", + "facts": "f03", + "query": "SELECT src, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY src UNION ALL SELECT src, approx_percentile_cont(bytes, 0.5) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY src" + }, + { + "name": "s6_join_pv", + "language": "sql", + "facts": "f06", + "query": "SELECT a.src, a.q / b.q AS r FROM (SELECT src, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY src) a JOIN (SELECT src, approx_percentile_cont(bytes, 0.5) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY src) b ON a.src = b.src" + }, + { + "name": "c_alias", + "language": "sql", + "facts": "f03", + "query": "SELECT src AS s, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY s" + }, + { + "name": "c_case", + "language": "sql", + "facts": "f03", + "query": "SELECT CASE WHEN bytes > 100 THEN src ELSE dst END AS k, approx_percentile_cont(p, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY CASE WHEN bytes > 100 THEN src ELSE dst END" + }, + { + "name": "c_cast_num", + "language": "sql", + "facts": "f03", + "query": "SELECT CAST(bytes AS BIGINT) AS b, approx_percentile_cont(p, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY CAST(bytes AS BIGINT)" + }, + { + "name": "c_cast_str", + "language": "sql", + "facts": "f03", + "query": "SELECT CAST(src AS VARCHAR) AS s, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY CAST(src AS VARCHAR)" + }, + { + "name": "c_cast_ts", + "language": "sql", + "facts": "f03", + "query": "SELECT CAST(ts AS BIGINT) AS t, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY CAST(ts AS BIGINT)" + }, + { + "name": "c_dst", + "language": "sql", + "facts": "f03", + "query": "SELECT dst, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY dst" + }, + { + "name": "c_raw_ts", + "language": "sql", + "facts": "f03", + "query": "SELECT ts, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY ts" + }, + { + "name": "c_sub_alias", + "language": "sql", + "facts": "f03", + "query": "SELECT k, approx_percentile_cont(bytes, 0.9) AS q FROM (SELECT date_trunc('minute', ts) AS k, bytes FROM flows WHERE ts >= now() - INTERVAL '15 minutes') t GROUP BY k" + }, + { + "name": "c_sub_upper", + "language": "sql", + "facts": "f03", + "query": "SELECT k, approx_percentile_cont(bytes, 0.9) AS q FROM (SELECT upper(src) AS k, bytes FROM flows WHERE ts >= now() - INTERVAL '15 minutes') t GROUP BY k" + }, + { + "name": "c_value_col", + "language": "sql", + "facts": "f03", + "query": "SELECT bytes, approx_percentile_cont(p, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY bytes" + }, + { + "name": "j_self", + "language": "sql", + "facts": "f03", + "query": "SELECT a.src, b.src AS src2, approx_percentile_cont(a.bytes, 0.9) AS q FROM flows a JOIN flows b ON a.dst = b.dst WHERE a.ts >= now() - INTERVAL '15 minutes' AND b.ts >= now() - INTERVAL '15 minutes' GROUP BY a.src, b.src" + }, + { + "name": "j_self_1key", + "language": "sql", + "facts": "f03", + "query": "SELECT a.src, approx_percentile_cont(a.bytes, 0.9) AS q FROM flows a JOIN flows b ON a.dst = b.dst WHERE a.ts >= now() - INTERVAL '15 minutes' AND b.ts >= now() - INTERVAL '15 minutes' GROUP BY a.src" + }, + { + "name": "j_two", + "language": "sql", + "facts": "f07", + "query": "SELECT f.src, h.region, approx_percentile_cont(f.bytes, 0.9) AS q FROM flows f JOIN hosts h ON f.dst = h.host WHERE f.ts >= now() - INTERVAL '15 minutes' GROUP BY f.src, h.region" + }, + { + "name": "j_two_one_side", + "language": "sql", + "facts": "f07", + "query": "SELECT h.region, approx_percentile_cont(f.bytes, 0.9) AS q FROM flows f JOIN hosts h ON f.dst = h.host WHERE f.ts >= now() - INTERVAL '15 minutes' GROUP BY h.region" + }, + { + "name": "p_hist_whole", + "language": "promql", + "facts": "f08", + "query": "histogram_quantile(0.9, sum by (le) (rate(m_bucket[5m])))" + }, + { + "name": "p_job_undeclared_whole", + "language": "promql", + "facts": "f09", + "query": "quantile by (job) (0.5, m)" + }, + { + "name": "p_labelrep_qot_whole", + "language": "promql", + "facts": "f01", + "query": "max(label_replace(quantile_over_time(0.9, m[5m]), \"job\", \"all\", \"\", \"\"))" + }, + { + "name": "p_labelrep_whole", + "language": "promql", + "facts": "f01", + "query": "quantile by (x) (0.5, label_replace(m, \"x\", \"$1\", \"job\", \"(.*)\"))" + }, + { + "name": "p_le_whole", + "language": "promql", + "facts": "f10", + "query": "quantile by (le) (0.5, m)" + }, + { + "name": "s3_src", + "language": "sql", + "facts": "f03", + "query": "SELECT src, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY src" + }, + { + "name": "d_distinct", + "language": "sql", + "facts": "f11", + "query": "SELECT DISTINCT src, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY src" + }, + { + "name": "g_expr", + "language": "sql", + "facts": "f11", + "query": "SELECT src || dst AS k, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY src || dst" + }, + { + "name": "g_two", + "language": "sql", + "facts": "f11", + "query": "SELECT src, dst, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY src, dst" + }, + { + "name": "g_two_nowin", + "language": "sql", + "facts": "f11", + "query": "SELECT src, dst, approx_percentile_cont(bytes, 0.9) AS q FROM flows GROUP BY src, dst" + }, + { + "name": "h_having", + "language": "sql", + "facts": "f11", + "query": "SELECT dst, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY dst HAVING count(*) > 1" + }, + { + "name": "j_full", + "language": "sql", + "facts": "f12", + "query": "SELECT f.src, h.region, approx_percentile_cont(f.bytes, 0.9) AS q FROM flows f FULL JOIN hosts h ON f.dst = h.host WHERE f.ts >= now() - INTERVAL '15 minutes' GROUP BY f.src, h.region" + }, + { + "name": "j_left", + "language": "sql", + "facts": "f12", + "query": "SELECT h.region, approx_percentile_cont(f.bytes, 0.9) AS q FROM flows f LEFT JOIN hosts h ON f.dst = h.host WHERE f.ts >= now() - INTERVAL '15 minutes' GROUP BY h.region" + }, + { + "name": "j_self_cross", + "language": "sql", + "facts": "f11", + "query": "SELECT a.src, b.dst, approx_percentile_cont(a.bytes, 0.9) AS q FROM flows a JOIN flows b ON a.src = b.src WHERE a.ts >= now() - INTERVAL '15 minutes' AND b.ts >= now() - INTERVAL '15 minutes' GROUP BY a.src, b.dst" + }, + { + "name": "j_three", + "language": "sql", + "facts": "f12", + "query": "SELECT a.src, b.src AS s2, c.region, approx_percentile_cont(a.bytes, 0.9) AS q FROM flows a JOIN flows b ON a.dst = b.dst JOIN hosts c ON b.dst = c.host WHERE a.ts >= now() - INTERVAL '15 minutes' GROUP BY a.src, b.src, c.region" + }, + { + "name": "l_limit_over", + "language": "sql", + "facts": "f11", + "query": "SELECT dst, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY dst ORDER BY q DESC LIMIT 5" + }, + { + "name": "l_limit_under", + "language": "sql", + "facts": "f11", + "query": "SELECT dst, approx_percentile_cont(b, 0.9) AS q FROM (SELECT dst, bytes AS b FROM flows WHERE ts >= now() - INTERVAL '15 minutes' LIMIT 10) t GROUP BY dst" + }, + { + "name": "p_and_on", + "language": "promql", + "facts": "f13", + "query": "quantile by (job) (0.5, a and on(job) b)" + }, + { + "name": "p_cv_by", + "language": "promql", + "facts": "f14", + "query": "quantile by (job) (0.5, count_values by (job) (\"v\", m))" + }, + { + "name": "p_cv_key", + "language": "promql", + "facts": "f14", + "query": "quantile by (job) (0.5, count_values(\"job\", m))" + }, + { + "name": "p_few_series", + "language": "promql", + "facts": "f15", + "query": "quantile by (job) (0.5, m)" + }, + { + "name": "p_gl_diff", + "language": "promql", + "facts": "f13", + "query": "quantile by (job, instance) (0.5, a * on(x) group_left(instance) b)" + }, + { + "name": "p_gl_same", + "language": "promql", + "facts": "f14", + "query": "quantile by (job, instance) (0.5, m{side=\"l\"} * on(x) group_left(instance) m{side=\"r\"})" + }, + { + "name": "p_gl_same_offset", + "language": "promql", + "facts": "f14", + "query": "quantile by (job, instance) (0.5, m * on(x) group_left(instance) m offset 1h)" + }, + { + "name": "p_gr_same", + "language": "promql", + "facts": "f14", + "query": "quantile by (job, instance) (0.5, m{side=\"l\"} * on(x) group_right(job) m{side=\"r\"})" + }, + { + "name": "p_hist", + "language": "promql", + "facts": "f16", + "query": "histogram_quantile(0.9, sum by (le, job) (rate(m_bucket[5m])))" + }, + { + "name": "p_hist_qot", + "language": "promql", + "facts": "f16", + "query": "quantile by (le) (0.5, rate(m_bucket[5m]))" + }, + { + "name": "p_ignoring_arith", + "language": "promql", + "facts": "f13", + "query": "quantile by (job) (0.5, a + ignoring(side) b)" + }, + { + "name": "p_labelrep_other", + "language": "promql", + "facts": "f14", + "query": "quantile by (job) (0.5, label_replace(m, \"x\", \"$1\", \"instance\", \"(.*)\"))" + }, + { + "name": "p_labelrep_same", + "language": "promql", + "facts": "f14", + "query": "quantile by (job) (0.5, label_replace(m, \"job\", \"$1\", \"instance\", \"(.*)\"))" + }, + { + "name": "p_or_diff", + "language": "promql", + "facts": "f13", + "query": "quantile by (job) (0.5, a or b)" + }, + { + "name": "p_or_same", + "language": "promql", + "facts": "f14", + "query": "quantile by (job) (0.5, m{side=\"l\"} or m{side=\"r\"})" + }, + { + "name": "p_qby_job", + "language": "promql", + "facts": "f14", + "query": "quantile by (job) (0.5, m)" + }, + { + "name": "p_qby_job_inst", + "language": "promql", + "facts": "f14", + "query": "quantile by (job, instance) (0.5, m)" + }, + { + "name": "p_subq", + "language": "promql", + "facts": "f14", + "query": "quantile by (job) (0.5, max_over_time(m[30m:1m]))" + }, + { + "name": "p_subq_outer", + "language": "promql", + "facts": "f14", + "query": "max_over_time(quantile by (job) (0.5, m)[30m:1m])" + }, + { + "name": "p_sum_by_job", + "language": "promql", + "facts": "f14", + "query": "sum by (job) (quantile_over_time(0.9, m[5m]))" + }, + { + "name": "p_sum_or_qot", + "language": "promql", + "facts": "f13", + "query": "sum by (job) (quantile_over_time(0.9, a[5m]) or quantile_over_time(0.9, b[5m]))" + }, + { + "name": "p_topk_by", + "language": "promql", + "facts": "f14", + "query": "topk by (job) (3, quantile_over_time(0.9, m[5m]))" + }, + { + "name": "p_unless", + "language": "promql", + "facts": "f13", + "query": "quantile by (job) (0.5, a unless ignoring(instance) b)" + }, + { + "name": "r_bytes_interval", + "language": "sql", + "facts": "f11", + "query": "SELECT bytes, approx_percentile_cont(p, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY bytes" + }, + { + "name": "r_bytes_no_rows", + "language": "sql", + "facts": "f17", + "query": "SELECT bytes, approx_percentile_cont(p, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY bytes" + }, + { + "name": "r_dst_interval_small", + "language": "sql", + "facts": "f18", + "query": "SELECT dst, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY dst" + }, + { + "name": "t_cd_by_src", + "language": "sql", + "facts": "f11", + "query": "SELECT src, count(DISTINCT dst) AS c FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY src" + }, + { + "name": "t_topk_count", + "language": "sql", + "facts": "f11", + "query": "SELECT dst, count(*) AS c FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY dst ORDER BY c DESC LIMIT 3" + }, + { + "name": "t_topk_sum", + "language": "sql", + "facts": "f11", + "query": "SELECT dst, sum(bytes) AS c FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY dst ORDER BY c DESC LIMIT 3" + }, + { + "name": "u_outer_union", + "language": "sql", + "facts": "f11", + "query": "SELECT src, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY src UNION ALL SELECT dst, approx_percentile_cont(bytes, 0.9) AS q FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY dst" + }, + { + "name": "u_union_mixed", + "language": "sql", + "facts": "f11", + "query": "SELECT k, approx_percentile_cont(b, 0.9) AS q FROM (SELECT src AS k, bytes AS b FROM flows WHERE ts >= now() - INTERVAL '15 minutes' UNION ALL SELECT dst AS k, bytes AS b FROM flows WHERE ts >= now() - INTERVAL '15 minutes') t GROUP BY k" + }, + { + "name": "u_union_same", + "language": "sql", + "facts": "f11", + "query": "SELECT k, approx_percentile_cont(b, 0.9) AS q FROM (SELECT src AS k, bytes AS b FROM flows WHERE ts >= now() - INTERVAL '15 minutes' AND p > 1 UNION ALL SELECT src AS k, bytes AS b FROM flows WHERE ts >= now() - INTERVAL '15 minutes' AND p <= 1) t GROUP BY k" + }, + { + "name": "w_window", + "language": "sql", + "facts": "f11", + "query": "SELECT src, approx_percentile_cont(bytes, 0.9) AS q, rank() OVER (ORDER BY src) AS r FROM flows WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY src" + }, + { + "name": "w_window_key", + "language": "sql", + "facts": "f11", + "query": "SELECT r, approx_percentile_cont(b, 0.9) AS q FROM (SELECT row_number() OVER (ORDER BY ts) AS r, bytes AS b FROM flows WHERE ts >= now() - INTERVAL '15 minutes') t GROUP BY r" + } + ] +} From f9e7bbfb9b9abb74e8e1d3323ecc11b31ca38996 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 06:00:32 +0000 Subject: [PATCH 14/24] docs(accuracy): a many-to-one match's other side is another scan; an outer join's nullable side is computed --- docs/design_docs/concepts/accuracy-models.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/design_docs/concepts/accuracy-models.md b/docs/design_docs/concepts/accuracy-models.md index 397465708..6fcb78b41 100644 --- a/docs/design_docs/concepts/accuracy-models.md +++ b/docs/design_docs/concepts/accuracy-models.md @@ -126,7 +126,14 @@ most 6 x 16 = 96 rows, so `R` = 96, and without a sampling interval or `rows_held` `R` is unbound. Keys from different scans multiply, since a join pairs their values: `flows JOIN hosts ... GROUP BY f.src, h.region` with 6 `src` and 6 `region` values declared returns up to 36 values, not 6 + 6. -With both unbound the target stays unmet. A grouped result's `G` follows +The "one" side of a many-to-one vector match is another scan in the same +way: `m{side="l"} * on(x) group_left(instance) m{side="r"}` by `job, +instance` pairs one series' `job` with another's `instance`, up to 10 x 10 +with 10 (`job`, `instance`) pairs declared. A column of an outer join's +nullable side gains a NULL its source may not hold (`flows LEFT JOIN hosts +... GROUP BY h.region` returns the 6 regions and a NULL), so it counts as +computed and only the rows read bound it. With both unbound the target +stays unmet. A grouped result's `G` follows the same rule as `R` (`Memo::max_values_per_evaluation` of the root group): the product, over the scans its group keys come from, of the distinct values each scan's source declares for them (`SourceFacts.distinct_values_per_label`, From 51c9a0da2eb40c58d252a644bef0f90d6de84992 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 06:04:15 +0000 Subject: [PATCH 15/24] test(types): a same-source group_right's one-side label is the other scan's --- crates/types/tests/column_lineage.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/types/tests/column_lineage.rs b/crates/types/tests/column_lineage.rs index 7f1f4caba..0a2ab50a4 100644 --- a/crates/types/tests/column_lineage.rs +++ b/crates/types/tests/column_lineage.rs @@ -671,8 +671,10 @@ fn a_same_source_binary_op_keeps_its_labels_scan() { /// `rate(x[5m]) * on(job) group_right(team) rate(x[1h])`: the output's rows /// are the right ("many") side's, so its labels take that side's lineage, -/// `x` scan 0's, not a shifted scan or a disagreement; `team` comes from -/// the left ("one") side, also `x` scan 0's. +/// `x` scan 0's, not a shifted scan or a disagreement. `team` comes from +/// the left ("one") side's series matched on `job`, another series of `x` +/// than the one each output row keeps, so it is `x` scan 1's: the two +/// sides pair labels of different series, as a self-join pairs rows. #[test] fn a_same_source_group_right_takes_the_right_sides_labels() { let mut memo = match_grouping_memo(); @@ -681,10 +683,10 @@ fn a_same_source_group_right_takes_the_right_sides_labels() { let product = memo .find_or_add_group(grouped_product(GroupSide::Right, short, long)) .unwrap(); - for label in ["job", "team"] { + for (label, scan_ordinal) in [("job", 0), ("team", 1)] { assert_eq!( lineage_and_sign_of(&memo, product, label).0, - ColumnLineage::SourceColumn(series_column("x", 0, label)) + ColumnLineage::SourceColumn(series_column("x", scan_ordinal, label)) ); } } From df288181b6f49d9f89beb121e582a22a78e241b0 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 06:16:18 +0000 Subject: [PATCH 16/24] refactor(types): name the join side whose columns an outer join makes nullable --- crates/types/src/stages/logical/group/column_properties.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/types/src/stages/logical/group/column_properties.rs b/crates/types/src/stages/logical/group/column_properties.rs index 69fa305f5..06e31f37e 100644 --- a/crates/types/src/stages/logical/group/column_properties.rs +++ b/crates/types/src/stages/logical/group/column_properties.rs @@ -304,10 +304,9 @@ fn non_asap_columns( }; let mut columns = columns_of(left); let mut right = after_left_scans(&columns, columns_of(right)); - for (columns, nullable) in [(&mut columns, left_nullable), (&mut right, right_nullable)] - { + for (side, nullable) in [(&mut columns, left_nullable), (&mut right, right_nullable)] { if nullable { - for column in columns.iter_mut() { + for column in side.iter_mut() { column.lineage = None; } } From a4b6120a807c1376c96b86a6000707f9e4f149f9 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 06:54:01 +0000 Subject: [PATCH 17/24] test(types): a LIMIT above a grouping bounds its values by n SELECT dst, approx_percentile_cont(bytes, 0.9) AS q FROM flows ... GROUP BY dst ORDER BY q DESC LIMIT 5 returns at most 5 values; the memo gives 96, the rows the window holds. Fails before the fix with Some(96). --- crates/integration-tests/tests/pass2_sql.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/integration-tests/tests/pass2_sql.rs b/crates/integration-tests/tests/pass2_sql.rs index 6d68372b5..f36fa1373 100644 --- a/crates/integration-tests/tests/pass2_sql.rs +++ b/crates/integration-tests/tests/pass2_sql.rs @@ -381,3 +381,15 @@ fn a_grouping_over_a_limit_returns_at_most_its_rows() { ); assert_eq!(values_per_evaluation(&logical), Some(10)); } + +/// A `LIMIT 5` above the grouping keeps at most 5 of its rows, so the +/// result holds at most 5 `dst` values, not the 96 rows the window holds. +#[test] +fn a_limit_over_a_grouping_returns_at_most_n_values() { + let logical = whole_result_quantile( + "SELECT dst, approx_percentile_cont(bytes, 0.9) AS q FROM flows \ + WHERE ts >= now() - INTERVAL '15 minutes' GROUP BY dst ORDER BY q DESC LIMIT 5", + flows_and_six_hosts(), + ); + assert_eq!(values_per_evaluation(&logical), Some(5)); +} From fe3a22aefb0f888f13eed268f02349540afcdfc3 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 06:46:35 +0000 Subject: [PATCH 18/24] fix(types): a LIMIT above a grouping bounds its values by n `SELECT dst, approx_percentile_cont(bytes, 0.9) AS q FROM flows ... GROUP BY dst ORDER BY q DESC LIMIT 5` returns at most 5 values, but Memo::max_values_per_evaluation read the keys through the Limit and gave the 96 rows the window holds. The bound now also takes the rows the group itself returns (max_rows_per_evaluation), which caps a whole-input LIMIT by n, as it already does for a LIMIT below the grouping. --- crates/types/src/stages/logical/memo/mod.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/types/src/stages/logical/memo/mod.rs b/crates/types/src/stages/logical/memo/mod.rs index 86037c453..2a108ebca 100644 --- a/crates/types/src/stages/logical/memo/mod.rs +++ b/crates/types/src/stages/logical/memo/mod.rs @@ -113,7 +113,9 @@ impl Memo { /// columns form them; /// - the rows its input reads per evaluation /// ([`Self::max_rows_per_evaluation`]): there are never more groups - /// than input rows. + /// than input rows; + /// - the rows the group itself returns: `ORDER BY q DESC LIMIT 5` above + /// `GROUP BY dst` returns at most 5 values, whatever `dst` declares. /// /// Keys from different scans multiply, for a join pairs their values: /// `flows JOIN hosts ... GROUP BY f.src, h.region`, with 6 `src` and 6 @@ -169,7 +171,11 @@ impl Memo { } KeysOfValues::ComputedOrTime => None, }; - smaller_known(by_keys, self.max_rows_per_evaluation(input)) + let by_rows = smaller_known( + self.max_rows_per_evaluation(input), + self.max_rows_per_evaluation(group), + ); + smaller_known(by_keys, by_rows) } /// The most rows one evaluation of `group` returns, from the facts the From 9a54746709d0c6f97cb8112c76e3354842e9733d Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 06:46:38 +0000 Subject: [PATCH 19/24] refactor(types): a semi or anti join skips the right side's lineage; name the kept identity checks The Join arm computed the right side's columns after the left's scans even for a semi or anti join, whose output keeps only the left side's columns. The lint header now names each kept `if let`/`matches!` and why every other variant's answer is "no", and says that arithmetic_sign's tuple match ends in an unchecked `_ => Unknown`, the safe answer there. --- .../stages/logical/group/column_properties.rs | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/crates/types/src/stages/logical/group/column_properties.rs b/crates/types/src/stages/logical/group/column_properties.rs index 06e31f37e..5cfd9c4bc 100644 --- a/crates/types/src/stages/logical/group/column_properties.rs +++ b/crates/types/src/stages/logical/group/column_properties.rs @@ -11,8 +11,17 @@ // Each rule here answers per operator (or per expression, measure or // operator kind) and lists every variant: a wildcard arm would give a new // operator a default lineage, which bounds and coverage then trust, -// instead of failing the build (under `-D warnings`). `matches!` and `let -// ... else` stay only for a check of one variant's identity. +// instead of failing the build (under `-D warnings`). Clippy does not +// check `if let`, `matches!` or a match on a tuple, so they stay only for +// a check of one variant's identity, where every other variant's answer +// is "no" by definition: "is it a source column" (only a copied source +// column has a scan to shift or a lineage to keep), "is it one top-k +// measure" (only a top-k returns its input's ranked rows), "is it `or`" +// (only `or` keeps both sides' rows), and "is it a merge of one +// `SummaryTumblingWindowChain`" (only a chain's windows merge into its +// build over a range). The tuple match in `arithmetic_sign` ends in `_ => +// Unknown`, unchecked, where `Unknown` is the safe answer for any +// operator or sign it does not list. #![warn( clippy::wildcard_enum_match_arm, clippy::match_wildcard_for_single_variants @@ -303,7 +312,12 @@ fn non_asap_columns( JoinKind::Semi | JoinKind::Anti => (false, false, false), }; let mut columns = columns_of(left); - let mut right = after_left_scans(&columns, columns_of(right)); + // A semi or anti join keeps only the left side's columns. + let mut right = if right_kept { + after_left_scans(&columns, columns_of(right)) + } else { + Vec::new() + }; for (side, nullable) in [(&mut columns, left_nullable), (&mut right, right_nullable)] { if nullable { for column in side.iter_mut() { @@ -311,9 +325,7 @@ fn non_asap_columns( } } } - if right_kept { - columns.extend(right); - } + columns.extend(right); columns } SetOp { left, right, .. } => agreed(&[columns_of(left), columns_of(right)]), From e27d1031e5bb601ccfb3ec55b876b72d82bab1ef Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 06:49:20 +0000 Subject: [PATCH 20/24] test(accuracy): check every memo bound the executor runs, and cross few-valued keys first The value-bounds oracle counted a case as run when any one of its bounds ran, so bounds that compile only alone (the n_q of a scalar() or vector() operand, a DISTINCT, a quantile over count_values) went unchecked silently. It now tracks each bound as `case:what` with the most values any dataset returned: EXECUTOR_GAPS lists the bounds the executor cannot run with the operator each lacks, and NEVER_REACHED the bounds that run but return nothing on every dataset (none now). Any other unrun or always-empty bound fails the test. p13_unless read `qot(0.9, m) unless qot(0.5, m)`, empty on every dataset; it now reads `a unless b` over two metrics. The crossed dataset gave the fastest digit to the label sorted first, so `dst` (300 values) cycled while `src` stayed fixed over the 96 rows a window reads. Declared components are now crossed smallest first: against the earlier bounds code the oracle finds j_full's 42 groups over a bound of 36, and j_left's 7 over 6, on the crossed dataset. --- .../tests/memo_value_bounds.rs | 216 +++++++++++++----- .../memo_value_bounds_fixtures/cases.json | 4 +- 2 files changed, 162 insertions(+), 58 deletions(-) diff --git a/crates/integration-tests/tests/memo_value_bounds.rs b/crates/integration-tests/tests/memo_value_bounds.rs index 9d01b4908..1dd193be8 100644 --- a/crates/integration-tests/tests/memo_value_bounds.rs +++ b/crates/integration-tests/tests/memo_value_bounds.rs @@ -77,29 +77,85 @@ const LARGE_SIZES: DatasetSizes = DatasetSizes { random_datasets: 40, }; -/// The default corpus's cases the executor cannot run yet, each with the -/// operator it lacks. A case leaves this list once the executor runs it, -/// so the corpus checks every shape the executor can run. +/// The default corpus's bounds the executor cannot run yet (`case:what`, +/// as [`Report::bounds`] names them), each with the operator it lacks. A +/// bound leaves this list once the executor runs it, so the corpus checks +/// every bound the executor can run. const EXECUTOR_GAPS: &[(&str, &str)] = &[ - ("c_cast_num", "CAST to BIGINT"), - ("c_cast_ts", "CAST to BIGINT"), - ("c_sub_alias", "date_trunc"), - ("c_sub_upper", "upper"), - ("p_hist", "rate"), - ("p_hist_qot", "rate"), - ("p_hist_whole", "rate"), - ("p_labelrep_other", "label_replace"), - ("p_labelrep_same", "label_replace"), - ("p_labelrep_whole", "label_replace"), - ("p_subq", "a subquery"), - ("s1_bucket_nosc", "date_trunc"), - ("s1_bucket_sc", "date_trunc"), - ("s2_src_bucket_sc", "date_trunc"), - ("u_union_mixed", "UNION ALL below a grouping"), - ("u_union_same", "UNION ALL below a grouping"), - ("w_window_key", "row_number() as a key"), + ("c_cast_num:g3 Aggregate", "CAST to BIGINT"), + ("c_cast_num:g4 Project", "CAST to BIGINT"), + ("c_cast_num:n_q", "CAST to BIGINT"), + ("c_cast_ts:g3 Aggregate", "CAST to BIGINT"), + ("c_cast_ts:g4 Project", "CAST to BIGINT"), + ("c_cast_ts:n_q", "CAST to BIGINT"), + ("c_sub_alias:g3 Aggregate", "date_trunc"), + ("c_sub_alias:g4 Project", "date_trunc"), + ("c_sub_alias:n_q", "date_trunc"), + ("c_sub_upper:g3 Aggregate", "upper"), + ("c_sub_upper:g4 Project", "upper"), + ("c_sub_upper:n_q", "upper"), + ("d_distinct:g4 Dedup", "DISTINCT"), + ("d_distinct:n_q", "DISTINCT"), + ( + "p11_scalar_pv:g5 Project", + "a binary operator over scalar()", + ), + ("p11_scalar_pv:n_q", "a binary operator over scalar()"), + ("p12_vector_or:n_q", "`or` over vector()"), + ("p_cv_by:g3 Aggregate", "a quantile over count_values"), + ("p_cv_by:n_q", "a quantile over count_values"), + ("p_cv_key:g3 Aggregate", "a quantile over count_values"), + ("p_cv_key:n_q", "a quantile over count_values"), + ("p_hist:g2 Aggregate", "rate"), + ("p_hist:g3 Aggregate", "rate"), + ("p_hist:g4 Aggregate", "rate"), + ("p_hist:n_q", "rate"), + ("p_hist_qot:g2 Aggregate", "rate"), + ("p_hist_qot:g3 Aggregate", "rate"), + ("p_hist_qot:n_q", "rate"), + ("p_hist_whole:g2 Aggregate", "rate"), + ("p_hist_whole:g3 Aggregate", "rate"), + ("p_hist_whole:g4 Aggregate", "rate"), + ("p_hist_whole:n_q", "rate"), + ("p_labelrep_other:g3 Aggregate", "label_replace"), + ("p_labelrep_other:n_q", "label_replace"), + ("p_labelrep_qot_whole:g4 Aggregate", "label_replace"), + ("p_labelrep_qot_whole:n_q", "label_replace"), + ("p_labelrep_same:g3 Aggregate", "label_replace"), + ("p_labelrep_same:n_q", "label_replace"), + ("p_labelrep_whole:g3 Aggregate", "label_replace"), + ("p_labelrep_whole:n_q", "label_replace"), + ("p_subq:g3 Aggregate", "a subquery"), + ("p_subq:g4 Aggregate", "a subquery"), + ("p_subq:n_q", "a subquery"), + ("p_subq_outer:g4 Aggregate", "a subquery"), + ("p_subq_outer:n_q", "a subquery"), + ("s1_bucket_nosc:g3 Aggregate", "date_trunc"), + ("s1_bucket_nosc:g4 Project", "date_trunc"), + ("s1_bucket_nosc:n_q", "date_trunc"), + ("s1_bucket_sc:g3 Aggregate", "date_trunc"), + ("s1_bucket_sc:g4 Project", "date_trunc"), + ("s1_bucket_sc:n_q", "date_trunc"), + ("s2_src_bucket_sc:g3 Aggregate", "date_trunc"), + ("s2_src_bucket_sc:g4 Project", "date_trunc"), + ("s2_src_bucket_sc:n_q", "date_trunc"), + ("u_union_mixed:g6 Aggregate", "UNION ALL below a grouping"), + ("u_union_mixed:g7 Project", "UNION ALL below a grouping"), + ("u_union_mixed:n_q", "UNION ALL below a grouping"), + ("u_union_same:g8 Aggregate", "UNION ALL below a grouping"), + ("u_union_same:g9 Project", "UNION ALL below a grouping"), + ("u_union_same:n_q", "UNION ALL below a grouping"), + ("w_window_key:g4 Aggregate", "row_number() as a key"), + ("w_window_key:g5 Project", "row_number() as a key"), + ("w_window_key:n_q", "row_number() as a key"), ]; +/// The default corpus's bounds the executor runs but that return no values +/// on every dataset, so nothing tests them (`case:what`), each with why no +/// dataset the facts allow fills them. Empty: every bound that runs is +/// reached. +const NEVER_REACHED: &[(&str, &str)] = &[]; + /// The default corpus's cases the frontend does not lower. const FRONTEND_GAPS: &[&str] = &[ // `src || dst`: string concatenation. @@ -109,19 +165,18 @@ const FRONTEND_GAPS: &[&str] = &[ ]; /// Every memo bound of the default corpus holds on every dataset; every -/// case runs on at least one dataset, but for the executor's and the -/// frontend's known gaps. +/// bound runs on at least one dataset and returns values on one, but for +/// the executor's and the frontend's known gaps. #[test] fn memo_value_bounds_hold_on_data_the_declared_facts_allow() { let report = check_corpus(&default_corpus(), DEFAULT_SIZES); report.assert_sound(); - let never_run: Vec<&str> = report - .never_executed - .iter() - .map(|case| case.split(':').next().unwrap()) - .collect(); - let gaps: Vec<&str> = EXECUTOR_GAPS.iter().map(|(case, _)| *case).collect(); - assert_eq!(never_run, gaps, "{:#?}", report.never_executed); + let never_executed = report.never_executed(); + let never_run: Vec<&str> = never_executed.iter().map(|(bound, _)| *bound).collect(); + let gaps: Vec<&str> = EXECUTOR_GAPS.iter().map(|(bound, _)| *bound).collect(); + assert_eq!(never_run, gaps, "{never_executed:#?}"); + let unreached: Vec<&str> = NEVER_REACHED.iter().map(|(bound, _)| *bound).collect(); + assert_eq!(report.never_reached(), unreached); assert_eq!(report.not_lowered, FRONTEND_GAPS); } @@ -275,27 +330,58 @@ struct Violation { observed: u64, } +/// What the datasets showed of one bound. +#[derive(Default)] +struct BoundCoverage { + /// The most values any dataset returned; `None` when the executor ran + /// the bound on no dataset. + largest_observed: Option, + /// Why the executor last rejected the bound or a dataset for it. + last_rejection: String, +} + #[derive(Default)] struct Report { cases: usize, - bounds_checked: usize, executions: usize, rejected_datasets: usize, - never_executed: Vec, + /// Each bound, as `case:what` (`p13_unless:n_q`, `d_distinct:g4 + /// Dedup`), with what the datasets showed of it. + bounds: BTreeMap, not_lowered: Vec, violations: Vec, } impl Report { + /// The bounds no dataset ran, with why, in `case:what` order. + fn never_executed(&self) -> Vec<(&str, &str)> { + self.bounds + .iter() + .filter(|(_, coverage)| coverage.largest_observed.is_none()) + .map(|(bound, coverage)| (bound.as_str(), coverage.last_rejection.as_str())) + .collect() + } + + /// The bounds every dataset ran returned no values for, so no dataset + /// tested them. + fn never_reached(&self) -> Vec<&str> { + self.bounds + .iter() + .filter(|(_, coverage)| coverage.largest_observed == Some(0)) + .map(|(bound, _)| bound.as_str()) + .collect() + } + fn assert_sound(&self) { println!( "memo value bounds: {} cases, {} bounds, {} executions, {} datasets the executor \ - rejected, {} cases never run, not lowered: {:?}, {} violations", + rejected, {} bounds never run, {} never reached, not lowered: {:?}, {} violations", self.cases, - self.bounds_checked, + self.bounds.len(), self.executions, self.rejected_datasets, - self.never_executed.len(), + self.never_executed().len(), + self.never_reached().len(), self.not_lowered, self.violations.len() ); @@ -352,14 +438,12 @@ fn check_corpus(corpus: &[Case], sizes: DatasetSizes) -> Report { let mut report = Report::default(); for part in reports { report.cases += part.cases; - report.bounds_checked += part.bounds_checked; report.executions += part.executions; report.rejected_datasets += part.rejected_datasets; - report.never_executed.extend(part.never_executed); + report.bounds.extend(part.bounds); report.not_lowered.extend(part.not_lowered); report.violations.extend(part.violations); } - report.never_executed.sort(); report.not_lowered.sort(); report } @@ -388,9 +472,6 @@ fn check_case(case: &Case, sizes: DatasetSizes, report: &mut Report) { }; let mut memo = Memo::new(&data); let mut group_of_node = HashMap::new(); - let mut ran_once = false; - let mut bounded = false; - let mut last_rejection = String::new(); for (index, root) in roots.iter().enumerate() { let QueryRoot::Operator(root) = root else { continue; @@ -404,23 +485,38 @@ fn check_case(case: &Case, sizes: DatasetSizes, report: &mut Report) { if bounds.is_empty() { continue; } - bounded = true; let query = QueryBounds { root: root.clone(), bounds, }; - report.bounds_checked += query.bounds.len(); + // Each bound's coverage entry: `case:what`, the query's index + // added when the case has several. + let keys: Vec = query + .bounds + .iter() + .map(|(what, _, _)| match roots.len() { + 1 => format!("{}:{what}", case.name), + _ => format!("{} query {index}:{what}", case.name), + }) + .collect(); + for key in &keys { + report.bounds.entry(key.clone()).or_default(); + } // The bounds whose nodes the executor compiles: all together, else // each alone. let all: Vec = (0..query.bounds.len()).collect(); let runnable: Vec = match compiles(&query, &all) { Ok(()) => all, - Err(reason) => { - last_rejection = reason; - all.into_iter() - .filter(|&bound| compiles(&query, &[bound]).is_ok()) - .collect() - } + Err(_) => all + .into_iter() + .filter(|&bound| match compiles(&query, &[bound]) { + Ok(()) => true, + Err(reason) => { + report.bounds.get_mut(&keys[bound]).unwrap().last_rejection = reason; + false + } + }) + .collect(), }; if runnable.is_empty() { continue; @@ -428,9 +524,10 @@ fn check_case(case: &Case, sizes: DatasetSizes, report: &mut Report) { for dataset in datasets(case, &query.root, &data, sizes) { match execute_counts(&query, &runnable, &dataset) { Ok(counts) => { - ran_once = true; report.executions += 1; for (&bound_index, observed) in runnable.iter().zip(counts) { + let coverage = report.bounds.get_mut(&keys[bound_index]).unwrap(); + coverage.largest_observed = coverage.largest_observed.max(Some(observed)); let (what, _, bound) = &query.bounds[bound_index]; if observed > *bound { report.violations.push(Violation { @@ -446,16 +543,17 @@ fn check_case(case: &Case, sizes: DatasetSizes, report: &mut Report) { } Err(reason) => { report.rejected_datasets += 1; - last_rejection = reason; + for &bound_index in &runnable { + report + .bounds + .get_mut(&keys[bound_index]) + .unwrap() + .last_rejection = reason.clone(); + } } } } } - if bounded && !ran_once { - report - .never_executed - .push(format!("{}: {last_rejection}", case.name)); - } } fn lower(case: &Case) -> Result, String> { @@ -1009,7 +1107,7 @@ fn scanned_columns(root: &OperatorNode, out: &mut BTreeMap *combinations, + ValueSet::Literals { .. } | ValueSet::Free { .. } => 0, + }); let components = components .into_iter() .map(|values| { diff --git a/crates/integration-tests/tests/memo_value_bounds_fixtures/cases.json b/crates/integration-tests/tests/memo_value_bounds_fixtures/cases.json index da505bd63..8bd46134c 100644 --- a/crates/integration-tests/tests/memo_value_bounds_fixtures/cases.json +++ b/crates/integration-tests/tests/memo_value_bounds_fixtures/cases.json @@ -342,8 +342,8 @@ { "name": "p13_unless", "language": "promql", - "facts": "f01", - "query": "quantile_over_time(0.9, m[5m]) unless quantile_over_time(0.5, m[5m])" + "facts": "f13", + "query": "quantile_over_time(0.9, a[5m]) unless quantile_over_time(0.5, b[5m])" }, { "name": "p14_share", From 9a9bee559a8ba18d2c80fa3c68ff2c63006759f3 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:50:32 +0000 Subject: [PATCH 21/24] test(cost): the KLL test reads Stage 1's offered k; the distinct rollup expects KMV at its planned 600M-row shape MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The δ split offers stored KLL windows at several k, so the KLL test compares the smallest k offered with the next larger one instead of fixed values. The distinct rollup test asserted exact counts. Measured, KMV is 1.26x faster at 60,000 rows per state (the planned shape) and exact is 1.39x faster at 12,000. The test is renamed for what it asserts and records the crossover. --- .../tests/default_cost_plan_ranking.rs | 86 ++++++++++++++----- .../tests/rollup_controlled.rs | 46 ++++++---- 2 files changed, 91 insertions(+), 41 deletions(-) diff --git a/crates/integration-tests/tests/default_cost_plan_ranking.rs b/crates/integration-tests/tests/default_cost_plan_ranking.rs index 603072769..31fed4477 100644 --- a/crates/integration-tests/tests/default_cost_plan_ranking.rs +++ b/crates/integration-tests/tests/default_cost_plan_ranking.rs @@ -15,12 +15,16 @@ //! 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 std::collections::BTreeSet; + use asap_cost::CostInputs; -use asap_devtools::planning_stages::{lower_promql_roots, run_planning_stages_over}; +use asap_devtools::planning_stages::{ + lower_promql_roots, run_planning_stages_over, PlanningStagesRun, +}; 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::ir::{ASAPOp, Operator}; use asap_types::plan::physical_asap_dag::{PhysicalASAPDAG, PhysicalASAPOperatorPayload}; use asap_types::workload::PlanningWorkload; @@ -85,9 +89,9 @@ fn workload(query: Query, setting: Setting) -> PlanningWorkload { .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) { +/// The planning stages' run over `workload` under the default cost inputs, +/// with raw data kept or not. +fn planning_run(workload: &PlanningWorkload, raw_data_retained: bool) -> PlanningStagesRun { let settings = CostInputs::default(); let capabilities = DeploymentCapabilities { raw_data_retained, @@ -99,7 +103,13 @@ fn selected(workload: &PlanningWorkload, raw_data_retained: bool) -> (PhysicalAS 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(); + run_planning_stages_over(roots, &entries, &data, models).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 run = planning_run(workload, raw_data_retained); let (plan, _) = run.run.selection.as_ref().expect("a plan is selected"); let cpu = predicted_cpu(plan, workload); (plan.clone(), cpu) @@ -314,21 +324,53 @@ fn with_kll_k(plan: &PhysicalASAPDAG, k: u32) -> PhysicalASAPDAG { } /// `quantile_over_time(0.99, latency[1h])`, 1% rank, at 1 Hz every 10 s and -/// every 60 s, raw data not kept. Stage 2 offers stored KLL windows at -/// k = 1,404 and at k = 547; merging 360 windows of 10 rows per series -/// measures 4.8 ms per series at k = 1,404 and 1.7 ms at k = 547 (the -/// merged sketch grows towards its `k`). Measured cost per second, k = -/// 1,404 against k = 547: 589 against 240 every 10 s, 31.4 against 19.4 -/// every 60 s. Charged a flat merge per state, both cost the same to the -/// last digit and the MILP picked either. It now selects the smaller `k`. +/// every 60 s, raw data not kept. Stage 1 offers stored KLL windows at +/// several `k` (269, 547, 1,404 and 2,863: one per split of the failure +/// probability over the windows' merge). Merging 360 windows of 10 rows per +/// series measures 4.8 ms per series at k = 1,404 and 1.7 ms at k = 547 +/// (the merged sketch grows towards its `k`). Measured cost per second, +/// k = 1,404 against k = 547: 589 against 240 every 10 s, 31.4 against 19.4 +/// every 60 s. Charged a flat merge per state, every `k` cost the same to +/// the last digit and the MILP picked any. The plan now keeps the smallest +/// `k` offered, and the next larger one is charged more. #[test] fn quantile_rank_without_raw_data_charges_the_larger_kll_more() { use asap_types::summary_type::SketchParams; for setting in [ONE_HZ_EVERY_10_S, ONE_HZ_EVERY_60_S] { let w = workload(QUANTILE_RANK, setting); - let (plan, _) = selected(&w, false); - assert_eq!(plan_kind(&plan), PlanKind::StoredStates, "{setting:?}"); - let kll_ks: Vec = plan + let run = planning_run(&w, false); + // The `k`s of the stored KLL windows (tumbling-window chains) + // Stage 1 offers, smallest first. + let offered_kll_ks: Vec = run + .run + .logical_asap_candidates + .as_ref() + .expect("Stage 1 ran") + .memo + .groups() + .iter() + .flat_map(|group| &group.candidates) + .filter(|candidate| { + matches!( + candidate.expression.operator, + Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) + ) + }) + .filter_map( + |candidate| match candidate.logical_properties.parameters() { + Some(SketchParams::Kll { k }) => Some(*k), + _ => None, + }, + ) + .collect::>() + .into_iter() + .collect(); + let (plan, _) = run.run.selection.as_ref().expect("a plan is selected"); + assert_eq!(plan_kind(plan), PlanKind::StoredStates, "{setting:?}"); + let [smallest, next_larger, ..] = offered_kll_ks[..] else { + panic!("{setting:?}: Stage 1 offers stored KLL windows at k {offered_kll_ks:?}"); + }; + let selected_kll_ks: Vec = plan .nodes .iter() .filter_map(|node| match node.parameters { @@ -337,14 +379,14 @@ fn quantile_rank_without_raw_data_charges_the_larger_kll_more() { }) .collect(); assert!( - !kll_ks.is_empty() && kll_ks.iter().all(|&k| k == 547), - "{setting:?}: selected KLL k {kll_ks:?}, the smallest offered is 547" + !selected_kll_ks.is_empty() && selected_kll_ks.iter().all(|&k| k == smallest), + "{setting:?}: selected KLL k {selected_kll_ks:?}, offered {offered_kll_ks:?}" ); let entries: Vec<_> = w.query_workload.entries().collect(); let data = w.data_workload.clone().unwrap(); let cost = |k| { let (cost, _) = asap_cost::plan_cost( - &with_kll_k(&plan, k), + &with_kll_k(plan, k), &entries, &data, &CostInputs::default(), @@ -352,10 +394,10 @@ fn quantile_rank_without_raw_data_charges_the_larger_kll_more() { .unwrap(); cost.cost_per_second.unwrap() }; - let (large, small) = (cost(1_404), cost(547)); + let (large, small) = (cost(next_larger), cost(smallest)); assert!( - large > 1.25 * small, - "{setting:?}: k = 1,404 predicted {large}/s, k = 547 {small}/s; measured 1.6-2.5x" + large > small, + "{setting:?}: k = {next_larger} predicted {large}/s, k = {smallest} {small}/s" ); } } diff --git a/crates/integration-tests/tests/rollup_controlled.rs b/crates/integration-tests/tests/rollup_controlled.rs index 4458f05d1..598d27867 100644 --- a/crates/integration-tests/tests/rollup_controlled.rs +++ b/crates/integration-tests/tests/rollup_controlled.rs @@ -5,9 +5,10 @@ use asap_planner::PlanningModels; use asap_types::{ ir::{ - schema::{DataType, Field, Schema}, + schema::{DataType, Field, FieldDataType, Schema}, ASAPOp, OperatorNode, QueryRoot, }, + summary_type::SketchAlgorithm, workload::PlanningWorkload, }; use std::rc::Rc; @@ -42,20 +43,24 @@ fn nodes(roots: &[QueryRoot]) -> Vec> { } /// The viewer's distinct-count rollup workload, `COUNT(DISTINCT user_id)` -/// by service and by (service, endpoint) over 5 min every 60 s: the -/// production entrypoint counts both exactly, the measured-better plan. A -/// KMV build compares each row with the hashes its state retains, and a -/// rollup inserts each state's hashes the same way. The planner sees -/// 600,000,000 rows per evaluation and 10,000 groups for each grouping, -/// and sizes the KMV at k = 26,656. Measured on the executor at that -/// shape (k = 26,656, 1,000,000 distinct users, grouped), a KMV build -/// takes 7.8 µs per row and an exact count 0.59 µs, 13x apart. The -/// planner charges the KMV plan 3.9x the exact counts (0.00154 against -/// 0.00039 per second; 2.8x while the rollup by `service` was charged as -/// passing one state per group through). Charged a fixed 48 per row and -/// per byte merged, the KMV rollup was selected. +/// by service and by (service, endpoint) over 5 min every 60 s, at 2,000,000 +/// rows per second: 600,000,000 rows per evaluation, 10,000 groups for each +/// grouping and 1,000,000 users. The production entrypoint answers both +/// queries from a KMV (k = 4,323), the plan that measures faster at the +/// largest shape measured. +/// +/// The better plan depends on the rows per state. Measured on the executor, +/// both plans over the same data: +/// - 60,000,000 rows (60,000 rows per state, scaled ×10 to one evaluation): +/// exact 609.6 s, KMV 483.6 s. KMV is 1.26x faster. +/// - 12,000,000 rows (12,000 rows per state): exact 12.79 s, KMV 17.84 s. +/// Exact is 1.39x faster. +/// +/// So the crossover lies between 12,000 and 60,000 rows per state. The +/// planned shape (600,000,000 rows over 10,000 groups) has 60,000 rows per +/// state, as the 60,000,000-row run does, so the test expects KMV. #[test] -fn production_distinct_rollup_counts_exactly() { +fn production_distinct_rollup_builds_kmv_at_600m_rows_per_evaluation() { let (workload, roots) = fixture(); let entries: Vec<_> = workload.query_workload.entries().collect(); let data = workload.data_workload.as_ref().unwrap(); @@ -78,18 +83,21 @@ fn production_distinct_rollup_counts_exactly() { let accepted = &selection.accepted_accuracy_per_query; assert_eq!(accepted.len(), 2); assert!( - accepted.iter().all(|accuracy| accuracy.sketches.is_empty()), - "{accepted:?}" + accepted.iter().all(|accuracy| accuracy.sketches == ["Kmv"]), + "both queries read a KMV: {accepted:?}" ); let roots = plan.query_roots().unwrap(); - let builds = nodes(&roots) + let kmv_builds = nodes(&roots) .iter() .filter(|node| { matches!( node.asap(), - Some(ASAPOp::SummaryAgg { .. } | ASAPOp::SummaryTumblingWindowChain { .. }) + Some(ASAPOp::SummaryAgg { + summary_type: FieldDataType::Sketch(SketchAlgorithm::Kmv), + .. + }) ) }) .count(); - assert_eq!(builds, 0, "the selected plan builds no summary"); + assert!(kmv_builds > 0, "the selected plan builds a KMV"); } From 83f9f7ad5b36028faf0203caa5ee16e1f59c8b4d Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 19:03:05 +0000 Subject: [PATCH 22/24] test(cost): the KLL test checks the measured k pair again; name the rollup test by rows per state --- .../tests/default_cost_plan_ranking.rs | 10 +++++++++- crates/integration-tests/tests/rollup_controlled.rs | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/crates/integration-tests/tests/default_cost_plan_ranking.rs b/crates/integration-tests/tests/default_cost_plan_ranking.rs index 31fed4477..e654b964a 100644 --- a/crates/integration-tests/tests/default_cost_plan_ranking.rs +++ b/crates/integration-tests/tests/default_cost_plan_ranking.rs @@ -332,7 +332,9 @@ fn with_kll_k(plan: &PhysicalASAPDAG, k: u32) -> PhysicalASAPDAG { /// k = 1,404 against k = 547: 589 against 240 every 10 s, 31.4 against 19.4 /// every 60 s. Charged a flat merge per state, every `k` cost the same to /// the last digit and the MILP picked any. The plan now keeps the smallest -/// `k` offered, and the next larger one is charged more. +/// `k` offered, k = 1,404 is charged over 1.25x more than k = 547 (the +/// measured pair), and the next larger `k` than the smallest is charged +/// more (not measured). #[test] fn quantile_rank_without_raw_data_charges_the_larger_kll_more() { use asap_types::summary_type::SketchParams; @@ -394,6 +396,12 @@ fn quantile_rank_without_raw_data_charges_the_larger_kll_more() { .unwrap(); cost.cost_per_second.unwrap() }; + assert!( + cost(1_404) > 1.25 * cost(547), + "{setting:?}: k = 1,404 predicted {}/s, k = 547 {}/s; measured 1.6-2.5x", + cost(1_404), + cost(547) + ); let (large, small) = (cost(next_larger), cost(smallest)); assert!( large > small, diff --git a/crates/integration-tests/tests/rollup_controlled.rs b/crates/integration-tests/tests/rollup_controlled.rs index 598d27867..a056a3f70 100644 --- a/crates/integration-tests/tests/rollup_controlled.rs +++ b/crates/integration-tests/tests/rollup_controlled.rs @@ -60,7 +60,7 @@ fn nodes(roots: &[QueryRoot]) -> Vec> { /// planned shape (600,000,000 rows over 10,000 groups) has 60,000 rows per /// state, as the 60,000,000-row run does, so the test expects KMV. #[test] -fn production_distinct_rollup_builds_kmv_at_600m_rows_per_evaluation() { +fn production_distinct_rollup_builds_kmv_at_60k_rows_per_state() { let (workload, roots) = fixture(); let entries: Vec<_> = workload.query_workload.entries().collect(); let data = workload.data_workload.as_ref().unwrap(); From 52094325e9cab3bf4ecfa69963dfd21353be7f4e Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:43:35 +0000 Subject: [PATCH 23/24] fix(accuracy): the redundant-level check reads the state's sign and distinct bound, as the edge check does After the rebase onto main, theoretical_guarantee takes the read state's value sign and its sources' distinct bound (DDSketch is certified over values of one sign, classic HLL over a bounded source). build_meets_level now takes the readout's state group and passes both, so dropping a redundant readout level uses the same guarantee as the logical edge check. The `or`/ratio readout test declares its values non-negative, so its DDSketch builds are certified and the ratio keeps the composed grid. --- crates/accuracy/src/logical_edge/mod.rs | 13 +++++++++++-- crates/accuracy/src/readout_caps/mod.rs | 23 ++++++++++++++++++++--- crates/accuracy/src/readout_caps/tests.rs | 9 ++++++++- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/crates/accuracy/src/logical_edge/mod.rs b/crates/accuracy/src/logical_edge/mod.rs index 30229470f..9036f1aef 100644 --- a/crates/accuracy/src/logical_edge/mod.rs +++ b/crates/accuracy/src/logical_edge/mod.rs @@ -6,7 +6,9 @@ use asap_types::accuracy::{AccuracyGuarantee, AccuracyMetric}; use asap_types::ir::schema::FieldDataType; use asap_types::ir::{ASAPOp, Operator}; -use asap_types::stages::{CandidateForMemoGroupId, Memo, RequiredAccuracy, RootPruneReason}; +use asap_types::stages::{ + CandidateForMemoGroupId, Memo, MemoGroupId, RequiredAccuracy, RootPruneReason, +}; use asap_types::summary_type::{MergedError, SketchParams, SketchStatistic, SummaryTypeCapability}; use crate::guarantee::{ @@ -188,10 +190,13 @@ impl ErrorMetricsOfMemo { /// level `required` for `statistic`, as the edge from the build checks it /// ([`ErrorMetricsOfMemo::edge_meets_logical_requirements`]): its guarantee /// at the required failure probability (`evidence`'s measurement, else the -/// theoretical one) is in the required metric and within the required +/// theoretical one over the sign and distinct bound of the state group +/// `summary_input`) is in the required metric and within the required /// error. A KLL build of k = 1404 meets rank error 0.0025 at δ = 0.01, not /// at δ = 0.00125. pub(crate) fn build_meets_level( + memo: &Memo, + summary_input: MemoGroupId, summary_type: &FieldDataType, parameters: &SketchParams, statistic: &SketchStatistic, @@ -211,6 +216,10 @@ pub(crate) fn build_meets_level( parameters, statistic, required.failure_probability, + memo.groups()[summary_input.0 as usize] + .group_properties + .state_column_sign(), + memo.max_distinct_per_evaluation(summary_input), ) }); guarantee_meets(guarantee.as_ref(), required) diff --git a/crates/accuracy/src/readout_caps/mod.rs b/crates/accuracy/src/readout_caps/mod.rs index f76cd3de1..6ada0ba8d 100644 --- a/crates/accuracy/src/readout_caps/mod.rs +++ b/crates/accuracy/src/readout_caps/mod.rs @@ -243,13 +243,30 @@ fn builds_meeting( ) -> Vec { readouts .iter() - .filter_map(|&readout| sketch_readout(memo, memo.candidate(readout))) - .flat_map(|(summary_type, statistic)| { + .filter_map(|&readout| { + let candidate = memo.candidate(readout); + let Operator::ASAP(ASAPOp::SummaryEstimate { summary_input, .. }) = + candidate.expression.operator + else { + return None; + }; + let (summary_type, statistic) = sketch_readout(memo, candidate)?; + Some((summary_input, summary_type, statistic)) + }) + .flat_map(|(summary_input, summary_type, statistic)| { let FieldDataType::Sketch(algorithm) = summary_type else { unreachable!("a sketch readout reads a sketch state") }; parameter_list(algorithm).iter().map(move |parameters| { - build_meets_level(&summary_type, parameters, statistic, level, evidence) + build_meets_level( + memo, + summary_input, + &summary_type, + parameters, + statistic, + level, + evidence, + ) }) }) .collect() diff --git a/crates/accuracy/src/readout_caps/tests.rs b/crates/accuracy/src/readout_caps/tests.rs index 478cdcab3..47f49a058 100644 --- a/crates/accuracy/src/readout_caps/tests.rs +++ b/crates/accuracy/src/readout_caps/tests.rs @@ -204,13 +204,20 @@ fn an_and_combines_no_readout_errors() { /// each copied from one of the 200 readout values below it, so δ = 0.01 /// splits over 100 of them: 1e-4 per value (min(200, 100 × 1)). A ratio /// of the same readouts combines both errors and keeps the composed grid. +/// The values are non-negative, so DDSketch's guarantee holds. #[test] fn an_or_takes_each_value_from_one_readout() { let target = AccuracyTarget::max_error(0.05) .failure_probability(0.01) .accuracy_metric(AccuracyMetric::Relative); + let mut data = with_series("m", 100); + data.sources + .get_mut("m") + .expect("m") + .column_signs + .insert("value".into(), ValueSign::NonNegative); let readouts_under = |operator: fn(MemoGroupId, MemoGroupId) -> _| { - let mut memo = Memo::new(&with_series("m", 100)); + let mut memo = Memo::new(&data); let (_, state, p90) = quantile_over_time(&mut memo, SketchAlgorithm::DDSketch, 0.9, 300); let p50 = estimate(&mut memo, state, SketchStatistic::Quantile { q: 0.5 }); let root = group(&mut memo, operator(p90, p50)); From 386ea88fb5451f18b1fec9481b456def8bc47d3b Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:54:26 +0000 Subject: [PATCH 24/24] test(accuracy): the executor now runs casts to BIGINT, DISTINCT, rate, label_replace, scalar(), UNION ALL and row_number(): drop them from the memo bounds' executor gaps After the rebase onto main, the executor runs 36 more of the corpus's bounds; all hold. The gaps left are a timestamp cast, date_trunc, upper, a quantile over count_values and subqueries. --- .../tests/memo_value_bounds.rs | 45 ++----------------- 1 file changed, 3 insertions(+), 42 deletions(-) diff --git a/crates/integration-tests/tests/memo_value_bounds.rs b/crates/integration-tests/tests/memo_value_bounds.rs index 1dd193be8..2fe97e3c4 100644 --- a/crates/integration-tests/tests/memo_value_bounds.rs +++ b/crates/integration-tests/tests/memo_value_bounds.rs @@ -82,49 +82,19 @@ const LARGE_SIZES: DatasetSizes = DatasetSizes { /// bound leaves this list once the executor runs it, so the corpus checks /// every bound the executor can run. const EXECUTOR_GAPS: &[(&str, &str)] = &[ - ("c_cast_num:g3 Aggregate", "CAST to BIGINT"), - ("c_cast_num:g4 Project", "CAST to BIGINT"), - ("c_cast_num:n_q", "CAST to BIGINT"), - ("c_cast_ts:g3 Aggregate", "CAST to BIGINT"), - ("c_cast_ts:g4 Project", "CAST to BIGINT"), - ("c_cast_ts:n_q", "CAST to BIGINT"), + ("c_cast_ts:g3 Aggregate", "CAST of a timestamp to BIGINT"), + ("c_cast_ts:g4 Project", "CAST of a timestamp to BIGINT"), + ("c_cast_ts:n_q", "CAST of a timestamp to BIGINT"), ("c_sub_alias:g3 Aggregate", "date_trunc"), ("c_sub_alias:g4 Project", "date_trunc"), ("c_sub_alias:n_q", "date_trunc"), ("c_sub_upper:g3 Aggregate", "upper"), ("c_sub_upper:g4 Project", "upper"), ("c_sub_upper:n_q", "upper"), - ("d_distinct:g4 Dedup", "DISTINCT"), - ("d_distinct:n_q", "DISTINCT"), - ( - "p11_scalar_pv:g5 Project", - "a binary operator over scalar()", - ), - ("p11_scalar_pv:n_q", "a binary operator over scalar()"), - ("p12_vector_or:n_q", "`or` over vector()"), ("p_cv_by:g3 Aggregate", "a quantile over count_values"), ("p_cv_by:n_q", "a quantile over count_values"), ("p_cv_key:g3 Aggregate", "a quantile over count_values"), ("p_cv_key:n_q", "a quantile over count_values"), - ("p_hist:g2 Aggregate", "rate"), - ("p_hist:g3 Aggregate", "rate"), - ("p_hist:g4 Aggregate", "rate"), - ("p_hist:n_q", "rate"), - ("p_hist_qot:g2 Aggregate", "rate"), - ("p_hist_qot:g3 Aggregate", "rate"), - ("p_hist_qot:n_q", "rate"), - ("p_hist_whole:g2 Aggregate", "rate"), - ("p_hist_whole:g3 Aggregate", "rate"), - ("p_hist_whole:g4 Aggregate", "rate"), - ("p_hist_whole:n_q", "rate"), - ("p_labelrep_other:g3 Aggregate", "label_replace"), - ("p_labelrep_other:n_q", "label_replace"), - ("p_labelrep_qot_whole:g4 Aggregate", "label_replace"), - ("p_labelrep_qot_whole:n_q", "label_replace"), - ("p_labelrep_same:g3 Aggregate", "label_replace"), - ("p_labelrep_same:n_q", "label_replace"), - ("p_labelrep_whole:g3 Aggregate", "label_replace"), - ("p_labelrep_whole:n_q", "label_replace"), ("p_subq:g3 Aggregate", "a subquery"), ("p_subq:g4 Aggregate", "a subquery"), ("p_subq:n_q", "a subquery"), @@ -139,15 +109,6 @@ const EXECUTOR_GAPS: &[(&str, &str)] = &[ ("s2_src_bucket_sc:g3 Aggregate", "date_trunc"), ("s2_src_bucket_sc:g4 Project", "date_trunc"), ("s2_src_bucket_sc:n_q", "date_trunc"), - ("u_union_mixed:g6 Aggregate", "UNION ALL below a grouping"), - ("u_union_mixed:g7 Project", "UNION ALL below a grouping"), - ("u_union_mixed:n_q", "UNION ALL below a grouping"), - ("u_union_same:g8 Aggregate", "UNION ALL below a grouping"), - ("u_union_same:g9 Project", "UNION ALL below a grouping"), - ("u_union_same:n_q", "UNION ALL below a grouping"), - ("w_window_key:g4 Aggregate", "row_number() as a key"), - ("w_window_key:g5 Project", "row_number() as a key"), - ("w_window_key:n_q", "row_number() as a key"), ]; /// The default corpus's bounds the executor runs but that return no values