adriangb opened a new issue, #27693:
URL: https://github.com/apache/datafusion/issues/27693

   ### Describe the bug
   
   Casting a `List<Struct>` that contains a null item to a `List` with a 
non-nullable item field panics. It should return an error.
   
   ### To Reproduce
   
   Tested on `main` at 
[`538ef6a`](https://github.com/apache/datafusion/commit/538ef6adb9abf602e702022372384adeffea6ecb).
 Save this as `datafusion/common/tests/mre.rs` and run `cargo test -p 
datafusion-common --test mre`:
   
   ```rust
   use std::sync::Arc;
   use arrow::array::{ArrayRef, Int64Array, ListArray, StructArray};
   use arrow::buffer::{NullBuffer, OffsetBuffer};
   use arrow::datatypes::{DataType, Field, Fields};
   use datafusion_common::ScalarValue;
   
   #[test]
   fn mre() {
       let fields = Fields::from(vec![Field::new("a", DataType::Int64, true)]);
       // [{a: 1}, NULL]
       let items = StructArray::new(
           fields.clone(),
           vec![Arc::new(Int64Array::from(vec![1, 0])) as ArrayRef],
           Some(NullBuffer::from(vec![true, false])),
       );
       let list = ListArray::new(
           Arc::new(Field::new("item", DataType::Struct(fields.clone()), true)),
           OffsetBuffer::from_lengths([2]),
           Arc::new(items),
           None,
       );
       let target = DataType::List(Arc::new(Field::new("item", 
DataType::Struct(fields), false)));
   
       let _ = ScalarValue::List(Arc::new(list)).cast_to(&target);
   }
   ```
   
   Output:
   
   ```
   thread 'mre' panicked at arrow-array-60.2.0/src/array/list_array.rs:259:54:
   called `Result::unwrap()` on an `Err` value: 
InvalidArgumentError("Non-nullable field of ListArray \"item\" cannot contain 
nulls")
   ```
   
   `ColumnarValue::cast_to` and `datafusion_common::nested_struct::cast_column` 
panic in the same way. `LargeList` behaves the same.
   
   ### Expected behavior
   
   The cast returns an `Err`, as `arrow::compute::cast` does for the same input:
   
   ```
   Err(InvalidArgumentError("Non-nullable field of ListArray \"item\" cannot 
contain nulls"))
   ```
   
   ### Additional context
   
   I could not reach the panic from SQL or `datafusion-cli`. Every path I tried 
rejects the cast before execution:
   
   | Path | Result |
   |---|---|
   | `SELECT arrow_cast([named_struct('a', 1), NULL], 'List(non-null 
Struct("a": Int64))')` | `Unsupported CAST` |
   | `arrow_cast` on a column | `Unsupported CAST` |
   | `INSERT` into a table with non-null list items | `Unsupported CAST` |
   | Parquet table whose files disagree on item nullability | `Cannot cast 
nullable struct field 'element' to non-nullable field` |
   
   So the panic affects Rust API users who call these cast functions directly.
   
   The panic is only reachable when the list contains a Struct. These casts go 
through 
[`cast_list_column`](https://github.com/apache/datafusion/blob/538ef6adb9abf602e702022372384adeffea6ecb/datafusion/common/src/nested_struct.rs#L334),
 which builds the result with `GenericListArray::new` (the panicking 
constructor). The ListView and FixedSizeList paths already use `try_new`.
   
   I found this while fuzzing the nested cast code during review of 
[#27383](https://github.com/apache/datafusion/pull/27383). That PR does not 
change this code.
   


-- 
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