feat: support GroupsAccumulator for first_value and last_value with string/binary types#21090
Draft
UBarney wants to merge 1 commit intoapache:mainfrom
Draft
feat: support GroupsAccumulator for first_value and last_value with string/binary types#21090UBarney wants to merge 1 commit intoapache:mainfrom
UBarney wants to merge 1 commit intoapache:mainfrom
Conversation
Dandandan
reviewed
Mar 21, 2026
| /// to correctly implement `RESPECT NULLS` behavior. | ||
| /// | ||
| pub(crate) struct BytesValueState { | ||
| vals: Vec<Option<Vec<u8>>>, |
Contributor
There was a problem hiding this comment.
I think this can be much more efficiently stored as values Vec<u8> and offsets Vec<OffsetType>
Contributor
Author
There was a problem hiding this comment.
I plan to implement it using the following approach and then run some benchmarks:
Data Structures
vals: Vec<u8>: A single, contiguous flat buffer for all raw bytes.offsets: Vec<usize>: The starting position of each group's data in the buffer.lengths: Vec<usize>: The logical length of the current value for each group.capacities: Vec<usize>: The physical space allocated for each group (enables in-place overwrites ifnew_len <= capacity).active_bytes: usize: A running counter of the sum of all currentlengths(used to track fragmentation and trigger GC).
Update Logic
- In-place Overwrite: If
new_len <= capacity, we overwrite the existing slot at the currentoffset. We update the logicallength, whilecapacityandoffsetremain unchanged. - Append: If
new_len > capacity, we append the value to the end ofvalsand update theoffset,length, andcapacityto point to the new location.
GC (Compaction) Logic
- Trigger: When the buffer grows too large (e.g.,
vals.len() > active_bytes * 2). - Action: Re-allocate a new buffer and copy only the latest valid data for each group to clear "dead" bytes left behind by the append path.
Contributor
There was a problem hiding this comment.
This is only needed for last_value, no?
Contributor
There was a problem hiding this comment.
Or wait nvm I see the queries having explicit order.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
GroupsAccumulatorforfirst_valueaggregate (speed upfirst_valueandDISTINCT ONqueries) #17899.Rationale for this change
Note: SQL queries for Q2, Q3, Q5, and Q6 are sourced from this PR.
Previously, the
first_valueandlast_valueaggregate 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.What changes are included in this PR?
ValueStateTrait: Abstracted the state management forfirst_valueandlast_valueto 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. Itoptimizes memory by reusing
Vec<u8>buffers for group updates.FirstLastGroupsAccumulator: Migrated the accumulator to use the generic ValueState trait, allowing it to handle both primitive and byte types uniformly.Are these changes tested?
YES
Are there any user-facing changes?