Skip to content

perf: use native distinct accumulator for COUNT(DISTINCT) on Decimal32/Decimal64 - #26821

Merged
alamb merged 3 commits into
apache:mainfrom
tjnangosha:count-distinct-decimal32-64
Oct 29, 2026
Merged

alamb merged 3 commits into
apache:mainfrom
tjnangosha:count-distinct-decimal32-64

Conversation

@tjnangosha

@tjnangosha tjnangosha commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

COUNT(DISTINCT) on a Decimal32 or Decimal64 column is slower than the same query on a Decimal128 column, even though the values are smaller. On main, counting the distinct values in a batch of 8,192 Decimal32 values takes about 870 µs, compared with about 110 µs for Decimal128.

The cause is in get_count_accumulator in datafusion/functions-aggregate/src/count.rs. Decimal128 and Decimal256 use PrimitiveDistinctCountAccumulator, which works on the raw values directly. Decimal32 and Decimal64 have no arm there, so they fall through to the generic DistinctCountAccumulator, which converts every row into a ScalarValue before inserting it into a hash set.

This looks like a spot that was missed when Decimal32/64 support was added in #17501 (issue #17489).

What changes are included in this PR?

  • count.rs: add Decimal32 and Decimal64 arms to get_count_accumulator, using PrimitiveDistinctCountAccumulator like Decimal128 and Decimal256 do.
  • benches/count_distinct.rs: add count_distinct cases for Decimal32, Decimal64 and Decimal128 (as a reference) at 80% and 99% distinct.
  • aggregate.slt: add COUNT(DISTINCT) tests on Decimal32 and Decimal64 columns.

Benchmark results

COUNT(DISTINCT) accumulator, one batch of 8,192 values (datafusion/functions-aggregate/benches/count_distinct.rs).

case main this PR change speedup
decimal32, 80% distinct 867.4 µs 99.0 µs −88.6% 8.8×
decimal32, 99% distinct 910.5 µs 100.0 µs −89.1% 9.1×
decimal64, 80% distinct 911.5 µs 98.4 µs −89.3% 9.3×
decimal64, 99% distinct 918.6 µs 99.8 µs −89.0% 9.2×
decimal128, 80% distinct (unchanged path) 108.6 µs 109.7 µs no change (p = 0.81) –
decimal128, 99% distinct (unchanged path) 113.2 µs 112.0 µs no change (p = 0.31) –

What is the testing strategy for this PR?

The change doesn't affect results, only which accumulator is used. The new sqllogictest cases in aggregate.slt run COUNT(DISTINCT) on Decimal32 and Decimal64 columns containing duplicates and a NULL, with and without GROUP BY. Each query counts two different columns, so the single-distinct rewrite (single_distinct_to_groupby) doesn't turn it into a GROUP BY and the query goes through the accumulator changed here. The new bench cases cover the performance side.

Are there any user-facing changes?

No. Query results are unchanged, and COUNT(DISTINCT) on Decimal32 and Decimal64 columns is faster. There are no public API changes.

@github-actions github-actions Bot added functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt) labels Oct 27, 2026
@tjnangosha
tjnangosha force-pushed the count-distinct-decimal32-64 branch from c7273c2 to 1b19736 Compare October 27, 2026 20:11
@tjnangosha
tjnangosha marked this pull request as ready for review October 28, 2026 17:10
@tjnangosha

Copy link
Copy Markdown
Contributor Author

@kumarUjjawal Could you please review?

@kumarUjjawal

Copy link
Copy Markdown
Contributor

run benchmark count_distinct

env:
BENCH_FILTER: '^count_distinct decimal(32|64|128) '
baseline:
ref: f4624b8
changed:
ref: 1b19736

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark running (GKE) | trigger
Instance: c4a-highmem-16 (12 vCPU / 65 GiB) | Linux bench-c6475789806-4072-vrbqx 6.12.105+ #1 SMP Sat Sep 19 07:55:25 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 1b19736 (1b19736) to f4624b8 diff

Run configuration
run benchmark count_distinct
env:
  BENCH_FILTER: "^count_distinct decimal(32|64|128) "
baseline:
  ref: "f4624b8aab2ac673ec7598c9e49d2209e295d336"
changed:
  ref: "1b19736c9d20545dc7b61730ea199384d070e9f8"

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)

Comparing 1b19736 (1b19736) to f4624b8 diff

Run configuration
run benchmark count_distinct
env:
  BENCH_FILTER: "^count_distinct decimal(32|64|128) "
baseline:
  ref: "f4624b8aab2ac673ec7598c9e49d2209e295d336"
changed:
  ref: "1b19736c9d20545dc7b61730ea199384d070e9f8"
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                                   count-distinct-decimal32-64
-----                                     ----                                   ---------------------------
count_distinct decimal128 80% distinct    1.08     54.1±0.42µs        ? ?/sec    1.00     50.2±0.37µs        ? ?/sec
count_distinct decimal128 99% distinct    1.08     53.3±0.43µs        ? ?/sec    1.00     49.2±0.22µs        ? ?/sec
count_distinct decimal32 80% distinct     12.77   590.7±5.14µs        ? ?/sec    1.00     46.3±0.40µs        ? ?/sec
count_distinct decimal32 99% distinct     13.31   587.2±5.29µs        ? ?/sec    1.00     44.1±0.12µs        ? ?/sec
count_distinct decimal64 80% distinct     13.30   611.2±7.08µs        ? ?/sec    1.00     46.0±0.40µs        ? ?/sec
count_distinct decimal64 99% distinct     13.32   611.3±6.60µs        ? ?/sec    1.00     45.9±0.23µs        ? ?/sec

Resource Usage

count_distinct — base (merge-base)

Metric Value
Wall time 300.1s
Peak memory 27.9 MiB
Avg memory 4.9 MiB
CPU user 75.5s
CPU sys 0.0s
Peak spill 0 B

count_distinct — branch

Metric Value
Wall time 285.1s
Peak memory 27.9 MiB
Avg memory 4.3 MiB
CPU user 66.1s
CPU sys 0.1s
Peak spill 0 B

File an issue against this benchmark runner

@alamb alamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @tjnangosha - this makes sense to me

@alamb alamb added the performance Make DataFusion faster label Oct 28, 2026
@tjnangosha

Copy link
Copy Markdown
Contributor Author

Thanks for the review @alamb, @kumarUjjawal

@jayzhan211
jayzhan211 added this pull request to the merge queue Oct 29, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 29, 2026
@alamb
alamb added this pull request to the merge queue Oct 29, 2026
Merged via the queue into apache:main with commit de6f3e7 Oct 29, 2026
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation performance Make DataFusion faster sqllogictest SQL Logic Tests (.slt) v56.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

COUNT(DISTINCT) on Decimal32/Decimal64 doesn’t use the native distinct accumulator

4 participants