Repository navigation
feat(kll): add weighted update - #293
Merged
Merged
Conversation
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>
Contributor
There was a problem hiding this comment.
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
Open (3)
What changed in this PR
Adds efficient weighted updates to KLL sketches.
Changes:
- Adds
update_with_weightand 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.
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>
WenyXu
reviewed
Nov 16, 2026
This reverts commit 3486e33.
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.
WenyXu
reviewed
Nov 19, 2026
WenyXu
reviewed
Nov 19, 2026
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.
tisonkun
approved these changes
Nov 19, 2026
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.


Adds
KllSketch::update_with_weight(item, weight)for updating a KLL sketch with an item repeatedweighttimes without looping over arbitrary stream weights. The method name and zero-weight semantics followCountMinSketch::update_with_weightandFrequentItemsSketch::update_with_weight.Behavior
0is a no-op, and a weight of1delegates directly toKllSketch::update.u64::MAX,update_with_weightpanics before modifying the sketch, matchingKllSketch::updateandKllSketch::merge.weight <= capacity - num_retained, the item is reserved and appended directly to level zero without compaction, preserving exact mode up tototal_weight() == k.merge_weightedupdates extrema, inserts one level-zero copy whenweightis odd, merges(weight >> h) & 1copies into each higher level1..top_levelandweight >> top_levelcopies intotop_level(folding bits at or above level 60 into at most 15 copies at level 60), and runsgeneral_compressin place without allocating an intermediateKllSketch.KllSketch::merge_higher_levelsalso reusesself.levelsin place viamerge_into_level.# Cross-language compatibilityrustdoc section convention inCONTRIBUTING.mdand applies it onKllSketch::update_with_weightto document both the0and2^61 - 1vs.u64::MAXdifferences relative to DataSketches Java and DataSketches C++, as well as Java's signed 64-bittotal_weightinterchange bound.Tests
Adds
tests-integration/tests/kll_test/update.rscovering:u64::MAXsketches.updatecalls up tototal_weight() == k, and transition to estimation mode on the next unit update.u64::MAXatMIN_K = 8with serialization round-trips.KllSketch::mergeover sparse weighted sketches (empty level zero and 39 empty intermediate levels) and 61-level top-folded sketches atMIN_K = 8.min_kpreservation across heterogeneous merges, deterministic query and round-trip invariants under repeated compactions, ownedStringitems, and state preservation on stream-weight overflow.Companion C++ PR: apache/datasketches-cpp#582 (superseding apache/datasketches-cpp#542)