HyukjinKwon opened a new pull request, #819:
URL: https://github.com/apache/spark-connect-rust/pull/819
### What changes were proposed in this pull request?
A follow-up of #792 (SPARK-61881) and #806 (SPARK-61916), from a round-3
review of UDTFs and data sources against pyspark-client 4.3.0. Each item was
probed on both clients against a Spark 4.3.0 server.
**1. A UDTF's return type is converted when its plan is built.**
- A return type that is neither a DataType nor a str failed at the call:
`udtf(H, returnType=5)(lit(1))` raised `TypeError: expected a DataType or a DDL
type string`.
- PySpark keeps the value (connect/udtf.py:159-165) and converts it when it
builds the plan, before it pickles the handler (plan.py:3073-3079). So its call
succeeds, and `schema`, `collect` and `register` raise `[UNSUPPORTED_OPERATION]
data type 5 is not supported.` (connect/types.py:210-214).
- The drop-in now does the same. The command of a call (`_udtf_command`)
converts the return type with `pyspark_types_to_proto_types`, then pickles the
handler. So the error comes after those of the arguments, and before the
handler's.
- The message uses the type's `str()` at the build, as PySpark's does: a
`__str__` that changes or raises after the call shows at the request.
**2. `spark.udtf.register` converts the name and the type before it pickles
the handler.** With an unpicklable handler, it raised
`UDTF_SERIALIZATION_ERROR` first. It now raises PySpark's errors:
| call | error |
|---|---|
| `register(5, …)` or `register(None, …)` | `TypeError: bad argument type
for built-in operation` |
| a lone-surrogate name | `UnicodeEncodeError` |
| `b"\xff"` | `UnicodeDecodeError` |
| returnType `5` or `AtomicType()` | `UNSUPPORTED_OPERATION` |
| a valid type | `UDTF_SERIALIZATION_ERROR` |
`_registerPythonUdtf` takes the pickled handler, or a function that pickles
it, which it calls after the conversions.
**3. `spark.dataSource.register` takes `name()` as protobuf takes a string
field.** It took a str only. PySpark puts `name()` into a protobuf string field
(core.py:1159-1162, plan.py:3185), before the class is pickled. So:
- bytes of UTF-8, and subclasses of bytes and str, register;
- an int, None or a bytearray raise `TypeError: bad argument type for
built-in operation`;
- bytes that are not UTF-8 raise `UnicodeDecodeError`;
- a lone surrogate raises `UnicodeEncodeError`.
**4. UDTF object details, as PySpark's.**
- `UserDefinedTableFunction(func)` without a returnType raises PySpark's
`TypeError`.
- `vars()` of a UDTF lists PySpark's attributes in its order.
- `TableArg` has `_subquery_expr` and `_is_partitioned()`
(table_arg.py:48-55).
`pyspark.sql.connect.types.pyspark_types_to_proto_types` raises PySpark's
`UNSUPPORTED_OPERATION` for a value that is not a DataType, as PySpark's does.
`docs/compatibility.md` describes the order of the conversions, and the data
source's name.
### Why are the changes needed?
So that UDTFs and data sources accept and refuse what PySpark's do, at
PySpark's points, with PySpark's errors.
### Does this PR introduce _any_ user-facing change?
Yes, as above:
- an invalid UDTF return type raises when a plan is built, not at the call;
- `spark.udtf.register` raises PySpark's first error;
- data sources can be registered under bytes names.
Remaining differences:
- A valid return type is still converted at the call, so a later change of
the type object is not sent, while PySpark sends it. A change into an invalid
type does raise at the build.
- `TableArg._subquery_expr` is a getter: it is not in `vars()`, and a new
object at each access.
### How was this patch tested?
- **New tests.**
- `test_dropin_offline.py`:
- `test_udtf_return_types_are_converted_when_plans_are_built_as_pyspark`;
- the data-source names and the UDTF's `Lone` type in
`test_registrations_and_interrupts_take_their_names_as_protobuf_as_pyspark`;
- the `vars` order and the missing returnType in
`test_udtf_construction`.
- `test_dropin_types_columns_offline.py`:
`test_pyspark_types_to_proto_types_refuses_values_that_are_not_types_as_pysparks`.
- `test_dropin_window_tablearg_offline.py`:
`test_table_arg_has_the_subquery_expression_and_partitioning_of_pyspark`.
- Live `test_dropin_e2e.py`:
`test_data_sources_are_registered_of_names_as_protobuf_converts_them_as_pyspark`,
which passes on the official client too.
- **Probes of both clients** give no difference for the cases above: 37 of
the return types, 20 of the data-source names, 13 of the `str()` uses and 7 of
the call counts.
- **Official parity tests** with the drop-in: `test_parity_udtf` (387
passed), the Arrow UDTF tests (47), TVF (24), data sources (32) and
`test_parity_python_datasource` (37).
- **Python.**
- The offline suite of `coverage.yml` on pandas 2 and 3.
- `test_type_stubs_offline.py` with mypy 2.4.1, and `gen_type_stubs.py
--check`.
- Live `test_dropin_e2e.py`.
- **Rust.**
- `cargo test -p apache-spark-connect -p apache-spark-connect-core
--release`, and `--features wasm-udf`;
- `cargo test -p pyspark-rs --lib`, linked as `rust.yml` links it.
- **Lint.** `cargo fmt --all --check`, `cargo +1.100.0 clippy -p pyspark-rs
-p apache-spark-connect --all-targets -- -D warnings`, ruff 0.14.0,
`audit_no_stubs.sh` and `scripts/tests`.
### 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]