zhuqi-lucas commented on code in PR #27771:
URL: https://github.com/apache/datafusion/pull/27771#discussion_r4729745729
##########
datafusion/physical-optimizer/src/join_selection.rs:
##########
@@ -340,13 +346,117 @@ fn statistical_join_selection_subrule(
None
};
- Ok(if let Some(transformed) = transformed {
- Transformed::yes(transformed)
- } else {
- Transformed::no(plan)
+ Ok(match transformed {
+ Some(transformed) => match keep_satisfied_requirements(&plan,
transformed)? {
+ Some(transformed) => Transformed::yes(transformed),
+ None => Transformed::no(plan),
+ },
+ None => Transformed::no(plan),
})
}
+/// Returns `new`, the rewrite of the join `old` over the same inputs, adjusted
+/// so that its inputs still satisfy the order and distribution requirements
+/// that `old`'s inputs satisfied, or `None` when that is not possible and
`old`
+/// should be kept. Once `EnsureRequirements` has run, nothing repairs a
+/// requirement that stops holding.
+///
+/// When a swap makes an input with several partitions the build side, which
+/// needs one partition, that input gets a [`CoalescePartitionsExec`]. If
+/// `EnsureRequirements` runs later, it replaces this merge with the ones the
+/// plan needs. The merge the old build side had moves to the probe side, which
+/// needs none, and is dropped when `old`'s output had several partitions and
no
+/// key, which nothing above the join can rely on.
+///
+/// Returns `new` unchanged when `old`'s inputs did not satisfy its
requirements
+/// to begin with, usually because `EnsureRequirements` has not run yet, or
when
+/// `old` is a hash join in [`PartitionMode::Auto`], whose mode and
requirements
+/// `EnsureRequirements` chooses later.
+fn keep_satisfied_requirements(
+ old: &Arc<dyn ExecutionPlan>,
+ new: Arc<dyn ExecutionPlan>,
+) -> Result<Option<Arc<dyn ExecutionPlan>>> {
+ let mode_undecided = old
+ .downcast_ref::<HashJoinExec>()
+ .is_some_and(|join| join.partition_mode() == &PartitionMode::Auto);
+ if mode_undecided || check_input_requirements(old).is_err() {
+ return Ok(Some(new));
+ }
+ let inputs = old.children();
+ let is_input = |node: &Arc<dyn ExecutionPlan>| {
+ inputs.iter().any(|input| Arc::ptr_eq(input, node))
+ };
+ let is_unchanged = |node: &Arc<dyn ExecutionPlan>| {
+ is_input(node)
+ || inputs.iter().any(|input| {
+ input
+ .downcast_ref::<CoalescePartitionsExec>()
+ .is_some_and(|merge| Arc::ptr_eq(merge.input(), node))
+ })
+ };
+ // Several partitions without a key satisfy only an unspecified
distribution.
+ let output_has_no_key = matches!(
Review Comment:
Keeping the merge can break a co-partitioned parent. With `JoinSelection`
after `EnsureRequirements`, `target_partitions = 2` and the single-partition
thresholds at 0:
```sql
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.k
```
```
HashJoinExec: 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?
--
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]