Repository navigation
feat(planner): certify distinct counts at scale: HLL sized from (ε, δ), provable KMV (#694) - #715
Conversation
…), provable KMV (#694) Classic HLL: - Pass 1 sizes precision from (ε, δ) without data: the smallest p with m ≥ 1/(2ε²δ) whose linear-counting tail bound certifies δ at the target ε for every distinct bound up to ⌈1/ε⌉. - SketchParams::Hll records the relative error it was sized for. The guarantee states that target ε as its error bound (not 1.04/√m), and its failure probability is evaluated at it: classic_hll_failure_probability[p=P,eps=E](max_distinct[source]). - Plan selection's enlargement (#669b) applies the same rule to the source's bound and δ/(n·G), at the recorded ε. - The contract's distinct-bound cap rises from 4096 to 2^17 (m/2 at precision 18). KMV: - A provable bound for asap_sketchlib's estimator: exact below k distinct items, and from k on two Chernoff tails of the k-th smallest hash, exp(-ε²(k-1)/((1+ε)(2+ε))) + exp(-ε²(k-1)/(2(1-ε))), for any number of distinct items, so no distinct bound is needed. - SketchParams::Kmv records its relative error; k is the smallest k meeting δ (k = 26,656 at ε = 0.02, δ = 0.01, down from 250,002). - Plan selection enlarges k for a smaller per-value δ. Fixes #700: the guarantee no longer reports a fixed 0.01 whatever δ was sized for. - Theta keeps its Chebyshev calibration (theta_k_99). Review nits from #693: negative tests for the HLL scope condition (a join, computed rows, Hydra grouping, a bound keyed to another source); max_distinct_per_evaluation must hold for the longest window any query over the source uses, per output row.
9819bbd to
555d0a0
Compare
|
Usage: let params = size_params(SketchAlgorithm::Hll, &intent, 0.05, 0.01); // Hll { precision, relative_error: Some(0.05) } This is confusing, basically, you should show the usage with a e2e workflow. what does even these three lines mean and used for? |
|
Rewrote the Usage as an end-to-end run, checked by running it at the merge commit. It starts from a SQL panel, |
Closes #694. Closes #700.
Why
#693 gave classic HLL an (ε, δ) guarantee from a deployment bound on distinct items, but Pass 1 still sized precision from ε alone. Precision 12 at ε = 0.02 certified δ = 0.01 for at most 9 distinct items, and the contract stopped at 4096. KMV reported a fixed 0.01 failure probability whatever δ it was sized for (#700). Plan selection then rejected it for a stricter δ, and its k was ten times larger than a provable bound needs.
What
SketchParams::Hllrecordsrelative_error, the ε it was sized for (None: not sized; the error bound is then 1.04/√m). Equality ignores it, as with KLL'sfailure_probability.classic_hll_failure_probability[p=P,eps=E](max_distinct[source]).max_distinct[source]for the upper coverage limit.src/sketches/kmv.rs). The estimator is exact while it keeps fewer than k hashes. From then on it returns (k−1)/U, where U is the k-th smallest hash mapped into (0, 1]. A merge keeps the k smallest distinct hashes of the union. Let X(u) ~ Binomial(N, u) count the hashes below u:P ≤ exp(−ε²(k−1)/((1+ε)(2+ε))).P ≤ exp(−ε²(k−1)/(2(1−ε))), and 0 for ε ≥ 1.SketchParams::Kmvrecordsrelative_error. k is the smallest k meeting δ, about 1 + (1+ε)(2+ε)·ln(2/δ)/ε², which is below the issue's 3·ln(2/δ)/ε² + 1.theta_k_99).max_distinct_per_evaluation's doc now says the bound must hold for the longest evaluation window any query over the source uses, and that it and δ are per output row.How
hll::hll_precision(ε, δ)andhll::precision_for(p, ε, bound, δ)shareclassic_failure_probability(p, ε, bound).cardinality::kmv_failure_probability(k, ε)andkmv_k(ε, δ).kmv_kbinary-searches between the closed forms at ln(1/δ) and ln(2/δ).with_failure_probabilitygains the KMV case.Key code interfaces added / modified
crates/types/src/post_asap/sketch.rscrates/asap-aware-mapping/src/accuracy/estimators/hll.rscrates/asap-aware-mapping/src/accuracy/estimators/cardinality.rs,mod.rsUsage, end to end: one SQL distinct count with ε = 0.05 and δ = 0.01, planned through
run_planning_stagesat this PR's merge commit. The numbers in the comments come from running this code there.Before this PR
After this PR
So Q1's distinct count over 10M source IPs is certifiable through KMV at any δ. Classic HLL stays limited by its linear-counting range.
stage_pipelinefor Example 2 (SQL, no distinct bound declared):kmv_exact_below_k_chernoff_v1) and HLL's precision (12 → 17, relative error 0.02).stage-viewer.md's numbers are unchanged, so it is not edited.Verification
kmv_sized_for_a_failure_probability_meets_it(KMV sizing ignores the requested δ: guarantee reports 0.01 after sizing for δ = 0.001 #700: δ 0.01 > 0.001) andprecision_is_sized_from_epsilon_and_delta(precision 9, not 15).kmv_bound_dominates_the_exact_order_statistic_tails: the bound is at least the exact binomial tails for k ∈ {16, 64, 256}, ε ∈ {0.1, 0.3, 0.6}, and N from k to 20k.estimates_k_minus_one_over_the_kth_smallest_hash: the executor's KMV estimate is the estimator the bound covers.largest_certified_bounds_by_precision: reproduces Certify distinct counts at realistic scale: size HLL from (ε, δ) and give KMV a provable guarantee #694's table, including 9,636 at precision 18 past the old cap.kmv_k_follows_the_failure_probability.stage3_plan_selection_accepts_kmv_for_any_failure_probability_without_a_bound, plus updated HLL bound tests.stage3_plan_selection_enlarges_tumbling_pane_hll: after the rebase onto feat(planner): tumbling-pane window composition as a Pass 2 rule (Example 3) #713, plan selection enlarges a tumbling-panePaneChain's HLL. A bound of 3,000 is valid only at precision 18, one above Pass 1's 17.3466f32f.cargo nextest runon asap-aware-mapping, asap-physical-operators, asap-integration-tests, asap-types and asap-devtools: 1339 passed, 31 skipped.cargo fmt --all -- --checkandcargo clippy --workspace --all-targets --all-features --locked -- -D warningsare clean.tools/verify_metricsql_external_consumer.shbuilds.Limitations and follow-ups
hll_precisionreturns 18 without a certificate. ε-only SQL requests (default δ = 0.01) at ε = 0.01 therefore get precision 18 instead of 14, which is 16× the registers.relative_erroryet (Update the docs after the G1–G11 consolidation groups land #677).docs/design_docs/concepts/accuracy-models.md(KMV "Chebyshev at 99%"; HLL "maxima from 1 to 4096") anddocs/develop_docs/end-to-end-accuracy-guarantees.md.