Repository navigation
Commit 90dc436
authored
## 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 apache#123` indicates that this PR will close issue apache#123.
-->
- Related to apache#20788.
## 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.
Please explain the problem you are trying to solve in terms of the
user-visible
behavior, rather than the implementation.
For example, "The code in `foo.rs` doesn't handle nulls" is a symptom of
the
implementation. "COUNT(DISTINCT) returns wrong results when the column
contains
nulls" is the user-visible problem.
-->
Ordered `ARRAY_AGG` and `STRING_AGG` currently have high memory and
sorting overhead for large inputs.
This is relevant to apache#20788, where removing `ORDER BY` from several
`ARRAY_AGG` expressions avoids the reported memory explosion. This PR
improves the ordered `ARRAY_AGG` and `STRING_AGG` accumulators.
## 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 changes the ordered aggregate accumulator to:
- Retain payload values as Arrow arrays.
- Store compact references to payload rows.
- Encode ordering keys with `RowConverter`.
- Reuse already sorted input and partial-state runs.
- Sort remaining entries and merge the sorted runs.
- Materialize the result with Arrow `interleave` or a zero-copy slice.
The accumulator is shared by ordered `ARRAY_AGG` and `STRING_AGG`.
## 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
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)?
-->
Yes. Unit and SQL logic tests cover ordering, partial-state merging,
NULL handling, and type preservation.
## 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.
-->
No. This is an internal performance optimization
## Benchmarks
Benchmark coverage is added in apache#24391.
| Benchmark | Main runtime | Optimized runtime | Runtime improvement |
Main peak memory | Optimized peak memory | Memory reduction |
| --- | ---: | ---: | ---: | ---: | ---: | ---: |
| Ordered `ARRAY_AGG` | 897.8 ms | 85.4 ms | 10.5x | 404.9 MB | 165.1 MB
| 59.2% |
| Two ordered `ARRAY_AGG` expressions | 2048.2 ms | 150.8 ms | 13.6x |
795.3 MB | 247.8 MB | 68.8% |
| Ordered `STRING_AGG` | 895.1 ms | 96.3 ms | 9.3x | 414.6 MB | 158.0 MB
| 61.9% |
1 parent 9e1e30d commit 90dc436
5 files changed
Lines changed: 1085 additions & 285 deletions
File tree
- datafusion
- functions-aggregate
- src
- sqllogictest/test_files
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
114 | 114 | | |
115 | 115 | | |
116 | 116 | | |
| 117 | + | |
117 | 118 | | |
118 | 119 | | |
119 | 120 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| 45 | + | |
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
| |||
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
| 56 | + | |
55 | 57 | | |
56 | 58 | | |
57 | 59 | | |
| |||
0 commit comments