Repository navigation
Cherry pick #131595 to 26.8: Destroy the queued aggregate states when the merge of GROUP BY tables throws - #131897
Conversation
… 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
Resolve the five conflicts of the 26.8 cherry-pick of #131595. They are all regions where master has code that 26.8 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.8, and the resolution takes none of them. 26.8 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.8 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.8 assertion is kept (no isSelfMerge). AggregateFunctionTuple.cpp and the test merge cleanly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
The five conflicts are all master code that 26.8 does not have: the deferral of large merges (#117153), One addition: 26.8 has no On the 26.8 tree (clang-22, ASan) I cannot push to |
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-picklocally.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:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job