You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "zhengruifeng (via GitHub)" <gi...@apache.org> on 2026/08/05 07:06:48 UTC
[PR] [SPARK-58582][PS] Use native Spark function for NumPy fmod [spark]
zhengruifeng opened a new pull request, #57784:
URL: https://github.com/apache/spark/pull/57784
### What changes were proposed in this pull request?
Replaces the pandas UDF used for `np.fmod` in the pandas API on Spark with Spark’s native remainder expression.
Adds coverage for integral and floating inputs, including zero divisors, infinities, and NaN.
### Why are the changes needed?
Using native Spark expressions avoids pandas UDF serialization and lets Spark optimize and code-generate this operation while preserving NumPy-compatible behavior.
### Does this PR introduce _any_ user-facing change?
No. This is a behavior-preserving implementation change.
### How was this patch tested?
`source ~/.zshrc && conda run -n spark-dev-313 python/run-tests --testnames pyspark.pandas.tests.test_numpy_compat.NumPyCompatTests.test_np_fmod`
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Codex (GPT-5)
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58582][PS] Use native Spark function for NumPy fmod [spark]
Posted by "uros-b (via GitHub)" <gi...@apache.org>.
uros-b commented on code in PR #57784:
URL: https://github.com/apache/spark/pull/57784#discussion_r3719221601
##########
python/pyspark/pandas/numpy_compat.py:
##########
@@ -116,7 +116,18 @@
.otherwise(F.greatest(c1, c2))
.cast("double"),
"fmin": lambda c1, c2: F.least(c1, c2).cast("double"),
- "fmod": pandas_udf(lambda s1, s2: np.fmod(s1, s2), DoubleType()), # type: ignore[call-overload]
+ "fmod": lambda c1, c2: F.when(
Review Comment:
When c2 is NULL and c1 is non-null, the outer when(c1.isNull() | c2.isNull(), c1.cast("double")) returns the dividend's value rather than NULL. Under the old pandas-udf, Spark NULL was converted to pandas NaN before calling np.fmod, so np.fmod(x, NaN) = NaN; the null-divisor propagated as NaN. The new code returns c1 unchanged for a null divisor. In practice, float columns in pandas-on-Spark use NaN (not NULL) for missing values, so the practically-affected population is nullable-integer columns with Python None entries (e.g. pd.array([1, 2, None], dtype="Int64")); those are fully supported and the divergence is reachable. Fix: the outer NULL clause is superfluous — (c1 % c2) already propagates NULL through Spark's built-in null semantics in both positions; removing the outer when clause entirely is cleaner and fixes the regression.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58582][PS] Use native Spark function for NumPy fmod [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #57784:
URL: https://github.com/apache/spark/pull/57784#discussion_r3719517399
##########
python/pyspark/pandas/numpy_compat.py:
##########
@@ -116,7 +116,18 @@
.otherwise(F.greatest(c1, c2))
.cast("double"),
"fmin": lambda c1, c2: F.least(c1, c2).cast("double"),
- "fmod": pandas_udf(lambda s1, s2: np.fmod(s1, s2), DoubleType()), # type: ignore[call-overload]
+ "fmod": lambda c1, c2: F.when(
Review Comment:
Addressed in ebdec053470. Removed the incorrect null branch; the zero-divisor case now also requires a non-null dividend, so native remainder propagates nulls. Added nullable Int64 coverage, and the focused test passes.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58582][PS] Use native Spark function for NumPy fmod [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on PR #57784:
URL: https://github.com/apache/spark/pull/57784#issuecomment-5204010447
**Merge Summary:**
- merged into master https://github.com/apache/spark/commit/6ef2cdb955a5d0d586c0792b01819cf7f1b5ad64
- merged into branch-4.x https://github.com/apache/spark/commit/e279c3d4d3d7d14897547fd68c5a3ca74bdb7709
*Posted by `merge_spark_pr.py`*
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58582][PS] Use native Spark function for NumPy fmod [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng closed pull request #57784: [SPARK-58582][PS] Use native Spark function for NumPy fmod
URL: https://github.com/apache/spark/pull/57784
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org