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

   ### What changes were proposed in this pull request?
   
   cloudpickle pickles the drop-in's UDF and UDTF objects by reference to their 
classes. Those classes were `pyspark.sql.udf.UserDefinedFunction` and 
`pyspark.sql.udtf.UserDefinedTableFunction`, and on the server's Python worker, 
which has PySpark 4.3.0 and no drop-in, those names are PySpark's classic 
classes. This PR:
   
   - gives `UserDefinedFunction` and `UserDefinedTableFunction` the 
`__module__` of `pyspark.sql.connect.udf` and `pyspark.sql.connect.udtf`, as 
PySpark Connect's classes have, and imports those modules where the objects are 
built, so that cloudpickle finds the classes by those names;
   - stores the UDTF's name as `_name`, the attribute that PySpark Connect's 
`UserDefinedTableFunction` reads, and keeps `name` readable as a property;
   - keeps the classic names, `pyspark.sql.udf.UserDefinedFunction` and 
`pyspark.sql.udtf.UserDefinedTableFunction`, as aliases of the same classes.
   
   The instance state now matches PySpark Connect's: `_name`, `_returnType`, 
`_returnType_placeholder`, `deterministic`, `evalType` and `func` for a UDF, 
and `_name`, `deterministic`, `evalType`, `func` and `returnType` for a UDTF. 
The UDTF also keeps the drop-in's `python_ver`.
   
   ### Why are the changes needed?
   
   A foreachBatch function that calls a UDF defined outside it failed in the 
drop-in and works in PySpark 4.3.0:
   
   ```python
   my_udf = F.udf(lambda x: int(x) + 1, IntegerType())
   
   def func(batch_df, batch_id):
       batch_df.select(my_udf("value")).collect()
   
   df.writeStream.foreachBatch(func).start()
   ```
   
   - Before: `[SESSION_OR_CONTEXT_NOT_EXISTS] SparkContext or SparkSession 
should be created first.`, raised on the server's worker, which rebuilt the 
classic class, whose `__call__` needs a SparkContext. The same happened for a 
pandas UDF and for the function that `spark.udf.register` returns. A UDTF 
called there (`TU(F.lit(3)).collect()`) raised `JVM_ATTRIBUTE_NOT_SUPPORTED` 
for `sparkContext`.
   - After: they all run as in PySpark 4.3.0, and the UDTF gives `[Row(x=3)]`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes:
   - A UDF or UDTF defined outside a foreachBatch function can be called inside 
it.
   - `type(...)` of the object of a UDF and of a UDTF is 
`pyspark.sql.connect.udf.UserDefinedFunction` and 
`pyspark.sql.connect.udtf.UserDefinedTableFunction`, as in PySpark 4.3.0. The 
classic import names still work.
   
   ### How was this patch tested?
   
   - New tests, each checked to fail without the fix:
     - offline: `test_udf_and_udtf_pickle_as_pyspark_connect_classes` checks 
the class modules, the instance attributes and the classes that the pickles 
name, and that an unpickled copy can still be called;
     - e2e against Spark 4.3.0: 
`test_udfs_captured_by_foreach_batch_functions_run_as_pyspark`, a foreachBatch 
function that calls a captured UDF, pandas UDF, registered UDF and UDTF.
   - The same captures in `mapInPandas`, `applyInPandas` and `foreach` 
functions match the reference.
   - Also passing, with `TZ=UTC`:
     - CI's offline Python set: 3689 passed;
     - `test_dropin_e2e.py`: 280 passed;
     - the Rust lib tests and `--no-run` builds;
     - `cargo fmt`, the stub audit and ruff.
   
   ### 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