@@ -374,7 +374,13 @@ fn ensure_sorting(
374374 {
375375 // This `SortPreservingMergeExec` is unnecessary, input already has a
376376 // single partition.
377- let child_node = requirements. children . swap_remove ( 0 ) ;
377+ // single partition and no fetch is required.
378+ let mut child_node = requirements. children . swap_remove ( 0 ) ;
379+ if let Some ( fetch) = plan. fetch ( ) {
380+ // Add the limit exec if the spm has a fetch
381+ child_node. plan =
382+ Arc :: new ( LocalLimitExec :: new ( Arc :: clone ( & child_node. plan ) , fetch) ) ;
383+ }
378384 return Ok ( Transformed :: yes ( child_node) ) ;
379385 }
380386
@@ -660,6 +666,7 @@ mod tests {
660666 sort_preserving_merge_exec, spr_repartition_exec, union_exec,
661667 RequirementsTestExec ,
662668 } ;
669+ use crate :: physical_optimizer:: utils:: sort_preserving_merge_exec_with_fetch;
663670 use crate :: physical_plan:: { displayable, get_plan_string, Partitioning } ;
664671 use crate :: prelude:: { SessionConfig , SessionContext } ;
665672 use crate :: test:: { csv_exec_ordered, csv_exec_sorted, stream_exec_ordered} ;
@@ -1399,6 +1406,30 @@ mod tests {
13991406 Ok ( ( ) )
14001407 }
14011408
1409+ #[ tokio:: test]
1410+ async fn test_remove_unnecessary_spm2 ( ) -> Result < ( ) > {
1411+ let schema = create_test_schema ( ) ?;
1412+ let source = memory_exec ( & schema) ;
1413+ let input = sort_preserving_merge_exec_with_fetch (
1414+ vec ! [ sort_expr( "non_nullable_col" , & schema) ] ,
1415+ source,
1416+ 100 ,
1417+ ) ;
1418+
1419+ let expected_input = [
1420+ "SortPreservingMergeExec: [non_nullable_col@1 ASC], fetch=100" ,
1421+ " MemoryExec: partitions=1, partition_sizes=[0]" ,
1422+ ] ;
1423+ let expected_optimized = [
1424+ "LocalLimitExec: fetch=100" ,
1425+ " SortExec: expr=[non_nullable_col@1 ASC], preserve_partitioning=[false]" ,
1426+ " MemoryExec: partitions=1, partition_sizes=[0]" ,
1427+ ] ;
1428+ assert_optimized ! ( expected_input, expected_optimized, input, true ) ;
1429+
1430+ Ok ( ( ) )
1431+ }
1432+
14021433 #[ tokio:: test]
14031434 async fn test_union_inputs_different_sorted2 ( ) -> Result < ( ) > {
14041435 let schema = create_test_schema ( ) ?;
0 commit comments