[SPARK-58191][PYTHON] Refactor SQL_TRANSFORM_WITH_STATE_PYTHON_ROW_UDF and SQL_TRANSFORM_WITH_STATE_PYTHON_ROW_INIT_STATE_UDF#57337
Open
Yicong-Huang wants to merge 2 commits into
Conversation
HyukjinKwon
approved these changes
Jul 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
This PR refactors
SQL_TRANSFORM_WITH_STATE_PYTHON_ROW_UDFandSQL_TRANSFORM_WITH_STATE_PYTHON_ROW_INIT_STATE_UDFso that the worker uses the plainArrowStreamSerializerfor pure Arrow stream I/O, moving the per-eval-type logic fromTransformWithStateInPySparkRowSerializer/TransformWithStateInPySparkRowInitStateSerializerintoread_udfs()inworker.py:Rowobjects, regrouping by grouping key, invoking the UDF once per key with a lazy iterator ofRows, and converting the yielded(iterator of Row, Spark type)result back into a struct-wrapped ArrowRecordBatch.inputData/initStatestruct columns, regrouping rows by grouping key, re-chunking intoRowlists bounded byarrow_max_records_per_batch, splitting each chunk into a data iterator and an init-state iterator, and converting results back to Arrow.This mirrors the earlier refactor of the Pandas variants (SPARK-57400, SPARK-58128). Both variants are done together in one PR because they share a serializer class hierarchy (
...RowInitStateSerializerextends...RowSerializer) and are deleted together in the follow-up cleanup. The two serializer classes are kept for now and deleted in that cleanup.Why are the changes needed?
Part of SPARK-55388. Keeping serializers as pure Arrow stream I/O and concentrating eval-type-specific logic in
worker.pymakes the per-eval-type data flow explicit.Does this PR introduce any user-facing change?
No.
How was this patch tested?
Existing tests. No behavior change.
ASV comparison (
bench_eval_type.TransformWithStateRowUDFTimeBench/TransformWithStateRowInitStateUDFTimeBench,-a repeat=3, each combo run in a fresh Python process to match production): before =upstream/master, after = this PR. Values are from one representative run per side, keeping ASV's native+-intervals; the conclusion is consistent across runs. All deltas are within run-to-run noise, showing no regression. (A handful of combos flagged larger deltas only when the before/after runs were executed concurrently; re-running those in isolation brought every one back within noise, confirming they were benchmark-ordering / scheduling artifacts that do not apply in production where each Spark task runs in a fresh Python worker.)