From 5143b0eb66ab4a061103f7e9641b7aac5504d2d9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Milo=C5=A1=20Stojko?=
Date: Tue, 1 Dec 2026 11:20:25 +0000
Subject: [PATCH 1/4] Frequent items: raise a threshold below the maximum error
to it
get_frequent_items(err_type, threshold) now uses max(threshold, offset),
as datasketches-java, -go and -rust do. The one-argument overload already
passed the maximum error.
Co-authored-by: Isaac
---
fi/include/frequent_items_sketch.hpp | 2 ++
fi/include/frequent_items_sketch_impl.hpp | 1 +
fi/test/frequent_items_sketch_test.cpp | 19 +++++++++++++++++++
3 files changed, 22 insertions(+)
diff --git a/fi/include/frequent_items_sketch.hpp b/fi/include/frequent_items_sketch.hpp
index 2dd337e5e..a1b1a6dba 100644
--- a/fi/include/frequent_items_sketch.hpp
+++ b/fi/include/frequent_items_sketch.hpp
@@ -244,6 +244,8 @@ class frequent_items_sketch {
* There may be items omitted from the set with true frequencies greater than the
* threshold (false negatives).
*
+ * If the threshold is lower than get_maximum_error(), then get_maximum_error() will be used instead.
+ *
* @param err_type determines whether no false positives or no false negatives are desired.
* @param threshold to include items in the result list
* @return an array of frequent items
diff --git a/fi/include/frequent_items_sketch_impl.hpp b/fi/include/frequent_items_sketch_impl.hpp
index ae77ae77c..13ec1dc55 100644
--- a/fi/include/frequent_items_sketch_impl.hpp
+++ b/fi/include/frequent_items_sketch_impl.hpp
@@ -158,6 +158,7 @@ auto frequent_items_sketch::get_frequent_items(frequent_items_err
template
auto frequent_items_sketch::get_frequent_items(frequent_items_error_type err_type, W threshold) const -> vector_row {
vector_row items(map.get_allocator());
+ threshold = std::max(threshold, offset);
for (auto it: map) {
const W lb = it.second;
const W ub = it.second + offset;
diff --git a/fi/test/frequent_items_sketch_test.cpp b/fi/test/frequent_items_sketch_test.cpp
index b2035439a..d6efa7ab5 100644
--- a/fi/test/frequent_items_sketch_test.cpp
+++ b/fi/test/frequent_items_sketch_test.cpp
@@ -133,6 +133,25 @@ TEST_CASE("frequent items: estimation mode", "[frequent_items_sketch]") {
REQUIRE(12 >= items.size()); // but not more than 12 items
}
+TEST_CASE("frequent items: threshold below the maximum error is raised to it", "[frequent_items_sketch]") {
+ struct identity_hash {
+ size_t operator()(int64_t v) const { return static_cast(v); }
+ };
+ frequent_items_sketch sketch(3);
+ for (int64_t i = 1; i <= 8; ++i) sketch.update(i);
+ REQUIRE(sketch.get_maximum_error() == 1); // capacity is 6, so the 7th distinct item purges every count of 1
+
+ // threshold 0 is raised to 1, so item 8 (lower bound 1) is not returned
+ auto items = sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_POSITIVES, 0);
+ REQUIRE(items.empty());
+ REQUIRE(items.size() == sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_POSITIVES).size());
+
+ // item 8 has upper bound 2, which is above 1
+ items = sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_NEGATIVES, 0);
+ REQUIRE(items.size() == 1);
+ REQUIRE(items[0].get_item() == 8);
+}
+
TEST_CASE("frequent items: purge clearing all counters is not empty", "[frequent_items_sketch]") {
// lg_k=8 -> capacity 192; the 193rd distinct item triggers a purge whose
// median (1) wipes every counter. The sketch still holds observations:
From be5de8335308a9aa23b5dc03d7b749c8941ba14b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Milo=C5=A1=20Stojko?=
Date: Tue, 1 Dec 2026 11:44:06 +0000
Subject: [PATCH 2/4] Frequent items: test that a threshold above the maximum
error is used as given
Co-authored-by: Isaac
---
fi/test/frequent_items_sketch_test.cpp | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/fi/test/frequent_items_sketch_test.cpp b/fi/test/frequent_items_sketch_test.cpp
index d6efa7ab5..4f70b1db3 100644
--- a/fi/test/frequent_items_sketch_test.cpp
+++ b/fi/test/frequent_items_sketch_test.cpp
@@ -144,12 +144,18 @@ TEST_CASE("frequent items: threshold below the maximum error is raised to it", "
// threshold 0 is raised to 1, so item 8 (lower bound 1) is not returned
auto items = sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_POSITIVES, 0);
REQUIRE(items.empty());
- REQUIRE(items.size() == sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_POSITIVES).size());
+ REQUIRE(sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_POSITIVES).empty());
- // item 8 has upper bound 2, which is above 1
items = sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_NEGATIVES, 0);
REQUIRE(items.size() == 1);
REQUIRE(items[0].get_item() == 8);
+
+ // thresholds above the maximum error are used as given
+ sketch.update(8); // item 8 now has lower bound 2
+ REQUIRE(sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_POSITIVES, 1).size() == 1);
+ REQUIRE(sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_POSITIVES, 2).empty());
+ REQUIRE(sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_NEGATIVES, 2).size() == 1);
+ REQUIRE(sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_NEGATIVES, 3).empty());
}
TEST_CASE("frequent items: purge clearing all counters is not empty", "[frequent_items_sketch]") {
From d59f5f9cf91740bd5b373a31f100f6fb374fcec2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Milo=C5=A1=20Stojko?=
Date: Tue, 1 Dec 2026 11:54:49 +0000
Subject: [PATCH 3/4] Frequent items: document the threshold clamp's scope and
reason
Co-authored-by: Isaac
---
fi/include/frequent_items_sketch.hpp | 3 ++-
fi/include/frequent_items_sketch_impl.hpp | 1 +
fi/test/frequent_items_sketch_test.cpp | 2 +-
3 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/fi/include/frequent_items_sketch.hpp b/fi/include/frequent_items_sketch.hpp
index a1b1a6dba..dfb710803 100644
--- a/fi/include/frequent_items_sketch.hpp
+++ b/fi/include/frequent_items_sketch.hpp
@@ -244,7 +244,8 @@ class frequent_items_sketch {
* There may be items omitted from the set with true frequencies greater than the
* threshold (false negatives).
*
- * If the threshold is lower than get_maximum_error(), then get_maximum_error() will be used instead.
+ * If the threshold is lower than get_maximum_error(), then get_maximum_error() will be used instead.
+ * It has no effect on NO_FALSE_NEGATIVES.
*
* @param err_type determines whether no false positives or no false negatives are desired.
* @param threshold to include items in the result list
diff --git a/fi/include/frequent_items_sketch_impl.hpp b/fi/include/frequent_items_sketch_impl.hpp
index 13ec1dc55..6dd94cd27 100644
--- a/fi/include/frequent_items_sketch_impl.hpp
+++ b/fi/include/frequent_items_sketch_impl.hpp
@@ -158,6 +158,7 @@ auto frequent_items_sketch::get_frequent_items(frequent_items_err
template
auto frequent_items_sketch::get_frequent_items(frequent_items_error_type err_type, W threshold) const -> vector_row {
vector_row items(map.get_allocator());
+ // Below the offset, a listed item can tie with a dropped item.
threshold = std::max(threshold, offset);
for (auto it: map) {
const W lb = it.second;
diff --git a/fi/test/frequent_items_sketch_test.cpp b/fi/test/frequent_items_sketch_test.cpp
index 4f70b1db3..1f328b8e8 100644
--- a/fi/test/frequent_items_sketch_test.cpp
+++ b/fi/test/frequent_items_sketch_test.cpp
@@ -150,7 +150,7 @@ TEST_CASE("frequent items: threshold below the maximum error is raised to it", "
REQUIRE(items.size() == 1);
REQUIRE(items[0].get_item() == 8);
- // thresholds above the maximum error are used as given
+ // thresholds at or above the maximum error are used as given
sketch.update(8); // item 8 now has lower bound 2
REQUIRE(sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_POSITIVES, 1).size() == 1);
REQUIRE(sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_POSITIVES, 2).empty());
From 893bf085d396428a88357d609a4ff9c1e2883418 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Milo=C5=A1=20Stojko?=
Date: Tue, 1 Dec 2026 12:07:05 +0000
Subject: [PATCH 4/4] Frequent items: test thresholds other than 0, scope the
NO_FALSE_NEGATIVES claim
Co-authored-by: Isaac
---
fi/include/frequent_items_sketch.hpp | 2 +-
fi/include/frequent_items_sketch_impl.hpp | 2 +-
fi/test/frequent_items_sketch_test.cpp | 6 ++++++
3 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/fi/include/frequent_items_sketch.hpp b/fi/include/frequent_items_sketch.hpp
index dfb710803..16f4db6f2 100644
--- a/fi/include/frequent_items_sketch.hpp
+++ b/fi/include/frequent_items_sketch.hpp
@@ -245,7 +245,7 @@ class frequent_items_sketch {
* threshold (false negatives).
*
* If the threshold is lower than get_maximum_error(), then get_maximum_error() will be used instead.
- * It has no effect on NO_FALSE_NEGATIVES.
+ * For integer weights, it has no effect on NO_FALSE_NEGATIVES.
*
* @param err_type determines whether no false positives or no false negatives are desired.
* @param threshold to include items in the result list
diff --git a/fi/include/frequent_items_sketch_impl.hpp b/fi/include/frequent_items_sketch_impl.hpp
index 6dd94cd27..3b9941d62 100644
--- a/fi/include/frequent_items_sketch_impl.hpp
+++ b/fi/include/frequent_items_sketch_impl.hpp
@@ -158,7 +158,7 @@ auto frequent_items_sketch::get_frequent_items(frequent_items_err
template
auto frequent_items_sketch::get_frequent_items(frequent_items_error_type err_type, W threshold) const -> vector_row {
vector_row items(map.get_allocator());
- // Below the offset, a listed item can tie with a dropped item.
+ // Below the offset, a listed item can be less frequent than a dropped item.
threshold = std::max(threshold, offset);
for (auto it: map) {
const W lb = it.second;
diff --git a/fi/test/frequent_items_sketch_test.cpp b/fi/test/frequent_items_sketch_test.cpp
index 1f328b8e8..d744ee3dc 100644
--- a/fi/test/frequent_items_sketch_test.cpp
+++ b/fi/test/frequent_items_sketch_test.cpp
@@ -156,6 +156,12 @@ TEST_CASE("frequent items: threshold below the maximum error is raised to it", "
REQUIRE(sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_POSITIVES, 2).empty());
REQUIRE(sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_NEGATIVES, 2).size() == 1);
REQUIRE(sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_NEGATIVES, 3).empty());
+
+ // any threshold below the maximum error is raised, not only 0
+ frequent_items_sketch sketch2(3);
+ for (int64_t i = 1; i <= 8; ++i) sketch2.update(i, 2);
+ REQUIRE(sketch2.get_maximum_error() == 2);
+ REQUIRE(sketch2.get_frequent_items(frequent_items_error_type::NO_FALSE_POSITIVES, 1).empty());
}
TEST_CASE("frequent items: purge clearing all counters is not empty", "[frequent_items_sketch]") {