tdcmeehan opened a new pull request, #448: URL: https://github.com/apache/spark-connect-rust/pull/448
### What changes were proposed in this pull request? [SPARK-61264](https://issues.apache.org/jira/browse/SPARK-61264): `collect()` of a string that is not valid UTF-8 raises a `PySparkRuntimeError` ("Failed to decode Arrow batch") in the Python client. The reference client raises the `UnicodeDecodeError` of Python's own decoder for the bytes. - `crates/spark-connect/src/dataframe.rs`: the normal decode is unchanged and still validates every batch. Only when arrow-rs fails a batch for its UTF-8 check, `decode_error` decodes the stream again with its string types read as binary types of the same layout (`binary_schema`, `binary_type`, `read_strings_as_binary`). The stream is split into its messages as arrow-rs's stream reader does, and each record batch is read with `read_record_batch` and the rewritten schema. So arrow-rs still checks the offsets and lengths of every batch, and only the UTF-8 check is not made; no `unsafe` is used. The first invalid value is then found in the order in which PySpark converts a result: column by column, then row by row, through lists, maps and structs (`first_invalid_utf8`, `invalid_utf8_in`). A stream with a dictionary, or one that does not read, keeps the previous error. `collect_with_schema` keeps the error of the first column across the batches of a result, and reads the rest of the s tream for it. - `crates/spark-connect-core/src/error.rs`: the new `SparkErrorKind::UnicodeDecode` (as `RecursionError` is), with `SparkError::invalid_utf8` and accessors for its column and bytes. - `crates/pyspark-rs/src/errors.rs`: the Python client raises the error of `bytes.decode("utf-8")` for the bytes, so the message is Python's own. `collect`, `first`, `head`, `take` and `toLocalIterator` now raise the same `UnicodeDecodeError` as PySpark, for a top-level string, a string in an array, struct or map, and in any column. Nested values are ordered column by column, then row by row, which matches PySpark for a single invalid value; with several in one column, the reported one can differ from PySpark's, whose bulk path converts struct fields field by field. Out of scope: `toArrow` and `toPandas`, which are unchanged here. PySpark's `toArrow` does not check the strings: it returns the table with the invalid bytes. PySpark's `toPandas` raises `pyarrow.lib.ArrowException: Unknown error: Wrapping � failed`. Here both still raise the `PySparkRuntimeError` of the Arrow decode. ### Why are the changes needed? A collected string that is not valid UTF-8 can come from a cast of binary to string. The Python client raised `PySparkRuntimeError`, which is not a `ValueError` either, so code that catches `UnicodeDecodeError` or `ValueError` missed it. The reference client raises `UnicodeDecodeError`. ### Does this PR introduce _any_ user-facing change? Yes, in the Python client: `collect`, `first`, `head`, `take` and `toLocalIterator` raise `UnicodeDecodeError` with the message `'utf-8' codec can't decode byte 0xc3 in position 0: unexpected end of data` for `SELECT CAST(X'C3' AS STRING)`, instead of `PySparkRuntimeError: Failed to decode Arrow batch: Invalid argument error: Invalid UTF8 sequence at string index 0 (0..1): incomplete utf-8 byte sequence from index 0`. Valid strings are unchanged. In Rust: the collect of such a result returns the new `SparkErrorKind::UnicodeDecode` (as `RecursionError` is added), whose message names the column. The enum has one more variant, so exhaustive matches on it need an arm. ### How was this patch tested? - Offline: `test_collect_of_a_string_that_is_not_utf8_raises_as_pyspark` in `python/tests/test_dropin_offline.py`, with a crafted Utf8 column whose bytes pyarrow does not check: a top-level string, an array of strings, and two columns in which the first column's invalid value is not in row 0. It asserts the exception type and the message of Python's decoder for the bytes, and a valid control. - Rust: `invalid_utf8_names_the_first_column_with_it_and_its_bytes` in `nested_decode_tests` (`crates/spark-connect/src/dataframe.rs`): a string in a list of the second column, and a top-level string in the first, with their bytes. - Live: `test_collect_of_a_string_that_is_not_utf8_raises_as_pyspark` in `python/tests/test_dropin_e2e.py` against OSS Spark 4.3.0, with `CAST(X'C3' AS STRING)` and the valid `CAST(X'61' AS STRING)`. - Compared with pyspark-client 4.3.0 on the same server for `collect`, `first`, `head`, `head(1)`, `take(1)` and `toLocalIterator`, on a top-level string, a string in an array, in a struct, a map key and a map value, a second-row string, and two columns: the same exception and message for each. - Offline Python suites (`tests/test_dropin_offline.py tests/test_ml_offline.py tests/test_arrow_handoff.py`): 2928 passed, 8 skipped. - Live Python suites against OSS Spark 4.3.0 (`tests/test_dropin_e2e.py tests/test_arrow_e2e.py tests/test_ml_e2e.py`): 237 passed, 1 skipped. - Rust: `cargo test -p apache-spark-connect --release --lib`: 529 passed. `--no-run`, and `--no-run` with `--features wasm-udf`: pass. `cargo fmt --all --check`: pass. `cargo clippy --workspace --all-targets --release`: no diagnostic on a changed line (the diagnostics on master in `pyspark-rs` are about the size of `SparkError`, and are unchanged). - Fails without the fix: with the UTF-8 branch disabled, the Rust unit test gets no column (`None`, expected `Some(1)`), and the offline Python test fails with `PySparkRuntimeError`. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Opus 5.5 and Claude Haiku 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]
