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

   ### What changes were proposed in this pull request?
   
   PySpark 4.3.0's `pyspark.sql.connect.observation.Observation` is a plain 
object of `_name`, `_result` and `_error`:
   - `_on` sets `_result = {}`, and `get` returns that dict. The client updates 
it in place with `observations[name]._result.update(...)` (`client/core.py`, 
`_execute_and_fetch_as_iterator`).
   - Its `CollectMetrics` plan holds the Observation itself 
(`connect/plan.py:1803-1837`).
   - The client asserts that the Observation of the name of observed metrics is 
used (`assert observation_result is not None`).
   
   The drop-in differed in each. Checked on both clients against a Spark 4.3.0 
server:
   
   | | pyspark-client 4.3.0 | drop-in before |
   |---|---|---|
   | `o.get is o.get` | True | False |
   | a dict read before the action, after `collect()` | `{'n': 3}` | `{}` |
   | `o.get["extra"] = 1`, then `o.get` | `{'n': 3, 'extra': 1}` | `{'n': 3}` |
   | `o._result`, `o._error` | None, None, then the dict | `AttributeError` |
   | `df._plan.observations` once a subclass Observation with an attribute is 
dropped | the object, of its subclass and attribute | a new `Observation`, 
without the attribute |
   | `client.to_table(plan, {"x": Observation("x")})` | `AssertionError` | sets 
nothing |
   
   The change:
   - **`get` is the live `_result` dict.**
     - The core's `Observation` gets a doc-hidden `MetricsMirror`, which a 
binding sets once on the metrics of a used Observation. Each update of the 
metrics is told to it, once the locks of the Observation are released.
     - The Python Observation's mirror is its result dict, made of the metrics 
on the first read. `get`, `_result` and the pickle state give that dict, and 
the executions update it in place.
     - A shallow copy shares it, as the metrics are shared. A deep copy has a 
deep copy of it, of the memo.
     - `_result` is None before the Observation is used, and `_error` is the 
error that `get` raises.
   - **The plan holds the Observation.**
     - The clone of the core state that the plan of `observe` holds keeps the 
Python Observation alive (`Observation::with_owner`), so `_plan.observations` 
gives that object, of its class and attributes. The deep copy of a DataFrame 
holds the copied Observation.
     - The DataFrame reports it to Python's garbage collector 
(`DataFrame::exclusive_observation_owners`, used by 
`PyDataFrame.__traverse__`), as it reports the relation owners and the UDF 
functions of its plan. So a cycle through an Observation that keeps its 
DataFrame is collected.
   - **An unused Observation given to an execution.**
     - The core's `set_observations` raises a new builtin kind, 
`BuiltinAssertionError`, of no message, for the metrics of an Observation that 
is not used, before their check, as PySpark's assertion comes before the 
conversion. The metrics after it are not set.
     - The binding raises it as Python's `AssertionError()`, with the 
`_operation_id` of the execution, as PySpark's `_handle_error` sets it.
   - **`docs/compatibility.md`** corrects the paragraph on 
`df._plan.observations` and describes `get`, `_result` and `_error`.
   
   ### Why are the changes needed?
   
   So that the Observations of the drop-in are PySpark's:
   - a dict held from `get` has the metrics once the action ran;
   - the Observations of a plan are the user's objects;
   - `client.to_table` raises PySpark's error for an Observation that is not 
used.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, as PySpark 4.3.0:
   - `Observation.get` is one dict, which the executions update in place;
   - `_result` and `_error` exist;
   - `df._plan.observations` gives the Observations that `observe` was given, 
which the DataFrame keeps alive, as PySpark's plan does;
   - `client.to_table`, `to_pandas` and the other executions of the client 
raise `AssertionError` for an Observation that is not used, given for the name 
of observed metrics.
   
   What stays different, documented:
   - `_error` is a new exception of the error at each read, where PySpark's is 
the one that its client set.
   - `_name`, `_result` and `_error` are attributes of the class, so `vars()` 
of an Observation does not show them, and they cannot be set.
   
   Also, a cycle through the result dict of an Observation, such as an 
Observation set in its own result dict, is not collected.
   
   ### How was this patch tested?
   
   - **New tests:**
     - `test_observations_are_pysparks_objects_of_a_live_result` in 
`test_dropin_offline.py`, on the mock server, checks:
       - the identity and the updates of `get`, `_result` and `_error`;
       - the shallow copy, the deep copy and the pickle of the result;
       - the subclass and attribute of an Observation that only the plan holds, 
and that a cycle through it is collected;
       - the `AssertionError` of an unused Observation, of no arguments and of 
an operation ID.
       
       It fails on master.
     - The same test in `test_dropin_e2e.py`, whose body passes on 
pyspark-client 4.3.0 too.
     - Core tests:
       - `a_mirror_is_set_once_of_the_metrics_and_told_of_each_update`;
       - `an_owner_is_kept_by_its_clone_and_its_clones_only`;
       - `the_metrics_of_an_unused_observation_are_pysparks_assertion_error`;
       - `exclusive_observation_owners_are_those_only_this_dataframe_keeps`;
       - `a_builtin_assertion_has_no_message_and_keeps_its_operation_id`.
   - **Probes of both clients** (`p_obs_get.py`, `p_obs_live_e2e.py`) give 
identical output for the rows above.
   - **The gates:**
     - `cargo fmt --all --check`;
     - `cargo +1.100.0 clippy -p apache-spark-connect-core -p 
apache-spark-connect -p pyspark-rs --all-targets -- -D warnings`;
     - `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps -p apache-spark-connect 
-p apache-spark-connect-core -p apache-spark-connect-macros -p 
apache-spark-connect-build --features apache-spark-connect/wasm-udf`;
     - in debug, as the coverage job builds them:
       - `cargo test -p apache-spark-connect-core --lib`: 242 passed;
       - `cargo test -p apache-spark-connect --lib`: 795 passed;
       - `cargo test -p apache-spark-connect --no-run`: all the integration 
tests compile;
     - `cargo test -p pyspark-rs --lib`, linked as `rust.yml` links it: 13 
passed;
     - ruff 0.14.0, `test_ruff_excludes.py`, `audit_no_stubs.sh`, the stub and 
dispatch generators, the guards and `test_type_stubs_offline.py` (7 passed, 2 
skipped);
     - the offline suite of `coverage.yml`: 6336 passed, 77 skipped;
     - `test_dropin_e2e.py` against a Spark 4.3.0 server: 320 passed.
   - The probe of the kd-peer review (`p_observations.py`) gives identical 
output on pyspark-client 4.3.0 and with this change.
   
   ### 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