File: test_columns_unpacked.test

package info (click to toggle)
duckdb 1.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 299,196 kB
  • sloc: cpp: 865,414; ansic: 57,292; python: 18,871; sql: 12,663; lisp: 11,751; yacc: 7,412; lex: 1,682; sh: 747; makefile: 558
file content (287 lines) | stat: -rw-r--r-- 5,954 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# name: test/sql/parser/test_columns_unpacked.test
# group: [parser]

statement ok
PRAGMA enable_verification

query I
select COALESCE(*COLUMNS(*)) from (select NULL, 2, 3) t(a, b, c);
----
2

query I
select column_name from (describe select COALESCE(*COLUMNS(*)) from (select NULL, 2, 3) t(a, b, c));
----
COALESCE(t.a, t.b, t.c)

# Aliasing the result
query I
select column_name from (describe select COALESCE(*COLUMNS(*)) as a from (select NULL, 2, 3) t(a, b, c));
----
a

statement ok
create table contains_test as select '123abc234' a, 4 b, 'abc' c;

# Feed columns a and c into contains, equivalent to contains(a, c)
query I
select contains(*COLUMNS('[a|c]')) from contains_test;
----
true

# COLUMNS and *COLUMNS are equivalent as a root expression
query II
select COLUMNS('[a|c]') from contains_test;
----
123abc234	abc

statement error
select *COLUMNS('[a|c]') from contains_test;
----
*COLUMNS not allowed at the root level, use COLUMNS instead

statement ok
create table sales as from ( values
	(150, '2017/06/12'::DATE, 3),
	(125, '2017/08/29'::DATE, 2),
	(175, '2017/06/12'::DATE, 4),
) t(amount, date, priority)

query I
SELECT first(amount ORDER BY date ASC, priority DESC) FROM sales;
----
175

# TODO: this could be supported
statement error
select first(amount ORDER BY *COLUMNS('date|priority') ASC) from sales;
----
*COLUMNS(...) is not supported in the order expression

# Using a lambda in *COLUMNS
query I
select COALESCE(*COLUMNS(lambda c: c in ('a', 'c'))) from (select NULL, 2, 3) t(a, b, c);
----
3

# IN (...)
query I
select 2 in (*COLUMNS(*)) from (select 1, 2, 3) t(a, b, c);
----
true

# IN in WHERE clause
query III
from (VALUES (1, 2, 3), (2, 3, 0), (0, 0, 1)) tbl(a, b, c) where 1 IN (*COLUMNS(*));
----
1	2	3
0	0	1

statement ok
create table data AS (
	SELECT * FROM (VALUES
	(1, 'Alice'),
	(2, 'Bob'),
	(3, 'Alice'),
	(4, 'Carol')
	) AS t(id, name)
)

# Using struct pack:
query I
select struct_pack(*COLUMNS(*)) from data
----
{'id': 1, 'name': Alice}
{'id': 2, 'name': Bob}
{'id': 3, 'name': Alice}
{'id': 4, 'name': Carol}

# It can not be used inside a COLUMNS expression
statement error
SELECT COLUMNS(*COLUMNS(*)) FROM (VALUES ('test'))
----
COLUMNS expression is not allowed inside another COLUMNS expression

statement error
SELECT *COLUMNS(COLUMNS(*)) FROM (VALUES ('test'))
----
COLUMNS expression is not allowed inside another COLUMNS expression

# COLUMNS and *COLUMNS of different expressions
query III
select COLUMNS(*), struct_pack(COLUMNS(['id'])) from data
----
1	Alice	{'id': 1}
2	Bob	{'id': 2}
3	Alice	{'id': 3}
4	Carol	{'id': 4}

# (temporary limitation)
# *COLUMNS with different expressions can not appear in the same expression
statement error
select struct_pack(struct_pack(*COLUMNS(['id']), struct_pack(*COLUMNS(['name'])))) from data
----
Multiple different STAR/COLUMNS in the same expression are not supported

# When they share the same expression, this is allowed:
query I
select struct_pack(
	b := struct_pack(*COLUMNS(['id'])),
	a := struct_pack(*COLUMNS(['id']))
) from data
----
{'b': {'id': 1}, 'a': {'id': 1}}
{'b': {'id': 2}, 'a': {'id': 2}}
{'b': {'id': 3}, 'a': {'id': 3}}
{'b': {'id': 4}, 'a': {'id': 4}}

# Multiple *COLUMNS in the same statement are allowed
# Whether they share the same expression or not:
query II
select struct_pack(*COLUMNS('id')) a, struct_pack(*COLUMNS('name')) from data
----
{'id': 1}	{'name': Alice}
{'id': 2}	{'name': Bob}
{'id': 3}	{'name': Alice}
{'id': 4}	{'name': Carol}

# Varargs function
query I
select CONCAT(*COLUMNS(*), *COLUMNS(*)) from data
----
1Alice1Alice
2Bob2Bob
3Alice3Alice
4Carol4Carol

# *COLUMNS inside COLUMNS lambda
statement error
select COLUMNS(lambda col: *COLUMNS('id')) from data
----
COLUMNS expression is not allowed inside another COLUMNS expression

statement error
select *COLUMNS(lambda col: *COLUMNS(*)) from data
----
COLUMNS expression is not allowed inside another COLUMNS expression

# *COLUMNS expanding to more than one column in binary op
statement error
with integers as (
	SELECT * FROM (VALUES
		(42, 31),
		(85, 76),
	) as t(a, b)
)
select *COLUMNS(*) + 42 from integers
----
No function matches the given name and argument types '+(INTEGER, INTEGER, INTEGER_LITERAL)'.

query I
with integers as (
	SELECT * FROM (VALUES
		(42, 31),
		(85, 76),
	) as t(a, b)
)
select *COLUMNS('a') + 42 from integers
----
84
127

# Expands to the expression: [a + a, a + b]
query I
with integers as (
	select * FROM (VALUES
		(21, 42),
		(1337, 7331)
	) as t(a, b)
)
select [(UNPACK(a + COLUMNS(['a', 'b'])))] from integers
----
[42, 63]
[2674, 8668]


# UNPACK operator nested inside another UNPACK operator
statement error
select
	[
		UNPACK([
			UNPACK(COLUMNS(*)),
			a + b
		])
	]
from (
	select 42 a, 21 b
)
----
UNPACK can only be used in combination with a STAR (*) expression or COLUMNS expression

# Cast every column to VARCHAR before unpacking into the list
query I
select
	[UNPACK(COLUMNS(*)::VARCHAR)]
from (
	select
		21::INTEGER a,
		True::BOOL b,
		0.1234::DOUBLE c
)
----
[21, true, 0.1234]

# Two different COLUMNS inside an UNPACK (could be supported in the future)
statement error
select
	[
		UNPACK(
			[
				COLUMNS(['a', 'b']),
				COLUMNS(['c'])
			]
		)
	]
from (select 21 a, 42 b, 1337 c)
----
Multiple different STAR/COLUMNS in the same expression are not supported

# An UNPACK without a COLUMNS inside of it
statement error
select
	[
		UNPACK(
			[
				'test'
			]
		)
	]
from (select 21 a, 42 b, 1337 c)
----
UNPACK can only be used in combination with a STAR (*) expression or COLUMNS expression

statement error
select
	[
		UNPACK(
			[
				COLUMNS([]::VARCHAR[])
			]
		)
	]
from (select 21 a, 42 b, 1337 c)
----
Star expression "COLUMNS(CAST(main.list_value() AS VARCHAR[]))" resulted in an empty set of columns

statement error
select
	[
		UNPACK(
			[
				COLUMNS(['d'])
			]
		)
	]
from (select 21 a, 42 b, 1337 c)
----
Column "d" was selected but was not found in the FROM clause