Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
27110: opt: add support for reverse scans r=madhavsuresh a=madhavsuresh

The optimizer does not generate plans which include reverse scans.
This PR adds support logical support for reverse scans.

Release note: None

27145: ui: depend on babel-polyfill r=couchand a=benesch

babel-polyfill is the standard means of importing the necessary set of
polyfills for the configured babel preset. We were previously forced to
manually importing the polyfills we needed because the combination of
Node 6, JSDom, and babel-polyfill was broken. Since we're no longer
using JSDom (see 7e89701), we can use babel-polyfill.

Release note: None

27204: sql/parser: unreserve VIEW r=knz a=knz

Found while looking at #26993.

VIEW does not need to be a reserved keyword. In fact, it sounds
innocuous enough for users to want to use it as a column name. So make
it an unreserved keyword.

Release note (sql change): CockroachDB now allows clients to use the
word `view` as identifier like in PostgreSQL.

Co-authored-by: madhavsuresh <[email protected]>
Co-authored-by: Nikhil Benesch <[email protected]>
Co-authored-by: Raphael 'kena' Poss <[email protected]>
  • Loading branch information
4 people committed Jul 9, 2018
4 parents c56fd84 + 15a0507 + ce57a22 + 67c0cfa commit 888d8fc
Show file tree
Hide file tree
Showing 30 changed files with 1,089 additions and 425 deletions.
2 changes: 1 addition & 1 deletion docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,7 @@ unreserved_keyword ::=
| 'VALIDATE'
| 'VALUE'
| 'VARYING'
| 'VIEW'
| 'WITHIN'
| 'WITHOUT'
| 'WRITE'
Expand Down Expand Up @@ -1549,7 +1550,6 @@ reserved_keyword ::=
| 'USER'
| 'USING'
| 'VARIADIC'
| 'VIEW'
| 'WHEN'
| 'WHERE'
| 'WINDOW'
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/opt/exec/execbuilder/relational_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ func (b *Builder) buildScan(ev memo.ExprView) (execPlan, error) {
needed,
def.Constraint,
def.HardLimit,
def.Reverse,
reqOrder,
)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/exec/execbuilder/testdata/distsql_numtables
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ https://cockroachdb.github.io/distsqlplan/decode.html?eJyMjzFLBEEMhXt_xfHqgLuFzV
query T
SELECT "URL" FROM [EXPLAIN (DISTSQL) SELECT y FROM NumToStr WHERE y < 1000 OR y > 9000 ORDER BY y DESC LIMIT 5]
----
https://cockroachdb.github.io/distsqlplan/decode.html?eJyskTFr8zAQhvfvV4SbFRI58aLJa-AjKWm34kG1jiCwfeZ0hpbg_15sFVK3sZNCRp30vM_L6Qw1OdzbCgOYV9CgIIVcQcNUYAjE_Tg-2rl3MGsFvm5a6ce5goIYwZxBvJQIBl7sW4lHtA55tQYFDsX6cohu2FeWP7K6rYSCMCg4tGIWmYa8U0CtXGKD2BOC0Z26X_1MLMirZGzN9BIU_PeVl0U6KUomRZd8YoeM7nd83l1ps6clNavt-PWtGptRDX3_qvWDV31D_bXqzWNXfUV0xNBQHXAkmkpe9_-A7oTx3wK1XOATUzFo4vEwcMPAYZB4q-NhV8ervuB3WM_CyQjWP-FkFk7nzZtZeDsPb_9UO-_-fQYAAP__GoVT2w==
https://cockroachdb.github.io/distsqlplan/decode.html?eJyMkDFrwzAQhff-ivBmldiBLJqyBkpSQrfiQbWOILB9QneCluD_XmQNTQpNM967-57e0wUTezq4kQT2HS0MtugMYuKeRDgVuR7t_SdsYxCmmLXInUHPiWAv0KADweLAzxzXxcWTujAsZ7MBZ_2BRN2ZYDezuTJu7xu_uY-BTuQ8pXVzY4-YwujS127Ko7JogsExq13tSoyXMAZdbfFXiPYmxD_tTiSRJ6GH6jVzZ0D-TPUHhXPq6TVxvzxTx-PCLYIn0brd1GE_1VUJeA23d-HmF9zNT98BAAD___cAnxU=

query TTTTT
EXPLAIN (VERBOSE) SELECT x FROM (SELECT x, 2*x, x+1 FROM NumToSquare)
Expand Down
14 changes: 6 additions & 8 deletions pkg/sql/opt/exec/execbuilder/testdata/limit
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,12 @@ limit · · (k, v) +v
query TTTTT
EXPLAIN (VERBOSE) SELECT k, v FROM t ORDER BY v DESC LIMIT (1+4) OFFSET 1
----
limit · · (k, v) -v
│ count 5 · ·
│ offset 1 · ·
└── sort · · (k, v) -v
│ order -v · ·
└── scan · · (k, v) ·
· table t@t_v_idx · ·
· spans ALL · ·
limit · · (k, v) -v
│ count 5 · ·
│ offset 1 · ·
└── revscan · · (k, v) -v
· table t@t_v_idx · ·
· spans ALL · ·

query TTTTT
EXPLAIN (VERBOSE) SELECT sum(w) FROM t GROUP BY k, v ORDER BY v DESC LIMIT 10
Expand Down
47 changes: 19 additions & 28 deletions pkg/sql/opt/exec/execbuilder/testdata/orderby
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,10 @@ render · · (c, b) ·
query TTT
EXPLAIN SELECT b FROM t ORDER BY a DESC
----
render · ·
└── sort · ·
│ order -a
└── scan · ·
· table t@primary
· spans ALL
render · ·
└── revscan · ·
· table t@primary
· spans ALL

# Check that LIMIT propagates past nosort nodes.
query TTT
Expand All @@ -74,26 +72,21 @@ render · ·
· spans ALL
· limit 1

# TODO(radu): Need to support revscan node
query TTT
EXPLAIN SELECT b FROM t ORDER BY a DESC, b ASC
----
render · ·
└── sort · ·
│ order -a
└── scan · ·
· table t@primary
· spans ALL
render · ·
└── revscan · ·
· table t@primary
· spans ALL

query TTT
EXPLAIN SELECT b FROM t ORDER BY a DESC, b DESC
----
render · ·
└── sort · ·
│ order -a
└── scan · ·
· table t@primary
· spans ALL
render · ·
└── revscan · ·
· table t@primary
· spans ALL

query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM t ORDER BY (b, t.*)
Expand Down Expand Up @@ -334,27 +327,26 @@ output row: [1 2 3]
fetched: /abc/bc/5/6 -> /4
output row: [4 5 6]

# TODO(radu): this should change once we have revscan
statement ok
SET tracing = on,kv,results; SELECT a FROM abc ORDER BY a DESC; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /abc/bc/2/3 -> /1
fetched: /abc/bc/5/6 -> /4
fetched: /abc/primary/4/5/6 -> NULL
fetched: /abc/primary/4/5/6 -> NULL
output row: [4]
fetched: /abc/primary/1/2/3 -> NULL
fetched: /abc/primary/1/2/3 -> NULL
output row: [1]

query TTT
EXPLAIN SELECT a FROM abc ORDER BY a DESC
----
sort · ·
│ order -a
└── scan · ·
· table abc@bc
· spans ALL
revscan · ·
· table abc@primary
· spans ALL

query TTT
EXPLAIN SELECT c FROM abc WHERE b = 2 ORDER BY c
Expand All @@ -364,7 +356,6 @@ render · ·
· table abc@bc
· spans /2-/3

# TODO(radu): sort is unnecessary here, because bc is unique (need revscan)
query TTT
EXPLAIN SELECT c FROM abc WHERE b = 2 ORDER BY c DESC
----
Expand Down
113 changes: 45 additions & 68 deletions pkg/sql/opt/exec/execbuilder/testdata/select_index
Original file line number Diff line number Diff line change
Expand Up @@ -652,16 +652,13 @@ INSERT INTO test2(k)
query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM test2 WHERE k <= '100' ORDER BY k DESC LIMIT 20
----
limit · · (id, k, v) -k
│ count 20 · ·
└── sort · · (id, k, v) -k
│ order -k · ·
└── index-join · · (id, k, v) ·
├── scan · · (id, k) ·
│ table test2@test2_k_key · ·
│ spans /!NULL-/"100"/PrefixEnd · ·
└── scan · · (id, k, v) ·
· table test2@primary · ·
index-join · · (id, k, v) -k
├── revscan · · (id, k) -k
│ table test2@test2_k_key · ·
│ spans /!NULL-/"100"/PrefixEnd · ·
│ limit 20 · ·
└── scan · · (id, k, v) ·
· table test2@primary · ·

# Result check: The following query must not issue more than the
# requested LIMIT K/V reads, even though an index join batches 100
Expand All @@ -677,66 +674,46 @@ SELECT regexp_replace(message, '\d\d\d\d\d+', '...PK...')
FROM [SHOW KV TRACE FOR SESSION]
WHERE message LIKE 'fetched:%'
----
fetched: /test2/test2_k_key/'001' -> /...PK...
fetched: /test2/test2_k_key/'002' -> /...PK...
fetched: /test2/test2_k_key/'003' -> /...PK...
fetched: /test2/test2_k_key/'004' -> /...PK...
fetched: /test2/test2_k_key/'005' -> /...PK...
fetched: /test2/test2_k_key/'006' -> /...PK...
fetched: /test2/test2_k_key/'007' -> /...PK...
fetched: /test2/test2_k_key/'008' -> /...PK...
fetched: /test2/test2_k_key/'009' -> /...PK...
fetched: /test2/test2_k_key/'010' -> /...PK...
fetched: /test2/test2_k_key/'011' -> /...PK...
fetched: /test2/test2_k_key/'012' -> /...PK...
fetched: /test2/test2_k_key/'013' -> /...PK...
fetched: /test2/test2_k_key/'014' -> /...PK...
fetched: /test2/test2_k_key/'015' -> /...PK...
fetched: /test2/test2_k_key/'016' -> /...PK...
fetched: /test2/test2_k_key/'017' -> /...PK...
fetched: /test2/test2_k_key/'018' -> /...PK...
fetched: /test2/test2_k_key/'019' -> /...PK...
fetched: /test2/test2_k_key/'020' -> /...PK...
fetched: /test2/test2_k_key/'021' -> /...PK...
fetched: /test2/test2_k_key/'022' -> /...PK...
fetched: /test2/test2_k_key/'023' -> /...PK...
fetched: /test2/test2_k_key/'024' -> /...PK...
fetched: /test2/test2_k_key/'025' -> /...PK...
fetched: /test2/test2_k_key/'026' -> /...PK...
fetched: /test2/test2_k_key/'027' -> /...PK...
fetched: /test2/test2_k_key/'028' -> /...PK...
fetched: /test2/test2_k_key/'029' -> /...PK...
fetched: /test2/test2_k_key/'030' -> /...PK...
fetched: /test2/primary/...PK.../k/v -> /'001'/42
fetched: /test2/primary/...PK.../k/v -> /'002'/42
fetched: /test2/primary/...PK.../k/v -> /'003'/42
fetched: /test2/primary/...PK.../k/v -> /'004'/42
fetched: /test2/primary/...PK.../k/v -> /'005'/42
fetched: /test2/primary/...PK.../k/v -> /'006'/42
fetched: /test2/primary/...PK.../k/v -> /'007'/42
fetched: /test2/primary/...PK.../k/v -> /'008'/42
fetched: /test2/primary/...PK.../k/v -> /'009'/42
fetched: /test2/primary/...PK.../k/v -> /'010'/42
fetched: /test2/primary/...PK.../k/v -> /'011'/42
fetched: /test2/primary/...PK.../k/v -> /'012'/42
fetched: /test2/primary/...PK.../k/v -> /'013'/42
fetched: /test2/primary/...PK.../k/v -> /'014'/42
fetched: /test2/primary/...PK.../k/v -> /'015'/42
fetched: /test2/primary/...PK.../k/v -> /'016'/42
fetched: /test2/primary/...PK.../k/v -> /'017'/42
fetched: /test2/primary/...PK.../k/v -> /'018'/42
fetched: /test2/primary/...PK.../k/v -> /'019'/42
fetched: /test2/primary/...PK.../k/v -> /'020'/42
fetched: /test2/primary/...PK.../k/v -> /'021'/42
fetched: /test2/primary/...PK.../k/v -> /'022'/42
fetched: /test2/primary/...PK.../k/v -> /'023'/42
fetched: /test2/primary/...PK.../k/v -> /'024'/42
fetched: /test2/primary/...PK.../k/v -> /'025'/42
fetched: /test2/primary/...PK.../k/v -> /'026'/42
fetched: /test2/primary/...PK.../k/v -> /'027'/42
fetched: /test2/primary/...PK.../k/v -> /'028'/42
fetched: /test2/primary/...PK.../k/v -> /'029'/42
fetched: /test2/test2_k_key/'029' -> /...PK...
fetched: /test2/test2_k_key/'028' -> /...PK...
fetched: /test2/test2_k_key/'027' -> /...PK...
fetched: /test2/test2_k_key/'026' -> /...PK...
fetched: /test2/test2_k_key/'025' -> /...PK...
fetched: /test2/test2_k_key/'024' -> /...PK...
fetched: /test2/test2_k_key/'023' -> /...PK...
fetched: /test2/test2_k_key/'022' -> /...PK...
fetched: /test2/test2_k_key/'021' -> /...PK...
fetched: /test2/test2_k_key/'020' -> /...PK...
fetched: /test2/test2_k_key/'019' -> /...PK...
fetched: /test2/test2_k_key/'018' -> /...PK...
fetched: /test2/test2_k_key/'017' -> /...PK...
fetched: /test2/test2_k_key/'016' -> /...PK...
fetched: /test2/test2_k_key/'015' -> /...PK...
fetched: /test2/test2_k_key/'014' -> /...PK...
fetched: /test2/test2_k_key/'013' -> /...PK...
fetched: /test2/test2_k_key/'012' -> /...PK...
fetched: /test2/test2_k_key/'011' -> /...PK...
fetched: /test2/primary/...PK.../k/v -> /'030'/42
fetched: /test2/primary/...PK.../k/v -> /'029'/42
fetched: /test2/primary/...PK.../k/v -> /'028'/42
fetched: /test2/primary/...PK.../k/v -> /'027'/42
fetched: /test2/primary/...PK.../k/v -> /'026'/42
fetched: /test2/primary/...PK.../k/v -> /'025'/42
fetched: /test2/primary/...PK.../k/v -> /'024'/42
fetched: /test2/primary/...PK.../k/v -> /'023'/42
fetched: /test2/primary/...PK.../k/v -> /'022'/42
fetched: /test2/primary/...PK.../k/v -> /'021'/42
fetched: /test2/primary/...PK.../k/v -> /'020'/42
fetched: /test2/primary/...PK.../k/v -> /'019'/42
fetched: /test2/primary/...PK.../k/v -> /'018'/42
fetched: /test2/primary/...PK.../k/v -> /'017'/42
fetched: /test2/primary/...PK.../k/v -> /'016'/42
fetched: /test2/primary/...PK.../k/v -> /'015'/42
fetched: /test2/primary/...PK.../k/v -> /'014'/42
fetched: /test2/primary/...PK.../k/v -> /'013'/42
fetched: /test2/primary/...PK.../k/v -> /'012'/42
fetched: /test2/primary/...PK.../k/v -> /'011'/42

# Regression test for #20035.
statement ok
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/opt/exec/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type Factory interface {
needed ColumnOrdinalSet,
indexConstraint *constraint.Constraint,
hardLimit int64,
reverse bool,
reqOrder sqlbase.ColumnOrdering,
) (Node, error)

Expand Down
4 changes: 3 additions & 1 deletion pkg/sql/opt/memo/memo_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ func (f exprFormatter) formatPrivate(private interface{}, mode formatMode) {
} else {
fmt.Fprintf(f.buf, " %s@%s", tab.TabName(), tab.Index(t.Index).IdxName())
}

if t.Reverse {
fmt.Fprintf(f.buf, ",rev")
}
if mode == formatMemo {
if tab.ColumnCount() != t.Cols.Len() {
fmt.Fprintf(f.buf, ",cols=%s", t.Cols)
Expand Down
9 changes: 8 additions & 1 deletion pkg/sql/opt/memo/private_defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func (p ProjectionsOpDef) AllCols() opt.ColSet {

// ScanOpDef defines the value of the Def private field of the Scan operator.
type ScanOpDef struct {
// Reverse defines if the Scan is a reverse scan.
Reverse bool

// Table identifies the table to scan. It is an id that can be passed to
// the Metadata.Table method in order to fetch opt.Table metadata.
Table opt.TableID
Expand Down Expand Up @@ -104,7 +107,11 @@ func (s *ScanOpDef) CanProvideOrdering(md *opt.Metadata, required *props.Orderin
for i := 0; i < index.KeyColumnCount(); i++ {
indexCol := index.Column(i)
colID := md.TableColumn(s.Table, indexCol.Ordinal)
ordering.AppendCol(colID, indexCol.Descending)
if s.Reverse {
ordering.AppendCol(colID, !indexCol.Descending)
} else {
ordering.AppendCol(colID, indexCol.Descending)
}
}
return ordering.SubsetOf(required)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/sql/opt/memo/private_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ func (ps *privateStorage) internScanOpDef(def *ScanOpDef) PrivateID {
// The below code is carefully constructed to not allocate in the case where
// the value is already in the map. Be careful when modifying.
ps.keyBuf.Reset()
if def.Reverse {
ps.keyBuf.WriteByte(1)
} else {
ps.keyBuf.WriteByte(0)
}
ps.keyBuf.writeUvarint(uint64(def.Table))
ps.keyBuf.writeUvarint(uint64(def.Index))

Expand Down
Loading

0 comments on commit 888d8fc

Please sign in to comment.