Skip to content

Cherry pick #131595 to 26.9: Destroy the queued aggregate states when the merge of GROUP BY tables throws - #131898

Merged
robot-clickhouse merged 4 commits into
backport/26.9/131595from
cherrypick/26.9/131595
Nov 17, 2026
Merged

robot-clickhouse merged 4 commits into
backport/26.9/131595from
cherrypick/26.9/131595

Conversation

@robot-clickhouse-ci-1

Copy link
Copy Markdown
Contributor

Original pull-request #131595

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 3 commits November 14, 2026 11:14
… throws

The map `mergeDataImpl` (serial single-level merge, two-level bucket merge,
parallel fixed hash map merge) and the non-indexed loop of
`mergeSingleLevelPartitionsImpl` take the state of a key found in both tables
out of the source table (`src = nullptr`) and queue it in a local
`PaddedPODArray` for `mergeAndDestroyDetachedBatch`. Until that call nothing
owns the queued states: a throw in between, e.g. `MEMORY_LIMIT_EXCEEDED` from
the destination `HashTable::resize`, leaked their heap buffers, since the
catches destroyed only the deferred pairs. #117153 made the window after the
batch starts exception-safe; this closes the collection window before it.

Extract the cleanup loop of `mergeAndDestroyDetachedBatch` into
`destroyDetachedSources` and call it from a catch around the collection of
`mergeDataImpl` (the JIT merge and the batch call stay outside the try, the
batch owns the states from there) and from the existing catch of the
partition merge. `flush_merges` clears its arrays in a `SCOPE_EXIT`, because
the batch takes the states also when it throws, so a state is never destroyed
twice. Deferred pairs come only from batches that started and the queued
states only from batches that did not, so the two cleanups are disjoint.

The new ONCE failpoint `aggregator_merge_collect_states_throw` throws at the
end of a collection while states are queued. The test runs it on six paths in
`clickhouse local`, whose LeakSanitizer check at exit aborts on a leak.

`IAggregateFunctionHelper::mergeAndDestroyBatch` and the `-Tuple` combinator
also destroy the rest of their batch when a merge throws, as
`mergeAndDestroyBatchOrDefer` does; the second ONCE failpoint
`aggregator_merge_batch_throw` tests a throw inside the batch in both merges.

Related: #131315 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…the batch

`destroyDetachedSources` and the catches of
`IAggregateFunctionHelper::mergeAndDestroyBatch` and of the `-Tuple`
combinator destroy their states with `destroyBatch`.

`aggregator_merge_batch_throw` now throws inside
`IAggregateFunctionHelper::mergeAndDestroyBatch` before the merge of its
second row (`IAggregateFunction::injectMergeBatchFault`, out of the header),
so the catches of the function and of `-Tuple` run in the test, which gains a
`quantileExactTuple` case. Before, it threw after the batch of a function had
returned.

The NULL key case has NULL keys and pins the non-adaptive partition merge,
because the adaptive aggregator merges by cell index and never queues states.
The test routes the sanitizer report of each `clickhouse local` to its own log
and counts the leak reports, so one reference fits every build, and runs the
cases in two processes.

Comments: `mergeAndDestroyDetachedBatch` owns the batch from its call on.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ued-states

Destroy the queued aggregate states when the merge of GROUP BY tables throws
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Nov 16, 2026
Resolve the five conflicts of the 26.9 cherry-pick of #131595. They are
all regions where master has code that 26.9 does not have: the deferral
of large merges (#117153: mergeAndDestroyBatchOrDefer, DeferredMerges,
mergeAndDestroyDetachedBatch and its catch), isSelfMerge (#130678), the
bound of zero-sized nested states (#130120), and the partition-by-partition merge of
the adaptive aggregator (#129565). None of them is labelled for 26.9, and
the resolution takes none of them.

26.9 has no catch around the per-function loop of the batch merge (master
got it in #117153), so a throw in the merge of one function would still
leak the queued states of every function after it. The resolution adds
a 26.9 mergeAndDestroyDetachedBatch, the master function without the
deferral: the per-function loop, and a catch that destroys the states of
the following functions. The map mergeDataImpl (also the JIT path) and
the flush of mergeSingleLevelPartitionImpl call it. The collection loop
of mergeSingleLevelPartitionImpl is wrapped in the try of #131595, so it
is re-indented.

In IAggregateFunctionHelper::mergeAndDestroyBatch the 26.9 assertion is
kept (no isSelfMerge). AggregateFunctionTuple.cpp and the test merge
cleanly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator

The conflicts and the resolution are the same as on 26.8 (#131897 (comment)): the five conflicts are master code that 26.9 does not have (#117153, #130678, #130120, #129565, none labelled for 26.9), so I kept the 26.9 side and added only the lines of #131595. The changed lines against backport/26.9/131595 are identical to the 26.8 ones, including the 26.9 mergeAndDestroyDetachedBatch (the master function without the deferral), which supplies the batch-loop catch that 26.9 lacks.

On the 26.9 tree (clang-22, ASan) 05638_aggregator_merge_exception_destroys_queued_states passes 50 of 50, and without the collection and batch-loop cleanup it reports a leak in both of its processes. The max_memory_usage throw inside the batch merge of mergeDataImpl leaves no leak, and ten GROUP BY queries over the changed merge paths return the same results as official 26.9.25.15.

I cannot push to cherrypick/26.9/131595. The branch on my fork has this PR's head 0c3ba30dd136 as an ancestor, so applying it is a fast-forward:

git fetch https://github.com/groeneai/ClickHouse.git resolve-26.9-cherrypick-131595
git push origin FETCH_HEAD:cherrypick/26.9/131595

@robot-clickhouse
robot-clickhouse merged commit 9ebca61 into backport/26.9/131595 Nov 17, 2026
2 checks passed
@robot-clickhouse
robot-clickhouse deleted the cherrypick/26.9/131595 branch November 17, 2026 03:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants