diff --git a/fi/include/frequent_items_sketch.hpp b/fi/include/frequent_items_sketch.hpp
index 2dd337e5..16f4db6f 100644
--- a/fi/include/frequent_items_sketch.hpp
+++ b/fi/include/frequent_items_sketch.hpp
@@ -244,6 +244,9 @@ 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.
+ * 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
* @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 ae77ae77..3b9941d6 100644
--- a/fi/include/frequent_items_sketch_impl.hpp
+++ b/fi/include/frequent_items_sketch_impl.hpp
@@ -158,6 +158,8 @@ 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 be less frequent than a dropped item.
+ 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 b2035439..d744ee3d 100644
--- a/fi/test/frequent_items_sketch_test.cpp
+++ b/fi/test/frequent_items_sketch_test.cpp
@@ -133,6 +133,37 @@ 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(sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_POSITIVES).empty());
+
+ items = sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_NEGATIVES, 0);
+ REQUIRE(items.size() == 1);
+ REQUIRE(items[0].get_item() == 8);
+
+ // 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());
+ 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]") {
// lg_k=8 -> capacity 192; the 193rd distinct item triggers a purge whose
// median (1) wipes every counter. The sketch still holds observations: