Skip to content

Commit

Permalink
Merge pull request MaterializeInc#3765 from benesch/topk-idiom
Browse files Browse the repository at this point in the history
test: add plan + test for idiomatic top-k-in-group query
  • Loading branch information
benesch authored Jul 28, 2020
2 parents 210caae + f276dfd commit f885f1f
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions test/sqllogictest/topk.slt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Copyright Materialize, Inc. 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)
| Distinct group=(#1)

%1 =
| Get %0
| ArrangeBy (#0)

%2 =
| Get %0
| ArrangeBy (#0)

%3 =
| Get materialize.public.cities (u1)

%4 =
| Join %2 %3 (= #0 #2)
| | implementation = Differential %3 %2.(#0)
| | demand = (#0, #1, #3)
| TopK group=(#0) order=(#3 desc) limit=3 offset=0

%5 =
| Join %1 %4 (= #0 #1)
| | implementation = Differential %4 %1.(#0)
| | demand = (#0, #2)
| Project (#0, #2)

EOF

0 comments on commit f885f1f

Please sign in to comment.