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

   ### What changes were proposed in this pull request?
   
   A follow-up of #822 (SPARK-61958) and #854 (SPARK-62010), from a review of 
the drop-in's Rust protobuf runtime (`pyspark.sql.connect.proto`, 
`crates/pyspark-rs/src/pbmsg`) against upb (protobuf 7.36.2), the 
implementation that pyspark-client 4.3.0 uses. Each item was probed on both 
clients.
   
   **1. `==` compares floats and doubles by their bits** (`Scalar::eq_as_upb`, 
used by `slot_eq` and `MapContainer::equal`).
   - upb's `==` of two messages (`upb_Message_IsEqual`) compares a float or a 
double field by its bits, as it compares an integer. The runtime compared their 
values, as Python's `==` does.
   - Before:
     - a message of a NaN never equaled its copy or its parse;
     - `in` and `remove` of a container missed it;
     - -0.0 equaled 0.0, and equaled the unset default of a field without 
presence.
   - Now, as upb's: a NaN equals a NaN of the same bits, -0.0 is neither 0.0 
nor the default, and the same holds for repeated fields and map values.
   - `==` of the containers and maps themselves still compares their values as 
Python's `==` does, as upb's list and `Mapping` comparisons do. So a repeated 
field or a map of a NaN is not equal to itself, on both clients.
   
   **2. `==` compares unknown fields by their contents** 
(`codec::unknown_fields_eq`).
   - upb (`upb_Message_UnknownFieldsAreEqual`) compares the unknown fields of 
two messages by their fields: their tags (number and wire type) and their 
values, stably sorted by tag. Groups are sorted too.
   - So for upb, neither the order of fields of other tags nor the encoding of 
a varint counts. A length-delimited field is compared by its bytes.
   - The runtime compared the bytes, so the same unknown fields in another 
order were unequal. Probed: 14 cases, including groups, nested groups, wire 
types and 10-byte varints.
   
   **3. Only `protobuf`'s messages of the type of its default pool are taken** 
(`is_protobufs_message_of` in `message.rs`, and `verify_message` in 
`containers.rs`).
   - `CopyFrom`, `MergeFrom`, the constructors (a message field, a repeated 
field or a map value) and the containers (`append`, `extend`, `insert`, and a 
map's `MergeFrom`) took any object that has the `DESCRIPTOR.full_name` of the 
type and a `SerializeToString`, by its bytes. That included `protobuf`'s 
message of a type of the same name of another pool, and a lookalike.
   - upb takes a message of its class only. It refuses those with the errors of 
another class: `Parameter to CopyFrom() must be instance of same class: ...`, 
`Parameter to initialize message field must be dict or instance of same class: 
...`, and `Expected a message object, but got ...` from `append` of a lookalike.
   - They now take a message of `protobuf`'s only when it is an instance of 
`google.protobuf.message.Message` of the type of `protobuf`'s default pool. 
That pool holds the files of the runtime, so its message is of the runtime's 
type.
   - `==` takes the same check, and it already compared messages of other pools 
unequal (#822).
   - The containers take a message of `protobuf`'s, of any type, before its 
merge refuses one of another type, as upb's `append` checks it.
   
   **4. `==` of two maps is upb's `Mapping.__eq__`** 
(`MapContainer::equal_as_dicts`).
   - upb's `==` of two maps compares the dicts of their items. So a map equals 
a map of other key or value types of equal keys and values: a plugin's maps of 
int32 keys and of int64 or bool keys, `{1: "t"}`. A map of a NaN value equals 
no map, itself included.
   - The runtime compared such maps unequal, and a map of a NaN equal to itself.
   - Two maps of the same types and of no floats are still compared in place.
   
   **5. Docs.**
   - `docs/compatibility.md` describes the above.
   - `docs/compatibility.md` and `dev/parity/known-differences.md` now give the 
reason why a plugin's file that imports no proto of Spark Connect keeps 
`protobuf`'s classes (Delta Lake's `base.proto`):
     - when `protobuf`'s builder builds such a file, the drop-in cannot know 
that a plugin's file will import it later (Delta Lake's 
`delta/connect/proto/__init__.py` imports `base_pb2` first);
     - giving every proto3 module the runtime's classes would change the 
classes of unrelated libraries.
   
   ### Why are the changes needed?
   
   So that `==`, `in`, `remove` and the type checks of the messages give upb's 
results:
   - a message of a NaN equaled no copy of itself;
   - messages of the same unknown fields in another order were unequal;
   - a message of another pool, or an object that only looks like a message, 
was taken by its bytes, so a same-named type of another definition was silently 
read as the runtime's.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, as upb behaves:
   
   | | pyspark-client 4.3.0, and now | before |
   |---|---|---|
   | `m = Expression.Literal(double=nan); m == copy.deepcopy(m)` | `True` | 
`False` |
   | `Sample(lower_bound=-0.0) == Sample()` | `False` | `True` |
   | two Literals of the unknown fields 501 and 502, in the other order | equal 
| unequal |
   | `StorageLevel().CopyFrom(<protobuf's StorageLevel of another pool>)` | 
`TypeError: Parameter to CopyFrom() must be instance of same class: ...` | 
copied |
   | `Expression.Literal.Array().elements.append(<a lookalike>)` | `TypeError: 
Expected a message object, but got <...>.` | appended |
   | a plugin's `m.i32 == m.i64`, both `{1: "t"}` | `True` | `False` |
   | `m.d == m.d` of a map of a NaN value | `False` | `True` |
   
   ### How was this patch tested?
   
   - **New tests in `python/tests/test_proto_rust_offline.py`.** They compare 
against upb, and all of them fail on master.
     - `test_equality_of_messages_compares_floats_of_their_bits_as_upbs`: 12 
cases on both sides of `==` and `!=`, against a copy and a parse:
       - NaNs of the same and of other bits, and -0.0;
       - floats and doubles, of fields with and without presence;
       - repeated fields, a map of messages, and a nested message.
   
       It also covers `in` and `remove` of a container, and `==` of the 
containers themselves.
     - `test_unknown_fields_of_messages_compare_as_upbs`: 14 cases, between 
messages of the runtime, between the runtime's and `protobuf`'s, and in a 
message that a message holds.
     - 
`test_messages_take_only_protobufs_messages_of_their_type_of_its_default_pool_as_upbs`:
 `CopyFrom`, `MergeFrom`, `CopyFrom` of a field, the constructors of a field, 
of a repeated field and of a map value, `append`, `extend`, `insert`, a map's 
`MergeFrom`, `==`, `!=` and `in`. Each is run with a message of another pool 
and with a lookalike, and gives upb's outcomes and errors. The map cases give 
upb's `CopyFrom` error through the map init of #844.
     - `test_maps_of_integer_keys_and_of_doubles_compare_as_upbs`: a 
plugin-like file of maps of int32, int64, uint32 and bool keys and of doubles. 
It comes at the end of the file, as the file adds its classes to those of the 
runtime, which earlier tests compare with upb's one by one.
   - **Probes of both clients:** the float, unknown-field, other-pool, 
lookalike and map cases. Apart from the documented differences (the names of 
the types), they give upb's results.
   - **Rust 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`;
     - `cargo test -p pyspark-rs --lib`, linked as `rust.yml` links it: 13 
passed.
   - **Python gates:**
     - ruff 0.14.0 `check` and `format --check`, `test_ruff_excludes.py` and 
`scripts/audit_no_stubs.sh`;
     - `gen_type_stubs.py --check` and `gen_fn_dispatch.py --check` against the 
v4.3.0 source, and the guards of the offline, e2e and live Rust test lists;
     - `test_type_stubs_offline.py` with mypy 2.4.1: 7 passed.
   - **The offline suite of `coverage.yml`:** 6390 passed and 77 skipped, the 
skips of master.
   - **`test_dropin_e2e.py` against a Spark 4.3.0 server:** 320 passed.
   
   ### 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