Skip to content

Allow DP join reordering with parallel replicas - #105889

Merged
nickitat merged 5 commits into
masterfrom
parallel_replicas_join_dp_reorder
May 28, 2026
Merged

nickitat merged 5 commits into
masterfrom
parallel_replicas_join_dp_reorder

Conversation

@nickitat

@nickitat nickitat commented May 26, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

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.

`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]>
@clickhouse-gh

clickhouse-gh Bot commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [078b228]

Summary: ✅


AI Review

Summary

This PR removes an unconditional override that forced query_plan_optimize_join_order_limit = 0 when parallel replicas were enabled, and adds a focused stateless regression test that now pins automatic_parallel_replicas_mode = 0 and exercises the DP join-order path with a 3-way join. After reviewing the current diff, surrounding optimizer paths, and all prior thread replies, I did not find any remaining contract violations or new high-signal issues.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label May 26, 2026
nickitat and others added 3 commits May 26, 2026 16:46
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
nickitat marked this pull request as ready for review May 27, 2026 11:07
@antaljanosbenjamin antaljanosbenjamin self-assigned this 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,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

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.

Indeed, thanks.

@clickhouse-gh

clickhouse-gh Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 66.67% (4/6) · Uncovered code

Full report · Diff report

@nickitat
nickitat added this pull request to the merge queue May 28, 2026
Merged via the queue into master with commit ee45070 May 28, 2026
166 checks passed
@nickitat
nickitat deleted the parallel_replicas_join_dp_reorder branch May 28, 2026 09:45
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 28, 2026
@fm4v

fm4v commented Jun 1, 2026

Copy link
Copy Markdown
Member

@groenai add test as performance test tests/queries/0_stateless/04277_parallel_replicas_join_dp_reorder.sql

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants