HyukjinKwon opened a new pull request, #387: URL: https://github.com/apache/spark-connect-rust/pull/387
### What changes were proposed in this pull request? [SPARK-61202](https://issues.apache.org/jira/browse/SPARK-61202): Send the relation of a DataFrame into the plans of `pyspark.ml` without parsing it, so that a deep DataFrame fits and transforms, as PySpark does. The plans of `pyspark.ml` took the relation of a DataFrame by parsing it twice. `_DataFramePlan.plan` decoded it with the Python protobuf decoder, and `DataFrame(plan, session)` and the ML commands decoded the built plan with prost. Both decoders refuse more than 100 levels of nesting, so a DataFrame deeper than that could not be fitted or transformed. PySpark builds the nested message without parsing it, so it works at these depths. The change: - `_DataFramePlan.plan` returns a placeholder of the relation of its DataFrame: a shallow `Relation` whose `extension` is a marker of the DataFrame. The vendored plans of `pyspark.ml` build over it as they do over a relation, so the plan of a transform over a deep DataFrame is shallow too. - The core keeps a registry of the placeholders: each marker names the plan node of its DataFrame and the plan IDs of its session, both held weakly, and an entry is pruned with its DataFrame, as `PlanIds` prunes its nodes. The same DataFrame gives the same marker. - The core replaces each placeholder with the relation of its DataFrame, built as `DataFrame.to_relation` builds it, so it is the relation that is sent today. The replacement is moved in, never decoded. It happens where a Python-built relation or command reaches the core: - `DataFrame::from_relation` (`DataFrame(plan, session)`), in the positions that the ML plans build over their inputs: `ml_relation.transform.input`, `ml_relation.model_summary_dataset`, the arguments of `fetch`, and the `root` and `references` of `with_relations`; - `execute_command_proto` (`session.client.execute_command`), in the datasets of the `fit`, `evaluate` and `create_summary` commands and in the arguments of `fetch`; - the session hooks, for the root or the command of the request they return. - The owner of a DataFrame of a plan is still the Python plan, so the model keep-alive of SPARK-61158 is unchanged. What server receives is the same relation as before this change: the same plan IDs and the same nesting, with no placeholder left. Official PySpark 4.3.0 sends the same nested relation, modulo plan IDs, because it also builds it without parsing it. Why not raise the limits: prost's `no-recursion-limit` feature removes the check on input that is not trusted, and a stack overflow is then a crash of the process. upb (the Python decoder) has no public setting for its limit. A larger constant only moves the failure: a OneVsRest transform adds layers per class, so any fixed limit fails at some class count. The placeholder does not decode the nesting at all, so its depth is bounded only by the stack of the encoder, as it is for `count` and `collect`. Known differences: - A placeholder is valid while its DataFrame is alive, as its DataFrame is kept by the plan that uses it. `df._plan.plan(client)` is therefore no longer a self-contained relation. It is internal, the plans of `pyspark.ml` are the users. The hook test of `test_ml_e2e.py` keeps its source DataFrame for this reason. - A placeholder that a user puts under a relation that is not one of the positions above reaches the server unresolved, and the server reports an error for it. Such a relation is not built by PySpark or by the vendored code. - Not changed here: `DataFrame._of_relation`, used when a DataFrame is unpickled, and the session hooks, which encode the request into Python and decode it back. Both still parse a deep relation: a hook on a DataFrame of 120 `withColumn` calls raises `DecodeError` with or without this change (checked on this branch). They are not on the path of `pyspark.ml`, and they are a follow-up. ### Why are the changes needed? A DataFrame of about 50 chained `withColumn` calls cannot be fitted: `LogisticRegression(maxIter=2).fit(df)` raises `DecodeError: Error parsing message: too many levels of nesting`. A `transform` of a `LogisticRegression` model over the same DataFrame raises the same error, and a `OneVsRestModel.transform` with 10 or more classes too, since each class adds layers. A pipeline of 60 `SQLTransformer` stages fails with `ValueError: ... recursion limit reached`. `count` and `collect` work at any depth, because the core sends the relation without decoding it. Before this change, the same DataFrames fit and transform on official PySpark 4.3.0. ### Does this PR introduce _any_ user-facing change? Yes. `pyspark.ml` fits and transforms DataFrames of any depth that PySpark accepts, and `DataFrame._plan.plan(client)` returns a placeholder (see the known differences). The sent requests are the same as before for the DataFrames that worked. ### How was this patch tested? - Rust unit tests in `crates/spark-connect/src/plan.rs` (`placeholder_tests`, 7 tests): a placeholder is shallow and resolves to the relation of its node; the same node and session give the same placeholder; the placeholders of the ML positions (`model_summary_dataset`, `transform.input`, the arguments of `fetch`, `with_relations`) and of the ML commands (`fit`, `evaluate`, `create_summary`, `fetch`) and of a hook's root are replaced; relations without placeholders are unchanged; a placeholder of a dropped DataFrame is an error and keeps nothing alive; a malformed one is an error. - Rust integration tests in `crates/spark-connect/tests/raw_relation.rs` (3 new): a placeholder of a 120-level DataFrame is replaced by its relation in `from_relation`, with the same plan IDs; a placeholder of a dropped DataFrame is an error; a live test runs a 120-level placeholder on the server. - Offline Python test `test_ml_plans_send_a_deep_dataframe_without_parsing_it_as_pyspark` in `python/tests/test_dropin_offline.py`: two transformer plans over a 120-level DataFrame; the request that the mock server receives carries the relation of the DataFrame byte for byte, and a placeholder of a dropped DataFrame is an error. - Live Python test `test_ml_deep_dataframes_fit_and_transform_as_pyspark` in `python/tests/test_ml_e2e.py`: `LogisticRegression` fits and transforms a 120-level DataFrame with the predictions of the shallow DataFrame, and `OneVsRest` with 12 classes transforms a 120-level DataFrame with the predictions of the shallow one. The live test fails before this change with `DecodeError`. - Existing tests that read `_plan.plan(...)` as a relation now read the relation that the server receives (`_relation` in `python/tests/test_ml_offline.py`, 9 places), and the session hook test keeps its source DataFrame. - The tests of both crates, e2e included, against a local Spark 4.3.0 server (926 passed), the build of all tests, also with `wasm-udf`, the offline Python suites (2457 passed, 8 skipped), and the live drop-in, Arrow and ML suites (218 passed) and live cases (11 passed). rustfmt passes, and 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]
