HyukjinKwon opened a new pull request, #557:
URL: https://github.com/apache/spark-connect-rust/pull/557

   ### What changes were proposed in this pull request?
   
   [SPARK-61523](https://issues.apache.org/jira/browse/SPARK-61523): follow-ups 
of a post-hoc review of the SPARK-61483 follow-up #546, checked against PySpark 
4.3.0. PySpark passes the values of these fields to PyArrow as they are. Both 
changes are in `crates/pyspark-rs/src/infer.rs`.
   
   - **A NumPy NaT in a day-time interval field.** `duration_micros` returns 
`None` for a NaT of `timedelta64[us]`, which NumPy holds as the least int64, 
and `arrow_scalar` sends it as null, as PyArrow makes it. This applies wherever 
the value is: a scalar, an element of a NumPy array of an array field, or a 
value of a struct or a map.
   - **The NumPy check of an array value.** `arrow_list` reads a list and a 
tuple first, since neither can be a NumPy array. Only other values go through 
`numpy_array_dimensions`, which now reads `sys` from a cache instead of 
importing it for every value.
   
   ### Why are the changes needed?
   
   - **A wrong value.**
     - `createDataFrame([(np.timedelta64("NaT", "us"),)], "v interval day to 
second").collect()` gave `[Row(v=timedelta(days=-106751992, seconds=71945, 
microseconds=224192))]`. That is -9223372036854775808 microseconds.
     - PySpark gives `[Row(v=None)]`, and its pandas path, as ours, gives None 
too.
     - The same held for `np.array([1, "NaT"], dtype="timedelta64[us]")` in an 
array field, and for a NaT in a list, a struct, a map or a dict row.
     - The defect predates #546: it has been there since SPARK-61106. #546's 
array path reaches it as well.
   - **A slowdown.**
     - #546 made every array value check for a NumPy array first, through an 
import of `sys`.
     - `createDataFrame` of 100,000 rows of `([1, 2, 3],)` in an `array<int>` 
field (the path without a converter) took 75 ms before this change and takes 37 
ms now. That is the time of the converter path (non-nullable fields), which did 
not change (39 ms). Measured under the same Python 3.11 (x86_64), best of 5.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes:
   - A NumPy NaT in a nullable day-time interval field is null, as in PySpark.
   - In a non-nullable field, it now raises the error of a null in a 
non-nullable column ("Column 'v' is declared as non-nullable but contains 
null"). Before, it sent the least int64. PySpark sends PyArrow's null slot 
there, which collects as `timedelta(0)`, a value the data did not have, so ours 
raises instead, as for the NaT of a date.
   
   ### How was this patch tested?
   
   - Offline (`python/tests/test_dropin_offline.py`): new 
`test_a_numpy_nat_of_a_day_time_interval_field_is_null_as_pyarrow`, placed 
after `test_values_of_local_rows_convert_and_raise_as_pyarrow`.
     - It checks the nulls in the Arrow table sent for a scalar, a NumPy array 
element, a struct field and a map value.
     - Without the change, it fails with `datetime.timedelta(days=-106751992, 
...) != None`.
   - Against PySpark 4.3.0 and a local Spark 4.3.0 server, the review's probe 
of 9 NaT cases (scalars, arrays, lists, structs, maps, dict rows and pandas) 
gives the same `collect()` results as PySpark. A NaT of another unit raises 
PySpark's `ArrowNotImplementedError` as before.
   - The array conversions are unchanged: the tests of #546 and of the 
local-rows conversions pass (103 tests selected by `-k "local_rows or 
no_converter or arrow_list or interval or numpy"`).
   - Checks:
     - the lib tests of both crates (840 passed);
     - the build of all tests, also with `wasm-udf` and `datafusion,polars`, 
without warnings;
     - `cargo fmt --all --check`, and clippy with no diagnostics on the changed 
lines;
     - `ruff check`;
     - a full trial on master before the merge.
   
   ### 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]

Reply via email to