HyukjinKwon opened a new pull request, #779:
URL: https://github.com/apache/spark-connect-rust/pull/779
### What changes were proposed in this pull request?
`SparkSession.client`, the drop-in's `SparkConnectClient`, now has the
public API of PySpark 4.3.0's `SparkConnectClient`
(`pyspark/sql/connect/client/core.py`), over the Rust transport. Before, it had
only what `pyspark.ml` and the modules that build their own messages use. A
`dir()` diff against the official client showed 34 public members missing; now
none is. All 49 public methods and properties of the client and `RpcDeadlines`
match the official ones in parameter names, kinds, defaults and property-ness.
**The members**, most of which delegate to features of the session or the
transport:
| group | members |
|---|---|
| tags | `add_tag`, `remove_tag`, `get_tags`, `clear_tags` |
| interrupts | `interrupt_all`, `interrupt_tag`, `interrupt_operation` |
| registrations | `register_udf`, `register_udtf`, `register_java`,
`register_data_source` |
| artifacts | `add_artifacts`, `cache_artifact`, `cache_artifacts`,
`copy_from_local_to_fs` |
| plans | `config`, `explain_string`, `schema`, `same_semantics`,
`semantic_hash`, `to_table`, `to_table_as_iterator`, `to_pandas`,
`execute_command_as_iterator` |
| sessions | `newSession`, `clone` |
| progress handlers | `register_progress_handler`,
`remove_progress_handler`, `clear_progress_handlers` |
| reattach and retries | `enable_reattachable_execute`,
`disable_reattachable_execute`, `get_retry_policies`, `set_retry_policies` |
| properties | `token` |
- They take and return PySpark's types: `pb2.Plan` and `pb2.Command`
messages, Arrow tables and schemas, `ExecutionInfo`, `AnalyzeResult`,
`ConfigResult`. A plan made by `df._plan.plan(client)` resolves in executions
and analyses.
- `execute_command` goes through the same path, so it returns PySpark's
`ExecutionInfo` (it returned None) and honors its `observations`.
- `pyspark.sql.connect.client` also exports `getLogLevel`, as PySpark's
package does.
**The constructor** takes upstream's form, `SparkConnectClient(connection,
user_id=None, channel_options=None, retry_policy=None,
use_reattachable_execute=True, session_hooks=None,
allow_arrow_batch_chunking=True, preferred_arrow_chunk_size=None,
rpc_deadlines=None, max_retry_exception_elapsed_time=None)`. It builds a Rust
session of the connection, a URL or a `ChannelBuilder`. The internal
construction of `SparkSession.client` is
`SparkConnectClient._of_session(session)`.
**Retry policies.** `get_retry_policies` and `set_retry_policies` take
PySpark's `RetryPolicy` and `DefaultPolicy` of
`pyspark/sql/connect/client/retries.py`, vendored from v4.3.0 under the drift
gate. It imports grpc at top level, so it is imported lazily, only by these
methods, a `retry_policy=` argument and the lazy names of `client.core`. The
Rust transport honors the policies' parameters: max retries, the initial and
maximum backoff, the multiplier, the jitter, its threshold, and the server's
retry delay. For example, with UNAVAILABLE:
- `max_retries=2` makes 3 calls;
- `[RetryPolicy(), DefaultPolicy(max_retries=1)]` makes 2;
- `[]` makes 1;
- a RetryInfo delay of 300 ms is waited.
**Errors.** `pyspark.errors.exceptions.connect` gets `convert_exception`,
`convert_observation_errors`, `_convert_exception` and
`_extract_jvm_stacktrace`, as upstream's. The file's adaptations are kept.
**In the core**, for these:
- `spark-connect-core`:
- a client's retry policy is shared by its clones and can be replaced
while in use;
- the connection's `token` parameter, the result chunking and a reattach
flag are exposed;
- a non-reattachable execution has no ReattachOptions and no deadline, is
not released, and is sent again when its stream fails in a way that the policy
retries, as PySpark does;
- `with_new_session_id`, as PySpark's `newSession`/`clone`, gives the
default policy and chunking and keeps the deadlines, hooks and reattach flag.
- `apache-spark-connect`: the proto entry points of executions, analyses and
configs, `cache_artifacts`, the policy and reattach accessors, and
`PlanExecution`.
- `pyspark-rs`: the new `client.rs`, and the session's primitives of them.
`docs/compatibility.md` has a new "SparkSession.client" section.
`dev/parity/known-differences.md`, `dev/parity/inventory.csv` and `AGENTS.md`
are updated.
### Why are the changes needed?
Code written against PySpark's `SparkConnectClient`, such as libraries that
tag, interrupt, run raw plans or set retry policies through `spark.client`,
failed with `AttributeError` on the drop-in. The official test
`pyspark/errors/tests/test_connect_errors_conversion.py` failed to import
`convert_exception`.
### Does this PR introduce _any_ user-facing change?
Yes: the members above, as PySpark's. Behavior changes to review:
- `client.execute_command` returns `ExecutionInfo` as its third item, where
it returned None.
- The conversion of a proto schema raises PySpark's conversion errors
(`proto_schema_to_pyspark_data_type`).
- In the Rust core, a retry policy without `max_backoff` no longer grows its
backoff, as PySpark's: `DefaultPolicy(max_backoff=None, initial_backoff=10,
backoff_multiplier=2)` waits `[10, 10, 10, 10]` on the official client.
- In the Rust core, `with_new_session_id` gives the default retry policy and
chunking.
Documented differences that remain:
- a policy with its own `can_retry` or `to_state`, which Rust cannot call,
raises `NOT_IMPLEMENTED`;
- so do a fractional backoff, jitter or delay, and `channel_options` with a
connection string;
- `get_tags()` returns a copy, where PySpark returns its live set;
- `client.reattach` is not provided;
- `RpcDeadlines.get_status` has no RPC in the transport.
### How was this patch tested?
- Probes of both clients against a Spark 4.3.0 server give the same results,
apart from the documented differences. They cover:
- schema, explain, same_semantics, semantic_hash,
to_table/to_pandas/to_table_as_iterator and both execute_command forms;
- config, tags and their errors, interrupts, token, host;
- the retry policies, register_udf/udtf/java, cache_artifact(s),
newSession and clone;
- the constructor's errors.
- New tests:
- 9 in `test_dropin_offline.py`, with the mock server, which now answers
same_semantics and semantic_hash;
- 2 in `test_dropin_e2e.py`, which also pass on the official client;
- 4 in `reattach_cleanup.rs`, a retries unit test, and an e2e test in
`e2e_session_catalog.rs`.
- The official `test_connect_errors_conversion.py` (12/12) and
`test_connect_retry.py` (9/9) pass on the drop-in. `test_client.py`,
`test_client_retries.py` and `test_client_call_stack_trace.py` still fail at
import, since they mock the internals of grpcio's client.
- The offline suite of `coverage.yml`; `test_dropin_e2e.py` live (296
passed); `cargo test -p apache-spark-connect-core`; `apache-spark-connect`'s
lib (743 passed) and `--no-run` with and without `wasm-udf`.
- `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`, ruff 0.14.0, `audit_no_stubs.sh`, `test_ruff_excludes.py` 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]