jayzhan211 opened a new pull request, #27771: URL: https://github.com/apache/datafusion/pull/27771
## Which issue does this PR close? - Part of #26481. ## Rationale for this change A rule list that runs `JoinSelection` after `EnsureRequirements` fails to plan many join queries. On the sqllogictest suite, 293 records fail with `SanityCheckPlan: order and distribution requirements stopped holding after physical optimizer rule 'join_selection'` (measured with a local harness that moves the rule; not part of this PR). In 292 of them the join's build side had one partition, as `EnsureRequirements` arranged, and `JoinSelection` then swapped the inputs, so the join builds on an input with several partitions that nothing merges. `SELECT a, b FROM big, small` over two tables with two partitions each is enough. With this PR, `JoinSelection` keeps a requirement it finds satisfied, as every rule after `EnsureRequirements` must, instead of leaving a plan that only a later pass could repair. ## What changes are included in this PR? - `statistical_join_selection_subrule` in `join_selection.rs` (start reading here): a swap or mode change is dropped when the join's inputs satisfied its order and distribution requirements and the rewritten join's inputs don't. Only the nodes the rewrite creates are checked, so the check doesn't walk the inputs. - A hash join in `PartitionMode::Auto` is exempt. It declares no requirement yet, and `EnsureRequirements` resolves every `Auto` join and satisfies the requirements of the mode chosen for it. The decision I'd most like reviewed is the `Auto` exemption. It's what keeps default plans unchanged: in the default order `JoinSelection` runs before `EnsureRequirements`, so it mostly sees `Auto` joins, and choosing `CollectLeft` for one over a multi-partition input is how it normally works. ## What is the testing strategy for this PR? - `join_selection_after_enforcement_keeps_merged_build_side` in `core/tests/physical_optimizer/join_selection.rs`: a session with `JoinSelection` after `EnsureRequirements` cross joins two tables with two partitions each and returns all 8 rows. On main it fails with the error above. - The sqllogictest suite (with TPC-H) under a local harness, main 73af333429 vs this PR: | Rule list | `SanityCheckPlan` errors | |---|---| | default | 0 → 0, no plan changes | | `JoinSelection` after `EnsureRequirements` | 293 → 1 | <details> <summary>The record left, and one that now fails differently</summary> - `group_by.slt:3330` still fails planning, with the same error naming `join_selection`. `EnsureRequirements` sorted the probe side so the join's output satisfies the order an `array_agg(... ORDER BY ...)` above needs; the swap makes that side the build side. The check above only looks at the join's own requirements. Also keeping the join's output order fixes this record, but changes three records in the default order (`joins.slt:4020`, `:4034`, `:4148`), so it isn't in this PR. - `null_aware_memory_limit.slt:28` now hits its memory limit instead of failing planning: the join keeps the build side `EnsureRequirements` left it with, which is the larger input there. </details> ## Are there any user-facing changes? No public API change, and no plan change in the default rule order. For rule lists that run `JoinSelection` after `EnsureRequirements`: a join whose swap would have failed planning now keeps the inputs `EnsureRequirements` left it with. The query plans and runs, but may build on the larger input. -- 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]
