forked from MaterializeInc/materialize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_subquery.slt
168 lines (130 loc) · 3.37 KB
/
array_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
# 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 (ARRAY[1])::text
### Array subqueries ###
statement ok
SELECT ARRAY(SELECT x FROM xs)::text
query T
SELECT ARRAY(SELECT x FROM xs)::text
----
{1,1,2}
query T
SELECT ARRAY(SELECT x FROM xs WHERE x > 1)::text
----
{2}
query T
SELECT ARRAY[ARRAY(SELECT x FROM xs)]::text
----
{{1,1,2}}
query T
SELECT ARRAY(SELECT x FROM xs LIMIT 2)::text
----
{1,1}
query T
SELECT ARRAY(SELECT x FROM xs ORDER BY x DESC)::text
----
{2,1,1}
query T
SELECT ARRAY(SELECT x FROM xs ORDER BY x DESC LIMIT 2)::text
----
{2,1}
query T
SELECT ARRAY(SELECT x FROM xs ORDER BY x DESC LIMIT 1)::text
----
{2}
query T
SELECT ARRAY[ARRAY(SELECT x FROM xs), ARRAY(SELECT y FROM ys)]::text
----
{{1,1,2},{2,4,5}}
query T
SELECT array_cat(ARRAY(SELECT x FROM xs), ARRAY(SELECT y FROM ys))::text
----
{1,1,2,2,4,5}
query T
SELECT array_cat(ARRAY(SELECT x FROM xs), ARRAY(SELECT y FROM ys ORDER BY y DESC))::text
----
{1,1,2,5,4,2}
query T
SELECT ARRAY(SELECT y FROM xs JOIN ys ON xs.x = ys.y)::text
----
{2}
query T
SELECT ARRAY(SELECT y FROM xs JOIN ys ON 2*xs.x >= ys.y)::text
----
{2,2,2,4}
query T
SELECT ARRAY(SELECT DISTINCT y FROM xs JOIN ys ON 2*xs.x >= ys.y)::text
----
{2,4}
query T
SELECT ARRAY(SELECT 1 WHERE FALSE)::text
----
{}
statement ok
CREATE TABLE zs (z int not null)
query T
SELECT ARRAY(SELECT z FROM zs)::text;
----
{}
query T
SELECT ARRAY(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 ARRAY(SELECT id FROM customer)::text
----
{1,2,3}
query T
SELECT ARRAY(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 ARRAY(SELECT first_name, last_name FROM customer)::text
# Verify nested arrays
query error text\[\]\[\] not yet supported
SELECT ARRAY(SELECT ARRAY[customer.first_name] FROM customer)
# Check CTE syntax can be included in a query
query T
SELECT ARRAY(WITH usps AS (SELECT 42) SELECT customer.first_name FROM customer)
----
{bob,alice,charlie}
query T
SELECT ARRAY(WITH usps AS (SELECT 42) SELECT * FROM usps)
----
{42}
statement ok
CREATE TABLE qs (q int not null)
query T
SELECT ARRAY(SELECT TRUE FROM(SELECT AVG(0) FROM qs))::text;
----
{t}
query T
SELECT ARRAY(SELECT TRUE FROM(SELECT AVG(0) FROM (SELECT FROM qs)))::text;
----
{t}
query error text list\[\] not yet supported
SELECT ARRAY(SELECT LIST[customer.first_name, customer.last_name, customer.zip, customer.id::text] FROM customer);