comphead commented on code in PR #27466:
URL: https://github.com/apache/datafusion/pull/27466#discussion_r4645954931
##########
datafusion/physical-plan/src/windows/bounded_window_agg_exec.rs:
##########
@@ -1095,13 +1130,32 @@ impl PartitionSearcher for SortedSearch {
// Reorder `partition_columns` such that its ordering matches input
ordering.
let partition_columns_ordered =
get_at_indices(&partition_columns,
&self.ordered_partition_by_indices)?;
- let partition_points =
+ let mut partition_points =
evaluate_partition_ranges(num_rows, &partition_columns_ordered)?;
let partition_bys = partition_columns
.into_iter()
.map(|arr| arr.values)
.collect::<Vec<ArrayRef>>();
+ // Only the first partition, which may continue a buffered one, and the
+ // last, which may continue in the next batch, need to be buffered.
+ let n_partitions = partition_points.len();
+ if self.can_evaluate_complete && n_partitions > 2 {
+ debug_assert!(
+ self.complete_out.is_empty(),
+ "the complete partitions of the previous batch must be emitted"
+ );
+ let complete = 1..n_partitions - 1;
+ // One run, since these partitions are inside one input batch
+ self.complete_out = compute_window_aggregates(
Review Comment:
True, but the stream cannot resume after an error on `main` either, so I
kept it. `update_partition_batch` can fail in `extend` or `concat_batches`
after earlier partitions were already appended to `partition_buffers`.
`compute_aggregates` can fail on the second window expression after the first
one advanced its state. Polling again after such an `Err` would emit misaligned
rows in both cases, and consumers stop at the first error. If we want the
stream to be resumable, that is a wider change than `complete_out`.
##########
datafusion/core/tests/user_defined/user_defined_window_functions.rs:
##########
@@ -369,6 +375,44 @@ async fn test_udwf_bounded_window_returns_null() {
assert_eq!(test_state.evaluate_all_called(), 0);
}
+/// A window function that is neither an aggregate nor sets
Review Comment:
Added in e0d1948b1 by reusing `test_udwf_bounded_window`, which now runs
with the flag off and on. With it on, `WindowAggExec` evaluates both partitions
in one call, so `evaluate` sees all 10 rows instead of 7, with the same results.
##########
datafusion/physical-plan/src/windows/utils.rs:
##########
@@ -22,3 +26,64 @@ pub(crate) fn create_schema(
.finish()
.with_metadata(input_schema.metadata().clone()))
}
+
+/// Evaluates `window_expr` on each of the whole partitions of `batch` in
+/// `partitions`, and returns one array per window expression that covers
+/// `partitions` in order. An expression that evaluates several partitions in
+/// one call gets runs of at most `batch_size` rows, or of one larger
+/// partition, so that its frames stay batch-sized.
+pub(crate) fn compute_window_aggregates(
+ window_expr: &[Arc<dyn WindowExpr>],
+ batch: &RecordBatch,
+ partitions: &[Range<usize>],
+ batch_size: usize,
+) -> Result<Vec<ArrayRef>> {
+ // The arrays to concatenate for each expression
+ let mut results = vec![vec![]; window_expr.len()];
+ // The expressions that are evaluated one partition at a time
+ let mut per_partition = vec![];
+ for (idx, expr) in window_expr.iter().enumerate() {
+ for run in partition_runs(partitions, batch_size) {
+ let Some(result) = expr.evaluate_partitions(batch, run)? else {
+ results[idx] = Vec::with_capacity(partitions.len());
+ per_partition.push(idx);
+ break;
+ };
+ results[idx].push(result);
+ }
+ }
+ if !per_partition.is_empty() {
Review Comment:
Confirmed. In a profile of `window partition and order by, u64_wide,
aggregate functions`, the per-partition `evaluate_args` was 29% of
`compute_window_aggregates`, `order_by_columns` 12%, the batch slice and its
drop 9%, and the per-partition `iter_to_array` 7%.
Plain and sliding aggregates now implement `evaluate_partitions`
(`AggregateWindowExpr::aggregate_evaluate_partitions`) in e0d1948b1. The
arguments, FILTER and ORDER BY are evaluated once per run. Each partition gets
a new accumulator, and its frames are offsets into the run's arrays, so the
values are not sliced per partition. A constant result is computed once per
partition and repeated with one `take`. A run with a single partition, which
can be larger than a batch, keeps `aggregate_evaluate`, so its frames are not
collected.
`window_query_sql` against `main`: `window partition and order by, u64_wide,
aggregate functions` 451 ms to 96 ms (333 ms before this change), and `window
partition by, u64_wide, aggregate functions` 223 ms to 124 ms. The other cases
are within noise. The `complete_partitions` case in `window.slt` gained a
FILTER column, since without it no test caught a dropped FILTER on this path.
##########
datafusion/physical-expr/src/window/standard.rs:
##########
Review Comment:
Agreed, done in e0d1948b1. `WindowFrameContext::reset()` in
`window_state.rs` clears the GROUPS state and drops the cached RANGE
comparator, so RANGE no longer depends on the `Arc::ptr_eq` key. The frame loop
moved to a shared `calculate_frames` helper, which the aggregate path from your
other comment uses too.
I kept one context with a reset rather than a new one per partition. A new
context clones the shared `Arc<WindowFrame>`, and that refcount contends across
threads: with 1M one-row partitions it took 90 ms at `target_partitions = 18`
against 7 ms with the reused context.
##########
datafusion/expr/src/partition_evaluator.rs:
##########
Review Comment:
Done in e0d1948b1. The implementation table now points to `evaluate_ranges`
and `evaluates_frames_independently`.
--
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]