Repository navigation
fix: JoinSelection keeps the requirements a join's inputs already satisfy - #27771
jayzhan211 wants to merge 5 commits into
Conversation
| && child.output_partitioning().partition_count() > 1 | ||
| { | ||
| merged_any = true; | ||
| Arc::new(CoalescePartitionsExec::new(Arc::clone(child))) as _ |
There was a problem hiding this comment.
After a swap the old build side becomes the probe side but keeps its CoalescePartitionsExec, so with JoinSelection after EnsureRequirements the probe side runs in one partition. SELECT big.column1, small.column1 FROM big, small (two 2-file parquet tables, target_partitions = 2):
CrossJoinExec
CoalescePartitionsExec
DataSourceExec (small)
CoalescePartitionsExec
DataSourceExec (big)
Could the swap drop that merge when nothing else needs it?
There was a problem hiding this comment.
The swap now drops it when the old join's output had several partitions and no hash or range key, since nothing above the join can rely on such an output. When the output was hash or range partitioned, the merge stays: a node above may rely on that partitioning, and the swapped join's one-partition output still satisfies it.
| }) | ||
| })? | ||
| .data; | ||
| let mut broken = false; |
There was a problem hiding this comment.
For group_by.slt:3330, the node relying on the order is the partial AggregateExec two levels above the join (a ProjectionExec in between), so a parent check wouldn't catch it. A whole-plan check like #27772 fixes it, but changes two default-order plans (cte_materialized.slt, preserve_file_partitioning.slt: CollectLeft becomes Partitioned), since before EnsureRequirements a simple plan can already satisfy its requirements.
alamb
left a comment
There was a problem hiding this comment.
Thanks for this PR @jayzhan211
I think it would help if we also cleaned up the PR summary. I found this really hard to understand (and I already know what this PR does):
With this PR, JoinSelection keeps the requirements a join's inputs already satisfy, as every rule after EnsureRequirements must, instead of leaving a plan that only a later pass could repair.
Maybe something like
After this PR, the
JoinSelectionphysical optimizer rule produces valid plans and does not require a follow on pass ofEnsureRequirementsto align requirements. It is part of our longer term goal to simplify required optimizer rule ordering
alamb
left a comment
There was a problem hiding this comment.
Thanks @jayzhan211 -- this seems like an improvement to me. I think it would help if we clarified some of the comments but otherwise this looks great
| } | ||
|
|
||
| /// A session whose physical optimizer runs [`JoinSelection`] right after | ||
| /// [`EnsureRequirements`] instead of before it. |
There was a problem hiding this comment.
maybe we can add rationale here (we want to ensure that JoinSelection does not make invalid plans that require EnsureRequirements to repair)
| /// [`EnsureRequirements`] instead of before it. | ||
| fn session_with_join_selection_after_enforcement() -> SessionContext { | ||
| let mut rules = PhysicalOptimizer::new().rules; | ||
| let position = |rules: &[Arc<dyn PhysicalOptimizerRule + Send + Sync>], |
There was a problem hiding this comment.
would this be better as a method on PhysicalOptimizer? this is fine, it just feels weird to be mucking round with the internal stte
There was a problem hiding this comment.
rules is already the public way to change the list (PhysicalOptimizer::with_rules points to the contract for non-default lists), and the test in output_requirements.rs reads it the same way, so a method would be new public API with only this test as a caller. If a test harness for other rule orders lands later, it would have more callers and I'd add the method there. OK to keep it as is here?
| /// order and distribution requirements that `old`'s inputs satisfy, because | ||
| /// nothing restores them once `EnsureRequirements` has run. | ||
| /// | ||
| /// A swap can make an input with several partitions the build side, which |
There was a problem hiding this comment.
I don't quite follow this paragraph -- is it trying to say that this returns None if it is not possible to build a valid join ?
There was a problem hiding this comment.
Yes: None means the rewritten join would break a requirement the old join's inputs met, so JoinSelection keeps the old join. The doc comment now says that in its first sentence.
…st session moves JoinSelection
…n nothing above relies on it
|
Address all the comments! |
| }) | ||
| }; | ||
| // Several partitions without a key satisfy only an unspecified distribution. | ||
| let output_has_no_key = matches!( |
There was a problem hiding this comment.
Keeping the merge can break a co-partitioned parent. With JoinSelection after EnsureRequirements, target_partitions = 2 and the single-partition thresholds at 0:
SELECT x.a, x.k FROM (SELECT a, s.k FROM big, (SELECT k FROM small GROUP BY k) s) x JOIN t ON x.k = t.kHashJoinExec: mode=Partitioned, on=[(k@1, k@0)]
ProjectionExec: expr=[a@1 as a, k@0 as k]
CrossJoinExec
CoalescePartitionsExec
AggregateExec: mode=FinalPartitioned, gby=[k@0 as k]
CoalescePartitionsExec
DataSourceExec: partitions=2
RepartitionExec: partitioning=Hash([k@0], 2)
Planning fails with HashJoinExec requires children [0, 1] to be co-partitioned. Maybe keep the join as is when the old output had a key?
Which issue does this PR close?
Rationale for this change
A rule list that runs
JoinSelectionafterEnsureRequirementsfails to plan many join queries: on the sqllogictest suite, 293 records fail withSanityCheckPlan: order and distribution requirements stopped holding after physical optimizer rule 'join_selection'(local harness that moves the rule, not part of this PR). In 292 of them,JoinSelectionswapped a join's inputs so that the build side, which needs one partition, has several.SELECT a, b FROM big, smallover two tables with two partitions each is enough.After this PR, the
JoinSelectionphysical optimizer rule produces valid plans for these joins (292 of the 293 records) and does not require a follow-on pass ofEnsureRequirementsto align requirements. It is part of the longer-term goal in #26481 to simplify the required physical optimizer rule ordering.What changes are included in this PR?
keep_satisfied_requirementsinjoin_selection.rs(start reading here) runs after each swap or mode change. If the old join's inputs satisfied its order and distribution requirements, the new join's inputs must too:CoalescePartitionsExec.PartitionMode::Autois exempt: it declares no requirement untilEnsureRequirementspicks its mode.The decision I'd most like reviewed: adding the merge instead of skipping the swap. Before
EnsureRequirementsruns, a larger input can have one partition only because it is one batch; skipping would keep it as the build side and change default plans. With the merge, the swap happens as on main, andEnsureRequirementslater replaces the merge with the ones the plan needs.What is the testing strategy for this PR?
core/tests/physical_optimizer/join_selection.rs:join_selection_after_enforcement_keeps_merged_build_side: withJoinSelectionafterEnsureRequirements, a cross join of two two-partition tables returns all 8 rows. On main it fails with the error above.join_selection_after_enforcement_drops_merge_from_probe_sideand..._keeps_merge_for_hash_partitioned_output: the probe side's merge is dropped when the old output had no key, and kept when it was hash partitioned.join_selection_swaps_larger_single_partition_input: the default order keeps main's plan for a larger input in one partition.SanityCheckPlanerrorsJoinSelectionafterEnsureRequirementsThe record left
group_by.slt:3330still fails planning, with the same error namingjoin_selection.EnsureRequirementssorted the probe side so the join's output satisfies the order anarray_agg(... ORDER BY ...)above the join needs; the swap makes that side the build side. The check 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.Are there any user-facing changes?
No public API change, and no plan change in the default rule order.
For rule lists that run
JoinSelectionafterEnsureRequirements, these swaps no longer fail planning. The swapped join's probe side keeps its partitions, unless the old join's output was hash or range partitioned; then it runs in one partition.