HyukjinKwon opened a new pull request, #476: URL: https://github.com/apache/spark-connect-rust/pull/476
### What changes were proposed in this pull request? [SPARK-61402](https://issues.apache.org/jira/browse/SPARK-61402): `VariantVal`, `Geometry` and `Geography` of the Python client pickle as PySpark pickles its plain classes, and `Geometry` and `Geography` can be subclassed, as `VariantVal` can since SPARK-61291 (#471). In `crates/pyspark-rs/src/values.rs`: - A value pickles as `copyreg.__newobj__` of its class, with the state that `object.__reduce_ex__` gives a plain class. That state is the dict of its instance attributes and its fields (`value`/`metadata`, or `wkb`/`srid`), or `(dict, slots)` for a subclass that has slots. - `__setstate__` takes that state and also PySpark's pickles of the classes. The fields go to the Rust fields and the rest to the instance's `__dict__` and slots. - `Geometry` and `Geography` get `subclass`, a `__new__` that takes any arguments, and an `__init__(wkb, srid)`, as `VariantVal` has. ### Why are the changes needed? - SPARK-61291 pickled a `VariantVal`'s state as the tuple `(value, metadata, __dict__)`. PySpark's `VariantVal` is a plain class without `__setstate__`, and pickle's BUILD gives such a class a dict state only. So official PySpark cannot load a `VariantVal` that the Python client pickled: ``` _pickle.UnpicklingError: state is not a dictionary ``` The server's Python workers run official PySpark. So a UDF, pandas UDF, UDTF, `foreachBatch` or other function that refers to a `VariantVal` fails on the server. With a UDF that returns `v.toJson()` of a collected `parse_json('{"a":1}')`: | Client | Result | |---|---| | master | `PythonException: ... _pickle.UnpicklingError: state is not a dictionary` | | before SPARK-61291 | `[Row(j='{"a":1}')]` | | official PySpark | `[Row(j='{"a":1}')]` | - The `Geometry` and `Geography` values of the Python client could not be pickled, copied or subclassed at all. The errors were `TypeError: cannot pickle 'pyspark.sql.types.Geometry' object` and `type 'pyspark.sql.types.Geometry' is not an acceptable base type`. So a `Row` that holds one could not be pickled or deep-copied either, and a UDF that refers to one could not be sent. PySpark's are plain classes, which support all of these. - A subclass of `VariantVal` with `__slots__` lost the values of its slots in a pickle. PySpark keeps them. - PySpark's own pickles of these classes did not load in the Python client: `TypeError: argument 'state': 'dict' object is not an instance of 'tuple'`. ### Does this PR introduce _any_ user-facing change? Yes, in the Python client, as PySpark behaves: - UDFs and other server-side Python functions that refer to a `VariantVal`, `Geometry` or `Geography` run. - `Geometry` and `Geography` values, and the Rows that hold them, can be pickled and copied. - `Geometry` and `Geography` can be subclassed. - PySpark's pickles of the three classes load. ### How was this patch tested? - New offline tests in `python/tests/test_dropin_offline.py`, placed before `test_str_of_variant_values_is_their_json_and_they_are_no_rows_as_pyspark`: - `test_variant_and_spatial_values_pickle_as_pysparks_plain_classes_which_load_them`, at every protocol from 2 up: - an unpickler that resolves the three classes to plain classes like PySpark's loads the client's pickles, with their attributes; - PySpark's byte-exact pickles of the classes load in the client. The test makes them by pickling a plain class while `pyspark.sql.types.<name>` is temporarily that class. - `test_spatial_values_copy_pickle_and_are_subclassed_as_pyspark`: - `copy`, `deepcopy` and pickling of `Geometry` and `Geography`, also in a `Row`; - subclasses whose `__init__` takes another argument, which unpickling does not run; - the slots of a subclass of `VariantVal`. - A new live test in `python/tests/test_dropin_e2e.py`, placed before `test_create_dataframe_of_multi_and_categorical_labels_as_pyspark`: `test_udfs_that_refer_to_variant_and_spatial_values_run_on_the_server_as_pyspark` runs a UDF that refers to a collected `VariantVal`, a `Geometry` and a `Geography` against a local Spark 4.3.0 server. - Without the fix, on a build of master: - the first offline test fails with `_pickle.UnpicklingError: state is not a dictionary`; - the module of the second does not import (`not an acceptable base type`); - the live test fails with `cannot pickle 'pyspark.sql.types.Geography' object`. - A `VariantVal` pickled by the fixed client loads in official PySpark 4.3.0. - The offline Python suites, the existing VariantVal, Geometry and Geography tests (offline and live), `cargo fmt --all --check`, and clippy, which reports nothing in `values.rs`. ### 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]
