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

   > Claude (an AI agent) is raising this issue on behalf of @cetra3.
   
   ## Describe the bug
   
   `nth_value` over a nested value (a struct or a list) that contains a 
`Utf8View` value takes time quadratic in the number of groups. The same query 
with a `Utf8` value runs in linear time.
   
   This is the same shape as #27390, which was in `first_value` / `last_value` 
and is fixed by #27391. `nth_value` runs through `GroupsAccumulatorAdapter`, so 
that fix does not cover it.
   
   ## To Reproduce
   
   ```sql
   SET datafusion.execution.target_partitions = 8;
   
   -- Control: a Utf8 field.
   SELECT count(c) FROM (
     SELECT nth_value(struct('a string over 12 bytes'), 1 ORDER BY value) AS c
     FROM generate_series(1, 16000) GROUP BY value::text
   );
   
   -- A Utf8View field.
   SELECT count(c) FROM (
     SELECT nth_value(struct(arrow_cast('a string over 12 bytes', 'Utf8View')), 
1 ORDER BY value) AS c
     FROM generate_series(1, 16000) GROUP BY value::text
   );
   ```
   
   `datafusion-cli`, `release-nonlto` profile, on `main` plus #27391:
   
   | groups | `Utf8` | `Utf8View` |
   |---|---|---|
   | 8,000 | 20 ms | 1.26 s |
   | 16,000 | 30 ms | 5.07 s |
   
   The string must be longer than 12 bytes. `make_array(...)` in place of 
`struct(...)` is equally slow, and so is the query without `ORDER BY`. A plain 
`Utf8View` value, not nested, is not affected.
   
   ## Expected behavior
   
   The `Utf8View` query runs in about the same time as the `Utf8` query, and 
its time grows linearly with the number of groups.
   
   ## Additional context
   
   `nth_value` has no `GroupsAccumulator`, so it runs per group through 
`GroupsAccumulatorAdapter`. `GroupsAccumulatorAdapter::state` builds the state 
array from one scalar per group with `ScalarValue::iter_to_array`, and the 
result keeps the data buffers of every input. In the final stage, each group's 
value is a slice of that array and carries all of its data buffers. `evaluate` 
then concatenates one such value per group, so it costs O(N^2).
   
   With `n = 2` the same query is fast, because every group has one row and 
`evaluate` returns NULL.
   
   Compacting the view buffers of the state array once in 
`GroupsAccumulatorAdapter::state`, with the `compact_view_buffers` helper from 
#27391, makes the query linear in a local experiment: 31 ms at 16,000 groups 
and 146 ms at 128,000, the same as the `Utf8` control. That change affects 
every aggregate that uses the adapter, so it needs its own benchmarks.
   
   ## Willingness to contribute
   
   Yes.
   


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