Skip to content

Frequencies: check lgMaxMapSize and item counts when reading a sketch - #825

Closed
leerho wants to merge 1 commit into
mainfrom
frequencies-image-checks
Closed

leerho wants to merge 1 commit into
mainfrom
frequencies-image-checks

Conversation

@leerho

@leerho leerho commented Dec 2, 2026

Copy link
Copy Markdown
Member

Summary

Adds two checks when reading a frequencies sketch. They apply 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 a negative number.
  • 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 negative count was rejected with a generic message. A count larger than the stream weight was accepted, so an item's estimate could exceed N.

The map-size checks from c95fb7c are unchanged.

Testing

  • New FrequenciesImageCountsTest covers the binary Longs and Items forms and the Longs String form. The three rejection tests fail without the change.
  • The valid images it checks include purged and merged sketches, and a sketch with the largest map size.
  • Full suite passes: 2,483 tests. The C++, Go, Rust and historical-C++ CL file checks pass.
  • This touches the same two classes as Frequencies: getFrequentItems uses > threshold, as documented and as in C++ #821 but different methods, and it merges cleanly with it.

🤖 Generated with Claude Code

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

long sum = 0;
for (int i = 0; i < numActive; i++) { //tokens after the map length alternate item, count
sum = checkCount(Long.parseLong(tokens[STR_PREAMBLE_TOKENS + 3 + (2 * i)]), sum, streamWt);
@leerho

leerho commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Combined into #827 with the other sketches' checks of the same kind; the commits are unchanged.

@leerho leerho closed this Dec 2, 2026
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.

2 participants