Specialized GroupValues Implementation for Ordered Data
Is your feature request related to a problem or challenge?
When processing data that's already sorted by group keys, the current GroupBy implementation only partially utilizes this ordering. While it uses GroupOrdering to emit batches early, it still relies on a HashMap for the GroupValues mapping, which is suboptimal for pre-sorted data.
For pre-sorted data, GroupValues can be optimized by:
- Replacing the HashMap with a simpler tracking mechanism that only stores:
- The last row from the previous batch
- The count of previously seen groups
- Using arrow_ord::partition::partition for direct equality comparison between rows, eliminating the need for hashing
- Enabling earlier row emission: As soon as a new group is detected, the previous group is guaranteed to be complete and can be emitted
Current Behavior:
- Uses GroupOrdering for early batch emission
- Still maintains a HashMap for GroupValues mapping regardless of data ordering
Describe the solution you'd like
Implement a specialized GroupValues for ordered data that takes full advantage of these optimization opportunities. This should improve performance for the pre-sorted data case while maintaining the existing functionality for unordered data.
Expected Benefits:
- Reduced memory usage (no HashMap needed)
- Fewer operations (equality comparison vs hashing + equality comparison)
- Make GroupOrderingFull obsolete?
Describe alternatives you've considered
No response
Additional context
No response
take
I'd like to work on a scoped implementation for fully ordered group keys, starting with multi-column keys and keeping the existing paths for partially ordered and unordered input.
I checked main at 423df6f4: OrderedAggregateTableBuffer still calls new_group_values, and fully ordered multi-column inputs use GroupValuesColumn<true>. I also read #9662 and its review: the mixed performance results, especially the single-column regression, are a reason to establish a repeatable benchmark before choosing where to enable a new implementation.
My first step is a benchmark and correctness prototype covering cardinality, key types, run lengths, and groups spanning input batches. The implementation would preserve GROUP BY equality semantics and the existing emission/index contracts. I will compare both the grouping microbenchmark and end-to-end ordered aggregation against current main, and leave single-column specializations alone unless measurements justify changing them.
I am treating #23986 as related work: it accelerates consecutive keys while retaining the general hash-based path; this issue can use a proven full ordering to avoid that path. I'll keep that boundary explicit and credit the earlier implementation in #9662. Suggestions on representative ordered workloads or overlapping work are welcome.
This work is AI-assisted. So far I have inspected source and previous discussion; I have not yet run performance or correctness experiments and am not claiming a speedup.