diff --git a/crates/accuracy/src/logical_edge/mod.rs b/crates/accuracy/src/logical_edge/mod.rs index f2f8fe857..9036f1aef 100644 --- a/crates/accuracy/src/logical_edge/mod.rs +++ b/crates/accuracy/src/logical_edge/mod.rs @@ -3,12 +3,17 @@ //! 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, MemoGroupId, 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 +174,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 +186,58 @@ 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 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, + 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, + 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) +} + +/// 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 3d3016bf3..6ada0ba8d 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}; +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. @@ -48,19 +52,21 @@ 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, } /// 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 -/// 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 @@ -72,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 @@ -82,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(); @@ -106,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 @@ -114,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) }) @@ -143,7 +173,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), @@ -163,39 +193,110 @@ 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| { + 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( + memo, + summary_input, + &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 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: ReadoutValueCounts, 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 +409,21 @@ 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 (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, } /// The [`ReadoutsReadByQueries`] of `memo`, in one walk up `order` (the @@ -329,19 +436,24 @@ 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 = ReadoutValueCounts { + 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 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] != Some(0) + readout_values_per_group[children[*input].0 as usize].per_evaluation != Some(0) && contract_per_metric .iter() .flatten() @@ -351,21 +463,39 @@ 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 }; 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 { + per_input.try_fold(0u64, |most, values| Some(most.max(values?))) + } else { + per_input.try_fold(0u64, |total, values| total.checked_add(values?)) + } + }); + most_readout_values = most_readout_values.larger(ReadoutValueCounts { + per_evaluation, + per_output_value, + }); } if most_inputs_reading_with_error > 1 { composed.extend( @@ -375,18 +505,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) { - memo.max_values_per_evaluation(group) + 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 }; } - 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); + *total = total.plus(readout_values_per_group[root.0 as usize]); } ReadoutsReadByQueries { composed, @@ -394,20 +546,54 @@ 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. 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( + 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 -/// 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 cb19c80e4..47f49a058 100644 --- a/crates/accuracy/src/readout_caps/tests.rs +++ b/crates/accuracy/src/readout_caps/tests.rs @@ -1,23 +1,31 @@ use super::*; +use crate::NoMeasuredGuarantees; +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::summary_type::{SketchAlgorithm, SketchStatistic}; use asap_types::test_support::memo_builders::{ - estimate, group, own_candidate, quantile_over_time, query, scan_group, sketch_state, + 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`. 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 @@ -192,17 +200,24 @@ 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. +/// 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)); @@ -219,7 +234,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 +319,479 @@ 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, + &NoMeasuredGuarantees, + ) + .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). 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)); + 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), + 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 +/// 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]); +} diff --git a/crates/integration-tests/tests/default_cost_plan_ranking.rs b/crates/integration-tests/tests/default_cost_plan_ranking.rs index 603072769..e654b964a 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,55 @@ 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, 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; 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 +381,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 +396,16 @@ 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)); assert!( - large > 1.25 * small, - "{setting:?}: k = 1,404 predicted {large}/s, k = 547 {small}/s; measured 1.6-2.5x" + 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, + "{setting:?}: k = {next_larger} predicted {large}/s, k = {smallest} {small}/s" ); } } 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..2fe97e3c4 --- /dev/null +++ b/crates/integration-tests/tests/memo_value_bounds.rs @@ -0,0 +1,1325 @@ +//! 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 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_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"), + ("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_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"), +]; + +/// 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. + "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 +/// 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_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); +} + +/// [`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, +} + +/// 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, + executions: usize, + rejected_datasets: usize, + /// 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, {} bounds never run, {} never reached, not lowered: {:?}, {} violations", + self.cases, + self.bounds.len(), + self.executions, + self.rejected_datasets, + self.never_executed().len(), + self.never_reached().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.executions += part.executions; + report.rejected_datasets += part.rejected_datasets; + report.bounds.extend(part.bounds); + report.not_lowered.extend(part.not_lowered); + report.violations.extend(part.violations); + } + 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(); + 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; + } + let query = QueryBounds { + root: root.clone(), + bounds, + }; + // 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(_) => 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; + } + for dataset in datasets(case, &query.root, &data, sizes) { + match execute_counts(&query, &runnable, &dataset) { + Ok(counts) => { + 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 { + case: case.name.clone(), + query: index, + dataset: dataset.name.clone(), + what: what.clone(), + bound: *bound, + observed, + }); + } + } + } + Err(reason) => { + report.rejected_datasets += 1; + for &bound_index in &runnable { + report + .bounds + .get_mut(&keys[bound_index]) + .unwrap() + .last_rejection = reason.clone(); + } + } + } + } + } +} + +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: four 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; + // Cross the smallest declared components first, so every combination + // of the few-valued keys appears within the rows a window reads. + components.sort_by_key(|values| match values { + ValueSet::Declared { combinations, .. } => *combinations, + ValueSet::Literals { .. } | ValueSet::Free { .. } => 0, + }); + 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..8bd46134c --- /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": "f13", + "query": "quantile_over_time(0.9, a[5m]) unless quantile_over_time(0.5, b[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" + } + ] +} 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..aa63d1de8 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,78 @@ 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)); +} + +/// 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 8b3f226fa..f36fa1373 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,337 @@ 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}" + ); + whole_result_quantile( + &query, + json!({"flows": { + "series_count": 6, + "distinct_values_per_label": src_values.map_or(json!({}), |src| json!({"src": src})), + }}), + ) +} + +/// `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 +/// (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`, 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_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:?}"); + 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:?}"); + } +} + +/// $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)); +} + +/// `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)); +} + +/// 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)); +} diff --git a/crates/integration-tests/tests/planning_common/mod.rs b/crates/integration-tests/tests/planning_common/mod.rs index e962f2d52..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,25 +97,40 @@ 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. 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` 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"}, {"name": "src", "type": "utf8"}, {"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, - json!({"flows": {"series_count": 1000}}), - Some(&[flows.to_string()]), + sources, + Some(&[flows.to_string(), hosts.to_string()]), ) } diff --git a/crates/integration-tests/tests/rollup_controlled.rs b/crates/integration-tests/tests/rollup_controlled.rs index 4458f05d1..a056a3f70 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_60k_rows_per_state() { 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"); } 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/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 6780f97d5..67546063e 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, ReadoutValueCounts, +}; diff --git a/crates/types/src/accuracy/target/mod.rs b/crates/types/src/accuracy/target/mod.rs index 5ed6aa715..42efa60f2 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,86 @@ 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: ReadoutValueCounts, + ) -> 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 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)] diff --git a/crates/types/src/accuracy/target/tests.rs b/crates/types/src/accuracy/target/tests.rs index 290b10a52..56b6af163 100644 --- a/crates/types/src/accuracy/target/tests.rs +++ b/crates/types/src/accuracy/target/tests.rs @@ -354,3 +354,71 @@ 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| ReadoutValueCounts { + 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) + ); +} + +/// `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/crates/types/src/stages/logical/group/column_properties.rs b/crates/types/src/stages/logical/group/column_properties.rs index 30115e8d2..5cfd9c4bc 100644 --- a/crates/types/src/stages/logical/group/column_properties.rs +++ b/crates/types/src/stages/logical/group/column_properties.rs @@ -8,6 +8,25 @@ //! 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`). 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 +)] + use std::collections::BTreeMap; use super::{MemoGroupAsInput, MemoGroupId}; @@ -74,9 +93,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 +196,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,14 +296,36 @@ 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)); - columns.extend(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() { + column.lineage = None; + } + } } + columns.extend(right); columns } SetOp { left, right, .. } => agreed(&[columns_of(left), columns_of(right)]), @@ -298,13 +343,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 +374,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 +570,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 +616,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 +703,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 3637934d5..2a108ebca 100644 --- a/crates/types/src/stages/logical/memo/mod.rs +++ b/crates/types/src/stages/logical/memo/mod.rs @@ -12,17 +12,18 @@ 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::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, TimeRangeKind}; use crate::stages::{ CandidateError, CandidateForMemoGroup, CandidateForMemoGroupId, ColumnLineage, 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, @@ -92,36 +93,186 @@ 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 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`). + /// 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: + /// - 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; + /// - 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 + /// `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 /// 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. 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 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, self.declared_groups_of_state(group)) { - (Some(series), Some(groups)) => Some(series.min(groups)), - (series, groups) => series.or(groups), + 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, + }; + 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 + /// 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, 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 { + 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; + }; + 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::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 { + 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, } } @@ -145,51 +296,95 @@ 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, 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. 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, - }) if !matches!(query, SketchStatistic::TopK { .. }) => { - self.declared_groups_of_state(*summary_input) + }) => { + let (keys, input) = self.keys_of_values(*summary_input)?; + return Some(if matches!(query, SketchStatistic::TopK { .. }) { + (keys.several_rows_per_group(), input) + } else { + (keys, input) + }); } 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()); - } - let Some(source) = source else { - return Some(1); - }; - let (facts, _) = series_facts(&self.source_facts, source_name(source))?; - facts.declared_distinct_combinations(&labels) + ) => (reduction, *child, true), + Operator::NonASAP(NonASAPOp::Aggregate { + reduction, + measures, + child, + .. + }) => ( + reduction, + *child, + !measures + .iter() + .any(|m| matches!(m, AggIntent::TopK { .. } | AggIntent::CountValues { .. })), + ), + _ => 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 Some((KeysOfValues::PerSeries, child)), + }; + let input = &self.groups[child.0 as usize].group_properties; + let mut columns = Vec::new(); + for key in keys { + let Some(ColumnLineage::SourceColumn(column)) = input.column_lineage.get(*key) else { + return Some((KeysOfValues::ComputedOrTime, child)); + }; + 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 Some((KeysOfValues::ComputedOrTime, child)); } - _ => None, + columns.push(column.clone()); } + let keys = KeysOfValues::SourceColumns(columns); + Some(if one_row_per_group { + (keys, child) + } else { + (keys.several_rows_per_group(), child) + }) } /// The queries, by index in the workload. @@ -551,5 +746,32 @@ fn add_parent(group: &mut MemoGroup, parent: MemoGroupId) { } } +/// What the values of a readout, state or aggregate are keyed by +/// ([`Memo::keys_of_values`]). +enum KeysOfValues { + /// 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 declared count bounds. + ComputedOrTime, +} + +impl KeysOfValues { + /// 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::PerSeries | Self::SourceColumns(_) => Self::PerSeries, + Self::ComputedOrTime => Self::ComputedOrTime, + } + } +} + #[cfg(test)] mod tests; diff --git a/crates/types/src/stages/logical/memo/tests.rs b/crates/types/src/stages/logical/memo/tests.rs index 0a40e109f..1ba98db1d 100644 --- a/crates/types/src/stages/logical/memo/tests.rs +++ b/crates/types/src/stages/logical/memo/tests.rs @@ -499,3 +499,152 @@ 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:?}" + ); + } +} + +/// 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:?}" + ); + } +} 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 f23fb5384..fe13441fd 100644 --- a/crates/types/src/workload/data.rs +++ b/crates/types/src/workload/data.rs @@ -268,6 +268,23 @@ 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)); + super::bounds::smaller_known(by_rate, self.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/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)) ); } } diff --git a/docs/design_docs/concepts/accuracy-models.md b/docs/design_docs/concepts/accuracy-models.md index c698b99cf..6fcb78b41 100644 --- a/docs/design_docs/concepts/accuracy-models.md +++ b/docs/design_docs/concepts/accuracy-models.md @@ -78,32 +78,78 @@ 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 -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` -enlarges nothing and 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 +`δ / 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), 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. +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`, +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. 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. +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: @@ -296,18 +342,19 @@ 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: 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 - 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. + 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`, 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 @@ -316,7 +363,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