Skip to content

Commit cf84acd

Browse files
authored
Merge pull request #590 from apache/density-family-id-23
fix(density): use family ID 23 for serialization
2 parents bebc87b + 2c01203 commit cf84acd

3 files changed

Lines changed: 233 additions & 17 deletions

File tree

‎density/include/density_sketch.hpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ class density_sketch {
201201
enum flags { RESERVED0, RESERVED1, IS_EMPTY };
202202
static const uint8_t PREAMBLE_INTS_SHORT = 3;
203203
static const uint8_t PREAMBLE_INTS_LONG = 6;
204-
static const uint8_t FAMILY_ID = 19;
204+
static const uint8_t FAMILY_ID = 23;
205+
static const uint8_t LEGACY_FAMILY_ID = 19;
205206
static const uint8_t SERIAL_VERSION = 1;
206207
static const size_t LEVELS_ARRAY_START = 5;
207208

@@ -217,7 +218,7 @@ class density_sketch {
217218

218219
static void check_k(uint16_t k);
219220
static void check_serial_version(uint8_t serial_version);
220-
static void check_family_id(uint8_t family_id);
221+
static void check_family_id(uint8_t family_id, uint8_t serial_version);
221222
static void check_header_validity(uint8_t preamble_ints, uint8_t flags_byte, uint8_t serial_version);
222223

223224
density_sketch(uint16_t k, uint32_t dim, uint32_t num_retained, uint64_t n, Levels&& levels,

‎density/include/density_sketch_impl.hpp‎

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,12 @@ density_sketch<T, K, A> density_sketch<T, K, A>::deserialize(std::istream& is, c
272272
read<uint16_t>(is); // unused
273273
const auto dim = read<uint32_t>(is);
274274

275-
check_k(k); // do we have constraints?
276-
check_serial_version(serial_version); // a little redundant with the header check
277-
check_family_id(family_id);
275+
if (!is.good()) throw std::runtime_error("error reading from std::istream");
276+
check_serial_version(serial_version);
277+
check_family_id(family_id, serial_version);
278278
check_header_validity(preamble_ints, flags_byte, serial_version);
279+
check_k(k);
279280

280-
if (!is.good()) throw std::runtime_error("error reading from std::istream");
281281
const bool is_empty = (flags_byte & (1 << flags::IS_EMPTY)) > 0;
282282
if (is_empty) {
283283
return density_sketch(k, dim, kernel, allocator);
@@ -330,10 +330,10 @@ density_sketch<T, K, A> density_sketch<T, K, A>::deserialize(const void* bytes,
330330
uint32_t dim;
331331
ptr += copy_from_mem(ptr, dim);
332332

333-
check_k(k);
334-
check_serial_version(serial_version); // a little redundant with the header check
335-
check_family_id(family_id);
333+
check_serial_version(serial_version);
334+
check_family_id(family_id, serial_version);
336335
check_header_validity(preamble_ints, flags_byte, serial_version);
336+
check_k(k);
337337

338338
const bool is_empty = (flags_byte & (1 << flags::IS_EMPTY)) > 0;
339339
if (is_empty) {
@@ -392,26 +392,32 @@ void density_sketch<T, K, A>::check_serial_version(uint8_t serial_version) {
392392
}
393393

394394
template<typename T, typename K, typename A>
395-
void density_sketch<T, K, A>::check_family_id(uint8_t family_id) {
396-
if (family_id == FAMILY_ID)
395+
void density_sketch<T, K, A>::check_family_id(uint8_t family_id, uint8_t serial_version) {
396+
// Earlier releases wrote family ID 19, which collided with EBPPS. Accept it for v1 images;
397+
// check_header_validity distinguishes Density (preamble_ints 3 or 6) from EBPPS (1 or 5).
398+
if (family_id == FAMILY_ID || (family_id == LEGACY_FAMILY_ID && serial_version == 1))
397399
return;
398400
else
399401
throw std::invalid_argument("Possible corruption. Family id does not indicate density sketch: " + std::to_string(family_id));
400402
}
401403

402404
template<typename T, typename K, typename A>
403405
void density_sketch<T, K, A>::check_header_validity(uint8_t preamble_ints, uint8_t flags_byte, uint8_t serial_version) {
404-
const bool empty = (flags_byte & (1 << flags::IS_EMPTY)) > 0;
406+
const uint8_t empty_mask = 1 << flags::IS_EMPTY;
407+
const bool valid_flags = (flags_byte & ~empty_mask) == 0;
408+
const bool empty = (flags_byte & empty_mask) != 0;
405409

406-
if ((empty && preamble_ints == PREAMBLE_INTS_SHORT)
407-
|| (!empty && preamble_ints == PREAMBLE_INTS_LONG))
408-
return;
410+
if (valid_flags
411+
&& ((empty && preamble_ints == PREAMBLE_INTS_SHORT)
412+
|| (!empty && preamble_ints == PREAMBLE_INTS_LONG)))
413+
return;
409414
else {
410415
std::ostringstream os;
411416
os << "Possible sketch corruption. Inconsistent state: "
412-
<< "preamble_ints = " << preamble_ints
417+
<< "preamble_ints = " << static_cast<unsigned>(preamble_ints)
418+
<< ", flags = " << static_cast<unsigned>(flags_byte)
413419
<< ", empty = " << (empty ? "true" : "false")
414-
<< ", serialization_version = " << serial_version;
420+
<< ", serialization_version = " << static_cast<unsigned>(serial_version);
415421
throw std::invalid_argument(os.str());
416422
}
417423
}

‎density/test/density_sketch_test.cpp‎

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ TEST_CASE("custom kernel", "[density_sketch]") {
111111
TEST_CASE("serialize empty", "[density_sketch]") {
112112
density_sketch<double> sk(10, 2);
113113
auto bytes = sk.serialize();
114+
REQUIRE(bytes.size() >= 3);
115+
REQUIRE(bytes[2] == 23);
114116
auto sk2 = density_sketch<double>::deserialize(bytes.data(), bytes.size());
115117
REQUIRE(sk2.is_empty());
116118
REQUIRE(!sk2.is_estimation_mode());
@@ -121,6 +123,9 @@ TEST_CASE("serialize empty", "[density_sketch]") {
121123

122124
std::stringstream s(std::ios::in | std::ios::out | std::ios::binary);
123125
sk.serialize(s);
126+
const std::string stream_bytes = s.str();
127+
REQUIRE(stream_bytes.size() >= 3);
128+
REQUIRE(static_cast<uint8_t>(stream_bytes[2]) == 23);
124129
auto sk3 = density_sketch<double>::deserialize(s);
125130
REQUIRE(sk3.is_empty());
126131
REQUIRE(!sk3.is_estimation_mode());
@@ -143,6 +148,8 @@ TEST_CASE("serialize bytes", "[density_sketch]") {
143148

144149
// exact mode
145150
auto bytes = sk.serialize();
151+
REQUIRE(bytes.size() >= 3);
152+
REQUIRE(bytes[2] == 23);
146153
auto sk2 = density_sketch<double>::deserialize(bytes.data(), bytes.size());
147154
REQUIRE(!sk2.is_empty());
148155
REQUIRE(!sk2.is_estimation_mode());
@@ -168,6 +175,8 @@ TEST_CASE("serialize bytes", "[density_sketch]") {
168175
REQUIRE(sk.is_estimation_mode());
169176

170177
bytes = sk.serialize();
178+
REQUIRE(bytes.size() >= 3);
179+
REQUIRE(bytes[2] == 23);
171180
sk2 = density_sketch<double>::deserialize(bytes.data(), bytes.size());
172181
REQUIRE(!sk2.is_empty());
173182
REQUIRE(sk2.is_estimation_mode());
@@ -199,6 +208,8 @@ TEST_CASE("serialize stream", "[density_sketch]") {
199208
// exact mode
200209
std::stringstream s(std::ios::in | std::ios::out | std::ios::binary);
201210
sk.serialize(s);
211+
REQUIRE(s.str().size() >= 3);
212+
REQUIRE(static_cast<uint8_t>(s.str()[2]) == 23);
202213
auto sk2 = density_sketch<float>::deserialize(s);
203214
REQUIRE(!sk2.is_empty());
204215
REQUIRE(!sk2.is_estimation_mode());
@@ -225,6 +236,8 @@ TEST_CASE("serialize stream", "[density_sketch]") {
225236

226237
std::stringstream s2(std::ios::in | std::ios::out | std::ios::binary);
227238
sk.serialize(s2);
239+
REQUIRE(s2.str().size() >= 3);
240+
REQUIRE(static_cast<uint8_t>(s2.str()[2]) == 23);
228241
sk2 = density_sketch<float>::deserialize(s2);
229242
REQUIRE(!sk2.is_empty());
230243
REQUIRE(sk2.is_estimation_mode());
@@ -271,4 +284,200 @@ TEST_CASE("density sketch: custom allocator", "[density_sketch]") {
271284
REQUIRE(test_allocator_net_allocations == 0);
272285
}
273286

287+
TEST_CASE("density sketch: legacy family id 19 compatibility", "[density_sketch]") {
288+
// Legacy empty Density sketch (k = 10, dim = 2, family_id = 19)
289+
const std::vector<uint8_t> legacy_empty = {
290+
3, 1, 19, 4,
291+
10, 0, 0, 0,
292+
2, 0, 0, 0
293+
};
294+
{
295+
auto sk = density_sketch<double>::deserialize(legacy_empty.data(), legacy_empty.size());
296+
REQUIRE(sk.is_empty());
297+
REQUIRE(sk.get_k() == 10);
298+
REQUIRE(sk.get_dim() == 2);
299+
REQUIRE(sk.get_n() == 0);
300+
REQUIRE(sk.get_num_retained() == 0);
301+
302+
auto upgraded = sk.serialize();
303+
REQUIRE(upgraded.size() == legacy_empty.size());
304+
REQUIRE(upgraded[2] == 23);
305+
306+
std::stringstream is(
307+
std::string(reinterpret_cast<const char*>(legacy_empty.data()), legacy_empty.size()),
308+
std::ios::in | std::ios::out | std::ios::binary
309+
);
310+
auto sk_stream = density_sketch<double>::deserialize(is);
311+
REQUIRE(sk_stream.is_empty());
312+
REQUIRE(sk_stream.get_k() == 10);
313+
REQUIRE(sk_stream.get_dim() == 2);
314+
315+
std::stringstream os(std::ios::in | std::ios::out | std::ios::binary);
316+
sk_stream.serialize(os);
317+
REQUIRE(static_cast<uint8_t>(os.str()[2]) == 23);
318+
}
319+
320+
// Legacy single-point Density sketch (k = 10, dim = 2, point = [1.0, 2.0], family_id = 19)
321+
const std::vector<uint8_t> legacy_single = {
322+
6, 1, 19, 0,
323+
10, 0, 0, 0,
324+
2, 0, 0, 0,
325+
1, 0, 0, 0,
326+
1, 0, 0, 0, 0, 0, 0, 0,
327+
1, 0, 0, 0,
328+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f,
329+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40
330+
};
331+
{
332+
auto sk = density_sketch<double>::deserialize(legacy_single.data(), legacy_single.size());
333+
REQUIRE(!sk.is_empty());
334+
REQUIRE(sk.get_k() == 10);
335+
REQUIRE(sk.get_dim() == 2);
336+
REQUIRE(sk.get_n() == 1);
337+
REQUIRE(sk.get_num_retained() == 1);
338+
REQUIRE(sk.get_estimate({1.0, 2.0}) == 1.0);
339+
340+
auto upgraded = sk.serialize();
341+
REQUIRE(upgraded.size() == legacy_single.size());
342+
REQUIRE(upgraded[2] == 23);
343+
upgraded[2] = 19;
344+
REQUIRE(upgraded == legacy_single);
345+
346+
std::stringstream is(
347+
std::string(reinterpret_cast<const char*>(legacy_single.data()), legacy_single.size()),
348+
std::ios::in | std::ios::out | std::ios::binary
349+
);
350+
auto sk_stream = density_sketch<double>::deserialize(is);
351+
REQUIRE(sk_stream.get_n() == 1);
352+
REQUIRE(sk_stream.get_estimate({1.0, 2.0}) == 1.0);
353+
}
354+
355+
// Legacy estimation-mode Density sketch round-trip and re-serialization to family ID 23
356+
density_sketch<double> original(10, 3);
357+
for (uint32_t i = 0; i < 1031; ++i) {
358+
const double val = static_cast<double>(i);
359+
original.update(std::vector<double>({val, std::sqrt(val), -val}));
360+
}
361+
REQUIRE(original.is_estimation_mode());
362+
363+
auto legacy_bytes = original.serialize();
364+
REQUIRE(legacy_bytes[2] == 23);
365+
legacy_bytes[2] = 19;
366+
367+
auto from_bytes = density_sketch<double>::deserialize(legacy_bytes.data(), legacy_bytes.size());
368+
REQUIRE(from_bytes.is_estimation_mode());
369+
REQUIRE(from_bytes.get_k() == original.get_k());
370+
REQUIRE(from_bytes.get_dim() == original.get_dim());
371+
REQUIRE(from_bytes.get_n() == original.get_n());
372+
REQUIRE(from_bytes.get_num_retained() == original.get_num_retained());
373+
REQUIRE(from_bytes.get_estimate({5.0, std::sqrt(5.0), -5.0}) == original.get_estimate({5.0, std::sqrt(5.0), -5.0}));
374+
REQUIRE(from_bytes.serialize()[2] == 23);
375+
376+
std::stringstream legacy_stream(
377+
std::string(reinterpret_cast<const char*>(legacy_bytes.data()), legacy_bytes.size()),
378+
std::ios::in | std::ios::out | std::ios::binary
379+
);
380+
auto from_stream = density_sketch<double>::deserialize(legacy_stream);
381+
REQUIRE(from_stream.get_n() == original.get_n());
382+
REQUIRE(from_stream.get_num_retained() == original.get_num_retained());
383+
REQUIRE(from_stream.get_estimate({5.0, std::sqrt(5.0), -5.0}) == original.get_estimate({5.0, std::sqrt(5.0), -5.0}));
384+
std::stringstream out_stream(std::ios::in | std::ios::out | std::ios::binary);
385+
from_stream.serialize(out_stream);
386+
REQUIRE(static_cast<uint8_t>(out_stream.str()[2]) == 23);
387+
}
388+
389+
TEST_CASE("density sketch: reject invalid family and ebpps headers", "[density_sketch]") {
390+
// Unrelated family ID (20 = T-Digest)
391+
const std::vector<uint8_t> wrong_family = {
392+
3, 1, 20, 4,
393+
10, 0, 0, 0,
394+
2, 0, 0, 0
395+
};
396+
REQUIRE_THROWS_AS(
397+
density_sketch<double>::deserialize(wrong_family.data(), wrong_family.size()),
398+
std::invalid_argument
399+
);
400+
{
401+
std::stringstream is(
402+
std::string(reinterpret_cast<const char*>(wrong_family.data()), wrong_family.size()),
403+
std::ios::in | std::ios::out | std::ios::binary
404+
);
405+
REQUIRE_THROWS_AS(density_sketch<double>::deserialize(is), std::invalid_argument);
406+
}
407+
408+
// Empty EBPPS header (preamble_longs = 1, ser_ver = 1, family_id = 19, flags = 4, 8 bytes)
409+
const std::vector<uint8_t> ebpps_empty = {
410+
1, 1, 19, 4,
411+
10, 0, 0, 0
412+
};
413+
REQUIRE_THROWS_AS(
414+
density_sketch<double>::deserialize(ebpps_empty.data(), ebpps_empty.size()),
415+
std::out_of_range
416+
);
417+
{
418+
std::stringstream is(
419+
std::string(reinterpret_cast<const char*>(ebpps_empty.data()), ebpps_empty.size()),
420+
std::ios::in | std::ios::out | std::ios::binary
421+
);
422+
REQUIRE_THROWS_AS(density_sketch<double>::deserialize(is), std::runtime_error);
423+
}
424+
425+
// Padded empty EBPPS header (preamble_longs = 1, family_id = 19, flags = 4, 12 bytes)
426+
const std::vector<uint8_t> ebpps_empty_padded = {
427+
1, 1, 19, 4,
428+
10, 0, 0, 0,
429+
2, 0, 0, 0
430+
};
431+
REQUIRE_THROWS_AS(
432+
density_sketch<double>::deserialize(ebpps_empty_padded.data(), ebpps_empty_padded.size()),
433+
std::invalid_argument
434+
);
435+
{
436+
std::stringstream is(
437+
std::string(reinterpret_cast<const char*>(ebpps_empty_padded.data()), ebpps_empty_padded.size()),
438+
std::ios::in | std::ios::out | std::ios::binary
439+
);
440+
REQUIRE_THROWS_AS(density_sketch<double>::deserialize(is), std::invalid_argument);
441+
}
442+
443+
// Non-empty EBPPS header (preamble_longs = 5, ser_ver = 1, family_id = 19, flags = 0 / 8)
444+
for (uint8_t flags : {uint8_t{0}, uint8_t{8}}) {
445+
std::vector<uint8_t> ebpps_non_empty(40, 0);
446+
ebpps_non_empty[0] = 5;
447+
ebpps_non_empty[1] = 1;
448+
ebpps_non_empty[2] = 19;
449+
ebpps_non_empty[3] = flags;
450+
ebpps_non_empty[4] = 10;
451+
REQUIRE_THROWS_AS(
452+
density_sketch<double>::deserialize(ebpps_non_empty.data(), ebpps_non_empty.size()),
453+
std::invalid_argument
454+
);
455+
std::stringstream is(
456+
std::string(reinterpret_cast<const char*>(ebpps_non_empty.data()), ebpps_non_empty.size()),
457+
std::ios::in | std::ios::out | std::ios::binary
458+
);
459+
REQUIRE_THROWS_AS(density_sketch<double>::deserialize(is), std::invalid_argument);
460+
}
461+
462+
// Unexpected flag bits (bit 3 = 8) must be rejected for both legacy (19) and current (23) IDs
463+
for (uint8_t fam : {uint8_t{19}, uint8_t{23}}) {
464+
std::vector<uint8_t> bad_flags(24, 0);
465+
bad_flags[0] = 6;
466+
bad_flags[1] = 1;
467+
bad_flags[2] = fam;
468+
bad_flags[3] = 8;
469+
bad_flags[4] = 10;
470+
bad_flags[8] = 2;
471+
REQUIRE_THROWS_AS(
472+
density_sketch<double>::deserialize(bad_flags.data(), bad_flags.size()),
473+
std::invalid_argument
474+
);
475+
std::stringstream is(
476+
std::string(reinterpret_cast<const char*>(bad_flags.data()), bad_flags.size()),
477+
std::ios::in | std::ios::out | std::ios::binary
478+
);
479+
REQUIRE_THROWS_AS(density_sketch<double>::deserialize(is), std::invalid_argument);
480+
}
481+
}
482+
274483
} /* namespace datasketches */

0 commit comments

Comments
 (0)