Repository navigation
Do not copy a constant aggregate function argument to the block size without GROUP BY - #131185
Conversation
Without keys, `Aggregator::prepareAggregateInstructions` materialized every
constant argument to the size of the block. For
`COUNTOrDefaultDistinct(toFixedString('', 1048577))` with
`max_block_size = 65409` that is a 64 GiB request in each
`AggregatingTransform` thread: the AST fuzzer run on PR ClickHouse#99639 had 16 of
them, and the server was OOM-killed at 44 GB RSS.
A function whose state ignores repeated values (`ignoresRepeatedValues()`,
added by ClickHouse#127940) gets the same state from one row as from every row of a
constant. So, without keys, a single constant argument of such a function is
now passed as its one-row data column and added once per block, the way
ClickHouse#127940 adds the used values of a `LowCardinality` argument and the radix
partitioned `uniqExact` routes a constant once per block.
`-Distinct` forwards to the nested function only the values it has not seen,
so it ignores repeated values whatever the nested function is. It now reports
it, which also gives it the `LowCardinality` path without keys.
The JIT branch of `executeWithoutKeyImpl` is not affected: its only caller
passes `use_compiled_functions = false`.
CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=99639&sha=00b62ef53d2811d1f363572394dbf8b9bb9ab2d7&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The previous last query of 05628 compared a LowCardinality column with its plain copy, which gives the same result on master, so it could not show that -Distinct now reports ignoresRepeatedValues() and takes the LowCardinality path of ClickHouse#127940. The new query runs -Distinct functions over a materialized LowCardinality(FixedString(100000)) column under a 100 MB query limit: master converts it to a full column (an 8 GiB request, MEMORY_LIMIT_EXCEEDED), the fix adds its one used value. 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; 2 findings over 1 full pass + 1 delta recheck, the recheck found none).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20261113-021501 |
|
Workflow [PR], commit [cc1a69d] Summary: ✅
AI ReviewSummaryReviewed #131185 at Consumers traced for the shortened arguments: Missing context / blind spots
Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units92 translation units recompiled, 808 s compile time in total, 92 of them have a recent master baseline. |
The previous commits avoided the copy of a constant argument to the size of the block only for functions that ignore repeated values. Every other function still received the full copy, although all its rows are equal. The AST fuzzer runs on PR ClickHouse#131697 hit exactly that case: `anyOrDefault(toFixedString(<const>, 1048576))` and `count(toFixedString(<const>, 1048576))` without keys asked for 64 GiB and 8 GiB per `AggregatingTransform` thread, and the server was OOM-killed. A constant of 1 MiB is not folded by the analyzer, so `count` keeps its argument and the copy happens. Now, without keys, when all arguments of a function are constants, they are copied to at most `max(1, 1 MiB / bytes of one row)` rows (one row for a function that ignores repeated values), and that copy is added repeatedly through `Aggregator::addBatchSinglePlace` until the rows of the block are covered. The function sees the same values in the same order through the same batch API, so every function and combinator is covered (`count`, `any`, `-OrDefault`, `-If`, `-Array`, `-State`, Null adapters). A constant of up to 16 bytes still gets one batch of the whole block at the default block size. The existing conversion pipeline (`convertToFullColumnIfConst`, replicated, sparse and `LowCardinality` handling) is reused on a shorter constant (`ColumnConst::cloneResized`), which replaces the separate one-row branch. Not covered: aggregation with keys (keyed consumers, the JIT loop and the adaptive producer's argument staging read arguments at block positions), and a function with both constant and non-constant arguments. CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=131697&sha=af5e7aee158e8379e77dc757c0a1c04b91ea14d2&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29 CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=131697&sha=af5e7aee158e8379e77dc757c0a1c04b91ea14d2&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Remove the comment narrating the new constant-argument copy in prepareAggregateInstructions; the field comment in Aggregator.h states the semantics. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review of
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261115-050500 |
The copy of a constant argument was sized from the encoded column, but the copy is expanded by `recursiveRemoveLowCardinality` after it is made. A constant `Array(LowCardinality(String))` that repeats one string of 1000 bytes 1000 times is small encoded and 1 MB per row expanded, so `count(c)` over 5000 rows still requested 494.52 MiB instead of about 1 MiB. Measure one row of the constant after removing `LowCardinality`, as the pipeline materializes it. Reported by the automated review: ClickHouse#131185 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
The case of the functions that do not ignore repeated values ran over 200000 rows of a 100 KB constant, so `argMax(c, c)` and `anyHeavy(c)` compared 20 GB per run. It was 230 s of the 246 s per run in `Stateless tests (amd_debug, flaky check)` and 493 s of 499 s under TSan, over the 180 s limit. 5000 rows still make the copy to the block 500 MB against the 100 MB limit, so the case still fails without the fix. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 55e5d03Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261115-110014 |
…ggregate-without-key Resolved a conflict in `AggregateFunctionDistinct.h`: kept the PR's `ignoresRepeatedValues` override and master's `[[nodiscard]]` attributes.
The randomized Parquet writer settings (ClickHouse#132712) made the four 10M-row fixtures of the test take up to 460 s in the tsan and msan flaky checks. Write 1M rows and lower the memory limits from 40 MiB to 3 MiB. With `max_block_size = 8192` and `max_untracked_memory = 0` a read in `max_block_size` chunks peaks below 0.5 MiB, while one chunk of 1M UInt64 rows needs 7.63 MiB, so every case still fails when its exclusion is removed. Since ClickHouse#131185 an aggregation without GROUP BY copies at most 1 MiB of a constant argument, so `count(x)` no longer materializes a one-chunk read. The case counts `materialize(x)` instead, which still does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
In a query without
GROUP BY, an aggregate function whose arguments are all constants no longer copies them to the size of the block: a function that ignores repeated values (uniq,min,max,groupUniqArray,-Distinct, ...) adds the constant once, and any other function adds a copy of at most 1 MiB repeatedly. For example,SELECT count(toFixedString('', 1048576)) FROM tno longer requests 64 GiB of memory per thread withmax_block_size = 65409.Description
Related: #99639, #131697. Failing jobs (server OOM-killed at about 45 GB):
AST fuzzer (amd_debug, targeted)on #99639 (COUNTOrDefaultDistinct), andAST fuzzer (amd_debug, targeted)and(..., old_compatibility)on #131697 (anyOrDefaultandcountoftoFixedString(<constant>, 1048576)).Without keys,
Aggregator::prepareAggregateInstructionscopied every constant argument to the size of the block: withmax_block_size = 65409, a 1 MiB constant is a 64 GiB request in eachAggregatingTransformthread. The analyzer does not fold a 1 MiB constant, socountkeeps it.Now, when all arguments of a function are constants, they are copied to at most
max(1, 1 MiB / bytes of one row)rows (one row for a function withignoresRepeatedValues(), #127940), and the copy is added repeatedly until the block is covered. The function gets the same values in the same order through the same batch API, so every function and combinator is covered; a constant of up to 16 bytes still gets one batch per default-size block.-Distinctforwards only unseen values, so it now reportsignoresRepeatedValues(), which also gives it theLowCardinalitypath of #127940.Validation: the memory queries of the new test fail with
MEMORY_LIMIT_EXCEEDED(requests of 0.5 to 64 GiB) on master and pass with the fix, 50 of 50 randomized runs; the rest compares constants with materialized copies, also split into several pieces.Not covered: aggregation with keys (also the keyed sets of
GROUPING SETS) and functions mixing constant and non-constant arguments. Thetargetedrun's 64 GiB request passingmax_server_memory_usageis theMemoryTrackerproblem of #131625.Workflow [PR]
Sync PR [sync-upstream/pr/131185]
Version info
26.12.1.67-master(included in26.12and later)