Skip to content

[SPARK-58630][PS] Use native Spark expressions for NumPy copysign - #57836

Open
Yicong-Huang wants to merge 2 commits into
apache:masterfrom
Yicong-Huang:native-numpy-copysign
Open

[SPARK-58630][PS] Use native Spark expressions for NumPy copysign#57836
Yicong-Huang wants to merge 2 commits into
apache:masterfrom
Yicong-Huang:native-numpy-copysign

Conversation

@Yicong-Huang

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Replace the numpy.copysign pandas UDF mapping in python/pyspark/pandas/numpy_compat.py with a native Spark SQL expression (umbrella: SPARK-58532).

copysign(x, y) returns |x| with the sign of y. The sign is taken from y's IEEE-754 sign bit: -0.0 counts as negative (detected via a string cast, as the existing reciprocal mapping does) rather than via y < 0, which misses -0.0. NaN falls through to a positive sign. Null handling is type-aware: a float y column carries missing values as NaN (surfaced as NULL), where copysign(x, NaN) is |x|; an integer y column's NULL is genuine and propagates.

Why are the changes needed?

Native expressions avoid pandas UDF serialization/worker overhead and work uniformly on classic and Spark Connect.

Does this PR introduce any user-facing change?

No.

How was this patch tested?

Added test_np_copysign and test_np_copysign_signed_zero (also run via the Spark Connect parity suite), covering integer/float/nullable-integer inputs and asserting np.signbit for the signed-zero, NaN, null, and +-inf edge cases.

Was this patch authored or co-authored using generative AI tooling?

No.

@uros-b

uros-b commented Aug 7, 2026

Copy link
Copy Markdown
Member

Thank you @Yicong-Huang!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants