Skip to content

Accuracy: per-group distinct bound certifies HLL sparse-exact; SIL REQUIRE … SCOPE per_value - #1567

Draft
zzylol wants to merge 3 commits into
feat/hll-measured-accuracyfrom
feat/hll-group-bound-scope
Draft

zzylol wants to merge 3 commits into
feat/hll-measured-accuracyfrom
feat/hll-group-bound-scope

Conversation

@zzylol

@zzylol zzylol commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Stacked on #1562 (feat/hll-measured-accuracy). Draft: do not merge.

What to review first

  1. crates/accuracy/src/guarantee/hll/mod.rs sparse_exact_guarantee: HLL's sparse state is the exact set of distinct 64-bit item hashes while it holds at most 2^p / 8 of them, so over groups declared to hold at most n ≤ 2^p / 8 distinct items per evaluation the count is exact unless two hashes collide. Certified as relative error 0, failing with probability at most n(n − 1) / 2^65 (1.4e-12 at n = 7,200).
  2. crates/accuracy/src/logical_edge/mod.rs exact_or_measured_guarantee: that guarantee is checked before the measured table and the theoretical guarantee, both on the readout edge and in build_meets_level (Pass 2 sizing). The bound is computed only for an HLL distinct count.
  3. crates/types/src/stages/logical/memo/mod.rs Memo::max_distinct_per_group_per_evaluation: the bound applies only when the summary's rows come from one scan of one source (no join, union or binary operator below it) and its keys include a declared label. The smallest declared bound among the keys wins. Every other case gets None: a coarser group (e.g. a rollup to user over a per-host bound), a self-join, a per-series value, a computed key, or an ungrouped count.
  4. crates/frontend-sil/src/parse.rs: REQUIRE ERROR m <= ε [DELTA δ] [SCOPE per_value | whole_result].

Problem

With the measured HLL table (#1562), the planner never keeps HLL for a grouped distinct count over the whole result. Example: E6's SELECT host, count(DISTINCT value) FROM flows WHERE ts > now() - INTERVAL '1 hour' GROUP BY host at rel 0.05, δ 0.05 over 10,000 hosts splits δ to 5e-6 per host. The table's smallest δ is 0.005, so no precision is certified and the plan is exact over raw data (7,154.9/s).

Change

  • (a) New data fact max_distinct_per_group_per_evaluation, keyed by the grouping label, e.g. {"host": 7200}. When the bound is at most 2^p / 8, HLL at precision p is certified exact (as above), so the δ split no longer matters. The fact holds only over one scan of the source. Without it, behaviour is unchanged.
  • (b) SIL SCOPE per_value sets the existing AccuracyTarget::accuracy_scope to PerValue: each grouped value gets δ, with no split across values. SCOPE whole_result spells out the default. EXACT SCOPE … is rejected with its own message.

Interfaces

 // crates/types/src/workload/data.rs
 pub struct SourceFacts {
     pub max_distinct_per_evaluation: Option<u64>,
+    /// Distinct items one group's distinct count holds per evaluation, keyed by
+    /// the grouping label, e.g. {"host": 7200}. Omitted from JSON when empty.
+    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
+    pub max_distinct_per_group_per_evaluation: BTreeMap<String, u64>,
     ...
 }
 // crates/types/src/workload/planning.rs
-    #[error("a source's max_distinct_per_evaluation must be greater than zero")]
+    #[error("a source's max_distinct_per_evaluation and max_distinct_per_group_per_evaluation bounds must be greater than zero")]
     ZeroMaxDistinct,
 // crates/types/src/stages/logical/memo/mod.rs
+impl Memo {
+    pub fn max_distinct_per_group_per_evaluation(&self, group: MemoGroupId) -> Option<u64>;
+}
 // crates/accuracy/src/guarantee/mod.rs (crate-internal)
+pub(crate) fn sparse_exact_guarantee(
+    parameters: &SketchParams,
+    statistic: &SketchStatistic,
+    max_distinct_per_group: impl FnOnce() -> Option<u64>,
+) -> Option<AccuracyGuarantee>;
 // SIL grammar (crates/frontend-sil/src/parse.rs)
-accuracy::= EXACT | ERROR (abs | rel | rank) '<=' number [ DELTA number ]
+accuracy::= EXACT | ERROR (abs | rel | rank) '<=' number [ DELTA number ]
+            [ SCOPE (per_value | whole_result) ]

Design scope

The fact sits beside max_distinct_per_evaluation in SourceFacts. They differ in scope: max_distinct_per_evaluation must hold for every distinct count over the source, an ungrouped one included. The new map holds only for groups keyed by the named label. The memo lookup sits beside Memo::max_distinct_per_evaluation. The HLL-specific rule lives in guarantee/hll, and the edge check takes it before the measured table.

Usage

End to end with asap_planner --run-until stage3 --measured-accuracy crates/accuracy/measured/hll_distinct.json. The workload is E6 x1's flows (10,000 hosts, one row per host every 500 ms, so at most 7,200 rows per host in 1 h). The query is the per-host distinct count above, every 10 s, at rel 0.05 and δ 0.05. The exact baseline excludes every distinct-count sketch with --deployment-capabilities.

# This branch alone (#1562's dense HLL pricing)
exact (sketches excluded)                       objective 7.154905e3/s  Aggregate cardinality() over raw data
whole result, no bound (today)                  objective 7.154905e3/s  Aggregate cardinality() over raw data
whole result, {"host": 7200} declared           objective 5.524932e2/s  SummaryAgg HLL θ=Hll(precision=16), query time
SCOPE per_value (accuracy_scope per_value)      objective 5.238499e2/s  SummaryAgg HLL θ=Hll(precision=13), query time

# With #1556 merged locally (sparse HLL pricing + ranking contract), not part of this PR
whole result, {"host": 7200} declared           objective 2.599679e2/s  HLL chain p16 kept at ingestion time (10 s + 60 s levels, 1 h cover)
per_value                                       objective 2.555245e2/s  HLL chain p13 kept at ingestion time
ranked (ORDER BY c DESC LIMIT 10), bound        objective 2.599819e2/s  HLL chain p16 kept at ingestion time
ranked, per_value                               objective 7.154919e3/s  exact: #1556's ranking contract splits δ over the 10,000 ranked rows (requires δ 0.000005)

In SIL the same targets are written as:

q1: SELECT host, count(DISTINCT dst) AS c FROM flows
    WHERE ts > $now - INTERVAL '1 hour' GROUP BY host
    REQUIRE ERROR rel <= 0.05 DELTA 0.05 SCOPE per_value, LATENCY <= 1000ms;

sil_intents::scope_per_value_keeps_measured_hll plans this with the measured table and reads HLL at precision 13. per_host_bound_keeps_sparse_exact_hll plans the default scope with {"host": 7200} declared and reads HLL at precision 16.

Tests

Failing tests were committed first (db2c887), then the implementation (6f3da00) and review fixes (9a80ab0).

Not in this PR

🤖 Generated with Claude Code

zzylol and others added 3 commits December 4, 2026 04:59
… and SIL SCOPE per_value

- types: max_distinct_per_group_per_evaluation is keyed by label and positive
- accuracy: HLL is exact while a declared per-group bound keeps it sparse (<= m/8)
- frontend-sil: REQUIRE ... SCOPE per_value | whole_result
- integration (W1, E6 x1): whole-result keeps no HLL without the bound; with
  {"host": 7200} Pass 2 keeps p16 and Stage 3 selects the chain below exact
- planner SIL: per-host distinct keeps p16 with the bound, p13 with SCOPE per_value

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…E ... SCOPE per_value

- SourceFacts::max_distinct_per_group_per_evaluation ({"host": 7200}): distinct
  items one group's distinct count holds per evaluation, keyed by the grouping label;
  validated positive (ZeroMaxDistinct)
- Memo::max_distinct_per_group_per_evaluation: the bound for a summary group's keys
- accuracy: exact_while_sparse_guarantee (HLL, bound <= 2^p/8): relative error 0,
  failure probability <= n(n-1)/2^65; checked before measured/theoretical guarantees
  on the logical edge and in build_meets_level
- SIL: REQUIRE ERROR m <= eps [DELTA d] [SCOPE per_value | whole_result] lowers to
  AccuracyTarget::accuracy_scope
- docs: the new fact (input-output workflow), HLL row (accuracy models)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…y for HLL distinct counts

- Memo::max_distinct_per_group_per_evaluation: None unless the summary reads one
  scan through single-input operators (a self-join pairs rows the bound does not
  cover); memo test covers host, (host, dst), dst, ungrouped and a self-join
- accuracy: one sparse_exact_guarantee (pub(crate)), checking HLL + Cardinality
  before computing the bound
- SIL: EXACT SCOPE gets its own error; SCOPE before DELTA rejected (test)
- docs: the fact holds over one scan of the source

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zzylol zzylol added enhancement New feature or request accuracy Accuracy requirements, guarantees, and propagation parser External or internal query parser behavior workload Batch, repeating, or multi-query workload modeling labels Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

accuracy Accuracy requirements, guarantees, and propagation enhancement New feature or request parser External or internal query parser behavior workload Batch, repeating, or multi-query workload modeling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant