jayzhan211 commented on code in PR #24766:
URL: https://github.com/apache/datafusion/pull/24766#discussion_r4026787414


##########
datafusion/physical-optimizer/src/ensure_requirements/enforce_distribution.rs:
##########
@@ -1548,37 +1577,78 @@ pub fn ensure_distribution(
                             && target_partitions > child_partitions;
 
                     // When subset satisfaction is enabled, preserve an
-                    // already-satisfying partitioning. Otherwise, hash
+                    // already-satisfying partitioning. Otherwise,
                     // repartition may also increase parallelism.
-                    let needs_hash_repartition = if 
allow_subset_satisfy_partitioning {
+                    let needs_repartition = if 
allow_subset_satisfy_partitioning {
                         !partitioning_satisfied
                     } else {
                         !partitioning_satisfied
                             || (target_partitions > child_partitions
                                 && !preserve_satisfying_file_partitioning)
                     };
-                    let should_add_hash_repartition =
-                        hash_necessary && needs_hash_repartition;
+                    let should_add_repartition = hash_necessary && 
needs_repartition;
 
                     // See 
https://github.com/apache/datafusion/issues/18341#issuecomment-3503238325 for 
background
-                    // When inserting hash is necessary to satisfy hash 
requirement, insert hash repartition.
-                    if should_add_hash_repartition {
+                    // Enforce unmet requirements, or increase parallelism 
when beneficial.
+                    if should_add_repartition {
+                        let partitioning = match 
child.plan.output_partitioning() {
+                            Partitioning::Range(range) if 
partitioning_satisfied => {
+                                // A satisfying range layout remains useful to 
the
+                                // co-partitioning pass even when its samples 
cannot
+                                // support the preferred degree of parallelism.
+                                match range.scale(target_partitions) {
+                                    Ok(range) => {
+                                        let scaled = 
Partitioning::Range(range);
+                                        // A single partition satisfies any 
key requirement,
+                                        // but scaling it must still use 
compatible keys.
+                                        if scaled
+                                            .satisfaction(
+                                                &requirement,
+                                                
child.plan.equivalence_properties(),
+                                                false,
+                                            )
+                                            .is_satisfied()
+                                        {
+                                            scaled_native_range =
+                                                
!child.plan.is::<RepartitionExec>();
+                                            Some(scaled)
+                                        } else {
+                                            Some(
+                                                requirement
+                                                    .clone()
+                                                    
.create_partitioning(target_partitions),
+                                            )
+                                        }
+                                    }
+                                    
Err(RangePartitioningScaleError::InsufficientSamples { .. }) => {
+                                        preserved_unscalable_range = true;
+                                        None

Review Comment:
   On `InsufficientSamples` the Range input keeps its partition count even with 
`preserve_file_partitions = 0`, and even for single-input requirements (e.g. an 
aggregate) where nothing needs to line up. Range sources today are built with 
`try_new` (max == current count), so all existing Range users lose parallelism. 
`range_satisfaction_config_matrix` `(NOT_MET, DISABLED, GREATER)` flips `Hash` 
→ `Reuse`, and sqllogictest TEST 13 now joins at 4 partitions with 
`target_partitions = 5`. Please keep the layout only when the inputs are 
co-partitioned (and confirm on #24712 whether joins should also require 
`preserve_file_partitions`):
   
   ```diff
   -                                    
Err(RangePartitioningScaleError::InsufficientSamples { .. }) => {
   +                                    
Err(RangePartitioningScaleError::InsufficientSamples { .. })
   +                                        if 
input_distributions.is_co_partitioned() =>
   +                                    {
                                            preserved_unscalable_range = true;
                                            None
                                        }
   +                                    
Err(RangePartitioningScaleError::InsufficientSamples { .. }) => Some(
   +                                        requirement
   +                                            .clone()
   +                                            
.create_partitioning(target_partitions),
   +                                    ),
                                        Err(error) => return Err(error.into()),
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to