forked from MaterializeInc/materialize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subquery.slt
756 lines (613 loc) · 14.3 KB
/
subquery.slt
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
# Copyright Materialize, Inc. and contributors. All rights reserved.
#
# Use of this software is governed by the Business Source License
# included in the LICENSE file at the root of this repository.
#
# As of the Change Date specified in that file, in accordance with
# the Business Source License, use of this software will be governed
# by the Apache License, Version 2.0.
mode cockroach
statement ok
CREATE TABLE nullary ()
statement ok
CREATE TABLE peeps (
peep text
)
statement ok
INSERT INTO peeps VALUES ('alice'), ('bob'), ('eve')
statement ok
CREATE TABLE likes (
liker text,
likee text
)
statement ok
INSERT INTO likes VALUES ('alice', 'bob'), ('bob', 'eve'), ('alice', 'eve')
query TB rowsort
SELECT peep, EXISTS(
SELECT * FROM likes WHERE peep = liker
) FROM peeps
----
alice true
bob true
eve false
query TTB rowsort
SELECT *, EXISTS(
SELECT * FROM likes as likes2 WHERE likes.likee = likes2.liker
) FROM likes
----
alice bob true
alice eve false
bob eve false
# inner scope shadows outer scope, so `likee` without qualification refers to `likes2.likee`
query TTB rowsort
SELECT *, EXISTS(
SELECT * FROM likes as likes2 WHERE likee = likes2.liker
) FROM likes
----
alice bob false
alice eve false
bob eve false
# similarly, without `as likes2`, `likes.liker` refers to the inner scope
query TTB rowsort
SELECT *, EXISTS(
SELECT * FROM likes WHERE likee = likes.liker
) FROM likes
----
alice bob false
alice eve false
bob eve false
statement error more than one record produced in subquery
SELECT peep, (
SELECT likee FROM likes WHERE liker = peep
) FROM peeps
statement ok
CREATE TABLE favorites (peep text, other text)
statement ok
INSERT INTO favorites VALUES ('alice', 'eve'), ('alice', 'alice'), ('bob', 'bob')
query TTB rowsort
SELECT
liker,
likee,
likee IN (SELECT other FROM favorites WHERE peep = liker) AS is_favorite
FROM likes
----
alice bob false
bob eve false
alice eve true
query TTB rowsort
SELECT
liker,
likee,
likee NOT IN (SELECT other FROM favorites WHERE peep = liker) AS is_favorite
FROM likes
----
alice bob true
bob eve true
alice eve false
statement ok
CREATE TABLE age (peep text, age int)
statement ok
INSERT INTO age VALUES ('alice', 103), ('bob', 100), ('eve', 104)
statement error more than one record produced in subquery
SELECT peeps.peep, (
SELECT age FROM likes, age WHERE peeps.peep = liker AND likee = age.peep
) FROM peeps
query TB rowsort
SELECT peeps.peep, age < ANY (
SELECT age FROM likes, age WHERE peeps.peep = liker AND likee = age.peep
) FROM peeps, age
WHERE peeps.peep = age.peep
----
alice true
bob true
eve false
query TB rowsort
SELECT peeps.peep, age < ALL (
SELECT age FROM likes, age WHERE peeps.peep = liker AND likee = age.peep
) FROM peeps, age
WHERE peeps.peep = age.peep
----
alice false
bob true
eve true
# ANY/ALL semantics
query BBBBBBBBBBBBBBB
(VALUES (
1 < ANY(SELECT * FROM (VALUES (1)) WHERE false),
1 < ANY(VALUES (0)),
1 < ANY(VALUES (1)),
1 < ANY(VALUES (2)),
1 < ANY(VALUES (0), (NULL)),
1 < ANY(VALUES (1), (NULL)),
1 < ANY(VALUES (2), (NULL)),
NULL < ANY(SELECT * FROM (VALUES (1)) WHERE false),
NULL < ANY(VALUES (0)),
NULL < ANY(VALUES (1)),
NULL < ANY(VALUES (2)),
NULL < ANY(VALUES (NULL)),
NULL < ANY(VALUES (0), (NULL)),
NULL < ANY(VALUES (1), (NULL)),
NULL < ANY(VALUES (2), (NULL))
))
----
false false false true NULL NULL true false NULL NULL NULL NULL NULL NULL NULL
query BBBBBBBBBBBBBBB
(VALUES (
1 < ALL(SELECT * FROM (VALUES (1)) WHERE false),
1 < ALL(VALUES (0)),
1 < ALL(VALUES (1)),
1 < ALL(VALUES (2)),
1 < ALL(VALUES (0), (NULL)),
1 < ALL(VALUES (1), (NULL)),
1 < ALL(VALUES (2), (NULL)),
NULL < ALL(SELECT * FROM (VALUES (1)) WHERE false),
NULL < ALL(VALUES (0)),
NULL < ALL(VALUES (1)),
NULL < ALL(VALUES (2)),
NULL < ALL(VALUES (NULL)),
NULL < ALL(VALUES (0), (NULL)),
NULL < ALL(VALUES (1), (NULL)),
NULL < ALL(VALUES (2), (NULL))
))
----
true false false true false false NULL true NULL NULL NULL NULL NULL NULL NULL
query error subquery has 0 columns available but 1 columns specified
SELECT 1 < ALL(SELECT * FROM nullary)
query error Expected subselect to return 1 column, got 0 columns
SELECT (SELECT);
----
query
SELECT * FROM (SELECT);
----
query error subquery has 2 columns available but 1 columns specified
SELECT 1 < ALL(SELECT 1, 2)
statement ok
CREATE TABLE s1 (a int NOT NULL)
statement ok
CREATE TABLE s2 (a int NOT NULL)
statement ok
CREATE TABLE s3 (b int NOT NULL)
statement ok
INSERT INTO s1 VALUES (0)
statement ok
INSERT INTO s2 VALUES (1)
statement ok
INSERT INTO s3 VALUES (1)
# Verify that scope resolution prefers the closer scope when a name occurs in
# multiple outer levels.
query B
SELECT true FROM s1 WHERE EXISTS (SELECT true FROM s2 WHERE EXISTS (SELECT true FROM s3 WHERE a = s3.b))
----
true
statement ok
CREATE TABLE t1 (a int NOT NULL)
statement ok
CREATE TABLE t2 (b int NOT NULL)
statement ok
CREATE TABLE t3 (a int NOT NULL, b int NOT NULL)
statement ok
INSERT INTO t1 VALUES (1), (2), (3)
statement ok
INSERT INTO t2 VALUES (1), (2), (3)
statement ok
INSERT INTO t3 VALUES (2, 3), (1, 4)
# Test a constraint that spans multiple levels of nesting.
query I
SELECT a FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE EXISTS (SELECT 1 FROM t3 WHERE t1.a = t3.a AND t2.b = t3.b))
----
2
mode standard
# Verify that the plans for some simple non-correlated subqueries are sane.
query T multiline
EXPLAIN PLAN FOR SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t2)
----
Source materialize.public.t1 (u9):
| Project (#0)
Source materialize.public.t2 (u10):
| Project ()
Query:
%0 =
| Get materialize.public.t1 (u9)
%1 =
| Get materialize.public.t2 (u10)
| Project ()
| Distinct group=()
| ArrangeBy ()
%2 =
| Join %0 %1
| | implementation = Differential %0 %1.()
EOF
query T multiline
EXPLAIN PLAN FOR SELECT * FROM t1, t3 WHERE t1.a = t3.a AND EXISTS (SELECT * FROM t2)
----
Source materialize.public.t1 (u9):
| Project (#0)
Source materialize.public.t2 (u10):
| Project ()
Source materialize.public.t3 (u11):
| Project (#0, #1)
Query:
%0 =
| Get materialize.public.t1 (u9)
| ArrangeBy (#0)
%1 =
| Get materialize.public.t3 (u11)
%2 =
| Get materialize.public.t2 (u10)
| Project ()
| Distinct group=()
| ArrangeBy ()
%3 =
| Join %0 %1 %2 (= #0 #1)
| | implementation = Differential %1 %2.() %0.(#0)
| Project (#0, #0, #2)
EOF
query T multiline
EXPLAIN PLAN FOR SELECT * FROM t1, t3 WHERE t1.a = t3.a AND EXISTS (SELECT * FROM t2 WHERE t3.b = t2.b)
----
Source materialize.public.t1 (u9):
| Project (#0)
Source materialize.public.t2 (u10):
| Project (#0)
Source materialize.public.t3 (u11):
| Project (#0, #1)
Query:
%0 =
| Get materialize.public.t1 (u9)
| ArrangeBy (#0)
%1 =
| Get materialize.public.t3 (u11)
%2 =
| Get materialize.public.t2 (u10)
| Distinct group=(#0)
| ArrangeBy (#0)
%3 =
| Join %0 %1 %2 (= #0 #1) (= #2 #3)
| | implementation = Differential %1 %2.(#0) %0.(#0)
| Project (#0, #0, #2)
EOF
# Regression test for materialize#1158
# The following subquery currently generates a plan with a map with
# 4 scalars that refer to other scalars in the map. If query planning optimizes away
# this particular case, replace with another query that generates such a plan
query T multiline
EXPLAIN PLAN FOR
SELECT age, ascii_num * 2 as result FROM (
SELECT age, ascii(letter) AS ascii_num FROM (
SELECT age, substr(replaced, 2, 1) AS letter FROM (
SELECT age, replace(likee, 'o', 'i') AS replaced FROM (
SELECT likee, age FROM likes, age WHERE liker=peep
)
)
)
)
----
Source materialize.public.likes (u3):
| Filter !(isnull(#0))
| Project (#0, #1)
Source materialize.public.age (u5):
| Filter !(isnull(#0))
| Project (#0, #1)
Query:
%0 =
| Get materialize.public.likes (u3)
| Filter !(isnull(#0))
| ArrangeBy (#0)
%1 =
| Get materialize.public.age (u5)
| Filter !(isnull(#0))
%2 =
| Join %0 %1 (= #0 #2)
| | implementation = Differential %1 %0.(#0)
| Map (ascii(substr(replace(#1, "o", "i"), 2, 1)) * 2)
| Project (#3, #4)
EOF
mode cockroach
query II rowsort
SELECT age, ascii_num * 2 as result FROM (
SELECT age, ascii(letter) AS ascii_num FROM (
SELECT age, substr(replaced, 2, 1) AS letter FROM (
SELECT age, replace(likee, 'o', 'i') AS replaced FROM (
SELECT likee, age FROM likes, age WHERE liker=peep
)
)
)
)
----
100 236
103 210
103 236
# regression test for https://github.com/MaterializeInc/materialize/issues/1708
statement ok
CREATE TABLE tab0(pk INTEGER PRIMARY KEY, col0 INTEGER, col1 FLOAT, col3 INTEGER)
statement ok
INSERT INTO tab0 VALUES(0,1,2,63)
statement ok
INSERT INTO tab0 VALUES(87,63,3,4)
query I rowsort
SELECT pk FROM tab0 WHERE
(
col3 IN (
SELECT col0 FROM tab0
)
)
----
0
query I rowsort
SELECT pk FROM tab0 WHERE
(
(
col0 IN (
SELECT col3 FROM tab0 WHERE col3 IN (
SELECT col0 FROM tab0
)
)
)
)
----
87
query I rowsort
SELECT pk FROM tab0 WHERE
(
col3 IN (
SELECT col0 FROM tab0
)
OR
(
col0 IN (
SELECT col3 FROM tab0 WHERE col3 IN (
SELECT col0 FROM tab0
)
)
)
)
----
0
87
query I
SELECT col FROM (SELECT 1 AS col) t WHERE col = ANY(VALUES (1))
----
1
query I
SELECT col FROM (SELECT 1 AS col) t WHERE col IN(VALUES(1))
----
1
statement ok
CREATE TABLE x (a int not null)
statement ok
CREATE TABLE y (b int not null)
statement ok
INSERT INTO x VALUES (1), (2), (3);
statement ok
INSERT INTO y VALUES (2), (3), (4);
query B
SELECT b != ALL(SELECT a FROM x) FROM y
----
false
false
true
query T multiline
EXPLAIN PLAN FOR SELECT b IN (SELECT a FROM x) FROM y
----
Source materialize.public.x (u13):
| Project (#0)
Source materialize.public.y (u14):
| Project (#0)
Query:
%0 = Let l0 =
| Get materialize.public.y (u14)
| Distinct group=(#0)
%1 =
| Get %0 (l0)
| ArrangeBy (#0)
%2 =
| Get materialize.public.x (u13)
| Distinct group=(#0)
| ArrangeBy (#0)
%3 = Let l1 =
| Join %1 %2 (= #0 #1)
| | implementation = DeltaQuery
| | delta %1 %2.(#0)
| | delta %2 %1.(#0)
| Project (#0)
%4 =
| Get materialize.public.y (u14)
| ArrangeBy (#0)
%5 =
| Get %3 (l1)
| Map true
%6 =
| Get %3 (l1)
| Negate
%7 =
| Union %6 %0
| Map false
%8 =
| Union %5 %7
%9 =
| Join %4 %8 (= #0 #1)
| | implementation = Differential %8 %4.(#0)
| Project (#2)
EOF
query T multiline
EXPLAIN PLAN FOR SELECT b != ALL(SELECT a FROM x) FROM y
----
Source materialize.public.x (u13):
| Project (#0)
Source materialize.public.y (u14):
| Project (#0)
Query:
%0 = Let l0 =
| Get materialize.public.y (u14)
| Distinct group=(#0)
%1 =
| Get %0 (l0)
| ArrangeBy (#0)
%2 =
| Get materialize.public.x (u13)
| Distinct group=(#0)
| ArrangeBy (#0)
%3 = Let l1 =
| Join %1 %2 (= #0 #1)
| | implementation = DeltaQuery
| | delta %1 %2.(#0)
| | delta %2 %1.(#0)
| Project (#0)
%4 =
| Get materialize.public.y (u14)
| ArrangeBy (#0)
%5 =
| Get %3 (l1)
| Map true
%6 =
| Get %3 (l1)
| Negate
%7 =
| Union %6 %0
| Map false
%8 =
| Union %5 %7
%9 =
| Join %4 %8 (= #0 #1)
| | implementation = Differential %8 %4.(#0)
| Map !(#2)
| Project (#3)
EOF
# Check that we correctly invert the filter here via De Morgan.
query T multiline
EXPLAIN PLAN FOR SELECT b > ALL(SELECT a FROM x) FROM y
----
Source materialize.public.x (u13):
| Project (#0)
Source materialize.public.y (u14):
| Project (#0)
Query:
%0 = Let l0 =
| Get materialize.public.y (u14)
| Distinct group=(#0)
%1 =
| Get %0 (l0)
| ArrangeBy ()
%2 =
| Get materialize.public.x (u13)
%3 = Let l1 =
| Join %1 %2
| | implementation = Differential %2 %1.()
| Filter (#0 <= #1)
| Project (#0)
| Distinct group=(#0)
%4 =
| Get materialize.public.y (u14)
| ArrangeBy (#0)
%5 =
| Get %3 (l1)
| Map true
%6 =
| Get %3 (l1)
| Negate
%7 =
| Union %6 %0
| Map false
%8 =
| Union %5 %7
%9 =
| Join %4 %8 (= #0 #1)
| | implementation = Differential %8 %4.(#0)
| Map !(#2)
| Project (#3)
EOF
statement ok
CREATE TABLE xs (x int not null)
statement ok
CREATE TABLE ys (y int not null)
statement ok
INSERT INTO xs VALUES (1), (1), (2);
statement ok
INSERT INTO ys VALUES (0), (1), (2);
# Check that we correctly perform subqueries that do not preserve
# multiplicity of the outer relation.
query II rowsort
select x, (select count(*) from ys where y < x) from xs
----
1 1
1 1
2 2
# Tests that conditional subqueries should not error.
query RR rowsort
select y, (
case when (select count(*) from xs where x < y) != 0
then (select 1.0 / count(*) from xs where x < y)
else (select 1.0 / (count(*) - 1) from xs where x < y)
end
)
from ys
----
0 -1
1 -1
2 0.5
# Regression test for #3924, in which quantified comparision simplification was
# not correctly handling LATERAL joins.
query II
SELECT * FROM (VALUES (1)), LATERAL (SELECT * FROM (SELECT column1) WHERE true)
----
1 1
# Regression test for #3933, in which the query planner mishandled the outer
# scope of a nested LATERAL join.
query IIII
SELECT * FROM
(SELECT 1, 1 AS col2),
LATERAL (SELECT * FROM (SELECT col2) LEFT JOIN LATERAL (SELECT col2) ON true)
----
1 1 1 1
# Regression test for #4157, in which quantified expression simplification
# failed to handle map expressions which depended upon a column introduced by
# an earlier expression in the same map node.
query I
SELECT (SELECT 1 FROM ((SELECT col1) UNION (SELECT 1)))
FROM (SELECT 1 col1)
----
1
query error aggregate functions that refer exclusively to outer columns not yet supported
SELECT (SELECT count(likes.likee)) FROM likes
# Regression test for #7121, where the multiplicity of the outer query was not
# preserved when an EXISTS subquery only involves constants/mpf/flatmaps.
query I
SELECT x FROM xs WHERE EXISTS (SELECT y FROM (SELECT 1 as y) WHERE x = y)
----
1
1
# Regression test for #9921, in which the presence of a SELECT subquery inside
# of another subexpression produced a degenerate join that was not elided before
# join implementation planning.
query I
SELECT 1 FROM x RIGHT JOIN (SELECT 2 FROM y) ON NULL
WHERE a IN (9, 0)
OR 0 < ALL (
SELECT agg1 NULL
FROM (SELECT MAX(3) agg1 FROM (SELECT FROM x)
GROUP BY TRUE)
)
GROUP BY TRUE;
----
1
query T multiline
EXPLAIN SELECT 1 FROM x RIGHT JOIN (SELECT 2 FROM y) ON NULL
WHERE a IN (9, 0)
OR 0 < ALL (
SELECT agg1 NULL
FROM (SELECT MAX(3) agg1 FROM (SELECT FROM x)
GROUP BY TRUE)
)
GROUP BY TRUE;
----
Source materialize.public.x (u13):
| Project ()
Source materialize.public.y (u14):
| Project ()
Query:
%0 =
| Get materialize.public.y (u14)
| Project ()
| Distinct group=()
| Map 1
EOF