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

   ### What changes were proposed in this pull request?
   
   PySpark 4.3.0's `createDataFrame` of local rows converts them with 
converters of the type objects of the schema (`LocalDataToArrowConversion` of 
`python/pyspark/sql/conversion.py`), and reads the rows with the Python 
protocol. The drop-in's conversion (`crates/pyspark-rs/src/infer.rs`, and 
`create_from_local` of `crates/pyspark-rs/src/session.rs`) differed in three 
ways:
   
   1. **The values of a user-defined type of an inferred schema.** PySpark's 
`_infer_type` returns `obj.__UDT__` (`types.py:2654-2655`), `_merge_type` keeps 
the first of two UDTs of one class (`types.py:2921-2974`), and the converter 
serializes every value with that object (`conversion.py:846-877`). The drop-in 
serialized each value with its own `__UDT__` (`udt_of`), as the core type of an 
inferred schema holds no object. So values whose `__UDT__` has other 
parameters, such as a scale, sent other data.
   2. **Rows of a tuple subclass of its own `__len__` or `__iter__`.** 
PySpark's `to_row` takes a tuple by `len(item)`, which must be the number of 
fields, and `tuple(item)` (`conversion.py:944-973`). `_infer_schema` names a 
tuple by `len(row)` and zips the names with `row` (`types.py:2810-2821`). The 
converter of a struct reads a nested tuple by `len(value)` and `value[i]` 
(`conversion.py:620-640`). The drop-in read the values as the tuple stores 
them, while its inference iterated the row.
   3. **The type objects of a given schema** (KD-types C7). PySpark's 
converters name the type object in their errors (`f"input for {dataType} must 
not be None"`, `conversion.py:599-925`), and the converter of a spatial type 
calls `dataType.toInternal(value)` (`conversion.py:893-916`). The drop-in named 
its core type, such as `BooleanType()` of a subclass of `BooleanType`, and did 
not call the `toInternal` of a subclass of `GeometryType` or `GeographyType`.
   
   The change:
   - **`infer::UdtObjects` becomes `infer::TypeObjects`**, the type objects of 
the schema at the places of its core type:
     - Of a given schema, a `StructType` or the atomic type of a single column, 
it holds the object at each place. They are read as PySpark makes its 
converters, of `fields`, `dataType`, `elementType`, `keyType`, `valueType` and 
the `sqlType()` of each UDT, after the rows are taken apart.
     - Of an inferred schema, it holds the UDT objects. Inference 
(`infer_type`, `infer_dict`, `infer_list`, `infer_fields`) gives the `__UDT__` 
of each value of a UDT with its type, and `merge_type` merges them as it merges 
the types, so each place holds the UDT of its first value that is not None. 
Once the rows are taken apart, the objects of the `sqlType()` of each UDT are 
read, as PySpark makes the converter of each of them. Data of no UDT allocates 
no objects.
     - `convert` serializes a value with the UDT object of its place. The 
errors of a None of a field that is not nullable, and of a value of a null 
type, name the `str()` of the type object. A spatial type of a subclass 
converts each value with its `toInternal`, whose result is converted as PyArrow 
converts a value to the struct of the type: None, a dict of `srid` and `wkb`, a 
tuple of two, or another value by PyArrow itself, whose error it raises after 
the converters, as PyArrow's.
   - **Tuple rows:**
     - `row_values` takes a tuple row whose type has another `__len__` or 
`__iter__` than `tuple`'s by `len(item)` and `tuple(item)` 
(`PySequence_Tuple`), as it takes another sequence row.
     - Inference (`row_items`) names a tuple or a list row by `len(row)`, 
extends the given names to it as `_infer_schema` does, and zips them with 
`iter(row)`; a Row and a namedtuple take `tuple(row)`.
     - `convert_struct` takes a nested tuple whose type has another `__len__` 
or `__getitem__` than `tuple`'s, or the Row class's, which reads an int index 
of its storage, by `len(value)` and `value[i]` of each field.
     - `create_from_local` raises the `IndexError: tuple index out of range` of 
PySpark's `row[i]` for a row whose `tuple(item)` has fewer values than the 
fields, as its column converts, and leaves out the values after the fields.
     - Exact tuples, Rows and namedtuples keep the native reads. The fast path 
of `createDataFrame` of exact tuples of ints, `exact_long_tuple_columns`, takes 
exact tuples only and is unchanged.
   - **Docs:**
     - `docs/compatibility.md` and `dev/parity/known-differences.md` no longer 
list the errors and the spatial conversions of local rows among the differences 
of a type of a subclass.
     - The paragraph of `docs/compatibility.md` on the types of no proto adds 
`transformWithState` and `transformWithStateInPandas`, which raise 
`UNSUPPORTED_OPERATION` at their first request since SPARK-61962.
   
   ### Why are the changes needed?
   
   The drop-in sent other data than PySpark, without an error, of UDT values of 
other parameters in an inferred schema and of tuple subclasses of their own 
`__iter__` or `__len__`. It raised other errors than PySpark's, or none, of 
such rows, and of values of types of subclasses, and did not call the 
`toInternal` of a spatial type of a subclass.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. With `Val(v, s)` a value whose `__UDT__` is a `ScaleUDT(s)` that 
serializes `v * s`, a tuple subclass `T` whose `__iter__` gives `(99, "z")`, a 
tuple subclass `Len3` whose `__len__` is 3, and `SubBool(BooleanType)`, checked 
on both clients against a Spark 4.3.0 server:
   
   | Call | pyspark-client 4.3.0, and now | Before |
   |---|---|---|
   | `createDataFrame([(Val(3, 2),), (Val(4, 10),)]).toArrow()` | `[{'_1': 6}, 
{'_1': 8}]` | `[{'_1': 6}, {'_1': 40}]` |
   | `createDataFrame([T((1, "a"))], "a long, b string").collect()` | 
`[Row(a=99, b='z')]` | `[Row(a=1, b='a')]` |
   | `createDataFrame([Len3((1, "a"))], "a long, b string")` | 
`[AXIS_LENGTH_MISMATCH]`, 2 and 3 | `[Row(a=1, b='a')]` |
   | a tuple row whose `__len__` raises | its error | no error |
   | `createDataFrame([(None,)], StructType([StructField("a", SubBool(), 
False)]))` | `input for SubBool() must not be None` | `input for BooleanType() 
must not be None` |
   | a `GeometryType` subclass whose `toInternal` returns SRID 0 | its 
`toInternal` is called, and the server refuses the SRID 
(`GEO_ENCODER_SRID_MISMATCH_ERROR`) | the value's SRID is sent |
   
   What stays different: `collect()` of an inferred UDT column deserializes 
with the user-defined type of its JSON that a type was last made of, where 
PySpark makes a new one (KD-types C8, owned by another PR).
   
   ### How was this patch tested?
   
   - **New offline tests** in `python/tests/test_dropin_offline.py`, with the 
mock server, of the Arrow data of the relation sent, the errors and the calls. 
Their values are those of official PySpark 4.3.0's conversion of the same data:
     - 
`test_local_rows_of_an_inferred_schema_are_serialized_by_its_udt_objects_as_pysparks`:
 values of UDTs of other scales in a field, nulls first, two fields, an array, 
the keys and the values of a map, a struct, a dict as a struct, and the SQL 
type of another UDT, of tuples, Rows, dicts, objects and names.
     - `test_tuple_rows_of_their_own_len_and_iter_convert_as_pysparks`: tuple, 
namedtuple and Row subclasses of their own `__iter__`, `__len__` or 
`__getitem__`, of a schema, of inference and of names, nested too; the 
`AXIS_LENGTH_MISMATCH`, the `IndexError` and the errors of a raising `__len__` 
or `__iter__`; and the order of the calls of `__len__` and `__iter__`.
     - 
`test_local_rows_name_and_convert_by_the_type_objects_of_the_schema_as_pysparks`:
 the None errors of types of subclasses and of their own repr, in a field, an 
array, a map's keys and values and a struct, and of a null type; the 
`toInternal` of spatial subclasses, in a field, an array, a map and a struct, 
of a dict, None, a tuple and pairs, PyArrow's error of another value, and its 
own error.
   
     They fail on master and pass with the change.
   - **A new e2e test**, 
`test_local_rows_of_an_inferred_schema_are_serialized_by_its_udt_objects_as_pyspark`
 in `python/tests/test_dropin_e2e.py`, against a Spark 4.3.0 server: the values 
that the server stores, of `toArrow()`. Its body passes on pyspark-client 
4.3.0, and fails on master.
   - **Probes of both clients** against the same server, compared line by line:
     - the inferred UDTs of 22 cases are identical, but for `collect()`, which 
C8 above covers;
     - the 51 lines of the tuple rows, with the calls of `__len__` and 
`__iter__`, are identical;
     - the 43 lines of the type objects of a schema are identical;
     - the reviewers' probes are identical, but for the address in an object's 
repr, and the intended difference of the JSON of a schema of a subclass.
   - **The gates:**
     - `cargo fmt --all --check`;
     - `cargo +1.100.0 clippy -p apache-spark-connect-core -p 
apache-spark-connect -p pyspark-rs --all-targets -- -D warnings`;
     - `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps -p apache-spark-connect 
-p apache-spark-connect-core`, and the lint job's of the four published crates 
with `--features apache-spark-connect/wasm-udf`;
     - `cargo test -p pyspark-rs --lib` as `rust.yml` runs it: 13 passed;
     - ruff 0.14.0 `check` and `format --check`, `test_ruff_excludes.py`, 
`scripts/audit_no_stubs.sh`, `gen_type_stubs.py --check`, `gen_fn_dispatch.py 
--check` and the guards of the test lists;
     - `test_type_stubs_offline.py`: 7 passed, 2 skipped;
     - the offline suite of `coverage.yml`: 6370 passed, 77 skipped;
     - `test_dropin_e2e.py` against a Spark 4.3.0 server: 320 passed.
   
   ### 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