Skip to content

Commit 90dc436

Browse files
authored
perf: use columnar state for ordered ARRAY_AGG and STRING_AGG (apache#24392)
## 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

‎Cargo.lock‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ arrow-ipc = { version = "59.2.0", default-features = false, features = [
114114
] }
115115
arrow-ord = { version = "59.2.0", default-features = false }
116116
arrow-schema = { version = "59.2.0", default-features = false }
117+
arrow-select = { version = "59.2.0", default-features = false }
117118
async-trait = "0.1.89"
118119
bigdecimal = "0.4.8"
119120
bytes = "1.11"

‎datafusion/functions-aggregate/Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ name = "datafusion_functions_aggregate"
4242

4343
[dependencies]
4444
arrow = { workspace = true }
45+
arrow-select = { workspace = true }
4546
datafusion-common = { workspace = true }
4647
datafusion-doc = { workspace = true }
4748
datafusion-execution = { workspace = true }
@@ -52,6 +53,7 @@ datafusion-physical-expr = { workspace = true }
5253
datafusion-physical-expr-common = { workspace = true }
5354
half = { workspace = true }
5455
hashbrown = { workspace = true }
56+
itertools = { workspace = true }
5557
log = { workspace = true }
5658
num-traits = { workspace = true }
5759

0 commit comments

Comments
 (0)