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

   ### What changes were proposed in this pull request?
   
   `pyspark.sql.connect.proto` now always exports the messages of the drop-in's 
Rust protobuf runtime (SPARK-61755), whether or not `protobuf` is installed. 
`PYSPARK_CONNECT_PROTO_IMPLEMENTATION` and `_implementation` are removed. So 
are the vendored generated `*_pb2.py` modules, which nothing could import any 
more; their `.pyi` stubs stay. The runtime also gains what `protobuf`'s own 
modules read from a message, so that it holds up where `protobuf` is installed:
   
   - **protobuf's default pool.** With `protobuf` installed, the files of the 
protos are added to its default descriptor pool, as the generated modules add 
them. Modules that protoc generates of other protos importing them now build, 
for example Delta Lake's Connect client. `json_format` also finds the types 
inside an `Any`. protobuf's own well-known-type modules register their own 
files first, and a file the pool already has is left alone.
   - **Descriptors.** `GetOptions()` returns protobuf's `descriptor_pb2` 
options, cached as upb caches them; `json_format` and `text_format` read 
`map_entry` from it. Also added: `CopyToProto()`, `FileDescriptor.pool` (a pool 
with upb's `Find*` API and errors), service and method descriptors 
(`services_by_name`, `FindServiceByName`, `FindMethodByName`), `has_options` of 
oneofs and enum values, `EnumDescriptor.is_closed` and 
`FieldDescriptor.CPPTYPE_BYTES`. The non-upb `Descriptor.file_name`/`package` 
and `EnumDescriptor.file_name` are gone.
   - **Well-known types.** `google.protobuf.Any` has 
`Pack`/`Unpack`/`Is`/`TypeName`, which are no longer on every message class. 
`google.protobuf.Timestamp` has `ToJsonString`, `FromJsonString`, `To`/`From` 
the units since the epoch, `ToDatetime`/`FromDatetime`, `GetCurrentTime` and 
arithmetic, with protobuf 7's results and errors. A Timestamp field takes a 
`datetime` in the constructor and by assignment (`_internal_assign`), as upb's 
does.
   - **Messages.** `HasExtension`, `ClearExtension` and `Extensions` behave as 
upb's do for a message without extension ranges.
   - **Modules.** Each module of a proto has the names of its generated module 
(`_RELATION`, `_JOIN_JOINTYPE`, `_SPARKCONNECTSERVICE`, 
`spark_dot_connect_dot_expressions__pb2`, ...). It also has a module spec, 
reloads, and imports again after it is removed from `sys.modules`.
   - **`example_plugins.proto`** (Spark's example plugin, from the pinned 
v4.3.0 tag) is compiled into the proto crate, so 
`pyspark.sql.connect.proto.example_plugins_pb2` is of the runtime too. It works 
without `protobuf`, and its messages take the package's.
   - **The pipelines hook.** `pyspark._proto_fallback` is renamed 
`pyspark._proto_hooks`. It now always gives the vendored 
`add_pipeline_analysis_context` the runtime's `Any`.
   - **CI.** `coverage.yml` runs the offline suite once. The env-var pass is 
removed, and `test_create_dataframe_configs_offline.py`, which only that pass 
ran, moves into the remaining one.
   
   ### Why are the changes needed?
   
   With one implementation, the messages behave the same whether or not, and 
whichever, `protobuf` is installed (upb's limit of 100 nested messages, the 
behavior of protobuf 6 vs 7, ...). The switch only existed because using the 
Rust runtime with `protobuf` installed was not safe: plugin modules generated 
by `protobuf` could not import, and `json_format`/`text_format` failed on maps 
and on an `Any` of a Spark type. This fixes those cases.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, compared with master (unreleased): where `protobuf` is installed, 
`pyspark.sql.connect.proto` no longer gives protobuf's generated classes. One 
difference cannot be closed from this side, and `docs/compatibility.md` 
documents it. `protobuf`'s messages accept only their own classes in 
`CopyFrom`, `MergeFrom` and their constructors, so a message field of a 
protobuf-generated plugin message takes a message of 
`pyspark.sql.connect.proto` only through bytes 
(`plugin.input.ParseFromString(relation.SerializeToString())`) or a dict. 
Otherwise it raises `TypeError`. Delta Lake 4.0's `DeltaTable.delete`, `update` 
and `merge` build their plans with `CopyFrom` of PySpark's messages, so they 
raise that error here. Scans and the other read paths work. The other direction 
works: our messages take protobuf's of the same type, and an `Any` of either 
runtime packs and unpacks messages of either.
   
   ### How was this patch tested?
   
   New tests in `python/tests/test_proto_rust_offline.py`. The upb reference 
classes are built by protobuf's own builder, the way generated modules build 
them. The tests compare against upb:
   - descriptor options and `CopyToProto` of every message, field, oneof, enum, 
enum value, file, service and method;
   - the pool's lookups and its errors for unknown names;
   - `Any` and `Timestamp` methods, including invalid inputs and arithmetic;
   - `datetime` assignment to Timestamp fields;
   - the extension API;
   - `json_format`/`text_format` print and parse with maps, an `Any` of a Spark 
type and a Timestamp;
   - a protoc-style plugin file that imports `spark/connect/relations.proto` 
(it builds, takes bytes and dicts, and raises the documented `TypeError` for 
our messages);
   - `example_plugins_pb2`;
   - the module names against protobuf's builder.
   
   The no-protobuf subprocess test now also covers `example_plugins_pb2`, the 
pool and `Timestamp`. The `parse_deep` test now uses protobuf's classes, since 
it parses protobuf's messages as a server worker does.
   
   Local runs:
   - The offline suite of `coverage.yml` on protobuf 7.36.2 (upb, as CI 
installs): 5901 passed and 76 skipped. Of the 5 failures, 2 tests imported the 
removed `_any_class` helper; they are fixed and pass. The other 3 compare 
datetimes in the local time zone (KST here) and pass with `TZ=UTC`, as CI runs. 
Python coverage is 94% (floor 93%), and 99% for `_rust.py`.
   - `test_proto_rust_offline.py`: 40 passed on protobuf 7.36.2. With 
`google.protobuf` blocked, 1 passed and 39 skipped, as in the no-protobuf live 
job.
   - `cargo test -p apache-spark-connect --release --lib` (743 passed), and 
`--no-run` with and without `--features wasm-udf`.
   - `cargo fmt --all --check`, `cargo +1.100.0 clippy -p pyspark-rs -p 
apache-spark-connect-proto --all-targets -- -D warnings`, and the `pyspark-rs` 
lib tests (9 passed).
   - `scripts/audit_no_stubs.sh`, `scripts/check_vendored_upstream.sh` and ruff.
   - Delta Lake 4.0's Connect plans: a scan builds, and a delete raises the 
documented `TypeError`.
   
   ### 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