2010YOUY01 opened a new pull request, #7337:
URL: https://github.com/apache/arrow-datafusion/pull/7337

   ## 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.
   -->
   
   NA
   
   ## Rationale for this change
   This PR would like to support `quantile_cont()` and `quantile_disc()` 
statistical aggregate functions.
   
   `select quantile_cont(score, 0.3) from test_scores` will sort the `score` 
column in ASC order and calculate its 30% percentile.
   It has been implemented in DucbDB: 
https://duckdb.org/docs/sql/aggregates.html#statistical-aggregates
   
   The more common equivalent syntax is:
   ```
   SELECT percentile_cont(0.3) WITHIN GROUP (ORDER BY score)
   FROM test_scores;
   ```
   this `within group` syntax is available in PostgreSQL, Spark, etc.
   https://www.postgresql.org/docs/9.5/functions-aggregate.html
   
   <!--
    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?
   
   Reuse the original `Accumulator` in `median()` aggregate function (moved 
from median.rs->percentile.rs) to implement `quantile_*()` functions.
   Now the `PercentileAccumulator` is used in 
`median/quantile_disc/quantile_cont` aggregate functions.
   
   I think this is one of the trickier aggregate functions: regular aggregate 
functions only maintain O(1) internal states inside `Accumulator` (e.g. 
`AvgAccumulator` only have to keep `sum` and `count`), while 
`PercentileAccumulator` have to keep a O(n) internal state.
   It will cache all data during aggregation, and sort the data and fetch the 
target percentile at final evaluation.
   This PR only extends the functionality to support new aggregate functions, 
but the underlying `Accumulator` is worth taking a look at for optimizations in 
the future.
   <!--
   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?
   sqllogictests
   <!--
   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?
   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