Repository navigation
Do not merge-sort the streams of an all-constant full sort fed by a fan-out - #126992
Conversation
…s all-constant The parallel final DISTINCT (ClickHouse#114550) hands its ScatterByPartitionTransform lanes downstream as separate streams, and a scatter holds its input chunk until every unfinished lane has accepted its piece, so the lanes are coupled. A full sort normally decouples them, because MergeSortingTransform consumes its whole input first. When every sort key is a constant, SortingTransform strips them all and MergeSortingTransform::consume passes chunks through, yet SortingStep::fullSort still merged the lanes with MergingSortedTransform. With all keys equal the cursor tie-break is the source number, so the merge asks for its first input only, until that input finishes. The first lane finishes only after every scatter has finished, and a scatter cannot finish while the lanes the merge does not read are full: Pipeline stuck (a logical error, so debug and sanitizer servers abort). Reachable without the fuzzer, e.g. SELECT * FROM (SELECT DISTINCT 'x' AS c, number FROM numbers_mt(100000)) ORDER BY c SETTINGS max_threads = 4, max_block_size = 100 and with a window OVER (ORDER BY <constant>). DISTINCT and ORDER BY in the same SELECT are not affected: preserveInputOrder() forbids the scatter there. An all-constant description orders nothing, so any interleaving is a valid result. fullSort now combines such streams with resize(1), whose ResizeProcessor drains whichever input is ready. This is the remedy that 5e1ec58 (ClickHouse#114626) applied to GatherSendStep for the same wedge; its helper sortDescriptionIsAllConstant moves to Core/SortDescription so both sites share it (no behaviour change in GatherSendStep). The stage grouping used by EXPLAIN PIPELINE is kept, virtual rows are still removed in the shared non-merge branch, and the top-K threshold tracker is skipped because it belongs to the merge. A limit on the sorting step is only a hint pushed down from the LimitStep that stays above it, and the single-stream path already relied on that, so LIMIT and WITH TIES stay correct. Found by AST fuzzer (amd_debug, targeted) on ClickHouse#125514 The new test fails with Pipeline stuck before this change and passes after it, also 50/50 with randomized settings. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Restore the original merge comment line and fold the all-constant note into one line. 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).
Also noticed, not changed: Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261020-162700 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20261020-153100 |
|
cc @alexey-milovidov @davenger, could you review this and add the |
|
Workflow [PR], commit [430c8f7] Summary: ✅
AI ReviewSummaryPrevents the Final VerdictStatus: ✅ Approve |
…feeds it Resizing every all-constant full sort changed the output order of queries like `SELECT ... FROM (... UNION ALL ...) ORDER BY tuple()`, which the merge has always returned in stream order and which tests depend on: 03358_block_structure_match and 03721_tuple_inside_nullable_array_element_function failed in the Fast test, because a resize interleaves the streams nondeterministically. With all-constant keys the merge drains its inputs one after another. That only stalls when a processor upstream must push to all of its outputs before it consumes again (`requiresAllOutputsPushable`, e.g. the scatter of the parallel final DISTINCT), so resize only in that case, the same test `narrowPipe` uses for its `ConcatProcessor`. Everything else keeps the merge and its order. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units839 translation units recompiled, 6589 s compile time in total, 816 of them have a recent master baseline. Translation units without a recent master baseline:
|
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
On master, a sort by constant keys over the parallel final
DISTINCT(#114550) fails withLogical error: 'Pipeline stuck', and debug and sanitizer servers abort:A window
OVER (ORDER BY <constant>)fails the same way, and so does the fuzzer'sORDER BY ALLovertoTypeName(...)columns.The parallel
DISTINCThands itsScatterByPartitionTransformlanes downstream as separate streams, and a scatter only moves on once every lane has accepted its piece. A sort by constant keys does not buffer (MergeSortingTransformpasses chunks through when no key is left), butSortingStep::fullSortstill merges the lanes withMergingSortedTransform. With all keys equal, that merge reads only its first input until it finishes, while the other lanes fill up and block the scatters the first input is waiting for.An all-constant description orders nothing, so when the pipeline contains a fan-out that must push to all of its outputs (
requiresAllOutputsPushable(), the testnarrowPipeuses since #121227),fullSortnow combines the streams withresize(1), the remedy #114626 applied toGatherSendStep. Without such a fan-out the merge stays, soORDER BY <constant>overUNION ALLkeeps returning the streams in order, which tests such as03721_tuple_inside_nullable_array_element_functionrely on. A description with at least one real column always keeps the merge. The helpersortDescriptionIsAllConstantmoves fromGatherSendStep.cpptoCore/SortDescriptionso both sites share it.The new test fails with
Pipeline stuckbefore this change and passes after it (50/50 with randomized settings).Related: #114550 (introduced the parallel
DISTINCT, not in any release yet)Related: #125514 (where this failure was reported)
Found by
AST fuzzer (amd_debug, targeted): CI report.Workflow [PR]
Sync PR [sync-upstream/pr/126992]
Version info
26.11.1.461-master(included in26.11and later)