HyukjinKwon opened a new pull request, #801:
URL: https://github.com/apache/spark-connect-rust/pull/801
### What changes were proposed in this pull request?
A follow-up of #786 (SPARK-61875), from a round-2 review of cached schemas
against PySpark 4.3.0. Each item was probed on both clients through a recording
proxy.
**1. The schema that `to()` keeps is copied where PySpark copies
`_cached_schema`.**
- `to(<a value that is not a DataType>)` keeps it as given, and it survived
only the set operations. So `to("a long").alias("x").schema` raised
`UNSUPPORTED_OPERATION`, where PySpark gives `'a long'` with no request.
- It is now copied in each method that copies `_cached_schema` in PySpark
(connect/dataframe.py:333 ... 1197): alias, filter/where, limit, sort/orderBy,
sortWithinPartitions, hint, the repartitions, coalesce, distinct,
dropDuplicates, sample, randomSplit and observe.
- `to(LongType())` and other types that are not structs are now kept as the
cached schema too, as PySpark keeps them (dataframe.py:2085). Before, `.schema`
sent an AnalyzePlan and got the server's `CONNECT_INVALID_PLAN`.
**2. Unpickled DataFrames.**
- PySpark sends one schema request at the unpickle of a DataFrame, as its
`__getattr__` reads `columns` when pickle looks up `__setstate__`, and then
restores its `_cached_schema` (dataframe.py:149-170 and 1871-1890).
- The drop-in sent no request, and kept no cached schema. Now a pickle
carries its cached schema, and an unpickle sends the schema request, raises its
error, and keeps the cached schema.
- A DataFrame whose plan fails to build raised at the pickle, where
PySpark's pickles and raises at the unpickle. It now pickles its error and
raises it at the unpickle. The docs that said its copy raises "at its requests"
are corrected.
- A stream writer's pickle requests nothing, as PySpark's, which holds only
the plan.
**3. Field metadata is compared as Python compares the dicts.** The set
operations' merge of schemas, and `StructType`/`StructField` `==`, compared
metadata as JSON values, so `1 != 1.0`, and a union sent a schema request that
PySpark does not. They now compare as Python's `==` compares the `json.loads`
values: `1 == 1.0 == True`, exact int/float comparison, recursing into arrays,
maps, structs and UDTs. This goes through the core's doc-hidden
`DataType::eq_as_python` and `StructField::eq_as_python`. The derived
`PartialEq` of the Rust API stays exact.
**4. `observe` keeps the DataFrame's `isStreaming`.**
`df.observe(Observation, ...)` sent `AnalyzePlan(is_streaming)` at each call.
PySpark reads the DataFrame's cached `isStreaming` (observation.py:68). It now
does too, through the core's new `observe_with_streaming`.
**5. A doc note.** PySpark builds the request of `isStreaming` inside
`functools`, so the call site of the `and` origin of a join of a list of
Columns is `functools.py`'s there. The drop-in's is the user's line.
### Why are the changes needed?
So that these DataFrames give PySpark's schemas with PySpark's requests, and
unpickle as PySpark's do.
### Does this PR introduce _any_ user-facing change?
Yes, as above. Unpickling a DataFrame now sends a schema request, as
PySpark's does.
A remaining difference: after the unpickle of a cached DataFrame, `.schema`
is the pickled schema, where PySpark gives the schema fetched at the unpickle.
They differ only if the schema changed between the pickle and the unpickle.
### How was this patch tested?
- **New tests in `test_dropin_offline.py`.** Each fails on master.
- `test_the_schema_of_to_is_cached_and_copied_as_pyspark`.
-
`test_unpickled_dataframes_request_their_schema_and_keep_the_cached_one_as_pyspark`.
- `test_observe_of_an_observation_reads_the_kept_is_streaming_as_pyspark`.
-
`test_struct_fields_compare_their_metadata_as_python_compares_the_dicts_as_pyspark`.
-
`test_schema_cache_set_operations_compare_the_metadata_as_python_as_pyspark`.
Two tests are updated, as the unpickle now sends a schema request. In Rust
there are three new unit tests.
- **The reviewer's probes give the same requests on both clients.**
- `to`: 84 and 34 operations.
- Set operations: 85.
- `observe`: 9.
- Writers: 12.
- The unary methods: 234, and the actions: 93, with no regression.
- Unpickles: 40. The only difference is when the process first reads
`spark.python.sql.dataFrameDebugging.enabled`.
- **Rust.**
- `cargo test -p apache-spark-connect -p apache-spark-connect-core
--release` (1620 passed);
- `--features wasm-udf` (1330 passed);
- after the rebase on master, `cargo test -p apache-spark-connect
--release` (1326 passed).
- **Python.**
- The offline suite of `coverage.yml` on pandas 2 and 3 (6111 and 6105
passed).
- After the rebase, `test_dropin_offline.py` on pandas 3 (2367 passed).
- Live `test_dropin_e2e.py`, including foreachBatch's DataFrames on the
worker path (301 passed).
- **Lint.** `cargo fmt --all --check`, `cargo +1.100.0 clippy -D warnings`,
ruff 0.14.0, `audit_no_stubs.sh` and `scripts/tests`.
### 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]