You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/12/13 20:04:01 UTC

[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #4586: Add need_data_exchange in the ExecutionPlan to indicate whether a physical operator needs data exchange

alamb commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1047656709


##########
datafusion/core/src/physical_optimizer/enforcement.rs:
##########
@@ -835,13 +836,42 @@ fn new_join_conditions(
     new_join_on
 }
 
+/// Within this function, it checks whether we need to add additional plan operators

Review Comment:
   Thank you for these comments



##########
datafusion/core/src/physical_optimizer/enforcement.rs:
##########
@@ -885,14 +915,9 @@ fn ensure_distribution_and_ordering(
                 Ok(child)
             } else {
                 let sort_expr = required.unwrap().to_vec();
-                if child.output_partitioning().partition_count() > 1 {
-                    Ok(Arc::new(SortExec::new_with_partitioning(
-                        sort_expr, child, true, None,
-                    )) as Arc<dyn ExecutionPlan>)
-                } else {
-                    Ok(Arc::new(SortExec::try_new(sort_expr, child, None)?)
-                        as Arc<dyn ExecutionPlan>)
-                }
+                Ok(Arc::new(SortExec::new_with_partitioning(

Review Comment:
   What is the rationale for this change? The previous code appears to avoid consolidating to a single partition if the child had more than one output partition.



##########
datafusion/core/src/physical_plan/planner.rs:
##########
@@ -838,22 +837,7 @@ impl DefaultPhysicalPlanner {
                             )),
                         })
                         .collect::<Result<Vec<_>>>()?;
-                    // If we have a `LIMIT` can run sort/limts in parallel (similar to TopK)
-                    Ok(if fetch.is_some() && session_state.config.target_partitions() > 1 {
-                        let sort = SortExec::new_with_partitioning(
-                            sort_expr,
-                            physical_input,
-                            true,
-                            *fetch,
-                        );
-                        let merge = SortPreservingMergeExec::new(
-                            sort.expr().to_vec(),
-                            Arc::new(sort),
-                        );
-                        Arc::new(merge)
-                    } else {
-                        Arc::new(SortExec::try_new(sort_expr, physical_input, *fetch)?)
-                    })
+                    Ok(Arc::new(SortExec::try_new(sort_expr, physical_input, *fetch)?))

Review Comment:
   Is the rationale here that the enforcement phase now creates the pattern with sort and merge?



##########
datafusion/core/src/physical_plan/mod.rs:
##########
@@ -243,6 +243,34 @@ pub trait ExecutionPlan: Debug + Send + Sync {
     fn statistics(&self) -> Statistics;
 }
 
+/// Indicate whether a data exchange is needed, which will be very helpful

Review Comment:
   ```suggestion
   /// Indicate whether a data exchange is needed at the input of `plan`, which will be very helpful
   ```



##########
datafusion/core/src/physical_optimizer/enforcement.rs:
##########
@@ -835,13 +836,42 @@ fn new_join_conditions(
     new_join_on
 }
 
+/// Within this function, it checks whether we need to add additional plan operators
+/// of data exchanging and data ordering to satisfy the required distribution and ordering.
+/// And we should avoid to manually add plan operators of data exchanging and data ordering in other places
 fn ensure_distribution_and_ordering(
     plan: Arc<dyn crate::physical_plan::ExecutionPlan>,
     target_partitions: usize,
 ) -> Result<Arc<dyn crate::physical_plan::ExecutionPlan>> {
     if plan.children().is_empty() {
         return Ok(plan);
     }
+    // It's mainly for changing the single node global SortExec to
+    // the SortPreservingMergeExec with multiple local SortExec.
+    // What's more, if limit exists, it can also be pushed down to the local sort
+    let plan = plan
+        .as_any()
+        .downcast_ref::<SortExec>()
+        .and_then(|sort_exec| {
+            // If it's already preserving the partitioning, it can be regarded as a local sort
+            // and there's no need for this optimization
+            if !sort_exec.preserve_partitioning() {

Review Comment:
   I wonder if this should also check if the `SortExec`s input has has more than one partition -- otherwise the SortPreservingMerge will be a noop. 



##########
datafusion/core/tests/sql/joins.rs:
##########
@@ -2040,8 +2040,8 @@ async fn left_semi_join() -> Result<()> {
         let physical_plan = state.create_physical_plan(&logical_plan).await?;
         let expected = if repartition_joins {
             vec![
-                "SortExec: [t1_id@0 ASC NULLS LAST]",
-                "  CoalescePartitionsExec",
+                "SortPreservingMergeExec: [t1_id@0 ASC NULLS LAST]",
+                "  SortExec: [t1_id@0 ASC NULLS LAST]",

Review Comment:
   This is a cool plan -- it is the first time I know of that DataFusion crates plans using `SortPreservingMerge` directly 👍 
   
   The rationale for this, as I understand it, is to support sorting in parallel. Internally the Sort operator does use a Merge if it has multiple input partitions. I wonder if we have any opinions about making the parallelism explicit in the plan (like this) or implict (within the operator)?
   
   I think explicit in the plan is consistent with other parts of DataFusion (e.g. `CoalscePartionExec`)
   
   fyi @tustvold 



##########
datafusion/core/src/physical_plan/mod.rs:
##########
@@ -243,6 +243,34 @@ pub trait ExecutionPlan: Debug + Send + Sync {
     fn statistics(&self) -> Statistics;
 }
 
+/// Indicate whether a data exchange is needed, which will be very helpful
+/// especially for the distributed engine to judge whether need to deal with shuffling.
+/// Currently there are 3 kinds of execution plan which needs data exchange
+///     1. RepartitionExec for changing the partition number between two operators
+///     2. CoalescePartitionsExec for collapsing all of the partitions into one without ordering guarantee
+///     3. SortPreservingMergeExec for collapsing all of the sorted partitions into one with ordering guarantee
+pub fn need_data_exchange(plan: Arc<dyn ExecutionPlan>) -> bool {

Review Comment:
   I wonder if you could generalize this code so that it compared `plan.child().output_partitioning()` and `plan.required_input_distribution()`
   
   Though it was not trivial when I was thinking about it



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org