Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

HLL, frequencies, t-digest, theta and KLL: more checks when reading a sketch - #827

Open
leerho wants to merge 10 commits into
mainfrom
deserialize-image-checks
Open

leerho wants to merge 10 commits into
mainfrom
deserialize-image-checks

Conversation

@leerho

@leerho leerho commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Summary

This combines #824, #825 and #826 into one PR, and adds checks of theta, of the frequencies maximum error offset, and of the KLL levels array. They are the same kind of change (checks when reading a sketch) at the same priority. The commits from #824, #825 and #826 are unchanged.

HLL (from #824)

HllUtil.checkPreamble now checks more of an image when it comes from outside the sketch. heapify, wrap and writableWrap all go through it, so all three apply the checks.

  • List and set images: a list or set marked empty must have a coupon count of 0. Each coupon must have a value (the top 6 bits are not 0) and appear once. In an updatable set, each coupon must be in the slot where a lookup finds it.
  • HLL_4 images: each register that holds the aux token (15) must have exactly one aux entry, and that entry's value must not fit in the register (at least curMin + 15). There must be no other entries. In an updatable image, each entry must be where a lookup finds it.

Before this change these images were read without an error. Then estimates and unions disagreed (a duplicate coupon counted once by one path and twice by another), or the next update or union threw SketchesStateException or IndexOutOfBoundsException. The checks only read the image, and scan at most the coupon table, or the K/2 register bytes and the aux table.

Frequencies (from #825)

In the binary readers of FrequentLongsSketch and FrequentItemsSketch, and in FrequentLongsSketch.getInstance(String):

  • lgMaxMapSize must not exceed 30. A sketch takes maxMapSize as a positive int power of 2, so 30 is the largest it can be built with. A larger value was accepted, and the sketch then reported a maximum map capacity of 0 or less.
  • Item counts. Each count must be positive, and together they must not exceed the stream weight. Before, a count of 0 was silently dropped, and a count larger than the stream weight was accepted, so an item's estimate could exceed N.
  • Maximum error offset. The offset is the total weight that purges removed from each counter, so it must be in [0, stream weight]. A negative or larger offset was accepted and returned as the maximum error.
  • Non-numeric tokens in the String form. FrequentLongsSketch.getInstance(String) threw NumberFormatException for a token that is not a number. It now throws SketchesArgumentException, like its other checks, and the javadoc documents it.

KLL

KllMemorySegmentValidate now checks, for the doubles, floats, longs and items sketches, when reading or wrapping an image:

  • numLevels is at least 1, and the MemorySegment holds the stored part of the levels array before it is read;
  • the level boundaries start at 0 or more and never decrease;
  • in the updatable form, the top boundary equals the item capacity that k, m and numLevels give, as the sketch's growth scheme keeps it;
  • the MemorySegment is at least as large as the image.

Before, the updatable form took the item capacity from the image, and reading it allocated that many items. The javadoc of the doubles, floats and longs reader now also says that it accepts both the compact and the updatable form, and names the exception.

Theta

Theta must be greater than 0. As a long it is in [1, Long.MAX_VALUE], and a value of 0 or a negative value was accepted, giving an estimate of Infinity or a negative number. The check applies to compact and compressed compact images, updatable sketches, unions (both the union's theta and its sketch's theta) and intersections, for heapify and wrap.

t-digest (from #826)

  1. k in the reference formats. asBytes and asSmallBytes store k as a double or a float, and it was cast to short before it was checked, so 65546 read as 10. k must now be in [10, 32767] before the cast.
  2. getRank near the largest double. When centroid means have opposite signs near Double.MAX_VALUE, the interpolation could be infinite, and the rank was Infinity or NaN. It now falls back to a fraction computed from halves of the values.
  3. Tail quantiles near the largest double. When a tail centroid's mean has the opposite sign from min or max, getQuantile returned NaN. It now uses the convex combination in that case.

In 2 and 3, the original arithmetic is kept whenever it is finite, so results for all other inputs are unchanged.

Testing

  • HLL: 11 new rejection tests in HllCorruptImageTest (heapify, wrap, HllUnion.heapify, and writableWrap for updatable images), plus a test of valid HLL_4 images with aux entries across lgK 4 to 10 and a million updates.
  • Frequencies: new FrequenciesImageCountsTest for the binary Longs and Items forms and the Longs String form, including purged and merged sketches and the largest map size. checkOffset covers the offset in all three forms, and checkNonNumericTokens covers every token of the String form.
  • KLL: new KllCorruptLevelsTest, with 7 rejection tests for both forms and both read paths, each checked to allocate under 1 MB. Before writing the checks, 5,600 valid images (all types, sketches with and without a MemorySegment, merges with different k) were confirmed to satisfy them.
  • Theta: checkThetaNotPositive in ThetaCorruptImageTest covers every form and path above. checkValidThetas covers estimation mode and sampling with a small p, including an empty sampled sketch.
  • t-digest: compatK, ranksAndQuantilesNearDoubleLimits and tailQuantilesNearDoubleLimits.
  • Every new rejection test fails without its change.
  • Full suite passes: 2,511 tests. The C++, Go, Rust and historical-C++ CL file checks pass; check_CL_binary_files has the same 4 failures as main.

🤖 Generated with Claude Code

leerho and others added 5 commits December 2, 2026 14:53
- a list or set image marked empty must have a zero count
- each coupon must have a value and appear once; in an updatable set
  each coupon must be where a lookup finds it
- in an HLL_4 image each register that refers to the aux map must have
  one aux entry, with a value that does not fit in the register, and
  there must be no other entries; in an updatable image each entry must
  be where a lookup finds it

The checks run in checkPreamble, so every way of reading an image
applies them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- lgMaxMapSize must not exceed 30, the largest a sketch can be
  constructed with
- each item count must be positive, and together they must not exceed
  the stream weight

Both binary readers and the String reader apply the checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reference formats store k as a double or a float. It was cast to
short before it was checked, so a value out of range could wrap to a
valid k: 65546 read as 10. k must now be in [10, 32767] before the cast.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The interpolation in getRank multiplied a weight by a difference of
values, and divided by the difference of two centroid means. For means of
opposite signs near Double.MAX_VALUE either can be infinite, and the rank
was Infinity or NaN. It now falls back to a fraction computed from the
halves of the values. For other values the arithmetic is unchanged.

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

A tail centroid can hold the extreme value and values of the opposite
sign, so its mean and min (or max) can have opposite signs. Near
Double.MAX_VALUE their difference is not finite, and the tail
interpolation in getQuantile gave NaN. It now uses the convex
combination in that case. For other values the arithmetic is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
leerho and others added 2 commits December 2, 2026 15:32
Theta is in (0, 1], so as a long it is in [1, Long.MAX_VALUE]. A
theta of 0 or a negative thetaLong was accepted, and the sketch then
gave an estimate of Infinity or a negative number. The check applies to
compact and compressed compact images, updatable sketches, unions
(including the union's own theta) and intersections, for heapify and
wrap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The offset is the total weight removed from each counter by purges, so
it is in [0, stream weight]. A negative or larger offset was accepted
and returned as the maximum error. The check applies to the binary
readers of FrequentLongsSketch and FrequentItemsSketch, and to
FrequentLongsSketch.getInstance(String).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leerho leerho changed the title HLL, frequencies and t-digest: more checks when reading a sketch HLL, frequencies, t-digest and theta: more checks when reading a sketch Dec 2, 2026
leerho and others added 3 commits December 2, 2026 15:37
FrequentLongsSketch.getInstance(String) parsed each token with
Integer.parseInt or Long.parseLong, so a token that is not a number
threw NumberFormatException. Each token is now parsed through a helper
that throws SketchesArgumentException instead, as the other checks of
the String form do. The javadoc now documents the exception.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When reading an image, KllMemorySegmentValidate now checks:
- numLevels is at least 1, and the MemorySegment holds the stored part
  of the levels array before it is read;
- the level boundaries start at 0 or more and never decrease;
- in the updatable form, the top boundary equals the item capacity
  that k, m and numLevels give, as the sketch's growth scheme keeps it;
- the MemorySegment is at least as large as the image.

Before, the updatable form took the item capacity from the image, and
reading it allocated that many items.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The javadoc of the method that reads an image into a new sketch, in
KllFloatsSketch, KllDoublesSketch and KllLongsSketch, named only the
compact form, but both forms are accepted. It now names both and
documents the SketchesArgumentException for an invalid image.
KllItemsSketch has only the compact form and is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leerho leerho changed the title HLL, frequencies, t-digest and theta: more checks when reading a sketch HLL, frequencies, t-digest, theta and KLL: more checks when reading a sketch Dec 2, 2026
@leerho
leerho requested review from proost and tisonkun December 3, 2026 00:04

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

Current changes looks correct to me. But can you adding more strict validation too?


//The image must have at least one level, and must hold the stored part of the levels array.
private void checkLevelsFit(final int storedLevels) {
if (numLevels < 1) { throw new SketchesArgumentException(LEVELS_INVALID + "numLevels: " + numLevels); }

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.

how about gurading numLevels > 61 too?

* @throws SketchesArgumentException if it is larger than a sketch can be constructed with
*/
static void checkLgMaxMapSize(final int lgMaxMapSize) {
if (lgMaxMapSize > LG_MAX_MAP_SIZE) {

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.

guarding "LG_MIN_MAP_SIZE" too?

This branch has not been deployed

No deployments
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