Skip to content

Commit 09ce72f

Browse files
authored
Update cast_to_variant slt tests (#54)
* Update cast_to_variant slt tests * add primitive array tests
1 parent 6b078dd commit 09ce72f

1 file changed

Lines changed: 175 additions & 43 deletions

File tree

tests/test_files/cast_to_variant.slt

Lines changed: 175 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,210 @@
22
# this function takes a Variant Scalar or Array and converts it into a respective Variant value
33
# you can also use metadata and value to create a Variant
44

5-
# one-arg path (from_scalar_value)
6-
# test_scalar_float64
7-
query T
8-
SELECT variant_pretty(cast_to_variant(3.25));
9-
----
10-
Double(3.25)
11-
12-
# one-arg path (from_array)
13-
# test_array_int32
5+
# one-arg path (from_array): primitive coverage
146
statement ok
157
CREATE TABLE cast_int_data (id INT, i INT) AS VALUES
168
(1, 1),
179
(2, NULL),
1810
(3, -5);
1911

20-
query IT
21-
SELECT id, variant_pretty(cast_to_variant(i)) FROM cast_int_data ORDER BY id;
12+
query ITTTTTT
13+
SELECT
14+
id,
15+
variant_pretty(cast_to_variant(i)),
16+
variant_pretty(cast_to_variant(arrow_cast(i, 'Int8'))),
17+
variant_pretty(cast_to_variant(arrow_cast(i, 'Int16'))),
18+
variant_pretty(cast_to_variant(arrow_cast(i, 'Int64'))),
19+
variant_pretty(cast_to_variant(arrow_cast(i, 'Float32'))),
20+
variant_pretty(cast_to_variant(arrow_cast(i, 'Float64')))
21+
FROM cast_int_data
22+
ORDER BY id;
2223
----
23-
1 Int32(1)
24-
2 NULL
25-
3 Int32(-5)
24+
1 Int32(1) Int8(1) Int16(1) Int64(1) Float(1.0) Double(1.0)
25+
2 NULL NULL NULL NULL NULL NULL
26+
3 Int32(-5) Int8(-5) Int16(-5) Int64(-5) Float(-5.0) Double(-5.0)
2627

27-
# test_array_string
28+
# one-arg path (from_array): string family with nulls
2829
statement ok
29-
CREATE TABLE cast_string_data (id INT, s TEXT) AS VALUES
30-
(1, 'abcdefghijklmnop'),
30+
CREATE TABLE cast_string_array_data (id INT, s TEXT) AS VALUES
31+
(1, 'hello world'),
3132
(2, NULL),
32-
(3, 'hello world');
33+
(3, 'Alex Dave Van Alex Dave Van Alex Dave Van Alex Dave Van Alex Dave Van');
3334

34-
query IT
35-
SELECT id, variant_pretty(cast_to_variant(s)) FROM cast_string_data ORDER BY id;
36-
----
37-
1 ShortString(ShortString("abcdefghijklmnop"))
38-
2 NULL
39-
3 ShortString(ShortString("hello world"))
40-
41-
# test_array_string_view
42-
query IT
43-
SELECT id, variant_pretty(cast_to_variant(arrow_cast(s, 'Utf8View'))) FROM cast_string_data ORDER BY id;
35+
query ITTT
36+
SELECT
37+
id,
38+
variant_pretty(cast_to_variant(s)),
39+
variant_pretty(cast_to_variant(arrow_cast(s, 'Utf8View'))),
40+
variant_pretty(cast_to_variant(arrow_cast(s, 'LargeUtf8')))
41+
FROM cast_string_array_data
42+
ORDER BY id;
4443
----
45-
1 ShortString(ShortString("abcdefghijklmnop"))
46-
2 NULL
47-
3 ShortString(ShortString("hello world"))
44+
1 ShortString(ShortString("hello world")) ShortString(ShortString("hello world")) ShortString(ShortString("hello world"))
45+
2 NULL NULL NULL
46+
3 String("Alex Dave Van Alex Dave Van Alex Dave Van Alex Dave Van Alex Dave Van") String("Alex Dave Van Alex Dave Van Alex Dave Van Alex Dave Van Alex Dave Van") String("Alex Dave Van Alex Dave Van Alex Dave Van Alex Dave Van Alex Dave Van")
4847

49-
# test_fixed_size_binary_uuid_like
50-
query IT
51-
SELECT id, variant_pretty(cast_to_variant(b))
48+
# one-arg path (from_array): binary family with nulls
49+
query ITTTT
50+
SELECT
51+
id,
52+
variant_pretty(cast_to_variant(b)),
53+
variant_pretty(cast_to_variant(lb)),
54+
variant_pretty(cast_to_variant(bv)),
55+
variant_pretty(cast_to_variant(fb))
5256
FROM (
5357
SELECT
5458
1 AS id,
55-
arrow_cast(X'01010101010101010101010101010101', 'FixedSizeBinary(16)') AS b
59+
arrow_cast(X'0a', 'Binary') AS b,
60+
arrow_cast(X'0b', 'LargeBinary') AS lb,
61+
arrow_cast(X'0c', 'BinaryView') AS bv,
62+
arrow_cast(X'01020304', 'FixedSizeBinary(4)') AS fb
5663
UNION ALL
5764
SELECT
5865
2 AS id,
59-
arrow_cast(NULL, 'FixedSizeBinary(16)') AS b
60-
UNION ALL
61-
SELECT
62-
3 AS id,
63-
arrow_cast(X'02020202020202020202020202020202', 'FixedSizeBinary(16)') AS b
66+
arrow_cast(NULL, 'Binary') AS b,
67+
arrow_cast(NULL, 'LargeBinary') AS lb,
68+
arrow_cast(NULL, 'BinaryView') AS bv,
69+
arrow_cast(NULL, 'FixedSizeBinary(4)') AS fb
6470
) t
6571
ORDER BY id;
6672
----
67-
1 Binary(01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01)
68-
2 NULL
69-
3 Binary(02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02)
73+
1 Binary(0a) Binary(0b) Binary(0c) Binary(01 02 03 04)
74+
2 NULL NULL NULL NULL
75+
76+
# one-arg path: primitive scalar coverage
77+
query TTTTTTTT
78+
SELECT
79+
variant_pretty(cast_to_variant(arrow_cast(true, 'Boolean'))),
80+
variant_pretty(cast_to_variant(arrow_cast(-8, 'Int8'))),
81+
variant_pretty(cast_to_variant(arrow_cast(-16, 'Int16'))),
82+
variant_pretty(cast_to_variant(arrow_cast(-32, 'Int32'))),
83+
variant_pretty(cast_to_variant(arrow_cast(-64, 'Int64'))),
84+
variant_pretty(cast_to_variant(arrow_cast(1.5, 'Float16'))),
85+
variant_pretty(cast_to_variant(arrow_cast(1.25, 'Float32'))),
86+
variant_pretty(cast_to_variant(arrow_cast(3.25, 'Float64')));
87+
----
88+
BooleanTrue Int8(-8) Int16(-16) Int32(-32) Int64(-64) Float(1.5) Float(1.25) Double(3.25)
89+
90+
# one-arg path: string scalar coverage
91+
query TTTTTT
92+
SELECT
93+
variant_pretty(cast_to_variant(arrow_cast('hello world', 'Utf8'))),
94+
variant_pretty(cast_to_variant(arrow_cast('hello world', 'Utf8View'))),
95+
variant_pretty(cast_to_variant(arrow_cast('hello world', 'LargeUtf8'))),
96+
variant_pretty(cast_to_variant(arrow_cast('Alex Dave Van Alex Dave Van Alex Dave Van Alex Dave Van Alex Dave Van', 'Utf8'))),
97+
variant_pretty(cast_to_variant(arrow_cast('Alex Dave Van Alex Dave Van Alex Dave Van Alex Dave Van Alex Dave Van', 'Utf8View'))),
98+
variant_pretty(cast_to_variant(arrow_cast('Alex Dave Van Alex Dave Van Alex Dave Van Alex Dave Van Alex Dave Van', 'LargeUtf8')));
99+
----
100+
ShortString(ShortString("hello world")) ShortString(ShortString("hello world")) ShortString(ShortString("hello world")) String("Alex Dave Van Alex Dave Van Alex Dave Van Alex Dave Van Alex Dave Van") String("Alex Dave Van Alex Dave Van Alex Dave Van Alex Dave Van Alex Dave Van") String("Alex Dave Van Alex Dave Van Alex Dave Van Alex Dave Van Alex Dave Van")
101+
102+
# one-arg path: unsigned integer coverage
103+
query TTTT
104+
SELECT
105+
variant_pretty(cast_to_variant(arrow_cast(8, 'UInt8'))),
106+
variant_pretty(cast_to_variant(arrow_cast(16, 'UInt16'))),
107+
variant_pretty(cast_to_variant(arrow_cast(32, 'UInt32'))),
108+
variant_pretty(cast_to_variant(arrow_cast(64, 'UInt64')));
109+
----
110+
Int8(8) Int16(16) Int32(32) Int64(64)
111+
112+
# one-arg path: binary coverage
113+
query TTTT
114+
SELECT
115+
variant_pretty(cast_to_variant(arrow_cast(X'0a', 'Binary'))),
116+
variant_pretty(cast_to_variant(arrow_cast(X'0b', 'LargeBinary'))),
117+
variant_pretty(cast_to_variant(arrow_cast(X'0c', 'BinaryView'))),
118+
variant_pretty(cast_to_variant(arrow_cast(X'01020304', 'FixedSizeBinary(4)')));
119+
----
120+
Binary(0a) Binary(0b) Binary(0c) Binary(01 02 03 04)
121+
122+
# one-arg path: decimal coverage (Decimal32/64/128/256)
123+
query TTTT
124+
SELECT
125+
variant_pretty(cast_to_variant(arrow_cast(123.45, 'Decimal32(9,2)'))),
126+
variant_pretty(cast_to_variant(arrow_cast(123.45, 'Decimal64(18,2)'))),
127+
variant_pretty(cast_to_variant(arrow_cast(123.45, 'Decimal128(20,2)'))),
128+
variant_pretty(cast_to_variant(arrow_cast(123.45, 'Decimal256(20,2)')));
129+
----
130+
Decimal4(VariantDecimal4 { integer: 12345, scale: 2 }) Decimal8(VariantDecimal8 { integer: 12345, scale: 2 }) Decimal16(VariantDecimal16 { integer: 12345, scale: 2 }) Decimal16(VariantDecimal16 { integer: 12345, scale: 2 })
131+
132+
# one-arg path: timestamp coverage (all units, no timezone)
133+
query TTTT
134+
SELECT
135+
variant_pretty(cast_to_variant(arrow_cast('2024-01-02T03:04:05', 'Timestamp(Second, None)'))),
136+
variant_pretty(cast_to_variant(arrow_cast('2024-01-02T03:04:05.123', 'Timestamp(Millisecond, None)'))),
137+
variant_pretty(cast_to_variant(arrow_cast('2024-01-02T03:04:05.123456', 'Timestamp(Microsecond, None)'))),
138+
variant_pretty(cast_to_variant(arrow_cast('2024-01-02T03:04:05.123456789', 'Timestamp(Nanosecond, None)')));
139+
----
140+
TimestampNtzMicros(2024-01-02T03:04:05) TimestampNtzMicros(2024-01-02T03:04:05.123) TimestampNtzMicros(2024-01-02T03:04:05.123456) TimestampNtzNanos(2024-01-02T03:04:05.123456789)
141+
142+
# one-arg path: timestamp coverage (all units, with timezone)
143+
query TTTT
144+
SELECT
145+
variant_pretty(cast_to_variant(arrow_cast('2024-01-02T03:04:05Z', 'Timestamp(Second, Some("UTC"))'))),
146+
variant_pretty(cast_to_variant(arrow_cast('2024-01-02T03:04:05.123Z', 'Timestamp(Millisecond, Some("UTC"))'))),
147+
variant_pretty(cast_to_variant(arrow_cast('2024-01-02T03:04:05.123456Z', 'Timestamp(Microsecond, Some("UTC"))'))),
148+
variant_pretty(cast_to_variant(arrow_cast('2024-01-02T03:04:05.123456789Z', 'Timestamp(Nanosecond, Some("UTC"))')));
149+
----
150+
TimestampMicros(2024-01-02T03:04:05Z) TimestampMicros(2024-01-02T03:04:05.123Z) TimestampMicros(2024-01-02T03:04:05.123456Z) TimestampNanos(2024-01-02T03:04:05.123456789Z)
151+
152+
# one-arg path: date and time coverage
153+
query TTTTTT
154+
SELECT
155+
variant_pretty(cast_to_variant(arrow_cast('2024-01-02', 'Date32'))),
156+
variant_pretty(cast_to_variant(arrow_cast('2024-01-02', 'Date64'))),
157+
variant_pretty(cast_to_variant(arrow_cast('01:02:03', 'Time32(Second)'))),
158+
variant_pretty(cast_to_variant(arrow_cast('01:02:03.123', 'Time32(Millisecond)'))),
159+
variant_pretty(cast_to_variant(arrow_cast('01:02:03.123456', 'Time64(Microsecond)'))),
160+
variant_pretty(cast_to_variant(arrow_cast('01:02:03.123456789', 'Time64(Nanosecond)')));
161+
----
162+
Date(2024-01-02) Date(2024-01-02) Time(01:02:03) Time(01:02:03.123) Time(01:02:03.123456) Time(01:02:03.123456)
163+
164+
# one-arg path: list coverage (List/LargeList/FixedSizeList)
165+
query TTT
166+
SELECT
167+
variant_pretty(cast_to_variant(arrow_cast(i, 'List(Int64)'))),
168+
variant_pretty(cast_to_variant(arrow_cast(i, 'LargeList(Int64)'))),
169+
variant_pretty(cast_to_variant(arrow_cast([i, i + 1, i + 2], 'FixedSizeList(3, Int64)')))
170+
FROM cast_int_data
171+
WHERE id = 1;
172+
----
173+
[Int64(1)] [Int64(1)] [Int64(1), Int64(2), Int64(3)]
174+
175+
# one-arg path: struct, map, and dictionary coverage
176+
query TTT
177+
SELECT
178+
variant_to_json(cast_to_variant(named_struct('a', 1, 'b', 'x'))),
179+
variant_to_json(cast_to_variant(MAP {'a': 1, 'b': 2})),
180+
variant_pretty(cast_to_variant(arrow_cast('foo', 'Dictionary(Int32, Utf8)')));
181+
----
182+
{"a":1,"b":"x"} {"a":1,"b":2} ShortString(ShortString("foo"))
183+
184+
# TODO: add coverage when SQL path / dependency behavior allows it
185+
# - Null input (currently awkward to assert in this file because variant_pretty on scalar null path is not stable)
186+
# - ListView / LargeListView inputs (DataFusion SQL cannot currently construct these scalar inputs here)
70187

71188
# one-arg path (already Variant): pass-through should be unchanged
72189
query T
73190
SELECT variant_pretty(cast_to_variant(json_to_variant('{"name": "norm"}')));
74191
----
75192
{"name": ShortString(ShortString("norm"))}
76193

194+
# one-arg path (from_array): Variant array pass-through should be unchanged
195+
statement ok
196+
CREATE TABLE cast_variant_array_data (id INT, json_str TEXT) AS VALUES
197+
(1, '{"name":"norm"}'),
198+
(2, NULL);
199+
200+
query IT
201+
WITH input AS (
202+
SELECT id, json_to_variant(json_str) AS v FROM cast_variant_array_data
203+
)
204+
SELECT id, variant_to_json(cast_to_variant(v)) FROM input ORDER BY id;
205+
----
206+
1 {"name":"norm"}
207+
2 NULL
208+
77209
# two-arg path (from_metadata_value)
78210
# test_scalar_binary_views
79211
query T

0 commit comments

Comments
 (0)