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

   ### What changes were proposed in this pull request?
   
   [SPARK-61260](https://issues.apache.org/jira/browse/SPARK-61260): Raise 
`RecursionError` instead of crashing the process on a deep plan, in the Python 
client as PySpark does.
   
   - A plan deeper than `MAX_PLAN_DEPTH` (256 levels) fails with a 
`RecursionError` (`SparkErrorKind::RecursionError`, which the Python client 
raises as the builtin) before any request is built (`relation_with_ids`, 
`crates/spark-connect/src/plan.rs`). Every request that sends a DataFrame's 
plan goes through it: actions, analysis, writes, streaming starts, merges, ML 
transforms and fits, pipelines, and the placeholders that `pyspark.ml` and 
session hooks send. The depth (`LogicalPlan::depth`) is found without 
recursing, and once for each node that several plans share. It counts the 
inputs of each plan; the plans that subquery expressions refer to are not 
counted.
   - A plan deeper than 32 levels is serialized on a scoped thread whose stack 
is sized for its depth, so it does not overflow the stack of a calling thread 
with as little as 512 KiB. A level takes about 5 KiB of native stack.
   - `LogicalPlan` drops the plans below it that nothing else holds without 
recursing, so dropping a plan of any depth, sent or not, does not overflow the 
stack. The plans that another plan still holds are kept as they are.
   - `DataFrame::to_relation` and `Transformer::build_ml_relation` of the Rust 
API now return a `Result`, as serializing can fail.
   
   The bound: PySpark builds the proto of a plan by recursing in Python, so 
under Python's default recursion limit of 1000 it raises `RecursionError` from 
245 to 247 levels, depending on the operation (measured with `filter`, 
`select`, `limit`, `withColumn` and `alias` chains). The server rejects a plan 
of about 510 levels or more with only `[Unknown error] Application error 
processing RPC`. 256 gives `RecursionError` for every plan PySpark raises it 
for, give or take ten levels, and keeps the plans that PySpark sends.
   
   ### Why are the changes needed?
   
   Serializing a plan recursed once per level with no bound. Against an OSS 
Spark 4.3.0 Connect server, with `spark.range(10)` under chained `filter`s and 
`count()`:
   
   ```python
   df = spark.range(10)
   for _ in range(2000):
       df = df.filter(F.col("id") >= 0)
   df.count()
   ```
   
   - PySpark 4.3.0 raises `RecursionError` from 245 levels, and the process 
lives.
   - master: 510 to 1,500 levels were rejected by the server with `[Unknown 
error] Application error processing RPC`, and from 2,000 levels the process 
died with SIGSEGV. On a thread with a stack of 1 MiB, the process died from 200 
levels.
   - master: dropping a plan of 100,000 levels that was never sent killed the 
process on a thread with a stack of 1 MiB. PySpark drops it.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes.
   - A plan deeper than 256 levels raises `RecursionError` in the Python 
client, and is an error of that kind in the Rust API, instead of a crash or an 
error of the server. Plans up to 256 levels are sent as before, also from 
threads with small stacks.
   - Dropping a deep plan no longer crashes the process.
   - Rust API: `DataFrame::to_relation` returns `Result<Relation>`, and 
`Transformer::build_ml_relation` returns `Result<MlRelation>`.
   
   ### How was this patch tested?
   
   - Rust unit tests (new, `plan::deep_plan_tests` in 
`crates/spark-connect/src/plan.rs`): the depth of chains and of plans with 
references; a plan whose levels each refer twice to the one below (2^64 paths) 
has its depth found at once; a plan of exactly 256 levels is serialized on a 
thread with a 512 KiB stack; plans of 257, 2,000 and 100,000 levels are 
`RecursionError`s; a plan of 1,000,000 levels drops on a 512 KiB stack; 
dropping a plan keeps the inputs that another plan holds. Without the 
serialization thread the 256-level test overflows the stack, and without the 
non-recursive drop the drop test does.
   - Rust unit test (new, `dataframe::plan_construction_tests`): `count`, 
`collect`, `schema` and `explain_string` of 10,000 chained filters are 
`RecursionError`s, offline, without a server.
   - Python offline test (new, 
`test_a_plan_too_deep_to_send_raises_recursion_error_without_a_request` in 
`python/tests/test_dropin_offline.py`, with the mock server): `count`, 
`collect`, `schema` and `explain` of 10,000 chained filters raise 
`RecursionError`, and no execute or analyze request is sent.
   - Python live test (new, 
`test_a_deep_plan_raises_recursion_error_as_pyspark` in 
`python/tests/test_dropin_e2e.py`): the depths of the issue, 1,000 to 10,000 
levels, raise `RecursionError` in a process of its own, which then runs 
`spark.range(3).count()`.
   - The issue's repro against a local Spark 4.3.0 server: from 255 to 10,000 
levels the drop-in raises `RecursionError` and the process lives, as PySpark 
4.3.0 does from 245; on a thread with a 1 MiB stack, plans up to 254 levels are 
sent (the server's error, as PySpark gets at those depths) and deeper ones 
raise `RecursionError`; plans of 10,000, 100,000 and 1,000,000 levels drop on 
the main thread and on a 1 MiB thread.
   - The lib tests of the crate (535 passed), all the Rust tests of both crates 
against a local Spark 4.3.0 server (1009 passed), the build of all tests, also 
with `wasm-udf` and with `datafusion,polars`, the offline Python suites (2928 
passed, 8 skipped), and the live drop-in, Arrow and ML suites (237 passed, 1 
skipped) against a local Spark 4.3.0 server. The official Connect suites 
through `scripts/run_official_tests.py`, in drop-in mode and through the 
transport, against a local Spark 4.3.0 server started without `SPARK_TESTING`: 
the only failures are the 29 (drop-in) and 25 (transport) tests that master 
fails identically on the same server, most of them needing the TIME type, which 
that server leaves off. `cargo fmt --all --check` is clean. Clippy reports no 
new diagnostics in the changed lines; three existing 
`field_reassign_with_default` warnings are on lines where only `?` was added.
   
   ### 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