@@ -925,32 +925,34 @@ fn add_hash_on_top(
925
925
Ok ( input)
926
926
}
927
927
928
- /// Adds a [`SortPreservingMergeExec`] operator on top of input executor
929
- /// to satisfy single distribution requirement.
928
+ /// Adds a [`SortPreservingMergeExec`] or a [`CoalescePartitionsExec`] operator
929
+ /// on top of the given plan node to satisfy a single partition requirement
930
+ /// while preserving ordering constraints.
930
931
///
931
- /// # Arguments
932
+ /// # Parameters
932
933
///
933
934
/// * `input`: Current node.
934
935
///
935
936
/// # Returns
936
937
///
937
- /// Updated node with an execution plan, where desired single
938
- /// distribution is satisfied by adding [`SortPreservingMergeExec`] .
939
- fn add_spm_on_top ( input : DistributionContext ) -> DistributionContext {
940
- // Add SortPreservingMerge only when partition count is larger than 1 .
938
+ /// Updated node with an execution plan, where the desired single distribution
939
+ /// requirement is satisfied.
940
+ fn add_merge_on_top ( input : DistributionContext ) -> DistributionContext {
941
+ // Apply only when the partition count is larger than one .
941
942
if input. plan . output_partitioning ( ) . partition_count ( ) > 1 {
942
943
// When there is an existing ordering, we preserve ordering
943
944
// when decreasing partitions. This will be un-done in the future
944
945
// if any of the following conditions is true
945
946
// - Preserving ordering is not helpful in terms of satisfying ordering requirements
946
947
// - Usage of order preserving variants is not desirable
947
948
// (determined by flag `config.optimizer.prefer_existing_sort`)
948
- let new_plan = if let Some ( ordering ) = input. plan . output_ordering ( ) {
949
+ let new_plan = if let Some ( req ) = input. plan . output_ordering ( ) {
949
950
Arc :: new ( SortPreservingMergeExec :: new (
950
- ordering . clone ( ) ,
951
+ req . clone ( ) ,
951
952
Arc :: clone ( & input. plan ) ,
952
953
) ) as _
953
954
} else {
955
+ // If there is no input order, we can simply coalesce partitions:
954
956
Arc :: new ( CoalescePartitionsExec :: new ( Arc :: clone ( & input. plan ) ) ) as _
955
957
} ;
956
958
@@ -1259,7 +1261,7 @@ pub fn ensure_distribution(
1259
1261
// Satisfy the distribution requirement if it is unmet.
1260
1262
match & requirement {
1261
1263
Distribution :: SinglePartition => {
1262
- child = add_spm_on_top ( child) ;
1264
+ child = add_merge_on_top ( child) ;
1263
1265
}
1264
1266
Distribution :: HashPartitioned ( exprs) => {
1265
1267
if add_roundrobin {
0 commit comments