HyukjinKwon opened a new pull request, #632:
URL: https://github.com/apache/spark-connect-rust/pull/632
### What changes were proposed in this pull request?
copy.copy, copy.deepcopy and pickle of the Python client's DataFrame helpers
raised `TypeError: cannot pickle '<class>' object`. PySpark 4.3.0 copies them
as plain Python objects. A post-merge review of #610, which made DataFrame
copies match PySpark, found this sibling gap. The affected classes were
GroupedData (and the result of `pivot`), DataFrameNaFunctions,
DataFrameStatFunctions, DataFrameReader, DataFrameWriter, DataFrameWriterV2,
Catalog and RuntimeConf. A sweep of the other user-facing classes found the
same gap in:
- DataStreamReader, DataStreamWriter, StreamingQuery and
StreamingQueryManager;
- MergeIntoWriter and its WhenMatched, WhenNotMatched and
WhenNotMatchedBySource;
- PandasCogroupedOps;
- TableValuedFunction;
- DataSourceRegistration.
They now copy as PySpark's do:
- **copy.copy**: the copy holds the same DataFrame or session, as a shallow
copy of PySpark's `__dict__` does. Where it is observable, the copy shares
mutable state with the original, as PySpark's copies share their attributes:
- a writer's copy (batch, V2 and stream) shares the whole writer, so a
setting made on the copy is in the original's write;
- a reader's copy (batch and stream) shares its options, while the format,
schema and source name stay per object;
- a merge builder's copy shares its action lists, while
`withSchemaEvolution` stays per object.
- **copy.deepcopy**: deep-copies the DataFrame or session that the object
holds through the memo, with #610's machinery. So `copy.deepcopy({"df": df,
"g": df.groupBy("id")})` keeps the copied `g` on the copied `df`.
- **pickle**: works where PySpark's works, at every protocol. An unpickle
without an active session raises `NO_ACTIVE_SESSION`, as PySpark's does. A
stream writer's pickle keeps its `foreach`/`foreachBatch` functions.
- **Where PySpark's copies fail, these fail too:**
- deep copies and pickles of RuntimeConf, the merge builders and their
actions raise `TypeError: cannot pickle '_thread._local' object`;
- StreamingQueryManager's raise `cannot pickle '_thread.lock' object`.
The Rust core gets the accessors that the copies need: the parts of a
GroupedData, the settings of the readers and writers, and the parts of a
StreamingQuery. They are doc-hidden and not a stable API. The `spark_session()`
accessors of the readers are public, like the writer's existing one.
### Why are the changes needed?
For parity with PySpark 4.3.0: code that copies or deep-copies an object
holding a writer, a grouping or the catalog failed on the drop-in.
### Does this PR introduce _any_ user-facing change?
Yes. These objects can be copied, deep-copied and pickled, as in PySpark,
where they raised `TypeError`.
A drop-in pickle of these objects names the drop-in's own reconstructors. So
it unpickles only where the drop-in is installed, unlike a DataFrame's pickle.
### How was this patch tested?
- New offline tests in `python/tests/test_dropin_offline.py`, with the
expected outcome of each case taken from the reference client:
- copy, deepcopy and pickle at every protocol of each object;
- unpickles without an active session;
- the memo identity of a deep copy of grouped data;
- the shared settings of the copies of the writers and readers;
- the per-object schema evolution of the merge builder.
Of the first 161 cases, 129 failed before the change; all pass after.
- StreamingQuery cannot be built offline. Its copy, deepcopy and pickle were
checked against the reference client on a live Spark 4.3.0 Connect server.
- Results:
- `tests/test_dropin_offline.py` and `tests/test_ml_offline.py`: 2337
passed, 8 skipped, after rebasing on master;
- the reader, writer, group, catalog, conf, stream, merge and TVF tests of
`tests/test_dropin_e2e.py`, against a live server: 98 passed;
- `cargo test -p apache-spark-connect -p apache-spark-connect-core --lib`:
679 and 220 passed;
- `cargo clippy --workspace --all-targets -- -D warnings` and `cargo fmt
--check` pass, as do `scripts/audit_no_stubs.sh` and `ruff check`.
### 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]