-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
subquery
505 lines (414 loc) · 11 KB
/
subquery
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# LogicTest: default parallel-stmts distsql
# Tests for subqueries (SELECT statements which are part of a bigger statement).
query I
SELECT (SELECT 1)
----
1
query B
SELECT 1 IN (SELECT 1)
----
true
query B
SELECT 1 IN ((((SELECT 1))))
----
true
query I
SELECT ARRAY(((((VALUES (1), (2))))))[2]
----
2
query I
SELECT 1 + (SELECT 1)
----
2
query error unsupported binary operator: <int> \+ <tuple{int, int}>
SELECT 1 + (SELECT 1, 2)
query B
SELECT (1, 2, 3) IN (SELECT 1, 2, 3)
----
true
query B
SELECT (1, 2, 3) = (SELECT 1, 2, 3)
----
true
query B
SELECT (1, 2, 3) != (SELECT 1, 2, 3)
----
false
query B
SELECT (SELECT 1, 2, 3) = (SELECT 1, 2, 3)
----
true
query B
SELECT (SELECT 1) IN (SELECT 1)
----
true
query B
SELECT (SELECT 1) IN (1)
----
true
# NB: Cockroach has different behavior from Postgres on a few esoteric
# subqueries. The Cockroach behavior seems more sensical and
# supporting the specific Postgres behavior appears onerous. Fingers
# crossed this doesn't bite us down the road.
# Postgres cannot handle this query (but MySQL can), even though it
# seems sensical:
# ERROR: subquery must return only one column
# LINE 1: select (select 1, 2) IN (select 1, 2);
# ^
query B
SELECT (SELECT 1, 2) IN (SELECT 1, 2)
----
true
# Postgres cannot handle this query, even though it seems sensical:
# ERROR: subquery must return only one column
# LINE 1: select (select 1, 2) IN ((1, 2));
# ^
query B
SELECT (SELECT 1, 2) IN ((1, 2))
----
true
# Postgres cannot handle this query, even though it seems sensical:
# ERROR: subquery has too many columns
# LINE 1: select (select (1, 2)) IN (select 1, 2);
# ^
query B
SELECT (SELECT (1, 2)) IN (SELECT 1, 2)
----
true
query B
SELECT (SELECT (1, 2)) IN ((1, 2))
----
true
# Postgres cannot handle this query, even though it seems sensical:
# ERROR: subquery must return only one column
# LINE 1: select (select 1, 2) in (select (1, 2));
# ^
query B
SELECT (SELECT 1, 2) IN (SELECT (1, 2))
----
true
query B
SELECT (SELECT (1, 2)) IN (SELECT (1, 2))
----
true
query B
SELECT 1 = ANY(SELECT 1)
----
true
query B
SELECT (1, 2) = ANY(SELECT 1, 2)
----
true
query B
SELECT 1 = SOME(SELECT 1)
----
true
query B
SELECT (1, 2) = SOME(SELECT 1, 2)
----
true
query B
SELECT 1 = ALL(SELECT 1)
----
true
query B
SELECT (1, 2) = ALL(SELECT 1, 2)
----
true
query error subquery must return only one column, found 2
SELECT (SELECT 1, 2)
query error unsupported comparison operator: <int> IN <tuple{tuple{int, int}}>
SELECT 1 IN (SELECT 1, 2)
query error unsupported comparison operator: <tuple{int, int}> IN <tuple{int}>
SELECT (1, 2) IN (SELECT 1)
statement ok
CREATE TABLE abc (a INT PRIMARY KEY, b INT, c INT)
statement ok
INSERT INTO abc VALUES (1, 2, 3), (4, 5, 6)
query TTT
EXPLAIN ALTER TABLE abc SPLIT AT VALUES ((SELECT 42))
----
split · ·
└── values · ·
│ size 1 column, 1 row
│ subqueries 1
└── limit · ·
└── render · ·
└── emptyrow · ·
statement ok
ALTER TABLE abc SPLIT AT VALUES ((SELECT 1))
query error unsupported comparison operator: <tuple{int, int}> IN <tuple{tuple{int, int, int}}>
SELECT (1, 2) IN (SELECT * FROM abc)
query B
SELECT (1, 2) IN (SELECT a, b FROM abc)
----
true
query B
SELECT (1, 2) IN (SELECT a, b FROM abc WHERE false)
----
false
query error subquery must return only one column
SELECT (SELECT * FROM abc)
query error more than one row returned by a subquery used as an expression
SELECT (SELECT a FROM abc)
query TTT
EXPLAIN SELECT EXISTS (SELECT a FROM abc)
----
render · ·
│ subqueries 1
├── limit · ·
│ └── render · ·
│ └── scan · ·
│ table abc@primary
│ spans ALL
└── emptyrow · ·
query I
SELECT (SELECT a FROM abc WHERE false)
----
NULL
query II
VALUES (1, (SELECT (2)))
----
1 2
statement ok
INSERT INTO abc VALUES ((SELECT 7), (SELECT 8), (SELECT 9))
query III
SELECT * FROM abc WHERE a = 7
----
7 8 9
statement error value type tuple{int, int, int} doesn't match type INT of column "a"
INSERT INTO abc VALUES ((SELECT (10, 11, 12)))
statement error subquery must return only one column, found 3
INSERT INTO abc VALUES ((SELECT 10, 11, 12))
statement ok
CREATE TABLE xyz (x INT PRIMARY KEY, y INT, z INT)
statement ok
INSERT INTO xyz SELECT * FROM abc
query III rowsort
SELECT * FROM xyz
----
1 2 3
4 5 6
7 8 9
statement ok
INSERT INTO xyz (x, y, z) VALUES (10, 11, 12)
statement ok
UPDATE xyz SET z = (SELECT 10) WHERE x = 7
query III rowsort
SELECT * FROM xyz
----
1 2 3
4 5 6
7 8 10
10 11 12
statement error value type tuple{int, int} doesn't match type INT of column "z"
UPDATE xyz SET z = (SELECT (10, 11)) WHERE x = 7
statement error subquery must return 2 columns, found 1
UPDATE xyz SET (y, z) = (SELECT (11, 12)) WHERE x = 7
#regression, see #6852
#statement ok
#UPDATE xyz SET (y, z) = (SELECT 11, 12) WHERE x = 7
#
#query III rowsort
#SELECT * FROM xyz
#----
#1 2 3
#4 5 6
#7 11 12
#10 11 12
query B
SELECT 1 IN (SELECT x FROM xyz ORDER BY x DESC)
----
true
query III
SELECT * FROM xyz WHERE x = (SELECT MIN(x) FROM xyz)
----
1 2 3
query III
SELECT * FROM xyz WHERE x = (SELECT MAX(x) FROM xyz)
----
10 11 12
statement ok
CREATE TABLE kv (k INT PRIMARY KEY, v STRING)
statement ok
INSERT INTO kv VALUES (1, 'one')
query IT
SELECT * FROM kv WHERE k = (SELECT k FROM kv WHERE (k, v) = (1, 'one'))
----
1 one
query B
SELECT EXISTS(SELECT 1 FROM kv AS x WHERE x.k = 1)
----
true
query B
SELECT EXISTS(SELECT 1 FROM kv WHERE k = 2)
----
false
# Tests for subquery in the FROM part of a SELECT
query II colnames,rowsort
SELECT * FROM (VALUES (1, 2)) AS foo
----
column1 column2
1 2
query II colnames,rowsort
SELECT * FROM (VALUES (1, 2))
----
column1 column2
1 2
query IT colnames,rowsort
SELECT * FROM (VALUES (1, 'one'), (2, 'two'), (3, 'three')) AS foo
----
column1 column2
1 one
2 two
3 three
query III colnames,rowsort
SELECT * FROM (VALUES (1, 2, 3), (4, 5, 6)) AS foo
----
column1 column2 column3
1 2 3
4 5 6
query III colnames,rowsort
SELECT * FROM (VALUES (1, 2, 3), (4, 5, 6)) AS foo (foo1, foo2, foo3)
----
foo1 foo2 foo3
1 2 3
4 5 6
query III colnames,rowsort
SELECT * FROM (VALUES (1, 2, 3), (4, 5, 6)) AS foo (foo1, foo2)
----
foo1 foo2 column3
1 2 3
4 5 6
query III colnames,rowsort
SELECT * FROM (SELECT * FROM xyz) AS foo WHERE x < 7
----
x y z
1 2 3
4 5 6
query III colnames,rowsort
SELECT * FROM (SELECT * FROM xyz) AS foo (foo1) WHERE foo1 < 7
----
foo1 y z
1 2 3
4 5 6
query III colnames,rowsort
SELECT * FROM (SELECT * FROM xyz AS moo (moo1, moo2, moo3)) as foo (foo1) WHERE foo1 < 7
----
foo1 moo2 moo3
1 2 3
4 5 6
query III colnames,rowsort
SELECT * FROM (SELECT * FROM xyz AS moo (moo1, moo2, moo3) ORDER BY moo1) as foo (foo1) WHERE foo1 < 7
----
foo1 moo2 moo3
1 2 3
4 5 6
query III colnames
SELECT * FROM (SELECT * FROM xyz AS moo (moo1, moo2, moo3) ORDER BY moo1) as foo (foo1) WHERE foo1 < 7 ORDER BY moo2 DESC
----
foo1 moo2 moo3
4 5 6
1 2 3
query III colnames
SELECT * FROM (SELECT * FROM (VALUES (1, 2, 3), (4, 5, 6)) AS moo (moo1, moo2, moo3) WHERE moo1 = 4) as foo (foo1)
----
foo1 moo2 moo3
4 5 6
query III colnames
SELECT * FROM (SELECT * FROM (VALUES (1, 8, 8), (3, 1, 1), (2, 4, 4)) AS moo (moo1, moo2, moo3) ORDER BY moo2) as foo (foo1) ORDER BY foo1
----
foo1 moo2 moo3
1 8 8
2 4 4
3 1 1
query TTT
EXPLAIN SELECT * FROM (SELECT * FROM (VALUES (1, 8, 8), (3, 1, 1), (2, 4, 4)) AS moo (moo1, moo2, moo3) ORDER BY moo2) as foo (foo1) ORDER BY foo1
----
sort · ·
│ order +foo1
└── values · ·
· size 3 columns, 3 rows
query II colnames
SELECT a, b FROM (VALUES (1, 2, 3), (3, 4, 7), (5, 6, 10)) AS foo (a, b, c) WHERE a + b = c
----
a b
1 2
3 4
query I colnames
SELECT foo.a FROM (VALUES (1), (2), (3)) AS foo (a)
----
a
1
2
3
query IITT colnames
SELECT foo.a, a, column2, foo.column2 FROM (VALUES (1, 'one'), (2, 'two'), (3, 'three')) AS foo (a)
----
a a column2 column2
1 1 one one
2 2 two two
3 3 three three
query I
SELECT x FROM xyz WHERE x IN (SELECT x FROM xyz WHERE x = 7)
----
7
query I
SELECT x FROM xyz WHERE x = 7 LIMIT (SELECT x FROM xyz WHERE x = 1)
----
7
query I
SELECT x FROM xyz ORDER BY x OFFSET (SELECT x FROM xyz WHERE x = 1)
----
4
7
10
query B
INSERT INTO xyz (x, y, z) VALUES (13, 11, 12) RETURNING (y IN (SELECT y FROM xyz))
----
true
# check that residual filters are not expanded twice
query TITTTTT
EXPLAIN (METADATA) SELECT x FROM xyz WHERE x IN (SELECT x FROM xyz)
----
render 0 render · · (x) x!=NULL; key(x)
└── scan 1 scan · · (x) x!=NULL; key(x)
│ 1 · table xyz@primary · ·
│ 1 · spans ALL · ·
│ 1 · subqueries 1 · ·
└── render 2 render · · (x) x!=NULL; key(x)
└── scan 3 scan · · (x) x!=NULL; key(x)
· 3 · table xyz@primary · ·
· 3 · spans ALL · ·
# This test checks that the double sub-query plan expansion caused by a
# sub-expression being shared by two or more plan nodes does not
# panic.
statement ok
CREATE TABLE tab4(col0 INTEGER, col1 FLOAT, col3 INTEGER, col4 FLOAT)
statement ok
INSERT INTO tab4 VALUES (1,1,1,1)
statement ok
CREATE INDEX idx_tab4_0 ON tab4 (col4,col0)
query I
SELECT col0 FROM tab4 WHERE (col0 <= 0 AND col4 <= 5.38) OR (col4 IN (SELECT col1 FROM tab4 WHERE col1 > 8.27)) AND (col3 <= 5 AND (col3 BETWEEN 7 AND 9))
----
query TTT
EXPLAIN SELECT col0 FROM tab4 WHERE (col0 <= 0 AND col4 <= 5.38) OR (col4 IN (SELECT col1 FROM tab4 WHERE col1 > 8.27)) AND (col3 <= 5 AND (col3 BETWEEN 7 AND 9))
----
render · ·
└── index-join · ·
├── scan · ·
│ │ table tab4@idx_tab4_0
│ │ spans /!NULL-/5.38/1
│ │ subqueries 1
│ └── render · ·
│ └── scan · ·
│ table tab4@primary
│ spans ALL
└── scan · ·
│ table tab4@primary
│ subqueries 1
└── render · ·
└── scan · ·
· table tab4@primary
· spans ALL