Repository navigation
Exempt zero-size aggregate states from the self-merge assertion - #130678
alexey-milovidov merged 1 commit into
Conversation
An aggregate over only-NULL arguments collapses to `nothing`, whose state has `sizeOfData() == 0`. A zero-byte arena allocation does not advance the arena, so distinct zero-size states allocated back to back share one address. Copying such a state, e.g. the same-frame copy of both window strategies (`AggregateWindowFunctionPrefix`, `AggregateWindowFunctionRecalculate`) through `ColumnAggregateFunction::insertFromWithOwnership`, which is `insertDefault()` followed by `merge(new, previous)`, then trips `chassert(place != rhs)` although the two states are distinct and have no bytes for `mergeImpl` to read or write. Every CI firing of this assertion family since it was added (ClickHouse#103543) is such a zero-size state. The window site is reached for `-State` results since ClickHouse#129257 and, before that, for `any`/`groupArray` over a state column. Refine the predicate once (`IAggregateFunction::isSelfMerge`) and use it at all six assertion sites instead of patching call sites one by one. `mergeImpl` is still called, non-empty states are checked as before, and release builds are unaffected (`chassert` is compiled out there). STID: 2508-3fb0 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding) plus my own review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261111-030800 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261111-022600 |
|
Workflow [PR], commit [fd71959] Summary: ✅
AI ReviewSummaryPR #130678 allows distinct zero-size aggregate states to share an address without triggering the self-merge assertion, while preserving the assertion for non-empty states. Reviewed commit Final VerdictStatus: ✅ Approve. No inline findings to post. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units334 translation units recompiled, 2696 s compile time in total, 334 of them have a recent master baseline. |
|
@Michicosun Could you please review. This has hit multiple PR CI fuzzer runs already. Also, please look at all the followups associated to #129257 |
dd27997
Resolve the five conflicts of the 26.8 cherry-pick of ClickHouse#131595. They are all regions where master has code that 26.8 does not have: the deferral of large merges (ClickHouse#117153: mergeAndDestroyBatchOrDefer, DeferredMerges, mergeAndDestroyDetachedBatch and its catch), isSelfMerge (ClickHouse#130678), the bound of zero-sized nested states (ClickHouse#130120), and the partition-by-partition merge of the adaptive aggregator (ClickHouse#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 ClickHouse#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 ClickHouse#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]>
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]>
Resolve the five conflicts of the 26.10 cherry-pick of #131595. They are all regions where master has code that 26.10 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.10, and the resolution takes none of them. 26.10 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.10 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.10 assertion is kept (no isSelfMerge). AggregateFunctionTuple.cpp and the test merge cleanly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #129257
Related: #103543
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Debug and sanitizer builds no longer abort with
IAggregateFunction::merge called with the same source and destination statewhen a window function copies a zero-size aggregate state.Description
CI:
AST fuzzer (amd_debug),Logical error: 'IAggregateFunction::merge called with the same source and destination state' (STID: 2508-3fb0), https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=104297&sha=88cddaf5fd5438d960957b64ba02fc174ee203b2&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29 (reproduces on master without that PR).An aggregate over only-NULL arguments becomes
nothing, whose state hassizeOfData() == 0, e.g.uniqArrayState([number % NULL]). A zero-byte arena allocation does not advance the arena, so distinct zero-size states share one address. Both window strategies copy the previous row's result for an unchanged frame withinsertRangeFrom, which forColumnAggregateFunctionisinsertDefault()plusmerge(new, previous), sochassert(place != rhs)fires although the states have no bytes to alias. #129257 extended that copy to-Stateresults;any/groupArrayover a state column reached it before.Since the assertion landed (#103543), all 12 CI rows of this message family (8 STIDs) had an only-NULL argument, i.e. a zero-size state, and several call sites were already patched one by one. Instead of patching another one, the predicate is refined once:
IAggregateFunction::isSelfMerge(place == rhs && sizeOfData() != 0) is used at all six assertion sites. OnlyAggregateFunctionNothingImpl(empty merge) and theWindowFunctionplaceholders (merge throws) report a zero size; every combinator forwards the nested size or adds a non-zero prefix, so no exempted merge touches state bytes. Non-empty states are checked as before,mergeImplis still called, and release builds are unchanged.Validation: the new test aborts the unfixed debug build at
IAggregateFunction.h:263and passes with the fix, 50/50 randomized runs; reverting only themergesite makes it abort again.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/130678]
Version info
26.11.1.2042-master(included in26.11and later)