Repository navigation
Fix Join is supported only for pipelines with one output port in a parallel_full_sorting_merge join - #117826
Conversation
`optimizeParallelFullSortingMergeJoin` converts both pre-join sorts with `convertToScatteredFullSort`, whose contract is one sorted stream per shard with no final merge, so that the two sides present the same number of ports. `JoinStep::updatePipeline` relies on that, because it pairs the shards of the two sides positionally. `SortingStep::fullSort` decided its final merge from `pipeline.getNumThreads`, which is a per-side budget rather than a property of the shared plan: a single-table subquery with a trivial `LIMIT` is planned with one thread, while a `UNION ALL` side is re-budgeted to the sum of its branches. The one-thread side therefore merged its shards back into a single port while the other kept them, the counts diverged, `use_sharding` was dropped, and the single-port fallback `joinPipelinesYShaped` threw `Join is supported only for pipelines with one output port`. On a release build the query fails with `Code: 49`; in debug and sanitizer builds the server aborts. Reachable on the released 26.8. The same line had a second effect: when both sides were limited to one thread the merge fired on both, the counts stayed equal, and the sharded join the optimizer had asked for was silently replaced by a single merge join. Skip the merge when the sort is a fixed-shard-count scatter. This completes 49c7604, which changed the scatter width from the thread count to `scatter_partitions` but left the merge-back condition consulting the thread count. A plain sort and a window-frame partitioned sort are unaffected, since both have `scatter_partitions` equal to zero. Found by the in-stress AST fuzzer on the `Stress test (arm_msan)` run of ClickHouse#117229. Co-Authored-By: Claude Opus 5 <[email protected]>
…d count The preceding commit dropped the `pipeline.getNumThreads() == 1` clause from the final merge condition in `SortingStep::fullSort`. That clause is what makes a window-frame partitioned sort merge its sorted streams into one when the pipeline is limited to a single thread, so without it such a sort keeps several output ports and the step no longer produces the single stream its consumer expects. Only the `scatter_partitions == 0` guard belongs there. A fixed-shard-count scatter must keep one stream per shard; every other partitioned sort keeps the behaviour it had before. Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review: 0 blocking findingsAn independent cold review and a second-model gate over the whole diff both came back with nothing Why six statements for a one-line fix. Each covers a different scenario, none repeats another: Both planners, and the one randomized setting that could have moved the plan. The test's output Nit I left alone: the new comment is three lines against one changed condition. |
|
Workflow [PR], commit [c085219] Summary: ✅
AI ReviewSummaryThe change preserves one sorted stream per hash shard in Missing context / blind spots
Final VerdictStatus: ✅ Approve. No code changes requested. |
|
cc @vdimir @antaljanosbenjamin, could you review this? This is the other-layer fix for the invariant discussed on #109393: |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 14 s compile time in total, 7 of them have a recent master baseline. |
| /// of its two sides positionally, and the thread count is a per-side budget, not a shared contract. | ||
| /// If there are several streams, then we merge them into one | ||
| if (pipeline.getNumStreams() > 1 && (partition_by_description.empty() || pipeline.getNumThreads() == 1)) | ||
| if (scatter_partitions == 0 && pipeline.getNumStreams() > 1 |
There was a problem hiding this comment.
Wouldn't merging all partitions break the whole idea of partitioned sorting and joining?
There was a problem hiding this comment.
The condition moved the other way: scatter_partitions == 0 && means a fixed-shard scatter no longer merges its shards back, so one sorted stream per shard now survives into the join. Merging them was the bug: one side ended at 1 output port and the other at 4, use_sharding dropped in JoinStep::updatePipeline, and joinPipelinesYShaped threw got 1 and 4.
After the change the merge fires exactly when the scatter did not partition the streams. scatterByPartitionIfNeeded takes threads = scatter_partitions > 0 ? scatter_partitions : pipeline.getNumThreads() and scatters only when threads > 1, so:
partition_by_descriptionempty, or non-empty withgetNumThreads() == 1: the scatter did not run, so the streams are not partition-disjoint and merging them is what keeps each window partition contiguous. Both unchanged, and the last statement of the test guards the second one: on a build with thegetNumThreads() == 1clause removed it goes from1 0to0 0, i.e. two non-disjoint streams left unmerged.- non-empty with
getNumThreads() > 1: scattered by the thread count, one stream per partition, unchanged. scatter_partitions > 0: always scattered intoscatter_partitionsshards, sinceoptimizeParallelFullSortingMergeJoinreturns early onnum_shards <= 1andskipScatterByPartitionexcludesisSortingForMergeJoin. Those are the shards the join pairs positionally, so they must not be merged.
So the merge only collapses streams that were never partitioned, and both join sides now keep the same shard count whatever their per-side thread budget is. That also fixes the quieter half: with both sides at one thread the sharding was collapsed into a single merge join instead of throwing, which the fifth statement in the test covers.
CI finish ledger - 2657185Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The rest of the run was clean: 170 check-runs, Session id: cron:our-pr-ci-monitor:20260903-113000 |
CI finish ledger - 0518a97Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only 170 check runs: 150 success, 19 skipped, 1 failure.
Session id: cron:our-pr-ci-monitor:20260903-180000 |
`Keep one output port per shard when a JOIN side reads nothing` (ClickHouse#115352) pads a nothing-to-read `ReadFromMergeTree` side with one `NullSource` per layer, so the primary-key-range sharding route this test drove no longer leaves the two join sides with different stream counts. The query now takes the by-shards pipeline (`MergeJoinTransform` x 8) and never reaches `joinPipelinesYShaped`, so the `EXPLAIN PIPELINE` assertion that looked for the merge went red while the four value assertions started holding on master as well. Point the test at the route that still reaches the same throw site. A `parallel_full_sorting_merge` join scatters both sides into a fixed number of shards and pairs those shards positionally, but `SortingStep::fullSort` decides the merge back into one stream per side from that side's own thread budget, so a trivial-`LIMIT` side collapses to one port while a `UNION ALL` side keeps its shards. Three arms on a debug build of this branch, comparing against the hash join: pristine master 2f4969d aborts, `got 1 and 4` at QueryPipelineBuilder.cpp:373 sorted merge replaced by an unordered `pipe.resize(1)` 0, wrong results this branch (build id fae830443...) 1 The middle arm is what the removed `EXPLAIN PIPELINE` assertion covered, and it covers it without pinning the plan text, so the test also survives the optimizer-side fix in ClickHouse#117826. 25/25 runs with randomized settings; the 21 `full_sorting` and 4 `join_shard` stateless tests pass, including `05019_join_shard_by_pk_ranges_pruned_side` from ClickHouse#115352. The `no-random-merge-tree-settings` tag and the explicit `SET`s are gone with the table: nothing the runner randomizes reaches the new query, whose settings are pinned per statement. No source change: the fix is byte-identical to the approved revision.
|
Dear @vdimir, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
…orting-merge-join-stream-divergence
|
@groeneai is this PR still needed? |
alexey-milovidov
left a comment
There was a problem hiding this comment.
At least I see no flaws in it.
|
It was merged today by @ alexey-milovidov (merge commit 4e08dbd), so nothing is left to decide here. On master |
Related: #117229 (comment)
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed the exception
Join is supported only for pipelines with one output portwhenjoin_algorithm = 'parallel_full_sorting_merge'is used withmax_threadsgreater than one and the two sides of the join are planned with different thread budgets, for example when one side is a subquery with a trivialLIMITand the other is aUNION ALL. The same defect also silently disabled the sharded join when both sides were limited to a single thread.Description
Found by the in-stress AST fuzzer on
Stress test (arm_msan)of #117229:https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=117229&sha=857ef8062ab13fe7870b03d3735acc1303c60b4b&name_0=PR&name_1=Stress%20test%20%28arm_msan%29
Reproducer:
On released 26.8.2.7 this is
Code: 49 ... got 1 and 4; debug and sanitizer builds abort.Root cause.
optimizeParallelFullSortingMergeJoinscatters both sides into a fixedscatter_partitions, and the join pairs the shards positionally, so each side must keep one stream per shard.SortingStep::fullSortinstead decided its final merge frompipeline.getNumThreads, a per-side budget: a trivial-LIMITsubquery gets one thread, aUNION ALLside the sum of its branches. The one-thread side merged back to a single port, the counts diverged,use_shardingwas dropped, andjoinPipelinesYShapedthrew.The change. Skip that merge when the sort is a fixed-shard-count scatter (
src/+4/-1, one function) - the missing half of49c76044b106ac5, which made the scatter width usescatter_partitionsrather than the thread count. This is the other-layer fix @ vdimir asked for on #109393 rather than the call-site merge he declined there; #115352 closed the primary-key-range route, so #109393 is superseded; its disposition is for its reviewers.Validation. Both directions on one tree; every statement of the new test
05059reddens pre-fix or holds as a control; 50/50 sequential and self-parallel green; the 21 existingparallel_full_sorting_mergetests plus the scatter, window andEXPLAIN-shape tests are byte-identical on both binaries. Theparallel_full_sorting_merge_joinperformance test may move, as one-thread sides now shard.Alternatives considered and rejected
joinPipelinesYShapedcall site - Fix LOGICAL_ERROR in full_sorting_merge JOIN when sharding stream counts diverge #109393's current diff, declined by @ vdimir; repairs the consumer and leaves the sharding silently off.JoinStep::updatePipelinewhen the counts diverge - same objection one step earlier, and that function treats divergence as an inconsistent plan rather than a case to absorb.scatter_partitionsafter scattering - would also remove the merge and would run the shards in parallel, but it overrides a budget that was deliberately lowered, changing the resource profile of queries that are not broken. The port count is the contract; the budget is a separate decision, so I left it out of this fix.Workflow [PR]
Sync PR [sync-upstream/pr/117826]
Version info
26.11.1.1790-master(included in26.11and later)