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


##########
datafusion/sqllogictest/test_files/sql_extension_types.slt:
##########
@@ -192,3 +192,148 @@ 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
+
+# UUID with a bare Binary literal is Binary, as in 56.0.0, until #27383 can
+# coerce the literal to UUID; UNION no longer fails with an internal error
+query ?TT
+SELECT id, arrow_typeof(id), arrow_metadata(id, 'ARROW:extension:name') FROM (
+  SELECT id FROM t
+  UNION
+  SELECT X'00010203040506070809000102030507'
+) ORDER BY id;
+----
+00010203040506070809000102030506 Binary NULL
+00010203040506070809000102030507 Binary NULL
+
+query ?TT
+SELECT id, arrow_typeof(id), arrow_metadata(id, 'ARROW:extension:name') FROM (
+  SELECT id FROM t
+  UNION ALL
+  SELECT X'00010203040506070809000102030507'
+) ORDER BY id;
+----
+00010203040506070809000102030506 Binary NULL
+00010203040506070809000102030507 Binary NULL
+
+# A plain FixedSizeBinary(16) keeps the UUID type, as in 56.0.0
+query ?T
+SELECT id, arrow_metadata(id, 'ARROW:extension:name') FROM (
+  SELECT id FROM t
+  UNION ALL
+  SELECT arrow_cast(X'00010203040506070809000102030507', 'FixedSizeBinary(16)')
+) ORDER BY id;
+----
+00010203040506070809000102030506 arrow.uuid
+00010203040506070809000102030507 arrow.uuid
+
+# The coerced storage type (Utf8View) already carries arrow.json in one input
+query TT
+SELECT j, arrow_metadata(j, 'ARROW:extension:name') FROM (
+  SELECT with_metadata('{"a":1}', 'ARROW:extension:name', 'arrow.json') AS j
+  UNION ALL
+  SELECT with_metadata(arrow_cast('{"b":2}', 'Utf8View'), 
'ARROW:extension:name', 'arrow.json')
+) ORDER BY j;
+----
+{"a":1} arrow.json
+{"b":2} arrow.json
+
+# Extension metadata that differs only in its bytes is not an error (#26810)
+query T
+SELECT arrow_metadata(j, 'ARROW:extension:name') FROM (
+  SELECT with_metadata(with_metadata('{"a":1}', 'ARROW:extension:name', 
'arrow.json'), 'ARROW:extension:metadata', '{}') AS j
+  UNION ALL
+  SELECT with_metadata(with_metadata('{"b":2}', 'ARROW:extension:name', 
'arrow.json'), 'ARROW:extension:metadata', '{ }')
+);
+----
+arrow.json
+arrow.json
+
+# As in the physical union, the last input's extension metadata wins
+query T
+SELECT arrow_metadata(j, 'ARROW:extension:metadata') FROM (
+  SELECT with_metadata(with_metadata('{"a":1}', 'ARROW:extension:name', 
'arrow.json'), 'ARROW:extension:metadata', '{}') AS j
+  UNION
+  SELECT with_metadata(with_metadata('{"b":2}', 'ARROW:extension:name', 
'arrow.json'), 'ARROW:extension:metadata', '{ }')
+);
+----
+{ }
+{ }
+
+# Utf8View never carried arrow.json in any input, so the result drops it
+query TT
+SELECT j, arrow_metadata(j, 'ARROW:extension:name') FROM (
+  SELECT with_metadata('{"a":1}', 'ARROW:extension:name', 'arrow.json') AS j
+  UNION ALL
+  SELECT arrow_cast('{"b":2}', 'Utf8View')
+) ORDER BY j;
+----
+{"a":1} NULL
+{"b":2} NULL
+
+# Extension metadata of an input cast to Utf8View is not kept
+query TT
+SELECT j, arrow_metadata(j, 'ARROW:extension:metadata') FROM (
+  SELECT with_metadata(with_metadata('{"a":1}', 'ARROW:extension:name', 
'arrow.json'), 'ARROW:extension:metadata', '{}') AS j
+  UNION
+  SELECT with_metadata(arrow_cast('{"b":2}', 'Utf8View'), 
'ARROW:extension:name', 'arrow.json')
+) ORDER BY j;
+----
+{"a":1} NULL
+{"b":2} NULL
+
+# NULL inputs keep their extension type when every input is NULL
+query T
+SELECT arrow_metadata(j, 'ARROW:extension:name') FROM (
+  SELECT with_metadata(NULL, 'ARROW:extension:name', 'arrow.opaque') AS j
+  UNION
+  SELECT with_metadata(NULL, 'ARROW:extension:name', 'arrow.opaque')
+);
+----
+arrow.opaque
+
+# A NULL input's metadata does not pick the extension type
+query T
+SELECT arrow_metadata(j, 'ARROW:extension:name') FROM (
+  SELECT with_metadata(NULL, 'ARROW:extension:name', 'arrow.opaque') AS j
+  UNION ALL
+  SELECT with_metadata('x', 'ARROW:extension:name', 'arrow.json')
+) WHERE j IS NOT NULL;
+----
+arrow.json
+
+# NULL branch does not force incompatibility and preserves extension metadata
+query T
+SELECT arrow_metadata(id, 'ARROW:extension:name') FROM (
+  SELECT id FROM t
+  UNION ALL
+  SELECT NULL
+) LIMIT 1;
+----
+arrow.uuid
+
+# UNION between two UUID columns works and preserves arrow.uuid
+query T
+SELECT arrow_metadata(id, 'ARROW:extension:name') FROM (
+  SELECT id FROM t
+  UNION
+  SELECT CAST(arrow_cast(X'00010203040506070809000102030507', 
'FixedSizeBinary(16)') AS UUID)
+);

Review Comment:
   🎉 



##########
datafusion/optimizer/src/analyzer/type_coercion.rs:
##########
@@ -2012,6 +2032,47 @@ mod test {
         )
     }
 
+    #[test]
+    fn coerce_union_schema_extension_type() -> Result<()> {
+        use super::coerce_union_schema;
+        use arrow_schema::extension::EXTENSION_TYPE_NAME_KEY;
+        let input = |data_type: DataType, extension: Option<&str>| {
+            let mut field = Field::new("c", data_type, true);
+            if let Some(extension) = extension {
+                field = field.with_metadata(std::collections::HashMap::from([(
+                    EXTENSION_TYPE_NAME_KEY.to_string(),
+                    extension.to_string(),
+                )]));
+            }
+            DFSchema::from_unqualified_fields(
+                vec![field].into(),
+                std::collections::HashMap::new(),
+            )
+            .map(|schema| {
+                Arc::new(LogicalPlan::EmptyRelation(EmptyRelation {
+                    produce_one_row: false,
+                    schema: Arc::new(schema),
+                }))
+            })
+        };
+
+        // No input stores arrow.uuid as Binary, so the result drops it
+        let schema = coerce_union_schema(&[
+            input(DataType::FixedSizeBinary(16), Some("arrow.uuid"))?,
+            input(DataType::Binary, None)?,
+        ])?;
+        assert_eq!(schema.field(0).data_type(), &DataType::Binary);
+        assert_eq!(schema.field(0).extension_type_name(), None);

Review Comment:
   Can we also add a test here that UUID + UUID -> UUID?



##########
datafusion/optimizer/src/analyzer/type_coercion.rs:
##########
@@ -2012,6 +2032,47 @@ mod test {
         )
     }
 
+    #[test]
+    fn coerce_union_schema_extension_type() -> Result<()> {
+        use super::coerce_union_schema;
+        use arrow_schema::extension::EXTENSION_TYPE_NAME_KEY;
+        let input = |data_type: DataType, extension: Option<&str>| {
+            let mut field = Field::new("c", data_type, true);
+            if let Some(extension) = extension {
+                field = field.with_metadata(std::collections::HashMap::from([(
+                    EXTENSION_TYPE_NAME_KEY.to_string(),
+                    extension.to_string(),
+                )]));
+            }
+            DFSchema::from_unqualified_fields(
+                vec![field].into(),
+                std::collections::HashMap::new(),
+            )
+            .map(|schema| {
+                Arc::new(LogicalPlan::EmptyRelation(EmptyRelation {
+                    produce_one_row: false,
+                    schema: Arc::new(schema),
+                }))
+            })
+        };
+
+        // No input stores arrow.uuid as Binary, so the result drops it
+        let schema = coerce_union_schema(&[
+            input(DataType::FixedSizeBinary(16), Some("arrow.uuid"))?,
+            input(DataType::Binary, None)?,
+        ])?;
+        assert_eq!(schema.field(0).data_type(), &DataType::Binary);
+        assert_eq!(schema.field(0).extension_type_name(), None);
+
+        // A NULL input's extension type does not decide the result
+        let schema = coerce_union_schema(&[
+            input(DataType::Null, Some("arrow.opaque"))?,
+            input(Utf8, Some("arrow.json"))?,
+        ])?;
+        assert_eq!(schema.field(0).extension_type_name(), Some("arrow.json"));

Review Comment:
   Can we remove this portion of the test?
   
   I am not sure this is behaviour we actually want, although it's definitely 
fine to live with if the rest of the approach works (a null valued extension 
type has probably never happened in the real world).
   
   I mostly just don't want a future PR that improves handling of extension 
types to run into this and add complexity trying to keep this test passing.



##########
datafusion/sqllogictest/test_files/sql_extension_types.slt:
##########
@@ -192,3 +192,148 @@ 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
+
+# UUID with a bare Binary literal is Binary, as in 56.0.0, until #27383 can
+# coerce the literal to UUID; UNION no longer fails with an internal error
+query ?TT
+SELECT id, arrow_typeof(id), arrow_metadata(id, 'ARROW:extension:name') FROM (
+  SELECT id FROM t
+  UNION
+  SELECT X'00010203040506070809000102030507'
+) ORDER BY id;
+----
+00010203040506070809000102030506 Binary NULL
+00010203040506070809000102030507 Binary NULL
+
+query ?TT
+SELECT id, arrow_typeof(id), arrow_metadata(id, 'ARROW:extension:name') FROM (
+  SELECT id FROM t
+  UNION ALL
+  SELECT X'00010203040506070809000102030507'
+) ORDER BY id;
+----
+00010203040506070809000102030506 Binary NULL
+00010203040506070809000102030507 Binary NULL
+
+# A plain FixedSizeBinary(16) keeps the UUID type, as in 56.0.0
+query ?T
+SELECT id, arrow_metadata(id, 'ARROW:extension:name') FROM (
+  SELECT id FROM t
+  UNION ALL
+  SELECT arrow_cast(X'00010203040506070809000102030507', 'FixedSizeBinary(16)')
+) ORDER BY id;
+----
+00010203040506070809000102030506 arrow.uuid
+00010203040506070809000102030507 arrow.uuid
+
+# The coerced storage type (Utf8View) already carries arrow.json in one input
+query TT
+SELECT j, arrow_metadata(j, 'ARROW:extension:name') FROM (
+  SELECT with_metadata('{"a":1}', 'ARROW:extension:name', 'arrow.json') AS j
+  UNION ALL
+  SELECT with_metadata(arrow_cast('{"b":2}', 'Utf8View'), 
'ARROW:extension:name', 'arrow.json')
+) ORDER BY j;
+----
+{"a":1} arrow.json
+{"b":2} arrow.json
+
+# Extension metadata that differs only in its bytes is not an error (#26810)
+query T
+SELECT arrow_metadata(j, 'ARROW:extension:name') FROM (
+  SELECT with_metadata(with_metadata('{"a":1}', 'ARROW:extension:name', 
'arrow.json'), 'ARROW:extension:metadata', '{}') AS j
+  UNION ALL
+  SELECT with_metadata(with_metadata('{"b":2}', 'ARROW:extension:name', 
'arrow.json'), 'ARROW:extension:metadata', '{ }')
+);
+----
+arrow.json
+arrow.json
+
+# As in the physical union, the last input's extension metadata wins
+query T
+SELECT arrow_metadata(j, 'ARROW:extension:metadata') FROM (
+  SELECT with_metadata(with_metadata('{"a":1}', 'ARROW:extension:name', 
'arrow.json'), 'ARROW:extension:metadata', '{}') AS j
+  UNION
+  SELECT with_metadata(with_metadata('{"b":2}', 'ARROW:extension:name', 
'arrow.json'), 'ARROW:extension:metadata', '{ }')
+);
+----
+{ }
+{ }
+
+# Utf8View never carried arrow.json in any input, so the result drops it
+query TT
+SELECT j, arrow_metadata(j, 'ARROW:extension:name') FROM (
+  SELECT with_metadata('{"a":1}', 'ARROW:extension:name', 'arrow.json') AS j
+  UNION ALL
+  SELECT arrow_cast('{"b":2}', 'Utf8View')
+) ORDER BY j;
+----
+{"a":1} NULL
+{"b":2} NULL
+
+# Extension metadata of an input cast to Utf8View is not kept
+query TT
+SELECT j, arrow_metadata(j, 'ARROW:extension:metadata') FROM (
+  SELECT with_metadata(with_metadata('{"a":1}', 'ARROW:extension:name', 
'arrow.json'), 'ARROW:extension:metadata', '{}') AS j
+  UNION
+  SELECT with_metadata(arrow_cast('{"b":2}', 'Utf8View'), 
'ARROW:extension:name', 'arrow.json')
+) ORDER BY j;
+----
+{"a":1} NULL
+{"b":2} NULL
+
+# NULL inputs keep their extension type when every input is NULL
+query T
+SELECT arrow_metadata(j, 'ARROW:extension:name') FROM (
+  SELECT with_metadata(NULL, 'ARROW:extension:name', 'arrow.opaque') AS j
+  UNION
+  SELECT with_metadata(NULL, 'ARROW:extension:name', 'arrow.opaque')
+);
+----
+arrow.opaque
+
+# A NULL input's metadata does not pick the extension type
+query T
+SELECT arrow_metadata(j, 'ARROW:extension:name') FROM (
+  SELECT with_metadata(NULL, 'ARROW:extension:name', 'arrow.opaque') AS j
+  UNION ALL
+  SELECT with_metadata('x', 'ARROW:extension:name', 'arrow.json')
+) WHERE j IS NOT NULL;
+----
+arrow.json

Review Comment:
   Again, perhaps just remove this since it's a fine side-effect but not 
behaviour I personally would like to lock in



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