Repository navigation
Conversation
zzylol
force-pushed
the
refactor/1207-contexts
branch
from
November 9, 2026 09:35
8a466a4 to
3b831f9
Compare
…tioned DDSketch guarantee and sum/avg relative contracts (#1207 PR 8)
zzylol
force-pushed
the
refactor/1207-accuracy
branch
from
November 9, 2026 10:09
55a1384 to
8a376d3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #1237 (base
refactor/1207-contexts). Part of #1207 (PR 8 of the restack plan). One commit,8a376d382. Rebuilt on the restacked PR 7 (3b831f97f); the previous head55a138469is kept asbackup/1207-accuracy-55a138469. Its design (read error on edges, (K) switched off by the model or evidence, the post-solve check,AccuracyCheckInputs::query_roots, string-encoded distinct bounds) was replaced by the 2026-11-08 accuracy redesign and is not carried over; PR 0–4 already landed the redesign's types (RequiredAccuracy,AccuracyEvidence::measured_guarantee,theoretical_guarantee,target_limit,MemoQuery { root, accuracy_target, values_per_evaluation }, readout requirement levels, ignore mode in the facade).Why
After PR 7, Stage 3's constraint (K) was
todo!(): no plan checked a query's composed accuracy at its root. A query whose readouts each met their own requirement could still miss its target (B = p99 / p50 at relative 0.03 read through two readouts at 0.02 each: 1.111 × 0.04 = 0.044), and a readout in the wrong metric could answer a query (PR 6/7 pins: DDSketch chosen for rank targets, HLL for a distinct count whose failure probability is unknown). The sign facts decided on 2026-11-08 (DDSketch's quantile guarantee; relative contracts of sum and avg) were not read yet.What
None) gives none.column_signNonNegative/NonPositive);sumandavg(and the exact sum accumulator) carry a relative error with β = 1 over values of one sign (E1(c), H2).readout_metricno longer depends on the failure probability (HLL's distinct count reads a relative error; it wasNone, so HLL readouts were treated as exact).AccuracyTarget::failure_probability_per_value, Pass 2 and Stage 3), one state-column sign (GroupProperties::state_column_sign, Stage 2's Count-Min rule and α^log), onetarget_limitfor (K)'s limits.How
ErrorModel::new(Stage 3, before any variable): per execution, whether a targeted query reaches its group, its own error (a readout'sRequiredAccuracy), its contract per metric (LogicalRequirements::error_propagation), whether it is a readout without a requirement, and its selectable input edges.ErrorModel::set_ceilings: the big-M per execution and metric,accuracy_variables:accuracy_constraints:solve_selectionadds (K) to (A)–(F), the one-consumer rule and (R); with (K) rows, HiGHS's primal and MIP feasibility tolerances are 1e-10 (errors of 1e-3 and failure probabilities of 1e-7 are below the default 1e-7).producer_satisfies_logical_requirementsreads the state group'sstate_column_signand appliessign_condition_holdsto the theoretical guarantee;error_propagationgives sum/avg (andSummaryAgg { ExactAggregate(Sum) }) a relative contract whenvalues_of_one_sign.Key code interfaces added / modified
Permalinks at
8a376d382.types/src/ir/properties/accuracy.rstypes/src/stages/memo.rstypes/src/stages/candidates.rsaccuracy/src/model.rsaccuracy/src/estimators/hll.rs,univmon.rsstage3-plan-selection/src/milp/model.rs(crate-private)Usage (integration test
accuracy_at_query_roots::each_query_meets_its_target_at_its_root, passing):Major algorithm changes
Constraint (K). Problem: PR 7 had no root check: on the example above with accuracy enforced, A's p95 could read a DDSketch readout (no rank requirement, treated as exact), and B could read two readouts at 0.02 each. Before: (K)$U$ ; a selected path's error is at most $E$ at its root (each $F$ equals its chosen producer's $E$ since (A) selects one producer per input; $M$ bounds every unselected producer). Tests:
todo!(); only per-edge α^log. After: How 1–5. Correctness: the rows are the doc's (K) per execution (§5) plusk_allocates_a_ratio_s_error_among_its_readouts(the doc's allocation table: only the 0.01 + 0.01 pair meets 0.03),k_accepts_error_only_in_the_target_s_metric,a_readout_without_a_requirement_answers_only_queries_without_a_target,k_divides_a_whole_result_failure_probability_over_the_values(1e-3 per value meets 0.01 over 10 values, not over 100 or an unknown count),a_scalar_root_with_a_target_reads_its_operands_exactly; end to end the threeaccuracy_at_query_rootstests. Complexity: variables ≤ executions × (5 + 2) + inputs × 6; rows ≤ selectable edges × 7 + Σ|G_q| × 7; the ceilings take rounds × edges × 5 (one round per acyclic path step; on a cycle, up to one per execution). Only executions a targeted query reaches count; ignore mode adds nothing. Limits: a shared producer reached by two paths counts twice, and failure probabilities add by a union bound (both conservative, doc §4).Sign conditions. Before: a DDSketch build met a quantile readout's requirement whatever the values' sign; sum/avg had no relative contract. After: How 6. Tests: Pass 2
ddsketch_quantiles_over_values_of_unknown_sign_have_no_guarantee,h2_avg_of_readouts_under_a_relative_target_needs_values_of_one_sign(was..._reads_exact_only: unknown sign → exact only; declared non-negative → the DDSketch readout feeds the avg), typessums_and_averages_carry_a_relative_error_over_values_of_one_sign.Deleted
accuracy_constraints'stodo!(),AccuracyVariables'#[allow(dead_code)]hll::{classic_relative_error, classic_failure_statistic, parse_classic_failure_statistic, classic_guarantee},univmon::{incomplete_top_layer_statistic, parse_incomplete_top_layer_statistic, distinct_guarantee}and their consts and round-trip testsAccuracyTarget::failure_probability_per_valueGroupProperties::state_column_signAccuracyModel,satisfies, (K)-disable, post-solve check,SelectedPlanFailedCheck, read error on edges, θ floors,AggIntentaccuracy,AccuracyCheckInputs::query_rootsPending approval (new items not in the doc)
quantile_over_time(0.95, latency[1h]), rank 0.01; its result group has the exact aggregate and Projects over the KLL readout (level rank 0.01) and over the DDSketch readout (no level).AccuracyTarget::failure_probability_per_value(&self, Option<u64>) -> Option<f64>andGroupProperties::state_column_sign(&self) -> Option<ValueSign>(dedup helpers, definitions above). Examples:max_error(0.01).failure_probability(0.01)(whole result) with n_q = 1,000 →Some(1e-5), with n_q unknown →Some(0.0), without δ →None; a DDSketch state group overlatencydeclared non-negative →Some(NonNegative), a scan →None.max_distinct), so no HLL readout meets a requirement and HLL is never selected under a target; UnivMon's distinct count has no guarantee. Binding the bound in Pass 2 needs it in the memo, e.g.Memo { declared_max_distinct: BTreeMap<String /* source */, u64>, .. }fromSourceFacts::max_distinct_per_evaluation, read by α^log for aCardinalityreadout over a state built directly on that source's rows. Example:COUNT(DISTINCT user_id)overhttp(max_distinct 1,000,000), relative 0.05 at δ 0.05: HLL p = 18 →classic_failure_probability(18, 0.05, 1e6)= 1 (out of the contract), so KMV is chosen either way; with max_distinct 9,636, p = 18 meets δ 0.01. Not done here (new interface).Spec questions (code kept independent where possible)
scalar(v)/ scalar subquery operands with the target (value-preserving).theoretical_guaranteeonly; a deployment's measurement is taken as is.production_three_sql_groupings_share_kll,production_sql_distinct_region_service_rollup,rollup_controlled::production_distinct_rollup_finds_shared_kmv) now select the right family (KLL, KMV; were DDSketch, HLL) but no rollup; (b) PromQL per-series readouts throughe2e_planwithout series identity:renaming_projectrefuses an open schema, so Pass 1 adds the readout groups without connecting them to the result group (the e2e test therefore lowers withpromql_rows::with_series_identity, as the other integration tests do).Before this PR
No root check; readouts without a requirement and readouts in another metric could answer a targeted query; DDSketch quantile guarantees held for values of any sign; sum and avg carried no relative error.
After this PR
Every selected plan meets each target at its root per (K): composed error per metric, per-value failure probability, no unbounded readout. Ignore mode plans on cost alone (no rows).
Verification
cargo testper crate (--all-features --no-fail-fast),cargo fmt --check,cargo clippy --workspace --all-features --all-targets -D warningsclean,crate_graphpasses. Wall-clock tests stay ignored.By test name vs PR 7: 0 newly failing; 13 new tests pass (Stage 3:
k_allocates_a_ratio_s_error_among_its_readouts,k_accepts_error_only_in_the_target_s_metric,a_readout_without_a_requirement_answers_only_queries_without_a_target,k_divides_a_whole_result_failure_probability_over_the_values,a_scalar_root_with_a_target_reads_its_operands_exactly; integrationaccuracy_at_query_roots::{each_query_meets_its_target_at_its_root, ignoring_accuracy_plans_without_k, a_whole_result_failure_probability_is_divided_over_the_values}; Pass 2ddsketch_quantiles_over_values_of_unknown_sign_have_no_guarantee,h2_avg_of_readouts_under_a_relative_target_needs_values_of_one_sign(replaces..._reads_exact_only); typessums_and_averages_carry_a_relative_error_over_values_of_one_sign; accuracyreadout_metric_is_the_guarantee_s_metric,incomplete_top_layer_probability_needs_enough_layers(replaces the twodistinct_guarantee_*tests)); deleted with the string-encoded guarantees:classic_failure_statistics_round_trip,classic_guarantee_binds_the_distinct_bound(its out-of-contract check moved tolargest_certified_bounds_by_precision),incomplete_top_layer_statistics_round_trip. The 74 failures are PR 7's; of the accuracy pins,production_three_sql_groupings_share_kllnow selects KLL (was DDSketch) andproduction_sql_distinct_region_service_rollupKMV (was HLL), both still failing on the missing rollup (PR 4 Q1).Self-review: no shims or aliases; the composition rule lives once per role (ceilings in$F$ /$H$ are created where used instead of stored. New beyond the doc: $U$ (Pending 1) and two helpers (Pending 2).
set_ceilings, rows inaccuracy_constraints, both overErrorModel::contractandedges_on); the per-value δ, state sign andtarget_limithave one definition each; the string-encoded distinct guarantees are deleted, the formulas they wrapped kept for Pending 3. Over-abstraction check:ErrorModelis the precomputation (K) needs once (contracts, ceilings);