Skip to content

Do not shard a join by primary-key ranges over bucketed distributed reads - #130887

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-04502-shard-by-pk-bucketed-read
Nov 12, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-04502-shard-by-pk-bucketed-read

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

optimizeJoinByShards (query_plan_join_shard_by_pk_ranges, enabled by default) re-splits the reads of a merge join into primary-key layers, taking the ranges from selectRangesToRead, which covers the whole table. On a worker of a distributed plan (make_distributed_plan), a ReadFromMergeTree with distributed read buckets must read only the marks pinned to its bucket. So every task of the parallel (probe) side of a broadcast join read all rows, and the join result was multiplied by the number of tasks. Now optimizeJoinByShards skips bucketed reads, like the projection, lazy FINAL and text index optimizations already do.

04502_distributed_plan_one_bucket_exchange failed on master because of this when query_plan_optimize_join_order_randomize was randomized: the randomized estimates make the cascades optimizer pick a broadcast merge join (ParallelRead ⋈ ReplicatedRead) instead of a local join. The query returned 27445000 instead of 13722500.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=64868e0d5c8e0c8c3cac1d2fed24489d30c4c5d4&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_release%2C%20parallel%2C%202%2F2%29

Checked with clickhouse local: the new test returns 1500 rows instead of 500 on master 64868e0d5c8 with 3 workers, and the correct result with the fix.

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):

Fix duplicated rows from a merge join of a distributed query plan (make_distributed_plan) when query_plan_join_shard_by_pk_ranges is enabled.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.2096-master (included in 26.11 and later)

…eads

`optimizeJoinByShards` (`query_plan_join_shard_by_pk_ranges`) re-splits the reads of a
merge join into primary-key layers, taking the ranges from `selectRangesToRead`, which
covers the whole table. On a worker of a distributed plan, a `ReadFromMergeTree` with
distributed read buckets must read only the marks pinned to its bucket, so every task of
the parallel (probe) side of a broadcast join read all rows, and the result was
multiplied by the number of tasks. Skip such reads, like other plan optimizations do.

The test `04502_distributed_plan_one_bucket_exchange` hit this with randomized
`query_plan_optimize_join_order_randomize`: the randomized estimates make the cascades
optimizer choose a broadcast merge join instead of a local join.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=64868e0d5c8e0c8c3cac1d2fed24489d30c4c5d4&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_release%2C%20parallel%2C%202%2F2%29

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4ff302c]

Summary: ✅


AI Review

Summary

The change prevents optimizeJoinByShards from replacing bucketed ReadFromMergeTree reads with whole-table primary-key layers, preserving the coordinator's pinned marks and avoiding duplicated distributed join results. No actionable findings in PR head 4ff302c19ac0cc4f038822128782f1817bbac491. The three-worker regression test checks both row count and aggregate value and passes in Fast test.

Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... labels Nov 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 4ff302c19 with master b666e1ef2 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.63, programs/clickhouse-keeper ×0.67 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 5.8 s 14.0 s +10.3 s (+282%)
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 3.3 s 7.4 s +5.3 s (+255%)
Compile time of recompiled translation units

7 translation units recompiled, 16 s compile time in total, 7 of them have a recent master baseline.

Job report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

@alexey-milovidov alexey-milovidov self-assigned this Nov 12, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 12, 2026
Merged via the queue into master with commit 489506b Nov 12, 2026
203 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-04502-shard-by-pk-bucketed-read branch November 12, 2026 13:26
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 12, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 22, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... 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.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants