Skip to content
Merged
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 @@ -674,6 +674,7 @@ class kll_sketch {
static void check_serial_version(uint8_t serial_version);
static void check_family_id(uint8_t family_id);
static void check_num_levels(uint8_t num_levels);
static void check_n(uint64_t n, uint32_t num_retained);
static void check_levels(const vector_u32& levels);

void check_sorting() const;
Expand Down
19 changes: 19 additions & 0 deletions kll/include/kll_sketch_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ kll_sketch<T, C, A> kll_sketch<T, C, A>::deserialize(std::istream& is, const Ser
}
levels[num_levels] = capacity;
check_levels(levels);
check_n(n, levels[num_levels] - levels[0]);
optional<T> min_item;
optional<T> max_item;
if (!is_single_item) {
Expand Down Expand Up @@ -729,6 +730,7 @@ kll_sketch<T, C, A> kll_sketch<T, C, A>::deserialize(const void* bytes, size_t s
}
levels[num_levels] = capacity;
check_levels(levels);
check_n(n, levels[num_levels] - levels[0]);
optional<T> min_item;
optional<T> max_item;
if (!is_single_item) {
Expand Down Expand Up @@ -1135,6 +1137,23 @@ void kll_sketch<T, C, A>::check_family_id(uint8_t family_id) {
}
}

// a sketch that is not empty retains at least one item, has n from 1 to kll_constants::MAX_N,
// the cap that update() and merge() keep, and has n at least as large as the number of retained items,
// each of which has a weight of at least 1
template<typename T, typename C, typename A>
void kll_sketch<T, C, A>::check_n(uint64_t n, uint32_t num_retained) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about adding check "num_retained == 0"?

Because this function is called after empty sketch early returned. So num_retained should be not zero.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, added in 5efc20a. Before it, an image with the first level offset at the capacity and nothing after min and max was accepted as a non-empty sketch with no retained items; the new test section "no retained items" checks it is rejected.

if (num_retained == 0) {
throw std::invalid_argument("Possible corruption: a sketch that is not empty retains no items");
}
if (n == 0 || n > kll_constants::MAX_N) {
throw std::invalid_argument("Possible corruption: n must be from 1 to 2^63 - 1: " + std::to_string(n));
}
if (n < num_retained) {
throw std::invalid_argument("Possible corruption: n " + std::to_string(n)
+ " is less than the number of retained items " + std::to_string(num_retained));
}
}

template<typename T, typename C, typename A>
void kll_sketch<T, C, A>::check_num_levels(uint8_t num_levels) {
if (num_levels == 0) {
Expand Down
40 changes: 40 additions & 0 deletions kll/test/kll_sketch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,7 @@ TEST_CASE("kll sketch: iterator weights when level zero is empty", "[kll_sketch]
TEST_CASE("kll sketch: deserialize rejects inconsistent levels", "[kll_sketch]") {
using vector_bytes = kll_sketch<int>::vector_bytes;
const size_t k_offset = 4;
const size_t n_offset = 8;
const size_t num_levels_offset = 18;
const size_t levels_offset = 20;
auto put_u32 = [](vector_bytes& bytes, size_t offset, uint32_t value) {
Expand Down Expand Up @@ -1203,6 +1204,45 @@ TEST_CASE("kll sketch: deserialize rejects inconsistent levels", "[kll_sketch]")
check_rejected(bytes);
}

SECTION("no retained items") {
auto bytes = one_level.serialize();
const uint32_t capacity = get_u32(bytes, levels_offset) + one_level.get_num_retained();
put_u32(bytes, levels_offset, capacity);
bytes.resize(bytes.size() - one_level.get_num_retained() * sizeof(int)); // no items after min and max
check_rejected(bytes);
}

SECTION("n above 2^63 - 1") {
auto bytes = three_levels.serialize();
const uint64_t n = kll_constants::MAX_N + 1;
std::memcpy(bytes.data() + n_offset, &n, sizeof(n));
check_rejected(bytes);
}

SECTION("n of zero") {
auto bytes = three_levels.serialize();
const uint64_t n = 0;
std::memcpy(bytes.data() + n_offset, &n, sizeof(n));
check_rejected(bytes);
}

SECTION("n below the number of retained items") {
auto bytes = three_levels.serialize();
const uint64_t n = three_levels.get_num_retained() - 1;
std::memcpy(bytes.data() + n_offset, &n, sizeof(n));
check_rejected(bytes);
}

SECTION("n of 2^63 - 1 is accepted") {
auto bytes = three_levels.serialize();
const uint64_t n = kll_constants::MAX_N;
std::memcpy(bytes.data() + n_offset, &n, sizeof(n));
REQUIRE(kll_sketch<int>::deserialize(bytes.data(), bytes.size()).get_n() == n);
std::stringstream s(std::ios::in | std::ios::out | std::ios::binary);
s.write(reinterpret_cast<const char*>(bytes.data()), bytes.size());
REQUIRE(kll_sketch<int>::deserialize(s).get_n() == n);
}

SECTION("zero levels") {
auto bytes = three_levels.serialize();
bytes[num_levels_offset] = 0;
Expand Down
Loading