gene-bordegaray commented on code in PR #23239:
URL: https://github.com/apache/datafusion/pull/23239#discussion_r3505686543


##########
datafusion/physical-optimizer/src/ensure_requirements/enforce_distribution.rs:
##########
@@ -1245,15 +1180,58 @@ pub fn ensure_distribution(
                     child = add_merge_on_top(child, removed_fetch);
                 }
                 Distribution::HashPartitioned(exprs) => {
+                    let child_partitions =
+                        child.plan.output_partitioning().partition_count();
+                    let hash_satisfied = child
+                        .plan
+                        .output_partitioning()
+                        .satisfaction(
+                            &requirement,
+                            child.plan.equivalence_properties(),
+                            allow_subset_satisfy_partitioning,
+                        )
+                        .is_satisfied();
+                    let range_satisfied_for_aggregate =
+                        aggregate_can_reuse_range_partitioning(&plan)
+                            && allow_subset_satisfy_partitioning
+                            && range_partitioning_satisfies_key_partitioning(
+                                child.plan.output_partitioning(),
+                                exprs,
+                                child.plan.equivalence_properties(),
+                                allow_subset_satisfy_partitioning,
+                            );
+
+                    // When subset satisfaction is enabled, preserve an
+                    // already-satisfying partitioning. Otherwise, hash
+                    // repartition may also increase parallelism.
+                    let needs_hash_repartition = if 
allow_subset_satisfy_partitioning {
+                        !hash_satisfied
+                    } else {
+                        !hash_satisfied || target_partitions > child_partitions
+                    };
+                    let should_add_hash_repartition = hash_necessary
+                        && needs_hash_repartition
+                        && !range_satisfied_for_aggregate;
+

Review Comment:
   I understand this rule well and have spent a good amount of time in this. 
Without huge churn, the complexity is mostly unavoidable but I do think there 
is room for a separete PR / effort to reshape the logic around enforce 
distribution but should not be here.



-- 
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