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/12 08:03:07 UTC

[GitHub] [arrow-datafusion] yahoNanJing opened a new pull request, #4586: Add a trait DataExchangeExecutionPlan to indicate whether an execution plan needs a data exchange

yahoNanJing opened a new pull request, #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #4585.
   
   # Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   # What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   # Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   # Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->


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


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

Posted by GitBox <gi...@apache.org>.
yahoNanJing commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1048233829


##########
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:
   yes. I'll add more comments 



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


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

Posted by GitBox <gi...@apache.org>.
Dandandan commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1048321953


##########
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:
   Both excellent points.
   
   * Limit looked like a relatively good heuristic, as one often provides a smaller value than the size of the input like 10, 100, 1000 or 10K but not 10M, 100M etc. One could fine tune the heuristic by only doing it on a "sufficiently" small limit. The limit causes the number of rows as input to `SortPreservingMergeExec` to be only `partitions * limit` (beside also speeding up the parallel sort).
   
   * I am not sure whether we should expect `SortPreservingMergeExec` + parallel `SortExec` to be (much) faster than  `CoalescePartitionsExec` + `SortExec`.  `SortPreservingMergeExec` itself is running on one partition/thread, is a relative heavy operation and also needs to wait on the sorted input partitions to be all ready.
   
   Maybe with some fine-tuning (or maybe already after https://github.com/apache/arrow-datafusion/pull/4301) it is possible it is faster than a single-threaded `SortExec` in certain cases, but other approaches like using range partitioning will be better for query parallelism.



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


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

Posted by GitBox <gi...@apache.org>.
mingmwang commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1047955341


##########
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:
   Agree. I think this check is still required to avoid unnecessary shuffle/data exchange.
   The reason is that the SortExec's input might be another `CoalescePartitionsExec`,  it is unnecessary to change `SortExec` to `SortPreservingMerge` + parallel `SortExec`
   



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


[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

Posted by GitBox <gi...@apache.org>.
alamb commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1050048160


##########
datafusion/core/src/physical_optimizer/enforcement.rs:
##########
@@ -835,13 +836,47 @@ 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| {
+            // There are three situations that there's no need for this optimization
+            // - There's only one input partition;
+            // - It's already preserving the partitioning so that it can be regarded as a local sort
+            // - There's no limit pushed down to the local sort (It's still controversial)
+            if sort_exec.input().output_partitioning().partition_count() > 1
+                && !sort_exec.preserve_partitioning()
+                && sort_exec.fetch().is_some()

Review Comment:
   FYI @Dandandan  the check for local limit has been restored



##########
datafusion/core/src/physical_optimizer/enforcement.rs:
##########
@@ -835,13 +836,47 @@ 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| {
+            // There are three situations that there's no need for this optimization
+            // - There's only one input partition;
+            // - It's already preserving the partitioning so that it can be regarded as a local sort
+            // - There's no limit pushed down to the local sort (It's still controversial)

Review Comment:
   Might be worth a link to the ticket / PR for anyone who sees this comment and wants more context / backstore



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


[GitHub] [arrow-datafusion] yahoNanJing commented on pull request #4586: Add a trait DataExchangeExecutionPlan to indicate whether an execution plan needs a data exchange

Posted by GitBox <gi...@apache.org>.
yahoNanJing commented on PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#issuecomment-1346043578

   Hi @mingmwang, @alamb, could you help review this PR?


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


[GitHub] [arrow-datafusion] mingmwang commented on a diff in pull request #4586: Add a trait DataExchangeExecutionPlan to indicate whether an execution plan needs a data exchange

Posted by GitBox <gi...@apache.org>.
mingmwang commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1045582422


##########
datafusion/core/src/physical_plan/mod.rs:
##########
@@ -580,6 +580,14 @@ impl Distribution {
     }
 }
 
+/// A trait indicated the related execution plan needs a data exchange, 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 trait DataExchangeExecutionPlan: ExecutionPlan {}
+

Review Comment:
   👍



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


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

Posted by GitBox <gi...@apache.org>.
mingmwang commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1047948804


##########
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:
   Why `SortPreservingMergeExec` + parallel `SortExec` is slower than `CoalescePartitionsExec` + `SortExec`?  Maybe it depends on the input data volumes need to be sorted.
   Do you have some test data and SQL ?



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


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

Posted by GitBox <gi...@apache.org>.
yahoNanJing commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1048233829


##########
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:
   yes. I'll add more comments 



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


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

Posted by GitBox <gi...@apache.org>.
Dandandan commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1051106998


##########
datafusion/core/src/physical_optimizer/enforcement.rs:
##########
@@ -835,13 +836,47 @@ 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| {
+            // There are three situations that there's no need for this optimization
+            // - There's only one input partition;
+            // - It's already preserving the partitioning so that it can be regarded as a local sort
+            // - There's no limit pushed down to the local sort (It's still controversial)
+            if sort_exec.input().output_partitioning().partition_count() > 1
+                && !sort_exec.preserve_partitioning()
+                && sort_exec.fetch().is_some()

Review Comment:
   👍 



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


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

Posted by GitBox <gi...@apache.org>.
mingmwang commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1046920847


##########
datafusion/core/src/physical_optimizer/enforcement.rs:
##########
@@ -835,13 +836,40 @@ 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);
     }
+    // If we have a `LIMIT` can run sort/limits in parallel (similar to TopK)
+    let plan = plan

Review Comment:
   👍



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


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

Posted by GitBox <gi...@apache.org>.
alamb commented on PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#issuecomment-1348416036

   > By the way, for the last commit for UT work around, if we upgrade the arrow-rs to 29.0.0, the issue can be avoided and the last commit in this PR can be reverted.
   
   I think @tustvold  has a PR close to ready to go for the upgrade:  https://github.com/apache/arrow-datafusion/pull/4587
   
   I'll try and get it ready later today if he hasn't had a chance
   


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


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

Posted by GitBox <gi...@apache.org>.
yahoNanJing commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1048198883


##########
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:
   Agree. Although the current implementation will not introduce additional shuffling, since SortPreservingMerge will also check the input partition count. I'll add the check



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


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

Posted by GitBox <gi...@apache.org>.
yahoNanJing commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1048252081


##########
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:
   Yes. I'll add some comments there. Otherwise, it may be misleading.



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


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

Posted by GitBox <gi...@apache.org>.
Dandandan merged PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586


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


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

Posted by GitBox <gi...@apache.org>.
yahoNanJing commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1046626071


##########
datafusion/core/src/physical_optimizer/enforcement.rs:
##########
@@ -835,13 +836,40 @@ 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);
     }
+    // If we have a `LIMIT` can run sort/limits in parallel (similar to TopK)
+    let plan = plan

Review Comment:
   Thanks for the suggestion. Refined the comments.



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


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

Posted by GitBox <gi...@apache.org>.
Dandandan commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1047694329


##########
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:
   In a previous PR for TopK-queries I added the check on a `LIMIT` to be present on purpose, 
   The reason is that `SortPreservingMergeExec` + parallel `SortExec` is slower than `CoalescePartitionsExec` + `SortExec`   in my tests, so actually making queries run quite a bit slower. I think we have to use other means to parallelize sorts, such as range partitioning.



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


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

Posted by GitBox <gi...@apache.org>.
yahoNanJing commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1048282610


##########
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'm not sure whether we can decide whether a data exchange is needed only depends on the `plan.child().output_partitioning()` and `plan.required_input_distribution()`. For example, how can we make a decision for Join which has two children. The enforcement rule will decide and add necessary physical operators like, `RepartitionExec`, `CoalescePartitionsExec`, `SortPreservingMergeExec`. This should not be part of this function.



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


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

Posted by GitBox <gi...@apache.org>.
yahoNanJing commented on PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#issuecomment-1352446564

   Hi @alamb, yes. This PR is ready for another round of review except for the concern of the removal for the limit check. 


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


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

Posted by GitBox <gi...@apache.org>.
yahoNanJing commented on PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#issuecomment-1352446852

   Maybe I can firstly add back the limit check. 


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


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

Posted by GitBox <gi...@apache.org>.
ursabot commented on PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#issuecomment-1355528990

   Benchmark runs are scheduled for baseline = ca8985ef17739c2d342ac987b3de30a29a5fc76d and contender = 920f11a64aa4d7a4571944c96d2d49f21aae2c7e. 920f11a64aa4d7a4571944c96d2d49f21aae2c7e is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
   Conbench compare runs links:
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ec2-t3-xlarge-us-east-2] [ec2-t3-xlarge-us-east-2](https://conbench.ursa.dev/compare/runs/aabd6317b7404740a0678d7ddbca143b...a83967104e8f4de29a66b7e51e204503/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on test-mac-arm] [test-mac-arm](https://conbench.ursa.dev/compare/runs/052da2f43b934a6a909e7b1fc4b00a98...e7ce897f434a4d59bfbc15da5c5e03c4/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ursa-i9-9960x] [ursa-i9-9960x](https://conbench.ursa.dev/compare/runs/94f349672c0149a48ac34120d826b58e...73a2f3ab7ffd4bc98d69c0154a46641d/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ursa-thinkcentre-m75q] [ursa-thinkcentre-m75q](https://conbench.ursa.dev/compare/runs/4407805b916d452aa6f73e884f3c63b8...145df0abcdc24d8aab7ae264e0d18d35/)
   Buildkite builds:
   Supported benchmarks:
   ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
   test-mac-arm: Supported benchmark langs: C++, Python, R
   ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
   ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java
   


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


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

Posted by GitBox <gi...@apache.org>.
Dandandan commented on PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#issuecomment-1355520052

   Nice!
   
   BTW I am not reagainst the change to use `SortpreserveExec` in more cases - if we can show we can maintain or improve the performance, then that would be perfect 👍 


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


[GitHub] [arrow-datafusion] mingmwang commented on a diff in pull request #4586: Add a trait DataExchangeExecutionPlan to indicate whether an execution plan needs a data exchange

Posted by GitBox <gi...@apache.org>.
mingmwang commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1045624266


##########
datafusion/core/src/physical_optimizer/enforcement.rs:
##########
@@ -835,13 +836,40 @@ 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);
     }
+    // If we have a `LIMIT` can run sort/limits in parallel (similar to TopK)
+    let plan = plan

Review Comment:
   I think this comment need to be modified to reflect the new purpose.



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


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

Posted by GitBox <gi...@apache.org>.
Dandandan commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1048066022


##########
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:
   I don't have a reproducable example ready unfortunately, the way I tested some queries was running some `SELECT x, y from test order by z` type of queries on TCP-h order lines table (using 16 partitions).
   
   This was before the https://github.com/apache/arrow-datafusion/pull/4301 got merged though, so maybe things are different now 🤔
   
   What about adding some similar queries to the parquet SQL benchmarks to confirm?



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


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

Posted by GitBox <gi...@apache.org>.
yahoNanJing commented on PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#issuecomment-1350822893

   Just rebased the latest master branch.


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


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

Posted by GitBox <gi...@apache.org>.
yahoNanJing commented on PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#issuecomment-1347697398

   If with this PR merged into the DataFusion, the Ballista side will be much more clear. One example is as follows:
   https://github.com/yahoNanJing/arrow-ballista/commit/9dbcedc178073ab685ee94f4f4e5883813e33118
   
   Hi @andygrove, @mingmwang, @alamb, could you help review this PR?
   
   By the way, for the last commit for UT work around, if we upgrade the arrow-rs to 29.0.0, the issue can be avoided and the last commit in this PR can be reverted.


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


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

Posted by GitBox <gi...@apache.org>.
yahoNanJing commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1048206057


##########
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:
   I think the core idea of divide and conquer for parallel sorting and merging is very general. Also curious why the performance is worse than the single node global sort. 



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


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

Posted by GitBox <gi...@apache.org>.
alamb commented on PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#issuecomment-1352103853

   Is this PR ready for another round of review?


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


[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

Posted by GitBox <gi...@apache.org>.
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


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

Posted by GitBox <gi...@apache.org>.
Dandandan commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1047694329


##########
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:
   In a previous PR for TopK-queries I added the check on a `LIMIT` to be present on purpose, 
   The reason is that `SortPreservingMergeExec` + parallel `SortExec` is slower than `CoalescePartition` + `SortExec`   in my tests, so actually making queries run quite a bit slower. I think we have to use other means to parallelize sorts, such as range partitioning.



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


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

Posted by GitBox <gi...@apache.org>.
yahoNanJing commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1048211106


##########
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:
   what's more, the reason of depending on the limit to decide whether to change to parallel sort is also not strong. Sometimes, the limit number is very large and nothing can be pruned.



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


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

Posted by GitBox <gi...@apache.org>.
mingmwang commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1047962931


##########
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:
   My understand is that the `SortExecs` added by the enforcement rule should be always local Sort. Because in the `ExecutionPlan` Trait, `output_ordering()` method specifies how data is ordered in each partition, and the `required_input_orderings()`  method specifies the sort order requirements for each partition. 
   
   @yahoNanJing 
   I think maybe you could add some comments to the `ExecutionPlan` methods and here to make it clear.



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


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

Posted by GitBox <gi...@apache.org>.
yahoNanJing commented on code in PR #4586:
URL: https://github.com/apache/arrow-datafusion/pull/4586#discussion_r1048202803


##########
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:
   Yes. It's better to avoid manually adding physical operators. It's the enforcement job.



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