Repository navigation
Allow DP join reordering with parallel replicas - #105889
Merged
Merged
Conversation
`QueryPlanOptimizationSettings` was forcing `query_plan_optimize_join_order_limit` to `0` whenever `allow_experimental_parallel_reading_from_replicas && max_parallel_replicas > 1`, silently skipping join reordering for every PR query. The DP optimizer itself has no parallel-replicas-specific paths, and the parallel-replicas plan builder in `buildQueryPlanForAutomaticParallelReplicas` constructs `QueryPlanOptimizationSettings` from the same context, so dropping the override gives both plan builds the same DP treatment as everything else. Add `04277_parallel_replicas_join_dp_reorder` as a regression test: a `small JOIN large` shape under PR — DP should reorder so the larger table ends up on the parallelized (left/probe) side. The previous behaviour would have preserved the user-written order. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Contributor
|
Workflow [PR], commit [078b228] Summary: ✅
AI ReviewSummaryThis PR removes an unconditional override that forced Final Verdict
|
The 2-table swap can also reach via `optimizeJoinLegacy`, so the original assertion wasn't actually exercising the DP path. Promote the test to a 3-way join and force `query_plan_optimize_join_order_algorithm='dpsize'` plus a non-trivial join-order limit, which exercises the `query_graph_size_limit > 2` code path in `optimizeJoinLogicalImpl`. Assert on the relevant invariant — that DP reordered, not on the specific shape DP picks. Under the stateless randomizer the cost model can pick different left-deep shapes (e.g. `[b, c, a]` vs `[c, b, a]`) depending on flips of `use_hash_table_stats_for_join_reordering`, `use_statistics`, etc.; what stays true across all of them is that the read-step order is not the user-written `[pr_dp_a, pr_dp_b, pr_dp_c]`. With the previous forced `query_plan_optimize_join_order_limit=0`, `optimizeJoinLogicalImpl` short-circuits and `optimizeJoinLegacy` is a no-op on `JoinStepLogical`, so the user order survives — the assertion catches the regression. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
The previous `groupArray(...) != [...]` workaround for variance comes from two distinct sources, both worth pinning explicitly: * `query_plan_optimize_join_order_randomize` injects random cardinalities/NDVs into the DP cost model when non-zero. It is currently `stress_test_only_settings` in `clickhouse-test`, so regular `--test-runs` see it at the default `0`, but pinning at the session level is cheap insurance against future randomizer changes. * `enable_join_transitive_predicates` is randomized true 30% of the time. With it on, the implicit `a.x = c.x` edge is materialized in the join graph; DP then picks a different left-deep shape (`[b, c, a]` instead of `[c, b, a]`). Pinning it off in the inner SETTINGS clause stabilizes the EXPLAIN order. With both pinned, the test asserts the precise DP-chosen read-step order `[pr_dp_c, pr_dp_b, pr_dp_a]`. Verified 30/30 randomized runs and that with `query_plan_optimize_join_order_limit=0` (the regression) the order collapses back to the user-written `[pr_dp_a, pr_dp_b, pr_dp_c]`. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
nickitat
marked this pull request as ready for review
May 27, 2026 11:07
antaljanosbenjamin
approved these changes
May 27, 2026
| CREATE TABLE pr_dp_c (x UInt32) ENGINE = MergeTree ORDER BY x AS SELECT number % 100 FROM numbers(100000); | ||
|
|
||
| SET | ||
| enable_parallel_replicas = 1, |
Member
There was a problem hiding this comment.
Actually AI had a good comment:
• Findings
- Medium: tests/queries/0_stateless/04277_parallel_replicas_join_dp_reorder.sql:22 does not pin automatic_parallel_replicas_mode = 0, and uses best-effort enable_parallel_replicas = 1. tests/clickhouse-test randomizes
automatic_parallel_replicas_mode to 2 25% of the time, and the analyzer disables enable_parallel_replicas when automatic mode is non-zero. In that run, the old buggy code would no longer force query_plan_optimize_join_order_limit = 0,
so this regression test can pass without exercising the intended parallel-replicas path. Add automatic_parallel_replicas_mode = 0; using enable_parallel_replicas = 2 or asserting the PR plan shape would make the coverage stronger.
Contributor
LLVM Coverage Report
Changed lines: 66.67% (4/6) · Uncovered code |
Member
|
@groenai add test as performance test tests/queries/0_stateless/04277_parallel_replicas_join_dp_reorder.sql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
DP JOIN reordering is now allowed with parallel replicas.
It looks like I really need it to implement support for JOINs with AutoPR. Otherwise, the local and distributed plans won't match in too many cases.