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

   # 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 [#6159](https://github.com/apache/arrow-datafusion/issues/6159).
   
   # 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.  
   -->
   Datafusion can detect aliased columns (exactly same columns). This enables 
us to do additional optimizations during planning. However, same thing cannot 
be done in terms of ordering. 
   Consider query below
   ```sql
   (SELECT c9,
        ROW_NUMBER() OVER(ORDER BY c9 ASC) as rn1
        FROM aggregate_test_100
        ORDER BY c9 ASC)
   ```
   Output of this query would satisfy `c9 ASC`. However, it will also satisfy 
`rn1 ASC`. However, currently we cannot detect this.
   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
   ```
   "SortExec: expr=[rn1@1 ASC NULLS LAST]",
   "  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]",
   ```
   We could have produced the following plan, if we were to detect that `c9 
ASC` and `rn1 ASC` defines same ordering.
   ```
   "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]",
   ```
   
   # 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 new method  called `ordering_equivalence_properties` to the 
`ExecutionPlan` trait. This method is similar to the `equivalence_properties`. 
`equivalence_properties` returns columns that are aliases. 
`ordering_equivalence_properties` returns columns that defines global ordering 
for the schema (They are equal in the ordering sense). This enables us to 
optimize away unnecessary `SortExec`s in the final plan.
   
   # Are these changes tested?
   Yes, two new tests are added to the `window.slt` file.
   <!--
   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