Skip to content

[SPARK-52593][PS] Avoid CAST_INVALID_INPUT of Series.dot and DataFrame.dot in ANSI mode #51310

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions python/pyspark/pandas/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
from pyspark.pandas.plot import PandasOnSparkPlotAccessor
from pyspark.pandas.utils import (
combine_frames,
is_ansi_mode_enabled,
is_name_like_tuple,
is_name_like_value,
name_like_string,
Expand Down Expand Up @@ -5660,11 +5661,21 @@ def dot(self, other: Union["Series", DataFrame]) -> Union[Scalar, "Series"]:
y -14
dtype: int64
"""
spark_session = self._internal.spark_frame.sparkSession
if not same_anchor(self, other):
if get_option("compute.eager_check") and not cast(
ps.Index, self.index.sort_values()
).equals(cast(ps.Index, other.index.sort_values())):
raise ValueError("matrices are not aligned")
if get_option("compute.eager_check"):
if is_ansi_mode_enabled(spark_session):
# In ANSI, "equals" leads to implicit casting which may cause CAST_INVALID_INPUT
# Instead, we compare raw index objects collected to the driver
if sorted(ps.Index(self.index).tolist()) != sorted(
ps.Index(other.index).tolist()
):
Comment on lines +5670 to +5672
Copy link
Member

Choose a reason for hiding this comment

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

I guess this means index.sort_values() has CAST_INVALID_INPUT issue?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's equals

>>> psidx1 = ps.Index(['x', 'y', 'z'])                     
>>> psidx2 = ps.Index([1, 2, 3])
>>> psidx1.sort_values().equals(psidx2.sort_values())
Traceback (most recent call last):
...
pyspark.errors.exceptions.captured.NumberFormatException: [CAST_INVALID_INPUT] The value 'x' of the type "STRING" cannot be cast to "BIGINT" because it is malformed. Correct the value as per the syntax, or change its target type. Use `try_cast` to tolerate malformed input and return NULL instead. SQLSTATE: 22018
== DataFrame ==
"__eq__" was called from
<stdin>:1

Copy link
Member Author

Choose a reason for hiding this comment

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

I know this collects the index to the driver which is not ideal, but I haven’t found an alternative yet

Copy link
Member

Choose a reason for hiding this comment

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

Let's fix equals, then. We should fix it anyway.

Copy link
Member

Choose a reason for hiding this comment

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

The other comparisons, as well? like !=, <, <=, >, and >=?

raise ValueError("matrices are not aligned")
else:
if not cast(ps.Index, self.index.sort_values()).equals(
cast(ps.Index, other.index.sort_values())
):
raise ValueError("matrices are not aligned")
elif len(self.index) != len(other.index):
raise ValueError("matrices are not aligned")

Expand Down
2 changes: 0 additions & 2 deletions python/pyspark/pandas/tests/diff_frames_ops/test_dot_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from pyspark.pandas.config import set_option, reset_option
from pyspark.testing.pandasutils import PandasOnSparkTestCase
from pyspark.testing.sqlutils import SQLTestUtils
from pyspark.testing.utils import is_ansi_mode_test, ansi_mode_not_supported_message


class DiffFramesDotFrameMixin:
Expand All @@ -35,7 +34,6 @@ def tearDownClass(cls):
reset_option("compute.ops_on_diff_frames")
super().tearDownClass()

@unittest.skipIf(is_ansi_mode_test, ansi_mode_not_supported_message)
def test_frame_dot(self):
pdf = pd.DataFrame([[0, 1, -2, -1], [1, 1, 1, 1]])
psdf = ps.from_pandas(pdf)
Expand Down
1 change: 0 additions & 1 deletion python/pyspark/pandas/tests/series/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,6 @@ def test_items(self):
self.assert_eq(p_name, k_name)
self.assert_eq(p_items, k_items)

@unittest.skipIf(is_ansi_mode_test, ansi_mode_not_supported_message)
def test_dot(self):
pdf = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
psdf = ps.from_pandas(pdf)
Expand Down