Repository navigation
Bump datasketches-cpp to 5.2.0 and backport the HyperLogLog union fix - #117422
Conversation
The submodule tracked `apache/datasketches-cpp` directly and was pinned to
`76edd74f` (2024-05-16), an upstream development commit. Move it onto a
`ClickHouse/`-prefixed branch of our fork, the way `docs/development/contrib`
asks, so that our patches stay isolated from upstream development:
ClickHouse/datasketches-cpp, branch ClickHouse/5.2.0
de8553ba 5.2.0, the newest upstream release (2025-01-15)
23bd9b07 backport of apache/datasketches-cpp#512
That fork had been archived in 2024 and is unarchived again.
apache/datasketches-cpp#512 is the HyperLogLog union fix asked for in #116587.
No upstream release carries it, so it is cherry-picked onto the `5.2.0` tag,
where it applies without conflicts. It is live on `5.2.0`: the four regression
tests that come with it fail on the plain tag and pass on the branch, and the
whole upstream `hll_test` (51 test cases) passes with the backport.
The fix changes nothing that ClickHouse compiles today, because
`contrib/datasketches-cpp-cmake/CMakeLists.txt` exposes only `common/include`,
`count/include` and `theta/include`. It is in the tree so that it is already
in place when HyperLogLog support lands (#95045).
Moving the pin to `5.2.0` does fix a live bug.
`theta/include/bit_packing.hpp` mis-decoded the compressed (serialization
version 4) Theta sketch format in two places:
* `unpack_bits_33` did `values[7] |= *ptr++ << 24` on a `uint8_t`. Integer
promotion makes that an `int` shift, so a byte >= 0x80 becomes a negative
`int` and sign-extends into the delta's top 32 bits.
* `unpack_bits_35` masked a two-bit field with 2 instead of 3, dropping bit
33 of the third delta in each block of eight.
`ThetaSketchData::read` calls `compact_theta_sketch::deserialize`, which routes
every state whose entry width is not 64 bits through those two routines.
Deltas are a prefix sum, so one bad delta corrupts every later entry, and
`theta_union::update` then either stops early, because the entry exceeds theta,
or deduplicates, because it collides with an earlier entry. Either way
`uniqTheta` returns a value that is too low, with no error. ClickHouse always
writes the uncompressed form itself, so such a state has to come from another
Apache DataSketches implementation.
The new test reads one state per routine and their union: 16, 16 and 30 with
the bump, 7, 14 and 19 without it.
The drift over the three compiled directories is five headers. `count/include`
is untouched, so the `countmin` statistics cannot move, and the uncompressed
Theta serialization ClickHouse writes is untouched as well: 719142 bytes of
state hex over 11 cardinalities are identical before and after.
|
Workflow [PR], commit [87dc669] Summary: ✅
AI ReviewSummaryThis PR moves Final Verdict✅ No findings. LLVM Coverage ReportMeasured on commit 87dc669.
Changed lines: Uncovered code analysis did not run: No coverable C/C++ source files changed (contrib/ is excluded from coverage). Newly covered: +920 lines in 238 files (-984 lines lost coverage) · Details |
Build profile diff (arm_release)Comparing Binary sizes
The official master build is compiled with Compile time of recompiled translation units
|
…ber collisions with master
…ber collisions with master
|
🕵 The two reds in |
Closes: #116587
Related: #116642
Related: #95045
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
uniqThetaand its variants silently returning a value that is too low when reading anAggregateFunction(uniqTheta, ...)state that another Apache DataSketches implementation had written in the compressed Theta sketch form.Description
Redo of #116642, which @rschu1ze closed for two reasons: it followed upstream
masterinstead of aClickHouse/branch of our fork, and upstreammasteris a moving development target rather than a release. Both are addressed here.contrib/datasketches-cpptrackedapache/datasketches-cppdirectly, pinned to76edd74f(2024-05-16), an upstream development commit. It now tracks our fork on aClickHouse/-prefixed branch, the way the contrib guide asks:ClickHouse/datasketches-cppClickHouse/5.2.0de8553ba5.2.0, the newest upstream release (2025-01-15)23bd9b07That fork had been archived in 2024, which is why it had no
ClickHouse/branch; it is unarchived again.The backport
apache/datasketches-cpp#512 is the Apache DataSketches HyperLogLog union fix that @UnamedRus asked for in #116587. No upstream release carries it, so it is cherry-picked (squashed) from its upstream merge commit
70b86faaonto the5.2.0tag, where it applies without conflicts.It is live on
5.2.0, not a no-op there: the lazy KxQ/curMin rebuild that the fix repairs was already released in5.2.0. The four regression tests that come with the fix all fail on the plain tag and pass on the branch, and the whole upstreamhll_test(51 test cases, 7635 assertions) passes with the backport.The fix changes nothing that ClickHouse compiles today, because
contrib/datasketches-cpp-cmake/CMakeLists.txtexposes onlycommon/include,count/includeandtheta/include. It is in the tree so that it is already in place when HyperLogLog support lands (#95045).The bug the version move fixes
Moving the pin from
76edd74fto5.2.0does fix a live bug, which is why this is aBug Fixand not a plain version bump.theta/include/bit_packing.hppmis-decoded the compressed (serialization version 4) Theta sketch format in two places:unpack_bits_33didvalues[7] |= *ptr++ << 24on auint8_t. Integer promotion makes that anintshift, so a byte >=0x80becomes a negativeintand sign-extends into the delta's top 32 bits. This is not undefined behaviour, which is why UBSan never flagged it.unpack_bits_35masked a two-bit field with2instead of3, dropping bit 33 of the third delta in each block of eight.ThetaSketchData::readcallscompact_theta_sketch::deserialize, which routes every state whose entry width is not 64 bits through those two routines. Deltas are a prefix sum, so one bad delta corrupts every later entry, andtheta_union::updatethen either stops early, because the entry exceeds theta and a version-4 sketch is ordered, or deduplicates, because it collides with an earlier entry. Either wayuniqThetareturns a value that is too low, with no error. ClickHouse always writes the uncompressed form itself, so such a state has to come from another Apache DataSketches implementation.The same bump also fixes two write-side bugs in that format (
pack_bits_13and a missing final partial byte); no ClickHouse code path reaches those today.Testing
05099_uniq_theta_compressed_statereads one canonical compressed state per routine plus their union. The two states are the ones from #116642; I re-derived them independently by decoding them against both header sets and by re-serializing each decoded sketch withserialize_compressed, which reproduces the input bytes exactly, so each blob is what a correct implementation writes for that sketch.76edd74f(before)ClickHouse/5.2.0(after)The two arms fail through different mechanisms — union early-stop truncation and hash-table deduplication — so neither is a weaker copy of the other.
For regressions, the 87 stateless tests matching
uniq_theta,uniqTheta,statisticsandip_types_aggregatewere run twice on the same machine, once against this build and once against amasterbuild, on two servers. The two FAIL sets are identical except that05099is red only onmaster. All sixuniqThetatests pass on both; the 16 shared failures are limits of a bare local server (nosystem.query_log, no test clusters, no ZooKeeper, notest.hits,/var/lib/clickhouse/user_files), not regressions. The upstreamtheta_test(81 test cases, 10165896 assertions) also passes on the new branch.Compatibility
The review surface is what is on the include path,
git diff 76edd74f..de8553ba -- common/include count/include theta/include: five headers, two of them thebit_packing.hppandtheta_sketch_impl.hppfixes above.count/includeis untouched, so the persistedcountminstatistics cannot move.theta_union— are byte-identical before and after (md59d64a0a1).common/include/xxhash64.his new on the include path. Nothing in the compiled directories includes it (it belongs to the uncompiled bloom filter), there is no otherxxhash64.hin the tree, and nothing in ClickHouse includes that name.random_utils::next_uint64distribution incommon_defs.hppand dead-branch removal inquantiles_sorted_view_impl.hpp.Post-
5.2.0upstream work in those three directories (9 more headers) is deliberately left out: it is unreleased refactoring plus fixes that nothing here needs. Individual ones can be cherry-picked onto theClickHouse/5.2.0branch later if a need appears — which is the point of tracking a release branch rather thanmaster.Workflow [PR]
Sync PR [sync-upstream/pr/117422]
Version info
26.9.1.880(included in26.9and later)