tschwarzinger commented on code in PR #27383:
URL: https://github.com/apache/datafusion/pull/27383#discussion_r4584073229


##########
datafusion/common/src/nested_struct.rs:
##########
@@ -1239,6 +1546,267 @@ mod tests {
         assert_eq!(result.value(2), 3);
     }
 
+    #[test]
+    fn test_cast_extension_bare_data_types() {
+        let source_field = field("value", DataType::Int8);
+        let target_field = field("value", DataType::Utf8);
+        assert!(source_field.metadata().is_empty());
+        assert!(target_field.metadata().is_empty());
+
+        let result = cast_column_fields(
+            &int8_values(),
+            &source_field,
+            &target_field,
+            Some(&BareInt8ToStringCast),
+            &DEFAULT_CAST_OPTIONS,
+        )
+        .unwrap();
+
+        assert_boolean_strings(&result);
+    }
+
+    fn int8_values() -> ArrayRef {
+        Arc::new(Int8Array::from(vec![Some(0), Some(1), Some(-20), None]))
+    }
+
+    fn cast_bool8_container(
+        source: ArrayRef,
+        source_field: Field,
+        target_field: Field,
+    ) -> ArrayRef {
+        validate_data_type_compatibility_with_extension(
+            source_field.name(),
+            source_field.data_type(),
+            target_field.data_type(),
+            Some(&Bool8ToStringCast),
+        )
+        .unwrap();
+        cast_column_fields(
+            &source,
+            &source_field,
+            &target_field,
+            Some(&Bool8ToStringCast),
+            &DEFAULT_CAST_OPTIONS,
+        )
+        .unwrap()
+    }
+
+    fn assert_boolean_strings(values: &ArrayRef) {
+        let values = values.as_string::<i32>();
+        assert_eq!(
+            values.iter().collect::<Vec<_>>(),
+            vec![Some("false"), Some("true"), Some("true"), None]
+        );
+    }
+
+    #[test]
+    fn test_cast_extension_nested_struct() {
+        let source_fields: Fields = 
vec![Arc::new(bool8_field("value"))].into();
+        let source = Arc::new(StructArray::new(
+            source_fields.clone(),
+            vec![int8_values()],
+            None,
+        ));
+        let target_fields: Fields = vec![arc_field("value", 
DataType::Utf8)].into();
+        let result = cast_bool8_container(
+            source,
+            field("root", Struct(source_fields)),
+            field("root", Struct(target_fields)),
+        );
+
+        assert_boolean_strings(result.as_struct().column(0));
+    }
+
+    #[test]
+    fn test_cast_extension_nested_list() {
+        let source_item = Arc::new(bool8_field("item"));
+        let source = Arc::new(ListArray::new(
+            Arc::clone(&source_item),
+            OffsetBuffer::new(ScalarBuffer::from(vec![0, 2, 4])),
+            int8_values(),
+            None,
+        ));
+        let target_item = arc_field("item", DataType::Utf8);
+        let result = cast_bool8_container(
+            source,
+            field("root", DataType::List(source_item)),
+            field("root", DataType::List(target_item)),
+        );
+
+        assert_boolean_strings(result.as_list::<i32>().values());

Review Comment:
   🔥 



##########
datafusion/common/src/nested_struct.rs:
##########
@@ -1225,6 +1451,87 @@ mod tests {
         Arc::new(struct_field(name, fields))
     }
 
+    fn bool8_field(name: &str) -> Field {
+        Field::new(name, DataType::Int8, true).with_metadata(HashMap::from([(
+            "ARROW:extension:name".to_string(),
+            "arrow.bool8".to_string(),
+        )]))
+    }
+
+    #[derive(Debug)]
+    struct Bool8ToStringCast;
+
+    impl CastExtension for Bool8ToStringCast {
+        fn can_cast_fields(
+            &self,
+            source_field: &Field,
+            target_field: &Field,
+        ) -> Result<bool> {
+            Ok(source_field
+                .metadata()
+                .get("ARROW:extension:name")
+                .map(String::as_str)
+                == Some("arrow.bool8")
+                && source_field.data_type() == &DataType::Int8

Review Comment:
   Just wondering whether an error is more appropriate here if the extension 
type has an invalid storage data type (for the tests it's ok anyways)



##########
datafusion/common/src/nested_struct.rs:
##########
@@ -570,7 +694,9 @@ fn cast_map_column(
         target_entries,
         target_sorted,
         None,
+        cast_extension,
     )?;
+    let (source_key, source_value) = 
validate_map_entries_field(source_entries)?;

Review Comment:
   Is this only needed because we need the fields for `cast_column_fields` or 
should we fix this even without the casting extensions?



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