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

   ## 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.
   -->
   
   part of #17789 
   
   ## 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.  
   -->
   `string_agg` is slow, see the tracking issue for details.
   
   This PR added a new `Accumulator` implementation for the simple case: if 
there is no `DISTINCT` and `ORDER BY` like `string_agg(distinct str, ',' ORDER 
BY str)`, use `SimpleStringAggAccumulator` instead. The original 
`StringAggAccumulator` is used for the general case with potential `DISTINCT` 
and `ORDER BY`s.
   
   While @vegarsti is working on a `GroupsAccumulator` solution that can 
further speed it up potentially, I think this PR is still necessary because the 
single group case like `select string_agg(str, ',') from t1` is still using the 
`Accumulator` interface instead of `GroupsAccumulator`
   
   I haven't checked the original implementation yet, and I don't know why is 
it so slow. It's using `array_agg` internally, and memory bloat can be 
observed. I guess the reason is redundant transcoding, and incorrect operations 
on `StringView` buffers.
   There are several ongoing work to improve `arrray_agg`: 
https://github.com/apache/datafusion/issues/17829
   
   ### Benchmark
   It's around 1000x faster. See the original issue for data setup, scaling 
factor 0.1 is used for the table.
   
   ```
   DataFusion CLI v50.0.0
   > CREATE EXTERNAL TABLE partsupp
   STORED AS PARQUET
   LOCATION '/Users/yongting/Code/datafusion-sqlstorm/data/partsupp.parquet';
   
   > select ps_partkey, string_agg(ps_comment, ';')
   from partsupp
   group by ps_partkey;
   ```
   
   Before: ~50s
   PR: 0.05s
   
   ## 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.
   -->
   1. Implemented a `SimpleStringAggAccumulator` for `string_agg`
   2. In the aggregate function implementation, opt for the new accumulator if 
there is no `DISTINCT` and `ORDER BY` in the `string_agg()` aggregate function.
   
   ## Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   3. 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)?
   -->
   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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to