Skip to content

Fix HLL union estimate/serialization regression from lazy KxQ rebuild - #512

Merged
proost merged 3 commits into
apache:masterfrom
SavicStefan:hll_bug_fix
Aug 15, 2026
Merged

Fix HLL union estimate/serialization regression from lazy KxQ rebuild#512
proost merged 3 commits into
apache:masterfrom
SavicStefan:hll_bug_fix

Conversation

@SavicStefan

@SavicStefan SavicStefan commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Since the lazy KxQ/curMin rebuild was introduced ("skip updating kxq in HLL merge"), Hll8Array::mergeHll only sets rebuild_kxq_curmin_ and defers the recompute. Several consumers read that deferred state without honoring the flag, so unioning HLL sketches with different lgConfigK could:

  • discard accumulated data: isEmpty() reads curMin_/numAtCurMin_, which stay at empty-sketch defaults after a downsampling merge, so a populated gadget looks empty and the next union update overwrites it;
  • corrupt the estimate: internalCouponUpdate does incremental KxQ/HIP updates against the stale base;
  • serialize non-deterministically: copyAs() rebuilds via register replay (convertToHll8) while estimate/bounds rebuild via the direct sum, so equivalent merges could serialize to different bytes.

The effect was estimates collapsing to ~one input's cardinality (merge-order dependent) and merge-order-dependent serialization; 4.0.1 had neither.

Fix, keeping the lazy-merge optimization:

  • isEmpty(): a pending rebuild means the array is non-empty.
  • internalCouponUpdate(): rebuild before an incremental update reads KxQ, only when the coupon changes a register (duplicate coupons stay lazy).
  • copyAs(): make every same-type result use the direct-sum rebuild so all paths agree, keeping serialization merge-order independent.

Adds a regression tests covering estimate order-independence, is_empty() after a downsampling merge, scalar-after-merge accumulation, and merge-order-independent serialization.

SavicStefan and others added 2 commits August 13, 2026 18:15
Since the lazy KxQ/curMin rebuild was introduced ("skip updating kxq in HLL
merge"), Hll8Array::mergeHll only sets rebuild_kxq_curmin_ and defers the
recompute. Several consumers read that deferred state without honoring the
flag, so unioning HLL sketches with different lgConfigK could:

- discard accumulated data: isEmpty() reads curMin_/numAtCurMin_, which stay at
  empty-sketch defaults after a downsampling merge, so a populated gadget looks
  empty and the next union update overwrites it;
- corrupt the estimate: internalCouponUpdate does incremental KxQ/HIP updates
  against the stale base;
- serialize non-deterministically: copyAs() rebuilds via register replay
  (convertToHll8) while estimate/bounds rebuild via the direct sum, so equivalent
  merges could serialize to different bytes.

The effect was estimates collapsing to ~one input's cardinality (merge-order
dependent) and merge-order-dependent serialization; 4.0.1 had neither.

Fix, keeping the lazy-merge optimization:
- isEmpty(): a pending rebuild means the array is non-empty.
- internalCouponUpdate(): rebuild before an incremental update reads KxQ, only
  when the coupon changes a register (duplicate coupons stay lazy).
- copyAs(): make every same-type result use the direct-sum rebuild so all paths
  agree, keeping serialization merge-order independent.

Adds a regression test covering estimate order-independence, is_empty() after a
downsampling merge, scalar-after-merge accumulation, and merge-order-independent
serialization.

Co-authored-by: Isaac
Co-authored-by: Stefan Savić <stefan.savic@databricks.com>
Signed-off-by: Stefan Savić <stefan.savic@databricks.com>

@proost proost left a comment

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.

I agree on overall, but how about changing get_result in union? Like java one, rebuild before get_result.

And rebuilding in copy_as changes logic semantically.

@SavicStefan
SavicStefan requested a review from proost August 15, 2026 11:05

@proost proost left a comment

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.

Thank you!

@proost
proost merged commit 70b86fa into apache:master Aug 15, 2026
17 checks passed
UnamedRus added a commit to UnamedRus/ClickHouse that referenced this pull request Aug 31, 2026
`lg_k` and the target type configure the sketch but are held by the aggregate
function, not by its state: `HllSketchData` is two pointers whatever they are,
and a serialized sketch records its own `lg_k`. States of two parameterisations
are therefore interchangeable, which is what `haveSameStateRepresentationImpl`
reports. `quantile` and `sequenceMatch` override it for the same reason.

Three checks consult it, so one override covers all of them: the `-Merge`
combinator can now read a state built with one `lg_k` under a function declared
with another, `CAST` can relabel a column between the two types, and a state can
be inserted into a column declared with different parameters. Merging a state of
a higher `lg_k` under a lower one rescales it, which is the point: a 2094 byte
`lg_k` 12 state becomes 170 bytes at `lg_k` 8, with the same registers a sketch
built at 8 from the start would have.

The type itself is unchanged. `getNormalizedStateType` is deliberately not
overridden, so `AggregateFunction(uniqApacheHLL(8), UInt64)` and
`AggregateFunction(uniqApacheHLL, UInt64)` remain distinct types, and only the
parameters are interchangeable: the argument types and the function name are
still compared.

The policy opts in through `states_compatible_across_parameters` rather than the
template asserting it for every future sketch, since a policy whose parameters
change the layout of its state must not claim this.

Requires the fix from apache/datasketches-cpp#512 in
`contrib/datasketches-cpp`. Before it, unioning two or more dense sketches whose
`lg_k` exceeds the union's `lg_max_k` kept only the last one, so merging across
`lg_k` returned roughly the cardinality divided by the number of states. That is
fixed upstream but is not in any release: every version from 4.1.0 to 5.2.0 is
affected, and the pinned commit predates the fix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants