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

   ### What changes were proposed in this pull request?
   
   [SPARK-61111](https://issues.apache.org/jira/browse/SPARK-61111): The 
DataFrame-based `pyspark.ml` API runs over the Rust transport, as PySpark 
Connect does. Estimators fit on the server, models transform, and pipelines 
save and load.
   
   Most of the diff (about 49k lines) is vendored byte for byte from Apache 
Spark v4.3.0. These files are added to the drift check 
(`scripts/check_vendored_upstream.sh`):
   - the modules of `pyspark.ml`: `base`, `classification`, `clustering`, 
`evaluation`, `feature`, `fpm`, `image`, `model_cache`, `pipeline`, 
`recommendation`, `regression`, `stat`, `tree`, `tuning`, `wrapper`, `__init__` 
and `_typing.pyi`
   - the ML internals of `pyspark.ml.connect`: `proto`, `serialize`, 
`readwrite`, `io_utils`, `summarizer` and `util`
   - the generated `pyspark.sql.connect.proto` modules (`*_pb2.py` and `.pyi`), 
without the gRPC stubs. `pyspark.sql.connect.proto` becomes a package that 
exports their messages, as PySpark's does.
   
   These modules build their own `spark.connect` messages. This PR gives them 
what they use of the Connect client, over the Rust transport:
   - `SparkSession.client`, of the class 
`pyspark.sql.connect.client.SparkConnectClient`:
     - `execute_command` of a `pb2.Command`, which returns the results of the 
responses as PySpark Connect's `SparkConnectClient` does
     - `_delete_ml_cache` and `_cleanup_ml_cache`
     - `_server_session_id`
   
     It replaces `SparkConnectClientStub`, whose `_cleanup_ml_cache` did 
nothing.
   - `DataFrame(plan, session)`: a DataFrame of the relation of a Python 
`LogicalPlan`, such as `TransformerRelation`. The core sends the relation as it 
is, through a new `Raw` plan. The DataFrame and the DataFrames derived from it 
keep the Python plan alive. That is how PySpark keeps a model alive through the 
plans of its transforms.
   - `DataFrame._plan`, whose `plan(client)` is the relation of the DataFrame.
   - `Column._expr`, from which `pyspark.ml.stat` names the metrics of 
`Summarizer`.
   - Ported from PySpark 4.3.0:
     - `pyspark.sql.connect.plan.LogicalPlan`
     - `pyspark.sql.connect.expressions.Expression` and `LiteralExpression`
   - `pyspark_types_to_proto_types` and `proto_schema_to_pyspark_data_type` of 
`pyspark.sql.connect.types`, which use the type messages of the core.
   - The `JVM_*` ranges of `pyspark.util`.
   
   The core gains `DataFrame::from_relation`, `DataFrame::to_relation` and 
`SparkSession::execute_command_proto`. All three are doc-hidden.
   
   ### Why are the changes needed?
   
   The `pyspark-client` package ships `pyspark.ml` (`connect_packages` of 
`python/packaging/client/setup.py`). This client shipped only 
`pyspark.ml.connect`, `linalg`, `param`, `torch` and `deepspeed`. So `from 
pyspark.ml import Pipeline`, `pyspark.ml.classification`, `feature`, 
`regression`, `clustering`, `evaluation`, `tuning` and the other modules raised 
`ImportError`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. `pyspark.ml` works as it does with PySpark 4.3.0. For example, 
`LogisticRegression().fit(df)` fits the model on the server, and 
`model.summary` reads its summary from there.
   
   **For review: `protobuf`.** The generated modules need `protobuf >= 6.33.5`. 
This PR does not add it to the dependencies of `pyspark-client-rust`. Without 
it, `pyspark.ml` still imports, the rest of the client works, and a fit or a 
transform raises an `AttributeError` that names `protobuf`. 
`docs/compatibility.md` says to install it. Making it a dependency or an extra 
is a packaging decision left to review.
   
   Known differences, documented where they apply:
   - A DataFrame of a Python plan takes this client's plan ID rather than the 
plan's own. Its columns refer to that ID.
   - `Column._expr` is the expression class of the core, not one of PySpark 
Connect's expression classes.
   - `LogicalPlan` has no `to_proto`, `print`, `_repr_html_` or 
`_collect_references`. Those belong to the plans and Columns of PySpark 
Connect's DataFrame, which the core builds instead.
   - Struct field metadata in type messages is compact JSON.
   
   `AGENTS.md` and `dev/parity/4.3.0-introspection-diff.md` no longer list 
`SparkSession.client` as a justified non-gap.
   
   ### How was this patch tested?
   
   - New offline tests (`test_ml_offline.py`, 55 tests). Where a command is 
sent, they use a mock Connect server. They check:
     - the literal messages of `LiteralExpression`, of inferred and given 
types, with their errors, values and reprs
     - the type messages and their round trip, including `VectorUDT`
     - `SparkSession.client`: one object per session, `execute_command`, 
`_delete_ml_cache` and `_cleanup_ml_cache`
     - the `MlCommand.Fit` that `fit` sends, and the `ml_relation` of 
`transform`, which later transformations take as input
     - that DataFrames of Python plans keep their plans alive
     - the IDs and `_with_relations` of `LogicalPlan`, and `Column._expr`
     - that the vendored modules import, also without `protobuf`
   
     The 48 tests that need no mock server also pass with official PySpark 
4.3.0.
   - New live tests (`test_ml_e2e.py`, 7 tests). They cover:
     - classifiers: fit, transform, summary and evaluate
     - pipeline save and load
     - feature transformers, statistics, evaluators and `CrossValidator`
     - the model cache: transformed DataFrames keep their models, and models 
are deleted from the server when collected
     - DataFrames of Python plans
   
     They pass with official PySpark 4.3.0.
   - All of the new Python tests fail without this change (55 offline and 7 
live).
   - New Rust tests (`raw_relation.rs`): 4 offline and 2 live.
   - On a local server, 109 ML cases give the same results as official PySpark 
4.3.0. They cover fits of over 20 estimators, model attributes and summaries, 
25 feature transformers, fpm, ALS, stat, tuning, and save and load. The literal 
messages of 117 cases and the type messages of 36 types are identical to 
official, apart from the spacing of struct metadata.
   - The lib tests of both crates (590 passed), the build of all tests, also 
with `wasm-udf`, the offline Python suites with the new ML tests (2160 passed, 
8 skipped), and the live drop-in, Arrow and ML suites (171 passed) and live 
cases (10 passed) against a local Spark 4.3.0 server. Clippy reports no 
diagnostics in the changed lines.
   
   The new test files are added to the offline coverage workflow and the live 
Python Connect workflow.
   
   ### 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