jayzhan211 opened a new pull request, #27648: URL: https://github.com/apache/datafusion/pull/27648
## Which issue does this PR close? - Part of #26481. ## 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 a `SanityCheckPlan` error. So a rule only works where the rules before it left a plan it handles. Running the sqllogictest suite with `JoinSelection` moved after `EnsureRequirements` (with a local harness, not part of this PR), 293 records fail with that error on main at c5361f0da2; with this PR, 0. This PR makes the physical planner repair such a plan: when a rule after `EnsureRequirements` returns a plan whose requirements no longer hold, the planner runs `EnsureRequirements` again before the next rule. In the default rule order no rule breaks a requirement, so nothing runs again and no plan changes. ## What changes are included in this PR? - `DefaultPhysicalPlanner::optimize_physical_plan` (start reading here): after each rule that follows `EnsureRequirements`, it checks the nodes the rule changed. If a requirement broke, it runs `EnsureRequirements` with 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_since` in `sanity_checker.rs` (`#[doc(hidden)]`): skips nodes shared with the previous plan (pointer identity), so the check walks only what the rule changed. - Docs: the contract in `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_repaired` in `core/tests/physical_optimizer/sanity_checker.rs`: a rule appended after the default list removes every sort. The plan gets its sort back and `row_number()` returns 1, 2, 3. On main it fails with "order and distribution requirements stopped holding after physical optimizer rule 'remove_sorts'". - The sqllogictest suite (with TPC-H), the physical optimizer integration tests and the crate's unit tests pass unchanged; a local counter showed 0 repairs over the suite. - Rearranged rule lists (local harness): no list gains a wrong or reordered result; plan errors drop as below. <details> <summary>sqllogictest records per rule list, main c5361f0da2 vs this PR (local harness)</summary> | Rule list | `SanityCheckPlan` errors | Result mismatches | Repairs | |---|---|---|---| | default | 0 → 0 | 0 → 0 | 0 | | `JoinSelection` after `EnsureRequirements` | 293 → 0 | 1 → 0 | 318 | | `EnsureRequirements` first | 294 → 0 | 3 → 3 (same records) | 315 | | shuffled (seed 1) | 296 → 1 | 16 → 15 (subset) | 314 | | whole list run twice (re-planning) | 0 → 0 | 1 → 1, intermittent in both (2 of 20 runs each) | 48 | Every mismatch left with this PR also occurs on main. The intermittent one is a MATERIALIZED CTE query whose `ORDER BY` rows come out of order when the list runs twice. Planning cost: TODO (`sql_planner` main vs this PR). </details> ## 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 `EnsureRequirements` and run rules after it: - A rule that leaves a requirement broken at the end used to fail planning; now the plan gets the missing sort, repartition or merge. - A rule that breaks a requirement that a later rule of the same list fixes used to be accepted as is; now `EnsureRequirements` runs between them, so the later rule sees the repaired plan and the final plan can differ. - Lists without `EnsureRequirements` are unchanged. <details> <summary>Downstream projects checked (by reading their code, not by running them)</summary> - Ballista's adaptive planner appends `ParallelWindowRule`, `PrefixWindowRule` and `DistributedExchangeRule` after the default list. `PrefixWindowRule` inserts a `PrefixMergeExec` that 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. - datafusion-distributed runs its passes on the plan the planner returns, so they are unaffected. - liquid-cache appends a rule whose wrapper node copies its child's partitioning and orderings, so it keeps every requirement. </details> -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
