Skip to content

Fix ReservoirSampler self-merging - #79031

Merged
nickitat merged 2 commits into
masterfrom
fix_reservoir
Apr 11, 2025
Merged

nickitat merged 2 commits into
masterfrom
fix_reservoir

Conversation

@nickitat

@nickitat nickitat commented Apr 11, 2025 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

todo

Closes: #78299

@clickhouse-gh

clickhouse-gh Bot commented Apr 11, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a019fb1]

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Apr 11, 2025
@serxa serxa self-assigned this 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.

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.

What is ATST?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at the same time )

@nickitat

Copy link
Copy Markdown
Member Author

Integration tests (tsan, 4/6) - test_storage_rabbitmq
Stress test (amd_tsan) - #64936

@nickitat
nickitat added this pull request to the merge queue Apr 11, 2025
Merged via the queue into master with commit c3e2337 Apr 11, 2025
@nickitat
nickitat deleted the fix_reservoir branch April 11, 2025 22:15
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 11, 2025
@clickgapai

Copy link
Copy Markdown
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ASAN heap-use-after-free on ReservoirSampler

5 participants