Repository navigation
Do not copy a constant column to every row of a block in DISTINCT - #131870
alexey-milovidov merged 3 commits into
Conversation
A DISTINCT whose columns are all constants returns one row, but
DistinctTransform::deduplicate converted the whole chunk to full columns
before cutting it to that row, so a constant was replicated to the chunk
size first (65409 rows x 1 MiB = 64 GiB for `SELECT DISTINCT
toFixedString('a', 1048577) FROM numbers(100000)`). The fuzzed query
`SELECT DISTINCT toFixedString(.., 1048577) FROM clusterAllReplicas(..,
system.text_log)` made one such ~8 GiB allocation per reading stream and
the AST fuzzer server was OOM-killed. The conversion now runs after the
cut, so the emitted chunk is unchanged (one row, full columns).
DistinctSortedStreamTransform (optimize_distinct_in_order) materialized
every column of every chunk as well, although header constants are never
its keys. It now materializes the non-constant columns and the sorting
prefix (a constant can be the sorting prefix), and keeps the other header
constants compact, as DistinctSetFilter already does for the hashed path
of the same DistinctStep.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
DistinctSortedStreamTransform still materialized a header constant that
is part of the sorting prefix, e.g. the final in-order DISTINCT of
`SELECT DISTINCT number AS k, repeat('a', 1000000) AS s FROM
numbers(100000) ORDER BY s`, which requested 60.92 GiB with
max_block_size = 65409. Only the non-constant columns are materialized
now, exactly the DistinctSetFilter rule. The range search already treats
a constant as one run (ColumnConst::compareAt is always equal), and the
previous-chunk key is saved from, and compared against, the constant's
single nested value, so both sides of that comparison have the same
representation whether a chunk carries the column as a constant or not.
The test gets the constant-sorting-prefix query, and both in-order
queries get an EXPLAIN PIPELINE check that they run
DistinctSortedStreamTransform, so a plan change cannot turn them into
checks of the hashed path.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the design note above materializeChunk in DistinctSortedStreamTransform::transform; the helper's own documentation already says only non-constant columns are returned. 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; 1 full pass + 1 fix round + 1 delta recheck with 0 findings).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261115-202802 |
|
cc @davenger, could you review this? A DISTINCT over constants only (and an in-order DISTINCT) materialized every constant column to the full block size before deduplicating, so a 1 MiB constant asked for ~64 GiB per block; the const-only path now converts after cutting to one row, and the in-order transform materializes only non-constant columns, as the hashed path already does. |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261115-200300 |
|
Workflow [PR], commit [42dc29d] Summary: ✅
AI ReviewSummaryPR #131870 avoids expanding constant columns before constant-only deduplication and keeps header constants compact during in-order Compact-column consumers traced:
Final Verdict✅ Approve. No required changes or new inline findings. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units9 translation units recompiled, 26 s compile time in total, 9 of them have a recent master baseline. |
009ed37
`ColumnConst` had no `getEqualRangeEndAssumeSorted`, so the generic galloping search compared the nested value with itself on every probe. Since ClickHouse#131870 a constant stays a `ColumnConst` in the sorting prefix of the in-order DISTINCT, and debug and sanitizer builds re-verify every run (the `chassert` in `SortedKeyRuns::findRunEnd` re-searches each key column, and `checkEqualRangeEndAssumeSorted` samples more probes). With a 1 MB constant before a distinct column every run is one row, which is about 25 one-megabyte compares per row inside one `work()` call: the query could not observe `max_execution_time` or KILL (51 s in a debug build, 1668 s in the `Stress test (amd_tsan)` hung check). Every row of a constant compares equal for any NaN direction hint, so the run starting at `begin` ends at `end`, the same property behind the identity `getPermutation` and the no-op `updatePermutation`. The release build was not affected (it searches the constant once per chunk), so this does not revert ClickHouse#131870, which would bring back the per-row copy of the constant that it removed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #131793
Related: #131625
Related: #131185
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
SELECT DISTINCTno longer copies a constant column to every row of a block when allDISTINCTcolumns are constants, or when theDISTINCTis done in order (optimize_distinct_in_order). For example,SELECT DISTINCT toFixedString('a', 1048577) FROM numbers(100000)no longer requests 64 GiB of memory withmax_block_size = 65409.Description
Failing job (server OOM-killed):
AST fuzzer (amd_release, oracle)on #131793,SELECT DISTINCT toFixedString(<3 bytes>, 1048577) FROM clusterAllReplicas(..., system.text_log) ... LIMIT -2147483648, one ~8 GiB allocation per reading stream. No related open issue found.A
DISTINCTover constants only returns one row, butDistinctTransform::deduplicateconverted the whole chunk to full columns before cutting it to that row, so each constant was first replicated to the chunk size. The conversion now runs after the cut; the emitted chunk is unchanged (one row, full columns).DistinctSortedStreamTransform(optimize_distinct_in_order) materialized every column of every chunk too, header constants included. It now materializes only the non-constant columns, asDistinctSetFilteralready does for the hashed path of the same step; a constant in the sorting prefix is compared through its single value.Validation: each memory query of the new test fails on master with
MEMORY_LIMIT_EXCEEDED(61 to 64 GiB requested; theFixedStringquery fails the same way on 25.3 and 26.8) and passes with the fix, 50 of 50 randomized runs; reverting either half reddens its own queries. Theremote()andclusterAllReplicasshapes behave the same. The 762 stateless tests that useDISTINCTshow no new failure (the 38 that fail locally, on stateful datasets and object storages, fail on master too).The fuzzer server's
max_server_memory_usagenot stopping the last allocation is theMemoryTrackerproblem of #131625.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/131870]
Version info
26.11.1.2411-master(included in26.11and later)