Repository navigation
fix: kll_sketch throws when a sketch is merged into itself, as in Java - #570
Open
stojkomilos wants to merge 1 commit into
Open
stojkomilos wants to merge 1 commit into
stojkomilos wants to merge 1 commit into
Conversation
merge() reads the items of the other sketch while it updates this one. When both are the same sketch, the merge fails once it compacts, with "Total weight does not match N", and with string items it reads destroyed strings. The Java KLL sketches throw a SketchesArgumentException with "A sketch cannot merge with itself." (apache/datasketches-java#516). kll_sketch now throws std::invalid_argument with the same message, before its other checks as in Java, so an empty sketch merged into itself throws too. Co-authored-by: Isaac <no-reply@databricks.com>
stojkomilos
marked this pull request as ready for review
November 9, 2026 12:20
stojkomilos
added a commit
to stojkomilos/datasketches-cpp
that referenced
this pull request
Nov 9, 2026
apache#570 makes a self-merge throw, as Java does, so merge() is the same as on master here. Without apache#570, a self-merge of a sketch whose buffer is still growing can fail where master works, so this PR merges after apache#570. Co-authored-by: Isaac <no-reply@databricks.com>
This branch has not been deployed
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.
kll_sketch::merge()now throwsstd::invalid_argument("A sketch cannot merge with itself.")when the other sketch is this sketch.Java
The Java KLL sketches throw in the same case, since apache/datasketches-java#516.
KllDoublesSketch,KllFloatsSketch,KllLongsSketchandKllItemsSketchall startmerge()with this check, whereSELF_MERGE_MSGinQuantilesAPIis "A sketch cannot merge with itself. ":Master
On master, a self-merge sometimes works and sometimes throws "Total weight does not match N", depending on n and k. A minimal repro with the default k = 200:
On master (c22f2b1) it prints:
With this PR, all three throw "A sketch cannot merge with itself.".
Tests
All 17 CTest suites pass with GCC 10.5 and C++11, and
kll_testalso passes with ASan and UBSan. Without the check, the new section fails.