[SPARK-56999][PYSPARK] Fix mapInArrow opaque getInt error by coercing output to declared schema#56049
Open
Yicong-Huang wants to merge 1 commit into
Open
[SPARK-56999][PYSPARK] Fix mapInArrow opaque getInt error by coercing output to declared schema#56049Yicong-Huang wants to merge 1 commit into
Yicong-Huang wants to merge 1 commit into
Conversation
… output to declared schema
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?
Make the
SQL_MAP_ARROW_ITER_UDFbranch inpython/pyspark/worker.pycoerce eachoutput
pa.RecordBatchagainst the UDF's declared output schema viaArrowBatchTransformer.enforce_schema, mirroring every sibling Arrow eval-typebranch.
reorder_by_nameis wired to the existingspark.sql.legacy.execution.pandas.groupedMap.assignColumnsByNameconfig, matchingSQL_GROUPED_MAP_ARROW_UDF/applyInArrow.Why are the changes needed?
Without coercion, an Arrow type mismatch between the UDF output and the declared
output schema surfaces in the JVM as an opaque accessor error:
Repro:
```python
import pyarrow as pa
from pyspark.sql.types import StructType, StructField, IntegerType
def double_x(iter_batches):
for batch in iter_batches:
df = batch.to_pandas()
df["x"] = df["x"] * 2
yield pa.RecordBatch.from_pandas(df[["x"]])
src = spark.createDataFrame([(1,), (2,), (3,)], ["x"]) # x is long (int64)
out = src.mapInArrow(double_x, schema=StructType([StructField("x", IntegerType())]))
out.show()
```
`createDataFrame` infers `x` as `LongType`; the pandas roundtrip preserves int64;
the declared schema is int32; the JVM picks `LongAccessor` for a `BigIntVector`
and the outer scan calls `getInt` on it, hitting the no-op base accessor.
Does this PR introduce any user-facing change?
Yes. `mapInArrow` output batches that previously caused an opaque JVM accessor
failure are now coerced in Python:
are cast with `safecheck=True`, so overflows still raise.
`spark.sql.legacy.execution.pandas.groupedMap.assignColumnsByName` (default
true), consistent with `applyInArrow`.
`RESULT_COLUMN_TYPES_MISMATCH` error instead of failing in the JVM.
How was this patch tested?
`python/run-tests --testnames pyspark.sql.tests.arrow.test_arrow_map`
(81 tests pass). Added regression test `test_coerce_output_type_to_declared_schema`
covering the int64 -> int32 case; updated `test_top_level_wrong_order` and
`test_nested_extraneous_field` to reflect the new coerce-by-name behavior.
Was this patch authored or co-authored using generative AI tooling?
No.