HyukjinKwon opened a new pull request, #785:
URL: https://github.com/apache/spark-connect-rust/pull/785
### What changes were proposed in this pull request?
Since #765 (SPARK-61786), `pyspark.sql.connect.proto` exports the messages
of the drop-in's Rust protobuf runtime. A Spark Connect plugin's Python client
has its own protoc-generated modules, which import Spark Connect's protos, such
as delta-spark 4.0.0's `delta.connect.proto`. Those modules built their
messages with protobuf's classes, whose fields of Spark Connect's types refuse
the drop-in's messages. So `DeltaTable.delete`, `update` and `merge` raised
`TypeError` at
`relation.delete_from_table.target.CopyFrom(self._target.plan(session))`, where
the official client works.
With this PR, such a module gets the classes of the Rust runtime. The plans
and commands of Delta Lake 4.0.0 are byte-identical on both clients, and so are
the requests that they send.
**The Rust runtime registers files at runtime**
(`crates/pyspark-rs/src/pbmsg/pool.rs`).
- `register()` adds a batch of files all or nothing, after checking that
each is proto3, that its imports are known, that it declares no name already
taken, and that its field types resolve.
- Registering is idempotent by file name. The lookups of types fall back to
the registered files, and so does `FindFileContainingSymbol` (`descriptors.rs`).
- The registered descriptors live as long as the process, as protobuf's pool
never removes files.
- The runtime computes the default JSON name of a field that declares none.
protoc leaves it out of generated modules, so `json_format` of a plugin's
messages was wrong without it.
**The hook** (`python/pyspark/sql/connect/proto/_rust.py`). `install()`
wraps protobuf's `builder.BuildTopDescriptorsAndMessages`.
- For a file whose imports include Spark Connect's protos, the wrapper calls
protobuf's builder, registers the file and its imports in the Rust runtime, and
sets the runtime's message classes and enum wrappers in the module.
- A lock serializes concurrent builds. A reloaded module gets the same
classes, as on upb, and installing again keeps one layer of the wrapper.
- Only that function is wrapped. `BuildMessageAndEnumDescriptors` binds the
`_FOO` descriptors, to which the generated code of protobuf's pure-Python
implementation sets attributes. So the module's `DESCRIPTOR`, its `_FOO`
descriptors, its extensions and its services stay protobuf's.
- Some files keep protobuf's classes, as before:
- files of other pools than protobuf's default one;
- files that are not proto3, or that import one, as the runtime does not
model proto2 or editions;
- files that import a `google.protobuf` file other than `Any` and
`Timestamp`, as the Rust classes lack the methods of the others.
A file that does not import Spark Connect's protos, such as Delta's
`base.proto`, stays protobuf's, and its messages are set into the runtime's
fields of their type.
**The placeholders of DataFrames in a plugin's message**
(`crates/spark-connect/src/plan.rs`).
- Delta's `merge` copies `source._plan.plan(session)` into its
`MergeIntoTable`. In the drop-in, that is a placeholder of the DataFrame's
relation, which the core replaces when it sends the plan. The core replaced
placeholders only where `pyspark.ml` puts them, never inside the `Any` of an
extension, so it sent the placeholder to the server.
- `resolve_relation` and `resolve_command` now also replace the placeholders
that the message of an extension holds. The core does not know the plugin's
types, so the walk only descends into encodings that contain the placeholder's
type URL and parse as messages, and replaces only the encodings that are
exactly a placeholder.
`docs/compatibility.md` (the Delta Lake paragraph),
`dev/parity/known-differences.md`, `AGENTS.md` and the docstring of
`pyspark.sql.connect.proto` describe this.
### Why are the changes needed?
So that plugins of Spark Connect whose Python clients build their plans with
their own generated modules, such as Delta Lake's `DeltaTable.delete`, `update`
and `merge`, work on the drop-in as on the official client. This is PR-F of the
handoff of the remote session.
### Does this PR introduce _any_ user-facing change?
Yes:
- the generated modules of proto3 plugins that import Spark Connect's protos
get the drop-in's message classes, so their messages take the drop-in's;
- Delta Lake's `delete`, `update` and `merge` work;
- the `json_format` of their messages gives the default JSON names.
Documented differences that remain:
- on the drop-in, a message that holds `df._plan.plan(session)` holds the
placeholder until the core sends it, so its bytes before the send differ from
the official client's;
- a message of a type of a file that stays protobuf's, such as Delta's
`relation.scan.table`, is of the runtime's class inside a message of the
runtime. So `isinstance` with that module's class is False, and it pickles only
inside its parent;
- placeholders inside the extensions of expressions are not replaced; Delta
does not need them.
### How was this patch tested?
- **Test data.** `python/tests/plugin_protos/`:
- `plugin.proto`, a plugin that imports Spark Connect's protos, of the
shapes of Delta's relations: a `DeleteFromTable`, a scan, assignments, a
`MergeIntoTable` with nested actions, an enum and a map;
- `base.proto`, which imports nothing of Spark, as Delta's `base.proto`.
The `*_pb2.py` modules are protoc 33.0's output with Spark's
`gen-protos.sh` import rewrite. `pyproject.toml` excludes them from ruff.
- **New tests.**
- `test_proto_rust_offline.py`, with a `plugin` fixture: the runtime's
classes, their messages and errors as upb's, the pure-Python implementation of
protobuf in a subprocess, threads, and the files that keep protobuf's classes.
The file has 51 passed, with pandas 2 and 3.
- `test_dropin_offline.py`: the plan or command of a plugin over a
DataFrame sends the DataFrame's relation.
- Rust: registration and its idempotency, refused files adding nothing,
and the JSON names against upb's cases in `pool.rs`; the placeholders of
relation and command extensions in `plan.rs`.
- **Probes of both clients**, with Delta Lake 4.0.0's own generated modules
and `plan.py`, not committed as they are third-party code:
- the relations (DeleteFromTable, UpdateTable, MergeIntoTable,
DescribeHistory, ConvertToDelta) and the commands (CreateDeltaTable, Vacuum,
Generate) match in bytes, `text_format`, `json_format`, `str()`, round trips
and `isinstance`;
- the requests of `forPath(...).delete` and `update`, and of
`.alias("t").merge(df.alias("s"), ...).execute()`, match on the mock server,
after renumbering the plan IDs;
- 3 runs of 6 rounds of 32 threads building at once gave no errors and one
class per shared type.
- The offline suite of `coverage.yml`, `test_ml_offline.py`, and
`test_proto_rust_offline.py` without protobuf, where the hook is not installed.
- Rust:
- `cargo test -p apache-spark-connect --release --lib` (745 passed) and
`cargo test -p pyspark-rs --lib` (12 passed);
- `--no-run` with and without `wasm-udf`.
- Lint: `cargo fmt --all --check`, `cargo +1.100.0 clippy -p pyspark-rs -p
apache-spark-connect --all-targets -- -D warnings`, ruff 0.14.0,
`audit_no_stubs.sh`, `test_ruff_excludes.py` and
`scripts/check_vendored_upstream.sh`.
There is no Delta server here, so nothing ran end to end against one; the
requests on the mock server are the closest check.
### 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]