This is an automated email from the ASF dual-hosted git repository.

HyukjinKwon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark-connect-rust.git


The following commit(s) were added to refs/heads/master by this push:
     new 6bfd8a0a [SPARK-61610][CONNECT][PYTHON] Read the nulls of a column 
that the server declares non-nullable, as PyArrow does, in Spark Connect Rust
6bfd8a0a is described below

commit 6bfd8a0a5ac419607c49735ea3c13d51d3b32d57
Author: Hyukjin Kwon <[email protected]>
AuthorDate: Fri Nov 13 13:00:07 2026 +0900

    [SPARK-61610][CONNECT][PYTHON] Read the nulls of a column that the server 
declares non-nullable, as PyArrow does, in Spark Connect Rust
    
    ### 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.
    
    Closes #606 from HyukjinKwon/SPARK-61610-nullable-nulls.
    
    Authored-by: Hyukjin Kwon <[email protected]>
    Signed-off-by: Hyukjin Kwon <[email protected]>
---
 crates/spark-connect/src/dataframe.rs     | 482 +++++++++++++++++++++++++-----
 crates/spark-connect/tests/e2e_columns.rs |  47 +++
 python/tests/test_dropin_e2e.py           |  15 +
 python/tests/test_dropin_offline.py       |  18 ++
 4 files changed, 488 insertions(+), 74 deletions(-)

diff --git a/crates/spark-connect/src/dataframe.rs 
b/crates/spark-connect/src/dataframe.rs
index ecb67ab3..961e1a04 100644
--- a/crates/spark-connect/src/dataframe.rs
+++ b/crates/spark-connect/src/dataframe.rs
@@ -1103,6 +1103,7 @@ impl DataFrame {
 
         let mut batches = vec![];
         let mut schema = None;
+        let mut relaxed = None;
         let mut info = ExecutionInfo::default();
         loop {
             let resp = stream.next_message()?;
@@ -1117,14 +1118,14 @@ impl DataFrame {
             if let 
Some(proto::execute_plan_response::ResponseType::ArrowBatch(batch)) =
                 resp.response_type
             {
-                let record_batches =
+                let (record_batches, read) =
                     decode_arrow_record_batches(&batch).map_err(|e| 
stream.fail(e))?;
                 batches.extend(record_batches);
+                relaxed = read.or(relaxed);
             }
         }
         self.record_execution(info);
-
-        Ok((batches, schema))
+        Ok((share_schema(batches, relaxed)?, schema))
     }
 
     /// Get the count of rows.
@@ -3104,25 +3105,11 @@ pub(crate) fn decode_arrow_batch_of(
     batch: &proto::execute_plan_response::ArrowBatch,
     spark_schema: Option<&DataType>,
 ) -> Result<Vec<Row>> {
-    use arrow::ipc::reader::StreamReader;
-    use std::io::Cursor;
-
-    if batch.data.is_empty() {
-        return Ok(vec![]);
-    }
-
-    let cursor = Cursor::new(&batch.data);
-    let mut reader = StreamReader::try_new(cursor, None).map_err(|e| {
-        SparkError::connect_msg(format!("Failed to create Arrow stream reader: 
{}", e))
-    })?;
+    let (record_batches, _) = read_record_batches(&batch.data, decode_error)?;
 
     let mut rows = vec![];
 
-    while let Some(record_batch) = reader
-        .next()
-        .transpose()
-        .map_err(|e| decode_error(&batch.data, e))?
-    {
+    for record_batch in record_batches {
         let schema = record_batch.schema();
         let num_rows = record_batch.num_rows();
         if num_rows == 0 {
@@ -3160,6 +3147,80 @@ pub(crate) fn decode_arrow_batch_of(
     Ok(rows)
 }
 
+/// The record batches of the Arrow IPC stream `data`, and the schema of them 
when they are read
+/// with the fields nullable, as [`read_stream_relaxed`] reads them.
+///
+/// arrow-rs refuses a batch with nulls in a non-nullable field, at any depth, 
but the server
+/// declares such a field for values that are nulls, as `arr.getItem(None)` 
does, and PyArrow
+/// reads them: such a stream is read again with its fields nullable, and the 
batches of it have
+/// that schema. Any other error of a batch is `batch_error` of it.
+fn read_record_batches(
+    data: &[u8],
+    batch_error: impl Fn(&[u8], arrow::error::ArrowError) -> SparkError,
+) -> Result<(
+    Vec<arrow::record_batch::RecordBatch>,
+    Option<arrow::datatypes::SchemaRef>,
+)> {
+    use arrow::ipc::reader::StreamReader;
+    use std::io::Cursor;
+
+    if data.is_empty() {
+        return Ok((vec![], None));
+    }
+
+    let reader = StreamReader::try_new(Cursor::new(data), None).map_err(|e| {
+        SparkError::connect_msg(format!("Failed to create Arrow stream reader: 
{}", e))
+    })?;
+
+    let mut batches = vec![];
+    for item in reader {
+        match item {
+            Ok(batch) => batches.push(batch),
+            Err(error) => {
+                let relaxed = is_non_nullable_error(&error)
+                    .then(|| read_stream_relaxed(data, false))
+                    .flatten();
+                return match relaxed {
+                    Some((_, relaxed)) => {
+                        let schema = relaxed.first().map(|batch| 
batch.schema());
+                        Ok((relaxed, schema))
+                    }
+                    // The strings of a stream that does not read with its 
fields nullable are
+                    // checked as those of any other, so that an invalid one 
is still named.
+                    None => Err(match first_invalid_utf8(data) {
+                        Some((column, value)) => 
SparkError::invalid_utf8(column, &value),
+                        None => batch_error(data, error),
+                    }),
+                };
+            }
+        }
+    }
+    Ok((batches, None))
+}
+
+/// The batches of a result, which share one schema: when some were read with 
their fields
+/// nullable, as `relaxed` is, each batch takes `relaxed`. A batch read 
without that relaxation
+/// only has its fields widened to nullable, which `with_schema` allows, so no 
data changes.
+fn share_schema(
+    batches: Vec<arrow::record_batch::RecordBatch>,
+    relaxed: Option<arrow::datatypes::SchemaRef>,
+) -> Result<Vec<arrow::record_batch::RecordBatch>> {
+    let Some(relaxed) = relaxed else {
+        return Ok(batches);
+    };
+    batches
+        .into_iter()
+        .map(|batch| batch.with_schema(relaxed.clone()))
+        .collect::<std::result::Result<_, _>>()
+        .map_err(|e| SparkError::connect_msg(format!("Failed to decode Arrow 
batch: {e}")))
+}
+
+/// Whether `error` is arrow-rs refusing a non-nullable field, at any depth, 
for its nulls.
+fn is_non_nullable_error(error: &arrow::error::ArrowError) -> bool {
+    let message = error.to_string();
+    message.contains("non-nullable") || message.contains("Non-nullable")
+}
+
 /// The error of an Arrow IPC stream `data` that arrow-rs fails with `error`. 
A string that is not
 /// valid UTF-8 is an error of its own, with its bytes, which the Python 
client raises as the
 /// reference client does.
@@ -3177,7 +3238,7 @@ fn decode_error(data: &[u8], error: 
arrow::error::ArrowError) -> SparkError {
 /// its bytes, in the order in which PySpark converts a result: column by 
column, each batch by
 /// batch, in the order of [`first_invalid_utf8_in`].
 fn first_invalid_utf8(data: &[u8]) -> Option<(usize, Vec<u8>)> {
-    let (strings, batches) = read_strings_as_binary(data)?;
+    let (strings, batches) = read_stream_relaxed(data, true)?;
     let columns = batches.first()?.num_columns();
     (0..columns).find_map(|column| {
         let data_type = strings.field(column).data_type();
@@ -3188,13 +3249,16 @@ fn first_invalid_utf8(data: &[u8]) -> Option<(usize, 
Vec<u8>)> {
     })
 }
 
-/// The schema of the Arrow IPC stream `data`, as the server sends it, and its 
record batches, with
-/// its string types read as binary types of the same layout. arrow-rs still 
checks the offsets and
-/// lengths of these batches, but not their UTF-8. The messages are split here 
because arrow-rs's
-/// stream reader cannot take a schema of its own. None for a stream with a 
message that is not a
-/// schema or a record batch, such as a dictionary, or one that does not read.
-fn read_strings_as_binary(
+/// The schema of the Arrow IPC stream `data`, as the server sends it, and its 
record batches, read
+/// with each field nullable (see [`read_schema`]) and, if 
`strings_as_binary`, with its string
+/// types read as binary types of the same layout. arrow-rs still checks the 
offsets and lengths of
+/// these batches, but not their UTF-8 when they are read as binary, nor their 
nulls. The messages
+/// are split here because arrow-rs's stream reader cannot take a schema of 
its own. None for a
+/// stream with a message that is not a schema or a record batch, such as a 
dictionary, or one that
+/// does not read.
+fn read_stream_relaxed(
     data: &[u8],
+    strings_as_binary: bool,
 ) -> Option<(
     arrow::datatypes::Schema,
     Vec<arrow::record_batch::RecordBatch>,
@@ -3232,7 +3296,7 @@ fn read_strings_as_binary(
             MessageHeader::Schema if strings.is_some() => return None,
             MessageHeader::Schema => {
                 let fields = fb_to_schema(message.header_as_schema()?);
-                schema = Some(Arc::new(binary_schema(&fields)));
+                schema = Some(Arc::new(read_schema(&fields, 
strings_as_binary)));
                 strings = Some(fields);
             }
             MessageHeader::RecordBatch => {
@@ -3253,46 +3317,93 @@ fn read_strings_as_binary(
     Some((strings?, batches))
 }
 
-/// The schema `schema`, with each string type of it, at any depth, as the 
binary type of the same
-/// layout: Utf8 as Binary, LargeUtf8 as LargeBinary and Utf8View as 
BinaryView.
-fn binary_schema(schema: &arrow::datatypes::Schema) -> 
arrow::datatypes::Schema {
+/// The schema `schema` as its batches are read by [`read_stream_relaxed`]: 
each field nullable,
+/// at any depth (see [`read_field`]), and, if `strings_as_binary`, each 
string type of it as the
+/// binary type of the same layout (see [`read_type`]).
+fn read_schema(
+    schema: &arrow::datatypes::Schema,
+    strings_as_binary: bool,
+) -> arrow::datatypes::Schema {
     let fields: Vec<arrow::datatypes::Field> = schema
         .fields()
         .iter()
-        .map(|field| binary_field(field))
+        .map(|field| read_field(field, strings_as_binary))
         .collect();
     arrow::datatypes::Schema::new_with_metadata(fields, 
schema.metadata().clone())
 }
 
-fn binary_field(field: &arrow::datatypes::Field) -> arrow::datatypes::Field {
-    field.clone().with_data_type(binary_type(field.data_type()))
+/// The field `field` as it is read: nullable, so that arrow-rs does not 
refuse its nulls, with
+/// its type as [`read_type`] reads it.
+fn read_field(field: &arrow::datatypes::Field, strings_as_binary: bool) -> 
arrow::datatypes::Field {
+    field
+        .clone()
+        .with_data_type(read_type(field.data_type(), strings_as_binary))
+        .with_nullable(true)
 }
 
-fn binary_type(data_type: &arrow::datatypes::DataType) -> 
arrow::datatypes::DataType {
+/// The type `data_type` as it is read: its string types, at any depth, as the 
binary types of the
+/// same layout if `strings_as_binary` (Utf8 as Binary, LargeUtf8 as 
LargeBinary and Utf8View as
+/// BinaryView), and its fields nullable (see [`read_field`]), except the 
entries of a map, whose
+/// keys arrow-rs requires to be non-nullable (see [`read_map_entries`]).
+fn read_type(
+    data_type: &arrow::datatypes::DataType,
+    strings_as_binary: bool,
+) -> arrow::datatypes::DataType {
     use arrow::datatypes::DataType as ArrowDataType;
 
+    let read = |field: &arrow::datatypes::Field| read_field(field, 
strings_as_binary);
     match data_type {
-        ArrowDataType::Utf8 => ArrowDataType::Binary,
-        ArrowDataType::LargeUtf8 => ArrowDataType::LargeBinary,
-        ArrowDataType::Utf8View => ArrowDataType::BinaryView,
-        ArrowDataType::List(field) => 
ArrowDataType::List(Arc::new(binary_field(field))),
-        ArrowDataType::LargeList(field) => 
ArrowDataType::LargeList(Arc::new(binary_field(field))),
+        ArrowDataType::Utf8 if strings_as_binary => ArrowDataType::Binary,
+        ArrowDataType::LargeUtf8 if strings_as_binary => 
ArrowDataType::LargeBinary,
+        ArrowDataType::Utf8View if strings_as_binary => 
ArrowDataType::BinaryView,
+        ArrowDataType::List(field) => 
ArrowDataType::List(Arc::new(read(field))),
+        ArrowDataType::LargeList(field) => 
ArrowDataType::LargeList(Arc::new(read(field))),
         ArrowDataType::FixedSizeList(field, size) => {
-            ArrowDataType::FixedSizeList(Arc::new(binary_field(field)), *size)
-        }
-        ArrowDataType::Map(field, sorted) => {
-            ArrowDataType::Map(Arc::new(binary_field(field)), *sorted)
+            ArrowDataType::FixedSizeList(Arc::new(read(field)), *size)
         }
-        ArrowDataType::Struct(fields) => ArrowDataType::Struct(
-            fields
-                .iter()
-                .map(|field| Arc::new(binary_field(field)))
-                .collect(),
+        ArrowDataType::Map(entries, sorted) => ArrowDataType::Map(
+            Arc::new(read_map_entries(entries, strings_as_binary)),
+            *sorted,
         ),
+        ArrowDataType::Struct(fields) => {
+            ArrowDataType::Struct(fields.iter().map(|field| 
Arc::new(read(field))).collect())
+        }
         other => other.clone(),
     }
 }
 
+/// The entries field of a map as it is read (see [`read_type`]): arrow-rs 
requires the entries
+/// to be non-nullable and their key too, as a map has no null entry or key, 
so only the value is
+/// read as any other field.
+fn read_map_entries(
+    entries: &arrow::datatypes::Field,
+    strings_as_binary: bool,
+) -> arrow::datatypes::Field {
+    use arrow::datatypes::DataType as ArrowDataType;
+
+    let ArrowDataType::Struct(children) = entries.data_type() else {
+        return entries.clone();
+    };
+    let children = children
+        .iter()
+        .enumerate()
+        .map(|(i, child)| {
+            let child = if i == 0 {
+                child
+                    .as_ref()
+                    .clone()
+                    .with_data_type(read_type(child.data_type(), 
strings_as_binary))
+            } else {
+                read_field(child, strings_as_binary)
+            };
+            Arc::new(child)
+        })
+        .collect();
+    entries
+        .clone()
+        .with_data_type(ArrowDataType::Struct(children))
+}
+
 /// The bytes of the first string of `array` that is not valid UTF-8, in the 
order in which
 /// PySpark's `ArrowTableToRowsConversion._to_pylist` converts an array, in 
bulk: a list by the
 /// values of all its rows, a struct field by field, each over all its rows, 
and a map by the keys
@@ -3388,30 +3499,13 @@ fn invalid_bytes(bytes: &[u8]) -> Option<Vec<u8>> {
 /// to DataFusion and Polars.
 fn decode_arrow_record_batches(
     batch: &proto::execute_plan_response::ArrowBatch,
-) -> Result<Vec<arrow::record_batch::RecordBatch>> {
-    use arrow::ipc::reader::StreamReader;
-    use std::io::Cursor;
-
-    if batch.data.is_empty() {
-        return Ok(vec![]);
-    }
-
-    let cursor = Cursor::new(&batch.data);
-    let mut reader = StreamReader::try_new(cursor, None).map_err(|e| {
-        SparkError::connect_msg(format!("Failed to create Arrow stream reader: 
{}", e))
-    })?;
-
-    let mut batches = vec![];
-
-    while let Some(record_batch) = reader
-        .next()
-        .transpose()
-        .map_err(|e| SparkError::connect_msg(format!("Failed to decode Arrow 
batch: {}", e)))?
-    {
-        batches.push(record_batch);
-    }
-
-    Ok(batches)
+) -> Result<(
+    Vec<arrow::record_batch::RecordBatch>,
+    Option<arrow::datatypes::SchemaRef>,
+)> {
+    read_record_batches(&batch.data, |_, error| {
+        SparkError::connect_msg(format!("Failed to decode Arrow batch: {}", 
error))
+    })
 }
 
 /// Extract a value at a specific index from an Arrow array.
@@ -3998,8 +4092,8 @@ mod nested_decode_tests {
             };
             assert_eq!(decode_arrow_batch(&compressed).unwrap(), rows, 
"{codec:?}");
             assert_eq!(
-                decode_arrow_record_batches(&compressed).unwrap(),
-                decode_arrow_record_batches(&plain).unwrap(),
+                decode_arrow_record_batches(&compressed).unwrap().0,
+                decode_arrow_record_batches(&plain).unwrap().0,
                 "{codec:?}"
             );
         }
@@ -6991,6 +7085,246 @@ mod plan_construction_tests {
 /// each Arrow type and asserts the decoded `Value` variant, so the large 
per-type
 /// match runs without a live server (the server->collect->decode path is 
covered
 /// separately by the server-gated e2e_integration tests).
+/// Streams that declare a field non-nullable but carry nulls in it, as the 
server does for
+/// `arr.getItem(None)`: arrow-rs refuses them when it reads them, and PyArrow 
reads them as is.
+#[cfg(test)]
+mod non_nullable_decode_tests {
+    use super::*;
+    use arrow::array::*;
+    use arrow::buffer::{NullBuffer, OffsetBuffer};
+    use arrow::datatypes::{DataType, Field, Fields, Schema};
+    use arrow::ipc::writer::StreamWriter;
+    use arrow::record_batch::RecordBatch;
+
+    /// The Arrow IPC stream of one batch of `columns`, each with the field it 
is declared as. The
+    /// writer declares the fields, and the arrays carry the values, as the 
server does: a
+    /// declared field may be non-nullable while its array has nulls, which 
arrow-rs's writer does
+    /// not check.
+    fn stream_declaring(
+        columns: Vec<(Field, ArrayRef)>,
+    ) -> proto::execute_plan_response::ArrowBatch {
+        let schema = Arc::new(Schema::new(
+            columns
+                .iter()
+                .map(|(field, _)| field.clone())
+                .collect::<Vec<_>>(),
+        ));
+        let batch = RecordBatch::try_new(
+            Arc::new(Schema::new(
+                columns
+                    .iter()
+                    .map(|(field, array)| Field::new(field.name(), 
array.data_type().clone(), true))
+                    .collect::<Vec<_>>(),
+            )),
+            columns.iter().map(|(_, array)| array.clone()).collect(),
+        )
+        .unwrap();
+        let mut data = vec![];
+        {
+            let mut writer = StreamWriter::try_new(&mut data, 
&schema).unwrap();
+            writer.write(&batch).unwrap();
+            writer.finish().unwrap();
+        }
+        proto::execute_plan_response::ArrowBatch {
+            data: data.into(),
+            ..Default::default()
+        }
+    }
+
+    fn declared(name: &str, data_type: DataType) -> Field {
+        Field::new(name, data_type, false)
+    }
+
+    /// `[1, null, 3]` as a non-nullable column of `arr.getItem(None)`, next 
to a nullable `id`.
+    fn null_column_stream() -> proto::execute_plan_response::ArrowBatch {
+        stream_declaring(vec![
+            (
+                declared("arr[NULL]", DataType::Int64),
+                Arc::new(Int64Array::from(vec![None, None, None])),
+            ),
+            (
+                Field::new("id", DataType::Int64, true),
+                Arc::new(Int64Array::from(vec![Some(0), Some(1), Some(2)])),
+            ),
+        ])
+    }
+
+    #[test]
+    fn a_non_nullable_column_with_nulls_decodes_as_rows() {
+        let rows = decode_arrow_batch_of(&null_column_stream(), None).unwrap();
+        let values: Vec<_> = rows.iter().map(|row| 
row.values().to_vec()).collect();
+        assert_eq!(
+            values,
+            vec![
+                vec![Value::Null, Value::Long(0)],
+                vec![Value::Null, Value::Long(1)],
+                vec![Value::Null, Value::Long(2)],
+            ]
+        );
+    }
+
+    #[test]
+    fn 
a_non_nullable_column_with_nulls_reads_as_record_batches_with_the_nulls() {
+        let (batches, relaxed) = 
decode_arrow_record_batches(&null_column_stream()).unwrap();
+        let schema = relaxed.expect("the stream is read with its fields 
nullable");
+        assert_eq!(batches.len(), 1);
+        let batch = &batches[0];
+        assert_eq!(batch.schema(), schema);
+        assert!(batch.schema().field(0).is_nullable());
+        assert_eq!(batch.column(0).null_count(), 3);
+        assert_eq!(batch.column(1).null_count(), 0);
+    }
+
+    #[test]
+    fn a_stream_without_nulls_in_a_non_nullable_column_is_read_as_declared() {
+        let batch = stream_declaring(vec![(
+            declared("n", DataType::Int64),
+            Arc::new(Int64Array::from(vec![1, 2])),
+        )]);
+        let (batches, relaxed) = decode_arrow_record_batches(&batch).unwrap();
+        assert!(relaxed.is_none());
+        assert!(!batches[0].schema().field(0).is_nullable());
+    }
+
+    #[test]
+    fn non_nullable_fields_with_nulls_decode_at_any_depth() {
+        // A list whose item is non-nullable, with a null item.
+        let list = Arc::new(ListArray::new(
+            Arc::new(Field::new("item", DataType::Int64, true)),
+            OffsetBuffer::from_lengths([2, 1]),
+            Arc::new(Int64Array::from(vec![None, Some(7), None])),
+            None,
+        )) as ArrayRef;
+        let list_type = DataType::List(Arc::new(declared("item", 
DataType::Int64)));
+        // A struct whose field is non-nullable, with a null field, and a null 
struct.
+        let struct_array = Arc::new(StructArray::new(
+            Fields::from(vec![Field::new("x", DataType::Int64, true)]),
+            vec![Arc::new(Int64Array::from(vec![None, Some(1)])) as ArrayRef],
+            Some(NullBuffer::from(vec![true, false])),
+        )) as ArrayRef;
+        let struct_type = DataType::Struct(Fields::from(vec![declared("x", 
DataType::Int64)]));
+        // A map whose value is non-nullable, with a null value.
+        let entries = StructArray::new(
+            Fields::from(vec![
+                Field::new("key", DataType::Utf8, false),
+                Field::new("value", DataType::Int64, true),
+            ]),
+            vec![
+                Arc::new(StringArray::from(vec!["a", "b"])) as ArrayRef,
+                Arc::new(Int64Array::from(vec![None, Some(2)])) as ArrayRef,
+            ],
+            None,
+        );
+        let map = Arc::new(MapArray::new(
+            Arc::new(Field::new("entries", entries.data_type().clone(), 
false)),
+            OffsetBuffer::from_lengths([1, 1]),
+            entries,
+            None,
+            false,
+        )) as ArrayRef;
+        let map_type = DataType::Map(
+            Arc::new(Field::new(
+                "entries",
+                DataType::Struct(Fields::from(vec![
+                    Field::new("key", DataType::Utf8, false),
+                    declared("value", DataType::Int64),
+                ])),
+                false,
+            )),
+            false,
+        );
+
+        let batch = stream_declaring(vec![
+            (declared("l", list_type), list),
+            (declared("s", struct_type), struct_array),
+            (declared("m", map_type), map),
+        ]);
+        let rows = decode_arrow_batch_of(&batch, None).unwrap();
+        let values: Vec<_> = rows.iter().map(|row| 
row.values().to_vec()).collect();
+        assert_eq!(
+            values,
+            vec![
+                vec![
+                    Value::List(vec![Value::Null, Value::Long(7)]),
+                    Value::Struct(StructValue::new(
+                        Arc::from(vec!["x".to_string()]),
+                        vec![Value::Null],
+                    )),
+                    Value::Map(vec![(Value::String("a".into()), Value::Null)]),
+                ],
+                vec![
+                    Value::List(vec![Value::Null]),
+                    Value::Null,
+                    Value::Map(vec![(Value::String("b".into()), 
Value::Long(2))]),
+                ],
+            ]
+        );
+    }
+
+    #[test]
+    fn a_non_nullable_column_with_an_invalid_string_names_the_string() {
+        // The marker of the string is replaced by bytes that are not UTF-8, 
of the same length.
+        let mut batch = stream_declaring(vec![
+            (
+                declared("n", DataType::Int64),
+                Arc::new(Int64Array::from(vec![None, None])),
+            ),
+            (
+                Field::new("s", DataType::Utf8, true),
+                Arc::new(StringArray::from(vec!["x", "qz9"])),
+            ),
+        ]);
+        let mut data = batch.data.to_vec();
+        let at = data.windows(3).position(|window| window == b"qz9").unwrap();
+        data[at..at + 3].copy_from_slice(&[0xc3, 0x28, 0x39]);
+        batch.data = data.into();
+        let Err(error) = decode_arrow_batch_of(&batch, None) else {
+            panic!("the invalid string must not decode");
+        };
+        assert_eq!(error.utf8_column(), Some(1));
+        assert_eq!(error.invalid_utf8_value(), Some(vec![0xc3, 0x28, 0x39]));
+    }
+
+    #[test]
+    fn a_stream_of_another_error_still_fails() {
+        // Truncated data is not a nullability error, so it is not read again.
+        let mut batch = null_column_stream();
+        let len = batch.data.len();
+        batch.data = batch.data.slice(..len - 8);
+        let Err(error) = decode_arrow_batch_of(&batch, None) else {
+            panic!("the truncated stream must not decode");
+        };
+        assert!(error.to_string().contains("Failed to decode Arrow batch"));
+    }
+
+    #[test]
+    fn share_schema_widens_the_batches_of_a_result_to_the_relaxed_schema() {
+        let (relaxed, schema) = 
decode_arrow_record_batches(&null_column_stream()).unwrap();
+        assert!(schema.is_some());
+        let relaxed = relaxed.into_iter().next().unwrap();
+        let strict = RecordBatch::try_new(
+            Arc::new(Schema::new(vec![
+                declared("arr[NULL]", DataType::Int64),
+                Field::new("id", DataType::Int64, true),
+            ])),
+            vec![
+                Arc::new(Int64Array::from(vec![1, 2, 3])) as ArrayRef,
+                Arc::new(Int64Array::from(vec![0, 1, 2])) as ArrayRef,
+            ],
+        )
+        .unwrap();
+        let relaxed_schema = relaxed.schema();
+        let widened =
+            share_schema(vec![strict, relaxed.clone()], 
Some(relaxed_schema.clone())).unwrap();
+        assert!(widened.iter().all(|batch| batch.schema() == relaxed_schema));
+        assert_eq!(widened[0].column(0).null_count(), 0);
+        assert_eq!(widened[1].column(0).null_count(), 3);
+        // Without a relaxed schema, the batches are as they are.
+        let plain = share_schema(vec![widened[0].clone()], None).unwrap();
+        assert_eq!(plain[0].schema(), relaxed_schema);
+    }
+}
+
 #[cfg(test)]
 mod arrow_value_tests {
     use super::*;
diff --git a/crates/spark-connect/tests/e2e_columns.rs 
b/crates/spark-connect/tests/e2e_columns.rs
index b4d2bfb6..ad938d5e 100644
--- a/crates/spark-connect/tests/e2e_columns.rs
+++ b/crates/spark-connect/tests/e2e_columns.rs
@@ -9,9 +9,11 @@ mod common;
 use common::{session, should_run};
 use spark_connect::column::{col, lit, lit_boolean, lit_double, lit_string, 
when, Column};
 use spark_connect::dataframe::DataFrame;
+use spark_connect::expression::{Expression, LiteralExpression};
 use spark_connect::functions as f;
 use spark_connect::row::Value;
 use spark_connect::session::SparkSession;
+use spark_connect::types::DataType;
 
 fn base(s: &SparkSession) -> DataFrame {
     s.sql(
@@ -251,3 +253,48 @@ fn column_struct_array_map_access() {
     let m = f::create_map(vec![lit_string("k"), lit(7)]);
     assert_eq!(i64s(&one(&df, m.get_item(lit_string("k")))), vec![Some(7)]);
 }
+
+/// `arr.getItem(None)` is declared non-nullable by the server, and its values 
are all null. The
+/// rows, the local iterator and the Arrow batches read those nulls as PyArrow 
does, unchanged.
+#[test]
+fn null_get_item_reads_its_nulls_though_the_server_declares_it_non_nullable() {
+    if !should_run() {
+        return;
+    }
+    let s = session();
+    let df = s.range(3).expect("range");
+    let arr = f::array(vec![lit(10), lit(20)]);
+    let null_key = 
Column::new(Expression::literal(LiteralExpression::null(DataType::Null)));
+    let null_item = arr.get_item(null_key);
+
+    let rows = df
+        .select(vec![null_item.clone()])
+        .collect()
+        .expect("collect");
+    assert_eq!(rows.len(), 3);
+    assert!(rows.iter().all(|row| row.get(0) == Some(&Value::Null)));
+
+    let iter = df
+        .select(vec![null_item.clone()])
+        .to_local_iterator(false)
+        .expect("to_local_iterator");
+    let streamed: Vec<_> = iter.collect::<Result<Vec<_>, 
_>>().expect("iterate");
+    assert_eq!(streamed.len(), 3);
+    assert!(streamed.iter().all(|row| row.get(0) == Some(&Value::Null)));
+
+    let batches = df
+        .select(vec![null_item.clone()])
+        .collect_record_batches()
+        .expect("collect_record_batches");
+    let nulls: usize = batches.iter().map(|b| b.column(0).null_count()).sum();
+    assert_eq!(nulls, 3);
+
+    // A struct field of the same value is read with its nulls too.
+    let st = f::r#struct(vec![null_item.alias("x")]);
+    let rows = df.select(vec![st]).collect().expect("collect struct");
+    assert_eq!(rows.len(), 3);
+    let Some(Value::Struct(first)) = rows[0].get(0) else {
+        panic!("a struct value, got {:?}", rows[0].get(0));
+    };
+    assert_eq!(first.values(), &[Value::Null]);
+}
diff --git a/python/tests/test_dropin_e2e.py b/python/tests/test_dropin_e2e.py
index bde5a00b..4b83aed2 100644
--- a/python/tests/test_dropin_e2e.py
+++ b/python/tests/test_dropin_e2e.py
@@ -5701,3 +5701,18 @@ def 
test_binary_subclasses_are_sent_as_the_bytes_of_pyspark(session):
     assert [row.b for row in rows] == [b"other", b"other", b"ba", None]
     rows = session.createDataFrame([([Bytes(b"orig")],)], "a 
array<binary>").collect()
     assert rows[0].a == [b"other"]
+
+
+def 
test_null_get_item_reads_its_nulls_though_the_server_declares_it_non_nullable(session):
+    # As official PySpark 4.3.0: `arr.getItem(None)` is declared non-nullable 
by the server while
+    # its values are null; the rows, the iterator, the struct field and the 
Arrow table keep them.
+    arr = F.array(F.lit(10), F.lit(20))
+    df = session.range(3).select(arr.getItem(None).alias("v"))
+    assert df.schema["v"].nullable is False
+    assert [row.v for row in df.collect()] == [None, None, None]
+    assert [row.v for row in df.toLocalIterator()] == [None, None, None]
+    rows = 
session.range(2).select(F.struct(arr.getItem(None).alias("x")).alias("s")).collect()
+    assert [row.s.x for row in rows] == [None, None]
+    # The table is cast to the non-nullable schema, which raises for a null, 
as PySpark's does.
+    with pytest.raises(ValueError, match="Casting field 'v' with null values 
to non-nullable"):
+        df.toArrow()
diff --git a/python/tests/test_dropin_offline.py 
b/python/tests/test_dropin_offline.py
index dbe95992..69b2e865 100644
--- a/python/tests/test_dropin_offline.py
+++ b/python/tests/test_dropin_offline.py
@@ -24640,3 +24640,21 @@ def 
test_random_forest_native_binding_sends_reference_parameters(mock_connect, u
         assert fit.estimator.name == 
"org.apache.spark.ml.classification.RandomForestClassifier"
         expected = 
proto.MlParams.FromString(base64.b64decode(case["params_b64"]))
         assert fit.params == expected
+
+
+def 
test_a_non_nullable_column_with_nulls_is_read_with_its_nulls_as_pyarrow_reads_it(mock_connect):
+    # The server declares `arr[NULL]` non-nullable for `arr.getItem(None)`, 
whose values are all
+    # null. PySpark reads the batch as PyArrow does, so the rows keep the 
nulls.
+    pa = pytest.importorskip("pyarrow")
+    spark, service = mock_connect
+    schema = pa.schema([pa.field("id", pa.int64(), nullable=False)])
+    batch = pa.RecordBatch.from_arrays([pa.array([None, None, None], 
pa.int64())], schema=schema)
+    sink = pa.BufferOutputStream()
+    with pa.ipc.new_stream(sink, schema) as writer:
+        writer.write_batch(batch)
+    # ExecutePlanResponse.arrow_batch=2: row_count=3, data=2.
+    service.execute_payload = message_field(
+        2, varint_field(1, 3) + message_field(2, sink.getvalue().to_pybytes())
+    )
+    assert spark.range(3).collect() == [Row(id=None), Row(id=None), 
Row(id=None)]
+    assert list(spark.range(3).toLocalIterator()) == [Row(id=None), 
Row(id=None), Row(id=None)]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to