Skip to content

Fix duplicated rows of ANY RIGHT JOIN with any_join_distinct_right_table_keys under parallel replicas - #123683

Merged
fm4v merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-pr-right-join-right-any-strictness
Oct 9, 2026
Merged

fm4v merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-pr-right-join-right-any-strictness

Conversation

@groeneai

@groeneai groeneai commented Oct 3, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #123992
Related: #75262

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed duplicated rows returned by ANY RIGHT JOIN with any_join_distinct_right_table_keys = 1 when parallel replicas are enabled.

Description

On 26.3.33.24, with any_join_distinct_right_table_keys = 1 and parallel replicas, SELECT a2, b2 FROM tab2 ANY RIGHT JOIN tab3 ON a2 = a3 OR b2 = b3 (the 01660_join_or_any tables) returns the row 6 4 twice in 79 of 100 runs; 26.8.10.6 does the same. Failing CI job: 01660_join_or_any, Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel).

Root cause: under this setting ANY RIGHT JOIN gets strictness RightAny, which gives each left row one right row of its key, chosen from all right rows. allowParallelReplicasForJoinTree admitted every RIGHT join, so the whole join was shipped to the replicas. Each replica read only its share of the right table, so a left row that matched on two replicas was emitted twice. The plan-based pass had the same gap in liftSplitAboveJoin. The query-tree walk already refuses this shape (#75262).

Change: in query-tree mode allowParallelReplicasForJoinTree refuses RIGHT + RightAny. In plan-based mode only the lift is refused, so the right table is still read with parallel replicas, within parallel_replicas_min_number_of_rows_per_replica, and the join runs once on the initiator. Other RIGHT strictnesses are unchanged.

Validation: the new test 05320_parallel_replicas_right_join_right_any fails 60/60 on master and passes 150/150 with the fix, including runs under the ParallelReplicas profile at both parallel_replicas_local_plan values. Reverting either change turns its own test line red. On the 01660_join_or_any tables, 273 of 800 master runs return the duplicate, against 0 of 800 with the fix.


Workflow [PR]
Sync PR [sync-upstream/pr/123683]

Version info

  • Merged into: 26.10.1.1880-master (included in 26.10 and later)
  • Backported to: 26.9.15.7, 26.8.23.8

…across parallel replicas

With `any_join_distinct_right_table_keys = 1`, `ANY RIGHT JOIN` becomes kind `Right` with
strictness `RightAny`: each left row takes the first right row of its key, and the whole key is
marked used. That choice is made over ALL right rows of the key.

The leftmost-leaf gate `allowParallelReplicasForJoinTree` admitted every `RIGHT` join, so the
whole join was shipped to the replicas, each replica read only its coordinated share of the right
table, and a left row that matched on several replicas was emitted once per replica
(`01660_join_or_any` returned `6 4` twice in the new ParallelReplicas job). The plan-based pass
had the same gap: `liftSplitAboveJoin` lifted the right-side split above such a join.

The query-tree walk `getSupportingParallelReplicasQueries` already refuses this shape (ClickHouse#75262).
Make the leftmost-leaf gate refuse it too, and in plan-based mode refuse only the lift, so the
right read stays distributed below the join and the join runs once over all right rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding labels Oct 3, 2026
@groeneai

groeneai commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding in the first pass, 0 in the re-review of the final PR description), plus my own review (1 nit).

# Sev Finding Verdict Evidence / action
1 💡 Changelog entry said the right table is no longer split across replicas, but plan-based mode still reads it with parallel replicas and only keeps the join on the initiator AGREE, fixed sentence removed from the changelog entry before publication
2 💡 The first test line (count() without parallel replicas) passes on master AGREE, noted it documents the expected row count; the EXPLAIN lines are the checks, and reverting either change turns its own line red

Severity: ❌ blocker / ⚠️ major / 💡 nit. No blocker or major findings.

Session id: cron:clickhouse-review-slot-10:20261003-111900

@groeneai

groeneai commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The EXPLAIN lines of 05320_parallel_replicas_right_join_right_any fail 60/60 on master. The 01660_join_or_any duplicate reproduces in a 200-run loop (54/200 query-based, 145/200 plan-based).
b Root cause explained? RightAny picks one right row per left row out of the whole right table. allowParallelReplicasForJoinTree (and liftSplitAboveJoin in plan-based mode) let such a RIGHT join run per replica on a share of the right table, so a left row matched on two replicas was emitted twice.
c Fix matches root cause? Yes. Both gates that allow splitting the right side now refuse RightAny, matching the existing check in getSupportingParallelReplicasQueries (#75262). No downstream guard, no test tag.
d Test intent preserved / new tests added? 01660_join_or_any is unchanged and passes 100/100 under the ParallelReplicas profile. New SQL test with EXPLAIN checks and positive controls (strictness Any still uses parallel replicas).
e Both directions demonstrated? Yes, on Build-ID-checked binaries: new test 60/60 FAIL on master, 150/150 OK with the fix. On the 01660 tables, 273/800 wrong on master, 0/800 with the fix. Reverting either change alone turns only its own test line red.
f Fix is general across code paths? Query-based leftmost-leaf gate and plan-based lift are both fixed; the walk already refused it; non-leftmost joins are covered by #112317; FULL/INNER never get RightAny admitted.
g Fix generalizes across inputs (params/datatypes/wrappers)? Checked ANY RIGHT, RIGHT ANY, join_default_strictness = 'ANY', USING, an OR condition and a Nullable key: all stop using parallel replicas. ALL/SEMI/ANTI RIGHT and ANY LEFT still use them.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or stored state changes; only these joins run without parallel replicas.
i Invariants and contracts preserved? Both changes only return "not eligible" earlier. The plan-based early return leaves the plan in the shape the pass already produces for a refused lift (split below the join).

Session id: cron:clickhouse-impl-slot-5:20261003-102500

@clickhouse-gh

clickhouse-gh Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ff9b30d]

Summary: ✅


AI Review

Summary

This PR fixes a real correctness hole in parallel replicas for ANY RIGHT JOIN under any_join_distinct_right_table_keys = 1: query-tree mode now refuses shipping the whole join, while plan-based mode still allows planner-side sizing of the right read and keeps the join local during split lifting. I re-checked the current diff, the surrounding planner/optimizer paths, and the prior discussion thread; the earlier concern about losing parallel_replicas_min_number_of_rows_per_replica handling in plan-based mode is fixed in the current code, and I did not find any remaining blocker or major issue in the changed paths.

Final Verdict

✅ No new findings.

LLVM Coverage Report

Measured on commit ff9b30d.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 11/12 (91.67%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 3, 2026
@groeneai
groeneai requested review from devcrafter and vdimir October 3, 2026 11:48
@clickhouse-gh clickhouse-gh Bot added the comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). label Oct 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit ff9b30db2cb13cfa35378389eaa5511f01466832 was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception).

See the job log for details.

@groeneai

groeneai commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 510cc85

Every 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 CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Build (amd_fuzzers) / Build ClickHouse not from this PR: linking clickhouse_fuzzer overflows the 2 GiB R_X86_64_PC32 reach (.text to .bss, 2148738730); master itself is red on this job since 12:48Z today with the same overshoot #122521 (mine, open)
Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel) / 04819_dynamic_aggregate_function_state_version flaky (0/26 reruns failed): the return code: 1 shape at a Native fetch, about 1 s in; 16 PRs in the last 24h on this new job #123649 (mine, open)
Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel) / 05257_sql_security_view_sealed not from this PR: a parallel replica reads the SQL SECURITY DEFINER view's row-policy table as default instead of the definer (Code: 497); 39 PRs in the last 24h on this new job a fix task is created (fix in review, fixing-PR link to follow here)
Stateless tests (arm_binary, s3 storage, DBReplicated, parallel, 1/2) / 05237_with_expression_alias_scope_descendant deterministic under a Replicated database: the replicated CREATE VIEW stores a nested WITH alias qualified with the database, so the view reads the same-named table; 89 PRs in the last 24h on this new job a fix task is created (fix in progress, fixing-PR link to follow here)
Mergeable Check, PR rollups, red only because of the rows above #122521, #123649 and the two fix tasks above

None of these goes through the ANY RIGHT JOIN gate this PR changes. All 160 checks completed and Finish Workflow is done; CIDB has no other FAIL or ERROR row on this head.

Session id: cron:our-pr-ci-monitor:20261003-170255

@groeneai

groeneai commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator Author

Fixing PR for 05257_sql_security_view_sealed (parallel replicas) created: #123729

@groeneai

groeneai commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator Author

Fixing PR for 05237_with_expression_alias_scope_descendant (DBReplicated): #123774

Comment thread src/Planner/PlannerJoinTree.cpp Resolved
In plan-based mode `allowParallelReplicasForJoinTree` does not ship the
join; it only guards the planner-side sizing by
`parallel_replicas_min_number_of_rows_per_replica`. Refusing RightAny there
skipped that sizing while `applyParallelReplicas` still distributed the
right read, so the query fanned out to `max_parallel_replicas` replicas
regardless of the threshold. Refuse RightAny in this gate only in the
query-tree mode; in plan-based mode `liftSplitAboveJoin` already keeps the
join on the initiator.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fm4v
fm4v added this pull request to the merge queue Oct 8, 2026
Merged via the queue into ClickHouse:master with commit dc7fc42 Oct 9, 2026
152 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 9, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Oct 10, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 10, 2026
Backport #123683 to 26.9: Fix duplicated rows of `ANY RIGHT JOIN` with `any_join_distinct_right_table_keys` under parallel replicas
clickhouse-gh Bot added a commit that referenced this pull request Oct 10, 2026
Backport #123683 to 26.8: Fix duplicated rows of `ANY RIGHT JOIN` with `any_join_distinct_right_table_keys` under parallel replicas
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 12, 2026
… JOIN` with `any_join_distinct_right_table_keys` under parallel replicas
JTCunning pushed a commit to JTCunning/ClickHouse that referenced this pull request Nov 3, 2026
The bugs behind their `no-parallel-replicas` / `no-replicated-database` tags are fixed on master:
- `01660_join_or_any`: ClickHouse#123683
- `04653_bloom_filter_fixed_string_constant_scalar`: ClickHouse#118988
- `04819_dynamic_aggregate_function_state_version`: ClickHouse#123649
- `04825_quantile_deterministic_alter_add_modify_column`: ClickHouse#115393, ClickHouse#123070
- `04494_atomic_populate_materialized_view_parallel_replicas`: ClickHouse#124477
- `04266_text_index_tokens_cardinality_order`: ClickHouse#124500 already disables parallel replicas for the log-checked query

Related: ClickHouse#124015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.8-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ANY RIGHT JOIN with OR and any_join_distinct_right_table_keys = 1 returns an extra row with parallel replicas

5 participants