Skip to content

[SPARK-58939][PYSPARK] Move Arrow collect batch reordering into ArrowCollectSerializer - #58221

Open
Yicong-Huang wants to merge 1 commit into
apache:masterfrom
Yicong-Huang:simplify-arrow-collect-serializer
Open

[SPARK-58939][PYSPARK] Move Arrow collect batch reordering into ArrowCollectSerializer#58221
Yicong-Huang wants to merge 1 commit into
apache:masterfrom
Yicong-Huang:simplify-arrow-collect-serializer

Conversation

@Yicong-Huang

@Yicong-Huang Yicong-Huang commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Move the batch-reordering logic for Arrow-based collect from the _collect_as_arrow caller into ArrowCollectSerializer.

Dataset.collectAsArrowToPython (Spark classic) streams Arrow batches to Python out of order and appends batch-order indices at the end. Previously the serializer yielded raw batches plus the order list, and _collect_as_arrow split off the order list and reordered. Now ArrowCollectSerializer extends ArrowStreamSerializer and yields the batches already ordered, so the caller drops the results slicing, the reorder comprehension, and the isinstance check. The unused dump_stream delegate is also removed. Net -14 lines.

To keep spark.sql.execution.arrow.pyspark.selfDestruct.enabled effective, the serializer drops its reference to each batch as it yields it (batches[i] = None; the indices are a permutation), so peak memory stays ~1x.

Why are the changes needed?

The ordering protocol is an implementation detail of the collect stream; owning it inside the serializer removes duplicated logic from _collect_as_arrow and drops dead code, with no behavior change.

Does this PR introduce any user-facing change?

No.

How was this patch tested?

Existing python/pyspark/sql/tests/arrow/test_arrow.py (toPandas/toArrow + self-destruct) in CI; also validated reorder, JVM-error, empty-stream, and repr against a simulated stream.

Was this patch authored or co-authored using generative AI tooling?

No.

@uros-b

uros-b commented Aug 22, 2026

Copy link
Copy Markdown
Member

LGTM, thank you @Yicong-Huang!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants