Skip to content

[SPARK-52570][PS] Enable divide-by-zero for numeric rmod with ANSI enabled #51275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from

Conversation

xinrong-meng
Copy link
Member

@xinrong-meng xinrong-meng commented Jun 24, 2025

What changes were proposed in this pull request?

Enable divide-by-zero for numeric rmod with ANSI enabled

Why are the changes needed?

Part of https://issues.apache.org/jira/browse/SPARK-52169.

Does this PR introduce any user-facing change?

Yes.

>>> ps.set_option("compute.fail_on_ansi_mode", False)
>>> ps.set_option("compute.ansi_mode_support", True)

>>> pdf = pd.DataFrame({"a": [0], "b": [False]})
>>> pdf.dtypes
a    int64
b     bool
dtype: object
>>> psdf = ps.from_pandas(pdf)
>>> 1 % psdf["a"]
0   NaN
Name: a, dtype: float64
>>> 1 % psdf["b"]
0   NaN
Name: b, dtype: float64

How was this patch tested?

Unit tests.

(dev3.11) spark (bool_mod_new) % SPARK_ANSI_SQL_MODE=true  ./python/run-tests --python-executables=python3.11 --testnames "pyspark.pandas.tests.data_type_ops.test_boolean_ops 
...
Tests passed in 4 seconds

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

No.

spark_session = left._internal.spark_frame.sparkSession

def safe_mod(left_col: PySparkColumn, right_val: Any) -> PySparkColumn:
if is_ansi_mode_enabled(spark_session):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious why don't we need to check ansi_mode here anymore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bools are considered numeric and will take num_ops logic if that makes sense

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merged to unblock prs, please let me know if you have further concerns :)

@xinrong-meng
Copy link
Member Author

Merged to master, thanks!

@@ -225,11 +225,12 @@ def test_binary_operator_floordiv(self):

def test_binary_operator_mod(self):
# Positive
pdf = pd.DataFrame({"a": [3], "b": [2]})
pdf = pd.DataFrame({"a": [3], "b": [2], "c": [0]})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this change broke the non ANSI mode:


======================================================================
ERROR [4.586s]: test_binary_operator_mod (pyspark.pandas.tests.computation.test_binary_ops.FrameBinaryOpsTests.test_binary_operator_mod)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/__w/spark/spark/python/pyspark/pandas/tests/computation/test_binary_ops.py", line 233, in test_binary_operator_mod
    self.assert_eq(1 % psdf["c"], 1 % pdf["c"])
                   ~~^~~~~~~~~~~
  File "/__w/spark/spark/python/pyspark/pandas/base.py", line 386, in __rmod__
    return self._dtype_op.rmod(self, other)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/__w/spark/spark/python/pyspark/pandas/data_type_ops/num_ops.py", line 177, in rmod
    return column_op(safe_rmod)(left, right)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/__w/spark/spark/python/pyspark/pandas/base.py", line 222, in wrapper
    scol = f(
           ^^
  File "/__w/spark/spark/python/pyspark/pandas/data_type_ops/num_ops.py", line 175, in safe_rmod
    return ((right % left) + left) % left
             ~~~~~~^~~~~~
  File "/__w/spark/spark/python/pyspark/pandas/base.py", line 386, in __rmod__
    return self._dtype_op.rmod(self, other)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/__w/spark/spark/python/pyspark/pandas/data_type_ops/num_ops.py", line 177, in rmod
    return column_op(safe_rmod)(left, right)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/__w/spark/spark/python/pyspark/pandas/base.py", line 222, in wrapper
    scol = f(
           ^^
  File "/__w/spark/spark/python/pyspark/pandas/data_type_ops/num_ops.py", line 175, in safe_rmod
    return ((right % left) + left) % left
             ~~~~~~^~~~~~
  File "/__w/spark/spark/python/pyspark/pandas/base.py", line 386, in __rmod__
    return self._dtype_op.rmod(self, other)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/__w/spark/spark/python/pyspark/pandas/data_type_ops/num_ops.py", line 177, in rmod
    return column_op(safe_rmod)(left, right)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/__w/spark/spark/python/pyspark/pandas/base.py", line 222, in wrapper
    scol = f(
           ^^

https://github.com/apache/spark/actions/runs/15987607479/job/45094971366

Let me revert it for now.

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

Successfully merging this pull request may close these issues.

4 participants