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

   ### Is your feature request related to a problem or challenge?
   
   Follow-ups to https://github.com/apache/datafusion/pull/27466, which 
evaluates partitions that start and end inside one input batch with one 
`WindowExpr::evaluate_partitions` call per run of partitions, in 
`BoundedWindowAggExec` and `WindowAggExec`. Part of 
https://github.com/apache/datafusion/issues/26541. These came up in its review.
   
   ### Describe the solution you'd like
   
   1. **Evaluate constant aggregates with a `GroupsAccumulator`.** For an 
aggregate whose result is the same in every row of a partition, such as `max(f) 
OVER (PARTITION BY k)`, `aggregate_evaluate_partitions` still creates one 
`Accumulator` per partition, slices the arguments for it, and collects one 
`ScalarValue` per partition.
      - With https://github.com/apache/datafusion/pull/27466 on 4M one-row 
partitions, `WindowAggExec` takes about 890 ms for `max`, `min` and `sum`, 
against about 100 ms for `first_value`, `last_value` and `nth_value` (window 
`elapsed_compute`).
      - A samply profile of the aggregate query puts 62% of its CPU in 
`get_aggregate_result_inside_range` (29% slicing the arguments, 11% 
`update_batch`) and 14% in creating the accumulators.
      - `AggregateFunctionExpr::create_groups_accumulator` could take a whole 
run in one `update_batch`, with the indices of the existing gather as group 
indices and the FILTER mask as `opt_filter`.
      - Larger partitions keep the `Accumulator` path, so results such as float 
sums must not depend on which path ran.
      - This is a concrete step toward 
https://github.com/apache/datafusion/issues/15607.
   2. **Drop the dictionary decline once `iter_to_array` handles it.** 
`ScalarValue::iter_to_array` gives every row of a dictionary its own key 
(https://github.com/apache/datafusion/issues/16550). So the PR evaluates 
aggregates with a dictionary result one partition at a time, and documents the 
same limit for `PartitionEvaluator::evaluates_frames_independently`. A single 
partition already fails on `main`:
   
      ```sql
      SELECT count(DISTINCT b) FROM (
        SELECT any_value(d) OVER (ORDER BY k) AS b
        FROM (SELECT value AS k, arrow_cast(concat('s', value % 7), 
'Dictionary(Int8, Utf8)') AS d
          FROM generate_series(1, 300))
      );
      -- Internal error: Can not create index of type Int8 from value 128.
      ```
   
      Fixing https://github.com/apache/datafusion/issues/16550 would fix this 
query, and the decline and the doc paragraph could then go.
   3. **Admit window functions that use the frame without the flag.** 
`BoundedWindowAggExec` evaluates complete partitions directly only when every 
window expression is an aggregate or sets `evaluates_frames_independently`.
      - For a function that uses the window frame, `evaluate` and 
`evaluate_stateful` already make the same single `evaluate_ranges` call on a 
whole partition.
      - So the gate could check `uses_window_frame()` alone, and the flag would 
only decide whether several partitions share one call.
      - A user-defined function that uses the frame would then no longer switch 
off the direct path for the aggregates in the same node.
      - This changes which path such functions take, so it needs a UDWF test.
   4. **Fuzz constant aggregates.** `window_fuzz` compares `WindowAggExec` with 
`BoundedWindowAggExec`, so its frames never use `UNBOUNDED FOLLOWING`, and it 
never reaches aggregates that are constant in a partition.
      - An injected fault that made them read only the first row of each 
partition passed both window fuzz tests, and only `window.slt` caught it.
      - Comparing `WindowAggExec` at `batch_size = 1`, where each partition is 
evaluated on its own, with a larger batch size would cover these frames.
   
   Each item can be its own PR.
   
   ### Describe alternatives you've considered
   
   For item 1, https://github.com/apache/datafusion/pull/27410 
(https://github.com/apache/datafusion/issues/27400) adds new `Accumulator` 
methods with typed kernels for `SUM`, `COUNT`, `AVG`, `MIN` and `MAX` in 
`BoundedWindowAggExec`, and leaves `WindowAggExec` as it is.
   


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