@@ -884,6 +884,12 @@ impl FileScanConfig {
884
884
sort_order : & LexOrdering ,
885
885
target_partitions : usize ,
886
886
) -> Result < Vec < FileGroup > > {
887
+ if target_partitions == 0 {
888
+ return Err ( DataFusionError :: Internal (
889
+ "target_partitions must be greater than 0" . to_string ( ) ,
890
+ ) ) ;
891
+ }
892
+
887
893
let flattened_files = file_groups
888
894
. iter ( )
889
895
. flat_map ( FileGroup :: iter)
@@ -903,12 +909,10 @@ impl FileScanConfig {
903
909
let indices_sorted_by_min = statistics. min_values_sorted ( ) ;
904
910
905
911
// Initialize with target_partitions empty groups
906
- let mut file_groups_indices: Vec < Vec < usize > > =
907
- vec ! [ vec![ ] ; target_partitions. max( 1 ) ] ;
912
+ let mut file_groups_indices: Vec < Vec < usize > > = vec ! [ vec![ ] ; target_partitions] ;
908
913
909
914
for ( idx, min) in indices_sorted_by_min {
910
- // Find all groups where the file can fit
911
- let mut suitable_groups: Vec < ( usize , & mut Vec < usize > ) > = file_groups_indices
915
+ if let Some ( ( _, group) ) = file_groups_indices
912
916
. iter_mut ( )
913
917
. enumerate ( )
914
918
. filter ( |( _, group) | {
@@ -917,12 +921,8 @@ impl FileScanConfig {
917
921
> statistics
918
922
. max ( * group. last ( ) . expect ( "groups should not be empty" ) )
919
923
} )
920
- . collect ( ) ;
921
-
922
- // Sort by group size to prioritize smaller groups
923
- suitable_groups. sort_by_key ( |( _, group) | group. len ( ) ) ;
924
-
925
- if let Some ( ( _, group) ) = suitable_groups. first_mut ( ) {
924
+ . min_by_key ( |( _, group) | group. len ( ) )
925
+ {
926
926
group. push ( idx) ;
927
927
} else {
928
928
// Create a new group if no existing group fits
0 commit comments