Skip to content

array_agg() performance improvements#23716

Open
fred1268 wants to merge 2 commits into
apache:mainfrom
fred1268:perf/array_aggregate
Open

array_agg() performance improvements#23716
fred1268 wants to merge 2 commits into
apache:mainfrom
fred1268:perf/array_aggregate

Conversation

@fred1268

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

Queries using array_agg(DISTINCT col) were significantly slower than expected.

Profiling revealed that DistinctArrayAggAccumulator::update_batch was allocating a
heap-owned String on every single input row — even for rows whose value was already
present in the accumulator. For a typical low-cardinality workload (e.g. a column of ~25
database names across thousands of rows), this meant paying the full allocation cost for
every duplicate, which dominated the runtime.

What changes are included in this PR?

This PR applies the same deduplication strategy already used by AggregateExec for
GROUP BY: duplicate rows now cost only a hash probe with no heap allocation, and new
distinct values are appended to a single shared buffer rather than allocated individually.
The fix applies to all column types, not just strings, and retract_batch support
(required for sliding window frames such as ROWS BETWEEN N PRECEDING AND CURRENT ROW)
is fully preserved.

Are these changes tested?

Four unit tests were added to DistinctArrayAggAccumulator — one each for Utf8,
Int64, Float64, and Date32 — to pin the deduplication contract across the most
common column types and serve as a regression guard for future changes. The existing
sliding window sqllogictest suite (array_agg_sliding_window.slt) covers retract_batch
correctness end-to-end and passes unchanged.

Two update_batch micro-benchmarks were added to measure the before/after on realistic
data: one with low cardinality (~25 distinct database names in 8 192 rows, modelling the
common production case) and one with high cardinality (~7 800 distinct values, modelling
the worst case where almost every row is new). Results on an 8 192-row batch:

Benchmark Before After Speedup
Low cardinality (~25 distinct DB names) 648.6 µs 189.4 µs 3.42×
High cardinality (~7 800 distinct values) 1078.3 µs 269.4 µs 4.00×

Are there any user-facing changes?

No user-facing changes

No breaking changes to public APIs

@github-actions github-actions Bot added the functions Changes to functions implementation label Jul 20, 2026
@codecov-commenter

codecov-commenter commented Jul 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.53731% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.69%. Comparing base (1fcdef2) to head (1cd1305).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
datafusion/functions-aggregate/src/array_agg.rs 92.53% 2 Missing and 8 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #23716      +/-   ##
==========================================
- Coverage   80.69%   80.69%   -0.01%     
==========================================
  Files        1089     1089              
  Lines      368525   368602      +77     
  Branches   368525   368602      +77     
==========================================
+ Hits       297392   297447      +55     
- Misses      53414    53426      +12     
- Partials    17719    17729      +10     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@gabotechs

Copy link
Copy Markdown
Contributor

run benchmarks array_agg

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark running (GKE) | trigger
Instance: c4a-highmem-16 (12 vCPU / 65 GiB) | Linux bench-c5022949366-1176-hk9sf 6.12.85+ #1 SMP Mon May 11 08:17:35 UTC 2026 aarch64 GNU/Linux

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected

Comparing perf/array_aggregate (33c5bb1) to 1e58928 (merge-base) diff using: array_agg
Results will be posted here when complete


File an issue against this benchmark runner

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark completed (GKE) | trigger

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Details

group                                                                              HEAD                                   perf_array_aggregate
-----                                                                              ----                                   --------------------
array_agg i64 merge_batch 30% nulls, 0% of nulls point to a zero length array      1.02   508.6±10.52µs        ? ?/sec    1.00    497.7±6.50µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 100% of nulls point to a zero length array    1.00      4.6±0.00µs        ? ?/sec    1.00      4.6±0.00µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 50% of nulls point to a zero length array     1.02    510.3±9.30µs        ? ?/sec    1.00    502.0±6.26µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 90% of nulls point to a zero length array     1.01   510.2±10.92µs        ? ?/sec    1.00    504.1±5.99µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 99% of nulls point to a zero length array     1.03    515.3±8.71µs        ? ?/sec    1.00    501.2±5.86µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 0% of nulls point to a zero length array      1.02    218.6±3.77µs        ? ?/sec    1.00    213.5±2.63µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 100% of nulls point to a zero length array    1.00      4.4±0.00µs        ? ?/sec    1.00      4.4±0.00µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 50% of nulls point to a zero length array     1.00    215.9±3.92µs        ? ?/sec    1.00    216.5±2.39µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 90% of nulls point to a zero length array     1.01    218.1±5.02µs        ? ?/sec    1.00    217.0±2.31µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 99% of nulls point to a zero length array     1.02    220.0±4.23µs        ? ?/sec    1.00    216.5±2.32µs        ? ?/sec
array_agg i64 merge_batch all nulls, 100% of nulls point to a zero length array    1.01     73.6±3.29ns        ? ?/sec    1.00     73.1±3.08ns        ? ?/sec
array_agg i64 merge_batch all nulls, 90% of nulls point to a zero length array     1.02     74.0±3.03ns        ? ?/sec    1.00     72.4±2.89ns        ? ?/sec
array_agg i64 merge_batch no nulls                                                 1.00     81.6±1.37ns        ? ?/sec    1.01     82.6±1.19ns        ? ?/sec
distinct_array_agg utf8 high cardinality (~7800 distinct, 5% db names)                                                    1.00    274.1±0.84µs        ? ?/sec
distinct_array_agg utf8 low cardinality (~25 distinct)                                                                    1.00    147.5±0.52µs        ? ?/sec

Resource Usage

array_agg — base (merge-base)

Metric Value
Wall time 355.1s
Peak memory 27.7 MiB
Avg memory 8.3 MiB
CPU user 156.3s
CPU sys 0.1s
Peak spill 0 B

array_agg — branch

Metric Value
Wall time 370.1s
Peak memory 30.7 MiB
Avg memory 10.5 MiB
CPU user 180.9s
CPU sys 0.2s
Peak spill 0 B

File an issue against this benchmark runner

@gabotechs

Copy link
Copy Markdown
Contributor

Seems like there's no impact in those benchmarks. Do you know if there are any other benchmarks that are using array_agg? maybe tpch or tpcds?

@gabotechs

Copy link
Copy Markdown
Contributor

Let's ship the benchmarks in a preliminary PR, and then come back to this one.

@fred1268
fred1268 force-pushed the perf/array_aggregate branch from 33c5bb1 to 1cd1305 Compare July 21, 2026 09:02
@gabotechs

Copy link
Copy Markdown
Contributor

run benchmarks array_agg

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark running (GKE) | trigger
Instance: c4a-highmem-16 (12 vCPU / 65 GiB) | Linux bench-c5032302654-1202-qdxqv 6.12.85+ #1 SMP Mon May 11 08:17:35 UTC 2026 aarch64 GNU/Linux

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected

Comparing perf/array_aggregate (1cd1305) to 1fcdef2 (merge-base) diff using: array_agg
Results will be posted here when complete


File an issue against this benchmark runner

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark completed (GKE) | trigger

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Details

group                                                                              HEAD                                   perf_array_aggregate
-----                                                                              ----                                   --------------------
array_agg i64 merge_batch 30% nulls, 0% of nulls point to a zero length array      1.00    502.5±4.80µs        ? ?/sec    1.00   501.6±10.91µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 100% of nulls point to a zero length array    1.00      4.6±0.00µs        ? ?/sec    1.00      4.6±0.00µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 50% of nulls point to a zero length array     1.01    507.6±5.23µs        ? ?/sec    1.00   504.5±11.24µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 90% of nulls point to a zero length array     1.01   509.7±12.25µs        ? ?/sec    1.00   507.1±11.09µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 99% of nulls point to a zero length array     1.00   504.8±10.50µs        ? ?/sec    1.01   509.2±10.60µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 0% of nulls point to a zero length array      1.00    214.2±2.30µs        ? ?/sec    1.00    214.5±4.55µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 100% of nulls point to a zero length array    1.00      4.4±0.00µs        ? ?/sec    1.00      4.4±0.01µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 50% of nulls point to a zero length array     1.00    215.4±2.38µs        ? ?/sec    1.01    217.2±4.56µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 90% of nulls point to a zero length array     1.00    215.5±2.38µs        ? ?/sec    1.01    216.8±4.77µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 99% of nulls point to a zero length array     1.00    215.6±2.63µs        ? ?/sec    1.01    217.9±4.56µs        ? ?/sec
array_agg i64 merge_batch all nulls, 100% of nulls point to a zero length array    1.00     72.1±2.71ns        ? ?/sec    1.04     74.7±3.17ns        ? ?/sec
array_agg i64 merge_batch all nulls, 90% of nulls point to a zero length array     1.00     72.2±2.70ns        ? ?/sec    1.11     80.2±4.36ns        ? ?/sec
array_agg i64 merge_batch no nulls                                                 1.00     81.8±1.32ns        ? ?/sec    1.01     82.4±1.25ns        ? ?/sec
distinct_array_agg utf8 high cardinality (~7800 distinct, 5% db names)             4.10   1121.5±2.78µs        ? ?/sec    1.00    273.2±0.68µs        ? ?/sec
distinct_array_agg utf8 low cardinality (~25 distinct)                             3.89    577.0±1.79µs        ? ?/sec    1.00    148.3±0.42µs        ? ?/sec

Resource Usage

array_agg — base (merge-base)

Metric Value
Wall time 260.1s
Peak memory 33.2 MiB
Avg memory 15.7 MiB
CPU user 181.2s
CPU sys 0.2s
Peak spill 0 B

array_agg — branch

Metric Value
Wall time 255.1s
Peak memory 30.9 MiB
Avg memory 15.5 MiB
CPU user 184.4s
CPU sys 0.2s
Peak spill 0 B

File an issue against this benchmark runner

@gabotechs

Copy link
Copy Markdown
Contributor

Nice!

distinct_array_agg utf8 high cardinality (~7800 distinct, 5% db names)             4.10   1121.5±2.78µs        ? ?/sec    1.00    273.2±0.68µs        ? ?/sec
distinct_array_agg utf8 low cardinality (~25 distinct)                             3.89    577.0±1.79µs        ? ?/sec    1.00    148.3±0.42µs        ? ?/sec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

array_agg() performance

4 participants