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


##########
datafusion/expr/src/logical_plan/builder.rs:
##########
@@ -275,23 +274,14 @@ impl LogicalPlanBuilder {
         let n_cols = values[0].len();
         let mut fields = ValuesFields::new();
         for j in 0..n_cols {
-            let field_type = schema.field(j).data_type();

Review Comment:
   Does this change have anyhting to do with this feature or is this just a 
general clean-up?



##########
datafusion/sqllogictest/src/test_context.rs:
##########
@@ -83,22 +81,6 @@ pub struct TestContext {
     test_dir: Option<TempDir>,
 }
 
-#[derive(Debug)]

Review Comment:
   🥳 



##########
datafusion/sqllogictest/test_files/sql_extension_types.slt:
##########
@@ -0,0 +1,279 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Tests for built-in mapping of SQL types to Arrow canonical extension types.
+# See: https://arrow.apache.org/docs/format/CanonicalExtensions.html
+
+#######
+# UUID Extension Type
+#######
+
+# CAST to UUID preserves extension metadata
+query ?T
+SELECT CAST(arrow_cast(X'00010203040506070809000102030506', 
'FixedSizeBinary(16)') AS UUID),
+       arrow_metadata(CAST(arrow_cast(X'00010203040506070809000102030506', 
'FixedSizeBinary(16)') AS UUID), 'ARROW:extension:name');
+----
+00010203040506070809000102030506 arrow.uuid
+
+# Test table creation and various insertion methods (Null, Naked Binary, and 
Cast)
+statement ok
+CREATE TABLE test_uuid (value UUID);
+
+query TTT
+DESCRIBE test_uuid;
+----
+value FixedSizeBinary(16) YES
+
+# 1. NULL insertion
+statement ok
+INSERT INTO test_uuid VALUES (NULL);
+
+# 2. Exercises Implicit Promotion to UUID from binary
+statement ok
+INSERT INTO test_uuid VALUES (X'00010203040506070809000102030506');
+
+# 3. Explicit CAST
+statement ok
+INSERT INTO test_uuid VALUES 
(CAST(arrow_cast(X'00010203040506070809000102030507', 'FixedSizeBinary(16)') AS 
UUID));
+
+query T
+SELECT arrow_metadata(value, 'ARROW:extension:name') FROM test_uuid;
+----
+arrow.uuid
+arrow.uuid
+arrow.uuid
+
+statement ok
+DROP TABLE test_uuid;
+
+#######
+# JSON Extension Type
+#######
+
+# CAST to JSON preserves extension metadata
+query TT
+SELECT CAST('{"a": 1}' AS JSON),
+       arrow_metadata(CAST('{"a": 1}' AS JSON), 'ARROW:extension:name');
+----
+{"a": 1} arrow.json
+
+statement ok
+CREATE TABLE test_json (data JSON);
+
+query TTT
+DESCRIBE test_json;
+----
+data Utf8 YES
+
+# 1. NULL insertion
+statement ok
+INSERT INTO test_json VALUES (NULL);
+
+# 2. Bare string literal (Implicit Promotion)
+statement ok
+INSERT INTO test_json VALUES ('{"bare": "literal"}');
+
+# 3. Explicit CAST
+statement ok
+INSERT INTO test_json VALUES (CAST('{"hello": "world"}' AS JSON));
+
+query T
+SELECT arrow_metadata(data, 'ARROW:extension:name') FROM test_json;
+----
+arrow.json
+arrow.json
+arrow.json
+
+# Verify Type Decay: Concatenation results in a bare Utf8 string
+query T
+SELECT arrow_metadata(data || '{"extra": 1}', 'ARROW:extension:name') FROM 
test_json;
+----
+NULL
+NULL
+NULL
+
+statement ok
+DROP TABLE test_json;
+
+#######
+# Error Handling
+#######
+
+# Error: Cannot cast an integer to UUID
+statement error Unsupported CAST from Int64 to FixedSizeBinary\(16\)
+SELECT CAST(12345 AS UUID);

Review Comment:
   If the `AS UUID` cast knows that it needs to convert to 
`FixedSizeBinary(16)`, can we omit the explicit casts to `FixedSizeBinary(16)` 
in the tests at the top of this file?
   
   E.g., `CAST(arrow_cast(X'00010203040506070809000102030506', 
'FixedSizeBinary(16)') AS UUID),`



##########
datafusion/expr/src/logical_plan/builder.rs:
##########
@@ -275,23 +274,14 @@ impl LogicalPlanBuilder {
         let n_cols = values[0].len();
         let mut fields = ValuesFields::new();
         for j in 0..n_cols {
-            let field_type = schema.field(j).data_type();

Review Comment:
   Or is it needed to get the tests in `datafusion/sql/tests/cases/params.rs` 
to work?



##########
datafusion/sqllogictest/test_files/sql_extension_types.slt:
##########
@@ -0,0 +1,279 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Tests for built-in mapping of SQL types to Arrow canonical extension types.
+# See: https://arrow.apache.org/docs/format/CanonicalExtensions.html
+
+#######
+# UUID Extension Type
+#######
+
+# CAST to UUID preserves extension metadata
+query ?T
+SELECT CAST(arrow_cast(X'00010203040506070809000102030506', 
'FixedSizeBinary(16)') AS UUID),
+       arrow_metadata(CAST(arrow_cast(X'00010203040506070809000102030506', 
'FixedSizeBinary(16)') AS UUID), 'ARROW:extension:name');
+----
+00010203040506070809000102030506 arrow.uuid
+
+# Test table creation and various insertion methods (Null, Naked Binary, and 
Cast)
+statement ok
+CREATE TABLE test_uuid (value UUID);
+
+query TTT
+DESCRIBE test_uuid;
+----
+value FixedSizeBinary(16) YES
+
+# 1. NULL insertion
+statement ok
+INSERT INTO test_uuid VALUES (NULL);
+
+# 2. Exercises Implicit Promotion to UUID from binary
+statement ok
+INSERT INTO test_uuid VALUES (X'00010203040506070809000102030506');
+
+# 3. Explicit CAST
+statement ok
+INSERT INTO test_uuid VALUES 
(CAST(arrow_cast(X'00010203040506070809000102030507', 'FixedSizeBinary(16)') AS 
UUID));
+
+query T
+SELECT arrow_metadata(value, 'ARROW:extension:name') FROM test_uuid;
+----
+arrow.uuid
+arrow.uuid
+arrow.uuid
+
+statement ok
+DROP TABLE test_uuid;
+
+#######
+# JSON Extension Type
+#######
+
+# CAST to JSON preserves extension metadata
+query TT
+SELECT CAST('{"a": 1}' AS JSON),
+       arrow_metadata(CAST('{"a": 1}' AS JSON), 'ARROW:extension:name');
+----
+{"a": 1} arrow.json
+
+statement ok
+CREATE TABLE test_json (data JSON);
+
+query TTT
+DESCRIBE test_json;
+----
+data Utf8 YES
+
+# 1. NULL insertion
+statement ok
+INSERT INTO test_json VALUES (NULL);
+
+# 2. Bare string literal (Implicit Promotion)
+statement ok
+INSERT INTO test_json VALUES ('{"bare": "literal"}');
+
+# 3. Explicit CAST
+statement ok
+INSERT INTO test_json VALUES (CAST('{"hello": "world"}' AS JSON));
+
+query T
+SELECT arrow_metadata(data, 'ARROW:extension:name') FROM test_json;
+----
+arrow.json
+arrow.json
+arrow.json
+
+# Verify Type Decay: Concatenation results in a bare Utf8 string
+query T
+SELECT arrow_metadata(data || '{"extra": 1}', 'ARROW:extension:name') FROM 
test_json;
+----
+NULL
+NULL
+NULL
+
+statement ok
+DROP TABLE test_json;
+
+#######
+# Error Handling
+#######
+
+# Error: Cannot cast an integer to UUID
+statement error Unsupported CAST from Int64 to FixedSizeBinary\(16\)
+SELECT CAST(12345 AS UUID);
+
+# Error: Cannot cast a short string to UUID
+statement error Unsupported CAST from Utf8 to FixedSizeBinary\(16\)
+SELECT CAST('too-short' AS UUID);
+
+# This should trigger cast plan_err: "Cannot cast Utf8 to extension type at 
column 0"
+# Because 'not-a-uuid' is Utf8, and it's being compared to a UUID target in a 
VALUES list.
+statement ok
+CREATE TABLE t1 (id UUID);
+
+# Then, try to insert the invalid type
+statement error Cannot automatically convert Utf8 to FixedSizeBinary\(16\)
+INSERT INTO t1 VALUES ('not-a-uuid');
+
+statement ok
+INSERT INTO t1 VALUES (NULL);
+
+statement ok
+DROP TABLE t1;
+
+# Error: Cannot mix different extension types
+statement ok
+CREATE TABLE mix_types (id UUID);
+
+# Even if the JSON string is 16 bytes, it lacks the arrow.uuid metadata
+statement error Cannot automatically convert Utf8 to FixedSizeBinary\(16\)
+INSERT INTO mix_types VALUES (CAST('{"key": "value"}' AS JSON));
+
+statement ok
+DROP TABLE mix_types;
+
+statement ok
+CREATE TABLE multi_col (info JSON, id UUID);

Review Comment:
   Does this test anything that is not covered by the other tests? If not, 
maybe it's better to keep the test suite just as big as necessary.



##########
datafusion/sqllogictest/test_files/sql_extension_types.slt:
##########
@@ -0,0 +1,279 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Tests for built-in mapping of SQL types to Arrow canonical extension types.
+# See: https://arrow.apache.org/docs/format/CanonicalExtensions.html
+
+#######
+# UUID Extension Type
+#######
+
+# CAST to UUID preserves extension metadata
+query ?T
+SELECT CAST(arrow_cast(X'00010203040506070809000102030506', 
'FixedSizeBinary(16)') AS UUID),
+       arrow_metadata(CAST(arrow_cast(X'00010203040506070809000102030506', 
'FixedSizeBinary(16)') AS UUID), 'ARROW:extension:name');
+----
+00010203040506070809000102030506 arrow.uuid
+
+# Test table creation and various insertion methods (Null, Naked Binary, and 
Cast)
+statement ok
+CREATE TABLE test_uuid (value UUID);
+
+query TTT
+DESCRIBE test_uuid;
+----
+value FixedSizeBinary(16) YES
+
+# 1. NULL insertion
+statement ok
+INSERT INTO test_uuid VALUES (NULL);
+
+# 2. Exercises Implicit Promotion to UUID from binary
+statement ok
+INSERT INTO test_uuid VALUES (X'00010203040506070809000102030506');
+
+# 3. Explicit CAST
+statement ok
+INSERT INTO test_uuid VALUES 
(CAST(arrow_cast(X'00010203040506070809000102030507', 'FixedSizeBinary(16)') AS 
UUID));
+
+query T
+SELECT arrow_metadata(value, 'ARROW:extension:name') FROM test_uuid;
+----
+arrow.uuid
+arrow.uuid
+arrow.uuid
+
+statement ok
+DROP TABLE test_uuid;
+
+#######
+# JSON Extension Type
+#######
+
+# CAST to JSON preserves extension metadata
+query TT
+SELECT CAST('{"a": 1}' AS JSON),
+       arrow_metadata(CAST('{"a": 1}' AS JSON), 'ARROW:extension:name');
+----
+{"a": 1} arrow.json
+
+statement ok
+CREATE TABLE test_json (data JSON);
+
+query TTT
+DESCRIBE test_json;
+----
+data Utf8 YES
+
+# 1. NULL insertion
+statement ok
+INSERT INTO test_json VALUES (NULL);
+
+# 2. Bare string literal (Implicit Promotion)
+statement ok
+INSERT INTO test_json VALUES ('{"bare": "literal"}');
+
+# 3. Explicit CAST
+statement ok
+INSERT INTO test_json VALUES (CAST('{"hello": "world"}' AS JSON));
+
+query T
+SELECT arrow_metadata(data, 'ARROW:extension:name') FROM test_json;
+----
+arrow.json
+arrow.json
+arrow.json
+
+# Verify Type Decay: Concatenation results in a bare Utf8 string
+query T
+SELECT arrow_metadata(data || '{"extra": 1}', 'ARROW:extension:name') FROM 
test_json;
+----
+NULL
+NULL
+NULL
+
+statement ok
+DROP TABLE test_json;
+
+#######
+# Error Handling
+#######
+
+# Error: Cannot cast an integer to UUID
+statement error Unsupported CAST from Int64 to FixedSizeBinary\(16\)
+SELECT CAST(12345 AS UUID);
+
+# Error: Cannot cast a short string to UUID
+statement error Unsupported CAST from Utf8 to FixedSizeBinary\(16\)
+SELECT CAST('too-short' AS UUID);
+
+# This should trigger cast plan_err: "Cannot cast Utf8 to extension type at 
column 0"
+# Because 'not-a-uuid' is Utf8, and it's being compared to a UUID target in a 
VALUES list.
+statement ok
+CREATE TABLE t1 (id UUID);
+
+# Then, try to insert the invalid type
+statement error Cannot automatically convert Utf8 to FixedSizeBinary\(16\)
+INSERT INTO t1 VALUES ('not-a-uuid');
+
+statement ok
+INSERT INTO t1 VALUES (NULL);
+
+statement ok
+DROP TABLE t1;
+
+# Error: Cannot mix different extension types
+statement ok
+CREATE TABLE mix_types (id UUID);
+
+# Even if the JSON string is 16 bytes, it lacks the arrow.uuid metadata
+statement error Cannot automatically convert Utf8 to FixedSizeBinary\(16\)

Review Comment:
   Wouldn't a test with a `FixedSizeBinary(16)` value with a different 
extension type name make more sense here?



##########
datafusion/sqllogictest/test_files/sql_extension_types.slt:
##########
@@ -0,0 +1,279 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Tests for built-in mapping of SQL types to Arrow canonical extension types.
+# See: https://arrow.apache.org/docs/format/CanonicalExtensions.html
+
+#######
+# UUID Extension Type
+#######
+
+# CAST to UUID preserves extension metadata
+query ?T
+SELECT CAST(arrow_cast(X'00010203040506070809000102030506', 
'FixedSizeBinary(16)') AS UUID),
+       arrow_metadata(CAST(arrow_cast(X'00010203040506070809000102030506', 
'FixedSizeBinary(16)') AS UUID), 'ARROW:extension:name');
+----
+00010203040506070809000102030506 arrow.uuid
+
+# Test table creation and various insertion methods (Null, Naked Binary, and 
Cast)
+statement ok
+CREATE TABLE test_uuid (value UUID);
+
+query TTT
+DESCRIBE test_uuid;
+----
+value FixedSizeBinary(16) YES
+
+# 1. NULL insertion
+statement ok
+INSERT INTO test_uuid VALUES (NULL);
+
+# 2. Exercises Implicit Promotion to UUID from binary
+statement ok
+INSERT INTO test_uuid VALUES (X'00010203040506070809000102030506');
+
+# 3. Explicit CAST
+statement ok
+INSERT INTO test_uuid VALUES 
(CAST(arrow_cast(X'00010203040506070809000102030507', 'FixedSizeBinary(16)') AS 
UUID));
+
+query T
+SELECT arrow_metadata(value, 'ARROW:extension:name') FROM test_uuid;
+----
+arrow.uuid
+arrow.uuid
+arrow.uuid
+
+statement ok
+DROP TABLE test_uuid;
+
+#######
+# JSON Extension Type
+#######
+
+# CAST to JSON preserves extension metadata
+query TT
+SELECT CAST('{"a": 1}' AS JSON),
+       arrow_metadata(CAST('{"a": 1}' AS JSON), 'ARROW:extension:name');
+----
+{"a": 1} arrow.json
+
+statement ok
+CREATE TABLE test_json (data JSON);
+
+query TTT
+DESCRIBE test_json;
+----
+data Utf8 YES
+
+# 1. NULL insertion
+statement ok
+INSERT INTO test_json VALUES (NULL);
+
+# 2. Bare string literal (Implicit Promotion)
+statement ok
+INSERT INTO test_json VALUES ('{"bare": "literal"}');
+
+# 3. Explicit CAST
+statement ok
+INSERT INTO test_json VALUES (CAST('{"hello": "world"}' AS JSON));
+
+query T
+SELECT arrow_metadata(data, 'ARROW:extension:name') FROM test_json;
+----
+arrow.json
+arrow.json
+arrow.json
+
+# Verify Type Decay: Concatenation results in a bare Utf8 string
+query T
+SELECT arrow_metadata(data || '{"extra": 1}', 'ARROW:extension:name') FROM 
test_json;
+----
+NULL
+NULL
+NULL
+
+statement ok
+DROP TABLE test_json;
+
+#######
+# Error Handling
+#######
+
+# Error: Cannot cast an integer to UUID
+statement error Unsupported CAST from Int64 to FixedSizeBinary\(16\)
+SELECT CAST(12345 AS UUID);
+
+# Error: Cannot cast a short string to UUID
+statement error Unsupported CAST from Utf8 to FixedSizeBinary\(16\)
+SELECT CAST('too-short' AS UUID);
+
+# This should trigger cast plan_err: "Cannot cast Utf8 to extension type at 
column 0"
+# Because 'not-a-uuid' is Utf8, and it's being compared to a UUID target in a 
VALUES list.
+statement ok
+CREATE TABLE t1 (id UUID);
+
+# Then, try to insert the invalid type
+statement error Cannot automatically convert Utf8 to FixedSizeBinary\(16\)
+INSERT INTO t1 VALUES ('not-a-uuid');
+
+statement ok
+INSERT INTO t1 VALUES (NULL);
+
+statement ok
+DROP TABLE t1;
+
+# Error: Cannot mix different extension types
+statement ok
+CREATE TABLE mix_types (id UUID);
+
+# Even if the JSON string is 16 bytes, it lacks the arrow.uuid metadata
+statement error Cannot automatically convert Utf8 to FixedSizeBinary\(16\)
+INSERT INTO mix_types VALUES (CAST('{"key": "value"}' AS JSON));
+
+statement ok
+DROP TABLE mix_types;
+
+statement ok
+CREATE TABLE multi_col (info JSON, id UUID);
+
+# Succeed with valid types
+statement ok
+INSERT INTO multi_col VALUES 
+  (CAST('{"a":1}' AS JSON), 
CAST(arrow_cast(X'00010203040506070809000102030506', 'FixedSizeBinary(16)') AS 
UUID));
+
+# This will fail because a Boolean cannot be cast to FixedSizeBinary(16)
+statement error Cannot automatically convert Boolean to FixedSizeBinary\(16\)
+INSERT INTO multi_col VALUES 
+  (CAST('{"a":1}' AS JSON), true);
+
+statement ok
+DROP TABLE multi_col;
+
+statement ok
+CREATE TABLE bulk_uuid (id UUID);

Review Comment:
   Can we do the same test on the `test_uuid` table from above?



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