Skip to content

perf: null-free fast path for COUNT(DISTINCT) primitive accumulator - #23956

Merged
neilconway merged 1 commit into
apache:mainfrom
viirya:perf-count-distinct-nullfree
Jul 29, 2026
Merged

perf: null-free fast path for COUNT(DISTINCT) primitive accumulator#23956
neilconway merged 1 commit into
apache:mainfrom
viirya:perf-count-distinct-nullfree

Conversation

@viirya

@viirya viirya commented Jul 28, 2026

Copy link
Copy Markdown
Member

Which issue does this PR close?

Rationale for this change

PrimitiveDistinctCountAccumulator::update_batch does a per-element validity check on every row even when the array has no nulls. Its own merge_batch already inserts wholesale from the values buffer, so the same fast path applies to update_batch. Inspired by the null-free fast paths added to percentile_cont/median in #23954.

What changes are included in this PR?

  • When null_count() == 0, insert directly from the values buffer (self.values.extend(arr.values().iter().copied())) instead of the per-element Option check.
  • Unit test asserting the fast and general paths agree (a dense array and a null-containing array with the same non-null values yield the same distinct count).

Benchmarks

count_distinct benchmark, null-free 8192-row batches:

count_distinct i64 80% distinct   -67.0%  (54.9 -> 18.1 us)
count_distinct i64 99% distinct   -66.0%  (54.9 -> 18.7 us)
count_distinct u32 80% distinct   -66.9%  (53.9 -> 17.8 us)
count_distinct u32 99% distinct   -65.3%  (53.3 -> 18.5 us)
count_distinct i32 80% distinct   -66.9%  (54.1 -> 17.9 us)
count_distinct i32 99% distinct   -66.2%  (53.2 -> 18.0 us)

The bitmap-backed cases (u8/i8/u16/i16) and the grouped-accumulator cases don't use this path and are unchanged, as expected.

Are these changes tested?

Yes — new unit test; existing count-distinct tests pass.

Are there any user-facing changes?

No.

`PrimitiveDistinctCountAccumulator::update_batch` iterated values with a
per-element `Option`/validity check even when the input array had no
nulls. Add a fast path that, when `null_count() == 0`, inserts directly
from the values buffer (mirroring what `merge_batch` already does),
skipping the per-element branch.

This is the same style of null-free fast path recently applied to the
median / percentile_cont accumulators (apache#23954).

Benchmarks (`count_distinct`, null-free 8192-row batches):

    count_distinct i64 80% distinct    -66.4%  (54.2 -> 18.4 us)
    count_distinct i64 99% distinct    -66.3%  (54.7 -> 18.4 us)
    count_distinct u32 80% distinct    -68.7%
    count_distinct u32 99% distinct    -62.9%
    count_distinct i32 80% distinct    -64.4%
    count_distinct i32 99% distinct    -63.7%

Co-authored-by: Claude Code
@github-actions github-actions Bot added the functions Changes to functions implementation label Jul 28, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.69%. Comparing base (b0b9dae) to head (034c216).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #23956      +/-   ##
==========================================
- Coverage   80.69%   80.69%   -0.01%     
==========================================
  Files        1095     1095              
  Lines      372529   372556      +27     
  Branches   372529   372556      +27     
==========================================
+ Hits       300626   300643      +17     
- Misses      53942    53946       +4     
- Partials    17961    17967       +6     

☔ 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.

@neilconway neilconway 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.

lgtm! Nice perf win for such a narrowly scoped change.

@neilconway
neilconway added this pull request to the merge queue Jul 29, 2026
@viirya

viirya commented Jul 29, 2026

Copy link
Copy Markdown
Member Author

Thanks @neilconway for review

Merged via the queue into apache:main with commit 5941552 Jul 29, 2026
37 checks passed
@viirya
viirya deleted the perf-count-distinct-nullfree branch July 29, 2026 15:01
kosiew pushed a commit to kosiew/datafusion that referenced this pull request Aug 12, 2026
…pache#23956)

## Which issue does this PR close?

- Closes apache#23955.

## Rationale for this change

`PrimitiveDistinctCountAccumulator::update_batch` does a per-element
validity check on every row even when the array has no nulls. Its own
`merge_batch` already inserts wholesale from the values buffer, so the
same fast path applies to `update_batch`. Inspired by the null-free fast
paths added to `percentile_cont`/`median` in apache#23954.

## What changes are included in this PR?

- When `null_count() == 0`, insert directly from the values buffer
(`self.values.extend(arr.values().iter().copied())`) instead of the
per-element `Option` check.
- Unit test asserting the fast and general paths agree (a dense array
and a null-containing array with the same non-null values yield the same
distinct count).

## Benchmarks

`count_distinct` benchmark, null-free 8192-row batches:

```
count_distinct i64 80% distinct   -67.0%  (54.9 -> 18.1 us)
count_distinct i64 99% distinct   -66.0%  (54.9 -> 18.7 us)
count_distinct u32 80% distinct   -66.9%  (53.9 -> 17.8 us)
count_distinct u32 99% distinct   -65.3%  (53.3 -> 18.5 us)
count_distinct i32 80% distinct   -66.9%  (54.1 -> 17.9 us)
count_distinct i32 99% distinct   -66.2%  (53.2 -> 18.0 us)
```

The bitmap-backed cases (u8/i8/u16/i16) and the grouped-accumulator
cases don't use this path and are unchanged, as expected.

## Are these changes tested?

Yes — new unit test; existing count-distinct tests pass.

## Are there any user-facing changes?

No.
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.

COUNT(DISTINCT <primitive>) misses a null-free fast path in its update loop

3 participants