paleolimbot commented on code in PR #21714: URL: https://github.com/apache/datafusion/pull/21714#discussion_r4278268291
########## 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); + +# Batch of multiple values (some NULL) +statement ok +INSERT INTO bulk_uuid VALUES + (CAST(arrow_cast(X'00010203040506070809000102030506', 'FixedSizeBinary(16)') AS UUID)), + (NULL), + (CAST(arrow_cast(X'00010203040506070809000102030507', 'FixedSizeBinary(16)') AS UUID)); + +query I +SELECT COUNT(*) FROM bulk_uuid; +---- +3 + +statement ok +DROP TABLE bulk_uuid; + +####### +# Relational Integration +####### Review Comment: I think this should be removed...these are bugs that are tracked elsewhere (or if they aren't...we should make sure they are filed!) ########## datafusion/sql/src/planner.rs: ########## @@ -677,6 +679,31 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> { // If no type_planner can handle this type, use the default conversion match sql_type { + // Canonical Arrow extension types + SQLDataType::Uuid => { Review Comment: Just flagging for myself and others that this is the main change: `UUID` and `JSON` types are now parsed in SQL. ########## 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: I have the same question here...my sense is that we should not remove this bit of code in this PR. ########## 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 +####### Review Comment: I *think* these tests are worth having here; however, any of them that are asserting incorrect behaviour that just happens to exist right now should be removed. This PR would also be equally strong without this file...the behaviour tested here mostly works by accident and as we go through and make it work on purpose we can add in these tests. If you want to keep this file, Tobias' suggestions are good and the tests for the two types should be in sync -- 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]
