Repository navigation
Fix -ForEach self-merge assertion for zero-size nested states - #108781
alexey-milovidov merged 3 commits into
Conversation
An aggregate over Array(Nothing) (all-NULL arrays, e.g.
arrayReduce('uniqStateForEach', [[NULL], [NULL, NULL, NULL]])) collapses
the nested aggregate to AggregateFunctionNothing, whose state is zero
bytes. When AggregateFunctionForEach::ensureAggregateData grows the
per-element state array, the zero-byte arena allocation does not advance
the arena, so the new buffer aliases the old one. The old-state migration
loop then called nested merge() with new_state == old_state, tripping the
IAggregateFunction::merge self-aliasing assertion added in ClickHouse#103543 and
aborting debug/sanitizer builds (the AST fuzzer found this). In release
builds the assertion compiles out and the no-op self-merge was harmless.
Skip the migration loop when the nested state is zero-sized: there is no
data to migrate and merge() with aliasing source/destination is undefined.
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-3:20260628-235700 |
|
cc @rschu1ze @KochetovNicolai — could you review? Fixes the |
|
Workflow [PR], commit [5581d5a] Summary: ✅
AI ReviewSummaryThis PR fixes the zero-size nested-state self-merge assertion for Missing context / blind spots
Final VerdictStatus: ✅ Approve |
The previous guard only covered the old-state migration loop in ensureAggregateData. The same zero-size aliasing trips mergeImpl: when two distinct -ForEach states have a zero-byte nested state, every arena.alignedAlloc(0) returns the same arena position, so the per-element nested_func->merge(place, rhs) receives place == rhs and fires the IAggregateFunction::merge self-aliasing assertion. arrayReduceInRanges reproduces this once the input exceeds minimum_step (64): it pre-aggregates one -ForEach state per 64 rows and then merges those states. Skip the per-element merge loop in mergeImpl for zero-size nested states (the merge is a no-op for an aggregate over Nothing). Extend 04420 with > 128-element all-NULL arrayReduceInRanges cases that exercise the pre-aggregation merge path. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-2:20260629-051700 |
AggregateFunctionMap stores each key's nested state in a separate arena slot. When the nested state is zero-byte (e.g. uniq over Nullable(Nothing), which collapses to AggregateFunctionNothing with sizeOfData()==0), every arena.alignedAlloc(0) returns the same arena position without advancing, so a key shared between two states has its nested_place aliasing the other state's nested place. mergeImpl then called nested_func->merge(place, place), tripping the IAggregateFunction::merge self-aliasing assertion. Skip only the per-key nested merge for zero-size nested states (the nested Nothing::mergeImpl is a no-op, so this is behavior-preserving) while still unioning the key sets. This is the -Map sibling of the -ForEach fix. Co-Authored-By: Claude Opus 4.8 <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 20/21 (95.24%) | Lost baseline coverage: none · Uncovered code |
…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
…reach-self-merge Fix -ForEach self-merge assertion for zero-size nested states
Related: #103543
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
An aggregate over
Array(Nothing)(all-NULL arrays) collapses the nested aggregate toAggregateFunctionNothing, whose state is zero bytes. WhenAggregateFunctionForEach::ensureAggregateDatagrows the per-element state array,arena.alignedAlloc(0)does not advance the arena, so the freshly "allocated" buffer aliases the old one. The old-state migration loop then called the nestedmerge()withnew_state == old_state, tripping theIAggregateFunction::mergeself-aliasing assertion added in #103543 and aborting debug/sanitizer builds. In release builds the assertion compiles out and the no-op self-merge was harmless, so there is no user-visible misbehavior.Fix: skip the migration merge loop when the nested state is zero-sized. There is no data to migrate, and
merge()with aliasing source/destination is undefined.Found by the AST fuzzer:
SELECT arrayReduce('uniqStateForEach', [[NULL], [NULL, NULL, NULL]]).CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108436&sha=ee9a2c24d179f38af6886ee315c4dee833626425&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29
The added test
04420_foreach_combinator_zero_size_nested_statecovers the zero-size case foruniqStateForEach,groupArrayStateForEach,arrayReduceInRanges, anduniqForEach, and also asserts that non-zero-size-ForEachaggregates (sumForEach,uniqForEach,groupArrayForEach) still migrate growing states correctly.Version info
26.7.1.259(included in26.7and later)