Skip to content
Closed
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
35 changes: 34 additions & 1 deletion datafusion/src/physical_optimizer/repartition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ mod tests {

use super::*;
use crate::datasource::PartitionedFile;
use crate::physical_plan::expressions::col;
use crate::physical_plan::expressions::{col, PhysicalSortExpr};
use crate::physical_plan::file_format::{FileScanConfig, ParquetExec};
use crate::physical_plan::filter::FilterExec;
use crate::physical_plan::hash_aggregate::{AggregateMode, HashAggregateExec};
use crate::physical_plan::limit::{GlobalLimitExec, LocalLimitExec};
use crate::physical_plan::sorts::sort_preserving_merge::SortPreservingMergeExec;
use crate::physical_plan::union::UnionExec;
use crate::physical_plan::{displayable, Statistics};
use crate::test::object_store::TestObjectStore;
Expand All @@ -137,6 +138,17 @@ mod tests {
))
}

fn sort_preserving_merge_exec(
input: Arc<dyn ExecutionPlan>,
) -> Arc<dyn ExecutionPlan> {
let expr = vec![PhysicalSortExpr {
expr: col("c1", &schema()).unwrap(),
options: arrow::compute::SortOptions::default(),
}];

Arc::new(SortPreservingMergeExec::new(expr, input))
}

fn filter_exec(input: Arc<dyn ExecutionPlan>) -> Arc<dyn ExecutionPlan> {
Arc::new(FilterExec::try_new(col("c1", &schema()).unwrap(), input).unwrap())
}
Expand Down Expand Up @@ -276,4 +288,25 @@ mod tests {
assert_eq!(&trim_plan_display(&plan), &expected);
Ok(())
}

#[test]
fn repartition_ignores_sort_preserving_merge() -> Result<()> {
let optimizer = Repartition {};

let optimized = optimizer.optimize(
sort_preserving_merge_exec(parquet_exec()),
&ExecutionConfig::new().with_target_partitions(5),
)?;

let plan = displayable(optimized.as_ref()).indent().to_string();

let expected = &[
"SortPreservingMergeExec: [c1@0 ASC]",
// Expect no repartition of SortPreservingMergeExec
"ParquetExec: limit=None, partitions=[x]",
];

assert_eq!(&trim_plan_display(&plan), &expected);
Ok(())
}
}
6 changes: 6 additions & 0 deletions datafusion/src/physical_plan/sorts/sort_preserving_merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ impl ExecutionPlan for SortPreservingMergeExec {
Distribution::UnspecifiedDistribution
}

fn should_repartition_children(&self) -> bool {
// if the children are repartitioned they may no longer remain
// sorted
false
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the fix, the rest is tests

Copy link
Contributor

@tustvold tustvold Feb 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this comment elsewhere, but going to make it here for visibility - this will only prevent repartitioning of direct children.

So for example if you had

Sort Preserving Merge <- Projection Exec <- Sorted Scan

I think Repartition could convert this to

Sort Preserving Merge <- Projection Exec <- Repartition <- Sorted Scan

Which would break sorting

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will write some more tests and fix this

}

fn children(&self) -> Vec<Arc<dyn ExecutionPlan>> {
vec![self.input.clone()]
}
Expand Down