HyukjinKwon opened a new pull request, #427: URL: https://github.com/apache/spark-connect-rust/pull/427
### What changes were proposed in this pull request? [SPARK-61249](https://issues.apache.org/jira/browse/SPARK-61249): Raise for out-of-range integers in `createDataFrame` and collect nanosecond timestamps in microseconds, raising in the Python client for them as PySpark does. - `createDataFrame` (`crates/spark-connect/src/session.rs`): an integer value that does not fit its declared integer type is an error naming the value and the type, instead of an `as` cast that wraps it. In-range narrowing and widening are unchanged. The float encoder is unchanged: a finite double beyond the float range becomes infinity, as official PySpark 4.3.0 gives (`inf` for 1e300 into FLOAT, with no error). - Collect of `TIMESTAMP_NTZ(p)` and `TIMESTAMP_LTZ(p)` with p > 6 (`crates/spark-connect/src/dataframe.rs`): the nanoseconds are floored to microseconds (`div_euclid`) instead of truncated toward zero. The collecting API and the `Value` variants document that collect gives microseconds, while `to_arrow` keeps nanoseconds. - Python drop-in (`crates/pyspark-rs/src/dataframe.rs`): `collect`, `first`, `head`, `take`, `tail`, `toLocalIterator`, `toPandas` and `toArrow` raise `PySparkValueError` `UNSUPPORTED_OPERATION` for a result whose schema holds a nanosecond timestamp, nested or not, with the message PySpark gives (`data type timestamp_ntz_nanos {\n precision: 9\n}\n is not supported.`). `count` and `show` keep working, as in PySpark. Every other type is unchanged. ### Why are the changes needed? - `spark.createDataFrame([(300,)], "b TINYINT").collect()` returned `Row(b=44)` on master, and `4294967297` into an `INT` column returned `1`. PySpark 4.3.0 raises `ArrowInvalid` ("Value 300 too large to fit in C integer type") for these, and so does the Python drop-in, which checks first. - `spark.sql("SELECT CAST('1969-12-31 23:59:59.1234567' AS TIMESTAMP_NTZ(7))").collect()` returned the microsecond value -876543 for the correct floor -876544, and the sub-microsecond digits were dropped silently. PySpark 4.3.0 refuses to collect these types with `PySparkValueError` `UNSUPPORTED_OPERATION`, from `collect`, `first`, `head`, `take`, `tail`, `toLocalIterator`, `toPandas` and `toArrow`, and the drop-in returned the values. ### Does this PR introduce _any_ user-facing change? Yes. - An out-of-range integer in `createDataFrame` through the Rust API is an error, not a wrapped value. - Collecting a `TIMESTAMP_NTZ(p)` or `TIMESTAMP_LTZ(p)` value with p > 6 raises in the Python client as in PySpark. The Rust API returns microseconds, floored. - Not changed: the drop-in's `schema` property. PySpark 4.3.0 raises for these types in `df.schema` too (checked; `columns` not checked); this PR leaves `schema` unchanged. ### How was this patch tested? - Rust unit tests in `crates/spark-connect/src/session.rs` (`mod tests`): `integers_that_do_not_fit_the_declared_type_are_errors_not_wrapped` (7 out-of-range cases for TINYINT, SMALLINT, INT and BIGINT, for the value and the batch path), `integers_that_fit_the_declared_type_still_narrow_and_widen`, and `a_double_beyond_the_float_range_is_infinite_as_in_pyspark`. - Rust unit test in `crates/spark-connect/src/dataframe.rs` (`mod arrow_value_tests`): `nanosecond_timestamps_floor_to_micros_at_top_level_and_nested`, at top level, in a struct and in an array. The existing `typed_scalar_layouts_preserve_exact_values_and_normalization` expectation for -1999 ns changes from -1 to -2 us, which is the floored value. - Python live test `test_nanosecond_timestamps_are_not_collected_as_pyspark` in `python/tests/test_dropin_e2e.py`: five queries (NTZ(9), LTZ(7), a struct, an array, a map) through the nine APIs, and `count`, `show` and an NTZ(6) collect that still work. - Python offline test `test_nanosecond_timestamps_are_not_collected_as_pyspark` in `python/tests/test_dropin_offline.py`, with a mock server: top level, array and struct nesting through the collecting APIs and `toArrow`. - The new Python tests fail without the guard (21 offline, 1 live). The new Rust tests fail with the wrapping cast and the truncating division restored. - The same cases give the same results with official PySpark 4.3.0 on the same server: the integer cases raise `ArrowInvalid`, 1e300 into FLOAT gives `inf`, and the nanosecond timestamps raise `PySparkValueError` `UNSUPPORTED_OPERATION` in each API above (`count` and `show` work). - The live Rust tests `e2e_dataframe` (4 passed), `e2e_coverage` (25 passed) and `e2e_integration` (17 passed) against a local Spark 4.3.0 server. - The lib tests of both crates (668 passed), the build of all tests, also with `wasm-udf`, the offline Python suites (2840 passed, 8 skipped), and the live drop-in and Arrow suites (234 passed) and live cases (13 passed) against a local Spark 4.3.0 server. Clippy reports no diagnostics in the changed lines. ### 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]
