Skip to content

Frequencies: getFrequentItems uses > threshold, as documented and as in C++ - #821

Draft
stojkomilos wants to merge 2 commits into
apache:mainfrom
stojkomilos:milosh-stojko/frequencies-strict-threshold
Draft

stojkomilos wants to merge 2 commits into
apache:mainfrom
stojkomilos:milosh-stojko/frequencies-strict-threshold

Conversation

@stojkomilos

@stojkomilos stojkomilos commented Dec 1, 2026 •

Copy link
Copy Markdown

NOT READY FOR REVIEW YET

getFrequentItems returns an item when its bound is greater than or equal to the threshold (ub >= threshold and lb >= threshold in sortItems). This PR changes both comparisons to >, in FrequentItemsSketch and in FrequentLongsSketch. The serialized format does not change.

Main argument: we want to match datasketches-cpp and datasketches java. Datasketches c++ and datasketches rust use > instead of >= . Java and Go use >= (although Go may have blind copied it from Java). The question remains which of the 2 to use

Argument for using > instead of >=: Logically it makes more sense. This check is used when you have "no false positives" flag. A no false positive (if I understood correctly) is that any element that is returned from the final array, should truly be in the top <array size used> (eg. if an element appears only once (it has c=1) and triggers a purge, we don't want to return it).

I claim this: ">= can return items that are not in the top k, and it can return an item that ties with a dropped item, and > can not"

This no false positives thing is achieved in the following way:

  • For the elements in the final array (when the getter for the final frequent items is called), if they have a counter value c, they are guaranteed to have appeared a number of times that is in this range: [c, c+offset] (both ends of range included). However elements that are not in the final array at the time of the getter of final result call, they appeared a number of times in the range [0, offset] (both ends included).
    The only way to guarantee these 2 are disjoint is that their ranges don't overlap. This obviously then requires offset < c, in other words, c > offset. Proof complete

Another argument against >= and for >:

  • it contradicts with java's own docstring of getFrequentItems - src/main/java/org/apache/datasketches/frequencies/FrequentItemsSketch.java:367. It says getLowerBound(item) &gt; threshold

Changes

  • FrequentItemsSketch.sortItems and FrequentLongsSketch.sortItems: ub > threshold and lb > threshold.
  • Results change only when a bound equals the threshold:
    • With the default threshold (getMaximumError()), NO_FALSE_POSITIVES no longer returns an item whose lower bound equals the maximum error. NO_FALSE_NEGATIVES returns the same items as before, because every active item has an upper bound above the maximum error.
    • With an explicit threshold, an item whose bound equals the threshold is no longer returned.
  • LongsSketchTest already asserts getUpperBound() > threshold and getLowerBound() > threshold for the default threshold.
  • ItemsSketchTest.severalItems called getFrequentItems(3, NO_FALSE_POSITIVES) and expected item b, whose count is 3. It now passes threshold 2.
  • New tests checkThresholdIsExclusive in ItemsSketchTest and LongsSketchTest: an item whose bound equals the threshold is not returned, with the default threshold and with an explicit one. On main, the two new tests fail, and so does severalItems.

Checks

  • mvn clean package javadoc:javadoc with JDK 25: all 2481 tests pass, and the Javadoc builds.
  • On main with this PR's test files, 3 of the 73 frequencies tests fail: ItemsSketchTest.checkThresholdIsExclusive (line 553), LongsSketchTest.checkThresholdIsExclusive (line 860), and ItemsSketchTest.severalItems (line 103, expected [1] but found [2]). The other 70 pass.

This pull request and its description were written by Isaac.

stojkomilos and others added 2 commits December 1, 2026 10:43
…in C++

sortItems kept an item whose bound equals the threshold (ub >= threshold,
lb >= threshold). The Javadoc says > threshold, and datasketches-cpp and
datasketches-rust use >. Change both comparisons in FrequentItemsSketch and
FrequentLongsSketch, and add a test for a bound equal to the threshold.

Co-authored-by: Isaac <no-reply@databricks.com>
Also say why threshold 2 returns only b in severalItems.

Co-authored-by: Isaac <no-reply@databricks.com>

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.

1 participant