HyukjinKwon opened a new pull request, #404:
URL: https://github.com/apache/spark-connect-rust/pull/404

   ### What changes were proposed in this pull request?
   
   [SPARK-61222](https://issues.apache.org/jira/browse/SPARK-61222): Raise 
`pickle.PicklingError` ("Could not serialize object: ...") for a function or 
data source that does not pickle in these methods, as PySpark does.
   
   The Rust client pickled these commands with `pyspark.cloudpickle.dumps` 
directly, which raises the bare `TypeError` of cloudpickle. It now pickles them 
with `pyspark.serializers.CloudPickleSerializer().dumps`, which PySpark uses 
for the Python function of these calls (`pyspark/sql/connect/expressions.py`) 
and for the data source class (`pyspark/sql/connect/plan.py`). The bytes of a 
pickled object are unchanged; only the error for an object that does not pickle 
changes, to the class and message PySpark gives. When the error is raised does 
not change.
   
   ### Why are the changes needed?
   
   A function that does not pickle, such as a closure holding 
`threading.Lock()`, raised `TypeError: cannot pickle '_thread.lock' object` 
from these calls. Official PySpark 4.3.0 raises `pickle.PicklingError: Could 
not serialize object: TypeError: cannot pickle '_thread.lock' object`, so 
`except pickle.PicklingError` did not catch it here. Scalar and pandas UDFs, 
UDTFs and streaming `foreach` already raised `PicklingError`.
   
   For example, `df.groupBy("id").applyInPandas(lambda pdf: (lock, pdf)[1], "id 
long")` with a lock `lock` raised a `TypeError` in this client, and a 
`PicklingError` in PySpark.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. A function or data source that does not pickle now raises 
`pickle.PicklingError` with PySpark's message, instead of a plain `TypeError`, 
from applyInPandas, applyInArrow, mapInPandas, mapInArrow, cogroup, 
transformWithState, transformWithStateInPandas and `spark.dataSource.register`.
   
   ### How was this patch tested?
   
   - Added `test_unpicklable_functions_raise_picklingerror_as_pyspark` to 
`test_dropin_offline.py`. It checks that each of the nine calls raises exactly 
`pickle.PicklingError` whose message starts with "Could not serialize object: " 
and contains "cannot pickle". The test fails without this change (`TypeError: 
cannot pickle '_thread.lock' object`) and passes with it. The same classes and 
messages were checked against official PySpark 4.3.0 on a live Connect server 
for transformWithStateInPandas, transformWithState, the data source 
registration and applyInPandas. Official raises for the map and cogroup 
functions at the action, not at the call; this PR does not change that timing.
   - The lib tests of both crates (658 passed), the build of all tests, also 
with `wasm-udf`, the offline Python suites (2704 passed, 8 skipped), and the 
live drop-in and Arrow suites (224 passed) and live cases (12 passed) against a 
local Spark 4.3.0 server. Clippy reports no diagnostics in the changed lines.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Opus 5.5
   
   This pull request and its description were written by Isaac.
   


-- 
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