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


##########
datafusion/optimizer/src/analyzer/type_coercion.rs:
##########
@@ -2012,6 +2054,38 @@ mod test {
         )
     }
 
+    #[test]
+    fn test_coerce_union_incompatible_extension_type() -> Result<()> {
+        let uuid_field = Field::new("id", DataType::FixedSizeBinary(16), false)
+            .with_metadata(std::collections::HashMap::from([(
+                "ARROW:extension:name".to_string(),
+                "arrow.uuid".to_string(),
+            )]));
+        let left_plan = Arc::new(LogicalPlan::EmptyRelation(EmptyRelation {
+            produce_one_row: false,
+            schema: Arc::new(DFSchema::from_unqualified_fields(
+                vec![uuid_field].into(),
+                std::collections::HashMap::new(),
+            )?),
+        }));
+        let right_plan = Arc::new(LogicalPlan::EmptyRelation(EmptyRelation {
+            produce_one_row: false,
+            schema: Arc::new(DFSchema::from_unqualified_fields(
+                vec![Field::new("id", DataType::Binary, false)].into(),

Review Comment:
   Maybe we can use `FixedSizeBinary(16)` here to make sure that the extension 
type is the cause of the error. 



##########
datafusion/sqllogictest/test_files/sql_extension_types.slt:
##########
@@ -192,3 +192,83 @@ INSERT INTO t1 VALUES (NULL);
 
 statement ok
 DROP TABLE t1;
+
+statement ok
+CREATE TABLE t (id UUID);
+
+statement ok
+INSERT INTO t VALUES
+  (CAST(arrow_cast(X'00010203040506070809000102030506', 'FixedSizeBinary(16)') 
AS UUID));
+
+# Verify t.id actually has arrow.uuid metadata
+query T
+SELECT arrow_metadata(id, 'ARROW:extension:name') FROM t;
+----
+arrow.uuid
+
+# UNION and UNION ALL between UUID and a bare Binary literal return a planning 
error
+statement error UNION types cannot be unified for column 'id'
+SELECT id FROM t
+UNION
+SELECT X'00010203040506070809000102030507';
+
+statement error UNION types cannot be unified for column 'id'
+SELECT id FROM t
+UNION ALL
+SELECT X'00010203040506070809000102030507';
+
+# Same extension name but different storage type (FixedSizeBinary(16) vs 
Binary) also fails
+statement error UNION types cannot be unified for column 'id'
+SELECT id FROM t
+UNION
+SELECT with_metadata(
+  X'00010203040506070809000102030507',
+  'ARROW:extension:name',
+  'arrow.uuid'
+);
+
+# Same extension name and storage type but different extension payload also 
fails
+statement error UNION types cannot be unified for column 'id'
+SELECT with_metadata(
+  with_metadata(
+    arrow_cast(X'00010203040506070809000102030506', 'FixedSizeBinary(16)'),
+    'ARROW:extension:name',
+    'arrow.uuid'
+  ),
+  'ARROW:extension:metadata',

Review Comment:
   I guess it could be that this might be rejected in the future because 
`arrow.uuid` has no specified extension type metadata. I think if 
`with_metadata` will check whether the actual extension name and metadata are 
compatible, this could become an error. Maybe the spec also leaves this open 
for implementations, at least now we're not checking the metadata in 
`Uuid::try_new`.
   
   Maybe we can use a different type here? Detecting different layouts of an 
`arrow.fixed_shape_tensor` could be a nice use case. For example, the tensor ` 
{ "shape": [2, 5] }` should not be merged with the tensor ` { "shape": [5, 2] 
}`.



##########
datafusion/optimizer/src/analyzer/type_coercion.rs:
##########
@@ -1721,11 +1721,53 @@ fn coerce_union_schema_with_schema(
         .iter()
         .enumerate()
         .map(|(i, field)| {
+            let input_fields: Vec<&Field> = inputs
+                .iter()
+                .map(|plan| plan.schema().field(i).as_ref())
+                .collect();
+
+            // Validate extension type compatibility across non-Null inputs.
+            // Merging extension metadata onto a widened storage type (e.g.
+            // FixedSizeBinary(16)<arrow.uuid> + Binary -> Binary) produces an
+            // invalid logical schema that causes internal errors on UNION and
+            // silently drops the extension type on UNION ALL.
+            if let Some(ext_field) = input_fields
+                .iter()
+                .find(|f| f.extension_type_name().is_some())
+            {
+                let ext_name = ext_field.extension_type_name();
+                let ext_meta = ext_field.extension_type_metadata();
+                for other in &input_fields {
+                    if matches!(other.data_type(), DataType::Null) {
+                        continue;
+                    }
+                    if other.extension_type_name() != ext_name

Review Comment:
   Do we have somewhere a helper function to check whether two extension types 
are "equivalent". Starting now with extracting this check into a single place 
might make it easier in the future to make this behavior more lenient (e.g., 
https://github.com/apache/datafusion/issues/26810).
   
   Even if we stay strict with the comparison, it would be good to have that in 
a single place. Could also be a follow-up if it does not yet exist where we 
search for existing usages in the code.



##########
datafusion/optimizer/src/analyzer/type_coercion.rs:
##########
@@ -2012,6 +2054,38 @@ mod test {
         )
     }
 
+    #[test]
+    fn test_coerce_union_incompatible_extension_type() -> Result<()> {
+        let uuid_field = Field::new("id", DataType::FixedSizeBinary(16), false)
+            .with_metadata(std::collections::HashMap::from([(
+                "ARROW:extension:name".to_string(),
+                "arrow.uuid".to_string(),
+            )]));
+        let left_plan = Arc::new(LogicalPlan::EmptyRelation(EmptyRelation {
+            produce_one_row: false,
+            schema: Arc::new(DFSchema::from_unqualified_fields(
+                vec![uuid_field].into(),
+                std::collections::HashMap::new(),
+            )?),
+        }));
+        let right_plan = Arc::new(LogicalPlan::EmptyRelation(EmptyRelation {
+            produce_one_row: false,
+            schema: Arc::new(DFSchema::from_unqualified_fields(
+                vec![Field::new("id", DataType::Binary, false)].into(),

Review Comment:
   But I think the slt tests cover everything fine, so this is just a remark.



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