Skip to content

feat(functions-aggregate): make approx_distinct HLL precision configurable#23816

Open
sandeshkr419 wants to merge 1 commit into
apache:mainfrom
sandeshkr419:hll-precision-param
Open

feat(functions-aggregate): make approx_distinct HLL precision configurable#23816
sandeshkr419 wants to merge 1 commit into
apache:mainfrom
sandeshkr419:hll-precision-param

Conversation

@sandeshkr419

Copy link
Copy Markdown

Which issue does this PR close?

Resolves #23815

Rationale for this change

Adds runtime-configurable HLL precision to approx_distinct. Previously the register count was a compile-time constant (p=14, 16 KiB per sketch); now any precision between 4 to 18 is supported.

What changes are included in this PR?

  • HyperLogLog::with_precision(p): constructs a sketch at the requested precision; new() still defaults to p=14.
  • HyperLogLog::from_registers(vec): replaces new_with_registers([u8; 16384]); infers p from the slice length.
  • ApproxDistinct::with_hll_precision(p): forwards p through every HLL accumulator path (HLLAccumulator, NumericHLLAccumulator, HllGroupsAccumulator). Has no effect for types that use exact bitmap counting (Boolean, Int8, UInt8, Int16, UInt16) — those paths are unaffected regardless of the value passed.

Are these changes tested?

9 new tests cover: construction at non-default precisions, accuracy within expected error bounds at p=10 and p=12, roundtrip serialization at p=10/12/14, cross-precision merge panic (programming error guard), and boundary validation.

Are there any user-facing changes?

None for existing callers. Old ApproxDistinct::new() and HyperLogLog::new() continue to use p=14.

@github-actions github-actions Bot added the functions Changes to functions implementation label Jul 22, 2026
@codecov-commenter

codecov-commenter commented Jul 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.98473% with 21 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.71%. Comparing base (c118002) to head (5188279).
⚠️ Report is 21 commits behind head on main.

Files with missing lines Patch % Lines
...afusion/functions-aggregate/src/approx_distinct.rs 85.41% 20 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #23816      +/-   ##
==========================================
- Coverage   80.72%   80.71%   -0.01%     
==========================================
  Files        1089     1089              
  Lines      368911   369040     +129     
  Branches   368911   369040     +129     
==========================================
+ Hits       297785   297884      +99     
- Misses      53374    53395      +21     
- Partials    17752    17761       +9     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

…rable

HyperLogLog precision was hardcoded at p=14 (16 384 registers, 16 KiB per
sketch). Lower precision reduces memory and inter-shard state size at the cost
of higher estimation error (p=12 → 4 KiB, ~1.6% error; p=10 → 1 KiB, ~3.3%).

Changes:
- `HyperLogLog::with_precision(p)` constructs a sketch at any p in 4..=18;
  `new()` keeps p=14 as the default (no behaviour change).
- `HyperLogLog::from_registers(vec)` replaces `new_with_registers([u8; 16384])`
  and infers p from the slice length (must be a power of two).
- `register_for_hash` and `count_from_hashes` take a `p` argument.
- `ApproxDistinct::with_precision(p)` / `HllGroupsAccumulator::with_precision(p)` /
  `HLLAccumulator::with_precision(p)` / `NumericHLLAccumulator::with_precision(p)`
  propagate the chosen precision through every accumulator path.
- Wire format is unchanged: serialized state is still raw register bytes; the
  length encodes p implicitly (len == 1 << p), so merge_serialized / TryFrom
  accept any valid precision automatically.
- All 167 existing unit tests pass; 9 new tests cover precision construction,
  accuracy at p=10/12, roundtrip serialization, cross-precision merge panic,
  and boundary validation.
@sandeshkr419
sandeshkr419 force-pushed the hll-precision-param branch from 5188279 to 834c2a9 Compare July 23, 2026 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

approx_distinct: make HLL register precision configurable

2 participants