Commit 587f4c0
feat: support GroupsAccumulator for first_value and last_value with string/binary types (#21090)
## 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 #17899.
## Rationale for this change
```
┏━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query ┃ main ┃ first_val_group_acc ┃ Change ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 0 │ 872.01 ms │ 904.35 ms │ no change │
│ QQuery 1 │ 156.37 ms │ 164.83 ms │ 1.05x slower │
│ QQuery 2 │ 448.58 ms │ 497.05 ms │ 1.11x slower │
│ QQuery 3 │ 233.99 ms │ 274.10 ms │ 1.17x slower │
│ QQuery 4 │ 1448.99 ms │ 1556.95 ms │ 1.07x slower │
│ QQuery 5 │ 10816.83 ms │ 11315.69 ms │ no change │
│ QQuery 6 │ 2053.16 ms │ 2030.02 ms │ no change │
│ QQuery 7 │ 2154.74 ms │ 2274.63 ms │ 1.06x slower │
│ QQuery 8 │ 405.62 ms │ 405.72 ms │ no change │
│ QQuery 9 │ 17160.65 ms │ 4167.34 ms │ +4.12x faster │
│ QQuery 10 │ 1206.03 ms │ 1090.69 ms │ +1.11x faster │
│ QQuery 11 │ 2437.12 ms │ 2446.51 ms │ no change │
│ QQuery 12 │ 331.27 ms │ 317.73 ms │ no change │
└───────────┴─────────────┴─────────────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Benchmark Summary ┃ ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ Total Time (main) │ 39725.35ms │
│ Total Time (first_val_group_acc) │ 27445.62ms │
│ Average Time (main) │ 3055.80ms │
│ Average Time (first_val_group_acc) │ 2111.20ms │
│ Queries Faster │ 2 │
│ Queries Slower │ 5 │
│ Queries with No Change │ 6 │
│ Queries with Failure │ 0 │
└────────────────────────────────────┴────────────┘
```
Previously, the `first_value` and `last_value` aggregate functions only
supported GroupsAccumulator for primitive types. For string or binary
types (Utf8, LargeUtf8, Binary, etc.), they fell back to the slower
row-based Accumulator path.
This change implements a specialized state management for byte-based
types, enabling high-performance grouped aggregation for strings and
binary data, especially when used with `ORDER BY`.
<!--
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?
- New `ValueState` Trait: Abstracted the state management for
`first_value` and `last_value` to support different storage backends.
- `PrimitiveValueState` : Re-implemented the existing primitive handling
using the new trait.
- `BytesValueState`: Added a new state implementation for Utf8,
LargeUtf8, Utf8View, Binary, LargeBinary, and BinaryView. It
optimizes memory by reusing `Vec<u8>` buffers for group updates.
- Refactored `FirstLastGroupsAccumulato`r: Migrated the accumulator to
use the generic ValueState trait, allowing it to handle both primitive
and byte types uniformly.
<!--
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?
<!--
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.
-->
---------
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>1 parent 3d2e6b2 commit 587f4c0
9 files changed
Lines changed: 838 additions & 325 deletions
File tree
- benchmarks/queries/clickbench
- extended
- datafusion
- functions-aggregate/src
- first_last
- sqllogictest/test_files
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
228 | 228 | | |
229 | 229 | | |
230 | 230 | | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
231 | 247 | | |
232 | 248 | | |
233 | 249 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
0 commit comments