Skip to content

Bump datasketches-cpp to 5.2.0 and backport the HyperLogLog union fix - #117422

Merged
alexey-milovidov merged 6 commits into
masterfrom
datasketches-5.2.0
Sep 7, 2026
Merged

alexey-milovidov merged 6 commits into
masterfrom
datasketches-5.2.0

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

Closes: #116587
Related: #116642
Related: #95045

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed uniqTheta and its variants silently returning a value that is too low when reading an AggregateFunction(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 master instead of a ClickHouse/ branch of our fork, and upstream master is a moving development target rather than a release. Both are addressed here.

contrib/datasketches-cpp tracked apache/datasketches-cpp directly, pinned to 76edd74f (2024-05-16), an upstream development commit. It now tracks our fork on a ClickHouse/-prefixed branch, the way the contrib guide asks:

repository 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, 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 70b86faa onto the 5.2.0 tag, 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 in 5.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 upstream hll_test (51 test cases, 7635 assertions) 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).

The bug the version move fixes

Moving the pin from 76edd74f to 5.2.0 does fix a live bug, which is why this is a Bug Fix and not a plain version bump. 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. This is not undefined behaviour, which is why UBSan never flagged it.
  • 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 and a version-4 sketch is ordered, 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 same bump also fixes two write-side bugs in that format (pack_bits_13 and a missing final partial byte); no ClickHouse code path reaches those today.

Testing

05099_uniq_theta_compressed_state reads 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 with serialize_compressed, which reproduces the input bytes exactly, so each blob is what a correct implementation writes for that sketch.

33-bit state 35-bit state union
76edd74f (before) 7 14 19
ClickHouse/5.2.0 (after) 16 16 30

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, statistics and ip_types_aggregate were run twice on the same machine, once against this build and once against a master build, on two servers. The two FAIL sets are identical except that 05099 is red only on master. All six uniqTheta tests pass on both; the 16 shared failures are limits of a bare local server (no system.query_log, no test clusters, no ZooKeeper, no test.hits, /var/lib/clickhouse/user_files), not regressions. The upstream theta_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 the bit_packing.hpp and theta_sketch_impl.hpp fixes above.

  • count/include is untouched, so the persisted countmin statistics cannot move.
  • The uncompressed Theta serialization that ClickHouse writes is untouched: 359,456 bytes of serialized state over 22 states — 11 cardinalities (0, 1, 2, 7, 8, 100, 4095, 4096, 4097, 10000, 1000000), each written both directly and through theta_union — are byte-identical before and after (md5 9d64a0a1).
  • common/include/xxhash64.h is new on the include path. Nothing in the compiled directories includes it (it belongs to the uncompiled bloom filter), there is no other xxhash64.h in the tree, and nothing in ClickHouse includes that name.
  • The remaining two hunks are a new random_utils::next_uint64 distribution in common_defs.hpp and dead-branch removal in quantiles_sorted_view_impl.hpp.

Post-5.2.0 upstream 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 the ClickHouse/5.2.0 branch later if a need appears — which is the point of tracking a release branch rather than master.


Workflow [PR]
Sync PR [sync-upstream/pr/117422]

Version info

  • Merged into: 26.9.1.880 (included in 26.9 and later)

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.
@clickhouse-gh

clickhouse-gh Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [87dc669]

Summary: ✅


AI Review

Summary

This PR moves contrib/datasketches-cpp from an upstream development commit to the ClickHouse fork’s 5.2.0 line, which brings in the theta compressed-state decode fixes the new 05104_uniq_theta_compressed_state regression test is targeting. I reviewed the current PR discussion, the top-level ClickHouse diff, and the vendored header delta on the actual old and new submodule commits; I did not find an actionable correctness, compatibility, or maintenance issue that merits an inline review comment.

Final Verdict

✅ No findings.

LLVM Coverage Report

Measured on commit 87dc669.

Metric Baseline Current Δ
Lines 88.80% 88.80% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.20% 81.10% -0.10%

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

Full report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default submodule changed At least one submodule changed in this PR. labels Aug 31, 2026
@thevar1able thevar1able self-assigned this Aug 31, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 87dc66910 with master b2bb5c518 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

⚠️ Significant changes: compile time of recompiled translation units.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units ⚠️

2431 translation units recompiled, 2811 s compile time in total, 14 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×1.08 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +5.2 s (+11%) in total before that adjustment.

Translation units without a recent master baseline:

  • contrib/arrow-cmake/__/arrow/cpp/src/arrow/compute/kernels/scalar_temporal_unary.cc: 23.4 s
  • contrib/grpc-cmake/__/grpc/src/core/ext/transport/chttp2/transport/chttp2_transport.cc: 12.2 s
  • contrib/llvm-project-cmake/__/llvm-project/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp: 11.3 s
  • contrib/llvm-project-cmake/__/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp: 10.5 s
  • contrib/grpc-cmake/__/grpc/src/core/server/server.cc: 10.4 s
  • contrib/google-protobuf-cmake/__/google-protobuf/src/google/protobuf/descriptor.cc: 10.1 s
  • contrib/grpc-cmake/__/grpc/src/core/load_balancing/rls/rls.cc: 9.6 s
  • contrib/llvm-project-cmake/__/llvm-project/llvm/lib/Passes/PassBuilderPipelines.cpp: 9.5 s
  • contrib/grpc-cmake/__/grpc/src/core/client_channel/retry_interceptor.cc: 9.5 s
  • contrib/grpc-cmake/__/grpc/src/core/call/server_call.cc: 9.4 s
  • contrib/llvm-project-cmake/__/llvm-project/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp: 9.3 s
  • contrib/grpc-cmake/__/grpc/src/core/ext/transport/chttp2/client/chttp2_connector.cc: 9.1 s
  • contrib/grpc-cmake/__/grpc/src/core/ext/transport/chttp2/transport/frame_rst_stream.cc: 8.9 s
  • contrib/grpc-cmake/__/grpc/src/core/lib/channel/promise_based_filter.cc: 8.8 s
  • contrib/grpc-cmake/__/grpc/src/core/xds/grpc/xds_cluster_parser.cc: 8.8 s
  • contrib/llvm-project-cmake/__/llvm-project/llvm/lib/Transforms/IPO/SampleProfile.cpp: 8.8 s
  • contrib/grpc-cmake/__/grpc/src/core/xds/grpc/xds_http_gcp_authn_filter.cc: 8.8 s
  • contrib/google-protobuf-cmake/__/google-protobuf/src/google/protobuf/compiler/cpp/message.cc: 8.8 s
  • contrib/grpc-cmake/__/grpc/src/core/ext/transport/chttp2/transport/frame_settings.cc: 8.7 s
  • contrib/llvm-project-cmake/__/llvm-project/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp: 8.7 s
  • ... and 45 more

Job report

@clickhouse-gh clickhouse-gh Bot added the comp-external-dependencies Third-party deps updates in contrib/, vendored code, and platform base libraries. label Sep 4, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 The two reds in Stateless tests (arm_asan_ubsan, azure, sequential) — 04648_distributed_plan_task_error_propagation and the collateral 04340_distributed_plan_status_reenqueue_fault — are a fleet-wide master failure since 2026-09-06 (13–19 failures across many PRs and master itself in CIDB), unrelated to the datasketches-cpp bump. Fixes are already in progress: #118390 and #118417. The 04035_alp_rd_codec_special_values red was fixed on master by #118335; merged master to pull it in.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 7, 2026
Merged via the queue into master with commit e568d26 Sep 7, 2026
175 checks passed
@alexey-milovidov
alexey-milovidov deleted the datasketches-5.2.0 branch September 7, 2026 00:54
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-external-dependencies Third-party deps updates in contrib/, vendored code, and platform base libraries. pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo submodule changed At least one submodule changed in this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

apache datasketches version bump (bug with HLL)

3 participants