HyukjinKwon opened a new pull request, #60543:
URL: https://github.com/apache/spark/pull/60543
### What changes were proposed in this pull request?
This is a follow-up of #58142 (SPARK-58890). Over a window frame whose lower
bound stays fixed, `ArrowWindowAggIncrementalUDFHandler` extends one running
buffer row by row instead of refolding each frame. It now converts each row's
`finish` result to Arrow at that row, before the next `reduce`, when the output
type's Python value can be mutable (nested and binary types). Other results are
still converted together after the loop.
It also documents the window lifecycle on `Aggregator`: `merge` is not used;
`finish` is called once per row on a buffer that later rows keep reducing into,
so it must not modify it; and `reduce` may update a mutable buffer field in
place (as `zero` returns a new buffer each time). The eval type comments that
described a fresh fold per frame and no exchange are corrected to match.
### Why are the changes needed?
The handler kept each row's `finish(running)` result as a live Python object
until the whole partition was processed. When `reduce` updates the buffer in
place and `finish` returns part of it, every row ends up with the partition's
last value. This happens for a `collect_list`-style aggregator over an ordered
window's default frame:
```python
from pyspark.sql import Window, functions as sf
from pyspark.sql.aggregator import Aggregator
from pyspark.sql.functions import udaf
from pyspark.sql.types import ArrayType, DoubleType, StructField, StructType
class CollectList(Aggregator):
@property
def bufferSchema(self):
return StructType([StructField("values", ArrayType(DoubleType()))])
@property
def outputType(self):
return ArrayType(DoubleType())
def zero(self):
return ([],)
def reduce(self, buffer, value):
buffer[0].append(value[0])
return buffer
def merge(self, b1, b2):
b1[0].extend(b2[0])
return b1
def finish(self, buffer):
return buffer[0]
df = spark.createDataFrame([(0, 1.0), (0, 2.0), (0, 3.0)], "k int, v double")
w = Window.partitionBy("k").orderBy("v")
df.select("v",
udaf(CollectList())("v").over(w).alias("l")).orderBy("v").collect()
# Before: [Row(v=1.0, l=[1.0, 2.0, 3.0]), Row(v=2.0, l=[1.0, 2.0, 3.0]),
Row(v=3.0, l=[1.0, 2.0, 3.0])]
# After: [Row(v=1.0, l=[1.0]), Row(v=2.0, l=[1.0, 2.0]), Row(v=3.0, l=[1.0,
2.0, 3.0])]
```
A shallow copy of each result would not be enough, because a nested result
such as a struct holding the list would still share the list with the buffer.
### Does this PR introduce _any_ user-facing change?
Yes, in unreleased code only (SPARK-58890 is not released yet). Incremental
Python aggregators over a window frame with a fixed lower bound return each
row's own value even when `reduce` updates the buffer in place.
### How was this patch tested?
Added `test_window_growing_frame_with_in_place_buffer`, run in classic and
Connect modes. It compares an in-place `CollectList`, and a variant that nests
the list in a struct, with `collect_list` over the default,
`rowsBetween(unboundedPreceding, currentRow)` and
`rangeBetween(unboundedPreceding, currentRow)` frames. It fails without the fix.
Ran `pyspark.sql.tests.arrow.test_arrow_python_aggregator` and
`pyspark.sql.tests.connect.arrow.test_parity_arrow_python_aggregator` locally.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Opus 5.5)
This pull request and its description were written by Isaac.
--
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]