tdcmeehan opened a new pull request, #471: URL: https://github.com/apache/spark-connect-rust/pull/471
### What changes were proposed in this pull request? [SPARK-61291](https://issues.apache.org/jira/browse/SPARK-61291): let `VariantVal` be subclassed in the Python client, as PySpark's can. - `crates/pyspark-rs/src/values.rs`: the `VariantVal` pyclass gets `subclass`, so `class TaggedVariant(VariantVal): pass` no longer raises `TypeError`. - The constructor is split as PySpark's is: `__new__` accepts any arguments and `__init__(value, metadata)` sets the bytes. A subclass's `__init__` may take other arguments, and `super().__init__(value, metadata)` works. - `__reduce__` pickles as a plain class instance does: the class is rebuilt through `copyreg.__newobj__` without calling `__init__`, and `__setstate__` restores the value, metadata and instance attributes. Subclass instances behave as the base class does everywhere the Rust code and the Python shims check for a `VariantVal`: `createDataFrame` sends them as variants, `_infer_type` types them as `VariantType`, and a row of one is rejected with the existing "Rows cannot be of type VariantVal" error. `collect()` still returns the base `VariantVal`, as PySpark's does. ### Why are the changes needed? PySpark's `VariantVal` is a plain class, so a subclass of it is a variant, and code that tags values this way fails on the Python client at the class statement. The reference is PySpark 4.3.0 against the same server: the issue's repro prints `{"a":1}` on it and raised `TypeError` here before this change. ### Does this PR introduce _any_ user-facing change? Yes. A subclass of `VariantVal` can be defined and passed to `createDataFrame`, where it previously raised `TypeError` at the class statement. `VariantVal` pickles with a different payload: it is rebuilt through `copyreg.__newobj__` with a state, instead of by calling `VariantVal(value, metadata)`. Pickles of the previous form still load, because pickle calls the class with those arguments, which runs `__init__`. Checked: a `VariantVal` pickled by the build before this change loads with this one, with the same value and metadata. ### How was this patch tested? - Repro from the issue, before the change: `TypeError: type 'pyspark.sql.types.VariantVal' is not an acceptable base type`. After the change: `{"a":1}`, as `pyspark-client` 4.3.0 prints it on the same server. - `python/tests/test_dropin_offline.py`: - `test_variant_val_subclass_as_pyspark`: a subclass is repr'd, str'd and typed as the class, compares by identity, pickles with its attributes without running its `__init__`, infers as a variant, and a row of it is rejected. It fails without the change, since the module's subclass cannot be defined. - `test_variant_val_subclass_is_sent_as_its_variant` (mock server): the local relation of a subclass equals that of the `VariantVal` it subclasses. - `python/tests/test_dropin_e2e.py`: `test_variant_val_subclass_round_trips_as_pyspark` runs on a live server: `createDataFrame` of a subclass, collected as a `VariantVal` that prints `{"a":1}`. - Offline suites: `tests/test_dropin_offline.py tests/test_ml_offline.py tests/test_arrow_handoff.py`: 2929 passed, 8 skipped. - Live suites: `tests/test_arrow_e2e.py tests/test_dropin_e2e.py tests/test_ml_e2e.py` against the Spark 4.3.0 Connect server: 237 passed, 1 skipped, 0 failed. - `cargo test -p apache-spark-connect --release --lib`: 528 passed. - `cargo test -p apache-spark-connect --release --no-run`, and with `--features wasm-udf`: both build. - `cargo fmt --all --check`: clean. - `cargo clippy --workspace --all-targets --release`: no diagnostic in `values.rs`; the other diagnostics in `pyspark-rs` and `spark-connect` are in files this change does not touch. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Opus 5.5 and Claude Haiku 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]
