HyukjinKwon opened a new pull request, #780:
URL: https://github.com/apache/spark-connect-rust/pull/780
### What changes were proposed in this pull request?
**The base classes of the data types are instantiable, as in PySpark
4.3.0.** That covers `DataType`, `AtomicType`, `NumericType`, `IntegralType`,
`FractionalType`, `DatetimeType`, `AnyTimeType`, `SpatialType` and
`AnsiIntervalType`. Before, they raised `TypeError: cannot create
'pyspark.sql.types.FractionalType' instances`. `DataType()` existed, but its
`simpleString()` was `void`, and it pickled to `NullType()`. Now, as PySpark's:
- `repr` and `str` are `XType()`. `typeName()`, `simpleString()` and
`jsonValue()` give the stem of the name (`data`, `atomic`, `numeric`,
`integral`, `fractional`, `datetime`, `anytime`, `spatial`, `ansiinterval`),
and `json()` gives `'"<stem>"'`.
- `needConversion()` is False, and `toInternal(x)`/`fromInternal(x)` return
`x`.
- Equality and hashing:
- instances of a class are `==` and hash equal, as PySpark compares the
class and the `__dict__`;
- `AtomicType() == DataType()` is False.
- `IntegralType` is a singleton, as in PySpark, and its arguments raise
`DataTypeSingleton.__call__()`'s errors. The others raise `XType() takes no
arguments`, unless a subclass's own `__init__` takes them, so pure-Python
`UserDefinedType` subclasses keep working.
- They pickle as PySpark's do, as `copyreg.__newobj__` of the class and its
`__dict__`. The drop-in loads PySpark's pickles of them at protocols 2 to 5,
and PySpark loads the drop-in's at every protocol.
- Nested in arrays, maps and structs, they keep their names in
`simpleString`, the JSON and `treeString`. A cast, a UDF's return type,
`df.to`, the proto conversion of `pyspark_types_to_proto_types` and the Arrow
conversion of local rows refuse them with PySpark's errors.
`PyDataType.inner` is now `Option<DataType>`, None for these classes, whose
state is their `__dict__`. Where nested, they are kept in the core as the JSON
of an unparsed type, as a `StructField` used as a type already is. The core's
`tree_string_with_names` lets the Python client name them in a tree string.
**The methods of the types take PySpark's parameter names:**
- `StringType.collationProvider(collationName)`;
- `DateType.toInternal(d)` / `fromInternal(v)`;
- `TimeType.toInternal(t)` / `fromInternal(nano)`;
- `TimestampType` and `TimestampNTZType`: `toInternal(dt)` /
`fromInternal(ts)`;
- `DayTimeIntervalType.toInternal(dt)` / `fromInternal(micros)`;
- `GeometryType.toInternal(geometry)` and
`GeographyType.toInternal(geography)`, new.
The signatures shown are PySpark's too:
- `()` for the parameterless types, `DataType`, the abstract classes and
`VariantType`;
- `StringType`'s `collation='UTF8_BINARY'`, the `fieldPath=''` of
`fromJson`, and `StructType.add`'s `nullable=True`.
`VariantType(1)` now raises `VariantType() takes no arguments`, as PySpark's
does.
### Why are the changes needed?
So that code written against PySpark's types works on the drop-in. The
official pandas-on-Spark test
`pyspark/pandas/tests/connect/data_type_ops/test_parity_base.py::test_data_type_ops`
failed with `TypeError: cannot create 'pyspark.sql.types.FractionalType'
instances`. It passes now, as does the rest of its file. Calls by keyword, such
as `TimestampType().toInternal(dt=...)`, raised `TypeError`.
### Does this PR introduce _any_ user-facing change?
Yes, as above, as PySpark 4.3.0 behaves.
Documented differences that remain:
- PyO3 cannot give a class a metaclass, so `type(IntegerType)` is `type`,
where PySpark's singletons are of `DataTypeSingleton`.
- A Python subclass of `IntegralType` cannot be instantiated, where PySpark
makes one instance per subclass.
- The `__init__` of the parameterized classes is a slot wrapper that shows
`(self, /, *args, **kwargs)`; the classes' signatures match.
### How was this patch tested?
- New tests in `python/tests/test_dropin_types_columns_offline.py`, with
PySpark's values and pickle bytes captured as literals: the object model of
each class, their pickles both ways, equality, nesting, the proto and Arrow
refusals, Python subclasses, and the parameter names. The file has 107 passed.
- Probes of both clients match, apart from the documented differences:
- repr, typeName, simpleString, json, needConversion, the conversions,
eq/hash and singletons;
- the argument errors, pickles at protocols 0 to 5, weak references and
attributes;
- nested types, the signatures, and errors against a Spark 4.3.0 server.
- `test_parity_base.py` passes on the drop-in. With master's extension,
`test_data_type_ops` fails as above.
- The official drop-in parity tests of types, geography, geometry, serde and
creation pass.
- The offline suite of `coverage.yml`, `test_ml_offline.py`, and live
`test_ml_e2e.py` and the UDT/vector tests of `test_dropin_e2e.py`.
- `cargo test -p apache-spark-connect --release --lib` and `--test
types_roundtrip`.
- `cargo fmt --all --check`, `cargo +1.100.0 clippy -p pyspark-rs -p
apache-spark-connect --all-targets -- -D warnings`, ruff 0.14.0 and
`audit_no_stubs.sh`.
### 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]