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

   # 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 #.
   
   # 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 Datafusion can detect ordering equivalent expressions. This 
enables us to do additional optimizations during planning. 
   
   Consider query below
   ```sql
   SELECT c9, rn1 FROM (SELECT c9,
          ROW_NUMBER() OVER(ORDER BY c9 ASC) as rn1
          FROM aggregate_test_100
          ORDER BY c9 ASC)
      ORDER BY rn1
   ```
   It produces the following plan
   ```
   "ProjectionExec: expr=[c9@0 as c9, ROW_NUMBER() ORDER BY 
[aggregate_test_100.c9 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND 
CURRENT ROW@1 as rn1]",
   "  BoundedWindowAggExec: wdw=[ROW_NUMBER(): Ok(Field { name: 
\"ROW_NUMBER()\", data_type: UInt64, nullable: false, dict_id: 0, 
dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, 
start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }], mode=[Sorted]",
   "    SortExec: expr=[c9@0 ASC NULLS LAST]",
   "      CsvExec: files={1 group: [[SOURCE_PATH]]}, has_header=true, 
limit=None, projection=[c9]",
   ```
   thanks to `ROW_NUMBER` result is ordered, and it can be detected by 
Datafusion (last SortExec is removed since `c9 ASC`, and `rn1 ASC` describes 
same ordering.). 
   When `ROW_NUMBER` window clause contains `PARTITION BY`, `ROW_NUMBER` result 
is not globally ordered. Hence we shouldn't add its results to the ordering 
equivalences. Currently this check is not done. 
   The query below 
   ```sql
   SELECT c9, rn1 FROM (SELECT c9,
          ROW_NUMBER() OVER(PARTITION BY c1 ORDER BY c9 ASC) as rn1
          FROM aggregate_test_100
          ORDER BY c9 ASC)
      ORDER BY rn1
   ```
   still removes last SortExec since it thinks that `c9 ASC`, and `rn1 ASC` 
describes same ordering (which is not the case).
   
   This PR adds partition by expression check, to prevent above case hapenning.
   Please note that, when different partitions are separated by ordering (all 
partition by expressions are set equal with existing ordering expressions), we 
can add newly introduced ordering to ordering equivalences. 
   
   # 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?
   Yes, new tests are added
   <!--
   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