Copilot commented on code in PR #60613:
URL: https://github.com/apache/spark/pull/60613#discussion_r4627101469
##########
python/pyspark/pandas/numpy_compat.py:
##########
@@ -253,6 +253,49 @@ def _fmin_func(c1: Column, c2: Column) -> Column:
return F.when(c1 == c2, tie).otherwise(F.least(c1, c2))
+def _integer_ufunc_func(ufunc: Any) -> Callable[[np.dtype], Callable[...,
Column]]:
+ """
+ Return the mapping of a NumPy ufunc that takes integers only, such as
np.gcd.
+
+ The mapping takes the integer dtype NumPy gives the operands, see
_integer_ufunc_dtype, and
+ returns a pandas UDF that runs the ufunc on the operands cast to that
dtype. So the result has
+ that dtype, as in pandas, and stays exact above 2**53.
+ """
+
+ def build(dtype: np.dtype) -> Callable[..., Column]:
+ # The nullable counterpart of the dtype, such as Int64, keeps nulls.
+ nullable_dtype = dtype.name.capitalize()
+ return pandas_udf( # type: ignore[call-overload]
+ lambda s1, s2: ufunc(s1.astype(nullable_dtype),
s2.astype(nullable_dtype)),
+ as_spark_type(dtype),
+ )
Review Comment:
With the default
`spark.sql.execution.pythonUDF.pandas.preferIntExtensionDtype=false`, a
null-bearing long batch reaches this UDF as float64, so casting to `Int64`
cannot recover large integers that have already rounded. For operands `[2**53 +
1, None]` and `[3, 3]` in one batch, `gcd` returns 1 instead of 3 for the first
row, and `lcm` is also incorrect. Replace nulls with typed integer placeholders
before the UDF, restore the original null mask afterward, and add a regression
combining a large integer and a null in the same batch.
--
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]