File: query_function.test

package info (click to toggle)
duckdb 1.5.1-3
  • 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: 564
file content (304 lines) | stat: -rw-r--r-- 5,927 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# name: test/sql/catalog/function/query_function.test
# description: test query() function
# group: [function]

statement ok
PRAGMA enable_verification

query I
SELECT * FROM query('SELECT 42');
----
42

query I
FROM query('SELECT 42 AS a');
----
42

query I
FROM query('SELECT 10 + 32;');
----
42

query I
FROM query('SELECT abs(-42)');
----
42

query I
SELECT * FROM query('SELECT * FROM (SELECT 1 + 2)');
----
3

query III
FROM query('SELECT 1, 2, 3');
----
1	2	3

query I
FROM query('SELECT 42;;;--- hello;');
----
42

# query text
query I
SELECT * FROM query('SELECT ''hello''');
----
hello

# query a table
statement ok
CREATE TABLE tbl (a INT, b INT, c INT);

statement ok
FROM query('SELECT *, 1 + 2 FROM tbl');

statement ok
INSERT INTO tbl VALUES (1, 2, 3), (4, 5, 6), (7, 8, 9);

query III
SELECT * FROM query('FROM tbl');
----
1	2	3
4	5	6
7	8	9

query I
SELECT * FROM query('SELECT a + b + c FROM tbl');
----
6
15
24

# query multiple nested type tags
query II
SELECT * FROM query('WITH a(i) AS (SELECT 1) SELECT a1.i AS i1, a2.i AS i2 FROM a AS a1, a AS a2');
----
1	1

# test incorrect usage
statement error
SELECT * FROM query(NULL);
----
<REGEX>:Parser Error.*NULL.*

statement error
SELECT * FROM query(' ');
----
Parser Error: Expected a single SELECT statement

statement error
SELECT * FROM query('');
----
Parser Error: Expected a single SELECT statement

statement error
SELECT * FROM query('FROM query(FROM)');
----
Parser Error: syntax error at or near "FROM"

# multiple statements are not supported
statement error
SELECT * FROM query('SELECT 1; SELECT 2');
----
Parser Error: Expected a single SELECT statement

# invalid input
statement error
SELECT query(SELECT 1);
----
Parser Error: syntax error at or near "SELECT"

statement error
SELECT * FROM query('CREATE TABLE tbl (a INT)');
----
Parser Error: Expected a single SELECT statement

# test PIVOT statements in query() function
query I
SELECT * FROM query('SELECT * FROM (PIVOT (SELECT 1 AS col) ON col IN (1) using first(col))');
----
1

# PIVOT without explicit IN clause should give helpful error message
statement error
SELECT * FROM query('SELECT * FROM (PIVOT (SELECT 1 AS col) ON col using first(col))');
----
Parser Error: PIVOT statements without explicit IN clauses are not supported in query() function. Please specify the pivot values explicitly, e.g.: PIVOT ... ON col IN (val1, val2, ...)

# test query_table()
statement ok
CREATE TABLE tbl_int AS SELECT 42;

statement ok
CREATE TABLE tbl_varchar AS SELECT 'duckdb';

statement ok
CREATE TABLE tbl2_varchar AS SELECT '1?ch@racter$';

statement ok
CREATE TABLE tbl_empty AS SELECT '';

query I
FROM query_table('tbl_int');
----
42

query I
FROM query_table(['tbl_int']);
----
42

query III
FROM query_table(tbl);
----
1	2	3
4	5	6
7	8	9

statement ok
CREATE TABLE tbl2 (a INT, b INT, c INT);

statement ok
INSERT INTO tbl2 VALUES (9, 8, 7), (6, 5, 4), (3, 2, 1);

query III
FROM query_table([tbl, tbl2]);
----
1	2	3
4	5	6
7	8	9
9	8	7
6	5	4
3	2	1

# test incorrect usage
statement error
FROM query_table();
----
No function matches the given name and argument types 'query_table()'.

statement error
FROM query_table(NULL);
----
<REGEX>:.*Cannot use NULL.*

statement error
FROM query_table([]);
----
Binder Error: No function matches the given name and argument types 'query_table(INTEGER[])'.

statement error
FROM query_table(['']);
----
Parser Error: syntax error at end of input

statement error
FROM query_table('tbl_int', 'tbl_varchar', tbl2_varchar);
----
Binder Error: No function matches the given name and argument types 'query_table(VARCHAR, VARCHAR, VARCHAR)'.

statement error
FROM query_table([tbl_int, tbl2]);
----
Binder Error: Set operations can only apply to expressions with the same number of result columns

statement error
FROM query_table(not_defined_tbl);
----
Catalog Error: Table with name not_defined_tbl does not exist!

statement error
FROM query_table('FROM query(''select 1 + 2;'')');
----
Catalog Error: Table with name FROM query('select 1 + 2;') does not exist!

statement error
FROM query_table('FROM query("select 1 + 2;")');
----
Catalog Error: Table with name FROM query(select 1 + 2;) does not exist!

statement error
FROM query_table('(SELECT 17 + 25)');
----
Catalog Error: Table with name (SELECT 17 + 25) does not exist!

# tables with special table names
statement ok
CREATE TABLE "(SELECT 17 + 25)"(i int);

statement ok
insert into "(SELECT 17 + 25)" values (100);

query I
SELECT * FROM "(SELECT 17 + 25)";
----
100

query I 
FROM query_table("(SELECT 17 + 25)");
----
100

query I 
FROM query_table('(SELECT 17 + 25)');
----
100

statement error
FROM query_table(SELECT 17 + 25);
----
Parser Error: syntax error at or near "SELECT"

statement error 
FROM query_table("SELECT 4 + 2");
----
Catalog Error: Table with name SELECT 4 + 2 does not exist!

statement error
FROM query_table('SELECT 4 + 2');
----
Catalog Error: Table with name SELECT 4 + 2 does not exist!

query I
SELECT f.* FROM query_table('tbl_int') as f;
----
42

query I
SELECT f.x FROM query_table('tbl_int') as f(x);
----
42

# test by_name argument
query I
FROM query_table(['tbl_int', 'tbl_varchar', 'tbl_empty', 'tbl2_varchar'], false);
----
42
duckdb
(empty)
1?ch@racter$

query IIII
from query_table([tbl_int, tbl_varchar, tbl_empty, tbl2_varchar], true);
----
42	NULL	NULL	NULL
NULL	duckdb	NULL	NULL
NULL	NULL	(empty)	NULL
NULL	NULL	NULL	1?ch@racter$

# test incorrect usage
statement error
FROM query_table(true);
----
Binder Error: No function matches the given name and argument types 'query_table(BOOLEAN)'.

statement error
FROM query_table(tbl2, true);
----
Binder Error: No function matches the given name and argument types 'query_table(VARCHAR, BOOLEAN)'.

statement error
FROM query_table(['tbl_int', 'tbl_varchar', 'tbl_empty', '(select ''I am a subquery'')'], false);
----
Catalog Error: Table with name (select 'I am a subquery') does not exist!