Repository navigation
Fix wrong results for a join tree with an unsafe non-leftmost join - #112317
alexey-milovidov merged 2 commits into
Conversation
Internal second-model review (click to expand)This change went through 11 internal review rounds before being opened. Every finding below Blockers found and fixed (❌ -> fixed):
Majors found and fixed (
Disagreed, with evidence (
Noted, not blocking (💡):
Session id: cron:clickhouse-review-slot-48:20260728-155200 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-41:20260728-060900 |
|
cc @devcrafter @yakov-olkhovskiy — could you review this? Parallel-replicas eligibility for a join tree is decided by |
|
Workflow [PR], commit [517c8a6] Summary: ✅
AI ReviewSummaryThis PR fixes the wrong-result class where task-based parallel replicas can duplicate rows when an unsafe non-leftmost join in the join tree is never examined by Findings
Final VerdictChanges requested. LLVM Coverage ReportMeasured on commit 517c8a6.
Changed lines: Changed C/C++ lines covered: 29/29 (100.00%) · Uncovered code |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 30/30 (100.00%) · Uncovered code |
CI finish ledger — 039bc64Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Not caused by this PR: the diff here touches Session id: cron:our-pr-ci-monitor:20260729-010000 |
… join With `enable_parallel_replicas = 1`, `t1 INNER JOIN t2 ON t1.c = t2.c ANY INNER JOIN t3 ON 1` returns the row once per replica instead of once: 3 rows in 60 of 60 runs on a three-replica cluster with `parallel_replicas_local_plan = 0`, and correct in 60 of 60 with the local plan on, which is why CI sees it intermittently. Found by 04490_any_join_constant_condition_reorder in Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel). Parallel-replicas eligibility for a join tree is decided by allowParallelReplicasForJoinTree, which is evaluated only on the first join node of the post-order table-expression stack. For (t1 ALL INNER JOIN t2) ANY INNER JOIN t3 that node is the Inner/All join, so the outer ANY join is never examined. The initiator then ships the whole join tree to every replica while only the leftmost leaf's reads are coordinated, so each replica applies ANY to its own slice and the initiator concatenates the results. This is why the extra rows are duplicates rather than different right-side values: the strictness is not lost, it is applied once per replica. should_disable_parallel_replicas already walks the whole stack and already vetoes n-way trees on other grounds (FULL/GLOBAL/CROSS, and a misplaced RIGHT); it simply never looked at strictness. It now also vetoes a non-leftmost join whose strictness is not ALL outside a LEFT kind, or whose kind is PASTE (pairs rows by position) or FULL (emits unmatched right rows, which each replica would decide from its own slice of the left side). Outside LEFT the strictness test is a whitelist, so a future JoinStrictness enumerator fails closed there; under LEFT every strictness is admitted, because those strictnesses are decided per left row. The veto is not gated on joins_count, because an ARRAY JOIN can occupy the leftmost slot without incrementing it. ARRAY JOIN ... ALL RIGHT JOIN is left eligible on purpose: vetoing it would disable parallel replicas for 03452_array_join_global_right_join_parallel_replicas, whose purpose is to exercise that path, and its row counts match a parallel-replicas-off oracle in 60 of 60 runs. That residual is master's own behaviour. The new test asserts the decision rather than the row count: whether the plan contains a ReadFromRemoteParallelReplicas step. Ten of its twenty assertions flip on a master binary, the other ten are guards that read the same value on both, and each term of the veto is pinned on its own. Brought up to master 77fa254. One conflict, in the flag declarations in front of the table-expression-stack loop, where both sides only append: master adds has_global_join_preserving_broadcast_rows, read at the distributed left-table wrapping decision, and this branch adds leftmost_join_tree_node_seen and has_unsafe_non_leftmost_join, read in should_disable_parallel_replicas. The readers are disjoint, so both declarations are kept; a duplicate-name census over the declaration block reports 10 names, 10 unique. Co-Authored-By: Claude Opus 5 <[email protected]>
039bc64 to
637c36c
Compare
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, 21 s compile time in total, 7 of them have a recent master baseline. |
A non-leftmost `ASOF INNER JOIN` decides each left row on its own, so the veto's comment
("a non-leftmost join must be distributive over a partition of the left side") made its
rejection look arbitrary. That criterion is not what decides.
A leaf's reads are coordinated only for the join shapes the search for that leaf descends:
`LEFT`, `INNER` with `ALL`, and a qualifying `RIGHT` (`getSupportingParallelReplicasQuery`,
src/Planner/findParallelReplicasQuery.cpp:161, and `findTableForParallelReplicas`, :476).
`Inner` with `Asof` is in neither set, so nothing partitions the left side, every replica
evaluates the whole join and the initiator concatenates the copies.
Measured on master without this veto, three replicas, `parallel_replicas_local_plan = 0`:
a non-leftmost `ASOF INNER JOIN` returns every row once per replica, 290 rows becoming 870
over a 300-row left side in 60/60 runs, and 3 rows becoming 9 on a minimal fixture in 40/40.
The count tracks `max_parallel_replicas` (1, 2, 3 give 3, 6, 9 rows). The ASOF picks are
correct: no row differs from the single-replica answer, so the defect is pure duplication.
`ASOF LEFT`, `ANY LEFT` and `SEMI LEFT` keep parallel replicas and stay correct in 40/40 runs
on both binaries, which is what the kind exemption is for; the kind, not per-left-row
semantics, is what makes a strictness safe here.
The comment now states that rule, at its previous length. The ASOF assertions move to a `t4`
fixture whose keys overlap `t1` (`t3`'s do not, so that ASOF answer was empty and no row
assertion on it could ever fail) and gain the row assertion, which returns the row three
times on a build that exempts `JoinStrictness::Asof`. The plan-shape line and the new row
line are the only two assertions that flip on such a build.
|
|
||
| /// A non-leftmost join that is not replica-safe (e.g. INNER ... ANY INNER). Deliberately not gated on | ||
| /// `joins_count`: an ARRAY JOIN can occupy the leftmost slot without incrementing it. | ||
| if (has_unsafe_non_leftmost_join) |
There was a problem hiding this comment.
enable_parallel_replicas = 2 is the "must use parallel replicas or fail" mode, and the neighboring planner gates preserve that contract by throwing SUPPORT_IS_DISABLED before they clear the setting (for example Planner.cpp:2615-2702, and 04604_join_using_nested_projection_alias_remote.sql has the regression test for the join side of that rule). This new veto clears the alias unconditionally instead, so a query like ... ARRAY JOIN ... ANY INNER JOIN ... SETTINGS enable_parallel_replicas = 2 now silently falls back to a plain local read. That makes an explicitly forced configuration indistinguishable from a supported plan that actually used parallel replicas. Please branch here the same way the other unsupported-shape checks do: if allow_experimental_parallel_reading_from_replicas >= 2, throw SUPPORT_IS_DISABLED; otherwise disable parallel replicas and add a focused force-mode test for one of these newly rejected join trees.
There was a problem hiding this comment.
Both citations check out, and force mode does throw where master implements it: on this build at enable_parallel_replicas = 2, additional_table_filters and the custom-key JOIN gate both give Code: 344 ... (SUPPORT_IS_DISABLED), so the "no exception" results below are not an artifact of a probe that never reaches force mode. enable_parallel_replicas is the alias of allow_experimental_parallel_reading_from_replicas (Settings.cpp, DECLARE_WITH_ALIAS), verified in both directions at runtime, so your predicate would read the value this site clears.
What that clear is not is this PR's line. It is the call site of should_disable_parallel_replicas, unconditional since d8f1f6f266392 (2024-12-09), and each of its four pre-existing checks reaches it the same way. Measured on a three-replica ReplicatedMergeTree cluster at = 2, pristine master 1b86f9d3f25a9 against this branch:
shape at enable_parallel_replicas = 2 |
master | this branch |
|---|---|---|
LEFT ... ALL RIGHT (master's misplaced-RIGHT check) |
no exception, no parallel replicas in the plan | same |
INNER ... FULL (master's n-way FULL check) |
no exception, no parallel replicas in the plan | same |
INNER ... ANY INNER ON 1 (what this PR vetoes) |
no exception, parallel replicas used, 3 rows against an oracle of 1 | no exception, 1 row |
The not-enough-rows clear in the same file behaves the same way at = 2, and a supported shape does plan parallel replicas there, so those zeroes are the veto and not a dead fixture. Three of the five unconditional clears in this file log the downgrade; this call site is not one of them, which is the other half of "silently".
So force mode is already advisory at this site for four shapes this PR does not touch, and this PR's own delta at = 2 is three duplicated rows becoming one correct row, not an error anyone loses.
I did not fold the branch in. At the call site it changes behaviour for those four other shapes; scoped to my check alone it makes one of five sibling checks throw. Either way that is a ruling about this site's contract rather than about a wrong-results fix, so I also left today's force-mode behaviour unpinned in 04651, and a later change will not have to fight it.
The lever is small: the >= 2 branch, the missing LOG_DEBUG, and a force-mode test for one of the newly rejected trees. If a reviewer wants it in this PR, say so and I will push it; otherwise it belongs in its own PR and I will keep it out of a bug fix.
Related: #109414 (comment)
Found by
04490_any_join_constant_condition_reorderinStateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel): CI report.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix wrong results with
enable_parallel_replicas = 1when a join tree contains a join other than the leftmost one that cannot be applied independently per replica: a strictness other thanALLoutside aLEFTjoin, for examplet1 INNER JOIN t2 ON t1.c = t2.c ANY INNER JOIN t3 ON 1, which returned the row once per replica instead of once; aFULLjoin, which emitted every row twice; or aPASTEjoin, which pairs rows by position. Parallel replicas are now disabled for such a join tree, as they already are for the equivalent two-table join.Description
Symptom.
SELECT * FROM t1 INNER JOIN t2 ON t1.c = t2.c ANY INNER JOIN t3 ON 1returns 3 rows instead of 1 on a three-replica cluster: measured 60/60 runs withparallel_replicas_local_plan = 0and 60/60 correct with it on, which is why CI sees it intermittently.Root cause. Parallel-replicas eligibility is decided by
allowParallelReplicasForJoinTree, evaluated only on the first join node of the post-order table-expression stack. For(t1 ALL INNER JOIN t2) ANY INNER JOIN t3that node is theInner/Alljoin, so the outerANYjoin is never examined. The initiator then ships the whole join tree to every replica while only the leftmost leaf's reads are coordinated, so each replica appliesANYto its own slice and the initiator concatenates the results.Fix.
should_disable_parallel_replicasalready walks the whole stack and already vetoes n-way trees on other grounds (FULL/GLOBAL/CROSS, a misplacedRIGHT); it never looked at strictness. It now also vetoes a non-leftmost join whose strictness is notALLoutside aLEFTkind (a whitelist, so a futureJoinStrictnessfails closed), or whose kind isPASTE(pairs rows by position) orFULL(emits unmatched right rows, which each replica would decide from its own slice of the left side). AnARRAY JOINalso occupies the leftmost slot, so the veto is not gated onjoins_count, which that node does not increment. The change only ever removes queries from the parallel-replicas path.Force mode (
enable_parallel_replicas = 2) still downgrades silently at this call site, as it has for its four other checks since 2024.Measurements and what the new test pins
Three-replica cluster (
ReplicatedMergeTree,cluster_for_parallel_replicas = 'test_cluster_one_shard_three_replicas_localhost'), row counts against a parallel-replicas-off oracle on the same fixture, pristinemastervs this branch:local_planINNER ... ANY INNER ON 1(oracle 1)ARRAY JOIN ... ANY INNER ON 1(oracle 1)ARRAY JOIN ... FULL JOIN(oracle 5)ALL LEFT(oracle 2, control)parallel_replicas_local_plan = 1keeps all three shapes correct on both binaries, and the runner randomizes that setting, which is why CI sees the failure intermittently.The test asserts the decision rather than the row count: whether the plan contains a
ReadFromRemoteParallelReplicasstep. Of its 20 assertions, ten flip on amasterbinary (the threeANY INNERshapes,ARRAY JOIN ... FULL, the fourARRAY JOIN ... RIGHTstrictness shapes,ASOF INNER,PASTE); the other ten are guards that read the same value on both. Each term is pinned on its own: reverting only theFULLterm flips exactlyarray join then full, onlyPASTEexactlypaste non-leftmost, deleting&& join_kind != JoinKind::Leftexactly the twoLEFTlines.ARRAY JOIN ... ALL RIGHT JOINis deliberately left eligible: vetoing it would disable parallel replicas for03452_array_join_global_right_join_parallel_replicas, whose purpose is to exercise that path. That residual ismaster's behaviour; its row counts match a parallel-replicas-off oracle in 60/60 runs on both binaries.No setting default changes, so no
SettingsChangesHistory.cppentry. No serialization or format change.Workflow [PR]
Sync PR [sync-upstream/pr/112317]
Version info
26.9.1.1336(included in26.9and later)