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

   ## Related PR
   #27047 [follow up 
comment](https://github.com/apache/datafusion/pull/27047#discussion_r4731290081)
   
   ## Problem
   
[main](https://github.com/apache/datafusion/commit/a56631d71d16cd14895a27653ae30212ece1bda0)
   
   A grouped `FIRST_VALUE(v ORDER BY view_key, tie_breaker)` or `LAST_VALUE` 
can select its winners successfully, then fail while emitting partial state. 
`Utf8View` and `BinaryView` (including dictionary-wrapped views) are eligible 
for row storage in 
[`first_last.rs`](datafusion/functions-aggregate/src/first_last.rs). Arrow 
60.2.0's row decoder builds one data buffer per decoded view column and rejects 
more than `i32::MAX` bytes of non-inline values in that column. Dictionary 
hydration hits this check before DataFusion casts the output back to the 
declared dictionary type.
   
   One partial partition with 4,096 groups, each retaining a 512 KiB key, needs 
2,147,483,648 non-inline bytes in that ordering column. The [grouped hash 
stream](datafusion/physical-plan/src/aggregates/grouped_hash_stream.rs) can 
call `state(EmitTo::All)` before slicing the output into smaller batches, so 
slicing does not prevent the error.
   
   ## Why it matters
   
   The previous scalar emitter could build a view array with multiple data 
buffers. This is a new failure for valid view states, not a new regression for 
ordinary `Utf8` or `Binary`, whose 32-bit offsets already impose a size limit.
   
   ## Reproducer (Arrow 60.2.0)
   
   From the DataFusion checkout root, save the code below as 
`datafusion/functions-aggregate/examples/view_row_decode_repro.rs` (create the 
`examples` directory if needed). Run `cargo run --locked -p 
datafusion-functions-aggregate --example view_row_decode_repro`. Cargo 
discovers the example automatically and uses the crate's existing `arrow` 
dependency; no manifest edits are needed.
   
   The probe also runs on `main` with Arrow 60.2.0, even without the grouped 
row-storage change from #27047. It decodes one row successfully, then passes 
4,096 references to that same row to the decoder. This exercises the exact 
limit without allocating 2 GiB of input or output. It verifies the Arrow limit, 
not a grouped SQL failure on `main`.
   
   ```rust
   use std::sync::Arc;
   
   use arrow::array::{Array, ArrayRef, BinaryViewArray, StringViewArray};
   use arrow::row::{RowConverter, SortField};
   
   fn main() {
       let key = "x".repeat(512 * 1024);
       let columns: [ArrayRef; 2] = [
           Arc::new(StringViewArray::from(vec![key.as_str()])),
           Arc::new(BinaryViewArray::from(vec![key.as_bytes()])),
       ];
       for column in columns {
           let data_type = column.data_type().clone();
           let converter = 
RowConverter::new(vec![SortField::new(data_type.clone())]).unwrap();
           let rows = converter.convert_columns(&[column]).unwrap();
           let row = rows.row(0);
           let decoded = converter.convert_rows([row]).unwrap();
           assert_eq!(decoded[0].data_type(), &data_type);
           assert_eq!(decoded[0].len(), 1);
   
           let error = converter
               .convert_rows(std::iter::repeat_n(row, 4096))
               .unwrap_err();
           assert!(error.to_string().contains(
               "2147483648 bytes of non-inlined values too long to decode into 
a view array with a single data buffer"
           ));
           println!("{data_type:?}: {error}");
       }
   }
   ```
   
   **Observed:** Both view types report the single-buffer error above. The 
review also confirmed a `Partial` → `FinalPartitioned` plan for a small grouped 
query with `(Utf8View, Int64)` ordering. The full-size DataFusion query was not 
run; the call path from partial-state emission to Arrow was verified in source.
   
   ## Desired behavior
   
   For valid view keys that fit in a multi-buffer array, partial state emission 
must not fail solely because the combined view payload exceeds one Arrow row 
decoder buffer. Preserve `[value, ordering keys…, is_set]`, including key 
types, nulls, and row order, so final merge selects the same winners as the 
scalar path.
   
   ## Proposed direction
   
   Fix this at `decode_rows`, where retained ordering rows are turned into 
partial state. Split rows into chunks bounded by the non-inline bytes of each 
decoded view column, decode them, and concatenate in row order using view 
concatenation, which retains multiple buffers. Cover dictionary-wrapped views 
before casting back to the declared state types. Keep small states on the 
normal decode path.
   
   ## Scope
   
   - **In:** Grouped `FIRST_VALUE` / `LAST_VALUE` partial-state emission for 
plain and dictionary-wrapped `Utf8View` and `BinaryView` ordering keys.
   - **Out:** Grouping-key storage, widening `Utf8` / `Binary` offsets, and the 
unrelated capacity-retention and timestamp coverage findings.
   
   ## Acceptance criteria
   
   - [ ] View ordering state spanning more than one decoder buffer emits and 
merges without the single-buffer error, given sufficient memory.
   - [ ] State preserves value/key/flag alignment, declared types (including 
dictionary types), and nulls across chunk boundaries; final winners match the 
scalar baseline.
   - [ ] Ordinary small view states retain the row path, or a fallback is 
justified with Q06 measurements.
   
   ## Tests / verification
   
   - Add a bounded regression in `first_last.rs`. For chunked decoding, use a 
small test byte budget to force a split; test at the budget and just over it. 
Assert exact state and merged winners for both aggregate directions, including 
both view types and dictionary-wrapped cases. Include null or inline keys and a 
secondary key that resolves a tie. Avoid a multi-GiB fixture in regular CI.
   - Run `cargo test -p datafusion-functions-aggregate first_last`. Use the 
Arrow probe above to verify the underlying decoder limit independently of 
DataFusion.
   


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