forked from MaterializeInc/materialize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcte.slt
244 lines (213 loc) · 4.64 KB
/
cte.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
# 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.
# Additional tests in test/sqllogictest/cockroach/with.slt
mode cockroach
statement ok
CREATE TABLE x (a int)
statement ok
INSERT INTO x VALUES (1), (2), (3)
statement ok
CREATE TABLE y (a int)
statement ok
INSERT INTO y VALUES (2), (3), (4)
# Check that a CTE on the lhs of a join works
query I
WITH t AS (SELECT * FROM y WHERE a < 3)
SELECT * FROM t NATURAL JOIN x
----
2
# Using a CTE inside a correlated subquery
query I
WITH t(x) AS (SELECT * FROM y WHERE a < 3)
SELECT * FROM x WHERE a IN (
SELECT a FROM t WHERE x.a = t.x
)
----
2
# Using a correlated subquery inside a CTE
query I
SELECT * FROM x WHERE a IN
(WITH t AS (SELECT * FROM y WHERE y.a = x.a) SELECT * FROM t);
----
2
3
statement error more than one record produced in subquery
WITH c AS (SELECT a + 1 FROM x) SELECT (SELECT * FROM c);
# Allow re-using names laterally.
query I rowsort
SELECT * FROM (
(WITH c AS (SELECT 1) SELECT * FROM c)
UNION ALL
(WITH c AS (SELECT 2) SELECT * FROM c)
)
----
1
2
statement error specified more than once
SELECT * FROM (
(WITH c AS (SELECT 1), c AS (SELECT 2)
SELECT * FROM c UNION ALL SELECT * FROM c)
)
# Allow re-using names nested.
query I rowsort
SELECT * FROM (
(WITH c AS (SELECT 1) SELECT * FROM
(WITH c AS (SELECT 2) SELECT * FROM c)
UNION ALL
SELECT * FROM c
)
)
----
1
2
# CTE names should only be accessible in their scope.
statement error unknown catalog item
SELECT * FROM (
(WITH c AS (SELECT 1) SELECT * FROM c)
UNION ALL
SELECT * FROM c
)
query I
WITH foo AS (SELECT 1)
(SELECT * FROM foo
UNION ALL
(WITH foo AS (SELECT 2) SELECT * FROM foo)
UNION ALL
(SELECT * FROM foo))
----
1
1
2
# See 5766.
query error column "a2.f1" does not exist
SELECT * FROM (VALUES (true)) a2 (f1) WHERE (
SELECT TRUE FROM (VALUES (true)) AS a2 (f2)
WHERE (SELECT a2.f1)
)
statement ok
CREATE TABLE squares (x int, y int);
statement ok
CREATE TABLE roots (x int, y int);
statement ok
CREATE TABLE cubes (x int, y int);
statement ok
INSERT INTO squares VALUES
(1, 1), (2, 4), (3, 9), (4, 16);
statement ok
INSERT INTO roots VALUES
(1, 1), (4, 2), (9, 3), (16, 4);
statement ok
INSERT INTO cubes VALUES
(1, 1), (2, 8), (3, 27), (4, 16);
# Correlated expression––this should only return values where squares.y is
# in roots.y and sqaures.x
query II
SELECT * FROM squares
WHERE x IN (
SELECT y FROM roots
WHERE y IN (
SELECT squares.y
)
);
----
1 1
# Correlated CTE
query II
SELECT * FROM squares
WHERE x IN (
WITH squares_y AS (
SELECT squares.y
)
SELECT y FROM roots
WHERE y IN (
SELECT y FROM squares_y
)
);
----
1 1
# Same query, but inside a view. Regression test for #5092.
statement ok
CREATE MATERIALIZED VIEW v AS
SELECT * FROM squares
WHERE x IN (
WITH squares_y AS (
SELECT squares.y
)
SELECT y FROM roots
WHERE y IN (
SELECT y FROM squares_y
)
);
query II
SELECT * FROM v
----
1 1
# Correlated CTE in different level than it was introduced. This is needlessly
# convoluted but caused crashes in early iterations of CTE's development.
query II
SELECT * FROM squares
WHERE x IN (
WITH squares_x AS (
SELECT squares.x
)
SELECT t0.x
FROM (
SELECT roots.x
FROM roots
JOIN (
SELECT t2.x FROM (
SELECT cubes.x FROM cubes
JOIN squares_x
ON squares_x.x = cubes.x
WHERE cubes.x IN (SELECT x FROM squares_x)
) t2
) AS t1
ON t1.x = roots.x
) AS t0
);
----
1 1
4 16
# Use same query at two levels. Obtusely check for quadratic powers.
query I rowsort
WITH squares_y AS (
SELECT y FROM squares
)
SELECT * FROM squares_y WHERE y IN (
SELECT y * y FROM squares_y
)
----
1
16
# PostgreSQL tests
query TT
WITH q AS (SELECT 'foo' AS x)
SELECT x, pg_typeof(x) FROM q;
----
foo text
query I rowsort
WITH outermost(x) AS (
SELECT 1
UNION (WITH innermost as (SELECT 2)
SELECT * FROM innermost
UNION SELECT 3)
)
SELECT * FROM outermost ORDER BY 1;
----
1
2
3
query error unknown catalog item 'outermost'
WITH outermost(x) AS (
SELECT 1
UNION (WITH innermost as (SELECT 2)
SELECT * FROM outermost -- fail
UNION SELECT * FROM innermost)
)
SELECT * FROM outermost ORDER BY 1;