HyukjinKwon opened a new pull request, #707:
URL: https://github.com/apache/spark-connect-rust/pull/707
### What changes were proposed in this pull request?
`serialize` of `pyspark.ml.connect.serialize` sends a `(value, DataType)`
pair, a tuple of two whose second item is a `DataType`, as
`LiteralExpression(value, dataType)` (serialize.py:141 of 4.3.0). That is the
literal of that type, and its asserts raise a bare `AssertionError`:
- of a value that is not of the type, such as `("x", IntegerType())`;
- of a type that no literal has (`MapType`, `StructType`, `CharType`,
`VarcharType`, `YearMonthIntervalType`, a UDT), also with a None value.
The native ML classes raised the `UNSUPPORTED_DATA_TYPE` of a tuple instead.
#697 and #701 documented this as the one remaining difference of their helper
calls. This PR changes the PyO3 base:
- **`object::fetch_arg_of`** serializes every argument of a native ML call:
the members of a model, and helper calls such as `ChiSquareTest.test`,
`Correlation.corr` and `PowerIterationClustering.assignClusters`. It now sends
such a pair as the literal of its type, through
`functions::typed_pair_literal`, which follows PySpark's constructor:
- it asserts the class of the type against PySpark's 17 classes of a
literal (`LITERAL_TYPE_CLASSES`);
- it converts the type as `pyspark_types_to_proto_types` does;
- it asserts the value (`literal_as`).
- **`literal_as`** now also asserts the class of each element's type, a None
element too, as `to_plan` constructs `LiteralExpression(element, elementType)`.
So `([None], ArrayType(MapType(...)))` raises PySpark's AssertionError, and
`([], ArrayType(MapType(...)))` is sent. This is neutral for the existing list
literals, whose element types are inferred and so are always of a literal's
class.
- **`helper_dataset_of`** (`PrefixSpan.findFrequentSequentialPatterns`, and
`ALSModel.recommendForUserSubset` and `recommendForItemSubset`) serializes its
argument through `fetch_arg_of` before its `TypeError`. So it raises PySpark's
errors of the serialization: the `AssertionError` of a pair whose value is not
of its type, and the `UNSUPPORTED_DATA_TYPE` of a Column. Those calls still
raise a `TypeError` of a value that PySpark sends for the server to reject, as
documented. A follow-up of the fpm and recommendation classes sends it, as #697
did for `assignClusters`.
### Why are the changes needed?
The native classes are to follow `pyspark.ml` 4.3.0. Official PySpark sends
these pairs, and the server rejects them as it rejects PySpark's, or official
raises its `AssertionError` at the call.
### Does this PR introduce _any_ user-facing change?
Yes. A `(value, DataType)` argument of a native ML call is now sent as the
literal of its type, as PySpark sends it. Examples: `ChiSquareTest.test((1,
IntegerType()), "features", "label")`, `Correlation.corr(df, "features",
("pearson", StringType()))`, `model.predict((1.0, DoubleType()))`. A pair that
PySpark's `LiteralExpression` rejects now raises its bare `AssertionError`
instead of `UNSUPPORTED_DATA_TYPE`. `docs/compatibility.md` drops the
difference.
### How was this patch tested?
- A new golden of official pyspark-client 4.3.0,
`tests/golden/ml_fetch_pairs.json` (`scripts/capture_ml_fetch_pairs_golden.py`,
cases in `python/tests/_ml_fetch_pair_cases.py`). It has 52 pairs and
near-pairs, recording either the literal that `ChiSquareTest.test` serializes
as its dataset, or the error of the call: 29 sent, 20 `AssertionError`, 3
`UNSUPPORTED_DATA_TYPE` of a tuple that is no pair. `test_ml_offline.py`
replays them on the native class, and all 52 are identical.
- The updated tests of `test_ml_offline.py`
(`test_native_ml_other_invalid_inputs`) and `test_statistics_offline.py`
(`test_a_typed_pair_argument_is_sent_as_the_literal_of_its_type`), which
asserted the old error.
- By hand, against a live Spark 4.3.0 server, a probe run on both official
and ours:
- the 52 cases through `PowerIterationClustering.assignClusters`:
identical arguments and errors;
- pairs and a Column through `ChiSquareTest.test` (with the server's
errors of a collect) and `LinearSVCModel.predict`: identical;
- `PrefixSpan.findFrequentSequentialPatterns`: identical but for the pairs
that PySpark sends, which this client refuses with the documented `TypeError`
(see above).
- `test_ml_offline.py` (384 passed), `test_statistics_offline.py` (116
passed), and CI's offline and live lists; `cargo fmt`, `cargo clippy -p
pyspark-rs --all-targets -D warnings`, `ruff check` and `ruff format --check`.
### 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]