Skip to content

feat(kll): add weighted update - #293

Merged
tisonkun merged 9 commits into
apache:mainfrom
shanielh:kll-weighted-update
Nov 19, 2026
Merged

tisonkun merged 9 commits into
apache:mainfrom
shanielh:kll-weighted-update

Conversation

@shanielh

@shanielh shanielh commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Adds KllSketch::update_with_weight(item, weight) for updating a KLL sketch with an item repeated weight times without looping over arbitrary stream weights. The method name and zero-weight semantics follow CountMinSketch::update_with_weight and FrequentItemsSketch::update_with_weight.

Behavior

  • A weight of 0 is a no-op, and a weight of 1 delegates directly to KllSketch::update.
  • If the combined stream weight would exceed u64::MAX, update_with_weight panics before modifying the sketch, matching KllSketch::update and KllSketch::merge.
  • If weight <= capacity - num_retained, the item is reserved and appended directly to level zero without compaction, preserving exact mode up to total_weight() == k.
  • Otherwise, merge_weighted updates extrema, inserts one level-zero copy when weight is odd, merges (weight >> h) & 1 copies into each higher level 1..top_level and weight >> top_level copies into top_level (folding bits at or above level 60 into at most 15 copies at level 60), and runs general_compress in place without allocating an intermediate KllSketch.
  • KllSketch::merge_higher_levels also reuses self.levels in place via merge_into_level.
  • Codifies the # Cross-language compatibility rustdoc section convention in CONTRIBUTING.md and applies it on KllSketch::update_with_weight to document both the 0 and 2^61 - 1 vs. u64::MAX differences relative to DataSketches Java and DataSketches C++, as well as Java's signed 64-bit total_weight interchange bound.

Tests

Adds tests-integration/tests/kll_test/update.rs covering:

  • Zero-weight updates on empty, populated, and u64::MAX sketches.
  • Byte-for-byte exact-mode equivalence with repeated update calls up to total_weight() == k, and transition to estimation mode on the next unit update.
  • Binary level decomposition for both even and odd large weights, plus top-level bit folding up to u64::MAX at MIN_K = 8 with serialization round-trips.
  • Bidirectional KllSketch::merge over sparse weighted sketches (empty level zero and 39 empty intermediate levels) and 61-level top-folded sketches at MIN_K = 8.
  • Exact disjoint-mass preservation, min_k preservation across heterogeneous merges, deterministic query and round-trip invariants under repeated compactions, owned String items, and state preservation on stream-weight overflow.

Companion C++ PR: apache/datasketches-cpp#582 (superseding apache/datasketches-cpp#542)

KllSketch::update_with_weight(item, weight) is equivalent to calling
update(item) weight times. A weight that fits into the free capacity is
applied as plain updates; a larger one builds an exact sketch holding one
copy of the item at each level whose bit is set in the weight and merges
it, so the cost is logarithmic in the weight. This mirrors the weighted
update in datasketches-java.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The randomized accuracy test can fail legitimately, and the complexity documentation overstates the implementation guarantee.

Review effort: Balanced
Findings: 1 Medium severity · 2 Low severity

Open (3)
What changed in this PR

Adds efficient weighted updates to KLL sketches.

Changes:

  • Adds update_with_weight and internal merge refactoring.
  • Adds weighted-update integration tests and changelog documentation.
File Description
datasketches/​src/​kll/​sketch.rs Implements weighted updates.
tests-integration/​tests/​kll_test/​update.rs Tests weighted-update behavior.
tests-integration/​tests/​kll_test/​main.rs Registers the new tests.
CHANGELOG.md Documents the API.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread CHANGELOG.md Resolved
Comment thread datasketches/src/kll/sketch.rs Resolved
@tisonkun
tisonkun requested review from PsiACE and notfilippo October 8, 2026 17:37
WenyXu added a commit to WenyXu/datasketches-rust that referenced this pull request Nov 13, 2026
Adapt apache#293 to the 0.6.0 API, retain the ordinary update path for short runs, and cover weight, overflow, merge and serialization invariants.

Signed-off-by: WenyXu <wenymedia@gmail.com>
Comment thread datasketches/src/kll/sketch.rs Resolved
Comment thread datasketches/src/kll/sketch.rs Resolved
Take `self.levels` directly as the working level vector and resize it in place instead of allocating a second `Vec<Vec<T>>` and moving every retained level across. Extract `merge_into_level` and relax `merge_sorted_vec` and `general_compress` bounds from `T: Clone + Ord` to `T: Ord`, since both move items out of owned vectors without cloning.
Add `KllSketch::update_with_weight(item, weight)` to insert an item repeated `weight` times without looping over arbitrary stream weights:

- Treat a zero weight as a no-op, matching `CountMinSketch::update_with_weight` and `FrequentItemsSketch::update_with_weight`, and delegate `weight == 1` to `update`.
- Check stream-weight overflow against `u64::MAX` before mutating sketch state.
- Append directly into level zero whenever `weight <= self.capacity - self.num_retained`, preserving exact mode up to full capacity (`total_weight() <= k`).
- Decompose larger weights directly into working levels with `(weight >> h) & 1` copies at levels `1..top_level` and `weight >> top_level` copies at `top_level` (folding bits at or above level 60), then compress in one pass without constructing an intermediate `KllSketch`.
Add deterministic integration coverage for `KllSketch::update_with_weight` in `tests-integration/tests/kll_test/update.rs`:

- Zero-weight no-op behavior on empty, populated, and `u64::MAX` sketches without altering extrema or serialized bytes.
- Byte-for-byte exact-mode equivalence with repeated `update` calls up to `total_weight() == k` and transition to estimation mode on the next unit update.
- Binary level decomposition for both even (empty level zero) and odd large weights, plus top-level bit folding up to `u64::MAX` at `MIN_K = 8`.
- Disjoint-mass preservation, `min_k` preservation after heterogeneous merges, deterministic CDF/PMF/rank/round-trip invariants under repeated compactions, owned `String` items, and state preservation on stream-weight overflow.
@tisonkun
tisonkun requested a review from WenyXu November 19, 2026 07:52
Comment thread datasketches/src/kll/sketch.rs Resolved
Comment thread datasketches/src/kll/sketch.rs Resolved
Codify a fixed `# Cross-language compatibility` section convention in `CONTRIBUTING.md` alongside the existing serialization-interchange and `# Cross-language hashing` patterns, keeping primary summaries and `# Errors` / `# Panics` sections focused on the Rust contract while isolating behavioral differences and serialized range bounds in a dedicated section before `# Examples`.

Apply the section to `KllSketch::update_with_weight` to document both the zero-weight and `2^61 - 1` vs. `u64::MAX` differences relative to DataSketches Java and DataSketches C++, as well as DataSketches Java's signed 64-bit stream-weight limit for serialized interchange. Also spell the fold shift in `merge_weighted` in terms of `MAX_NUM_LEVELS - 1`.
Exercise `KllSketch::merge` in both directions on sketches built with `update_with_weight`, covering empty level zero with 39 empty intermediate levels (`1 << 40`) as well as 61-level top-folded sketches at `MIN_K = 8`, asserting `total_weight`, `sorted_view().total_weight()`, `num_retained`, extrema, exact ranks, and byte-identical serialization round-trips.

@WenyXu WenyXu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tisonkun
tisonkun enabled auto-merge (squash) November 19, 2026 09:04
@tisonkun
tisonkun merged commit 8ea7899 into apache:main Nov 19, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants