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

   ### What changes were proposed in this pull request?
   
   [SPARK-61263](https://issues.apache.org/jira/browse/SPARK-61263): compress a 
plan over the server's plan compression threshold, as PySpark does.
   
   - `crates/spark-connect-core/src/client.rs`: the client reads 
`spark.connect.session.planCompression.threshold` and 
`spark.connect.session.planCompression.defaultAlgorithm` with one `Config` Get 
on its first plan, and keeps the result for the client (clones share it; a new 
session starts fresh). A failed lookup, a missing, empty or invalid threshold, 
or an algorithm other than `ZSTD` leaves plans uncompressed, as PySpark's 
`_get_plan_compression_threshold_and_algorithm` does.
   - A relation (`Plan.root`) or a command (`Plan.command`) whose serialized 
size is over the threshold is replaced by a `Plan.compressed_operation` (zstd, 
level 3, op type `OP_TYPE_RELATION` or `OP_TYPE_COMMAND`) when that makes it 
smaller, as PySpark's `_try_compress_and_set_plan`.
   - The compression is applied in `execute_plan` and 
`execute_plan_reattachable` (before the execute hooks, as PySpark builds the 
plan before its hooks) and in `analyze_plan` for the analyses that carry a 
plan: schema, explain, tree string, is local, is streaming, input files, same 
semantics and semantic hash. Persist, unpersist and storage level carry 
relations in PySpark and are not compressed.
   - A `CONNECT_INVALID_PLAN.CANNOT_PARSE` error disables compression for the 
client, as PySpark's `_handle_rpc_error` does.
   - `crates/spark-connect-core/Cargo.toml`: adds `zstd = "0.13"` (already in 
the lock file at 0.13.3 through arrow and polars).
   - Raw passthrough paths (`execute_plan_raw`, `analyze_plan_raw`) are 
unchanged: their bytes are built by the caller, which compresses them itself.
   
   ### Why are the changes needed?
   
   The Rust client never compressed plans. A plan whose encoding is over the 
server's plan compression threshold (10 MB on OSS Spark 4.3.0, zstd) is sent by 
PySpark as `Plan.compressed_operation`; the Rust client sent it uncompressed. 
Uncompressed, a plan over the 128 MB gRPC message limit fails on the wire with 
`[Internal error] h2 protocol error: http2 error`, and a plan between the 
threshold and that limit goes over the wire uncompressed.
   
   On a default OSS Spark 4.3.0 server, the decompressed plan is also limited, 
by `spark.connect.grpc.maxInboundMessageSize` (128 MiB). So a 150 MiB literal 
fails for both clients even with compression: the server rejects the 
decompressed plan and returns a message header that declares 157286618 bytes. 
The reference client reports this as `RESOURCE_EXHAUSTED` ("Received message 
larger than max"), and the Rust client reports it as `decoded message length 
too large` (tonic's own check of the same header). This is server behavior and 
is not changed here. A 120 MiB literal, under that limit, succeeds on both 
clients.
   
   With this change the Rust client sends the plan as PySpark does: the request 
for the 150 MiB literal is about 5.7 KB on the wire (the reference client's is 
about 6.5 KB), where before it was over 150 MB and failed in the transport. The 
remaining failure at 150 MiB is the server's limit on the decompressed plan, 
the same for both clients.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, in behavior only, with no API change: plans over the server's plan 
compression threshold are now sent compressed, as PySpark sends them, so they 
take less of the network, and a plan whose encoding is over 128 MB fails with 
the server's error about its decompressed size, as in PySpark, instead of a 
transport error. The client makes one extra `Config` call per session, on its 
first plan, as PySpark does.
   
   ### How was this patch tested?
   
   - Rust unit tests in `client.rs` (`plan_compression_tests`): the threshold 
parsing for the valid and the disabled confs; a relation and a command over the 
threshold are sent as zstd operations that decode back to the original bytes; 
the threshold boundary (a plan of exactly the threshold is sent as it is); a 
payload that zstd does not shrink is sent as it is; the plan-bearing analyses. 
`cargo test -p apache-spark-connect-core --release --lib plan_compression`: 5 
passed.
   - Offline Python tests in `python/tests/test_dropin_offline.py` against the 
mock server: an over-threshold relation is sent as a zstd operation whose 
decompressed relation has the same fields as the uncompressed one; the 
threshold boundary at the size of the plan and one byte under; the threshold is 
read once per session; an absent threshold value disables compression; a 
`CANNOT_PARSE` error disables it for the session; an over-threshold schema 
analysis is compressed.
   - The mock does not record the plan-compression lookup 
(`_mock_connect_server.py`), so the existing assertions about other requests 
are unchanged.
   - Live, against an OSS Spark 4.3.0 Connect server on `sc://localhost:15002`: 
a 100 MiB and a 120 MiB literal return their lengths (104857600 and 125829120) 
from both the reference client and the drop-in. A 150 MiB literal fails on both 
clients at the server's decompressed-plan limit, as described above, so no e2e 
test of that size is added.
   - The lib tests of both crates (528 and 155 passed), the build of all tests, 
also with `wasm-udf`, `cargo fmt --all --check`, the offline Python suites 
(2933 passed, 8 skipped), and the live drop-in, Arrow and ML suites against a 
local Spark 4.3.0 server (236 passed, 1 skipped). Clippy reports no diagnostics 
in the changed lines.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Opus 5.5 and Claude Haiku 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