HyukjinKwon opened a new pull request, #475: URL: https://github.com/apache/spark-connect-rust/pull/475
### What changes were proposed in this pull request? [SPARK-61386](https://issues.apache.org/jira/browse/SPARK-61386): low-priority findings of a review of the native ML classes (`spark_connect::ml` and the drop-in's `pyspark.ml.connect`). Each was checked against official PySpark 4.3.0: `pyspark.ml.connect`, and the JVM-backed `pyspark.ml` classes over Spark Connect. The changes apply to every native class, `RandomForestClassifier` included. **Rust API** (`crates/spark-connect/src/ml.rs`): - Every JVM Int param is `i32`: `LogisticRegression::set_max_iter`/`max_iter`, `LinearSVC::set_max_iter` and `set_aggregation_depth`, as on `LinearRegression`, `KMeans` and `RandomForestClassifier`. - Every native estimator has a getter per param, in one style: the value in its params, else Spark's default (an `Option` for a param without one, such as `seed` or `weight_col`). This covers `KMeans` (12), `LinearSVC` (13), `LinearRegression` (15), `RandomForestClassifier` (23) and `CrossValidator::seed`. The getters of the older classes read the params too. - `LogisticRegressionModel`, `StringIndexerModel` and `MaxAbsScalerModel` keep their fields private, as the other models do. `LogisticRegressionModel` gets `feature_col`, `label_col` and `prediction_col`. - `VectorAssembler::input_cols` and `Pipeline::stages` return `Vec<String>`. The three evaluators get an inherent `params_mut`. - The `Params` builder's `set_param_int32`, `set_param_int_narrow`, `set_param_string_array`, `set_param_double_array` and the new `clear_param` are public. - Seeds are narrowed as PySpark sends a Python int: `CrossValidator` as `KMeans`, with one helper. - Generated uids are `<Class>_<12 hex>`, as PySpark's `Identifiable._randomUID`, so server messages name the class. **Python** (`crates/pyspark-rs/src/ml.rs`, one shared param path): - Values are converted with PySpark's `TypeConverters` (`k=3.0` is `3`, `np.int64` works), with `Params._set`'s TypeError "Invalid param value given for param ...". - An int past a long raises `VALUE_NOT_BETWEEN` at fit, before any request. - `None` leaves a param unset. PySpark sends a null, which the server rejects. - Constructors send only the params that they are given, as PySpark's. A wire capture of 13 cases is identical. - Constructors are keyword-only: "Method __init__ forces keyword arguments.", as `@keyword_only` raises. `MulticlassClassificationEvaluator`, which is not `@keyword_only` in PySpark, takes its arguments by position in PySpark's order. - A setter works while another thread fits or evaluates the same object: the core object is cloned under a short borrow. - The transform of a model on a DataFrame of another session gives a DataFrame that raises `CONNECT_ML.CACHE_INVALID` from the server, as PySpark's does. The Rust API still rejects the transform itself. **Session** (`session.rs`): `release_ml_objects` skips a stopped session, as `release_cached_remote_relation` does. **Docs** (`docs/compatibility.md`): - Migration paragraphs for the signature changes, and for the removal of `BinaryClassificationEvaluator::set_score_col`/`score_col` and Python `scoreCol=`/`setScoreCol`, which shipped in v4.2.0. - Without a seed, Spark's default seed (the hash of the class name) applies, so fits are reproducible. - The behavior of the Python bindings. ### Why are the changes needed? Inconsistencies across the natives and differences from PySpark: - `i64` and `i32` for the same JVM Int params. - Getters on one class but not the others, and public fields on some models. - `TypeError` for `KMeans(k=3.0)` and `OverflowError` for big ints, where PySpark converts or raises its own error. - Default params sent where PySpark sends none, and bare-UUID uids. - Positional arguments where PySpark's classes are keyword-only. - `RuntimeError: Already borrowed` for a setter during a fit. - A client-side `PySparkValueError` where PySpark gets `CONNECT_ML.CACHE_INVALID`. - Docs that missed a v4.2.0 removal and misdescribed seeds. ### Does this PR introduce _any_ user-facing change? Yes, all of the above: - **Rust signatures:** `i32` params, private model fields, `Vec<String>` getters. - **Python constructors:** keyword-only, and `MulticlassClassificationEvaluator`'s positional order. - **Python params:** conversions and errors as PySpark. The migration paragraphs describe the Rust changes. ### How was this patch tested? - Rust unit tests (`ml.rs`): `generated_uids_name_the_class_as_pyspark`, `seeds_are_sent_as_pyspark_sends_a_python_int`, `native_estimators_get_their_params_or_the_spark_defaults`, `getters_read_the_params_that_are_sent` and `logistic_regression_model_reads_its_columns_from_its_params`. Also `params_builders_send_the_literals_of_pyspark_from_outside_the_crate` (`tests/ml_coverage.rs`). - Offline Python (`python/tests/test_ml_offline.py`): - `test_ml_connect_constructors_are_keyword_only_as_pyspark` - `test_ml_connect_params_convert_with_the_type_converters_of_pyspark` - `test_ml_connect_constructors_send_only_the_params_they_are_given_as_pyspark` - `test_ml_connect_unsendable_ints_none_and_uids_as_pyspark` - `test_ml_connect_setters_run_while_a_fit_or_an_evaluate_waits_as_pyspark` - `test_native_models_released_while_their_session_stops_send_no_delete` - Live Python (`python/tests/test_ml_e2e.py`): `test_native_models_are_released_once_their_last_owner_is_gone_as_pyspark` (all 8 native models) and `test_native_models_transform_a_dataframe_of_another_session_as_pyspark`. - `python/tests/test_dropin_e2e.py`: a `_native_ml()` helper replaces the six fallbacks to the official classes. The native tests now fail if a native class is missing, instead of silently testing the official one. - Failing without the change: - every new offline test, and the cross-session live test, against an extension built from the base; - the uid and seed Rust tests; - the getter and builder tests do not compile against the base. - The reviewer's KMeans probe (47 cases) now differs from official only in `seed=None` (by design) and the narrower native API surface. - Checks: - the lib tests of both crates (759 passed); - the build of all tests, also with `wasm-udf` and `datafusion,polars`, without warnings; - `cargo fmt --all --check`, and clippy with no diagnostics on the changed lines; - the ML golden and coverage tests; - the live `e2e_kmeans`, `e2e_linear_svc`, `e2e_linear_regression` and `e2e_random_forest`; - `test_ml_offline.py` (104 passed), and targeted live ML selections. ### 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]
