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

Conversation

xinrong-meng
Copy link
Member

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

What changes were proposed in this pull request?

Avoid CAST_INVALID_INPUT of Series.dot and DataFrame.dot in ANSI mode

Why are the changes needed?

Ensure pandas on Spark works well with ANSI mode on.
Part of https://issues.apache.org/jira/browse/SPARK-52556.

Does this PR introduce any user-facing change?

Yes. Series.dot raises expected error in ANSI, for example

>>> pdf = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
>>> psdf = ps.from_pandas(pdf)
>>> psdf_other = ps.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}, index=["x", "y", "z"])
>>> ps.set_option("compute.ops_on_diff_frames", True)

FROM

>>> psdf["b"].dot(psdf_other)
...
25/06/27 15:08:57 ERROR Executor: Exception in task 0.0 in stage 14.0 (TID 98)
org.apache.spark.SparkNumberFormatException: [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
...

TO

>>> psdf["b"].dot(psdf_other)
Traceback (most recent call last):
...
ValueError: matrices are not aligned
>>> 

SAME AS ANSI OFF

>>> spark.conf.set("spark.sql.ansi.enabled", False)
>>> psdf["b"].dot(psdf_other)
Traceback (most recent call last):
...
ValueError: matrices are not aligned

How was this patch tested?

Unit tests

(dev3.11) spark (series_dot) % SPARK_ANSI_SQL_MODE=true  ./python/run-tests --python-executables=python3.11 --testnames "pyspark.pandas.tests.series.test_series SeriesTests.test_dot"
...
Finished test(python3.11): pyspark.pandas.tests.series.test_series SeriesTests.test_dot (6s)
Tests passed in 6 seconds

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

No

@xinrong-meng
Copy link
Member Author

@ueshin may I get a review please?

@xinrong-meng xinrong-meng changed the title [SPARK-52593][PS] Avoid CAST_INVALID_INPUT of Series.dot in ANSI mode [SPARK-52593][PS] Avoid CAST_INVALID_INPUT of Series.dot and "DataFrame.dot" in ANSI mode Jun 30, 2025
@xinrong-meng xinrong-meng changed the title [SPARK-52593][PS] Avoid CAST_INVALID_INPUT of Series.dot and "DataFrame.dot" in ANSI mode [SPARK-52593][PS] Avoid CAST_INVALID_INPUT of Series.dot and DataFrame.dot in ANSI mode Jun 30, 2025
Comment on lines +5670 to +5672
if sorted(ps.Index(self.index).tolist()) != sorted(
ps.Index(other.index).tolist()
):
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 >=?

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.

2 participants