@@ -55,6 +55,8 @@ use datafusion_physical_plan::projection::ProjectionExec;
5555use datafusion_physical_plan:: repartition:: RepartitionExec ;
5656use datafusion_physical_plan:: sorts:: sort_preserving_merge:: SortPreservingMergeExec ;
5757
58+ const TEST_TARGET_PARTITIONS : usize = 8 ;
59+
5860/// Mock ExecutionPlan with configurable partition count and output ordering.
5961#[ derive( Debug ) ]
6062struct MockMultiPartitionExec {
@@ -124,11 +126,18 @@ impl ExecutionPlan for MockMultiPartitionExec {
124126 }
125127}
126128
129+ fn test_config ( ) -> ConfigOptions {
130+ let mut config = ConfigOptions :: default ( ) ;
131+ // Keep plan-shape tests deterministic across machines with different CPU counts.
132+ config. execution . target_partitions = TEST_TARGET_PARTITIONS ;
133+ config
134+ }
135+
127136/// Helper: run EnsureRequirements and verify SanityCheckPlan passes
128137fn optimize_and_sanity_check (
129138 plan : Arc < dyn ExecutionPlan > ,
130139) -> Result < Arc < dyn ExecutionPlan > > {
131- let config = ConfigOptions :: default ( ) ;
140+ let config = test_config ( ) ;
132141 let optimized = EnsureRequirements :: new ( ) . optimize ( plan, & config) ?;
133142 // SanityCheckPlan must pass
134143 SanityCheckPlan :: new ( ) . optimize ( Arc :: clone ( & optimized) , & config) ?;
@@ -137,7 +146,7 @@ fn optimize_and_sanity_check(
137146
138147/// Helper: verify idempotency — running twice produces the same plan
139148fn assert_idempotent ( plan : Arc < dyn ExecutionPlan > ) {
140- let config = ConfigOptions :: default ( ) ;
149+ let config = test_config ( ) ;
141150 let p1 = EnsureRequirements :: new ( )
142151 . optimize ( plan, & config)
143152 . expect ( "first optimize failed" ) ;
@@ -195,7 +204,7 @@ fn plan_string(plan: &Arc<dyn ExecutionPlan>) -> String {
195204/// updating an intentional plan change is a single `cargo insta accept`.
196205macro_rules! assert_ensure_requirements_plan {
197206 ( $plan: expr, @ $snapshot: literal $( , ) ?) => { {
198- let config = ConfigOptions :: default ( ) ;
207+ let config = test_config ( ) ;
199208 let p1 = EnsureRequirements :: new( )
200209 . optimize( $plan, & config)
201210 . expect( "EnsureRequirements::optimize failed (pass 1)" ) ;
@@ -655,7 +664,7 @@ fn test_issue_21973_idempotent_spm_sort_multi_partition() {
655664 // No-extra-SPM property: count SortPreservingMergeExec occurrences in
656665 // the first optimisation pass — must be ≤ 1 (the original SPM survives,
657666 // none are added).
658- let config = ConfigOptions :: default ( ) ;
667+ let config = test_config ( ) ;
659668 let optimized = EnsureRequirements :: new ( )
660669 . optimize ( Arc :: clone ( & limit) , & config)
661670 . expect ( "optimize failed" ) ;
@@ -688,7 +697,7 @@ fn test_issue_21973_parallel_sort_survives_multiple_passes() {
688697 let sort: Arc < dyn ExecutionPlan > = Arc :: new ( SortExec :: new ( sort_expr, coalesce) ) ;
689698 let limit: Arc < dyn ExecutionPlan > = Arc :: new ( GlobalLimitExec :: new ( sort, 0 , Some ( 10 ) ) ) ;
690699
691- let config = ConfigOptions :: default ( ) ;
700+ let config = test_config ( ) ;
692701
693702 let p1 = EnsureRequirements :: new ( )
694703 . optimize ( Arc :: clone ( & limit) , & config)
@@ -816,7 +825,7 @@ fn test_issue_14150_fetch_survives_multiple_passes() {
816825 let sort = Arc :: new ( SortExec :: new ( sort_expr, repartition as _ ) . with_fetch ( Some ( 5 ) ) ) ;
817826 let limit: Arc < dyn ExecutionPlan > = Arc :: new ( GlobalLimitExec :: new ( sort, 0 , Some ( 5 ) ) ) ;
818827
819- let config = ConfigOptions :: default ( ) ;
828+ let config = test_config ( ) ;
820829
821830 // Pass 1
822831 let p1 = EnsureRequirements :: new ( )
@@ -885,7 +894,7 @@ fn test_issue_14150_fetch_survives_with_input_spm() {
885894
886895 let limit: Arc < dyn ExecutionPlan > = Arc :: new ( GlobalLimitExec :: new ( spm, 0 , Some ( 5 ) ) ) ;
887896
888- let config = ConfigOptions :: default ( ) ;
897+ let config = test_config ( ) ;
889898
890899 let p1 = EnsureRequirements :: new ( )
891900 . optimize ( Arc :: clone ( & limit) , & config)
@@ -1018,7 +1027,7 @@ fn test_idempotent_parallelize_sorts() {
10181027 let limit: Arc < dyn ExecutionPlan > = Arc :: new ( GlobalLimitExec :: new ( sort, 0 , Some ( 10 ) ) ) ;
10191028
10201029 // First pass should parallelize the sort (Sort+SPM replaces Coalesce+Sort)
1021- let config = ConfigOptions :: default ( ) ;
1030+ let config = test_config ( ) ;
10221031 let p1 = EnsureRequirements :: new ( )
10231032 . optimize ( Arc :: clone ( & limit) , & config)
10241033 . expect ( "first optimize failed" ) ;
@@ -1194,7 +1203,7 @@ fn test_enforce_distribution_idempotent_hash_join() {
11941203 . expect ( "HashJoinExec creation failed" ) ,
11951204 ) ;
11961205
1197- let config = ConfigOptions :: default ( ) ;
1206+ let config = test_config ( ) ;
11981207 let p1 = EnsureRequirements :: new ( )
11991208 . optimize ( Arc :: clone ( & join) , & config)
12001209 . expect ( "first EnsureRequirements pass failed" ) ;
0 commit comments