HyukjinKwon opened a new pull request, #773:
URL: https://github.com/apache/spark-connect-rust/pull/773

   ### What changes were proposed in this pull request?
   
   PySpark's data types are plain classes. Pickle saves one as its class and 
its `__dict__`: `copyreg.__newobj__(cls)` and the state at protocols 2 and up, 
pickle's default being 4. So the pickle of a type that official PySpark made is 
loaded by `cls.__new__(cls)` followed by `__setstate__`. The drop-in's 
parameterized type classes required their constructor arguments and had no 
`__setstate__`, so those pickles did not load right:
   
   | PySpark's pickle of | loaded in the drop-in before | after |
   |---|---|---|
   | `DecimalType(10, 2)` | `DecimalType(10,0)`, `.scale == 0` (the state went 
to `vars()`) | `DecimalType(10,2)` |
   | `StringType('UTF8_LCASE')` | `StringType()` | `StringType('UTF8_LCASE')` |
   | `TimeType(3)` | `TimeType(6)` | `TimeType(3)` |
   | `DayTimeIntervalType(1, 2)`, `YearMonthIntervalType(0, 0)` | the default 
fields | the same type |
   | `ArrayType`, `MapType`, `StructField`, `StructType`, `CharType`, 
`VarcharType`, `GeometryType`, `GeographyType`, so any `df.schema` | 
`TypeError: ... missing 1 required positional argument` | the same type |
   
   In `crates/pyspark-rs/src/types.rs`:
   - **Construction.** `ArrayType`, `MapType`, `StructField`, `CharType`, 
`VarcharType`, `GeometryType` and `GeographyType` construct without arguments, 
as the `object.__new__` of PySpark's classes does. The constructor's work moves 
to `__init__`, as `PyStorageLevel` already does. PySpark's constructor messages 
stay: `ArrayType()` still raises `ArrayType.__init__() missing 1 required 
positional argument: 'elementType'`. `text_signature` keeps PySpark's 
signature, and `inspect.signature` now shows `containsNull=True` where it 
showed `Ellipsis`.
   - **`__setstate__`** on the 13 parameterized classes. It sets each entry of 
PySpark's state as `setattr` does: a parameter through its setter, and any 
other attribute in the instance `__dict__`.
     - A `StructType` keeps PySpark's pickled `_needConversion`, or computes it 
when the state lacks it.
     - The private `_crs` and `_alg` of the spatial types are derived from the 
SRID.
   - **The parameters are assignable**, as PySpark's plain attributes are: 
`precision`, `scale`, `hasPrecisionInfo`, `collation`, `length`, 
`startField`/`endField`, and `srid`. The core type follows each assignment, so 
`t.scale = 3` makes a `DecimalType(10, 3)` in its repr, JSON, `==` and hash, 
also inside the struct, array or map that holds it.
   - Unchanged:
     - the singletons and the UDT base;
     - the drop-in's own pickles, `__reduce__` as `_parse_datatype_json_string` 
of the JSON, which the server's Python workers load (AGENTS.md section 4).
   
   `scripts/capture_type_pickles_golden.py` captures 
`tests/golden/type_pickles.json` on official PySpark 4.3.0. It holds the 
pickles of 52 types at protocols 0 to 5, and of five types edited after they 
were made: every type family, nested types, non-bool flags, collations and a 
UDT (`VectorUDT`). It also records each type's repr, JSON, simpleString and 
attributes.
   
   `docs/compatibility.md` describes what loads and what remains different.
   
   ### Why are the changes needed?
   
   A schema that was pickled with PySpark could not be loaded with the drop-in: 
before switching clients, or inside an object of an application, a cache or a 
model. Where it did load, it silently had the wrong precision, collation or 
interval fields. And code that sets a type's attribute, as PySpark allows, 
raised `AttributeError: ... is not writable`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, as PySpark 4.3.0 behaves:
   - PySpark's pickles of all data types load at protocols 2 to 5;
   - the parameters of a type can be assigned.
   
   Documented differences:
   - At protocols 0 and 1, PySpark's pickles call `object.__new__(cls)`, which 
CPython refuses for a class with a native base, as already documented for 
Observation. They still raise `TypeError`.
   - `vars(t)` is empty, as the parameters are not entries of the `__dict__`.
   - An assigned value that the constructor refuses raises the constructor's 
error at once, where PySpark keeps any value and its repr or JSON may fail 
later.
   - An assigned `srid` changes the JSON too.
   
   ### How was this patch tested?
   
   New tests in `python/tests/test_dropin_types_columns_offline.py`, against 
the golden:
   - every type at protocols 2 to 5 loads equal to the drop-in's own 
constructed type, with the same hash, repr, JSON, simpleString and attributes, 
and the same object for the singletons;
   - protocols 0 and 1 raise the `TypeError`;
   - the edited pickles keep their state;
   - parameter assignment, also of nested types, gives PySpark's outputs;
   - the constructors keep their messages and signatures.
   
   On master's build all of them fail except the protocol 0/1 one. Also:
   - Against a Spark 4.3.0 server: official PySpark's pickle of a real 
`df.schema` loads equal to the server's schema, and `createDataFrame` of it 
gives PySpark's rows.
   - PySpark still loads the drop-in's pickles of all 52 types at every 
protocol.
   - The offline suite of `coverage.yml`, `test_dropin_e2e.py` against a server 
(294 passed), `cargo fmt --all --check`, `cargo +1.100.0 clippy -p pyspark-rs 
--all-targets -- -D warnings`, ruff 0.14.0 and `scripts/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]

Reply via email to