Skip to content

Tuple: support custom update seeds, and check seed hashes - #785

Merged
leerho merged 4 commits into
mainfrom
tuple-custom-seed
Oct 31, 2026
Merged

leerho merged 4 commits into
mainfrom
tuple-custom-seed

Conversation

@leerho

@leerho leerho commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Summary

Java tuple sketches always hashed with the default seed. They wrote a fixed seed hash and never checked the seed hash, either when reading an image or when combining sketches. As a result, Java accepted images created with any seed, and set operations could silently mix sketches built with different seeds. The C++, Go and Rust tuple sketches support custom seeds and reject mismatches. This PR brings Java in line with them, and with Java theta.

API: all changes are additive overloads

No existing signature changes. The new methods and constructors are:

Behavior

  • Writing: compact images record the sketch's seed hash. Empty images record zero, as in theta. Images written with the default seed are byte-for-byte unchanged.
  • Reading: reading a non-empty compact image checks its seed hash against the expected seed. That is the default seed unless the new overload is used. Legacy images (serial versions 1 and 2) were always written with the default seed.
  • Set operations: union, intersection, A-not-B and Jaccard check the seed hash of every non-empty tuple or theta input. Their results carry the operation's seed hash.
  • Updatable form: the deprecated updatable serialized form has no seed hash, so it is always read as the default seed. Serializing an updatable sketch built with a custom seed throws SketchesStateException. Call compact() first. UpdatableTupleSketch.toByteArray() makes this check by comparing the user's seed with the default seed, so a custom seed whose 16-bit seed hash happens to equal the default seed hash is refused too. C++ doesn't serialize updatable tuple sketches at all.

For default-seed users nothing changes. The only new exceptions are seed mismatches, which used to produce wrong results silently.

Seed handling cleanup in theta

The update seed should never be visible outside a sketch. Only its 16-bit seed hash is public. Two theta changes follow from that:

  • UpdatableThetaSketch.getSeed() is deprecated in favor of getSeedHash(), and will be removed in a future release. Internal code no longer calls it.
  • UpdatableThetaSketchBuilder.toString() and ThetaSetOperationBuilder.toString() print the seed hash instead of the seed. Builder output often ends up in logs.

The builders' getSeed() methods are unchanged.

Verification

  • mvn verify passes, including javadoc and style checks.
  • The cross-language checks (check_java_files, check_cpp_files and check_go_files) pass for TupleCrossLanguageTest. Java still reads the C++ and Go tuple_int_* images, now with their seed hash checked.
  • The new test file is TupleCustomSeedTest. It covers:
    • seed hashes through copy(), compact() and serialization;
    • that the default-seed image still carries 37836, as C++ does;
    • the empty-image handling;
    • reading compact images with a matching and with a mismatched seed;
    • the updatable-form behavior;
    • the stateful and stateless union, intersection and A-not-B, with both tuple and theta inputs;
    • Jaccard and Filter;
    • the derived sketches, including FdtSketch.
    • a custom seed whose seed hash collides with the default seed hash, which updatable serialization still refuses.
  • The theta builder tests check that toString() prints the seed hash and never the seed.

🤖 Generated with Claude Code

@leerho
leerho requested review from proost and tisonkun October 29, 2026 21:11
Java tuple sketches always hashed with the default seed, wrote a fixed seed
hash, and never checked the seed hash when reading an image or combining
sketches. C++, Go and Rust support custom seeds and reject mismatches.

All API changes are additive overloads:
- UpdatableTupleSketchBuilder.setSeed(), and a seed constructor on
  UpdatableTupleSketch, DoubleTupleSketch, IntegerTupleSketch,
  ArrayOfStringsTupleSketch and FdtSketch.
- Seed constructors on TupleUnion, TupleIntersection and TupleAnotB, and
  seed overloads of the static TupleAnotB.aNotB methods and of the tuple
  JaccardSimilarity methods.
- TupleSketch.heapifySketch(seg, deserializer, seed) and
  TupleSketch.getSeedHash().

Behavior:
- Compact images record the sketch's seed hash (zero when empty, as in
  theta). Default-seed images are byte-for-byte unchanged.
- Reading a non-empty compact image checks its seed hash.
- Set operations check the seed hash of non-empty tuple and theta inputs,
  and their results carry the operation's seed hash.
- The deprecated updatable form has no seed hash, so it is read as the
  default seed, and serializing an updatable sketch with a custom seed
  throws; compact() it first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leerho
leerho force-pushed the tuple-custom-seed branch from 212cc54 to 74ed06c Compare October 30, 2026 17:47
@leerho

leerho commented Oct 30, 2026

Copy link
Copy Markdown
Member Author

Merging #784 (tuple PreambleUtil) caused a conflict with this PR in CompactTupleSketch, so I rebased it onto the new main and force-pushed. The rebase keeps #784's PreambleUtil constants and adds the seed handling on top: the seed hash is read and written at SEED_HASH_SHORT. The functional changes are the same as before, and mvn verify passes locally. If you've already started reviewing, please look at the new commit 74ed06cb5.

@proost proost 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.

Overall looks good to me, leave nit review

leerho and others added 2 commits October 31, 2026 15:26
…hash

The updatable (QuickSelectSketch) serialized form has no seed hash field,
so an updatable sketch created with a custom seed cannot be serialized in
that form. The check was in QuickSelectSketch.toByteArray() and compared
seed hashes, because QuickSelectSketch has only the seed hash.

Move the check to UpdatableTupleSketch.toByteArray(), which has the user's
seed, and compare it with DEFAULT_UPDATE_SEED. This also refuses a custom
seed whose 16-bit seed hash happens to equal the default seed hash.
QuickSelectSketch.toByteArray() no longer checks anything: the other
QuickSelectSketch instances are heapified from the updatable form, which
always uses the default seed, or are the internal sketch of TupleUnion,
which is never serialized.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…sh()

The update seed should not be visible outside of the sketch. Only its
16-bit seed hash, which ThetaSketch.getSeedHash() already returns, is
meant to be public.

- Deprecate UpdatableThetaSketch.getSeed(). It will be removed in a
  future release.
- HeapUpdateSketch no longer calls getSeed(). It computes its seed hash
  once, at construction, as the direct sketch does.
- SketchTest compares seed hashes instead of seeds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UpdatableThetaSketchBuilder.toString() and ThetaSetOperationBuilder.toString()
printed the configured update seed. Builder output often ends up in logs,
which can be readable by people who also handle the sketch images. Print
the seed hash instead, in the same hex | decimal form as the sketches'
toString(). A seed whose seed hash is zero, which the sketches reject, is
reported as invalid instead of throwing from toString().

The builders' getSeed() methods are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leerho
leerho merged commit ade7ecb into main Oct 31, 2026
6 checks passed
@leerho
leerho deleted the tuple-custom-seed branch October 31, 2026 23:34
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.

3 participants