yussypu opened a new pull request, #27821:
URL: https://github.com/apache/datafusion/pull/27821
## Which issue does this PR close?
- Closes #25118.
## Rationale for this change
A grouped `array_agg` with few groups is slow in the final aggregation
stage. `ArrayAggGroupsAccumulator::merge_batch` records one `(group, row)`
entry per list element, and `evaluate` gathers them with `interleave`. When
merging partial states, each group contributes long contiguous runs, so a per
element gather does far more work than copying those runs as slices. The issue
reports a 64 group `collect_list` getting about 5x slower in its final stage.
## What changes are included in this PR?
- Each stored batch now keeps either per row `(group, row)` entries, as
before, or `(group, start, len)` ranges.
- `merge_batch` records one range per list when the state's lists hold 2
or more values on average, coalescing adjacent lists of the same group. Shorter
lists keep the per row form.
- `update_batch` input keeps the per row form.
- `evaluate` counts rows and runs per group, then picks a gather:
- average run of 64 rows or more: counting sort of the runs, then `concat`
of slices of the source arrays
- shorter runs: `interleave`, as before
- Dictionary values always go through `interleave`, since `concat` would
keep the whole dictionary shared across groups (this matters after spilling,
see `aggregate_memory_spill.slt`).
- `size()` still accounts per row for range batches, so spilling decisions
under a memory limit don't change.
- `EmitTo::First` compaction handles both forms and keeps ranges contiguous.
- New criterion benchmark `array_agg groups merge`: merges 8 partial states
of 8192 values each and emits all groups.
The 64 row threshold comes from measuring both gathers on their own: at list
length 64 concat was faster, at 32 interleave was.
## What is the testing strategy for this PR?
New unit tests in `array_agg.rs`:
- `groups_accumulator_merge_batch_records_one_range_per_list`: range
bookkeeping, null and empty lists, coalescing
- `groups_accumulator_merge_batch_short_lists_stored_per_row`: short lists
keep the per row form
- `groups_accumulator_merge_matches_naive_grouping`: compares against a
naive grouping for list lengths 1, 2, 16 and 64, mixing per row and range
batches, covering both gathers, with and without `EmitTo::First`
- `contains_dictionary_finds_nested_dictionaries`
The existing accumulator tests, `cargo test -p
datafusion-functions-aggregate`, clippy, and the sqllogictests for aggregate,
array_agg, group_by, distinct_on, unnest, struct and window_topn pass.
Benchmark on an Apple Silicon Mac (`cargo bench -p
datafusion-functions-aggregate --bench array_agg -- "array_agg groups merge"`):
| groups (list length) | main | this PR | change |
|---|---|---|---|
| 64 (128) | 426.8µs | 40.4µs | −90.5% |
| 128 (64) | 423.7µs | 65.9µs | −84.5% |
| 256 (32) | 424.6µs | 91.4µs | −78.4% |
| 512 (16) | 431.9µs | 97.0µs | −77.5% |
| 1024 (8) | 430.2µs | 107.5µs | −75.1% |
| 8192 (1) | 296.4µs | 291.7µs | no change |
## Are there any user-facing changes?
No API changes. Grouped `array_agg` is faster when merging partial states
with multi value lists.
Open questions for reviewers:
- The thresholds (2 values per list for ranges, 64 rows per run for concat)
were measured with Int64 values on one machine. Wider or variable length types
may shift them.
- `size()` overestimates range batches slightly (range metadata plus per row
accounting). I kept it that way so spill behavior matches the per row form;
happy to change it.
--
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]