kumarUjjawal commented on code in PR #27466:
URL: https://github.com/apache/datafusion/pull/27466#discussion_r4638355440
##########
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:
`complete_out` is set in `evaluate_partition_batches` before anything else
in the batch can fail. If the batch then errors, nothing clears it.
##########
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:
Myabe a test for `evaluates_frames_independently` flag, upto you.
##########
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:
For aggregates, the new per-partition loop slices a `RecordBatch` and calls
`evaluate` once per partition. Each call re-evaluates the argument, FILTER and
ORDER BY expressions and builds a new `WindowFrameContext` and comparator for a
tiny slice. `evaluate_frames` instead evaluates arguments once per run.
##########
datafusion/physical-expr/src/window/standard.rs:
##########
Review Comment:
https://github.com/apache/datafusion/blob/6b8ef8ec77ddcc357d94727e506f5541821fc244/datafusion/physical-expr/src/window/standard.rs#L193
`evaluate_frames` reuses one `WindowFrameContext` across partitions. It
resets the GROUPS state by writing its public fields by hand, and RANGE is only
safe because its comparator cache is keyed by `Arc::ptr_eq` on fresh slices.
The reset belongs in `window_state.rs`, as a `WindowFrameContext::reset()` or a
new context per partition.
##########
datafusion/expr/src/partition_evaluator.rs:
##########
Review Comment:
https://github.com/apache/datafusion/blob/6b8ef8ec77ddcc357d94727e506f5541821fc244/datafusion/expr/src/partition_evaluator.rs#L193
Should we update The `PartitionEvaluator` docs for the new flag.
--
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]