Repository navigation
Keep one output port per shard when a JOIN side reads nothing - #115352
Conversation
A JOIN sharded by primary-key ranges (`query_plan_join_shard_by_pk_ranges`)
pairs the shards of its two sides positionally, so both sides have to produce
exactly one output port per layer. `optimizeJoinByShards` builds the layers
from the parts of all the sources at once and hands every source the same
number of them, and `ReadFromMergeTree::readByLayers` keeps the port of an
empty layer occupied by substituting a `NullSource` for it.
That invariant did not hold for a source that reads nothing at all:
`initializePipeline` replaced the whole side with a single `NullSource` before
it ever reached the layers. A side pruned to zero parts - by the primary key,
by column statistics, or simply because the table is empty - therefore arrived
at the JOIN with one port while the other side kept its layers. The stream
counts disagreed, `JoinStep` stopped using sharding, and the `YShaped`
pipeline it went to instead accepts only a single port per side:
Logical error: 'Join is supported only for pipelines with one output port, got 1 and 2'
Emit one `NullSource` per layer instead, so the number of ports stays what the
plan says it is. The comment at the `JoinStep` fallback claimed that the
regular pipeline handles diverging stream counts; that is true for
`joinPipelinesRightLeft` (hash) but not for the merge-join path, so it is
corrected as well.
Found by the CI stress and AST fuzzer jobs, where the settings randomizer
enables `query_plan_join_shard_by_pk_ranges` for 30% of the runs: 57 hits of
this stack since 2026-05-27, 5 of them on master. The report this commit
starts from (`03447_analyzer_correlated_subqueries_tpc_h`, TPC-H Q2, where
`part` is pruned to `Parts: 0` by the statistics on `p_size` while `partsupp`
keeps two layers):
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=172d9bf947f5081ddd3288073fe0cf6be664b924&name_0=MasterCI&name_1=Stress%20test%20%28arm_debug%29
|
Workflow [PR], commit [286dd88] Summary: ✅
AI ReviewSummaryThis PR fixes the Final Verdict
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 21/22 (95.45%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units112 translation units recompiled, 1245 s compile time in total, 112 of them have a recent master baseline. |
alexey-milovidov
left a comment
There was a problem hiding this comment.
Ok, that's clear.
`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.
Closes: #115057
Related: #109393
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a
LOGICAL_ERROR(Join is supported only for pipelines with one output port) for afull_sorting_mergeJOIN withquery_plan_join_shard_by_pk_rangesenabled when one of the two sides is pruned to no parts at all - by the primary key, by column statistics, or because the table is empty.Description
A JOIN sharded by primary-key ranges pairs the shards of its two sides positionally, so both sides have to produce exactly one output port per layer.
optimizeJoinByShardsbuilds the layers from the parts of all the sources at once and hands every source the same number of them, andReadFromMergeTree::readByLayerskeeps the port of an empty layer occupied by substituting aNullSourcefor it.That invariant did not hold for a source that reads nothing at all:
ReadFromMergeTree::initializePipelinereplaced the whole side with a singleNullSourcebefore it ever reached the layers. A side pruned to zero parts therefore arrived at the JOIN with one port while the other side kept its layers, the stream counts disagreed,JoinStepstopped using sharding, and theYShapedpipeline it went to instead accepts only a single port per side:The fix emits one
NullSourceper layer, so the number of ports stays what the plan says it is. The layers are only ever set byoptimizeJoinByShards(gated byquery_plan_join_shard_by_pk_ranges, off by default) and are ignored for a read coordinated across parallel replicas, so for every other query the pipeline is unchanged.The comment at the
JoinStepfallback claimed that the regular pipeline handles diverging stream counts. That is true forjoinPipelinesRightLeft(hash) but not for the merge-join path, and it is what made the missing invariant look intentional, so it is corrected as well.This replaces the approach in #109393, which merged the diverging streams back together inside
joinPipelinesYShaped; @vdimir asked there for the invariant to be fixed at its source instead, which is what this pull request does. The throw injoinPipelinesYShapedis left in place.Reproducer (the second query is the shape from the CI report - the sharded side keeps its layers while the other side is pruned away):
How the CI report maps onto this
The failing test in the report is
03447_analyzer_correlated_subqueries_tpc_h(TPC-H Q2), run by the settings randomizer withquery_plan_join_shard_by_pk_ranges = 1andjoin_algorithm = 'full_sorting_merge'. Replaying that query with the report's own settings,EXPLAIN PLAN indexes=1shows the sharded join with one side gone:partis pruned toParts: 0and collapses to a single port;partsuppkeeps one port per layer.CI report this starts from:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=172d9bf947f5081ddd3288073fe0cf6be664b924&name_0=MasterCI&name_1=Stress%20test%20%28arm_debug%29
The stack is one of the most frequent stress and AST-fuzzer failures at the moment: 57 hits of this exact
STID 4488-4e11since 2026-05-27 (5 of them on master), plus 21 more of the two sibling stacks4488-4590and4488-4a91.Verification
05019_join_shard_by_pk_ranges_pruned_sideall abort on a master binary (got 4 and 1) and pass with the fix.join_algorithm = 'hash'withcount()plus acityHash64checksum for theLEFT/RIGHTcases, where every row of the surviving side reaches the output. This covers the semantics of the padded pipeline, not only the disappearance of the exception.03447_analyzer_correlated_subqueries_tpc_hwith the report's full settings passes.join, and the primary-key-sharding tests (03357,03668,03774,04065,04251,04517,04655,04812), pass.Workflow [PR]
Sync PR [sync-upstream/pr/115352]
Version info
26.8.1.1724(included in26.8and later)