cetra3 opened a new pull request, #27855: URL: https://github.com/apache/datafusion/pull/27855
> Claude (an AI agent) is raising this PR on behalf of @cetra3. ## Which issue does this PR close? - Closes #27851. This PR is a draft because it needs #27391 to merge first. It calls `compact_view_buffers`, which #27391 makes public in `datafusion_common::utils`. Until then, the diff here includes the commits of #27391; only the last commit is new. I will rebase onto `main` and mark it ready for review after #27391 merges. ## Rationale for this change `nth_value` over a struct, list or map that contains a `Utf8View` or `BinaryView` value takes time quadratic in the number of groups. The repro in #27851 takes 5.5 s for 16,000 groups. The same query with a `Utf8` field takes 25 ms. `nth_value` has no `GroupsAccumulator`, so it runs through `GroupsAccumulatorAdapter`. The adapter builds its state array from one scalar per group with `ScalarValue::iter_to_array`. For a nested type this concatenates the values, and the result keeps the data buffers of every value. In the repro, each batch that reaches the final stage has 16,000 data buffers, and each group's value is a slice that carries all of them. `evaluate` then builds its output from about 2,000 such values per partition, which takes about 2 s per partition. ## What changes are included in this PR? - `GroupsAccumulatorAdapter` compacts the view buffers of each array it builds from per-group scalars. It does this in `state`, `evaluate`, `evaluate_preserving` and `convert_to_state`, through one helper. - It compacts only when the data type is nested and contains a view type. A plain `Utf8View` column is not affected by the problem, so it is left as it is. - This applies to every aggregate that uses the adapter, not only `nth_value`. - Compaction copies the bytes of each view. Values that shared bytes each get their own copy, as they would for a `Utf8` field. - An `nth_value_nested_view` group in `datafusion/core/benches/aggregate_query_sql.rs` runs the query from #27851 with 16,000 groups. It shares a helper with the `first_last_nested_view` group from #27391. ## What is the testing strategy for this PR? - `adapter_compacts_nested_view_buffers` runs 50 groups of a struct with a `Utf8View` field through the adapter. It checks the values and that the output of each of the four functions has one data buffer. Without the fix, each has 50. - The extended test suite passes. ### Benchmarks (local, `main` vs this PR) `cargo bench -p datafusion --bench aggregate_query_sql -- nth_value_nested_view`, 16,000 groups: | case | main | this PR | |---|---|---| | `struct_utf8` (control) | 24.5 ms | 25.1 ms | | `struct_utf8view` | 5.56 s | 26.7 ms | ## Are there any user-facing changes? Faster aggregates on nested values that contain view types, when the aggregate runs through `GroupsAccumulatorAdapter`. No API changes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- 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]
