Yicong-Huang opened a new pull request, #57800:
URL: https://github.com/apache/spark/pull/57800

   ### What changes were proposed in this pull request?
   
   Tighten the `mapInPandas` (`SQL_MAP_PANDAS_ITER_UDF`) return-value contract 
in `worker.py` to require a strict `Iterator`, matching `mapInArrow` 
(`SQL_MAP_ARROW_ITER_UDF`).
   
   Previously the runtime check was `isinstance(result, Iterator) or 
hasattr(result, "__iter__")`, which accepted any iterable (e.g. a returned 
`list`). It is now `isinstance(result, Iterator)`; a non-`Iterator` iterable is 
rejected with `UDF_RETURN_TYPE` ("iterator of 
pandas.DataFrame"/"pandas.Series"). The comment explaining why 
`verify_return_type` was not reused is removed accordingly.
   
   ### Why are the changes needed?
   
   The declared signature has always been `PandasMapIterFunction = 
Callable[[Iterator[DataFrameLike]], Iterator[DataFrameLike]]`, and the 
`DataFrame.mapInPandas` docstring states the function "outputs an iterator of 
pandas.DataFrames". The runtime, however, leniently accepted any iterable, 
diverging from the documented contract. This aligns the runtime with the 
declared `Iterator[...]` signature, mirroring the same tightening already done 
for `mapInArrow` in SPARK-56612. It also unblocks reusing `verify_return_type` 
at the `mapInPandas` site with `Iterator[...]` (SPARK-58598).
   
   ### Does this PR introduce _any_ user-facing change?
   
   No. Minor tightening: a UDF returning a non-`Iterator` iterable (e.g. 
`list`) is now rejected with `UDF_RETURN_TYPE`, aligning the runtime with the 
documented `Iterator[...]` signature. This mirrors the `mapInArrow` tightening 
in SPARK-56612.
   
   ### How was this patch tested?
   
   Updated `test_pandas_map.py`: removed the "returning list of DataFrames" 
positive case from `test_map_in_pandas`, and added a `list_not_iter` negative 
case to `check_other_than_dataframe_iter` asserting a returned `list` is 
rejected, mirroring `test_arrow_map.py::test_other_than_recordbatch_iter`. Ran 
`pyspark.sql.tests.pandas.test_pandas_map` and the Connect parity suite.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   No
   


-- 
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]

Reply via email to