Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kll/include/kll_sketch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ class kll_sketch {
* Merges another sketch into this one.
* If sketches contain strings, callers are responsible for ensuring that
* both sketches were built using compatible string encodings.
* If other is this sketch, this throws std::invalid_argument.
* @param other sketch to merge into this one
*/
template<typename FwdSk>
Expand Down
1 change: 1 addition & 0 deletions kll/include/kll_sketch_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ uint32_t kll_sketch<T, C, A>::internal_update() {
template<typename T, typename C, typename A>
template<typename FwdSk>
void kll_sketch<T, C, A>::merge(FwdSk&& other) {
if (this == &other) { throw std::invalid_argument("A sketch cannot merge with itself."); }
if (other.is_empty()) { return; }
if (m_ != other.m_) {
throw std::invalid_argument("incompatible M: " + std::to_string(m_) + " and " + std::to_string(other.m_));
Expand Down
10 changes: 10 additions & 0 deletions kll/test/kll_sketch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,16 @@ TEST_CASE("kll sketch", "[kll_sketch]") {
REQUIRE(sketch2.get_max_item() == 999999.0f);
}

SECTION("merge into itself") {
kll_float_sketch sketch(200, std::less<float>(), 0);
REQUIRE_THROWS_WITH(sketch.merge(sketch), "A sketch cannot merge with itself.");
for (int i = 0; i < 150; i++) sketch.update(static_cast<float>(i));
const auto bytes = sketch.serialize();
REQUIRE_THROWS_WITH(sketch.merge(sketch), "A sketch cannot merge with itself.");
REQUIRE_THROWS_WITH(sketch.merge(static_cast<const kll_float_sketch&>(sketch)), "A sketch cannot merge with itself.");
REQUIRE(sketch.serialize() == bytes);
}

SECTION("sketch of ints") {
kll_sketch<int> sketch;
REQUIRE_THROWS_AS(sketch.get_quantile(0), std::runtime_error);
Expand Down