HyukjinKwon opened a new pull request, #832:
URL: https://github.com/apache/spark-connect-rust/pull/832
### What changes were proposed in this pull request?
A follow-up of #780 (SPARK-61859), #806 (SPARK-61916), #807 (SPARK-61917)
and #801 (SPARK-61912), from a round-3 review of the data types against
pyspark-client 4.3.0. Each item was probed on both clients against a Spark
4.3.0 server.
**1. The concrete data types are subclassable, as PySpark's.**
- In PySpark, the data types of `pyspark.sql.types` are plain Python
classes, so `class MyString(StringType): ...` works. The drop-in's 27 concrete
classes, from NullType to GeographyType, were PyO3 classes without `subclass`,
so `type("X", (StringType,), {})` raised `type 'StringType' is not an
acceptable base type`, and `class X(IntegralType)` raised `cannot create`.
- They are now subclassable, and a subclass behaves as PySpark's:
- **Names.** Its `typeName()` comes from its name, and drives
`simpleString`, `jsonValue` and `json` wherever PySpark's class has no override
of its own, and the `"type"` of array, map and struct JSON.
- **Reprs and hashes.** The reprs that name the class name the subclass,
and the hash follows the repr.
- **Overrides** that PySpark's methods call are called:
`StringType.jsonValue` calls `simpleString`, and `json` calls `jsonValue`.
- **Equality** is PySpark's: `isinstance(other, type(self))`, the
parameters, then `__dict__`.
- **Pickles and copies** keep the subclass and PySpark's `__dict__` state,
of user attributes and `__slots__` too. The pickles of module-level subclasses
are byte-identical to PySpark's at protocols 2 and 5.
- **Construction.** The singleton classes keep one instance per subclass,
as `DataTypeSingleton._instances`. String, Decimal, Time, the two intervals and
StructType set their parameters in an `__init__`, as PySpark's do.
- Subclass objects held by arrays, maps, fields and structs are kept,
compared, rendered and pickled as PySpark's (including `toNullable`, the tree
string and `needConversion`).
- Readers, `applyInPandasWithState` and `createDataFrame` of empty data send
`schema.json()`, as PySpark does.
- UDFs of a subclass return type run the subclass's `toInternal` on the
worker.
- A class of two branches of the type hierarchy (`class X(StringType,
IntegerType)`) is refused with `multiple bases have instance lay-out conflict`,
as its native state would otherwise be left uninitialized.
**2. The metadata of fields is compared as Python compares the dicts.**
- #801 compared the metadata of StructFields as JSON values. So the drop-in
said True where PySpark (`self.__dict__ == other.__dict__`, types.py:124-125)
says False for:
- `{"k": (1, 2)}` and `{"k": [1, 2]}`;
- `{1: "a"}` and `{"1": "a"}`;
- `10**30` and `10**30 + 1`.
- `json()` wrote `1e+30` where PySpark writes the exact integer.
- The metadata dict is now compared with Python's `==` (also inside structs
and nested types) and written by `json.dumps`, so `jsonValue()["metadata"] is
f.metadata`, as in PySpark.
`docs/compatibility.md` and `dev/parity/known-differences.md` update their
"Subclasses" lists and describe these.
### Why are the changes needed?
So that Python code that subclasses the data types, as libraries do, works
on the drop-in as on PySpark, and field metadata compares as PySpark's.
### Does this PR introduce _any_ user-facing change?
Yes, as above:
- the concrete data types can be subclassed;
- field metadata compares and serializes as PySpark's.
Remaining differences, documented:
- **Not in `__dict__`.** The parameters of a type are not in its `__dict__`.
- **A skipped `super().__init__()`.** A subclass that does not call it gets
the class defaults, where PySpark's raises `AttributeError`.
- **Singletons.** A singleton subclass's `__init__` runs at each call, and
its copies are the same instance.
- **Argument errors.** The argument-count messages are PyO3's.
- **`createDataFrame` of non-empty data** sends the core's schema JSON,
where PySpark sends `schema.json()` of the subclass.
- **Cast reprs.** A Column's cast to a subclass shows the base type's name.
- **inf and nan metadata.** Field metadata of `inf` or `nan` still raises at
the field's construction.
### How was this patch tested?
- **New tests.**
- `test_dropin_types_columns_offline.py`:
- a test parametrized over the 27 classes;
- their construction, and pickles of golden bytes;
- their defaults without `super().__init__()`;
- the two-branch guard;
- nested overrides;
- metadata equality (10 cases) and JSON;
- the JSON of `createDataFrame` of empty data.
- `test_dropin_offline.py`: reader and `applyInPandasWithState` schemas of
subclasses, and a UDF command of a subclass return type.
- Live `test_dropin_e2e.py`: UDFs run the subclass's methods.
The new tests pass on pyspark-client 4.3.0 too, but for the lines of the
documented differences.
- **Probes of both clients:**
- 27 classes by about 45 checks each, which differ only in the documented
ways;
- pickles that load across the two clients;
- the metadata probes.
- **Python.**
- The offline suite of `coverage.yml` on pandas 2 and 3.
- Live `test_dropin_e2e.py`, `test_arrow_e2e.py` and
`test_misc_api_e2e.py`.
- The type-stub drift check and test, with mypy and pyright.
- **Rust.** `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
--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]