Jefffrey commented on code in PR #12533:
URL: https://github.com/apache/arrow-rs/pull/12533#discussion_r4779872806


##########
arrow-cast/src/cast/list.rs:
##########
@@ -55,17 +56,28 @@ 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);
+    };
+
+    // 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)?;

Review Comment:
   we probably should gate this filter to only execute for strict casts



##########
arrow-cast/src/cast/list.rs:
##########
@@ -55,17 +56,28 @@ 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);
+    };
+
+    // 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();
+    take(values.as_ref(), &indices, None)

Review Comment:
   i wonder if there is a better way to do this null masking; especially for 
the strict = false case, we could probably just operate directly on the null 
buffers. there is a caveat that some array types wont have a null buffer (run 
array & union array) so probably can keep this type of fallback for them



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

Reply via email to