Repository navigation
Fix JaccardSimilarity.exactlyEqual throwing for small theta sketches - #778
Open
MaxFreedomPollard wants to merge 1 commit into
Open
MaxFreedomPollard wants to merge 1 commit into
MaxFreedomPollard wants to merge 1 commit into
Conversation
exactlyEqual() sized its union with ceilingPowerOf2(countA + countB) and no bounds, so ThetaSetOperationBuilder.setNominalEntries() threw SketchesArgumentException whenever the two retained counts summed to 8 or less, for example two sketches holding one item each, or to more than 2^26. Clamp the union size to [2^4, 2^26], as jaccard() has done since apache#153 and as the C++ compute_union() does for both jaccard() and exactly_equal(). Equal sketches that retain more than 2^26 entries, which needs 2^26 nominal entries, now compare as not equal instead of throwing; the javadoc notes this. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.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.
org.apache.datasketches.theta.JaccardSimilarity.exactlyEqual(a, b)throws when the two sketches are small:On the same pair,
jaccard(a, b)returns[1.0, 1.0, 1.0]and the tupleJaccardSimilarity.exactlyEqualreturnstrue.Root cause
exactlyEqual()sizes its union withceilingPowerOf2(countA + countB)and passes that straight toThetaSetOperationBuilder.setNominalEntries(), which only accepts 16 to 2^26 (JaccardSimilarity.java:120). So any two non-empty sketches whose retained counts add up to 8 or less make it throw. That includes p-sampled sketches that are non-empty but retain no entries: the count is 0, so the requested size is 1. A total above 2^26 throws too.This is #153 again. That issue reported the same expression in
jaccard(). The 2017 fix (e22e016) clamped the size there to[1 << MIN_LG_NOM_LONGS, 1 << MAX_LG_NOM_LONGS]and leftexactlyEqual()unchanged. In C++,jaccard()andexactly_equal()share one clampedcompute_union()(theta/include/theta_jaccard_similarity_base.hpp).Fix
This copies the three clamping lines from
jaccard()intoexactlyEqual(). With the clamp, the result is exact for sketches configured with up to 2^25 nominal entries, because two equal sketches of that size never retain more than 2^26 entries, so the union keeps all of them. Two equal sketches that each retain more than 2^26 entries (which needs 2^26 nominal entries) now return false instead of throwing, because the union result is cut back to 2^26 entries. A javadoc note says so, modelled on the onejaccard()already has.The new
JaccardSimilarityTest.checkMinKExactlyEqualfollowscheckMinK, which was added forjaccard()with #153.Verification
Maven Central was not reachable from my environment, so I could not run
mvn test, and the TestNG suite has not been run on this change. What I did instead, with Temurin 25.0.4.1 on macOS aarch64:src/main/javawithjavac --release 25 -Xlint:all -Xdoclint:all/protected. There were no diagnostics for this class.JaccardSimilarityTestclasses against a minimal local stand-in fororg.testng.annotations.Testandorg.testng.Assert, then called every@Testmethod by reflection.I did not run the case above 2^26. It needs several GB of heap, so that part is reasoned from the code only.
🤖 Generated with Claude Code