HyukjinKwon opened a new pull request, #470: URL: https://github.com/apache/spark-connect-rust/pull/470
### What changes were proposed in this pull request? [SPARK-61363](https://issues.apache.org/jira/browse/SPARK-61363): low-priority findings of a review of the Python drop-in, each checked against official PySpark 4.3.0 with the same probe on both clients. - **Literals** (`crates/pyspark-rs/src/functions.rs`): - `lit()`, `isin`, `when` and sql args take an int or a float by its type, not by `__index__` or `__float__`. An object of only `__index__` (or only `__float__`, or a `Fraction`) raises `UNSUPPORTED_DATA_TYPE` with its class name, as PySpark does. int and float subclasses, `IntEnum` and NumPy scalars work as before. - A str that UTF-8 cannot encode (a lone surrogate) raises protobuf's `ValueError` ("... isn't a valid unicode string and can't be encoded in UTF-8."), as PySpark's serializer does. - A pandas `Timestamp` literal of nanoseconds drops them silently, of the same value, for TIMESTAMP and TIMESTAMP_NTZ literals. Before, pandas warned "Discarding nonzero nanoseconds in conversion" (also `session.rs`). - **`DataFrame.transform`** asserts that the function returned a DataFrame: `AssertionError: Func returned an instance of type [<class 'int'>], should have been DataFrame.` - **Observations** (`observation.rs`, `interrupt.rs`): - An action whose execution sends a metric that PySpark's `LiteralExpression._to_value` does not convert (a year-month interval, a nanosecond timestamp), for an Observation of the session, raises `PySparkTypeError` `UNSUPPORTED_LITERAL` with the literal's protobuf text, as PySpark does. - That Observation, and those after it in the server's order, then give `{}` from `get`. - `toLocalIterator` checks at the end of its stream. - **Nanosecond type names** (`types.rs`, `python/pyspark/sql/types.py`): - The JSON-name parsers (`_parse_datatype_json_value`, `_parse_datatype_json_string`, and the `fromJson` of `StructType`, `StructField` and `ArrayType`) raise `CANNOT_PARSE_DATATYPE` for `timestamp_ntz(9)` and `timestamp_ltz(9)`. - `pyspark.sql.types` no longer has `TimestampNTZNanosType` and `TimestampLTZNanosType`, which PySpark does not have. They stay internal to `pyspark._pyspark`. - **`toLocalIterator`** ignores `prefetchPartitions`, as PySpark Connect does. A batch is fetched when the iteration reaches it, so the progress handlers run in `next()`, on the iterating thread. - **Caches** (`PyDataFrame`): `isStreaming`, `semanticHash`, `isEmpty` and `inputFiles` are asked once per DataFrame, as PySpark's `cached_property` and `functools.cache` keep them, also after `stop()`. A failure is not kept. - **`dev/parity/known-differences.md`**: `toArrow` of invalid UTF-8 raises, where PySpark returns a table whose `StringArray` holds the invalid bytes, which is not valid Arrow. ### Why are the changes needed? Each was a difference from PySpark 4.3.0: - **Literals and `transform`:** the drop-in took objects that PySpark rejects. It gave a misleading `UNSUPPORTED_DATA_TYPE "Unsupported DataType str"` for surrogates, and warned where PySpark is silent. `transform` returned a function's non-DataFrame result. - **Observations:** `get` returned an internal marker object, where PySpark raises from the action. - **Nanosecond types:** the parsers accepted names that PySpark's do not, and the module had names that PySpark's does not. - **`toLocalIterator(prefetchPartitions=True)`:** it called the handlers on a runtime thread ahead of the consumer. An exception of a handler that the consumer never reached stayed referenced for good. - **Caches:** the drop-in asked the server again, so the answers could change, such as `isStreaming` after a temp view is redefined as streaming. And after `stop()` it raised where PySpark returns the kept answer. ### Does this PR introduce _any_ user-facing change? Yes, all of the above, to match PySpark. Two differences remain: - `toLocalIterator` no longer reads ahead with `prefetchPartitions=True`, as PySpark Connect does not. - The check of Observation metrics matches the Observations of a session by name and reads the session's last execution, so two actions that one session runs at once from two threads could check each other's execution. ### How was this patch tested? - Offline (`python/tests/test_dropin_offline.py`, placed before `test_property_not_method`). Each test fails on master for the reason it tests: - `test_lit_takes_an_int_or_a_float_by_its_type_as_pyspark` - `test_a_str_that_utf8_cannot_encode_is_the_value_error_of_protobuf_as_pyspark` - `test_pandas_timestamp_literals_drop_their_nanoseconds_silently_as_pyspark` - `test_transform_asserts_that_the_function_returned_a_dataframe_as_pyspark` - `test_metrics_that_pyspark_cannot_convert_fail_the_action_of_their_observation_as_pyspark` - `test_json_names_of_nanosecond_timestamps_are_not_data_types_as_pyspark` - `test_local_iterators_call_the_progress_handlers_in_next_whatever_the_prefetch_as_pyspark` - `test_is_streaming_is_empty_and_input_files_are_asked_once_per_dataframe_as_pyspark` - Live (`python/tests/test_dropin_e2e.py`, placed before `test_sample_split_and_sample_by_without_a_seed_follow_python_random_as_pyspark`): - `test_answers_that_pyspark_keeps_are_asked_once_per_dataframe_as_pyspark`, with `semanticHash`; - `test_an_observation_of_a_year_month_interval_fails_the_action_as_pyspark`. - Changed existing tests: - `EVERY_TYPE` drops the two nanosecond instances. - `test_timestamp_nanos_types_are_private_types_of_their_own` replaces `test_timestamp_nanos_types_roundtrip_and_pickle`. - `test_is_streaming_asks_the_server_as_pyspark` asks a new DataFrame for the second answer. - The reviewers' probes, against a local Spark 4.3.0 server, now give the same output for official PySpark and ours. Two differences remain: the Column operators raise `UNSUPPORTED_DATA_TYPE` where official raises an incidental `AttributeError`, and the nested UTF-8 order, which a separate change fixes. - Checks: - the build of all tests, also with `wasm-udf` and `datafusion,polars`, without warnings; - the lib tests of both crates (732 passed); - `cargo fmt --all --check`, and clippy with no diagnostics on the changed lines; - the offline Python suites (3040 passed, 8 skipped); - the live drop-in, Arrow and ML suites against a local Spark 4.3.0 server (267 passed). ### 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]
