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

   # 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
   Some of the  seemingly conflicting ordering requirements can be resolved by 
reversing the aggregation function if possible.
   For instance, consider query below
   ```sql
   SELECT country, FIRST_VALUE(amount ORDER BY amount ASC) AS fv1,
     LAST_VALUE(amount ORDER BY amount DESC) AS fv2,
     ARRAY_AGG(amount ORDER BY amount ASC) AS amounts
     FROM sales_global
     GROUP BY country
   ```
   requirements `amount ASC` by `FIRST_VALUE`, and `amount DESC` by `LAST_VALUE 
`are conflicting. However, `FIRST_VALUE(amount ORDER BY amount ASC) AS fv1` is 
equivalent to `LAST_VALUE(amount ORDER BY amount DESC) AS fv1`, in terms of 
behavior. Hence by converting query above to its equivalent form below
   ```sql
   SELECT country, LAST_VALUE(amount ORDER BY amount DESC) AS fv1,
     LAST_VALUE(amount ORDER BY amount DESC) AS fv2,
     ARRAY_AGG(amount ORDER BY amount ASC) AS amounts
     FROM sales_global
     GROUP BY country
   ```
   we can resolve conflicting requirement, and successfully run equivalent 
version.
   
   As another use case assume that, `sales_global` table is ordered by `ts 
ASC`. Query below,
   ```sql
   SELECT country, LAST_VALUE(amount ORDER BY ts DESC) AS lv1
     FROM sales_global
     GROUP BY country
   ```
   It produces following plan
   ```sql
   ProjectionExec: expr=[country@0 as country, LAST_VALUE(sales_global.amount) 
ORDER BY [sales_global.ts DESC NULLS FIRST]@1 as lv1]
     AggregateExec: mode=Single, gby=[country@0 as country], 
aggr=[LAST_VALUE(sales_global.amount)]
       SortExec: expr=[ts@1 DESC]
         MemoryExec: partitions=1, partition_sizes=[1], output_ordering=[ts ASC]
   ```
   However, if we were to treat above query  in its equivalent for like below
   ```sql
   SELECT country, FIRST_VALUE(amount ORDER BY ts ASC) AS lv1
     FROM sales_global
     GROUP BY country
   ``` 
   we can produce following plan
   ```sql
   ProjectionExec: expr=[country@0 as country, FIRST_VALUE(sales_global.amount) 
ORDER BY [sales_global.ts ASC NULLS LAST]@1 as lv1]
     AggregateExec: mode=Single, gby=[country@0 as country], 
aggr=[LAST_VALUE(sales_global.amount)]
       MemoryExec: partitions=1, partition_sizes=[1], output_ordering=[ts ASC]
   ```
   <!--
    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?
   This PR analyzes reverse ordering requirements, both to resolve conflicting 
ordering requirements and also remove away `SortExec`s from the physical plan, 
if it is possible.
   <!--
   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 to `groupby.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