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

   ### What changes were proposed in this pull request?
   
   A follow-up of #785 (SPARK-61867) and #805 (SPARK-61925), from a review of 
them against upb (protobuf 7.36.2) and pyspark-client 4.3.0.
   
   Since #785, a Spark Connect plugin's generated modules that import Spark 
Connect's protos, such as Delta Lake 4.0's, get the classes of the drop-in's 
Rust protobuf runtime. upb has one class for each type of a pool. The drop-in 
has a second class for the same types, protobuf's own:
   - the module of a plugin's file that imports no proto of Spark Connect keeps 
protobuf's classes (Delta Lake's `base.proto`);
   - protobuf's own lookups give protobuf's classes: `message_factory`, 
`symbol_database`, and upb's pickles of nested messages.
   
   **1. `==` compares a message of the runtime with protobuf's message of its 
type by their contents, as upb's do** 
(`crates/pyspark-rs/src/pbmsg/message.rs`). Before, a message of the runtime 
never equaled a message that is not the runtime's, whatever its contents.
   
   Checked on both clients, with upb and with pure-Python protobuf. Here `t` is 
a `DeltaTable` message, and `e` is a message of the runtime whose fields `ks` 
and `kmap` are of a type `K` of a file that keeps protobuf's classes:
   
   | | pyspark-client 4.3.0 | drop-in before | this PR |
   |---|---|---|---|
   | Delta 4.0.0: `DeltaScan(t).to_delta_relation(c).scan.table == t` | True | 
False | True |
   | Delta 4.0.0: 
`DescribeHistory(t).to_delta_relation(c).describe_history.table == t` | True | 
False | True |
   | `e.ks[0] == K()`, `K() in e.ks`, `e.kmap["a"] == K()` | True | False | 
True |
   | `e.ks.remove(K())` | removes it | `ValueError: remove(x): x not in 
container` | removes it |
   | a message of the module's class `==` the same message of protobuf's 
`message_factory` class, or of upb's pickle of a nested message | True | False 
| True |
   
   How it works:
   - `__richcmp__` takes an `other` that is not a message of the runtime when 
its `DESCRIPTOR` is the message's type, of protobuf's default pool. That pool 
holds the files that the runtime holds.
   - It converts `other` with `message_from_value`, as a message field takes 
it: by its bytes, with a full interrupt budget, as for a parse. Then it 
compares the two with `content_eq`.
   - protobuf's `==` gives `NotImplemented` for the runtime's message, so 
Python falls back to the runtime's `==`. So both orders work, and so do `!=`, 
`in`, `remove` and the `==` of the containers and maps.
   - These are still not equal:
     - a message of a type of another pool, as for upb's messages of its other 
class;
     - a class, a dict, None and any other object.
   - `==` of two messages of the runtime is unchanged.
   
   **2. A message field takes protobuf's message of its type before any mapping 
other than a dict** (`message_from_value`).
   - Before, any object with `keys` was taken for a mapping before the check 
for a message of the type.
   - `Expression.Literal.Map` has a field named `keys`. So 
`Expression.Literal(map=<protobuf's Literal.Map>)` raised `AttributeError: 
items`, where upb builds it (bytes `ba01081a02300122023002` on both clients 
now). `CopyFrom` already took it.
   - Now a dict comes first, then a message of protobuf's of the type, then any 
other mapping.
   
   **3. The docs say which modules keep protobuf's classes, and how those 
classes take the runtime's messages** (`docs/compatibility.md`, 
`dev/parity/known-differences.md`, `AGENTS.md`, and the docstring of 
`_hook_protobuf_builder`). These behaviors are unchanged; tests now pin them:
   - **Kept modules that import a runtime module.** A module keeps protobuf's 
classes when its file is proto2 or of editions, or imports a well-known type 
other than `Any` and `Timestamp`. If it imports a file whose module has the 
runtime's classes (the plugin's proto3 file that imports Spark Connect's 
protos), it takes that module's messages only through their bytes or dicts. upb 
takes them as they are, and so did the drop-in before #785, when all of a 
plugin's classes were protobuf's. The docs said only "the package's messages 
... as before", and the "as before" is gone.
   
     | | pyspark-client 4.3.0 | drop-in |
     |---|---|---|
     | `B(a=A(name="n"))`: B is proto3 and imports A's file and 
`duration.proto`; A's file imports Spark Connect | `0a0312016e` | `TypeError: 
Parameter to initialize message field must be dict or instance of same class: 
expected <class 'xplug.a_pb2.A'> got <class 'xplug.a_pb2.A'>.` |
     | the same with B of proto2, or of edition 2023 | `0a0312016e` | 
`TypeError` |
   - **Copying back into a kept class.** The class of a file that keeps 
protobuf's classes takes the runtime's message of its type, which a runtime 
field gives, only through its bytes or a dict: 
`base_pb2.DeltaTable().CopyFrom(relation.scan.table)` raises `TypeError`. The 
two now compare equal.
   - **Modules of protoc before 3.20.** Such a module (protoc 3.19, 
grpcio-tools 1.44 and earlier) makes its classes with 
`reflection.GeneratedProtocolMessageType`, not with protobuf's builder, which 
the drop-in wraps. So it keeps protobuf's classes. Its fields take the 
package's messages only through their bytes or dicts, where upb takes 
PySpark's. The docs said the builder is "the function the generated modules 
call".
   - **GIL.** `==` of two messages of the runtime holds the GIL, as before. 
`==` with protobuf's message parses that message first, as a field does when it 
takes one.
   
   Left out: giving the runtime's classes of `Duration`, `Struct`/`ListValue` 
and `FieldMask` protobuf's well-known-type methods, so that files that import 
those types could hold the runtime's classes. That needs those files in the 
runtime's pool, and the mixins' semantics against the runtime's containers 
(`Struct.__setitem__`, `get_or_create_list`, `_internal_assign` and 
`_internal_compare`, `FieldMask.MergeMessage`). It is a larger change than this 
follow-up.
   
   ### Why are the changes needed?
   
   - A message that a plugin's runtime field gives (Delta Lake's `scan.table`) 
never equaled the message it was built from. Neither did the messages of 
protobuf's own lookups and of PySpark's pickles. Containers missed them in `in` 
and `remove`. upb compares all of these by contents. This regressed with #785.
   - A constructor refused protobuf's messages of a type that has a field named 
`keys`.
   - The docs promised more than the drop-in does for some plugin modules: 
those that import a module with the runtime's classes, and those of protoc 
before 3.20.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes:
   - `==` and `!=` between a message of `pyspark.sql.connect.proto` (or of a 
plugin's runtime class) and protobuf's message of its type of the default pool 
compare their contents, as upb's do. So do `in`, `remove` and `==` of the 
containers and maps. They were always unequal before.
   - `Expression.Literal(map=<protobuf's Literal.Map>)`, and any field given 
protobuf's message of a type with a field named `keys`, now builds as upb's, 
where it raised `AttributeError`.
   - The docs describe the plugin modules that keep protobuf's classes.
   
   ### How was this patch tested?
   
   - **New tests in `test_proto_rust_offline.py`.** All of them fail on 
2bee7dc9's build (4 failed) and pass with this change.
     - 
`test_messages_of_a_type_of_protobufs_classes_and_of_the_runtimes_compare_as_upbs`:
       - a kept type held by a runtime field, `message_factory` classes of the 
protos and of a plugin's file, on either side of `==` and `!=`;
       - `in`, `remove` and `==` of a repeated field and a map;
       - messages of another pool, a class, a dict, None and a string, which 
stay unequal;
       - `Expression.Literal(map=<protobuf's Map>)` against upb's bytes.
     - 
`test_modules_that_keep_protobufs_classes_take_the_runtimes_messages_through_bytes`:
 the proto3-with-Duration, proto2 and edition 2023 files that import a file 
with the runtime's classes refuse its messages in constructors, `CopyFrom` and 
`MergeFrom`, and take them through bytes and dicts, where upb's take them as 
they are.
     - `test_modules_of_protoc_before_3_20_keep_protobufs_classes`, with the 
new test data `python/tests/plugin_protos/old_style.proto` and 
`old_style_pb2.py`. The module is written in protoc 3.19's form (that of 
protobuf 3.19.6's own `timestamp_pb2.py`), from the descriptor that protoc 33.0 
gives of the proto, with the import rewrite of the other test modules. 
`pyproject.toml`'s comment of the ruff exclude names it.
     - 
`test_generated_modules_of_plugins_build_with_protobufs_pure_python_implementation`
 now also checks equality under pure-Python protobuf.
   - **Probes of both clients, upb and pure-Python protobuf:**
     - with Delta Lake 4.0.0's own modules: `DeltaScan`, `DescribeHistory` and 
copying back into `base_pb2.DeltaTable`, in both import orders;
     - the `B`/`C`/`D` and `E`/`K` shapes above, and the `keys` field.
     - Apart from the behaviors documented as different, this PR's cases now 
give the official client's results. The only other differences are pure-Python 
protobuf's own error texts, which the runtime does not follow, as it follows 
upb's.
   - **Python:**
     - `test_proto_rust_offline.py`: 85 passed on upb, with pandas 2 and with 
pandas 3; 24 passed and 61 skipped with pure-Python protobuf; 24 passed and 61 
skipped without protobuf.
     - The offline suite of `coverage.yml` on upb: 6252 passed and 77 skipped 
(`test_type_stubs_offline.py` skips without mypy there, and runs below).
     - `test_type_stubs_offline.py` with mypy 2.4.1: 7 passed.
   - **Rust:** `cargo test -p pyspark-rs --lib`, linked as `rust.yml` links it: 
13 passed.
   - **Lint and guards:**
     - `cargo fmt --all --check`, `cargo +1.100.0 clippy -p pyspark-rs 
--all-targets -- -D warnings`, and `RUSTDOCFLAGS="-D warnings" cargo doc 
--no-deps -p apache-spark-connect -p apache-spark-connect-core`;
     - ruff 0.14.0 (`check` and `format --check`), `test_ruff_excludes.py`, 
`scripts/audit_no_stubs.sh` and `scripts/tests`;
     - `scripts/gen_type_stubs.py --check` and `scripts/gen_fn_dispatch.py 
--check` against the v4.3.0 source;
     - the offline-suite guard of `coverage.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