HyukjinKwon opened a new pull request, #354: URL: https://github.com/apache/spark-connect-rust/pull/354
### What changes were proposed in this pull request? [SPARK-61165](https://issues.apache.org/jira/browse/SPARK-61165): Keep the metadata of a `StructField` as its own dict, so that edits of it show in the field and its struct, in the Python client as PySpark does. A `StructField` of the Python client now keeps its metadata dict, as PySpark's `StructField` does (`self.metadata = metadata or {}`): - `f.metadata` is the same dict on each access, so `f.metadata["comment"] = "x"` changes the field. The edit shows in `f.jsonValue()`, `f.json()`, the equality of fields, pickling, and the `StructType` that holds the field (`st.jsonValue()`, `st.json()`, `st["a"].metadata[...]`), and in the schema of a DataFrame created with the struct. - A non-empty dict passed to the constructor is stored as is, so an edit of it shows in the field. An empty or no dict gives a new one. - The metadata is read from the dict when the field is converted to the core type, so the field and the struct use the metadata as it is now. A `StructType` takes the metadata of each field that has its `StructField` object from that object (SPARK-61137 keeps those objects); the others are as before. Official PySpark 4.3.0 gives the same results for each of these cases. ### Why are the changes needed? The client's `StructField` built a new metadata dict from the Rust field on each access, so an in-place edit went to a temporary and was lost without an error: ```python f = StructField("a", IntegerType()) f.metadata["comment"] = "x" f.metadata # {} in the client, {'comment': 'x'} in PySpark 4.3.0 ``` The same happened through a struct: `st["a"].metadata["comment"] = "x"` did not change `st.jsonValue()`, `StructType.fromJson(st.jsonValue()) == st` was false, and `createDataFrame(rows, st)` did not keep the metadata in `df.schema`. ### Does this PR introduce _any_ user-facing change? Yes. An in-place edit of the metadata of a `StructField` now persists, as in PySpark 4.3.0. Otherwise the metadata of a field is the same. ### How was this patch tested? - `test_struct_field_metadata_is_the_stored_dict` (offline): an edit in place of `f.metadata` (identity of the dict, `jsonValue`, `json`, equality, pickling), the identity of a passed non-empty dict and the new dict of an empty or no one, and the edit through a struct made of the field, of an added field and of a field taken from the struct (`jsonValue`, `json`, `fromJson` equality, pickling). It fails without this change and passes with it, and the same results come from official PySpark 4.3.0. - `test_create_dataframe_schema_keeps_metadata_edited_in_place` (live): `createDataFrame(rows, st)` after an in-place metadata edit keeps the metadata in `df.schema`, `df.schema.jsonValue()`, and the rows are collected. It fails without this change (`{} == {'comment': 'the a'}`) and passes with it; official PySpark 4.3.0 gives the same schema. - Apache Spark's `test_parity_types.py` (connect, against a local Spark 4.3.0 server, drop-in run as `SPARK_CONNECT_TESTING_REMOTE`): 69 passed, 21 skipped, 1 failed, the same as before the change. The failure is `test_infer_schema_specification`, which fails on the server with `UNSUPPORTED_TIME_TYPE` in both runs. The struct, metadata, field and schema cases: 20 passed, 10 skipped, 1 failed (the same test). - Offline suites (`test_dropin_offline.py`, `test_arrow_handoff.py`, `test_ml_offline.py`): 2212 passed, 8 skipped. - Live `-k "struct or schema or metadata or types"` and the new live test: 46 passed. - `cargo fmt --all --check` passes. `cargo clippy` reports no diagnostics in the changed lines. `cargo test -p apache-spark-connect --release --no-run` compiles all tests. - The tests of both crates, e2e included, against a local Spark 4.3.0 server (884 passed), the build of all tests, also with `wasm-udf`, the offline Python suites (2212 passed, 8 skipped), and the live drop-in, Arrow and ML suites (193 passed) and live cases (10 passed). rustfmt passes, and clippy reports no diagnostics in the changed lines. ### 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]
