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

   # 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 [#5100](https://github.com/apache/arrow-datafusion/issues/5100).
   
   # 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.
   -->
   We want to have the ability to leverage parallelism in sorts when 
appropriate, which requires `EnforceSorting` to be partition aware. We also 
want to further decouple `EnforceSorting` and `EnforceDistribution` rules, with 
the end goal being complete orthogonality. Currently, there is coupling in both 
directions: In order to produce valid plans, one needs to apply the 
`EnforceDistribution` rule before applying `EnforceSorting`, and the 
`EnforceDistribution` breaks sorting requirements and produces invalid plans on 
its own.
   
   With this PR, coupling is broken in one direction (`EnforceSorting` does not 
require `EnforceDistribution` to work correctly anymore), but not the other way 
yet (`EnforceDistribution` may still invalidate sort correctness, so one still 
needs to call `EnforceSorting` after that). Note that it is on our TODO list to 
fix the other direction too.
   
   # 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.
   -->
   
   This PR adds a couple of functionalities to the `EnforceSorting` rule. 
   
   - Adds parallelize option to the`EnforceSorting` rule. If this flag is 
enabled. Physical plans of the form
       
       ```rust
       "SortExec: [t1_id@0 ASC NULLS LAST]",
       "  CoalescePartitionsExec",
       "    RepartitionExec: partitioning=RoundRobinBatch(2), 
input_partitions=1",
       ```
       
       will turn into the plan below:
       
       ```rust
       "SortPreservingMergeExec: [t1_id@0 ASC NULLS LAST]",
       "  SortExec: [t1_id@0 ASC NULLS LAST]",
       "    RepartitionExec: partitioning=RoundRobinBatch(2), 
input_partitions=1",
       ```
       
       This increases speed in multi-threaded environments. You can see time 
comparisons below:
       
       | n_row | distinct | batch_size | Repartition Mode(8) | Rule On (mean) | 
Rule On(Median) | Rule Off (mean) | Rule Off(Median) |
       | --- | --- | --- | --- | --- | --- | --- | --- |
       | 1000 | 100 | 100 | RoundRobin | 3.015032ms | 2.988125ms | 5.213412ms | 
4.515166ms |
       | 1000 | 100 | 100 | Hash | 6.00907ms | 5.895166ms | 10.636287ms | 
10.3025ms |
       | 10_000 | 100 | 100 | RoundRobin | 24.429487ms | 24.042291ms | 
84.282599ms | 84.117083ms |
       | 10_000 | 100 | 100 | Hash | 48.779278ms | 48.970708ms | 517.195637ms | 
520.449625ms |
       | 20_000 | 100 | 100 | RoundRobin | 47.297341ms | 47.141041ms | 
289.02502ms | 290.58625ms |
       | 20_000 | 100 | 100 | Hash | 114.230287ms | 107.842291ms | 1.992879062s 
| 2.002725333s |
       | 50_000 | 100 | 100 | RoundRobin | 117.185937ms | 115.300583ms | 
1.604551912s | 1.609479958s |
       | 50_000 | 100 | 100 | Hash | 425.755595ms | 430.346625ms | 
12.683566324s | 12.502681458s |
   - Issue [#5100](https://github.com/apache/arrow-datafusion/issues/5100) is 
closed, fixing the limit bug.
   - The `EnforceSorting` rule now also considers `SortPreservingMergeExec`s 
when performing optimizations. This enables us to optimize more queries.
   - We handle cases where a `UnionExec` partially maintains the ordering of 
some of its children, such as the case below:
       
       ```rust
       "  UnionExec",
       "    SortExec: [nullable_col@0 ASC,non_nullable_col@1 ASC]",
       "      ParquetExec: limit=None, partitions={1 group: [[x]]}, 
projection=[nullable_col, non_nullable_col]",
       "    ParquetExec: limit=None, partitions={1 group: [[x]]}, 
output_ordering=[nullable_col@0 ASC], projection=[nullable_col, 
non_nullable_col]",
       ```
       
       In this case, output ordering of `UnionExec` is `nullable_col@0 ASC`. It 
maintains the ordering of its second child and partially maintains the ordering 
of its first child. The `EnforceSorting` rule now considers such 
partially-order-maintaining paths during optimization. 
       
   - In order to handle multi-children operators like `UnionExec`s properly, 
the assumption that ordering only comes from a single path in the physical plan 
tree is removed. Now ordering can come from multiple paths.
   
   # 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)?
   -->
   
   We have added multiple tests to cover above mentioned functionalities. 
Approximately 550 lines of the changes come from test or test utils.
   
   # Are there any user-facing changes?
   
   No.
   
   <!--
   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: [email protected]

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

Reply via email to