HyukjinKwon opened a new pull request, #829:
URL: https://github.com/apache/spark-connect-rust/pull/829
### What changes were proposed in this pull request?
PySpark 4.3.0's `createDataFrame` of local rows serializes each value of a
user-defined type with the type object that the schema holds:
`LocalDataToArrowConversion._create_converter` binds `udt = dataType` and calls
`udt.serialize(value)` (`python/pyspark/sql/conversion.py:846-877`). The
drop-in's converter, `udt_of` of `crates/pyspark-rs/src/infer.rs`, did
something else:
- it took the value's `__UDT__` when it was of the class;
- otherwise it made an instance of the class with no arguments,
`class.call0()`.
So:
- a UDT of a parameter that its JSON does not hold, such as a scale,
serialized silently with the parameter's default. `ScaleUDT(2)` and
`ScaleUDT(10)` of one schema both sent the values of `ScaleUDT()`.
- a UDT whose constructor needs an argument raised `TypeError: __init__()
missing 1 required positional argument`.
Now the conversion of local rows takes the UDT objects of the given
`StructType` (`infer::UdtObjects`). It reads them alongside the core type, as
PySpark's converters read them: the `dataType` of each field, `elementType`,
`keyType` and `valueType`, and the `sqlType()` of a UDT that holds another.
They are read after the rows are taken apart and before the values are
converted, as PySpark makes its converters. This covers:
- UDT columns;
- UDTs in arrays, in the keys and the values of maps, and in structs;
- UDTs in the SQL type of another UDT.
`udt_of` remains for an inferred schema, which holds no objects, and takes
the `__UDT__` of each value as before. PySpark's inferred schema holds the
`__UDT__` of the first value (`_merge_type` keeps the first of two UDTs of one
class), which is the same object when `__UDT__` is an attribute of the class,
as usual. A pandas DataFrame goes through PySpark's own conversion and is
unchanged.
### Why are the changes needed?
The drop-in sent other data than PySpark for a UDT of parameters, without an
error. It could not create a DataFrame of local rows of a UDT whose constructor
needs an argument at all.
### Does this PR introduce _any_ user-facing change?
Yes. With a schema of `ScaleUDT(2)`, `ScaleUDT(10)`,
`ArrayType(ScaleUDT(10))` and `MapType(StringType(), ScaleUDT(2))`,
`createDataFrame([(3, 4, [5], {"k": 6})], schema).toArrow()` gives these rows:
- PySpark's `[{'a': 6, 'b': 40, 'c': [50], 'd': [('k', 12)]}]`, as now;
- `[{'a': 3, 'b': 4, 'c': [5], 'd': [('k', 6)]}]` before.
A UDT whose constructor needs an argument serializes too, where it raised
`TypeError`.
### How was this patch tested?
-
`test_local_rows_are_serialized_by_the_udt_objects_of_the_schema_as_pysparks`
in `python/tests/test_dropin_offline.py`, with the mock server, checks the
Arrow data of the relation sent:
- UDTs in a field, an array, the keys and the values of maps, a struct,
and the SQL type of another UDT;
- `ScaleUDT(2)` and `ScaleUDT(10)`, and a UDT whose constructor needs its
argument;
- nulls, rows by position and by name, and a pandas DataFrame.
The expected values are those that official PySpark 4.3.0's
`LocalDataToArrowConversion` gives of the same data and schema, as literals. On
master the test fails with the `TypeError`.
-
`test_local_rows_are_serialized_by_the_udt_objects_of_the_schema_as_pyspark` in
`python/tests/test_dropin_e2e.py`, against a Spark 4.3.0 server: the values
that the server stores, of `toArrow()`, as `pyspark-client` 4.3.0 gives them.
On master it fails with `{'a': 3, 'b': 4, 'c': [5], 'd': [('k', 6)]}`.
- The UDT and `createDataFrame` tests of the offline files: 168 passed.
- The Rust gates:
- `cargo +1.100.0 fmt --all --check`;
- `cargo +1.100.0 clippy -p apache-spark-connect -p
apache-spark-connect-core -p pyspark-rs --all-targets -- -D warnings`;
- `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps -p apache-spark-connect
-p apache-spark-connect-core`;
- `cargo test -p pyspark-rs --lib` as `rust.yml` runs it: 13 passed.
- The Python gates:
- ruff 0.14.0 `check` and `format --check`, `test_ruff_excludes.py` and
`scripts/audit_no_stubs.sh`;
- `scripts/check_vendored_upstream.sh` against the `v4.3.0` tag from a
local clone of apache/spark;
- `scripts/tests`;
- `gen_type_stubs.py --pyspark-src <v4.3.0>/python --check`;
- `test_type_stubs_offline.py` with mypy 2.4.1: 7 passed;
- the guards of SPARK-61851.
- The offline suite of `coverage.yml`, with upb: 6259 passed and 77 skipped.
Python coverage is 94%. The skips are those of master: 68 for "not a public
constructor", 7 that need `SPARK_REMOTE`, and `test_type_stubs_offline.py`,
which needs mypy and ran above.
- `test_dropin_e2e.py` against a Spark 4.3.0 server: 311 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]