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

   ### What changes were proposed in this pull request?
   
   [SPARK-61332](https://issues.apache.org/jira/browse/SPARK-61332): Send plans 
of up to 746 levels as PySpark does, and count the plans of subqueries for the 
depth bound without overflowing the stack. This is a follow-up of SPARK-61260 
(#451), with two fixes in `crates/spark-connect/src/plan.rs`:
   
   - `MAX_PLAN_DEPTH` is 746, the depth from which official PySpark 4.3.0's 
serializer raises `RecursionError`, instead of 256. Its doc comment gives the 
measured depths.
   - Plans are serialized on the calling thread, on a stack that grows on 
demand. Each level of `relation_with_references` goes through the stack helper 
of SPARK-61328 (#456), which `expression.rs` now shares. The thread sized for 
the outer plan (`INLINE_PLAN_DEPTH`, `STACK_PER_PLAN_LEVEL`) is gone.
   - The levels of the plans are counted as they are serialized, those of the 
plans of subqueries included, under the plan that has them, as they are sent in 
its `WithRelations`. `relation_with_ids` fails with a `RecursionError` when the 
count is past the bound, and stops serializing at the bound, so the proto of 
the request, which is encoded and dropped by recursing too, is never deeper. 
The check of `LogicalPlan::depth` before the serialization stays, for the 
levels of the plan itself.
   
   ### Why are the changes needed?
   
   Two regressions of SPARK-61260.
   
   - It bounded a plan at 256 levels, on the premise that PySpark raises 
`RecursionError` from about 245 levels. It does not. Bisecting `_plan.to_proto` 
with official PySpark 4.3.0 against a local Spark 4.3.0 server, under the 
default recursion limit:
   
     | Chain | Serialized | `RecursionError` from |
     |---|---|---|
     | `filter` | 745 levels | 746 |
     | `withColumn` | 746 | 747 |
     | `select` | 746 | 747 |
     | `limit` | 747 | 748 |
   
     The server runs plans of up to about 510 levels. So the Rust client and 
the Python drop-in rejected plans of 257 to about 510 levels that PySpark, the 
previous master and the server all run, such as the 400 `withColumn`s of 
Apache's own `test_recursion_handling_for_plan_logging`.
   - The thread that SPARK-61260 serialized a deep plan on was sized for the 
levels of the outer plan only, but the plans of its subqueries (EXISTS, scalar, 
IN, table arguments) were serialized on it too. An outer plan of about 34 
levels with an EXISTS over a plan of 256 levels (Rust) or 300 (drop-in) 
overflowed that thread and aborted the process. In debug builds a 5-level plan 
over a 30-level subquery was enough. The previous master ran these plans.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. Plans of up to 746 levels, with the plans of their subqueries, are 
sent, as PySpark sends them. Deeper ones raise `RecursionError` in the Python 
client, and are an error of that kind in the Rust API, as before. A subquery 
over a deep plan no longer crashes the process.
   
   ### How was this patch tested?
   
   - 
`plan::deep_plan_tests::a_subquery_counts_for_the_bound_under_the_levels_above_it`
 (offline, placed before 
`dropping_a_deep_plan_does_not_overflow_a_small_stack`): limits over a filter 
with an EXISTS subquery, with the subquery's levels within the bound and one 
past it, under 0 and 33 levels. The plan within is serialized and the one past 
is a `RecursionError`, both on a thread with a stack of 512 KiB. A subquery of 
100,000 levels is a `RecursionError` and is dropped there too.
   - 
`plan::deep_plan_tests::a_plan_at_the_bound_is_encoded_on_a_thread_of_the_runtime`
 (same place): a plan at the bound is encoded and dropped on a thread with a 
stack of 2 MiB, that of the runtime's threads, where the transport encodes a 
request.
   - The existing deep-plan and deep-expression tests pass. All of them pass in 
release and in debug builds.
   - 
`test_a_subquery_of_a_deep_plan_is_sent_or_raises_recursion_error_as_pyspark` 
in `python/tests/test_dropin_offline.py` (mock server, placed before 
`test_a_plan_too_deep_to_send_raises_recursion_error_without_a_request`): a 
subquery of 300 levels under 33 is sent, and one of 800 raises `RecursionError` 
without a request.
   - `test_plans_of_a_few_hundred_levels_are_sent_as_pyspark` in 
`python/tests/test_dropin_e2e.py` (live, placed before 
`test_a_deep_plan_raises_recursion_error_as_pyspark`): 400 `withColumn`s give 
401 fields and count 10, and the filter of a 300-level subquery under 33 levels 
counts 10.
   - The lib tests of both crates (720 passed), the build of all tests, also 
with `wasm-udf`, and the offline Python suites (2981 passed, 8 skipped) with 
the rebuilt extension. 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
   
   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