viirya opened a new pull request, #162:
URL: https://github.com/apache/spark-connect-rust/pull/162
### What changes were proposed in this pull request?
In the Rust-backed PySpark drop-in, some data types did not survive
`pickle.loads(pickle.dumps(t))`, although every `DataType` must be picklable so
that it reaches an official-pyspark UDF worker. This PR fixes them:
- `pyspark.sql.types._parse_datatype_json_value` now parses the JSON string
forms (atomics, `decimal(p,s)`, `string collate X`, intervals,
`geometry(<crs>)`, `geography(<crs>, <alg>)`, ...) with the Rust core's
`DataType::from_json`, through a new `_pyspark.parse_datatype_json`. Before, it
kept a second grammar in Python that knew neither collations nor the spatial
forms. The nested struct/array/map objects still recurse in Python, because a
`udt` field has to rebuild its Python class.
- `GeometryType` and `GeographyType` get `__reduce__`, as the other type
classes have.
- The base `DataType` gets a `__reduce__` fallback, built from
`self.json()`, for a bare unparsed `DataType` and a pure-Python
`UserDefinedType`.
- The core JSON parser reads `"unparsed(x)"` back as `x`; it used to read it
as `unparsed(unparsed(x))`. It also accepts `"decimal"`, `"null"` and
`"calendarinterval"`, which the Python parser accepted before.
- `data_type_to_py` returns a bare `DataType` for an unparsed type instead
of calling back into the Python parser, which now sends JSON strings back to
Rust and would loop.
### Why are the changes needed?
To match PySpark. Before this change:
```python
>>> import pickle
>>> from pyspark.sql.types import *
>>> pickle.dumps(GeometryType(4326))
TypeError: cannot pickle 'pyspark.sql.types.GeometryType' object
>>> pickle.loads(pickle.dumps(StringType("UTF8_LCASE")))
ValueError: cannot parse datatype json: 'string collate UTF8_LCASE'
```
### Does this PR introduce _any_ user-facing change?
Yes. Spatial types, collated strings and every type nested in a
`StructType`, `ArrayType` or `MapType` now pickle, as in PySpark, so they can
be a UDF's return type or appear in a schema sent to a worker.
### How was this patch tested?
- New offline tests in `python/tests/test_dropin_offline.py`: every data
type variant (atomic, parameterized, intervals, collated strings, each
supported spatial SRID, nested array/map) round-trips through pickle with the
same class, `==`, `hash` and JSON, alone and nested in a `StructType` with
field metadata. Further tests cover parsing the spatial and collated JSON
forms, an unparsed type, and a UDT nested next to a spatial type.
- New Rust unit tests for the parser aliases and the `unparsed(x)` round
trip.
- `cargo fmt --all --check`, `cargo test -p apache-spark-connect --release
--lib`, and `cargo test -p apache-spark-connect --release [--features wasm-udf]
--no-run` pass, and clippy reports nothing in the changed lines.
### Was this patch authored or co-authored using generative AI tooling?
Yes.
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]