HyukjinKwon opened a new pull request, #393: URL: https://github.com/apache/spark-connect-rust/pull/393
### What changes were proposed in this pull request? [SPARK-61207](https://issues.apache.org/jira/browse/SPARK-61207): Stop `StructType.add` from re-reading every field and its metadata, in the Python client as PySpark does. Since SPARK-61178, `add` rebuilt the core type of every field on each call. For each field it JSON-encoded the metadata dict, and for a field of a struct, array or map type it read every nested field the same way. A loop of `add` calls was therefore quadratic in that work. PySpark's `add` appends the new field and computes `_needConversion` from the type of each field (`[f.needConversion() for f in self]`). This change does the same in `StructType.add`: - It reads the metadata of the new field only. The base core type takes the new field, as the field list does. The struct's other code reads its live fields; only the fallback for a bare base `DataType` in the type conversion reads this base type. - It computes `_needConversion` from the type object of each field, as PySpark's `add` does. A nested struct needs a conversion without its fields being read, and an array or a map reads the type objects of its elements. A field of a nested type therefore costs about as much as one of an atomic type. - It keeps the value per field for the types that cannot change in place, which are the atomic types. A year-month interval is the exception: its value follows `PYSPARK_YM_INTERVAL_LEGACY` at each `add`, as in PySpark. The value is reset when `StructField.dataType` is set. A nested struct, array, map or UDT is read again on each `add`, since an edit in place can change it. The results are the same as before, and the same as in official PySpark 4.3.0. That covers list identity, edits through the list, and the `_needConversion` values after an edit of a field type or of `PYSPARK_YM_INTERVAL_LEGACY`. Timings of 1000 `add` calls, best of three, against official PySpark 4.3.0 on a loaded machine: | Fields | Before | After | Official | |---|---|---|---| | `add(name, StringType(), True, {"comment": ...})` | 1.6 s | 24 ms | 55 ms | | a commented struct, array of a struct or map of a struct, in turn, with a commented nested field | 4.0 s | 0.22 s | 0.10 s | ### Why are the changes needed? A struct built with a loop of `add` calls took a time quadratic in its fields and their metadata. Official PySpark 4.3.0 takes 55 ms for the same 1000 commented fields: ```python st = StructType() for i in range(1000): st.add(f"c{i}", StringType(), True, {"comment": f"col {i}"}) # 1.6 s before, 24 ms after ``` ### Does this PR introduce _any_ user-facing change? No. The results are the same. Only the time of `StructType.add` changes. ### How was this patch tested? - New offline test `test_struct_type_add_with_metadata_is_linear_and_matches_the_constructor` in `python/tests/test_dropin_offline.py`: - It adds 2000 commented fields under a 3 s bound, and checks that the struct equals the one built by the constructor (`==`, `json`, `simpleString`, `_needConversion`, `names`, `fieldNames`). It also checks that a set `dataType` is seen by the next `add`. - It adds 1500 commented fields of nested types under a 3 s bound, and checks the struct and its `_needConversion` against the constructor's. - It checks that the `_needConversion` of a year-month interval follows `PYSPARK_YM_INTERVAL_LEGACY` at each `add`. - Without this change, the first bound fails: 6.7 s on a loaded machine (17 s on an idle one). With it, the test takes 0.9 s. It passes on official PySpark 4.3.0 in 0.7 s. - A probe of `_needConversion` and `_needSerializeAnyField` after adds of 26 types gives the same values as official PySpark 4.3.0. The types are atomic, nested arrays and maps, structs, a UDT and intervals. The probe also covers edits of field types, a struct from `fromJson`, and `PYSPARK_YM_INTERVAL_LEGACY`. - The live drop-in and Arrow tests of `-k "struct or schema"` against a local Spark 4.3.0 server pass (36 passed). - The lib tests of both crates (618 passed), the build of all tests, also with `wasm-udf`, and the offline Python suites (2457 passed, 8 skipped); rustfmt passes, and clippy reports no diagnostics in the changed lines. CI runs the e2e and live suites. ### 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]
