HyukjinKwon opened a new pull request, #578:
URL: https://github.com/apache/spark-connect-rust/pull/578

   ### What changes were proposed in this pull request?
   
   This PR lets `copy.copy`, `copy.deepcopy` and `pickle` work on an 
`Observation` of the Python client, as they do on PySpark's, which is a plain 
Python class under Spark Connect 
(`pyspark.sql.connect.observation.Observation`).
   
   - `copy.copy` returns an Observation with the same name and state. A copy of 
a used Observation shares its metrics, the way PySpark's shallow copy shares 
the `_result` dict, so it also sees metrics that later actions set. An error 
reported later is set on the original only. A copy of an unused Observation can 
be used on its own.
   - `copy.deepcopy` returns a detached copy of the name and state as they are 
now.
   - Both copy the instance attributes the way a plain object's are copied; 
`deepcopy` goes through its memo.
   - Pickling uses PySpark's state, its `__dict__`: `_name`, `_result`, 
`_error` and the attributes. The class's module is now 
`pyspark.sql.connect.observation`, the module of PySpark's Connect Observation. 
`pyspark.sql.observation` imports that module, so that cloudpickle pickles the 
class by reference: it only does that for a module already in `sys.modules`.
     - A server-side Python worker, whose PySpark has no Rust client, unpickles 
PySpark's own Observation.
     - The client unpickles its own: `__setstate__` restores the name and keeps 
`_result` and `_error`, which `get` returns as PySpark's does. An unpickled 
used Observation is used, so it raises `REUSE_OBSERVATION`.
   - The core's `Observation` keeps its metrics in a list that a shallow copy 
shares. It gains hidden (`#[doc(hidden)]`) `shallow_copy()`, `detached()` and 
`state()` for the drop-in.
   
   ### Why are the changes needed?
   
   For parity with PySpark 4.3.0. All three operations raised in the client:
   
   ```python
   import copy, pickle
   from pyspark.sql import Observation, functions as F
   
   o = Observation("o")
   df = spark.range(3).observe(o, F.count(F.lit(1)).alias("n"))
   s, d = copy.copy(o), copy.deepcopy(o)
   df.collect()
   print(o.get, s.get, d.get, pickle.loads(pickle.dumps(o)).get)
   ```
   
   - PySpark 4.3.0: `{'n': 3} {'n': 3} {} {'n': 3}`
   - Before: `TypeError: cannot pickle 'pyspark.sql.observation.Observation' 
object` from `copy.copy`, and the same from `copy.deepcopy` and `pickle.dumps`
   - After: `{'n': 3} {'n': 3} {} {'n': 3}`
   
   A UDF or `foreachBatch` function that refers to an Observation also failed, 
because cloudpickle could not pickle it.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. `copy.copy`, `copy.deepcopy`, `pickle` and cloudpickle now work on an 
Observation, as in PySpark. `type(Observation()).__module__` is now 
`pyspark.sql.connect.observation`, as it is for PySpark's under Spark Connect. 
Both are unreleased behavior of this client.
   
   ### How was this patch tested?
   
   New tests, each checked to fail without the fix:
   
   - Rust: 
`a_shallow_copy_shares_the_metrics_of_a_used_observation_but_not_a_later_error`.
   - Offline, against the mock server:
     - `test_an_observation_copies_as_the_plain_object_of_pyspark`: copies of 
unused and used Observations, reuse, shared and snapshotted metrics, 
attributes, the memo.
     - `test_an_observation_pickles_as_the_plain_object_of_pyspark`: pickle and 
cloudpickle of unused, used and failed Observations; the worker-side state, 
read with a plain stand-in class; the module in `sys.modules` in a new process.
   - e2e: 
`test_an_observation_copies_and_pickles_as_the_plain_object_of_pyspark`, where 
the server's Python worker unpickles an Observation captured by a UDF.
   
   The same probe script against PySpark 4.3.0 and the client gives the same 
output for every case except `get`'s identity: `o.get is o.get` is true in 
PySpark and false here. That part is unchanged by this PR.
   
   Also ran, with `TZ=UTC` under protobuf's default (upb) backend: CI's offline 
set (`test_dropin_offline.py`, `test_arrow_handoff.py`, `test_ml_offline.py`), 
3263 passed; `test_dropin_e2e.py` against Spark 4.3.0, 274 passed and 1 
skipped; the Rust lib tests; the `--no-run` builds with and without `wasm-udf`; 
`cargo fmt`; `scripts/audit_no_stubs.sh`; and ruff 0.14.0 `check` and `format 
--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]

Reply via email to