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

   # 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 [#5915](https://github.com/apache/arrow-datafusion/issues/5915).
   
   # 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.  
   -->
   Currently when window frame is `PARTITION BY a,b ..` and input is ordered by 
`b, a`. `WindowAggExec` expects its input to be ordered by `a, b`. Hence we 
need to add another `SortExec` before `WindowAggExec` to change ordering from 
`b, a` to `a, b`. However, if we were to use `PARTITION BY b,a ..` existing 
ordering would work. The algorithm for removing sort should treat partition by 
columns as set. Algorithm shouldn't depend on the order of the columns. With 
this functionality we can produce better plans.
   
   # 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.
   -->
   
   With the changes in this PR we do WindowSortRemoval analysis more cleverly. 
Previously, algorithm was depended on the order of partition by expressions 
seen in the window expression. Now we treat `PARTITION BY` columns as 
mathematical set. Consider the query below
   ```sql
   SELECT c9,
     SUM(c9) OVER(PARTITION BY c1, c2 ORDER BY c9 ASC ROWS BETWEEN 1 PRECEDING 
AND UNBOUNDED FOLLOWING) as sum1,
     SUM(c9) OVER(PARTITION BY c2, c1 ORDER BY c9 ASC ROWS BETWEEN 1 PRECEDING 
AND UNBOUNDED FOLLOWING) as sum2
   FROM aggregate_test_100
   ``` 
   Previously it was generating the plan
   ``` sql
   "ProjectionExec: expr=[c9@8 as c9, SUM(aggregate_test_100.c9) PARTITION BY 
[aggregate_test_100.c1, aggregate_test_100.c2] ORDER BY [aggregate_test_100.c9 
ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING@13 as sum1, 
SUM(aggregate_test_100.c9) PARTITION BY [aggregate_test_100.c2, 
aggregate_test_100.c1] ORDER BY [aggregate_test_100.c9 ASC NULLS LAST] ROWS 
BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING@14 as sum2]",
   "  WindowAggExec: wdw=[SUM(aggregate_test_100.c9): Ok(Field { name: 
\"SUM(aggregate_test_100.c9)\", data_type: UInt64, nullable: true, dict_id: 0, 
dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, 
start_bound: Preceding(UInt64(1)), end_bound: Following(UInt64(NULL)) }]",
   "    SortExec: expr=[c2@1 ASC NULLS LAST,c1@0 ASC NULLS LAST,c9@8 ASC NULLS 
LAST]",
   "      WindowAggExec: wdw=[SUM(aggregate_test_100.c9): Ok(Field { name: 
\"SUM(aggregate_test_100.c9)\", data_type: UInt64, nullable: true, dict_id: 0, 
dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, 
start_bound: Preceding(UInt64(1)), end_bound: Following(UInt64(NULL)) }]",
   "        SortExec: expr=[c1@0 ASC NULLS LAST,c2@1 ASC NULLS LAST,c9@8 ASC 
NULLS LAST]",
   "          CsvExec: files={1 group: [[<path>/aggregate_test_100.csv]]}, 
has_header=true, limit=None, projection=[c1, c2, c3, c4, c5, c6, c7, c8, c9, 
c10, c11, c12, c13]",
   ``` 
   we now produce plan below
   ``` sql
   "ProjectionExec: expr=[c9@8 as c9, SUM(aggregate_test_100.c9) PARTITION BY 
[aggregate_test_100.c1, aggregate_test_100.c2] ORDER BY [aggregate_test_100.c9 
ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING@13 as sum1, 
SUM(aggregate_test_100.c9) PARTITION BY [aggregate_test_100.c2, 
aggregate_test_100.c1] ORDER BY [aggregate_test_100.c9 ASC NULLS LAST] ROWS 
BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING@14 as sum2]",
   "  WindowAggExec: wdw=[SUM(aggregate_test_100.c9): Ok(Field { name: 
\"SUM(aggregate_test_100.c9)\", data_type: UInt64, nullable: true, dict_id: 0, 
dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, 
start_bound: Preceding(UInt64(1)), end_bound: Following(UInt64(NULL)) }]",
   "    WindowAggExec: wdw=[SUM(aggregate_test_100.c9): Ok(Field { name: 
\"SUM(aggregate_test_100.c9)\", data_type: UInt64, nullable: true, dict_id: 0, 
dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, 
start_bound: Preceding(UInt64(1)), end_bound: Following(UInt64(NULL)) }]",
   "      SortExec: expr=[c1@0 ASC NULLS LAST,c2@1 ASC NULLS LAST,c9@8 ASC 
NULLS LAST]",
   "        CsvExec: files={1 group: [[<path>/aggregate_test_100.csv]]}, 
has_header=true, limit=None, projection=[c1, c2, c3, c4, c5, c6, c7, c8, c9, 
c10, c11, c12, c13]",
   ``` 
   
   
   # Are these changes tested?
   New tests are added to check partition by columns are treated as set.
   <!--
   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: [email protected]

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

Reply via email to