HyukjinKwon commented on code in PR #60543:
URL: https://github.com/apache/spark/pull/60543#discussion_r4581886227
##########
python/pyspark/eval_handlers/_arrow.py:
##########
@@ -591,13 +591,27 @@ def run(self, split_index: int, data:
Iterator[GroupedBatch]) -> Iterator[pa.Rec
yield ArrowBatchTransformer.enforce_schema(batch,
self._return_schema)
+def _may_be_mutable_python_value(arrow_type: pa.DataType) -> bool:
+ """Whether a Python value of ``arrow_type`` can be a mutable object -- a
list, a dict, a
+ tuple holding one, or a bytearray -- rather than an immutable scalar."""
+ import pyarrow as pa
+
+ return (
+ pa.types.is_nested(arrow_type)
Review Comment:
Thanks, reproduced: with a `bytearray` buffer, a `StringType` result over
the default frame gave `['123', '123', '123']` at ae2290f. Switched to an
allowlist in d6f5cee: only null, boolean, integer, floating, decimal, date,
time, timestamp, duration and interval result types skip the conversion, so
`string` and `large_string` (and any other type) take the safe path.
`test_window_frames_with_in_place_buffer` now covers string and binary results
from a `bytearray`.
##########
python/pyspark/eval_handlers/_arrow.py:
##########
@@ -670,8 +691,17 @@ def fold(agg: Any, buffer: Any, value_cols: list, start:
int, end: int) -> Any:
running = fold(agg, agg.zero(), value_cols, begin,
end)
have_running = True
prev_begin, prev_end = begin, end
- results.append(agg.finish(running))
- result_arrays.append(pa.array(results, type=result_type))
+ result = agg.finish(running)
+ if convert_per_row:
+ results.append(pa.array([result],
type=result_type))
Review Comment:
Agreed. In d6f5cee, the results stay Python values and are converted only
right before `reduce` extends the buffer that they were finished from, and only
if one of them may be mutable. Immutable values (a `str`, `bytes`, or a tuple
of scalars like your `struct<mn, mx>` case) are never converted early. The
converted chunks are combined every 1024, so their overhead stays bounded.
With the handler on 20,000 rows (pyarrow 26, best of 3), a growing frame of
`struct<mn: double, mx: double>` tuples now takes 0.041s, against 0.212s at
ae2290f and 0.030s before this PR. RSS grows by 2.8 MB instead of 49 MB. Fresh
dicts over a growing frame still have to be converted per row, but over 200,000
rows RSS grows by 11 MB instead of 488 MB.
##########
python/pyspark/eval_handlers/_arrow.py:
##########
@@ -656,7 +670,14 @@ def fold(agg: Any, buffer: Any, value_cols: list, start:
int, end: int) -> Any:
# ``zero`` -- O(n) overall rather than O(n^2). Otherwise
-- the lower bound
# advanced (a row left the window, which ``reduce`` cannot
subtract) or the
# frame shrank -- refold the frame from ``zero``.
- results = []
+ #
+ # Later rows keep reducing into the running buffer, which
``reduce`` may
+ # update in place, so a row's ``finish`` result can share
state with it,
+ # e.g. a list that ``reduce`` appends to. Convert such a
result to Arrow at
+ # its own row, before the next ``reduce`` changes it; a
result whose Python
+ # value is immutable is converted with the others after
the loop.
+ convert_per_row = _may_be_mutable_python_value(result_type)
Review Comment:
Done in d6f5cee, along the lines of your snippet. The pending results are
converted right before the extend branch reduces into `running` again, but not
when `end == prev_end`, since nothing is reduced then. A `rowsBetween(-2,
currentRow)` frame over 20,000 rows now takes 0.067s, against 0.250s at ae2290f
and 0.058s before this PR.
##########
python/pyspark/eval_handlers/_arrow.py:
##########
@@ -670,8 +691,17 @@ def fold(agg: Any, buffer: Any, value_cols: list, start:
int, end: int) -> Any:
running = fold(agg, agg.zero(), value_cols, begin,
end)
have_running = True
prev_begin, prev_end = begin, end
- results.append(agg.finish(running))
- result_arrays.append(pa.array(results, type=result_type))
+ result = agg.finish(running)
+ if convert_per_row:
+ results.append(pa.array([result],
type=result_type))
+ else:
+ results.append(result)
+ if not convert_per_row:
Review Comment:
Done in d6f5cee. The result is built with `pa.chunked_array(chunks,
type=result_type).combine_chunks()`, and whether each UDF's result type can be
mutable is computed once in `__init__`.
--
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]