|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership |
| 3 | +# SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements |
| 4 | + |
| 5 | +# Tests for built-in mapping of SQL types to Arrow canonical extension types. |
| 6 | +# See: https://arrow.apache.org/docs/format/CanonicalExtensions.html |
| 7 | + |
| 8 | +####### |
| 9 | +# UUID Extension Type |
| 10 | +####### |
| 11 | + |
| 12 | +# CAST to UUID preserves extension metadata |
| 13 | +query ?T |
| 14 | +SELECT CAST(X'00010203040506070809000102030506' AS UUID), |
| 15 | + arrow_metadata(CAST(X'00010203040506070809000102030506' AS UUID), 'ARROW:extension:name'); |
| 16 | +---- |
| 17 | +00010203040506070809000102030506 arrow.uuid |
| 18 | + |
| 19 | +# Test table creation and various insertion methods (Null, Naked Binary, and Cast) |
| 20 | +statement ok |
| 21 | +CREATE TABLE test_uuid (value UUID); |
| 22 | + |
| 23 | +query TTT |
| 24 | +DESCRIBE test_uuid; |
| 25 | +---- |
| 26 | +value FixedSizeBinary(16) YES |
| 27 | + |
| 28 | +# 1. NULL insertion |
| 29 | +statement ok |
| 30 | +INSERT INTO test_uuid VALUES (NULL); |
| 31 | + |
| 32 | +# 2. Exercises Implicit Promotion to UUID from binary |
| 33 | +statement ok |
| 34 | +INSERT INTO test_uuid VALUES (X'00010203040506070809000102030506'); |
| 35 | + |
| 36 | +# 3. Explicit CAST |
| 37 | +statement ok |
| 38 | +INSERT INTO test_uuid VALUES (CAST(X'00010203040506070809000102030507' AS UUID)); |
| 39 | + |
| 40 | +query T |
| 41 | +SELECT arrow_metadata(value, 'ARROW:extension:name') FROM test_uuid; |
| 42 | +---- |
| 43 | +arrow.uuid |
| 44 | +arrow.uuid |
| 45 | +arrow.uuid |
| 46 | + |
| 47 | +# Batch of multiple values (some NULL) |
| 48 | +statement ok |
| 49 | +INSERT INTO test_uuid VALUES |
| 50 | + (CAST(X'00010203040506070809000102030506' AS UUID)), |
| 51 | + (NULL), |
| 52 | + (CAST(X'00010203040506070809000102030507' AS UUID)); |
| 53 | + |
| 54 | +query I |
| 55 | +SELECT COUNT(*) FROM test_uuid; |
| 56 | +---- |
| 57 | +6 |
| 58 | + |
| 59 | +statement ok |
| 60 | +DROP TABLE test_uuid; |
| 61 | + |
| 62 | +####### |
| 63 | +# JSON Extension Type |
| 64 | +####### |
| 65 | + |
| 66 | +# CAST to JSON preserves extension metadata |
| 67 | +query TT |
| 68 | +SELECT CAST('{"a": 1}' AS JSON), |
| 69 | + arrow_metadata(CAST('{"a": 1}' AS JSON), 'ARROW:extension:name'); |
| 70 | +---- |
| 71 | +{"a": 1} arrow.json |
| 72 | + |
| 73 | +statement ok |
| 74 | +CREATE TABLE test_json (data JSON); |
| 75 | + |
| 76 | +query TTT |
| 77 | +DESCRIBE test_json; |
| 78 | +---- |
| 79 | +data Utf8 YES |
| 80 | + |
| 81 | +# 1. NULL insertion |
| 82 | +statement ok |
| 83 | +INSERT INTO test_json VALUES (NULL); |
| 84 | + |
| 85 | +# 2. Bare string literal (Implicit Promotion) |
| 86 | +statement ok |
| 87 | +INSERT INTO test_json VALUES ('{"bare": "literal"}'); |
| 88 | + |
| 89 | +# 3. Explicit CAST |
| 90 | +statement ok |
| 91 | +INSERT INTO test_json VALUES (CAST('{"hello": "world"}' AS JSON)); |
| 92 | + |
| 93 | +query T |
| 94 | +SELECT arrow_metadata(data, 'ARROW:extension:name') FROM test_json; |
| 95 | +---- |
| 96 | +arrow.json |
| 97 | +arrow.json |
| 98 | +arrow.json |
| 99 | + |
| 100 | +# Verify Type Decay: Concatenation results in a bare Utf8 string |
| 101 | +query T |
| 102 | +SELECT arrow_metadata(data || '{"extra": 1}', 'ARROW:extension:name') FROM test_json; |
| 103 | +---- |
| 104 | +NULL |
| 105 | +NULL |
| 106 | +NULL |
| 107 | + |
| 108 | +statement ok |
| 109 | +DROP TABLE test_json; |
| 110 | + |
| 111 | +####### |
| 112 | +# Error Handling |
| 113 | +####### |
| 114 | + |
| 115 | +# Error: Cannot cast an integer to UUID |
| 116 | +statement error Unsupported CAST from Int64 to FixedSizeBinary\(16\) |
| 117 | +SELECT CAST(12345 AS UUID); |
| 118 | + |
| 119 | +# Error: Cannot cast a short string to UUID |
| 120 | +statement error Unsupported CAST from Utf8 to FixedSizeBinary\(16\) |
| 121 | +SELECT CAST('too-short' AS UUID); |
| 122 | + |
| 123 | +# This should trigger cast plan_err: "Types don't match and no valid cast exists" |
| 124 | +# Because 'not-a-uuid' is Utf8, and it's being compared to a UUID target in a VALUES list. |
| 125 | +statement ok |
| 126 | +CREATE TABLE t1 (id UUID); |
| 127 | + |
| 128 | +# Then, try to insert the invalid type |
| 129 | +statement error Types don't match and no valid cast exists, received data of type Utf8 for field of type FixedSizeBinary\(16\) |
| 130 | +INSERT INTO t1 VALUES ('not-a-uuid'); |
| 131 | + |
| 132 | +statement ok |
| 133 | +INSERT INTO t1 VALUES (NULL); |
| 134 | + |
| 135 | +statement ok |
| 136 | +DROP TABLE t1; |
0 commit comments