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

   ### What changes were proposed in this pull request?
   
   Since #814 (SPARK-61940), the drop-in's `PlanObservedMetrics` holds the 
`Expression.Literal` messages that the server sends, as PySpark's does. 
Protobuf pickles such a message as its `_InternalConstructMessage` of the 
message's full name (`spark.connect.Expression.Literal`), which loads only once 
the files of the protos of Spark Connect are in protobuf's default pool.
   
   PySpark's `pyspark.sql.connect.client.core` imports 
`pyspark.sql.connect.proto` (`core.py:90`). A pickle of `PlanObservedMetrics` 
imports that module before it makes the messages, so in PySpark it loads in a 
new process. An example is one in the `attrs` of a pandas DataFrame of 
`toPandas` of an observed DataFrame. The drop-in's `core` did not import the 
package, so such a pickle failed in a new process.
   
   Checked with the pickle of a pandas DataFrame of pyspark-client 4.3.0's 
`spark.range(3).observe(Observation("o"), ...).toPandas()`, and with a bare 
pickle of `PlanObservedMetrics`, each loaded in a new process:
   
   | | pyspark-client 4.3.0 | drop-in before |
   |---|---|---|
   | `pd.read_pickle(path).attrs["observed_metrics"]` (upb) | `[Plan 
observed(o=[long: 3\n])]` | `KeyError: "Couldn't find message 
spark.connect.Expression.Literal"` |
   | the same with `PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python` | the same | 
`KeyError: 'spark.connect.Expression.Literal'` |
   | the drop-in's own pickle of it | loads | loads |
   
   The change:
   - `pyspark/sql/connect/client/core.py` imports `pyspark.sql.connect.proto`, 
as PySpark's does. Only an explicit import of `client.core` and the unpickling 
of its classes import that module, so a session does not import the package any 
earlier.
   - `docs/compatibility.md` says when upb's pickles of messages load: where 
`protobuf` is installed, once `pyspark.sql.connect.proto` is imported, as in 
PySpark. So a pickle of PySpark's `PlanObservedMetrics` loads in a new process.
   - Two doc comments in `crates/pyspark-rs/src/dataframe.rs` are corrected:
     - `execution_metrics`: PySpark's `_build_metrics` makes the `PlanMetrics`, 
and `_execute_and_fetch_as_iterator` makes the `PlanObservedMetrics`;
     - `unsupported_literal_marker`: since SPARK-61940, its marker is that of 
the metrics of an Observation, which `check_observed_metrics` refuses before 
they are converted, not of `PlanObservedMetrics.pairs`.
   
   ### Why are the changes needed?
   
   So that PySpark's pickles of the observed metrics of an execution, such as 
those of a pandas DataFrame of `toPandas`, load in the drop-in as they do in 
PySpark.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. A pickle of PySpark's `PlanObservedMetrics`, or of an object that holds 
one, loads in a new process of the drop-in, where protobuf is installed. 
Importing `pyspark.sql.connect.client.core` imports 
`pyspark.sql.connect.proto`, as in PySpark.
   
   ### How was this patch tested?
   
   - **Probes:** a pickle of pyspark-client 4.3.0's `PlanObservedMetrics`, bare 
and in the `attrs` of a pandas DataFrame of `toPandas`, loads in a new process 
of pyspark-client 4.3.0 and, after this change, of the drop-in, with either 
implementation of protobuf.
   - **New test:** 
`test_the_pickles_of_pysparks_observed_metrics_load_in_a_new_process_as_pyspark`
 in `test_dropin_offline.py`. It loads a pickle of PySpark's 
`PlanObservedMetrics`, made by pyspark-client 4.3.0 and kept as bytes, in a new 
process, with upb and with the pure-Python implementation of protobuf. It 
checks the class, the keys, the `pairs`, the `repr` and the class of the 
metric. It fails without the change and passes with it.
   - `cargo test -p pyspark-rs --lib`: 13 passed.
   - The offline suite of `coverage.yml`: 6260 passed, 77 skipped.
   - `test_dropin_e2e.py` against a Spark 4.3.0 server: 310 passed.
   - **Lint and guards:**
     - `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`;
     - ruff 0.14.0, `test_ruff_excludes.py`, `scripts/audit_no_stubs.sh`;
     - `scripts/gen_type_stubs.py --check` and `scripts/gen_fn_dispatch.py 
--check` against the v4.3.0 source, and `test_type_stubs_offline.py` with mypy 
2.4.1: OK, and 7 passed;
     - the guards of `coverage.yml` and `build_python_connect.yml`.
   
   ### 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