jayzhan211 commented on code in PR #25787:
URL: https://github.com/apache/datafusion/pull/25787#discussion_r4652516025
##########
datafusion/physical-optimizer/src/ensure_requirements/enforce_distribution.rs:
##########
@@ -1362,6 +1364,26 @@ fn enforce_distribution_relationships(
}
};
+ // Do not adapt a child above the `CollectLeft` thresholds to a
reference
+ // below them; fall back to hash partitioning instead. Otherwise keep
the
+ // reference, so that it is not shuffled as well.
+ let best_satisfied_child = best_satisfied_child.filter(|(ref_idx, _)| {
+ let is_small = |idx: usize| {
+ let stats = stats_ctx
+ .compute_arc(&children[idx].context.plan,
&StatisticsArgs::new())
+ .ok()?;
+ below_collect_thresholds(
+ &stats,
+ config.optimizer.hash_join_single_partition_threshold,
+ config.optimizer.hash_join_single_partition_threshold_rows,
+ )
+ };
+ is_small(*ref_idx) != Some(true)
Review Comment:
This also fires for hash-partitioned references, where it only adds work:
hash routing doesn't depend on the small input's data, so adopting its layout
can't skew the large input. A small `Hash([a], 8)` source joined to a large
unpartitioned one at `target_partitions = 4` keeps the reference on main (large
side to `Hash(8)`), but this PR repartitions both sides to `Hash(4)`. No test
covers it: limiting the gate to `Range` still passes every test. Could we limit
it to range references, and add the test below?
```suggestion
// A small input's range split points can put most rows of a larger
input
// in one partition, so do not adapt a child above the `CollectLeft`
// thresholds to a range reference below them; fall back to hash
// partitioning instead. Keep any other reference, so that it is not
// shuffled as well: hash partitioning does not depend on the data.
let best_satisfied_child = best_satisfied_child.filter(|(ref_idx,
part)| {
let is_small = |idx: usize| {
let stats = stats_ctx
.compute_arc(&children[idx].context.plan,
&StatisticsArgs::new())
.ok()?;
below_collect_thresholds(
&stats,
config.optimizer.hash_join_single_partition_threshold,
config.optimizer.hash_join_single_partition_threshold_rows,
)
};
!matches!(part, Partitioning::Range(_))
|| is_small(*ref_idx) != Some(true)
```
<details>
<summary>Test for core/tests/physical_optimizer/enforce_distribution.rs
(fails without the condition above)</summary>
```rs
#[test]
fn hash_join_reference_kept_below_thresholds() -> Result<()> {
let bytes = TestConfig::default()
.config
.optimizer
.hash_join_single_partition_threshold;
let left = parquet_exec_with_partitioning_and_size(
Partitioning::UnknownPartitioning(4),
Precision::Absent,
Precision::Inexact(bytes * 10),
);
// More partitions than `target_partitions`, so keeping the reference and
// falling back to hash partitioning give different plans.
let right = parquet_exec_with_partitioning_and_size(
Partitioning::Hash(vec![col("a", &schema())?], 8),
Precision::Absent,
Precision::Inexact(100),
);
let join_on = vec![(
Arc::new(Column::new_with_schema("a", &left.schema())?) as _,
Arc::new(Column::new_with_schema("a", &right.schema())?) as _,
)];
let join = hash_join_exec(left, right, &join_on, &JoinType::Inner);
let plan = TestConfig::default()
.with_query_execution_partitions(4)
.to_plan(join, &DISTRIB_DISTRIB_SORT);
// The small hash reference is not repartitioned; the large input adopts
it.
assert!(plan.children()[1].is::<DataSourceExec>());
for child in plan.children() {
assert!(
matches!(child.output_partitioning(), Partitioning::Hash(_, 8)),
"got {:?}",
child.output_partitioning(),
);
}
Ok(())
}
```
</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]