Repository navigation
Make automatic parallel replicas match plans for an INNER JOIN with plan-based parallel replicas - #127845
Make automatic parallel replicas match plans for an INNER JOIN with plan-based parallel replicas#127845devcrafter wants to merge 10 commits into
Conversation
…d runtime filters Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
Workflow [PR], commit [ac6f86e] Summary: ✅
AI ReviewSummaryPR #127845 restores plan matching for automatic parallel replicas with an Final VerdictStatus: ✅ Approve. |
There was a problem hiding this comment.
🟢 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
left a comment
There was a problem hiding this comment.
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 stays0 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, anINsubquery,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 aGROUP BY keysubquery 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 = 0the plan-based results equal the ones without parallel replicas, and the query condition cache hits and misses are the same on both builds.
nickitat
left a comment
There was a problem hiding this comment.
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.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units155 translation units recompiled, 1387 s compile time in total, 155 of them have a recent master baseline. |
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…-pre-join-actions
|
Thanks, checked by execution. QCC re-walk. This PR runs the merge after Other gates. Agreed, and they stay on Unrelated to this PR, the check did find a filter that loses its key, the one left after a partial move to |
|
@groeneai |
|
It is not caused by this PR. It is a race in |
With
parallel_replicas_plan_based, automatic parallel replicas never collected statistics for anINNER JOINthat gets a join runtime filter:AutoParallelReplicasloggedCannot 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_filtersoff 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 onjoin_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_addedto 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_joinand04340_autopr_input_bytes_estimation_joinpass with it.05292_autopr_stats_key_constantsstill differs there, for another reason (unused right-side join columns are pruned in the local plan only), left for a follow-up. The new test05452_pr_plan_based_autopr_inner_join_runtime_filterfails without the change.Related: #112351
Changelog category (leave one):
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/127845]