HyukjinKwon opened a new pull request, #791:
URL: https://github.com/apache/spark-connect-rust/pull/791
### What changes were proposed in this pull request?
A review of the native ML classes against PySpark 4.3.0 found these gaps in
how their members take and send arguments. Each was probed on both clients
against a Spark 4.3.0 server.
**1. The DataFrame methods of models and summaries send their positional
arguments only.** PySpark's `try_remote_attribute_relation`
(ml/util.py:127-137) sends `invoke_remote_attribute_relation(self, f.__name__,
*args)`: it drops keywords, checks no arity and sends no default. The
`relation_methods` arms of the native macros now take `(*args, **_kwargs)` with
PySpark's text signature, as `recommend` and the LSH and `findSynonyms` methods
already did:
| call | PySpark 4.3.0 (now here too) | before |
|---|---|---|
| `lda.describeTopics(maxTermsPerTopic=1)` | 3 terms, as the keyword is
dropped | 1 term |
| `glr.summary.residuals(residualsType='pearson')` | the deviance residuals
| the pearson ones |
| `w2v.getVectors(x=1).count()` | 4 | `TypeError` |
| unbound, such as `Word2VecModel().getVectors(1)` | `AssertionError`, or
the `AttributeError` of `split` | `TypeError` |
**2. The native members bind a call as PySpark's functions do.**
- Each member is a function of the parameters of PySpark's member as Python
binds it, of the qualified name of the class of PySpark's that defines it, such
as `JavaMLWritable.write`, `JavaPredictionModel.predict` or
`_JavaClassificationModel.predictRaw`. It also has PySpark's module, docstring
and signature.
- So `lrm.predict(v, 1)` raises `JavaPredictionModel.predict() takes 2
positional arguments but 3 were given`, as PySpark's does.
- The models that the drop-in makes of server objects run PySpark's own
`JavaModel.__init__`.
**3. The param maps are in PySpark's order.**
- The defaults of a CrossValidatorModel were `foldCol, numFolds, seed`,
where PySpark's are `seed, numFolds, foldCol`. The validators and their models,
and the 45 JavaModel classes when unbound, now give PySpark's order.
- A OneVsRest and its model give PySpark's order of their defaults, and
after a load, of `classifier` and then the metadata's params. Their saves write
the metadata's param maps in their objects' order, as PySpark's writer does.
**4. `from_vocabulary`, `from_labels` and `from_arrays_of_labels`**
extracted their arguments as Rust types first. So labels given as a str, a set,
an iterator or dict keys raised `TypeError`, and `from_labels(['a'], None)` did
too. They now follow PySpark's order (feature.py:1201-1247, 4808-4845 and
4849-4891):
1. the `len` check, PySpark's `ValueError`;
2. the constructor;
3. `list(...)` of the labels;
4. the session;
5. the request, of `(list, ArrayType(StringType()))` pairs, whose literal
asserts each str;
6. the setters, whose `TypeConverters` errors come after the request;
7. the vocabulary's size.
QuantileDiscretizer's Bucketizer is built as PySpark's `_create_model`
builds it, which also fixes its `outputCol` default.
**5. The location of the warnings.** `BisectingKMeansModel.computeCost`'s
`FutureWarning` was attributed to the drop-in's wrapper. Now it is attributed
to `pyspark/ml/clustering.py:1025`, and OneVsRest's `weightCol` `UserWarning`
to `pyspark/ml/classification.py:3587`, as PySpark's are. So a
`warnings.filterwarnings` by PySpark's module matches them.
`docs/compatibility.md` describes these.
### Why are the changes needed?
So that the members of the native ML classes take, check and send their
arguments as PySpark's do. A keyword call of `describeTopics` or `residuals`
silently returned other data than PySpark's.
### Does this PR introduce _any_ user-facing change?
Yes, as above:
- keyword arguments of those methods are dropped, as PySpark drops them;
- calls that do not bind raise PySpark's messages;
- the param maps' order;
- the `from_*` class methods' types and errors;
- the warnings' locations.
A subclass of a model class now gets a uid of its own name, as in PySpark.
Subclasses of PipelineModel, OneVsRestModel and the validator models still take
their base class's uid, as documented.
### How was this patch tested?
- **Probes of both clients** match:
- the relation methods, bound and unbound;
- the bindings and qualnames of the members;
- the param-map orders of the 45 JavaModel and validator model classes,
unbound, fitted and loaded;
- OneVsRest's orders, through fit, load, re-save and copy, and its
metadata files;
- the 46 cases of the `from_*` methods, with a session and without;
- the warnings' locations.
- **New tests.**
- `test_ml_offline.py`: the positional arguments of the DataFrame methods,
the models of server objects, the warnings' locations, and OneVsRest's fitted
model.
- `test_ml_features_offline.py`: the Bucketizer, and the `from_*` methods
with and without a session.
- `test_ml_subclasses_offline.py`: the binding of the members, of
summaries too.
- `test_ml_api_offline.py`: the orders of the meta models.
- `test_ml_api_e2e.py`, live: the param maps of a validator's model, and
of a OneVsRest and its metadata.
Each fails on master and passes on the official client where it can run
there.
- **Python.** The 15 ML offline files on pandas 2 and 3 (2316 passed each),
the offline suite of `coverage.yml`, and the live ML e2e files.
- **Rust.** `cargo test -p apache-spark-connect --release --lib`.
- **Lint.** `cargo fmt --all --check`, `cargo +1.100.0 clippy -p pyspark-rs
-p apache-spark-connect --all-targets -- -D warnings`, ruff 0.14.0,
`audit_no_stubs.sh` and `scripts/tests`.
### 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]