Commit 31f58da
committed
[SPARK-58249][PS] Use native functions for NumPy math ufuncs
### What changes were proposed in this pull request?
Replace the scalar pandas UDF mappings or column expressions for NumPy `cosh`, `deg2rad`, `exp2`, `fabs`, `negative`, `positive`, `rad2deg`, `sign`, `sinh`, `square`, and `tanh` on pandas-on-Spark objects with native Spark SQL functions or expressions.
`np.square` casts its input to `double` before multiplication, avoiding fixed-width integer overflow under ANSI mode. The end-to-end pandas-on-Spark coverage compares the mapped ufunc results with pandas; it includes the `int64` minimum value for `fabs` and `NaN` and signed-zero values for `sign`.
### Why are the changes needed?
These mappings originated before the corresponding native Spark SQL functions and expressions were available. Current Spark provides native equivalents, including Spark Connect support, so this removes the Python worker boundary while preserving NumPy-compatible results.
### Does this PR introduce _any_ user-facing change?
Yes. `np.square` on overflowing integer values now produces a double-valued result instead of NumPy fixed-width integer overflow. The other mapped ufuncs preserve their existing NumPy-compatible results.
### How was this patch tested?
- Added end-to-end pandas-on-Spark coverage for the mapped ufuncs.
- Ran `build/sbt -Phive package`.
- Ran `SPARK_TESTING=1 SPARK_PREPEND_CLASSES=1 PYSPARK_PYTHON=.venv/bin/python PYSPARK_DRIVER_PYTHON=.venv/bin/python bin/pyspark pyspark.pandas.tests.test_numpy_compat`.
- Ran `git diff --check`.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Codex (GPT-5)
Closes #57417 from zhengruifeng/pandas-native-math-ufuncs-dev2.
Authored-by: Ruifeng Zheng <ruifengz@apache.org>
Signed-off-by: Ruifeng Zheng <ruifengz@apache.org>1 parent c1d8707 commit 31f58da
2 files changed
Lines changed: 23 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
43 | | - | |
| 42 | + | |
| 43 | + | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
| 46 | + | |
47 | 47 | | |
48 | | - | |
| 48 | + | |
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | | - | |
61 | 60 | | |
62 | 61 | | |
63 | | - | |
64 | | - | |
65 | | - | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
66 | 65 | | |
67 | 66 | | |
68 | 67 | | |
69 | 68 | | |
70 | 69 | | |
71 | | - | |
| 70 | + | |
72 | 71 | | |
73 | 72 | | |
74 | | - | |
| 73 | + | |
75 | 74 | | |
76 | 75 | | |
77 | | - | |
| 76 | + | |
78 | 77 | | |
79 | | - | |
| 78 | + | |
80 | 79 | | |
81 | 80 | | |
82 | 81 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
88 | | - | |
| 88 | + | |
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
93 | 104 | | |
94 | 105 | | |
95 | 106 | | |
| |||
0 commit comments