Repository navigation
Do not alias pre-aggregation states of a zero-size aggregate in arrayReduceInRanges - #115008
Conversation
…ReduceInRanges An aggregate over only-NULL arguments collapses to AggregateFunctionNothing, whose sizeOfData() is 0. Arena::alignedAlloc advances the arena by size, so every zero-byte allocation returns the same address. arrayReduceInRanges allocates one pre-aggregation state per 64 rows plus one state per range from a single arena, so with a zero-size aggregate all of those states alias and both the pairwise level merge and the per-range merge call merge(place, place), tripping the assertion added in ClickHouse#103543. Allocate max(sizeOfData(), 1) bytes for the pre-aggregation states. They then have distinct addresses, every merge still runs, and for an aggregate with a non-empty state the allocation is byte-identical to before. Only the pre-aggregation allocation needs padding. The per-range merge runs only when the range spans a whole 64-row block, which is also the condition for at least one pre-aggregation state to exist, and that state's one-byte allocation has already advanced the arena. Both merges remain reachable, at 64 elements for the per-range one and at 128 for the pairwise level loop, and the test covers each. Merging is not suppressed for zero-size states. sizeOfData() == 0 does not imply that a merge is a no-op: StatelessWindowFunction is also zero-size and its mergeImpl throws. Since the aggregate here is caller-supplied, there is no way to tell a genuinely empty state from one whose merge enforces a contract, so skipping the call could drop that enforcement. The -ForEach guard in ClickHouse#108781 is not a counter-example: it was written for AggregateFunctionNothing, whose merge is empty, and it keys on state size alone, so it would skip a throwing zero-size merge too. Padding the allocation avoids that question. The test pins that with an empty range list: the per-range loop does not run, so the pairwise level merge over the pre-aggregation places is the only remaining path to the aggregate's merge, and a build that skips zero-size merges returns an empty array instead of reporting the error. Found by the AST fuzzer (amd_debug, targeted), STID 2508-5153: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=114747&sha=95a67283e1b33c000dba21cddefcd4418c4a2c94&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29 Carrier: ClickHouse#114747 Related: ClickHouse#108781
Internal second-model review (4 rounds, all findings resolved)An independent model reviewed every revision before publication and I adjudicated each finding
A position I got wrong and reversed: I twice rejected the finding that the per-range allocation Also noticed, not raised: those |
|
Workflow [PR], commit [51495e4] Summary: ❌
AI ReviewSummaryThis PR fixes Final Verdict✅ No blocking issues found. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 9/9 (100.00%) · Uncovered code |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20260816-115800 |
|
cc @george-larionov @azat, could you review this? An all-NULL argument collapses the aggregate to AggregateFunctionNothing whose state is zero bytes, and Arena::alignedAlloc advances by size, so every pre-aggregation state in arrayReduceInRanges returned the same address and both merges became merge(p, p); padding the allocation to max(sizeOfData(), 1) keeps the states distinct without suppressing any merge. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units7 translation units recompiled, 7 s compile time in total, 7 of them have a recent master baseline. |
CI finish ledger - 51495e4CI is fully finished on this head: 177 check-runs, 156 success / 20 skipped / 1 failed, 0 still
The The two The regression test Session id: cron:our-pr-ci-monitor:20260817-020000 |
b70af22
Related: #108781
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
arrayReduceInRangesover an array whose elements are all NULL aborts a debug or sanitizer buildwith
Logical error: 'IAggregateFunction::merge called with the same source and destination state':Found by the AST fuzzer:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=114747&sha=95a67283e1b33c000dba21cddefcd4418c4a2c94&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29
An all-NULL argument collapses the aggregate to
AggregateFunctionNothing, whosesizeOfData()is0.
Arena::alignedAllocadvances the arena bysize, so every zero-byte allocation returns the sameaddress. The function allocates one pre-aggregation state per 64 rows plus one state per range from a
single arena, so all of those states alias and both the pairwise level merge and the per-range merge
become
merge(p, p).The fix allocates
max(sizeOfData(), 1)bytes for the pre-aggregation states, so they are distinctand every merge still runs. The per-range state needs no padding: that merge only runs when the range
spans a whole 64-row block, which is also the condition for a pre-aggregation state to exist, and its
one-byte allocation has already advanced the arena. For an aggregate with a non-empty state the
allocation is byte-identical to before.
I deliberately did not suppress the merge for zero-size states.
sizeOfData() == 0does not implythat merging is a no-op:
StatelessWindowFunctionis also zero-size and itsmergeImplthrows.Since the aggregate is caller-supplied, a genuinely empty state cannot be told apart from one whose
merge enforces a contract, so skipping the call could drop that enforcement.
Two further places with the same allocation pattern are left alone.
ColumnAggregateFunctioncanhold zero-size states (the test builds one via
nothingState) but itsinsertMergeFrommergesacross two different arenas. The
Aggregatormerge paths have one reported sighting of the siblingassertion (
mergeAndDestroyBatch, on #111459) that 15 probes did not reproduce.Release behaviour is unchanged - the assertion compiles out and
Nothing::mergeImplis empty.