HyukjinKwon opened a new pull request, #60541:
URL: https://github.com/apache/spark/pull/60541

   ### What changes were proposed in this pull request?
   
   This is a follow-up of #57899 (SPARK-58695). A scalar or scalar iterator 
Arrow UDF lifted out of a higher-order function lambda 
(`SQL_SCALAR_ARROW_ELEMENTWISE_UDF`, `SQL_SCALAR_ARROW_ITER_ELEMENTWISE_UDF`) 
now always casts its result to the declared type safely, as the same UDF does 
outside a lambda (`SQL_SCALAR_ARROW_UDF`, `SQL_SCALAR_ARROW_ITER_UDF`).
   
   The Arrow flavor of `_VectorizedElementwiseUDFHandler._to_flat_batch` passed 
`runner_conf.safecheck`, which is 
`spark.sql.execution.pandas.convertToArrowArraySafely`, to `enforce_schema`. 
That setting belongs to pandas UDFs, so the pandas flavor keeps following it, 
as its base path does.
   
   ### Why are the changes needed?
   
   With `spark.sql.execution.pandas.convertToArrowArraySafely` set to false, a 
lifted Arrow UDF silently overflows or truncates a result that does not fit its 
declared type, while the same UDF outside a lambda raises:
   
   ```python
   import pyarrow as pa
   from pyspark.sql import functions as sf
   from pyspark.sql.functions import arrow_udf
   
   spark.conf.set("spark.sql.execution.pandas.convertToArrowArraySafely", 
"false")
   
   @arrow_udf("int")
   def add_max_int(a: pa.Array) -> pa.Array:
       return pa.compute.add(a, 2147483647)
   
   df = spark.createDataFrame([([1, 2],)], "values array<long>")
   df.select(add_max_int(df.values[0])).collect()
   # PythonException: ... [RESULT_COLUMN_TYPES_MISMATCH] ... (expected int32, 
actual int64)
   df.select(sf.transform("values", lambda x: add_max_int(x))).collect()
   # Before: [Row(...=[-2147483648, -2147483647])]
   # After:  PythonException: ... [RESULT_COLUMN_TYPES_MISMATCH] ...
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, in unreleased code only (SPARK-58695 is not released yet). With 
`spark.sql.execution.pandas.convertToArrowArraySafely` set to false, a lifted 
Arrow UDF whose result does not fit its declared type now raises 
`RESULT_COLUMN_TYPES_MISMATCH` instead of silently returning overflowed or 
truncated values.
   
   ### How was this patch tested?
   
   Added `test_scalar_arrow_udf_result_cast_is_safe`, run in classic and 
Connect modes. It checks scalar and scalar iterator Arrow UDFs that return 
out-of-range values, inside and outside a lambda, with the setting both on and 
off. Without the fix, the lifted cases fail to raise when the setting is off.
   
   Ran `pyspark.sql.tests.test_udf_in_higher_order_function` and 
`pyspark.sql.tests.connect.test_parity_udf_in_higher_order_function` locally.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.5)
   
   This pull request and its description were written by Isaac.
   


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