Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions datafusion/physical-optimizer/src/enforce_sorting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,11 +676,45 @@ fn adjust_window_sort_removal(
/// the plan, some of the remaining `RepartitionExec`s might become unnecessary.
/// Removes such `RepartitionExec`s from the plan as well.
fn remove_bottleneck_in_subplan(
requirements: PlanWithCorrespondingCoalescePartitions,
) -> Result<PlanWithCorrespondingCoalescePartitions> {
// The root is the node `parallelize_sorts` is rewriting (a `SortExec`,
// `SortPreservingMergeExec` or `CoalescePartitionsExec`). Its own distribution
// requirement does not constrain the removal, because the caller drops the node and
// rebuilds the cascade around the result.
remove_bottleneck_in_subplan_impl(requirements, true)
}

fn remove_bottleneck_in_subplan_impl(
mut requirements: PlanWithCorrespondingCoalescePartitions,
is_root: bool,
) -> Result<PlanWithCorrespondingCoalescePartitions> {
let plan = &requirements.plan;
// Below the root, a `CoalescePartitionsExec` feeding a child that requires
// `Distribution::SinglePartition` is not an avoidable bottleneck: it is what satisfies
// that requirement. Removing it leaves the parent with a multi-partition input it cannot
// accept, and nothing re-runs distribution enforcement afterwards, so the plan reaches
// `SanityCheckPlan` invalid. The traversal reaches such a node because
// `update_coalesce_ctx_children` marks a node as connected when *any* child qualifies:
// a `CollectLeft` `HashJoinExec` whose probe side is connected is descended into even
// though its build side must stay single-partition.
//
// Only `SinglePartition` is protected. A `HashPartitioned` child is in principle in the
// same position — a single-partition input trivially satisfies a hash requirement, so a
// coalesce below one is also load-bearing — but nothing puts a coalesce there:
// `ensure_distribution` satisfies a hash requirement with a `RepartitionExec`, never a
// `CoalescePartitionsExec`. Widening the check would be dead code today.
let dist_reqs = plan.required_input_distribution();
let removable = |idx: usize| {
is_root || !matches!(dist_reqs.get(idx), Some(Distribution::SinglePartition))
};
let remove_from_first_child = requirements
.children
.first()
.is_some_and(|child| is_coalesce_partitions(&child.plan))
&& removable(0);
let children = &mut requirements.children;
if is_coalesce_partitions(&children[0].plan) {
if remove_from_first_child {
// We can safely use the 0th index since we have a `CoalescePartitionsExec`.
let mut new_child_node = children[0].children.swap_remove(0);
while new_child_node.plan.output_partitioning() == plan.output_partitioning()
Expand All @@ -694,9 +728,14 @@ fn remove_bottleneck_in_subplan(
requirements.children = requirements
.children
.into_iter()
.map(|node| {
if node.data {
remove_bottleneck_in_subplan(node)
.enumerate()
.map(|(idx, node)| {
// Deliberately conservative: not descending at all also skips legitimate
// cleanups *below* a protected child (a redundant second coalesce under the
// load-bearing one, say). This could later be narrowed to "descend, but
// protect only the topmost coalesce" if that turns out to matter.
if node.data && removable(idx) {
remove_bottleneck_in_subplan_impl(node, false)
} else {
Ok(node)
}
Expand Down
74 changes: 74 additions & 0 deletions datafusion/sqllogictest/test_files/joins.slt
Original file line number Diff line number Diff line change
Expand Up @@ -5527,3 +5527,77 @@ DROP TABLE t1;

statement ok
DROP TABLE t2;

# Regression test: a `CollectLeft` `HashJoinExec` requires `SinglePartition` on its build
# (left) child, and the `CoalescePartitionsExec` that satisfies it must survive the
# sort-parallelization phase of `EnsureRequirements`. It used to be removed positionally
# (the traversal descends into the join because the *probe* side is linked to a coalesce),
# leaving a multi-partition build side that `SanityCheckPlan` rejects with
# "does not satisfy distribution requirements: SinglePartition".

statement ok
set datafusion.execution.target_partitions = 8;

# Keep the scan multi-partition as written, i.e. one partition per file.
statement ok
set datafusion.optimizer.repartition_file_scans = false;

statement ok
CREATE TABLE collect_left_src (id INT, ts INT) AS VALUES (1, 10), (2, 20), (3, 30);

query I
COPY (SELECT * FROM collect_left_src) TO 'test_files/scratch/joins/collect_left/0.parquet' STORED AS PARQUET;
----
3

query I
COPY (SELECT * FROM collect_left_src) TO 'test_files/scratch/joins/collect_left/1.parquet' STORED AS PARQUET;
----
3

query I
COPY (SELECT * FROM collect_left_src) TO 'test_files/scratch/joins/collect_left/2.parquet' STORED AS PARQUET;
----
3

query I
COPY (SELECT * FROM collect_left_src) TO 'test_files/scratch/joins/collect_left/3.parquet' STORED AS PARQUET;
----
3

statement ok
CREATE EXTERNAL TABLE collect_left STORED AS PARQUET LOCATION 'test_files/scratch/joins/collect_left/';

# The build side is the 4-partition scan; the probe side is the `DISTINCT ON` aggregate,
# whose `CoalescePartitionsExec` is what makes the traversal reach the join.
query I
SELECT a.id
FROM collect_left a
LEFT JOIN (SELECT DISTINCT ON (id) id, ts FROM collect_left ORDER BY id, ts) f
ON a.id = f.id
ORDER BY a.id;
----
1
1
1
1
2
2
2
2
3
3
3
3

statement ok
DROP TABLE collect_left;

statement ok
DROP TABLE collect_left_src;

statement ok
reset datafusion.optimizer.repartition_file_scans;

statement ok
set datafusion.execution.target_partitions = 4;
Loading