CODERNSINGH opened a new pull request, #60222:
URL: https://github.com/apache/spark/pull/60222

   ### What changes were proposed in this pull request?
   
   In classic PySpark, `Column.startswith` and `Column.endswith` now convert a 
non-`Column` argument to a literal `Column` before calling the JVM, so the 
`Column` overload of `startsWith`/`endsWith` is always used.
   
   ### Why are the changes needed?
   
   `df.s.startswith(None)` / `df.s.endswith(None)` intermittently fail with:
   
   ```
   py4j.protocol.Py4JJavaError: An error occurred while calling o51.startsWith.
   : java.lang.NullPointerException: Cannot invoke 
"org.apache.spark.sql.Column.expr()" because "x$1" is null
   ```
   
   `None` is passed to the JVM as `null`, and `Column.startsWith`/`endsWith` 
are overloaded for `Column` and `String`. Py4J cannot tell the overloads apart 
for a `null` argument, and the one it picks varies between JVM runs. When it 
picks the `Column` overload, `Column.fn` dereferences the null. When it picks 
the `String` overload, `lit(null)` is used and the result is `NULL`.
   
   On master, running `df.s.startswith(None)` and `df.s.endswith(None)` in 6 
fresh sessions failed in 5 of them, on different methods each time. Spark 
Connect is not affected because it creates `lit(None)` on the client.
   
   The `String` overload is implemented as `startsWith(lit(literal))`, so 
always passing `lit(...)` keeps the existing results for non-null literals.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. `Column.startswith(None)` and `Column.endswith(None)` in classic 
PySpark now consistently return `NULL` (same as Spark Connect and as 
`contains(None)`), instead of sometimes failing with a `NullPointerException`.
   
   ### How was this patch tested?
   
   Added `test_startswith_endswith_with_none` to `ColumnTestsMixin`, so it runs 
for both classic (`test_column`) and Spark Connect (`test_parity_column`).
   
   Also checked manually by repeating the following in 6 fresh sessions: before 
this change 5 of 6 failed, after it all return `[None, None]`.
   
   ```python
   df = spark.createDataFrame([("abc",), (None,)], "s string")
   df.select(df.s.startswith(None)).collect()
   df.select(df.s.endswith(None)).collect()
   ```
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.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: [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]

Reply via email to