should be it
This commit is contained in:
75
external/duckdb/test/sql/function/variant/variant_extract.test
vendored
Normal file
75
external/duckdb/test/sql/function/variant/variant_extract.test
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
# name: test/sql/function/variant/variant_extract.test
|
||||
# group: [variant]
|
||||
|
||||
statement ok
|
||||
pragma enable_verification;
|
||||
|
||||
require json
|
||||
|
||||
query I
|
||||
select variant_extract({'a': 1234}::VARIANT, 'a')::VARCHAR;
|
||||
----
|
||||
1234
|
||||
|
||||
statement ok
|
||||
CREATE MACRO struct_cast_data() AS TABLE (
|
||||
SELECT {'a': [
|
||||
{
|
||||
'b': 'hello',
|
||||
'c': NULL,
|
||||
'a': '1970/03/15'::DATE
|
||||
},
|
||||
{
|
||||
'b': NULL,
|
||||
'c': True,
|
||||
'a': '2020/11/03'::DATE
|
||||
}
|
||||
]}::VARIANT AS a
|
||||
UNION ALL
|
||||
SELECT {'a': [
|
||||
{
|
||||
'b': 'this is a long string',
|
||||
'c': False,
|
||||
'a': '1953/9/16'::DATE
|
||||
}
|
||||
]}::VARIANT
|
||||
);
|
||||
|
||||
statement error
|
||||
select variant_extract(a, 'a[1].c') from struct_cast_data();
|
||||
----
|
||||
Invalid Input Error: VARIANT(OBJECT(a)) is missing key 'a[1].c'
|
||||
|
||||
query I
|
||||
select variant_extract(a, 'a').variant_extract(1::UINTEGER).variant_extract('c') from struct_cast_data();
|
||||
----
|
||||
NULL
|
||||
false
|
||||
|
||||
# Using shorthand notation
|
||||
|
||||
query I
|
||||
select ('{"a": 42, "b": [true, "test", true]}'::JSON::VARIANT).b[2];
|
||||
----
|
||||
test
|
||||
|
||||
query I
|
||||
select ('{"a": 42, "b": [true, "test", true]}'::JSON::VARIANT)['b'][2];
|
||||
----
|
||||
test
|
||||
|
||||
# When the argument is a string, it's taken as an object field, not an array index
|
||||
statement error
|
||||
select ('{"a": 42, "b": [true, "test", true]}'::JSON::VARIANT)['b']['1'];
|
||||
----
|
||||
Invalid Input Error: Can't extract key '1' from a VARIANT(ARRAY)
|
||||
|
||||
statement error
|
||||
select ('{"a": 42, "b": [true, "test", true]}'::JSON::VARIANT)['b[2]'];
|
||||
----
|
||||
Invalid Input Error: VARIANT(OBJECT(a,b)) is missing key 'b[2]'
|
||||
|
||||
query I
|
||||
select ('{"a": 42, "b": [true, "test", true]}'::JSON::VARIANT)['b'][2];
|
||||
----
|
||||
test
|
||||
99
external/duckdb/test/sql/function/variant/variant_typeof.test
vendored
Normal file
99
external/duckdb/test/sql/function/variant/variant_typeof.test
vendored
Normal file
@@ -0,0 +1,99 @@
|
||||
# name: test/sql/function/variant/variant_typeof.test
|
||||
# group: [variant]
|
||||
|
||||
statement ok
|
||||
pragma enable_verification;
|
||||
|
||||
query I
|
||||
select variant_typeof({'a': 42}::VARIANT);
|
||||
----
|
||||
OBJECT(a)
|
||||
|
||||
query I
|
||||
select variant_typeof(({'a': 42}::VARIANT).variant_extract('a'));
|
||||
----
|
||||
INT32
|
||||
|
||||
query I
|
||||
select variant_typeof(struct_pack(*COLUMNS(*))::VARIANT) test from test_all_types();
|
||||
----
|
||||
OBJECT(bool, tinyint, smallint, int, bigint, hugeint, uhugeint, utinyint, usmallint, uint, ubigint, bignum, date, time, timestamp, timestamp_s, timestamp_ms, timestamp_ns, time_tz, timestamp_tz, float, double, dec_4_1, dec_9_4, dec_18_6, dec38_10, uuid, interval, varchar, blob, bit, small_enum, medium_enum, large_enum, int_array, double_array, date_array, timestamp_array, timestamptz_array, varchar_array, nested_int_array, struct, struct_of_arrays, array_of_structs, map, union, fixed_int_array, fixed_varchar_array, fixed_nested_int_array, fixed_nested_varchar_array, fixed_struct_array, struct_of_fixed_array, fixed_array_of_int_list, list_of_fixed_int_array)
|
||||
OBJECT(bool, tinyint, smallint, int, bigint, hugeint, uhugeint, utinyint, usmallint, uint, ubigint, bignum, date, time, timestamp, timestamp_s, timestamp_ms, timestamp_ns, time_tz, timestamp_tz, float, double, dec_4_1, dec_9_4, dec_18_6, dec38_10, uuid, interval, varchar, blob, bit, small_enum, medium_enum, large_enum, int_array, double_array, date_array, timestamp_array, timestamptz_array, varchar_array, nested_int_array, struct, struct_of_arrays, array_of_structs, map, union, fixed_int_array, fixed_varchar_array, fixed_nested_int_array, fixed_nested_varchar_array, fixed_struct_array, struct_of_fixed_array, fixed_array_of_int_list, list_of_fixed_int_array)
|
||||
OBJECT(bool, tinyint, smallint, int, bigint, hugeint, uhugeint, utinyint, usmallint, uint, ubigint, bignum, date, time, timestamp, timestamp_s, timestamp_ms, timestamp_ns, time_tz, timestamp_tz, float, double, dec_4_1, dec_9_4, dec_18_6, dec38_10, uuid, interval, varchar, blob, bit, small_enum, medium_enum, large_enum, int_array, double_array, date_array, timestamp_array, timestamptz_array, varchar_array, nested_int_array, struct, struct_of_arrays, array_of_structs, map, union, fixed_int_array, fixed_varchar_array, fixed_nested_int_array, fixed_nested_varchar_array, fixed_struct_array, struct_of_fixed_array, fixed_array_of_int_list, list_of_fixed_int_array)
|
||||
|
||||
statement error
|
||||
CREATE TABLE T (v VARIANT);
|
||||
----
|
||||
A table cannot be created from a VARIANT column yet
|
||||
|
||||
statement error
|
||||
create table all_types as select struct_pack(*COLUMNS(*))::VARIANT test from test_all_types();
|
||||
----
|
||||
Not implemented Error: A table cannot be created from a VARIANT column yet
|
||||
|
||||
query I
|
||||
with all_types as (
|
||||
select struct_pack(*COLUMNS(*))::VARIANT test from test_all_types()
|
||||
)
|
||||
select variant_typeof(variant_extract(test, 'bool')) from all_types;
|
||||
----
|
||||
BOOL_FALSE
|
||||
BOOL_TRUE
|
||||
VARIANT_NULL
|
||||
|
||||
query I
|
||||
with all_types as (
|
||||
select struct_pack(*COLUMNS(*))::VARIANT test from test_all_types()
|
||||
)
|
||||
select variant_typeof(variant_extract(test, 'struct')) from all_types;
|
||||
----
|
||||
OBJECT(a, b)
|
||||
OBJECT(a, b)
|
||||
VARIANT_NULL
|
||||
|
||||
query I
|
||||
with all_types as (
|
||||
select struct_pack(*COLUMNS(*))::VARIANT test from test_all_types()
|
||||
)
|
||||
select variant_typeof(variant_extract(test, 'struct').a) from all_types;
|
||||
----
|
||||
VARIANT_NULL
|
||||
INT32
|
||||
VARIANT_NULL
|
||||
|
||||
query I
|
||||
with all_types as (
|
||||
select struct_pack(*COLUMNS(*))::VARIANT test from test_all_types()
|
||||
)
|
||||
select variant_typeof(variant_extract(test, 'struct').variant_extract('a')) from all_types limit 2;
|
||||
----
|
||||
VARIANT_NULL
|
||||
INT32
|
||||
|
||||
query I
|
||||
with all_types as (
|
||||
select struct_pack(*COLUMNS(*))::VARIANT test from test_all_types()
|
||||
)
|
||||
select variant_typeof(variant_extract(test, 'dec_18_6')) from all_types
|
||||
----
|
||||
DECIMAL(18, 6)
|
||||
DECIMAL(18, 6)
|
||||
VARIANT_NULL
|
||||
|
||||
query I
|
||||
with all_types as (
|
||||
select struct_pack(*COLUMNS(*))::VARIANT test from test_all_types()
|
||||
)
|
||||
select variant_typeof(variant_extract(test, 'array_of_structs')) from all_types
|
||||
----
|
||||
ARRAY(0)
|
||||
ARRAY(3)
|
||||
VARIANT_NULL
|
||||
|
||||
query III
|
||||
with all_types as (
|
||||
select struct_pack(*COLUMNS(*))::VARIANT test from test_all_types() offset 1 limit 1
|
||||
)
|
||||
select variant_typeof(variant_extract(test, 'array_of_structs')[1]), variant_typeof(variant_extract(test, 'array_of_structs')[2]), variant_typeof(variant_extract(test, 'array_of_structs')[3]) from all_types;
|
||||
----
|
||||
OBJECT(a, b) OBJECT(a, b) VARIANT_NULL
|
||||
Reference in New Issue
Block a user