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

   ### What changes were proposed in this pull request?
   
   The Rust client now reads an Arrow result that the server declares 
non-nullable but that holds nulls, as PyArrow does. A post-merge review found 
this.
   
   - arrow-rs's IPC reader (`RecordBatch::try_new_with_options`) refuses a 
batch where a non-nullable field holds nulls, at any depth. Such a stream is 
now read again with its fields nullable. Map keys and entries stay 
non-nullable, as arrow-rs requires.
   - The batches of a result then share one schema, so the PyArrow schema check 
still holds.
   - Every decode path goes through one reader (`read_record_batches`): rows 
(`collect`, `toLocalIterator`) and Arrow batches (`collect_record_batches`, 
`toArrow`, `toPandas`, `to_datafusion`, `to_polars`).
   - Other decode errors, such as invalid UTF-8 or truncated data, are reported 
as before, and offsets, lengths and types are still validated.
   
   ### Why are the changes needed?
   
   The server declares `F.col("arr").getItem(None)` (and `arr[None]`) 
non-nullable while every value is null, so `collect()` failed:
   
   ```
   >>> df.select(F.col("arr").getItem(None)).collect()
   PySparkRuntimeError: Failed to decode Arrow batch: Invalid argument error: 
Column 'arr[NULL]' is declared as non-nullable but contains null values
   ```
   
   PySpark 4.3.0 (pyarrow), and this client after the change, return 
`[Row(arr[NULL]=None), Row(arr[NULL]=None)]`. A struct field of such a value 
reads as `Row(x=None)` in both. `toArrow` and `toPandas` of the top-level 
column raise the same errors as PySpark's: PyArrow's "Casting field ... with 
null values to non-nullable", and pandas' IntCastingNaNError. The `toArrow` of 
a struct holding such a field raises `ArrowInvalid` in both, with a different 
message.
   
   One known difference is left: the `toArrow` of a nested non-nullable list 
item that holds nulls (`array(arr.getItem(None))`) returns the table, where 
PySpark raises `ArrowInvalid`. Its rows already match PySpark's.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. A query whose non-nullable result holds nulls returns them instead of 
failing. In the Rust API, the batches of such a result 
(`collect_record_batches`, `to_datafusion`, `to_polars`) carry nullable fields.
   
   ### How was this patch tested?
   
   New tests, each checked to fail without the change:
   - Rust unit tests with hand-built streams that put a non-nullable field 
holding nulls at the top level and inside lists, structs and maps.
   - Rust e2e: 
`null_get_item_reads_its_nulls_though_the_server_declares_it_non_nullable`.
   - Python: an offline test with the mock server, and an e2e test.
   
   Guard tests check that a stream with nulls only in nullable fields, a stream 
with another error, and a stream with an invalid string all behave as before.
   
   Also passing against Spark 4.3.0, with `TZ=UTC`:
   - the Rust lib tests and all Rust e2e suites;
   - CI's offline Python set (3280 passed);
   - `test_arrow_e2e.py` and `test_dropin_e2e.py` (288 passed);
   - `cargo fmt`, the stub audit and ruff.
   
   The lib tests and the new tests pass again on the tree merged with the 
latest master.
   
   ### 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