kosiew opened a new issue, #27903:
URL: https://github.com/apache/datafusion/issues/27903

   ## Related PR
   #27047 
[comment](https://github.com/apache/datafusion/pull/27047#pullrequestreview-6102969604)
   
   ## Problem
   
   In [`first_last.rs`](datafusion/functions-aggregate/src/first_last.rs), 
[#27303](https://github.com/apache/datafusion/pull/27303) added 
`FirstLastPrimitiveKeyGroupsAccumulator` for a single primitive `ORDER BY` key. 
`FirstLastGroupsAccumulator` handles other supported orderings; #27047 adds 
Arrow row storage to that path while retaining a scalar fallback. Both paths 
track whether a group has a winner, update its value, and implement 
partial-state emission and merge. Specializing a key representation currently 
means duplicating much of that lifecycle, so fixes to filtering, winner 
tracking, or partial emission can land in one path and miss the other.
   
   ## Proposed direction
   
   Use one ordered `GroupsAccumulator` implementation for the lifecycle. Reuse 
`ValueState` for winning values and put typed single-key, Arrow row, and scalar 
key storage behind a private ordering strategy. Keep comparison and key updates 
batched so the primitive path can downcast once per batch and keep its native 
comparison loop. The shared accumulator should own row qualification, winner 
presence, application of winning value updates, and assembly of partial state. 
Do not force every backend into the same inner loop.
   
   Confirm this boundary with maintainers before implementation. Compare full 
consolidation with extracting only shared lifecycle logic while retaining two 
entry points. If full consolidation adds measurable overhead or more complexity 
than it removes, keep the typed kernel and share the smallest lifecycle 
boundary that eliminates duplication. Routing primitive keys through Arrow rows 
alone is not the goal: it would give up the typed fast path without evidence 
that it is safe to do so.
   
   ## Scope
   
   - **In:** Ordered grouped `FIRST_VALUE` and `LAST_VALUE`, including the 
single-primitive-key fast path, multi-key row storage, and scalar fallback. 
Preserve current dispatch eligibility.
   - **Out:** Unordered and non-grouped accumulators, window execution, planner 
changes, new ordering types, and the [large view-key decode 
fix](NEW_ISSUE_01.md).
   - Use a baseline with both optimizations present. Coordinate with #27047 
rather than duplicating its row-storage work; if it lands first, rebase the 
refactor on the merged implementation.
   
   ## Acceptance criteria
   
   - [ ] Ordered grouped paths share lifecycle code for accepting candidates, 
updating winning values, and emitting/merging state. Document any specialized 
logic that remains and why it belongs in the backend.
   - [ ] Preserve winner selection for each path: ascending/descending and NULL 
placement, ties with distinct payloads, lexicographic secondary keys, `FILTER`, 
and `IGNORE NULLS`. Pre-ordered updates keep their current positional behavior; 
merging still compares keys even when updates used the pre-ordered shortcut.
   - [ ] Preserve `[value, ordering keys…, is_set]` with the same field types, 
nulls, and row alignment. After `EmitTo::First(n)`, both retained and newly 
added groups can still update and merge correctly. No public API or state 
schema changes.
   - [ ] Report before/after throughput and retained memory for the primitive 
and general paths, plus an unaffected unordered control. Explain any material 
regression before choosing a design.
   
   ## Tests / verification
   
   - Run `cargo test -p datafusion-functions-aggregate first_last` and the 
existing `first_last_primitive_key.slt`, `first_last_ordered.slt`, and 
`ordered_aggregate_key_types.slt` suites before and after. Assert expected 
winners and exact partial state, not the concrete accumulator type. Cover typed 
timestamp state (including time zone), a nullable secondary key, and an 
ordering that uses the scalar fallback.
   - Check partial emission followed by further updates and a merge. Include a 
merge where the better key is not in the position the pre-ordered update 
shortcut would select.
   - Use the `first_last` Criterion benchmark and the [`first_last_orderings` 
SQL benchmark in #27864](https://github.com/apache/datafusion/pull/27864). 
Compare matched runs with one primitive key and multi-key fixed- and 
variable-width cases, on pre-ordered and shuffled input where applicable. 
Include single- and multi-partition runs for a time-zone-aware key to check the 
workload in [#27046](https://github.com/apache/datafusion/issues/27046).
   


-- 
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]

Reply via email to