forked from MaterializeInc/materialize
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtopk.slt
127 lines (108 loc) · 2.85 KB
/
topk.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
# 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 cities (
name text NOT NULL,
state text NOT NULL,
pop int NOT NULL
)
statement ok
INSERT INTO cities VALUES
('Los_Angeles', 'CA', 3979576),
('Phoenix', 'AZ', 1680992),
('Houston', 'TX', 2320268),
('San_Diego', 'CA', 1423851),
('San_Francisco', 'CA', 881549),
('New_York', 'NY', 8336817),
('Dallas', 'TX', 1343573),
('San_Antonio', 'TX', 1547253),
('San_Jose', 'CA', 1021795),
('Chicago', 'IL', 2695598),
('Austin', 'TX', 978908)
query TT rowsort
SELECT state, name FROM
(SELECT DISTINCT state FROM cities) grp,
LATERAL (SELECT name FROM cities WHERE state = grp.state ORDER BY pop DESC LIMIT 3)
----
AZ Phoenix
CA Los_Angeles
CA San_Diego
CA San_Jose
IL Chicago
NY New_York
TX Houston
TX San_Antonio
TX Dallas
mode standard
query T multiline
EXPLAIN PLAN FOR SELECT state, name FROM
(SELECT DISTINCT state FROM cities) grp,
LATERAL (SELECT name, pop FROM cities WHERE state = grp.state ORDER BY pop DESC LIMIT 3)
----
%0 =
| Get materialize.public.cities (u1)
| TopK group=(#1) order=(#2 desc) limit=3 offset=0
| Project (#1, #0)
EOF
query T multiline
EXPLAIN PLAN FOR SELECT state, name FROM
(SELECT DISTINCT state FROM cities) grp
LEFT JOIN LATERAL (SELECT name, pop FROM cities where cities.state = grp.state ORDER BY pop DESC LIMIT 3) ON true
----
%0 = Let l0 =
| Get materialize.public.cities (u1)
| TopK group=(#1) order=(#2 desc) limit=3 offset=0
| Project (#0, #1)
%1 =
| Get %0 (l0)
| Project (#1, #0)
%2 =
| Get %0 (l0)
| Project (#1)
| Distinct group=(#0)
| Negate
%3 =
| Get materialize.public.cities (u1)
| Project (#1)
| Distinct group=(#0)
%4 =
| Union %2 %3
| Map null
%5 =
| Union %1 %4
EOF
# Test that LIMIT 0 is optimized out
query T multiline
EXPLAIN PLAN FOR SELECT state, COUNT(*) FROM (
SELECT state, name FROM
(SELECT DISTINCT state FROM cities) grp,
LATERAL (SELECT name, pop FROM cities WHERE state = grp.state ORDER BY pop DESC LIMIT 0)
)
GROUP BY state
----
%0 =
| Constant
EOF
# Test that LIMIT 1 results in a unique key
query T multiline
EXPLAIN PLAN FOR SELECT state, COUNT(*) FROM (
SELECT state, name FROM
(SELECT DISTINCT state FROM cities) grp,
LATERAL (SELECT name, pop FROM cities WHERE state = grp.state ORDER BY pop DESC LIMIT 1)
)
GROUP BY state
----
%0 =
| Get materialize.public.cities (u1)
| Project (#1, #2)
| TopK group=(#0) order=(#1 desc) limit=1 offset=0
| Map 1
| Project (#0, #2)
EOF