Files
email-tracker/external/duckdb/test/common/test_cast_struct.test
2025-10-24 19:21:19 -05:00

169 lines
4.1 KiB
SQL

# name: test/common/test_cast_struct.test
# description: Test casting structs
# group: [common]
statement ok
PRAGMA enable_verification
statement error
SELECT struct_pack(b => 42)::STRUCT(a INT);
----
<REGEX>:Binder Error.*STRUCT to STRUCT cast must have at least one matching member.*
statement error
SELECT struct_extract(struct_pack(b => 42)::STRUCT(a INT), 'a');
----
<REGEX>:Binder Error.*STRUCT to STRUCT cast must have at least one matching member.*
query I
SELECT struct_extract(struct_pack(a => 42)::STRUCT(a STRING), 'a');
----
42
statement error
SELECT struct_extract(struct_pack(b => 42)::ROW(a INT), 'a');
----
<REGEX>:Binder Error.*STRUCT to STRUCT cast must have at least one matching member.*
query I
SELECT struct_extract(struct_pack(a => 42)::ROW(a INT), 'a');
----
42
statement error
SELECT struct_extract(struct_pack(b => 42::DOUBLE)::STRUCT(a INT), 'a');
----
<REGEX>:Binder Error.*STRUCT to STRUCT cast must have at least one matching member.*
query I
SELECT struct_extract(struct_pack(a => 42::DOUBLE)::STRUCT(a INT), 'a');
----
42
statement error
SELECT struct_extract(struct_pack(b => '42'::DOUBLE)::STRUCT(a INT), 'a');
----
<REGEX>:Binder Error.*STRUCT to STRUCT cast must have at least one matching member.*
query I
SELECT struct_extract(struct_pack(a => '42'::DOUBLE)::STRUCT(a INT), 'a');
----
42
statement error
SELECT struct_pack(b => '42'::DOUBLE)::STRUCT(a INT, c STRING)
----
<REGEX>:Binder Error.*STRUCT to STRUCT cast must have at least one matching member.*
statement error
SELECT struct_pack(b => 'hello'::STRING)::STRUCT(b INT)
----
Could not convert string 'hello' to INT32
statement error
SELECT struct_pack(a => 'hello'::STRING, b => 'world'::STRING)::STRUCT(a STRING, b INT)
----
Could not convert string 'world' to INT32
statement error
SELECT struct_pack(a => [1, 2, 3])::STRUCT(a INT)
----
Unimplemented type for cast (INTEGER[] -> INTEGER)
statement error
SELECT struct_pack(a => struct_pack(b => 42)::STRUCT(b INT))::STRUCT(a INT)
----
Unimplemented type for cast (STRUCT(b INTEGER) -> INTEGER)
statement error
SELECT struct_pack(b => 'hello'::STRING)::STRUCT(a INT)
----
<REGEX>:Binder Error.*STRUCT to STRUCT cast must have at least one matching member.*
statement error
SELECT struct_pack(b => '42'::DOUBLE, c => 'asdf'::STRING)::STRUCT(a1 INT, a2 STRING);
----
<REGEX>:Binder Error.*STRUCT to STRUCT cast must have at least one matching member.*
query I
SELECT struct_pack(a1 => '42'::DOUBLE, a2 => 'asdf'::STRING)::STRUCT(a1 INT, a2 STRING);
----
{'a1': 42, 'a2': asdf}
query I
SELECT ROW(42, 'asdf');
----
(42, asdf)
statement error
SELECT ROW();
----
pack nothing into a struct
query I
SELECT ROW(NULL);
----
(NULL)
query I
SELECT ROW(NULL, NULL);
----
(NULL, NULL)
# MB example
query I
SELECT CAST(ROW(1, 2) AS ROW(a INTEGER, b INTEGER))
----
{'a': 1, 'b': 2}
query I
SELECT a::ROW(a INT, b STRING) r FROM (VALUES (ROW(1, 'asdf')), (ROW(4, 'fdsa'))) s(a);
----
{'a': 1, 'b': asdf}
{'a': 4, 'b': fdsa}
statement error
SELECT struct_extract({'a': a}, a) FROM (SELECT a::VARCHAR AS a FROM range(10) tbl(a));
----
<REGEX>:.*Binder Error.*Key name for struct_extract needs to be a constant string.*
statement error
SELECT struct_extract({'a': 42}, 42)
----
<REGEX>:.*Binder Error.*can only be used on unnamed structs.*
query I
SELECT struct_extract_at({'a': 42}, 1)
----
42
statement error
SELECT struct_extract_at({'a': 42}, 0)
----
<REGEX>:.*Binder Error.*out of range.*
statement error
SELECT struct_extract_at({'a': 42}, 42)
----
<REGEX>:.*Binder Error.*out of range.*
# Test string to struct cast within struct casting.
query I
SELECT {a: {b: '{a: 3, b: "Hello World"}'}}::STRUCT(a STRUCT(b STRUCT(a INT, b VARCHAR)));
----
{'a': {'b': {'a': 3, 'b': Hello World}}}
# Test if try_cast continues after encountering error.
query I
SELECT TRY_CAST(struct_pack(a => 4, b => 'Ducky', c => '1964-06-15')
AS STRUCT(a INT, b DOUBLE, c DATE));
----
{'a': 4, 'b': NULL, 'c': 1964-06-15}
query I
SELECT TRY_CAST(struct_pack(a => 4, b => 'Ducky', c => 'Tommorow', d => {a:3.0})
AS STRUCT(a VARCHAR[], b VARCHAR, c DATE, d STRUCT(a INT)));
----
{'a': NULL, 'b': Ducky, 'c': NULL, 'd': {'a': 3}}