Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8b8cb3f
refactor(stage3): select over execution contexts: x_{κ,v}, y_{κ,u,c,i…
zzylol Nov 9, 2026
ca62eb7
feat(stage3): a query reads the groups Pass 2 lists as exact-only for…
zzylol Nov 12, 2026
f22af5e
fix(stage3): one run rate shared with plan_cost, request contexts per…
zzylol Nov 13, 2026
b64b5fe
docs(stage3): list exact_only.rs and milp/retention.rs, and (X), in t…
zzylol Nov 13, 2026
ce06041
fix(stage3): the greedy bound counts the least retention an execution…
zzylol Nov 13, 2026
a692ec2
perf(stage3): leave out a query-time execution with lifetime None tha…
zzylol Nov 13, 2026
7972447
perf(stage3): leave out (B) and (D) rows the one-reader equality (F) …
zzylol Nov 13, 2026
8c8a674
fix(stage3): an anchored input runs once per request of the queries t…
zzylol Nov 13, 2026
a154b31
fix(stage3): an ingestion-time view's pi_v is one more output's cost …
zzylol Nov 13, 2026
51e8b28
fix(stage3,cost): an instant query's @ end() input runs in its evalua…
zzylol Nov 14, 2026
60b0388
fix(cost): an @ start() input runs once per request of each distinct …
zzylol Nov 14, 2026
902d1c1
refactor(stage3,cost): one walk of retained reads, one rule for a con…
zzylol Nov 14, 2026
b83abbf
perf(stage3): h_v is at least the least need over an execution's uses…
zzylol Nov 14, 2026
1c75883
fix(stage3,cost): rate a context by the queries whose walk reaches it…
zzylol Nov 14, 2026
1429490
test(types): an ingestion-time view without a window delivers on ever…
zzylol Nov 14, 2026
70701e9
refactor(types,cost,stage3): place shared rules where their facts liv…
zzylol Nov 14, 2026
5f3141a
test(cost,integration): an ingestion-time reader of raw rows kept at …
zzylol Nov 14, 2026
bb62f81
perf(stage3): a copy of an equivalent evaluation context shares the o…
zzylol Nov 15, 2026
a1abc86
refactor(types,cost,stage3): one refresh rule for an ingestion-time s…
zzylol Nov 15, 2026
6ca90a0
fix(stage3,types,cost): a query's own context for context copies; one…
zzylol Nov 15, 2026
3417d63
fix(accuracy,stage3): a count's input is not exact-only; one retained…
zzylol Nov 15, 2026
44a223d
fix(stage3,accuracy,cost,types): round-5 review fixes: context copies…
zzylol Nov 15, 2026
0b5ec46
fix(stage3,types,cost,accuracy): round-6 review fixes: context copies…
zzylol Nov 15, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions crates/accuracy/src/error_contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,5 +336,18 @@ pub struct ErrorContract {
pub linear_bound_holds_up_to_per_input: Vec<Option<f64>>,
}

impl ErrorContract {
/// Whether any of input `input`'s error reaches the output
/// ($\beta_{c,i} > 0$). Where it does not, the input may be off in the
/// contract's metric whatever the output allows.
///
/// Example: `count` outputs how many values it reads, whatever their
/// error ($\beta = 0$): false; `sum` passes its input's relative error
/// on ($\beta = 1$): true.
pub(crate) fn input_error_reaches_output(&self, input: usize) -> bool {
self.error_factor_per_input[input] > 0.0
}
}

#[cfg(test)]
mod tests;
4 changes: 2 additions & 2 deletions crates/accuracy/src/logical_edge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl ErrorMetricsOfMemo {
let Some(producer_metric) = self.readout_error_metric(producer) else {
return Ok(());
};
return match self.error_metrics_allowed_on_inputs(consumer) {
return match self.error_metrics_allowed_on_input_edge(consumer, input) {
Some(allowed) => metric_rule(producer_metric, &allowed),
None => Ok(()),
};
Expand All @@ -100,7 +100,7 @@ impl ErrorMetricsOfMemo {
let failure_probability = required.map_or(1.0, |required| required.failure_probability);
let measured =
evidence.measured_guarantee(summary_type, parameters, statistic, failure_probability);
let allowed = self.error_metrics_allowed_on_inputs(consumer);
let allowed = self.error_metrics_allowed_on_input_edge(consumer, input);
// The theoretical guarantee before its failure-probability check,
// computed once for both checks below.
let theoretical = (measured.is_none() && (allowed.is_some() || required.is_some()))
Expand Down
100 changes: 63 additions & 37 deletions crates/accuracy/src/metric_rule/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ impl ErrorMetricsOfMemo {
/// input those its output allows that its error contract passes to it;
/// a group the union over its consumers. These are the metrics of the
/// readout caps' walk (`readout_caps`), which also reaches a group
/// through inputs the output does not depend on.
/// through inputs the output does not depend on. A metric only such an
/// input would allow is not added here: a readout's edge into such a
/// consumer is admitted by [`Self::error_metrics_allowed_on_input`],
/// and widening a whole group for it would admit every alternative
/// readout of the group (`avg` of a relative quantile: 62 → 455
/// MILP variables, the same plan).
pub fn of(memo: &Memo, order: &[MemoGroupId]) -> Self {
let error_contract: Vec<Vec<ErrorContractPerMetric>> = memo
.groups()
Expand Down Expand Up @@ -111,46 +116,63 @@ impl ErrorMetricsOfMemo {
self.readout_error_metric[candidate.group.0 as usize][candidate.index_in_group]
}

/// The metrics among `metrics` that `consumer`'s error contract passes
/// to its inputs: those an input of it may be off in when its output
/// may be off in `metrics`.
fn error_metrics_passed_to_inputs(
/// The metrics `consumer`'s input `input` may be off in when its
/// output may be off in `output_allowed`, the metric rule: those of
/// `output_allowed` its error contract passes to its inputs, and every
/// metric in which the input's error does not reach the output
/// ([`ErrorContract::input_error_reaches_output`]). In
/// `AccuracyMetric::ALL` order.
///
/// Example: a readout's contract passes every metric, so a readout
/// allows its output's; `count` (error factor 0) allows every metric,
/// even under an exact target; a sum of values of unknown sign under a
/// relative target allows none.
pub(crate) fn error_metrics_allowed_on_input(
&self,
consumer: CandidateForMemoGroupId,
metrics: &[AccuracyMetric],
input: usize,
output_allowed: &[AccuracyMetric],
) -> Vec<AccuracyMetric> {
metrics
.iter()
.copied()
.filter(|&metric| self.error_contract(consumer, metric).is_some())
AccuracyMetric::ALL
.into_iter()
.filter(|&metric| {
self.error_contract(consumer, metric)
.is_some_and(|contract| {
output_allowed.contains(&metric)
|| !contract.input_error_reaches_output(input)
})
})
.collect()
}

/// The metrics `consumer` allows an input's error in, the metric rule
/// on its input edges: those its group's output allows that its error
/// contract passes to its inputs (a readout's contract passes every
/// metric, so a readout allows its output's). `None` where no query
/// with a target reaches the consumer.
pub(crate) fn error_metrics_allowed_on_inputs(
/// [`Self::error_metrics_allowed_on_input`] at what `consumer`'s group's
/// output allows, the metric rule on its input edges; `None` where no
/// query with a target reaches the consumer.
pub(crate) fn error_metrics_allowed_on_input_edge(
&self,
consumer: CandidateForMemoGroupId,
input: usize,
) -> Option<Vec<AccuracyMetric>> {
let allowed = self.error_metrics_allowed_at_output[consumer.group.0 as usize].as_ref()?;
Some(self.error_metrics_passed_to_inputs(consumer, allowed))
Some(self.error_metrics_allowed_on_input(consumer, input, allowed))
}

/// Per query: the groups below its root it may read only exactly, the
/// metric rule per query where the edges apply it per group. Walking
/// down the live candidates (not in `dead`) from each root with a
/// target, which allows its target's metrics
/// ([`query_roots_with_target`]; an exact target none), a candidate passes to its inputs the
/// metrics its error contract passes; a group keeps, per query, the
/// ([`query_roots_with_target`]; an exact target none), a candidate
/// passes to each input [`Self::error_metrics_allowed_on_input`] (a
/// `count`'s input is never read exactly for its sake); a group keeps,
/// per query, the
/// metrics every path to it passes. A group is exact-only for a query
/// when no metric reaches it (one path through a consumer without a
/// contract, a comparison or a sum, suffices) or when it holds a sketch
/// readout whose error metric is not among them (`quantile_over_time`
/// at a relative target reads no KLL readout, whose error is a rank
/// one); and so is every group below one.
/// one); and so is every group below one, except through an input
/// that may be off in some metric under an output allowing none (a
/// `count`'s).
/// `exact_only_readout_groups_of_query` adds, per query, the readout
/// groups the readout requirements list (a readout whose level another
/// query gave, for a query whose failure probability has no per-value
Expand Down Expand Up @@ -212,32 +234,36 @@ impl ErrorMetricsOfMemo {
}
}
}
let passed = self.error_metrics_passed_to_inputs(consumer, &AccuracyMetric::ALL);
for &child in memo.candidate(consumer).expression.operator.children() {
for (input, &child) in memo
.candidate(consumer)
.expression
.operator
.children()
.into_iter()
.enumerate()
{
for (&query, metrics) in &allowed_here {
let passed_here = metrics
.iter()
.copied()
.filter(|metric| passed.contains(metric))
.collect();
keep_common(&mut allowed, child, query, passed_here);
let passed = self.error_metrics_allowed_on_input(consumer, input, metrics);
keep_common(&mut allowed, child, query, passed);
}
}
}
}
// Every group below an exact-only group is read exactly too.
// Every group below an exact-only group is read exactly too, except
// through an input that may be off in some metric under an output
// allowing none (a `count`'s).
for &group in order.iter().rev() {
for groups in &mut exact_only {
if groups.contains(&group) {
for consumer in live_candidates(group) {
groups.extend(
memo.candidate(consumer)
.expression
.operator
.children()
.into_iter()
.copied(),
);
let children = memo.candidate(consumer).expression.operator.children();
groups.extend(children.into_iter().enumerate().filter_map(
|(input, &child)| {
self.error_metrics_allowed_on_input(consumer, input, &[])
.is_empty()
.then_some(child)
},
));
}
}
}
Expand Down
85 changes: 79 additions & 6 deletions crates/accuracy/src/metric_rule/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ fn allowed_at_output(memo: &Memo, group: MemoGroupId) -> Option<Vec<AccuracyMetr
error_metrics(memo).error_metrics_allowed_at_output[group.0 as usize].clone()
}

/// The metrics `consumer` allows on its inputs.
fn allowed_on_inputs(
/// The metrics `consumer` allows on its input `input`.
fn allowed_on_input(
memo: &Memo,
consumer: CandidateForMemoGroupId,
input: usize,
) -> Option<Vec<AccuracyMetric>> {
error_metrics(memo).error_metrics_allowed_on_inputs(consumer)
error_metrics(memo).error_metrics_allowed_on_input_edge(consumer, input)
}

/// $\alpha^{\log}$ of the edge from `producer` into input `input` of
Expand Down Expand Up @@ -93,7 +94,7 @@ fn a_ratio_reads_no_rank_error() {
query(&mut memo, ratio, Some(target));
let relative = Some(vec![AccuracyMetric::Relative]);
assert_eq!(allowed_at_output(&memo, p99), relative);
assert_eq!(allowed_on_inputs(&memo, own_candidate(ratio)), relative);
assert_eq!(allowed_on_input(&memo, own_candidate(ratio), 0), relative);
assert!(matches!(
edge(&memo, own_candidate(p99), own_candidate(ratio), 0),
Err(RootPruneReason::MetricRule {
Expand Down Expand Up @@ -143,7 +144,7 @@ fn the_metric_rule_cuts_the_readout_edges_and_keeps_the_exact_division() {
let target = AccuracyTarget::max_error(0.05).accuracy_metric(AccuracyMetric::ABSOLUTE);
query(&mut memo, avg, Some(target));
let division = own_candidate(ratio);
assert_eq!(allowed_on_inputs(&memo, division), Some(vec![]));
assert_eq!(allowed_on_input(&memo, division, 0), Some(vec![]));
let result = edge(&memo, division, own_candidate(sum), 0);
assert!(result.is_ok(), "{result:?}");
for input in 0..2 {
Expand Down Expand Up @@ -195,7 +196,7 @@ fn a_key_only_filter_keeps_the_ddsketch_readout_admissible() {
let target = AccuracyTarget::max_error(0.05).accuracy_metric(AccuracyMetric::Relative);
query(&mut memo, filtered, Some(target));
assert_eq!(
allowed_on_inputs(&memo, own_candidate(filtered)),
allowed_on_input(&memo, own_candidate(filtered), 0),
Some(vec![AccuracyMetric::Relative])
);
let result = readout_edge(&memo, &p99.by_algorithm[0], own_candidate(filtered), 0);
Expand Down Expand Up @@ -332,3 +333,75 @@ fn a_scalar_roots_operands_are_roots() {
check(scalar_of, vec![AccuracyMetric::Relative], true);
check(doubled, vec![], false);
}

/// `count(quantile_over_time(0.99, m[5m]))` at an exact target: the
/// count of the quantiles does not depend on their values (its error
/// factor on its input is 0), so the quantile, its readouts and their
/// states are not exact-only for the query: the count's own group is,
/// and the raw window and scan below are (read only by the exact
/// quantile and the sketch builds, which pass no error). A
/// `count` beside a `sum` in `avg = sum / count` is such a path: it no
/// longer makes the quantile exact-only for a relative `avg`.
#[test]
fn a_count_reads_its_input_in_any_metric() {
let mut memo = Memo::new(&DataWorkload::default());
let p99 = quantile_readouts(&mut memo, 0.99);
let count = group(
&mut memo,
Operator::NonASAP(NonASAPOp::Aggregate {
child: p99.result,
reduction: Reduction::by(vec![]),
measures: vec![AggIntent::Count],
output_names: vec!["count".into()],
filters: vec![None],
having: None,
}),
);
query(&mut memo, count, Some(AccuracyTarget::exact()));
let order = memo.topological_order().expect("acyclic");
let exact_only = error_metrics(&memo).exact_only_groups_of_query(
&memo,
&order,
&BTreeSet::new(),
&BTreeMap::new(),
);
let [exact_only] = exact_only.as_slice() else {
panic!("one query: {exact_only:?}");
};
assert!(exact_only.contains(&count));
let mut quantile_groups = vec![p99.result];
for readout in &p99.by_algorithm {
quantile_groups.extend([readout.state, readout.readout.group]);
}
for group in quantile_groups {
assert!(!exact_only.contains(&group), "{group:?} in {exact_only:?}");
}
assert_eq!(exact_only.len(), 3, "{exact_only:?}");
}

/// A `count`'s output does not depend on its input's error (error factor
/// 0 in every metric), so the metric rule lets its input be off in any
/// metric, on its edges as in `exact_only_groups_of_query`.
/// `count(quantile_over_time(0.99, m[5m]))` with a relative target, the
/// count reading the KLL readout directly: the readout's rank error is not
/// one the output allows, but its edge into the `count` holds.
#[test]
fn a_counts_input_edge_from_a_readout_holds_in_a_metric_its_output_does_not_allow() {
let mut memo = Memo::new(&DataWorkload::default());
let (_, _, p99) = quantile_over_time(&mut memo, SketchAlgorithm::Kll, 0.99, 300);
let count = group(
&mut memo,
Operator::NonASAP(NonASAPOp::Aggregate {
child: p99,
reduction: Reduction::by(vec![]),
measures: vec![AggIntent::Count],
output_names: vec!["count".into()],
filters: vec![None],
having: None,
}),
);
let relative = AccuracyTarget::max_error(0.05).accuracy_metric(AccuracyMetric::Relative);
query(&mut memo, count, Some(relative));
let result = edge(&memo, own_candidate(p99), own_candidate(count), 0);
assert!(result.is_ok(), "{result:?}");
}
4 changes: 2 additions & 2 deletions crates/accuracy/src/readout_caps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,10 @@ pub(crate) fn caps(
for (input, &child) in children.iter().enumerate() {
let child_caps = caps[child.0 as usize].get_or_insert_with(HashMap::new);
child_caps.entry(metric).or_default();
let factor = contract.error_factor_per_input[input];
// An input the output does not depend on (a count's,
// β = 0) gets no cap from it.
if factor > 0.0 {
if contract.input_error_reaches_output(input) {
let factor = contract.error_factor_per_input[input];
let holds_up_to = contract.linear_bound_holds_up_to_per_input[input]
.unwrap_or(f64::INFINITY);
for (&query, &cap) in caps_per_query {
Expand Down
6 changes: 3 additions & 3 deletions crates/cost/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//! Stage 3 (`asap-stage3-plan-selection`) costs each candidate as one node
//! with the same function and the rules `plan_cost` applies:
//! [`plan::execution_demand::executions_per_second`],
//! [`maintained_window_ms`], [`read_back_ms`],
//! `RefreshWindow::computed_from` (an ingestion-time node's window), [`read_back_ms`],
//! [`raw_data_disk_cost_per_second`] and
//! [`request_overhead_cost_per_second`].
mod measured;
Expand All @@ -40,8 +40,8 @@ pub use model::{
};
pub use node::operator_statistics::EdgeStatistics;
pub use node::usage::{
maintained_window_ms, node_resource_usage_and_cost, summary_type_name, NodeCost,
NodeResourceUsageAndCost, ProducerOutput,
node_resource_usage_and_cost, output_interval_ms, reads_one_batch_of, summary_type_name,
NodeCost, NodeResourceUsageAndCost, ProducerOutput,
};
pub use plan::cost::{
plan_cost, raw_data_disk_cost_per_second, request_overhead_cost_per_second, PlanCost,
Expand Down
Loading
Loading