Ap-95671 commented on code in PR #12533:
URL: https://github.com/apache/arrow-rs/pull/12533#discussion_r4787190728
##########
arrow-cast/src/cast/list.rs:
##########
@@ -55,17 +56,53 @@ pub(crate) fn cast_values_to_list_view<O: OffsetSizeTrait>(
Ok(Arc::new(list))
}
-/// Cast fixed size list array to inner values type, essentially flattening the
-/// lists.
-///
-/// For example: `FixedSizeList<Int32, 2>([[1, 2], [3, 4]]) -> Int32([1, 2, 3,
4])`
+/// Cast a size-one fixed-size list to its child type, preserving null rows.
pub(crate) fn cast_single_element_fixed_size_list_to_values(
array: &dyn Array,
to: &DataType,
cast_options: &CastOptions,
) -> Result<ArrayRef, ArrowError> {
- let values = array.as_fixed_size_list().values();
- cast_with_options(values, to, cast_options)
+ let array = array.as_fixed_size_list();
+ let values = array.values();
+ let Some(nulls) = array.nulls().filter(|nulls| nulls.null_count() != 0)
else {
+ return cast_with_options(values, to, cast_options);
+ };
+
+ if !cast_options.safe && values.data_type() != to {
+ // Skip masked child values so a strict cast cannot fail on a null row.
+ let valid = BooleanArray::new(nulls.inner().clone(), None);
+ let values = filter(values.as_ref(), &valid)?;
+ let values = cast_with_options(values.as_ref(), to, cast_options)?;
+ let mut valid_indices = 0_u64..;
+ let indices: UInt64Array = nulls
+ .iter()
+ .map(|valid| if valid { valid_indices.next() } else { None })
+ .collect();
+ return take(values.as_ref(), &indices, None);
+ }
+
+ let values = cast_with_options(values, to, cast_options)?;
Review Comment:
A safe cast can return an error here before `apply_null_mask` runs. I tested
a size-one `FixedSizeList<Struct<number: Utf8 not null>>` with child strings
`["2", "invalid"]` and parent validity `[true, false]`. Casting to
`Struct<number: Int32 not null>` should return `[{number: 2}, null]`, but
`8a8d135` reports an unmasked null in `number`. The first PR revision passes
the same test because it filters the null list row first. Could the direct path
fall back to the existing filter/cast/take path on error? That variant passed
the `arrow-cast` suite with the regression case.
--
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]