Repository navigation
fix: the physical planner runs EnsureRequirements again after a rule breaks a requirement - #27648
jayzhan211 wants to merge 1 commit into
Conversation
…breaks a requirement
| { | ||
| // Repair only: added parallelism could split the input of a fetch. | ||
| let mut config = session_state.config_options().clone(); | ||
| config.optimizer.enable_round_robin_repartition = false; |
There was a problem hiding this comment.
This repair also drops parallelism far from the broken node. ensure_distribution strips existing repartitions on every node and, with round-robin off, doesn't add them back. With target_partitions = 4 and RemoveSorts appended:
SELECT b, c, row_number() OVER (ORDER BY b) FROM
(SELECT value % 7 AS b, count(*) AS c FROM generate_series(1, 1000000)
WHERE value > 5 GROUP BY value % 7)the sort comes back, but the RoundRobinBatch(4) under the filter and partial aggregate is gone (Hash(...), input_partitions=1), so that part now runs on one partition.
| /// them: either `EnsureRequirements` itself, or the rule after which the | ||
| /// requirements stopped holding. To find it, `optimizers` run again from `input_plan`, so | ||
| /// planning that succeeds keeps no intermediate plans. | ||
| fn sanity_check_error( |
There was a problem hiding this comment.
The replay doesn't repair, so it can name the wrong rule. With RemoveSorts (repaired in the loop) followed by a rule whose break can't be repaired, the error says stopped holding after physical optimizer rule 'remove_sorts'. My second rule was contrived (a node that ignores new children), but the loop already knows which rule's repair failed, so recording it there would avoid the replay.
| Box::new(e), | ||
| ) | ||
| })?; | ||
| enforced = input_requirements_hold(&new_plan); |
There was a problem hiding this comment.
The observer below then sees the repaired plan under the rule that broke it, so EXPLAIN VERBOSE shows physical_plan after remove_sorts: SAME TEXT AS ABOVE (with the SortExec) and the repair is invisible. Could the repair be reported as its own EnsureRequirements step?
|
Thank you for working on this. I got a question:
Why are we moving I don't think we need to enforce a rule that all optimizations must run after
So the question is: what additional requirements motivate moving |
One thing it would allow is for JoinSelection to maintain the invariant of "The input must be an executable plan, and the output is also an executable plan" Since it runs today before EnsureRequirements, it can't make assumptions of the input plans |
| let mut new_plan = Arc::clone(&plan); | ||
| let optimizer_context = SessionOptimizerContext::new(session_state); | ||
| let enforcement = EnsureRequirements::new(); | ||
| // Whether the requirements held after enforcement ran. Once they have, a rule |
There was a problem hiding this comment.
Isn't this just going to cause confusion as plans may work even with broken optimizer rules (this code will repair them)? That feels like it will go against the idea of separating out the "make the plans valid" from the "make a valid plan faster" ideas 🤔
There was a problem hiding this comment.
Agreed. Fixing the two rules that break requirements after EnsureRequirements looks like a better choice than this PR. On the sqllogictest suite, when I move each rule that runs before EnsureRequirements (other than OutputRequirements) to after it, only JoinSelection and WindowTopN break a requirement. With each of them fixed to keep the requirements it finds satisfied (#27771, #27772):
JoinSelectionafterEnsureRequirements: 293 planning errors → 1WindowTopNafterEnsureRequirements: 1 → 0, and a query that put the NULL row first forORDER BY pknow returns it last- no plan change in the default order
Each rule then turns a valid plan into a valid plan, so a broken rule still fails planning with its name, and the physical planner needs no repair step after each rule, so the lost parallelism @zhuqi-lucas found can't happen.
If you agree, I'll close this PR in favor of those two.
The default order doesn't change. The goal is that no rule implicitly assumes it runs before Thanks @2010YOUY01, @alamb and @zhuqi-lucas for the reviews. #27771 ( |
Which issue does this PR close?
Rationale for this change
Every physical optimizer rule after
EnsureRequirements, including rules a project appends to the default list, must keep the plan's order and distribution requirements, or planning fails with aSanityCheckPlanerror. So a rule only works where the rules before it left a plan it handles. Running the sqllogictest suite withJoinSelectionmoved afterEnsureRequirements(with a local harness, not part of this PR), 293 records fail with that error on main at c5361f0; with this PR, 0.This PR makes the physical planner repair such a plan: when a rule after
EnsureRequirementsreturns a plan whose requirements no longer hold, the planner runsEnsureRequirementsagain before the next rule. In the default rule order no rule breaks a requirement, so nothing runs again and no plan changes; the checks add about 1–2% to planning time.What changes are included in this PR?
DefaultPhysicalPlanner::optimize_physical_plan(start reading here): after each rule that followsEnsureRequirements, it checks the nodes the rule changed. If a requirement broke, it runsEnsureRequirementswith round-robin repartitioning and file-scan splitting off, so the repair adds only what a requirement needs, not extra parallelism that could split a limit's input.input_requirements_hold_sinceinsanity_checker.rs(#[doc(hidden)]): skips nodes shared with the previous plan (pointer identity), so the check walks only what the rule changed.optimizer.rs. The error naming the rule that broke the requirements now only appears when the repair can't restore them.The decision I'd most like reviewed: repairing right after the rule that broke a requirement, not once after the last rule, so every later rule still gets a plan whose requirements hold. Repairing at the end would skip the per-rule check; I did not measure it.
What is the testing strategy for this PR?
test_appended_rule_that_breaks_requirements_is_repairedincore/tests/physical_optimizer/sanity_checker.rs: a rule appended after the default list removes every sort. The plan gets its sort back androw_number()returns 1, 2, 3. On main it fails with "order and distribution requirements stopped holding after physical optimizer rule 'remove_sorts'".sql_plannerbenchmarks, main vs this PR, 3 alternating rounds: planning the TPC-H, TPC-DS and ClickBench queries takes +1.4%, +1.1% and +1.7% longer (median round). For comparison, a logical-planning benchmark this PR does not touch moved +0.5%.sqllogictest records per rule list, main c5361f0 vs this PR (local harness)
SanityCheckPlanerrorsJoinSelectionafterEnsureRequirementsEnsureRequirementsfirstEvery mismatch left with this PR also occurs on main. The intermittent one is a MATERIALIZED CTE query whose
ORDER BYrows come out of order when the list runs twice.`sql_planner` per round, main c5361f0 vs this PR (Criterion estimate, ms; main ran first in each pair)
physical_plan_tpch_allphysical_plan_tpcds_allphysical_plan_clickbench_allphysical_select_all_from_1000logical_select_all_from_1000(not touched)Are there any user-facing changes?
No public API change and no plan change in the default rule order.
Downstream impact, for rule lists that contain
EnsureRequirementsand run rules after it:EnsureRequirementsruns between them, so the later rule sees the repaired plan and the final plan can differ.EnsureRequirementsare unchanged.Downstream projects checked (by reading their code, not by running them)
ParallelWindowRule,PrefixWindowRuleandDistributedExchangeRuleafter the default list.PrefixWindowRuleinserts aPrefixMergeExecthat keeps its input's partitions and adds no merge for the parent (prefix_window.rs:174-179). If that parent needs one partition, planning failed before; now it gets a merge. I don't know whether real plans reach that.