Skip to content

Merge the parallel replicas local plan like the single-node plan after join runtime filters - #127628

Merged
nickitat merged 2 commits into
masterfrom
autopr-local-plan-merge-expressions
Nov 3, 2026
Merged

nickitat merged 2 commits into
masterfrom
autopr-local-plan-merge-expressions

Conversation

@nickitat

@nickitat nickitat commented Oct 23, 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):

Automatic parallel replicas (automatic_parallel_replicas_mode) now collects statistics for queries that join an aggregated subquery whose filter goes to PREWHERE. Previously it could not match the two plans for such queries and built the plan with parallel replicas on every execution without ever using it.


Related: #118265
Related: #118657

Pushing a join runtime filter into the subquery splits the expression it passes through: the part the filter needs goes below it, the rest stays above. PREWHERE then takes the filter, and the split part is left as an Expression of its own. After the runtime filters are added, optimizeTreeSecondPass merges expressions and filters over the whole plan, so the single-node plan folds that part back in.

With classic parallel replicas, the initiator's share of the read is an opaque ReadFromLocalParallelReplicaStep while that merge runs. The local plan is optimized later, on its own. It has no joins, so it never gets the merge, and when it is put back only the step where it meets the outer plan is merged. The extra step changes the cache key of every step above it, so the boundary of the replicas plan (the partial Aggregating) has no counterpart in the single-node plan.

plan with parallel replicas (local branch)         single-node plan
Aggregating                                        Aggregating
└─Expression ((Before GROUP BY + ...))             └─Expression ((... Before GROUP BY + ...) + (...)[split])
  └─Expression ((...)[split])                        └─ReadFromMergeTree (lineitem)
    └─ReadFromMergeTree (lineitem)                       Prewhere: l_returnflag = 'R' AND runtime filter
        Prewhere: l_returnflag = 'R' AND runtime filter

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

Version info

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

nickitat and others added 2 commits October 23, 2026 18:37
…r join runtime filters

Pushing a join runtime filter into an aggregated subquery splits the expression it passes through, and
once PREWHERE takes the filter, the split part is left as an `Expression` of its own. After the runtime
filters are added, `optimizeTreeSecondPass` merges expressions and filters over the whole plan, so the
single-node plan folds that part back. With classic parallel replicas the initiator's share of the read
is an opaque `ReadFromLocalParallelReplicaStep` while that merge runs, and the local plan is optimized
later on its own. It has no joins, so it never gets the merge, and when it is put back only the step
where it meets the outer plan is merged.

The extra step changes the cache key of every step above it, so automatic parallel replicas cannot find
the boundary of the replicas plan (the partial `Aggregating`) in the single-node plan. It counts
`AutoParallelReplicasPlansNotComparable`, collects no statistics, and builds the plan with parallel
replicas again on every run. On staging TPC-H sf=1 this made a05, a07 and a10 (aggregation pushed into
a filtered `lineitem` subquery, then joined) 1.18-1.33x slower than single-node while never using
replicas; locally all three match after this change and decide from statistics from the second run,
while master still fails to match them.

The local plan now gets the same merge loop when the outer plan added runtime filters.

Two related mismatches remain and are pinned in the test: the single-node plan can skip the runtime
filter for a probe side it estimates below `join_runtime_filter_min_probe_rows` (the replicas plan
cannot estimate through `MergingAggregated`), and with `query_plan_merge_filters = 0` the runtime
filter stays a `Filter` of its own in the local branch. Both fail on master as well.

Related: #118265

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017LMx8PkGBrowNrHvRMw4xi
Each pin was flipped on its own against the fix and against master. Kept are the ones the fix needs
(`join_runtime_filter_min_probe_rows`, `query_plan_merge_filters`) and the ones without which the test
stops reproducing the problem on master (`optimize_move_to_prewhere`, `enable_join_runtime_filters`,
`parallel_replicas_plan_based`). The join order, join algorithm, spilling and table granularity pins
changed nothing either way and are dropped. 300 randomized runs pass; master fails every run.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017LMx8PkGBrowNrHvRMw4xi
@clickhouse-gh

clickhouse-gh Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9b652ce]

Summary: ✅


AI Review

Summary

PR #127628 merges expression and filter steps throughout the restored local parallel-replica plan after join runtime filters are added. This lets automatic parallel replicas match the corresponding single-node aggregation and collect statistics instead of repeatedly building and discarding an incomparable plan. No actionable findings.

Missing context / blind spots
  • ⚠️ The broader CI workflow is still running. The new regression test, 05446_autopr_local_plan_merged_like_single_node, passed in Fast test. The author's reported baseline comparisons and randomized runs were not repeated locally; completed functional and sanitizer jobs would close the remaining validation gap.
Final Verdict

Status: ✅ Approve. No changes requested.

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). labels Oct 23, 2026
@nickitat
nickitat marked this pull request as ready for review November 2, 2026 16:35
@nickitat
nickitat requested a review from yariks5s November 2, 2026 16:39
@yariks5s yariks5s self-assigned this Nov 2, 2026
@nickitat
nickitat added this pull request to the merge queue Nov 3, 2026
Merged via the queue into master with commit c05e831 Nov 3, 2026
178 checks passed
@nickitat
nickitat deleted the autopr-local-plan-merge-expressions branch November 3, 2026 16:47
@robot-clickhouse robot-clickhouse added 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 labels Nov 3, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 3, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 3, 2026
Backport #127628 to 26.10: Merge the parallel replicas local plan like the single-node plan after join runtime filters
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…e the single-node plan after join runtime filters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). 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 pr-performance Pull request with some performance improvements 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.

4 participants