lyne7-sc opened a new pull request, #21782:
URL: https://github.com/apache/datafusion/pull/21782
## 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 #123`
indicates that this PR will close issue #123.
-->
- Closes #.
## Rationale for this change
This PR improves two hot paths for count distinct.
For the primitive non-sliding path, it adds a fast path to avoid per-row
null handling when the input batch contains no nulls.
For the sliding path, it adds a primitive accumulator and keeps other types
on the generic path for now.
<!--
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.
-->
## What changes are included in this PR?
This pr adds a null-free fast path for primitive `COUNT(DISTINCT)` and a
primitive sliding distinct accumulator for integer types.
### Benchmarks
```
group optimized
main
----- -----------------
--------------------
count_distinct i16 bitmap 1.00
4.4±0.01µs ? ?/sec 1.01 4.4±0.06µs ? ?/sec
count_distinct i32 80% distinct 1.00
18.9±0.29µs ? ?/sec 2.79 52.7±0.12µs ? ?/sec
count_distinct i32 99% distinct 1.00
18.9±0.20µs ? ?/sec 2.78 52.5±0.23µs ? ?/sec
count_distinct i64 80% distinct 1.00
18.5±0.22µs ? ?/sec 2.89 53.5±0.27µs ? ?/sec
count_distinct i64 99% distinct 1.00
18.8±0.39µs ? ?/sec 2.85 53.6±0.23µs ? ?/sec
count_distinct i8 bitmap 1.00
1250.0±3.33ns ? ?/sec 1.00 1251.0±1.82ns ? ?/sec
count_distinct u16 bitmap 1.00
4.4±0.01µs ? ?/sec 1.00 4.4±0.01µs ? ?/sec
count_distinct u32 80% distinct 1.00
18.9±0.35µs ? ?/sec 2.80 52.9±0.22µs ? ?/sec
count_distinct u32 99% distinct 1.00
19.2±0.26µs ? ?/sec 2.72 52.4±0.14µs ? ?/sec
count_distinct u8 bitmap 1.01
1254.1±3.38ns ? ?/sec 1.00 1246.5±3.27ns ? ?/sec
count_distinct_groups i32_g10000_high_skewed 1.00
91.6±0.20µs ? ?/sec 19.86 1818.9±15.75µs ? ?/sec
count_distinct_groups i32_g10000_high_uniform 1.00
58.5±0.17µs ? ?/sec 31.61 1849.8±16.72µs ? ?/sec
count_distinct_groups i32_g10000_low_skewed 1.00
91.7±0.15µs ? ?/sec 20.10 1843.3±52.54µs ? ?/sec
count_distinct_groups i32_g10000_low_uniform 1.00
57.4±0.33µs ? ?/sec 32.00 1838.0±17.75µs ? ?/sec
count_distinct_groups i32_g10000_mid_skewed 1.00
91.7±0.22µs ? ?/sec 19.94 1827.7±19.82µs ? ?/sec
count_distinct_groups i32_g10000_mid_uniform 1.00
58.0±0.19µs ? ?/sec 31.92 1850.4±21.75µs ? ?/sec
count_distinct_groups i32_g1000_high_skewed 1.00
91.2±0.10µs ? ?/sec 3.21 292.2±1.92µs ? ?/sec
count_distinct_groups i32_g1000_high_uniform 1.00
55.1±0.31µs ? ?/sec 5.28 291.0±1.76µs ? ?/sec
count_distinct_groups i32_g1000_low_skewed 1.00
91.3±0.14µs ? ?/sec 3.21 293.4±1.90µs ? ?/sec
count_distinct_groups i32_g1000_low_uniform 1.00
37.9±0.10µs ? ?/sec 6.16 233.5±0.61µs ? ?/sec
count_distinct_groups i32_g1000_mid_skewed 1.00
90.7±0.19µs ? ?/sec 3.24 294.2±3.20µs ? ?/sec
count_distinct_groups i32_g1000_mid_uniform 1.00
55.3±0.17µs ? ?/sec 5.16 285.4±1.49µs ? ?/sec
count_distinct_groups i32_g100_high_skewed 1.00
89.9±0.13µs ? ?/sec 1.21 108.9±0.25µs ? ?/sec
count_distinct_groups i32_g100_high_uniform 1.00
55.0±0.27µs ? ?/sec 1.64 90.5±0.27µs ? ?/sec
count_distinct_groups i32_g100_low_skewed 1.00
89.3±0.26µs ? ?/sec 1.20 107.5±0.31µs ? ?/sec
count_distinct_groups i32_g100_low_uniform 1.00
29.7±0.09µs ? ?/sec 2.01 59.8±0.28µs ? ?/sec
count_distinct_groups i32_g100_mid_skewed 1.00
95.3±6.28µs ? ?/sec 1.14 108.9±0.35µs ? ?/sec
count_distinct_groups i32_g100_mid_uniform 1.00
55.0±0.59µs ? ?/sec 1.53 84.3±0.40µs ? ?/sec
count_distinct_groups i64_g10000_high_skewed 1.00
56.7±0.09µs ? ?/sec 31.80 1802.7±19.52µs ? ?/sec
count_distinct_groups i64_g10000_high_uniform 1.00
94.0±0.18µs ? ?/sec 19.94 1875.4±24.44µs ? ?/sec
count_distinct_groups i64_g10000_low_skewed 1.00
56.0±0.16µs ? ?/sec 31.24 1750.1±21.51µs ? ?/sec
count_distinct_groups i64_g10000_low_uniform 1.00
93.7±0.18µs ? ?/sec 19.94 1869.6±20.30µs ? ?/sec
count_distinct_groups i64_g10000_mid_skewed 1.00
56.9±0.40µs ? ?/sec 31.73 1804.5±10.60µs ? ?/sec
count_distinct_groups i64_g10000_mid_uniform 1.00
95.1±0.12µs ? ?/sec 19.38 1842.7±15.07µs ? ?/sec
count_distinct_groups i64_g1000_high_skewed 1.00
54.7±0.22µs ? ?/sec 4.96 271.2±0.97µs ? ?/sec
count_distinct_groups i64_g1000_high_uniform 1.00
91.4±0.10µs ? ?/sec 3.43 313.3±1.74µs ? ?/sec
count_distinct_groups i64_g1000_low_skewed 1.00
36.8±0.09µs ? ?/sec 6.38 235.1±1.35µs ? ?/sec
count_distinct_groups i64_g1000_low_uniform 1.00
91.0±0.15µs ? ?/sec 3.44 313.3±1.42µs ? ?/sec
count_distinct_groups i64_g1000_mid_skewed 1.00
55.0±0.12µs ? ?/sec 4.85 266.6±1.56µs ? ?/sec
count_distinct_groups i64_g1000_mid_uniform 1.00
91.5±0.12µs ? ?/sec 3.42 312.7±0.89µs ? ?/sec
count_distinct_groups i64_g100_high_skewed 1.00
55.2±0.23µs ? ?/sec 1.85 102.3±0.22µs ? ?/sec
count_distinct_groups i64_g100_high_uniform 1.00
91.4±0.16µs ? ?/sec 1.36 124.6±0.65µs ? ?/sec
count_distinct_groups i64_g100_low_skewed 1.00
37.4±0.05µs ? ?/sec 1.68 63.0±0.15µs ? ?/sec
count_distinct_groups i64_g100_low_uniform 1.00
90.4±0.21µs ? ?/sec 1.36 122.5±0.33µs ? ?/sec
count_distinct_groups i64_g100_mid_skewed 1.00
54.4±0.11µs ? ?/sec 1.62 88.3±0.35µs ? ?/sec
count_distinct_groups i64_g100_mid_uniform 1.00
90.6±0.14µs ? ?/sec 1.36 123.2±0.26µs ? ?/sec
count_distinct_groups u32_g10000_high_skewed 1.00
91.6±0.16µs ? ?/sec 20.03 1834.9±13.81µs ? ?/sec
count_distinct_groups u32_g10000_high_uniform 1.00
58.4±0.17µs ? ?/sec 31.92 1865.4±16.83µs ? ?/sec
count_distinct_groups u32_g10000_low_skewed 1.00
91.8±0.14µs ? ?/sec 19.81 1817.9±21.85µs ? ?/sec
count_distinct_groups u32_g10000_low_uniform 1.00
57.3±0.16µs ? ?/sec 32.31 1850.8±21.91µs ? ?/sec
count_distinct_groups u32_g10000_mid_skewed 1.00
91.8±0.18µs ? ?/sec 19.80 1816.9±14.35µs ? ?/sec
count_distinct_groups u32_g10000_mid_uniform 1.00
57.9±0.20µs ? ?/sec 31.82 1842.1±10.99µs ? ?/sec
count_distinct_groups u32_g1000_high_skewed 1.00
91.1±0.13µs ? ?/sec 3.52 320.4±43.68µs ? ?/sec
count_distinct_groups u32_g1000_high_uniform 1.00
55.1±0.26µs ? ?/sec 5.41 298.4±1.54µs ? ?/sec
count_distinct_groups u32_g1000_low_skewed 1.00
91.3±0.15µs ? ?/sec 3.25 296.8±1.17µs ? ?/sec
count_distinct_groups u32_g1000_low_uniform 1.00
37.8±0.19µs ? ?/sec 6.23 235.4±1.70µs ? ?/sec
count_distinct_groups u32_g1000_mid_skewed 1.00
90.6±0.15µs ? ?/sec 3.30 299.5±2.82µs ? ?/sec
count_distinct_groups u32_g1000_mid_uniform 1.00
55.3±0.13µs ? ?/sec 5.19 287.3±4.64µs ? ?/sec
count_distinct_groups u32_g100_high_skewed 1.00
89.9±0.13µs ? ?/sec 1.22 109.3±0.40µs ? ?/sec
count_distinct_groups u32_g100_high_uniform 1.00
54.7±0.18µs ? ?/sec 1.66 90.8±0.37µs ? ?/sec
count_distinct_groups u32_g100_low_skewed 1.00
89.2±0.09µs ? ?/sec 1.21 107.9±0.63µs ? ?/sec
count_distinct_groups u32_g100_low_uniform 1.00
29.4±0.10µs ? ?/sec 2.03 59.9±0.35µs ? ?/sec
count_distinct_groups u32_g100_mid_skewed 1.00
91.0±1.12µs ? ?/sec 1.20 108.8±0.26µs ? ?/sec
count_distinct_groups u32_g100_mid_uniform 1.00
54.3±0.21µs ? ?/sec 1.56 84.7±0.52µs ? ?/sec
count_distinct_sliding i64 high window_1024 1.00
503.8±1.41µs ? ?/sec 1.64 827.2±3.43µs ? ?/sec
count_distinct_sliding i64 high window_256 1.00
487.3±1.17µs ? ?/sec 1.69 823.4±2.38µs ? ?/sec
count_distinct_sliding i64 low window_1024 1.00
525.0±1.29µs ? ?/sec 1.59 833.4±6.13µs ? ?/sec
count_distinct_sliding i64 low window_256 1.00
471.2±1.25µs ? ?/sec 1.70 802.3±3.48µs ? ?/sec
count_distinct_sliding i64 mid window_1024 1.00
513.3±1.23µs ? ?/sec 1.63 838.0±4.07µs ? ?/sec
count_distinct_sliding i64 mid window_256 1.00
479.1±0.77µs ? ?/sec 1.72 825.8±3.68µs ? ?/sec
```
Note: The grouped `count_distinct` benchmarks show large improvements, but I
think the current benchmark inputs do not include nulls, so these numbers
should be interpreted as null-free results rather than a general improvement
for all inputs.
<!--
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.
-->
## Are these changes tested?
yes
<!--
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)?
-->
## Are there any user-facing changes?
no
<!--
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.
-->
--
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]