spark-test commented on code in PR #60685:
URL: https://github.com/apache/spark/pull/60685#discussion_r4624463180
##########
python/pyspark/sql/tests/test_udf_transpile_unit.py:
##########
@@ -2529,6 +2529,47 @@ def test_udf_transpile_string_len(self):
self._raises(strlen, "a string", [(None,)], needle="len()")
self._raises(strlen, "a long", [(5,)], needle="")
+ def test_udf_transpile_string_methods(self):
+ # SPARK-59816: upper/lower/startswith/endswith lower to Catalyst and
match
+ # Python, including full Unicode case mappings (sharp s, final sigma).
A NULL
+ # receiver or argument raises like CPython, a proven-non-null branch
skips
+ # the guard, and a non-string column or a start index falls back to
Python.
+ S, B = StringType(), BooleanType()
+ up = lambda s: s.upper() # noqa: E731
+ low = lambda s: s.lower() # noqa: E731
+ starts = lambda s, p: s.startswith(p) # noqa: E731
+ ends = lambda s: s.endswith("ab") # noqa: E731
+ branch = lambda s: "y" if s.startswith("a") else "n" # noqa: E731
+ negated = lambda s: not s.startswith("a") # noqa: E731
+ both = lambda s: s.startswith("a") and s.endswith("c") # noqa: E731
+ up_guarded = lambda s: s.upper() if s is not None else None # noqa:
E731
+ starts_at = lambda s: s.startswith("a", 1) # noqa: E731
+ words = [("",), ("abc",), ("straße",), ("ΟΔΟΣ",)]
+ pairs = [("abc", "ab"), ("abc", "b"), ("abc", ""), ("", "a")]
+ for func, rt, schema, rows in [
+ (up, S, "s string", words),
+ (low, S, "s string", words),
+ (starts, B, "s string, p string", pairs),
+ (ends, B, "s string", [("cab",), ("abc",), ("",)]),
+ (branch, S, "s string", [("ab",), ("ba",)]),
+ (negated, B, "s string", [("ab",), ("ba",)]),
+ (both, B, "s string", [("abc",), ("ab",), ("bc",)]),
+ ]:
+ with self.subTest(func=func):
+ expected = [func(*r) for r in rows]
+ self.assertEqual(self._vals(func, rt, schema, rows), expected)
+ self.assertEqual(self._vals(up_guarded, S, "s string", [("a",),
(None,)]), ["A", None])
+ self._raises(up, "s string", [(None,)], needle="str.upper()",
return_type=S)
+ self._raises(
+ starts, "s string, p string", [("abc", None)],
needle="str.startswith()", return_type=B
+ )
+ self._raises(up, "s long", [(5,)], needle="", return_type=S)
Review Comment:
**Nit (P3):** This case covers the non-string fallback only for the
receiver. Every `startswith` and `endswith` case in this test binds a string
column or literal as the argument, so nothing fails if the operand check at
python/pyspark/sql/transpile.py:664 stops validating the argument. A JVM-free
run of the transpiler (Column builders stubbed), with the `_is_nonnull_node`
fix applied and that check narrowed to the receiver, emitted `(string,
integer)` and `(string, float)` options for `s.startswith(p)` and options for
`(s: str, p: int)` and `s.endswith(1)`, none of which the current check emits.
From the code, the JVM keeps an `integer` option for any integral column
(sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveTranspiledPythonUDFOptions.scala:108),
and ANSI coercion casts a bigint argument to string for `StartsWith`, so
`s.startswith(p)` over `(string, bigint)` columns would return a boolean where
Python raises TypeError.
A case such as `self._raises(starts, "s string, p long", [("1abc", 1)],
needle="", return_type=B)`, plus a `_fallback_reason` check inside the
`sql_conf(_TRANSPILE_ON)` block below for a named `ends_int = lambda s:
s.endswith(1)`, would catch that. The bigint case needs the string-method
lowering to produce its string option, so it lands with or after the fix for
the missing `_is_nonnull_node` helper.
**Verification:**
- **Regression:** With transpilation and ANSI on, startswith over a string
receiver column and a bigint argument column raises as interpreted Python does
instead of returning a boolean, and endswith with an integer literal argument
produces no transpiled option; both cases fail if the operand check validates
only the receiver.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]