Repository navigation
Cherry pick #120110 to 26.10: Make DPsub the default join order algorithm, with the CD-C conflict detector - #127578
Merged
robot-ch-test-poll2 merged 56 commits intoNov 5, 2026
Conversation
…ults by DPSub are nevertheless correct.
Two conflicts, both from master reorganizing around our changes: - `optimizeJoin.cpp`: master dropped the `AggregatingStep` and `CommonSubplanReferenceStep` includes along with the code that used them, and moved `joinOrder.h` into its sorted block. Kept master's block and added `joinOrderAlgorithms.h` to it, which we need for `DPSUB_MAX_RELATIONS`. - `05054_cascades_aggregation_pushdown_join_subtree.sql`: master removed the old-analyzer test mode and with it the whole tag line, while we had added `no-random-settings` to it. Kept `no-random-settings` and dropped the obsolete `no-old-analyzer`.
Master moved the release to 26.10, so the two defaults this change flips have to be recorded there: the settings history is keyed on the version that ships the change, and `compatibility` reads it to hand back the older value. The 26.9 block goes back to what it says on master. It records what 26.9 shipped, which is `query_plan_optimize_join_order_use_conflict_detector_c` still off - our flipping it to on belongs to 26.10, not to a release that already went out.
The test proves that a satisfied `LIMIT` stops the stages upstream of it, using a scan that sleeps a second per block: if the stop signal does not reach it, the scan runs on and `max_execution_time` aborts the query. That only works while the sleeping scan sits on the probe side of the top join. A hash join reads its whole build side before it emits a row, so a sleeping scan placed there cannot be stopped by a `LIMIT` above it. The test already guards the one route it knew about, `query_plan_join_swap_table`, but `dpsub` gets there by a different one: it reorders the join so the scan's subtree is the top join's right input, and the query times out after 25 seconds instead of finishing in 2. Pin the algorithm alongside the settings the test already pins for the join order. This test is about the stop signal crossing idle exchanges, not about which order the optimizer picks.
…conflict-detector # Conflicts: # src/Core/Settings.cpp # src/Core/SettingsChangesHistory.cpp # src/Processors/QueryPlan/Optimizations/optimizeJoin.cpp # tests/queries/0_stateless/05024_distributed_plan_limit_early_stop_idle_sink.sql
A join the optimizer has already settled enters its parent's graph as a single relation, and the row count it arrived at is the only estimate that relation has. `estimateReadRowsCount` returned early for any node with two children, so the parent saw nothing and `computeJoinCost` read the missing count as one row through `value_or(1)` - the largest input in the query looked like the cheapest thing to join. Read the estimate before that check. On TPC-DS q72 the boundary is real: the non-equi predicate `inv_quantity_on_hand < cs_quantity` splits `inventory` and `catalog_sales` into their own graph, which estimates itself at 945,072,914 rows against a true 944,916,882 - 0.02% off - and the parent then planned eight relations around it as if it returned one row. With column statistics the query goes from 11.6s to 0.8s, and under `dpsub` with the CD-C conflict detector from 72.7s to 1.0s: the detector widens the search space, so a wrong estimate buys more wrong plans. Measured over the full 99-query TPC-DS suite at scale factor 1 with column statistics, no other query changes. The relation name of such a sub-join is a chain that already carries its own inputs' estimates, so it is marked composite and `EXPLAIN` no longer appends a second estimate to it. This restores #97114, reverted in #99957 for a suspected performance regression. On tables without column statistics the estimate it propagates is itself unreliable, and there the change is a wash across TPC-DS - 18 queries faster, 11 slower. #101398 proposes gating propagation on a trust flag. Related: #97114 Related: #99957 Related: #101398
…imate Both tests assert plan shapes that change once an already-optimized sub-join passes its row estimate to the parent join graph. `03837_subquery_ndv_propagation_join_order` is the direct case, and the update is the behaviour it was written to assert: the subquery input was printed as `sq[no_stats~?]` with `ResultRows: unknown`, and now carries the 50000 rows its own join settled on, which also reorders the two dimensions above it. `03836_tpch_join_order_plans` changes in 2 of its 25 plans, Q02 and Q20, both of which join through a correlated subquery. It pins `query_plan_optimize_join_order_algorithm = 'dpsize,greedy'`, so these are the estimate alone, not the `dpsub` default. The other 23 plans are unchanged.
…conflict-detector Resolve the conflict with the move of the settings changes history into the setting declarations (ee6b44f): the history records this PR added to `SettingsChangesHistory.cpp` now live as trailing arguments of the `DECLARE` of each setting. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…elation as build side
…conflict-detector # Conflicts: # src/Processors/QueryPlan/Optimizations/optimizeJoin.cpp # tests/queries/0_stateless/03836_tpch_join_order_plans.reference
`buildPhysicalPlan` puts the smaller input on the build side, mirroring outer joins through `reverseJoinKind`. DP entries are built as `All` whatever the graph's strictness, which `chooseJoinOrder` stamps on later, so an `ANY LEFT JOIN` was turned into an `ANY RIGHT JOIN`. That changes the result (one match per left row vs. per right row) and bypasses the checks of the swap in `optimizeJoin`, e.g. for `join_any_take_last_row`. Carry the graph's strictness on `QueryGraph` and only turn joins round when it is `All`; the swap in `optimizeJoin` still decides for the rest. Fixes `04670_any_join_take_last_row_no_swap`, `04330_cascades_activation_gating` and `02302_join_auto_lc_nullable_bug` in Fast test: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=120110&sha=6820e959c2e315db8400b1e206dec7f785147785&name_0=PR #120110 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…conflict-detector
`buildPhysicalPlan` turned a join round only when both inputs had an estimate, so a join over an input without one (e.g. a `UNION ALL` subquery) kept whatever sides the enumeration reached first. In TPC-DS Q5 that read the whole sales subquery into the hash table and probed it with `date_dim`, making the query about 40% slower than with greedy. Take an input without an estimate to be the larger one, and keep the input with more relations on the left when neither is known, which gives the left-deep plans greedy produces. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=120110&sha=bed198340b7adbb7879231887f6b40a117045d50&name_0=PR #120110 Co-Authored-By: Claude Opus 5.5 <[email protected]>
… perf test The hash table sizes cached for join reordering are measured under the plan of the previous run, after its runtime filters, so they can flip the join order on every other run. Q64 then alternated between two plans, one of them more than 2x slower, which the comparison reported as a 3x slowdown. Pin `use_hash_table_stats_for_join_reordering = 0` so every run plans from the same estimates. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=120110&sha=bed198340b7adbb7879231887f6b40a117045d50&name_0=PR #120110 Co-Authored-By: Claude Opus 5.5 <[email protected]>
With an input without a row estimate kept on the probe side, DPsub now plans `03580_join_runtime_filter_pushdown` and `03721_aggregate_projection_actions_dag` left-deep, like greedy, instead of reading a whole join subtree into the hash table. Both tests assert on the plan, and CI randomizes `query_plan_join_swap_table`: pinning it keeps the sides the join order search reached first, so pin it to `auto` next to the other settings they already pin for this reason. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The previous change took an input without an estimate to be the larger one. A missing estimate says nothing about the size: in `04630_prewhere_subcolumn_guard_throwing_expression` it is `numbers(50)`, which then became the probe side and read the 66666-row side into the hash table, so the runtime filter no longer reached that side's read; in `03312_squashing_with_low_card_mem_usage` the large table became the `parallel_hash` build side and the query ran past its memory limit. When an estimate is missing, only keep the input with more relations on the left, which is what TPC-DS Q5 needs, and leave two single relations as they are. Also initialize the side choice in one expression, which `clang-tidy` requires (`cppcoreguidelines-init-variables`). https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=120110&sha=97f219f781a12fe8f31abff83cdae65ddea71bc2&name_0=PR #120110 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…conflict-detector # Conflicts: # tests/queries/0_stateless/03580_join_runtime_filter_pushdown.reference
…conflict-detector
The PR was not in 26.10, so the default changes of `query_plan_optimize_join_order_algorithm` and `query_plan_optimize_join_order_conflict_detector` belong to 26.11, as the `settings_changes_history` check requires. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…conflict-detector # Conflicts: # tests/queries/0_stateless/03580_join_runtime_filter_pushdown.reference
…conflict-detector # Conflicts: # src/Processors/QueryPlan/Optimizations/joinOrderDPSub.cpp
…conflict-detector # Conflicts: # tests/queries/0_stateless/03836_tpch_join_order_plans.reference
…g` to 05441 The number was taken by tests merged from master in the meantime. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t-detector Make DPsub the default join order algorithm, with the CD-C conflict detector
Contributor
|
Dear @ahmadov, @fkastrati, @rschu1ze, this cherry-pick PR has not been updated for 3d0h0m12s. Please resolve the conflicts to backport #120110, or close this PR if the backport is no longer needed. This PR will be automatically closed after 7 days of inactivity. |
Contributor
|
Dear @ahmadov, @fkastrati, @rschu1ze, this cherry-pick PR has not been updated for 7d0h8m33s. Closing automatically. If you still want to backport #120110, please resolve the conflicts and reopen this PR. |
Contributor
|
Dear @ahmadov, @fkastrati, @rschu1ze, this PR is opened while #120110 was marked as backported. The If the cherry-pick is not needed anymore, then just close this PR. |
Resolve the conflicts of backporting #120110 to 26.10: - `joinOrderDPSub.cpp`: take the log message of #120110. - `03836_tpch_join_order_plans.reference`: regenerated on a 26.10 build with #120110; it differs from the 26.10 reference only in the plans of Q2 and Q20, the two that #120110 changes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rted `compatibility = '26.9'` keeps `greedy` without a conflict detector, while 26.10 and later default to `dpsub,greedy` with CD-C. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Original pull-request #120110
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Before you resolve anything
Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job