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

   ### What changes were proposed in this pull request?
   
   Two documented gaps of the drop-in's protobuf runtime (`pyspark._pyspark`'s 
messages, which `pyspark.sql.connect.proto` installs) against PySpark 4.3.0 
with upb (protobuf 7.36.2), from a review of the documented differences.
   
   **1. The map fields of a message's constructor** take their value as upb's 
`InitMapAttributes` does. Before, only a mapping was taken, and every other 
value raised `TypeError: ... is not iterable`.
   - A map of scalars takes its value through 
`collections.abc.MutableMapping.update`, which is literally upb's `update`. So 
pairs, an iterator of pairs, lists of 2-character strings and `[]` are taken, 
and a pair of another shape raises Python's `ValueError`. A tuple is the 
arguments of `update`, as upb's `PyObject_CallMethod(map, "update", "O", 
value)` makes it: `(("a", "b"),)` raises, and `({"a": "b"},)` works.
   - A map of messages takes the keys that the value iterates, each of 
`value[key]`, so pairs fail as on upb:
     - a dict sets the fields of the key's message;
     - a message of the type is copied;
     - anything else goes through `Message.CopyFrom`, with its errors.
     The error of a key that the map refuses comes first, as upb reads the 
value first.
   - An exact dict keeps the fast path, which reads the items in Rust and keeps 
the constructor's interrupt budget steps.
   
   **2. The runtime's `DecodeError`** is now 
`pyspark.sql.connect.proto._rust.DecodeError`.
   - It subclasses the runtime's `ProtoDecodeError` and, where protobuf imports 
(either implementation), `google.protobuf.message.DecodeError`. So `except 
google.protobuf.message.DecodeError` catches a failed parse, as it catches 
upb's.
   - Its text and args are upb's: `Error parsing message with type 
'spark.connect.Plan': Wire format was corrupt`.
   - It pickles, copies and deep-copies. Before, its pickle raised `attribute 
lookup DecodeError on pyspark.sql.connect.proto failed`.
   - Without protobuf, it is a subclass of `ProtoDecodeError` only.
   
   `docs/compatibility.md` describes both. The remaining differences: 
`type(error)` is not protobuf's class, and a traceback names the runtime's.
   
   ### Why are the changes needed?
   
   So that code written for PySpark's protobuf messages works on the drop-in:
   - code that builds a `Write(options=[("k", "v")])`, or a map from an 
iterator of pairs;
   - code that catches protobuf's `DecodeError` of a failed parse.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes:
   - constructors take the map values that upb takes, with upb's errors for the 
others;
   - a failed parse raises an error that protobuf's `DecodeError` catches, and 
that pickles.
   
   ### How was this patch tested?
   
   - **New tests in `test_proto_rust_offline.py`.**
     - `test_map_fields_of_constructors_take_mappings_and_pairs_as_upbs`: 73 
cases checked against upb, each also as a nested message's map.
     - `test_parse_errors_are_protobufs_decode_errors_as_upbs`:
       - 7 corrupt inputs, each through `FromString`, `ParseFromString`, 
`MergeFromString` and `__setstate__`;
       - the `except` clauses of both implementations, the latter in a 
subprocess with `PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python`;
       - pickles at every protocol, copy and deepcopy.
     - The no-protobuf test checks the class and its pickle.
     - A map-of-scalars constructor case in the test that another thread runs 
during large operations.
   - **Probes of both clients:**
     - `pb_map.py`: 12 differences down to 1, the documented container class;
     - `map_init.py`: 115 cases, 65 differences down to 4, all documented or of 
field-name types;
     - the tuple cases, the DecodeError texts, catches and pickles, and timings 
on 262k entries (dict 0.085 s, against upb's 0.11 s).
   - **Python.** The offline suite of `coverage.yml`, and 
`test_proto_rust_offline.py` on pandas 2 and 3. Live `test_dropin_e2e.py`.
   - **Rust.** `cargo test -p pyspark-rs --lib`, linked as `rust.yml` links it.
   - **Lint.** `cargo fmt --all --check`, `cargo +1.100.0 clippy -p pyspark-rs 
--all-targets -- -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]

Reply via email to