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

   ### What changes were proposed in this pull request?
   
   PySpark's `SparkConnectClient.register_udtf` (`core.py:1117-1149`) works in 
this order:
   1. it makes a `PythonUDTF`, which reads the `__name__` of the handler class 
(`plan.py:3062`);
   2. its `udtf_plan` sets the name in the string field of its command 
(`plan.py:3138`);
   3. `PythonUDTF.to_plan` converts the return type with 
`pyspark_types_to_proto_types` (`plan.py:3075-3076`);
   4. only then does it pickle the handler (`plan.py:3078-3088`).
   
   The drop-in's `spark.client.register_udtf` (#779) pickled the handler before 
`_registerPythonUdtf` converted the name. Its own check of the return type 
(#815) came before the name. So it raised PySpark's errors in another order. 
`spark.udtf.register` uses PySpark's order since #819.
   
   Checked on both clients against a Spark 4.3.0 server, with a handler class 
that closes over a `threading.Lock`, which cloudpickle cannot pickle:
   
   | `client.register_udtf(...)` | pyspark-client 4.3.0 | drop-in before |
   |---|---|---|
   | `(Unpicklable, "x int", 5)` | `TypeError: bad argument type for built-in 
operation` | `UDTF_SERIALIZATION_ERROR` |
   | `(Unpicklable, "x int", "\udc80")` | `UnicodeEncodeError` | 
`UDTF_SERIALIZATION_ERROR` |
   | `(Unpicklable, StructField("x", IntegerType()), "t")` | 
`UNSUPPORTED_OPERATION` of `data type StructField('x', IntegerType(), True)` | 
`UDTF_SERIALIZATION_ERROR` |
   | `(Unpicklable, "x \udc80", "t")` | `UnicodeEncodeError` | 
`UDTF_SERIALIZATION_ERROR` |
   | `(Unpicklable, 5, 5)` | the `TypeError` of the name | 
`UNSUPPORTED_OPERATION` of `data type 5` |
   | `(object(), 5, 5)` | `AttributeError: 'object' object has no attribute 
'__name__'` | `UNSUPPORTED_OPERATION` of `data type 5` |
   | `(Unpicklable, 5, "t")` | `UNSUPPORTED_OPERATION` of `data type 5` | the 
same |
   | `(Unpicklable, "x int", "t")` | `UDTF_SERIALIZATION_ERROR` | the same |
   
   The change, in `python/pyspark/sql/connect/client/__init__.py`:
   - Since #819, `_registerPythonUdtf` takes a function of no arguments that 
pickles the handler. It converts the name and then the return type, and only 
then calls that function.
   - `register_udtf` now reads the `__name__` of the handler class first, as 
`PythonUDTF` does.
   - It then passes `functools.partial` of the pickling of 
`spark.udtf.register` (`pyspark.sql.udtf._pickled_handler`), which raises 
PySpark's `UDTF_SERIALIZATION_ERROR` of the name of the class.
   - Its own check of the return type is removed: `_registerPythonUdtf` raises 
the same `UNSUPPORTED_OPERATION`, after the name.
   - `register_udtf` is the only method of the client that changes; its 
`__init__`, `newSession` and `clone` are untouched.
   
   `docs/compatibility.md` says that `spark.client.register_udtf` sets the name 
and converts the return type first, as `spark.udtf.register` does.
   
   ### Why are the changes needed?
   
   So that `spark.client.register_udtf` raises PySpark's errors in PySpark's 
order, as `spark.udtf.register` does since #819.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. Of a handler that does not pickle, `spark.client.register_udtf` raises 
the error of the handler's `__name__`, then of the name, then of the return 
type, as PySpark does, before `UDTF_SERIALIZATION_ERROR`. Its other results are 
unchanged.
   
   ### How was this patch tested?
   
   - **Probes of both clients against a Spark 4.3.0 server:** the eight calls 
above give identical errors after this change.
   - **New test:** `test_client_registers_a_udtf_in_the_order_of_pyspark` in 
`test_dropin_offline.py`, on the mock server. It checks each of these errors of 
an unpicklable handler, with a bad name and with a bad return type, and that 
nothing is sent. It fails without the change and passes with it.
   - The existing tests of the registrations of the client 
(`test_client_raises_the_argument_errors_of_pyspark`, 
`test_client_registers_functions_and_artifacts_as_pyspark`) pass.
   - `cargo test -p pyspark-rs --lib`: 13 passed.
   - The offline suite of `coverage.yml`: 6259 passed, 77 skipped.
   - `test_dropin_e2e.py` against a Spark 4.3.0 server: 310 passed.
   - **Lint and guards:**
     - `cargo fmt --all --check`;
     - `cargo +1.100.0 clippy -p apache-spark-connect-core -p 
apache-spark-connect -p pyspark-rs --all-targets -- -D warnings`;
     - `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps -p apache-spark-connect 
-p apache-spark-connect-core`;
     - ruff 0.14.0, `test_ruff_excludes.py`, `scripts/audit_no_stubs.sh`;
     - `scripts/gen_type_stubs.py --check` and `scripts/gen_fn_dispatch.py 
--check` against the v4.3.0 source, and `test_type_stubs_offline.py` with mypy 
2.4.1: OK, and 7 passed;
     - the guards of `coverage.yml` and `build_python_connect.yml`.
   
   The conversions of `eval_type` and `deterministic`, which PyO3 makes before 
the name, are left for a later change.
   
   ### 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