forked from MaterializeInc/materialize
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist_subquery.slt
164 lines (127 loc) · 3.74 KB
/
list_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
# 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 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 (2), (4), (5);
statement ok
SELECT (LIST[1])::text
### List subqueries ###
statement ok
SELECT LIST(SELECT x FROM xs)::text
query T
SELECT LIST(SELECT x FROM xs)::text
----
{1,1,2}
query T
SELECT LIST(SELECT x FROM xs WHERE x > 1)::text
----
{2}
query T
SELECT LIST[LIST(SELECT x FROM xs)]::text
----
{{1,1,2}}
statement ok
SELECT LIST(SELECT x FROM xs LIMIT 2)::text
query T
SELECT LIST(SELECT x FROM xs ORDER BY x DESC)::text
----
{2,1,1}
query T
SELECT LIST(SELECT x FROM xs ORDER BY x DESC LIMIT 2)::text
----
{2,1}
query T
SELECT LIST(SELECT x FROM xs ORDER BY x DESC LIMIT 1)::text
----
{2}
query T
SELECT LIST[LIST(SELECT x FROM xs), LIST(SELECT y FROM ys)]::text
----
{{1,1,2},{2,4,5}}
query T
SELECT list_cat(LIST(SELECT x FROM xs), LIST(SELECT y FROM ys))::text
----
{1,1,2,2,4,5}
query T
SELECT list_cat(LIST(SELECT x FROM xs), LIST(SELECT y FROM ys ORDER BY y DESC))::text
----
{1,1,2,5,4,2}
query T
SELECT LIST(SELECT y FROM xs JOIN ys ON xs.x = ys.y)::text
----
{2}
query T
SELECT LIST(SELECT y FROM xs JOIN ys ON 2*xs.x >= ys.y)::text
----
{2,2,2,4}
query T
SELECT LIST(SELECT DISTINCT y FROM xs JOIN ys ON 2*xs.x >= ys.y)::text
----
{2,4}
query T
SELECT LIST(SELECT 1 WHERE FALSE)::text
----
{}
statement ok
CREATE TABLE zs (z int not null)
query T
SELECT LIST(SELECT z FROM zs)::text;
----
{}
query T
SELECT LIST(SELECT AVG(0) FROM zs)::text;
----
{NULL}
statement ok
CREATE TABLE users (id int not null, other_field int not null)
statement ok
CREATE TABLE customer (id int not null, first_name string not null, last_name string not null, zip string not null)
statement ok
INSERT INTO users VALUES (1, 10), (2, 5), (3, 8);
statement ok
INSERT INTO customer VALUES (1, 'alice', 'lasta', '10003'::text), (2, 'bob', 'lastb', '10013'::text), (3, 'charlie', 'lastc', '11217'::text);
query T
SELECT LIST(SELECT id FROM customer)::text
----
{1,2,3}
query T
SELECT LIST(SELECT other_field FROM users ORDER BY id ASC)::text
----
{10,5,8}
query error Expected subselect to return 1 column, got 2 columns
SELECT LIST(SELECT first_name, last_name FROM customer)::text
# Verify nested lists
query error LIST expression cannot be cast to uniform type: text list vs integer list
SELECT LIST[LIST[customer.first_name, customer.last_name], LIST[customer.zip], LIST[customer.id]]::text FROM customer JOIN users ON customer.id = users.id ORDER BY users.other_field DESC LIMIT 2
query T
SELECT LIST[LIST[customer.first_name, customer.last_name], LIST[customer.zip], LIST[customer.id::text]]::text FROM customer JOIN users ON customer.id = users.id ORDER BY users.other_field DESC LIMIT 2
----
{{alice,lasta},{10003},{1}}
{{charlie,lastc},{11217},{3}}
# Check CTE syntax can be included in a query
query T
SELECT LIST(WITH usps AS (SELECT 42) SELECT LIST[LIST[customer.first_name, customer.last_name], LIST[customer.zip], LIST[customer.id::text]] FROM customer JOIN users ON customer.id = users.id ORDER BY users.other_field DESC LIMIT 2)::text
----
{{{alice,lasta},{10003},{1}},{{charlie,lastc},{11217},{3}}}
statement ok
CREATE TABLE qs (q int not null)
query T
SELECT LIST(SELECT TRUE FROM(SELECT AVG(0) FROM qs))::text;
----
{t}
query T
SELECT LIST(SELECT TRUE FROM(SELECT AVG(0) FROM (SELECT FROM qs)))::text;
----
{t}