HyukjinKwon opened a new pull request, #550:
URL: https://github.com/apache/spark-connect-rust/pull/550
### What changes were proposed in this pull request?
A follow-up of #495 (SPARK-61417), from a review of it. Two of its items did
not hold for every path.
1. **`BinaryClassificationEvaluator` takes PySpark's `probabilityCol`.**
#495 said that the three evaluators of `pyspark.ml.connect` show PySpark's
signatures. The binary one took `rawPredictionCol='rawPrediction'`. PySpark's
is `BinaryClassificationEvaluator(*, metricName='areaUnderROC',
labelCol='label', probabilityCol='probability')`
(pyspark/ml/connect/evaluation.py:186-203), and it has no `rawPredictionCol`.
- The Python class now takes `probabilityCol`, defaulting to
`"probability"`, and `setProbabilityCol`. `rawPredictionCol=` raises the
`TypeError` of an unexpected argument, as in PySpark.
- The class still evaluates through the server's evaluator, which scores
its `rawPredictionCol`. So the probability column is sent as that param
(`Native::jvm_name`). The JVM evaluator reads a probability vector at its
element 1, the positive class, and takes a double column as it is.
- The constructor sends the default, as the multiclass evaluator sends
`accuracy`, because the JVM default is `rawPrediction`.
- The Rust API (`crates/spark-connect`) keeps Spark's
`raw_prediction_col`. `docs/compatibility.md` says so.
2. **Protobuf's `ValueError` for a str that UTF-8 cannot encode, in the
paths that #495 missed.** #495 made the keys and values of the reader, writer,
stream and session options raise protobuf's `ValueError` for a str with a lone
surrogate. Several sibling paths still raised the `UnicodeEncodeError` that
PyO3's extraction of a str gives. PySpark raises protobuf's `ValueError`
("'a\ud800b' isn't a valid unicode string and can't be encoded in UTF-8.") when
the str goes into a request. The new helper `str_argument`
(`crates/pyspark-rs/src/options.rs`) raises it. It takes the text of the str
itself, as PyO3's extraction did, unlike `str_text`, which takes `str()` of an
option value. These paths now use it:
- the key of `option()` of `DataFrameReader`, `DataFrameWriter`,
`DataFrameWriterV2`, `DataStreamReader` and `DataStreamWriter`;
- the key and the value of `RuntimeConf.set`, and the key of `get`,
`unset` and `isModifiable`;
- the key of `SparkSession.builder.config(key, value)`;
- a str fill value of `fillna`, a key of its dict and a name of its
subset. #495 had made these raise the `UnicodeEncodeError` of the encoding;
- the text of `spark.sql`, the keys of its `args` dict, and the name of
`spark.table`.
### Why are the changes needed?
To match PySpark 4.3.0. Probes with the Connect client of official PySpark
4.3.0 and with this client, against a local Spark 4.3.0 server:
- PySpark's docstring example of `BinaryClassificationEvaluator` (a
DataFrame with the columns `label` and `probability`, evaluated with the
defaults) failed in this client with `[FIELD_NOT_FOUND] No such struct field
'rawPrediction'`. It now evaluates on `probability`. The metric is the
server's, as before. Its `areaUnderPR` is 0.9027777777777777 (Spark's
trapezoids over the thresholds), where PySpark's torcheval metric gives the
docstring's 0.9166666865348816. That is an existing difference: the evaluators
of this client score on the server, with no torcheval.
- Of 18 calls with a str that has a lone surrogate (the fill value, dict key
and subset of `fillna`, the option keys of the five readers and writers, the
four conf methods, `builder.config`, `table` and `sql`), this client raised
`UnicodeEncodeError` for 14. PySpark raises protobuf's `ValueError` for each.
All 18 now raise PySpark's `ValueError`, with its message.
One difference remains: PySpark keeps the options of a reader or writer, and
`builder.config`, and raises when it builds the request of the action (`load`,
`save`, `getOrCreate`). This client raises at the call, as before this PR.
### Does this PR introduce _any_ user-facing change?
Yes, on unreleased changes:
- `pyspark.ml.connect.evaluation.BinaryClassificationEvaluator` takes
`probabilityCol` (default `"probability"`) and `setProbabilityCol`, instead of
`rawPredictionCol` and `setRawPredictionCol`.
- The paths above raise protobuf's `ValueError` instead of
`UnicodeEncodeError` for a str that UTF-8 cannot encode. `UnicodeEncodeError`
is a `ValueError` too.
### How was this patch tested?
Each new or changed test fails on an extension built from master and passes
with the change:
- `test_ml_offline.py`:
- `test_ml_connect_evaluators_take_the_signatures_and_defaults_of_pyspark`
now expects `probabilityCol`;
- new
`test_ml_connect_binary_evaluator_sends_its_probability_col_as_the_jvm_raw_prediction`;
- the tests that compare the params a class sends include the default
`probabilityCol`.
- `test_ml_e2e.py`, against a local Spark 4.3.0 server:
- new
`test_ml_connect_binary_evaluator_scores_the_probability_of_pyspark_docstring`;
- the existing evaluator test uses `probabilityCol`.
- `test_dropin_offline.py`:
-
`test_strings_that_utf8_cannot_encode_raise_value_error_as_options_and_fills_as_pyspark`
now asserts the message of the fill value, dict key and subset;
- new
`test_strings_that_utf8_cannot_encode_raise_value_error_in_option_keys_conf_and_names_as_pyspark`.
- The offline drop-in and ML suites (2007 passed), the live `test_ml_e2e.py`
and `test_dropin_e2e.py` against a Spark 4.3.0 server (288 passed), `cargo
build -p pyspark-rs --release`, `cargo test -p apache-spark-connect -p
apache-spark-connect-core --release --no-run`, `cargo fmt --all -- --check`,
`ruff check`, and `scripts/audit_no_stubs.sh`.
### 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]