Skip to content

Make automatic parallel replicas match plans for an INNER JOIN with plan-based parallel replicas - #127845

Open
devcrafter wants to merge 10 commits into
masterfrom
plan-based-pr-merge-pre-join-actions
Open

devcrafter wants to merge 10 commits into
masterfrom
plan-based-pr-merge-pre-join-actions

Conversation

@devcrafter

@devcrafter devcrafter commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

With parallel_replicas_plan_based, automatic parallel replicas never collected statistics for an INNER JOIN that gets a join runtime filter: AutoParallelReplicas logged Cannot find step with matching hash ... in single-node plan, so no estimate was made and the query was never sent to the replicas.

The local plan of plan-based parallel replicas is cloned while its joins are still logical and the runtime filters are already in it, then optimized once more with enable_join_runtime_filters off so they are not added twice. That pass converts the joins all the same, but the merge of the expressions the conversion adds (Left Pre Join Actions) is gated on join_runtime_filters_were_added, which is local to the pass and stayed false. The left input of the join then kept two expression steps where the single-node plan has one merged step, and the two hash differently.

The outer pass now passes join_runtime_filters_already_added to the local plan, and the post-conversion merge runs when either is set.

Found with #112351 (plan-based parallel replicas on by default): 04341_autopr_mode1_apply_join, 04305_autopr_output_bytes_estimation_join and 04340_autopr_input_bytes_estimation_join pass with it. 05292_autopr_stats_key_constants still differs there, for another reason (unused right-side join columns are pruned in the local plan only), left for a follow-up. The new test 05452_pr_plan_based_autopr_inner_join_runtime_filter fails without the change.

Related: #112351

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

🤖 Generated with Claude Code


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

…d runtime filters

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ac6f86e]

Summary: ✅


AI Review

Summary

PR #127845 restores plan matching for automatic parallel replicas with an INNER JOIN and runtime filters by merging expressions after join conversion in the inherited local plan. Reviewed head ac6f86e3f7875984760066948008c228cf74f97a; no actionable findings remain. The focused regression test passes in the current head's Fast test report.

Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Oct 26, 2026
@devcrafter
devcrafter requested review from groeneai and nickitat and a balanced review from Copilot October 26, 2026 14:46
@devcrafter devcrafter added the comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). label Oct 26, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The narrowly scoped change restores existing plan-normalization behavior, with focused regression coverage and no identified blocking issues.

No open issues 🎉

What changed in this PR

This PR fixes plan matching for automatic parallel replicas when an INNER JOIN has runtime filters, allowing statistics collection with plan-based parallel replicas.

Changes:

  • Tracks runtime filters inherited from an outer optimization pass.
  • Runs the existing post-conversion expression merge for inherited filters, keeping local and single-node plans comparable.
  • Adds a stateless regression test checking plan comparability and output-byte statistics for both replica implementations.
File Description
tests/​queries/​0_stateless/​05452_pr_plan_based_autopr_inner_join_runtime_filter.sql Tests plan matching and statistics collection with join runtime filters.
tests/​queries/​0_stateless/​05452_pr_plan_based_autopr_inner_join_runtime_filter.reference Records successful comparability and statistics checks.
src/​Processors/​QueryPlan/​Optimizations/​QueryPlanOptimizationSettings.h Adds internal state indicating inherited runtime filters.
src/​Processors/​QueryPlan/​Optimizations/​optimizeTree.cpp Propagates filter state and enables the required expression merge.

Review effort: Balanced

@groeneai groeneai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed cb89393 on a local debug build, A/B against the same tree with the optimizeTree.cpp hunk reverted.

  • 05452 matches its reference with the change. Without it the plan-based row is 1 0, while the query-based row stays 0 1.
  • 15 more join queries in mode 2: the 11 that were not comparable without the change are all comparable with it (aggregation above the join, a right-side column in the output, two joins on independent and on chained keys, a left-side WHERE, a filtered right-side subquery, an IN subquery, query_plan_merge_expressions = 0, query_plan_merge_filters = 0, use_query_condition_cache = 1). The 4 that were already comparable stay so: LEFT JOIN, enable_join_runtime_filters = 0, ORDER BY key LIMIT 10, and a join above a GROUP BY key subquery of the parallelized table.
  • That last query covers passing the plan-wide flag: its join stays on the initiator above MergingAggregated, and only the pushed-down runtime filter is inside the local plan. The single-node pass runs this merge over the whole plan there as well, so the plans still match.
  • With automatic_parallel_replicas_mode = 0 the plan-based results equal the ones without parallel replicas, and the query condition cache hits and misses are the same on both builds.

@nickitat nickitat left a comment

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.

Your instinct is right, but the fix isn't simply seeding one flag from the other. The two names stand for two different facts, and the PR already has the "forgot one place" bug you're worried about.

Seeding join_runtime_filters_were_added from the setting isn't a drop-in fix. The local variable gates two kinds of work:

1. Work that should run only when this pass inserted the filters:
   - The push-down re-run at optimizeTree.cpp:400-441. It uses rewrite_regardless_of_settings, which overrides merge_filters=0, filter_push_down=0 and similar.
   - collectAppliedJoinRuntimeFilters at :438. This one matters. The local plan's ReadFromMergeTree is a clone, and clone already copies the descriptors through copyJoinRuntimeFilterIndexAnalysisDescriptors (ReadFromMergeTree.cpp:4984). addJoinRuntimeFilterIndexAnalysisOnDataRead just does push_back with no dedup. So running it a second time would register every descriptor twice.
2. Work that should run whenever the plan contains runtime filters:
   - The merge after convertLogicalJoinToPhysical at :559, which is the line you're asking about.
   - The QCC re-walk at :933.

:933 is a missed spot. Its own comment says "The merges above rebuild FilterSteps and drop the QCC key, so re-walk to set it again". It's gated only on join_runtime_filters_were_added || predicates_were_propagated || lazy_materialization_applied. With this PR, the local plan now runs the :559 merges because of join_runtime_filters_already_added, but it doesn't redo the re-walk. So the query condition cache key is probably lost on the rebuilt filters in the initiator's local plan. I haven't confirmed this by running it, so it should be checked or tested.

What I'd suggest in the review: keep two booleans, each named for what it means, and compute the second one once:

bool join_runtime_filters_were_added = false;   // inserted by this pass: push-down, index-analysis registration, TopK QCC
...
/// The plan carries join runtime filters, whether added above or by an outer optimization
/// (the local plan of plan-based parallel replicas).
const bool plan_has_join_runtime_filters = join_runtime_filters_were_added || optimization_settings.join_runtime_filters_already_added;

Then use plan_has_join_runtime_filters at :559 and :933, and leave the others on join_runtime_filters_were_added. That way the || appears once. The open question for the author is which group the TopK QCC disabling at :445 belongs to. That depends on whether clone keeps the disabled-cache state, which I didn't check.

@clickhouse-gh

clickhouse-gh Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing ac6f86e3f with master a7f713612 (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.

Compile time of recompiled translation units

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

Job report

@devcrafter

Copy link
Copy Markdown
Member Author

Thanks, checked by execution.

QCC re-walk. This PR runs the merge after convertLogicalJoinToPhysical in the local plan only if the local plan inherited runtime filters, which means the outer pass added them, so the outer pass's re-walk runs too. The local plan is optimized separately, so this merge can drop QCC keys there, but it is spliced into the outer plan before that re-walk, which sets the keys of its filters again. I confirmed this with a debug build that drops every QCC key in the local plan right after its optimize: they were all back after the outer re-walk. I renamed the flag to parallel_replicas_local_plan_has_join_runtime_filters, since only that local plan sets it, and noted this in its comment.

Other gates. Agreed, and they stay on join_runtime_filters_were_added. The clone already carries the index-analysis descriptors and allow_top_k_prewhere_query_condition_cache.

Unrelated to this PR, the check did find a filter that loses its key, the one left after a partial move to PREWHERE: #128229

@devcrafter
devcrafter requested a review from nickitat October 31, 2026 09:38
@devcrafter

Copy link
Copy Markdown
Member Author

@groeneai 05339_string_filters_parquet_delta_length_byte_array test is failing, is there any fix for it?

@groeneai

Copy link
Copy Markdown
Collaborator

It is not caused by this PR. It is a race in StringValueFilter statistics: two row-group decoders update the counters at once, one of them reads checked without the matching replaced and turns the filter off mid-query (hence StringValueFilterValuesChecked: 26384 instead of 30000 here). The fix is #128277 (open, CI green, awaiting review).

This branch has not been deployed

No deployments
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-not-for-changelog This PR should not be mentioned in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants