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
68 changes: 68 additions & 0 deletions datafusion/core/src/physical_optimizer/dist_enforcement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::physical_plan::joins::{
use crate::physical_plan::projection::ProjectionExec;
use crate::physical_plan::repartition::RepartitionExec;
use crate::physical_plan::sorts::sort::SortOptions;
use crate::physical_plan::union::{can_interleave, InterleaveExec, UnionExec};
use crate::physical_plan::windows::WindowAggExec;
use crate::physical_plan::Partitioning;
use crate::physical_plan::{with_new_children_if_necessary, Distribution, ExecutionPlan};
Expand Down Expand Up @@ -825,6 +826,35 @@ fn ensure_distribution(
return Ok(Transformed::No(plan));
}

// special case for UnionExec: We want to "bubble up" hash-partitioned data. So instead of:
//
// Agg:
// Repartition (hash):
// Union:
// - Agg:
// Repartition (hash):
// Data
// - Agg:
// Repartition (hash):
// Data
//
// We can use:
//
// Agg:
// Interleave:
// - Agg:
// Repartition (hash):
// Data
// - Agg:
// Repartition (hash):
// Data
if let Some(union_exec) = plan.as_any().downcast_ref::<UnionExec>() {
if can_interleave(union_exec.inputs()) {
let plan = InterleaveExec::try_new(union_exec.inputs().clone())?;
return Ok(Transformed::Yes(Arc::new(plan)));
}
}

let required_input_distributions = plan.required_input_distribution();
let children: Vec<Arc<dyn ExecutionPlan>> = plan.children();
assert_eq!(children.len(), required_input_distributions.len());
Expand Down Expand Up @@ -2134,4 +2164,42 @@ mod tests {
assert_optimized!(expected, exec);
Ok(())
}

#[test]
fn union_to_interleave() -> Result<()> {
// group by (a as a1)
let left = aggregate_exec_with_alias(
parquet_exec(),
vec![("a".to_string(), "a1".to_string())],
);
// group by (a as a2)
let right = aggregate_exec_with_alias(
parquet_exec(),
vec![("a".to_string(), "a1".to_string())],
);

// Union
let plan = Arc::new(UnionExec::new(vec![left, right]));

// final agg
let plan =
aggregate_exec_with_alias(plan, vec![("a1".to_string(), "a2".to_string())]);

// Only two RepartitionExecs added, no final RepartionExec required
let expected = &[
"AggregateExec: mode=FinalPartitioned, gby=[a2@0 as a2], aggr=[]",
"AggregateExec: mode=Partial, gby=[a1@0 as a2], aggr=[]",
"InterleaveExec",
"AggregateExec: mode=FinalPartitioned, gby=[a1@0 as a1], aggr=[]",
"RepartitionExec: partitioning=Hash([Column { name: \"a1\", index: 0 }], 10), input_partitions=1",
"AggregateExec: mode=Partial, gby=[a@0 as a1], aggr=[]",
"ParquetExec: limit=None, partitions={1 group: [[x]]}, projection=[a, b, c, d, e]",
"AggregateExec: mode=FinalPartitioned, gby=[a1@0 as a1], aggr=[]",
"RepartitionExec: partitioning=Hash([Column { name: \"a1\", index: 0 }], 10), input_partitions=1",
"AggregateExec: mode=Partial, gby=[a@0 as a1], aggr=[]",
"ParquetExec: limit=None, partitions={1 group: [[x]]}, projection=[a, b, c, d, e]",
];
assert_optimized!(expected, plan);
Ok(())
}
}
Loading