Repository navigation
Fix ReservoirSampler self-merging - #79031
Merged
Merged
Conversation
Contributor
nickitat
commented
Apr 11, 2025
serxa
approved these changes
Apr 11, 2025
|
|
||
| // There will be an aliasing issue if we merge the same object with itself. I.e. we will insert from `b.samples` into `a.samples`, | ||
| // but both refer to the same array. It might happen in case of multiplying an aggregate function state by a numeric constant. | ||
| // ATST, it seems that self-merging cannot improve accuracy, so there is no point to do it anyway. |
alexey-milovidov
approved these changes
Apr 11, 2025
Member
Author
|
|
Contributor
|
Hi @nickitat @serxa @alexey-milovidov — while reviewing this PR I found the following:
Happy to discuss — close anything that's wrong or already addressed. |
groeneai
added a commit
to groeneai/ClickHouse
that referenced
this pull request
Apr 26, 2026
This PR's `executeAggregateMultiply` rework copies the aggregate state into an independent column before each squaring step, so source and destination no longer alias on entry to `merge`. For aggregate functions whose `merge` reallocates and then reads the source (e.g. `quantilesExact`, `groupArray`, `groupUniqArray`, `windowFunnel`, `sequenceMatch`), this fixes the heap-use-after-free. `ReservoirSampler::merge` (used by `medianState` / `quantileState`) had been guarded since ClickHouse#79031 with an early-return when `this == &b`. That guard is unreachable from `executeAggregateMultiply` now: the squaring step never feeds the same state as both arguments. Squaring becomes a real merge instead of a no-op, so `state * N` for a ReservoirSampler- backed aggregate now actually accumulates `N` copies of the input rather than collapsing to a single copy. `03406_reservoir_sample_self_merging` was added in ClickHouse#79031 with reference values that match the no-op semantics: 10 * medianState(numbers(10)) -> 4.5 (100 inserts <= 8192, exact) 10 * medianState(numbers(1000)) -> 499.5 (was no-op; same as state) 10 * medianState(numbers(100000)) -> 50501.5 (already over reservoir) Under the corrected semantics the middle case crosses the 8192-sample reservoir cap (10 x 1000 = 10000 inserts) and the deterministic seed yields `495` for the median. The first and third cases are unchanged: the small one stays under the cap, the large one was already saturated. The test still verifies the absence of UAF / `LOGICAL_ERROR` on `state * N` for ReservoirSampler-backed aggregates - it's a regression test for ClickHouse#78299 - and only the boundary value moves. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=103543&sha=f7dd195af45870446b0c48654753c337438e90b2&name_0=PR
This was referenced Apr 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
todo
Closes: #78299