[SPARK-58646][PS][FOLLOWUP] Fix NumPy reciprocal parity for decimal, boolean, and narrower integer columns - #58218
Open
Yicong-Huang wants to merge 1 commit into
Open
Conversation
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?
This is a follow-up of #57856 (SPARK-58646), which replaced the scalar pandas UDF fallback of
np.reciprocalwith native Spark expressions for non-floating-point inputs.That change routed every non-
float/doubledtype through a single integer branch that hard-codes the int64 minimum as the divide-by-zero sentinel and casts the quotient throughlong. This does not match the previous pandas UDF (np.reciprocalapplied to the pandasSeries) for decimals, booleans, and narrower integers:np.reciprocal-> Double)0-9.2e18(int64 min)-9.2e18(unchanged)0-2147483648(int32 min)-9.2e1800(numpy1 // 0does not overflow on narrow widths)-9.2e18False0.0(numpy promotes bool to int8:True -> 1,False -> 0)-9.2e182.50.4(numpy takes a true floating reciprocal)0.0(truncated to long)This PR restores parity:
typeofstarts withdecimal), since numpy computes a true reciprocal for them. A decimal0(which the old UDF could not handle --np.reciprocal(Decimal('0'))raisesDivisionByZero) now maps toinf, consistent with the floating-point branch.int, int64 minimum forbigint, and0for the narrower widths (tinyint,smallint, andbooleanpromoted to int8) -- and casts throughlongso boolean and narrower integers can take part in the division.int64 columns, the only case exercised by the original PR, are unchanged.
Why are the changes needed?
The merged native expression regressed the observable pandas-on-Spark behavior for decimal, boolean, and narrower-integer columns relative to the pandas UDF it replaced. This restores parity so that
np.reciprocalproduces the same results as before across all supported dtypes.Does this PR introduce any user-facing change?
No. #57856 is unreleased (master only), so this only fixes an unreleased regression before it ships; there is no change relative to any released Spark version.
How was this patch tested?
Added
test_np_reciprocal_non_default_dtypesinpython/pyspark/pandas/tests/test_numpy_compat.py, inherited by the Spark Connect parity suite, assertingnp.reciprocal(psser)equalsnp.reciprocal(pdf)for int8/int16/int32, boolean, and decimal columns (covering positive, negative, and the per-width zero-overflow sentinel).Was this patch authored or co-authored using generative AI tooling?
No.