Repository navigation
Send a subquery with a redundant ORDER BY to parallel replicas as a whole - #125685
Conversation
… whole `findQueryForParallelReplicas` stopped at any `Sorting` step, so in `SELECT sum(...) FROM (SELECT ... ORDER BY k)` only the inner subquery went to the replicas: they sorted their rows (the local one reading in order), and the initiator merged the sorted streams just to aggregate them. `query_plan_remove_redundant_sorting` then dropped the merge on the initiator but not the sorting on the replicas. Remove the redundant sortings from the plan this decision is made on, the same way the plan the query runs with has them removed. The whole query then goes to the replicas and they aggregate, exactly as when the subquery has no `ORDER BY`. This also lets automatic parallel replicas match such queries: the single-node plan had no sorting, the parallel replicas plan did, so the hash of the replicas' part was never found in the single-node plan (`Cannot find step with matching hash`). `02967_parallel_replicas_joins_and_analyzer` covers a subquery with `ORDER BY` stopping the subquery from going to the replicas; the sortings there are redundant, so it now keeps them with `query_plan_remove_redundant_sorting = 0`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Workflow [PR], commit [31bee42] Summary: ✅
AI ReviewSummaryThe PR removes redundant sorting before choosing parallel-replica query boundaries, allowing replicas to aggregate sorted subqueries and enabling automatic plan matching for the query-based implementation. No unresolved findings remain at Final VerdictStatus: ✅ Approve. |
…in `05261` The redundant `ORDER BY` shape of `05395_autopr_nested_query_shapes` needs #125685, so it is commented out with a reference to it. `05261_autopr_transplant_analysis_table_order` reads two of its three tables whole on every replica, about half of what the query reads, and the duplicated-read limit merged into master since then declined it. Raise the limit in the test: what it covers is the pairing of reads. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… subquery Every case of `05394_parallel_replicas_redundant_subquery_order_by` ran with `automatic_parallel_replicas_mode = 0`, so none of them went through `considerEnablingParallelReplicas` and its plan-hash matching. Run the aggregation over the sorted subquery with `automatic_parallel_replicas_mode = 2` and check that `query_log` records nonzero `RuntimeDataflowStatisticsInputBytes` and `RuntimeDataflowStatisticsOutputBytes` for it. Without the fix both are 0 and the server logs `Cannot find step with matching hash`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units7 translation units recompiled, 14 s compile time in total, 7 of them have a recent master baseline. |
…dant-subquery-order-by
…ER BY` test The plan-based implementation decides on the optimized plan, where the redundant sorting is already gone, so it never had this problem. Its `EXPLAIN` shows `ReadFromParallelReplicas` instead of `ReadFromRemoteParallelReplicas`, and automatic parallel replicas does not match its plan even without an `ORDER BY`, so the test's checks print 0 under `parallel_replicas_plan_based = 1` while the results agree. Co-Authored-By: Claude Opus 5.5 <[email protected]>
groeneai
left a comment
There was a problem hiding this comment.
I reviewed it and the change looks correct to me. The decision now uses the optimizer's own redundancy check (tryRemoveRedundantSorting behind the same query_plan_enable_optimizations && query_plan_remove_redundant_sorting gate as QueryPlanOptimizationSettings.cpp:255), so a sorting stops blocking only when the initiator's real plan drops it anyway. The removal only relinks parent->children and the node stays in QueryPlan::nodes, so the QueryNode -> Node mapping cannot dangle.
What I ran (the PR applied to current master f03969e1, which has #119350 and #128556; debug build, 3 localhost replicas, parallel_replicas_plan_based = 0, 3M-row MergeTree):
- 28 sorted-subquery shapes return the same result as
enable_parallel_replicas = 0, also withparallel_replicas_local_plan = 0and withparallel_replicas_prefer_local_join = 1. The order-dependent ones keep the old decision: floatsum,any/anyLast,groupArray,rowNumberInAllBlocks, window functions,LIMIT BY,WITH FILL. - 05394 passes 5/5 with randomized settings, 02967 passes. On master
e6c0a8e3both new assertions read0 0, so they are not vacuous. - Perf, for the open bot question:
SELECT key % 13 AS g, count(), sum(value) FROM (SELECT key, value FROM t ORDER BY value DESC) GROUP BY gships 1,626,001 rows / 31.5 MB from the replicas to the initiator on master vs 26 rows / 1.8 KB with the PR; median 148 ms vs 17 ms over 5 runs (debug build, so only the ratio means anything).
One coverage note, not blocking. The effect is wider than the changelog entry: a sorting made redundant by an outer ORDER BY ... LIMIT, or one under a JOIN, also stops blocking, so those queries now go to the replicas whole. The JOIN case is exactly what the new SET in 02967 pins to the old path. With that line removed, every 02967 result row still matches the reference and only the four "ORDER BY in sub1/sub3" EXPLAIN blocks change, so nothing checks the JOIN case at default settings. One JOIN query in 05394, compared against enable_parallel_replicas = 0, would cover it.
| SET optimize_move_to_prewhere = 1; | ||
| SET enable_join_runtime_filters = 1, join_runtime_filter_min_probe_rows = 0; -- Changes the plan (BuildRuntimeFilter node) | ||
| SET use_statistics = 0; -- auto_statistics_types changes the plan | ||
| SET query_plan_remove_redundant_sorting = 0; -- the ORDER BY in a subquery below is redundant, and a removed one does not stop the subquery from going to the replicas |
There was a problem hiding this comment.
Not clear why we do it here. Could you explain intention? Also, this way we are losing default behavior coverage with enabled query_plan_remove_redundant_sorting
There was a problem hiding this comment.
It kept the "ORDER BY in sub3/sub1" cases testing the WithMergeableState boundary at the sorting, which this PR removes when the sorting is redundant. You are right that it dropped the default for the whole test: it is back to the default now, and the plans for those two cases show the query going to the replicas whole. 05394_parallel_replicas_redundant_subquery_order_by covers the case with query_plan_remove_redundant_sorting = 0.
…d parallel replicas in the tests `02967_parallel_replicas_joins_and_analyzer` turned `query_plan_remove_redundant_sorting` off for every query, so the default behaviour was not covered. Keep the default, where the redundant `ORDER BY` in sub3/sub1 no longer stops the query from going to the replicas whole, and add an `EXPLAIN` with the setting off for each of the two cases to keep the `WithMergeableState` boundary at the sorting covered. `05394_parallel_replicas_redundant_subquery_order_by` no longer pins `parallel_replicas_plan_based = 0`: it checks for `MergingAggregated` on the initiator, which both implementations show when the replicas aggregate. Only the automatic mode case stays on the query-tree implementation, because the plan-based one does not match that plan yet (`Cannot find step with matching hash`, independent of this change). #125685 (comment) #125685 (comment) Related: #128413 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ndant_subquery_order_by` Related: #131089 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…2967_parallel_replicas_joins_and_analyzer` `05394_parallel_replicas_redundant_subquery_order_by` already covers the sorting that is not removed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
|
#125685 is merged, so the redundant `ORDER BY` shape of `05395_autopr_nested_query_shapes` is back. `05261_autopr_transplant_analysis_table_order` compared only the selected marks, which a read handed another read's analysis can leave unchanged: the filter selects as many marks of `t_order_mid` as of `t_order_big`. Print the result of every run too, so such a read shows as wrong rows. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Backport #125685 to 26.10: Send a subquery with a redundant `ORDER BY` to parallel replicas as a whole
…` to parallel replicas as a whole
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Improve parallel replica execution for queries that aggregate over subqueries with redundant ORDER BY clauses by allowing replicas to perform the aggregation. Also enable automatic parallel replica plan matching for these queries.
Workflow [PR]
Sync PR [sync-upstream/pr/125685]
Version info
26.11.1.2135-master(included in26.11and later)