From bd00633ec9d7e1baecc03427d597ef12f7ea4a13 Mon Sep 17 00:00:00 2001 From: proost Date: Sat, 21 Nov 2026 01:20:54 +0900 Subject: [PATCH] fix: M item should be 0 after deserialize --- sampling/include/var_opt_sketch_impl.hpp | 4 +- sampling/test/var_opt_sketch_test.cpp | 65 ++++++++++++++++++++++++ sampling/test/var_opt_union_test.cpp | 47 +++++++++++++++++ 3 files changed, 114 insertions(+), 2 deletions(-) diff --git a/sampling/include/var_opt_sketch_impl.hpp b/sampling/include/var_opt_sketch_impl.hpp index 30d526af6..24ddd4116 100644 --- a/sampling/include/var_opt_sketch_impl.hpp +++ b/sampling/include/var_opt_sketch_impl.hpp @@ -550,7 +550,7 @@ var_opt_sketch var_opt_sketch::deserialize(const void* bytes, size_t ptr += sd.deserialize(ptr, end_ptr - ptr, &(items.get()[h + 1]), r); items.get_deleter().set_r(r); // serde didn't throw, so the items are now valid - return var_opt_sketch(k, h, (r > 0 ? 1 : 0), r, n, total_wt_r, rf, array_size, false, + return var_opt_sketch(k, h, 0, r, n, total_wt_r, rf, array_size, false, std::move(items), std::move(weights), num_marks_in_h, std::move(marks), allocator); } @@ -635,7 +635,7 @@ var_opt_sketch var_opt_sketch::deserialize(std::istream& is, const S if (!is.good()) throw std::runtime_error("error reading from std::istream"); - return var_opt_sketch(k, h, (r > 0 ? 1 : 0), r, n, total_wt_r, rf, array_size, false, + return var_opt_sketch(k, h, 0, r, n, total_wt_r, rf, array_size, false, std::move(items), std::move(weights), num_marks_in_h, std::move(marks), allocator); } diff --git a/sampling/test/var_opt_sketch_test.cpp b/sampling/test/var_opt_sketch_test.cpp index 179d7016f..c8dd5c18a 100644 --- a/sampling/test/var_opt_sketch_test.cpp +++ b/sampling/test/var_opt_sketch_test.cpp @@ -495,4 +495,69 @@ TEST_CASE("varopt sketch: estimate subset sum", "[var_opt_sketch]") { REQUIRE(summary.estimate < total_weight); // exact mode, so know it must be strictly less } +TEST_CASE("varopt sketch: update after deserialize in estimation mode", "[var_opt_sketch]") { + const uint32_t k = 10; + var_opt_sketch sk(k); + double total_weight = 0.0; + for (int i = 1; i <= 100; ++i) { + sk.update(i, static_cast(i)); + total_weight += i; + } + REQUIRE(sk.get_num_samples() == k); + + auto bytes = sk.serialize(); + auto sk_bytes = var_opt_sketch::deserialize(bytes.data(), bytes.size()); + std::stringstream ss(std::ios::in | std::ios::out | std::ios::binary); + sk.serialize(ss); + auto sk_stream = var_opt_sketch::deserialize(ss); + + const double light_weight = 0.5; + const double heavy_weight = 1e6; + total_weight += light_weight + heavy_weight; + + for (var_opt_sketch* d : {&sk_bytes, &sk_stream}) { + REQUIRE_NOTHROW(d->update(1000, light_weight)); + REQUIRE_NOTHROW(d->update(1001, heavy_weight)); + REQUIRE(d->get_n() == 102); + REQUIRE(d->get_num_samples() == k); + auto summary = d->estimate_subset_sum([](int) { return true; }); + REQUIRE(summary.total_sketch_weight == Approx(total_weight).margin(EPS)); + bool heavy_kept_exactly = false; + for (const auto& sample : *d) { + if (sample.first == 1001) heavy_kept_exactly = (sample.second == heavy_weight); + } + REQUIRE(heavy_kept_exactly); + } +} + +TEST_CASE("varopt sketch: update after deserialize in exact mode", "[var_opt_sketch]") { + const uint32_t k = 10; + const uint32_t n = GENERATE(0u, 5u, 10u); + var_opt_sketch sk(k); + double total_weight = 0.0; + for (uint32_t i = 1; i <= n; ++i) { + sk.update(static_cast(i), static_cast(i)); + total_weight += i; + } + REQUIRE(sk.get_num_samples() == n); + + auto bytes = sk.serialize(); + auto sk_bytes = var_opt_sketch::deserialize(bytes.data(), bytes.size()); + std::stringstream ss(std::ios::in | std::ios::out | std::ios::binary); + sk.serialize(ss); + auto sk_stream = var_opt_sketch::deserialize(ss); + + const double weight = 0.5; + total_weight += weight; + const uint32_t expected_samples = n < k ? n + 1 : k; + + for (var_opt_sketch* d : {&sk_bytes, &sk_stream}) { + REQUIRE_NOTHROW(d->update(1000, weight)); + REQUIRE(d->get_n() == n + 1); + REQUIRE(d->get_num_samples() == expected_samples); + auto summary = d->estimate_subset_sum([](int) { return true; }); + REQUIRE(summary.total_sketch_weight == Approx(total_weight).margin(EPS)); + } +} + } diff --git a/sampling/test/var_opt_union_test.cpp b/sampling/test/var_opt_union_test.cpp index a37a34f9e..fa7884d39 100644 --- a/sampling/test/var_opt_union_test.cpp +++ b/sampling/test/var_opt_union_test.cpp @@ -317,4 +317,51 @@ TEST_CASE("varopt union: deserialize truncated non-empty preamble", "[var_opt_un } } +TEST_CASE("varopt union: update after deserialize in estimation mode", "[var_opt_union]") { + const uint32_t k = 10; + var_opt_union u(k); + u.update(create_unweighted_sketch(k, 100)); + u.update(create_unweighted_sketch(k, 100)); + + auto bytes = u.serialize(); + auto u_bytes = var_opt_union::deserialize(bytes.data(), bytes.size()); + std::stringstream ss(std::ios::in | std::ios::out | std::ios::binary); + u.serialize(ss); + auto u_stream = var_opt_union::deserialize(ss); + + for (var_opt_union* du : {&u_bytes, &u_stream}) { + REQUIRE_NOTHROW(du->update(create_unweighted_sketch(k, 50))); + auto result = du->get_result(); + REQUIRE(result.get_n() == 250); + REQUIRE(result.get_num_samples() == k); + auto summary = result.estimate_subset_sum([](int) { return true; }); + REQUIRE(summary.total_sketch_weight == Approx(250.0).margin(EPS)); + } +} + +TEST_CASE("varopt union: update after deserialize in exact mode", "[var_opt_union]") { + const uint32_t k = 10; + const uint64_t extra = GENERATE(3u, 10u); + var_opt_union u(k); + u.update(create_unweighted_sketch(k, 5)); + + auto bytes = u.serialize(); + auto u_bytes = var_opt_union::deserialize(bytes.data(), bytes.size()); + std::stringstream ss(std::ios::in | std::ios::out | std::ios::binary); + u.serialize(ss); + auto u_stream = var_opt_union::deserialize(ss); + + const uint64_t total = 5 + extra; + const uint32_t expected_samples = total < k ? static_cast(total) : k; + + for (var_opt_union* du : {&u_bytes, &u_stream}) { + REQUIRE_NOTHROW(du->update(create_unweighted_sketch(k, extra))); + auto result = du->get_result(); + REQUIRE(result.get_n() == total); + REQUIRE(result.get_num_samples() == expected_samples); + auto summary = result.estimate_subset_sum([](int) { return true; }); + REQUIRE(summary.total_sketch_weight == Approx(static_cast(total)).margin(EPS)); + } +} + }