Skip to content

Commit

Permalink
sql: Implement support for a show indexes from database command
Browse files Browse the repository at this point in the history
Implements the feature requested in cockroachdb#37270.

Release note (sql change): Support a show indexes from database command
  • Loading branch information
rohany committed Jun 3, 2019
1 parent 4d815f8 commit b395c80
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,11 @@ show_grants_stmt ::=

show_indexes_stmt ::=
'SHOW' 'INDEX' 'FROM' table_name
| 'SHOW' 'INDEX' 'FROM' 'DATABASE' database_name
| 'SHOW' 'INDEXES' 'FROM' table_name
| 'SHOW' 'INDEXES' 'FROM' 'DATABASE' database_name
| 'SHOW' 'KEYS' 'FROM' table_name
| 'SHOW' 'KEYS' 'FROM' 'DATABASE' database_name

show_jobs_stmt ::=
'SHOW' opt_automatic 'JOBS'
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/delegate/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func TryDelegate(
case *tree.ShowCreate:
return d.delegateShowCreate(t)

case *tree.ShowDbIndexes:
return d.delegateShowDbIndexes(t)

case *tree.ShowIndexes:
return d.delegateShowIndexes(t)

Expand Down
14 changes: 14 additions & 0 deletions pkg/sql/delegate/show_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ func (d *delegator) delegateShowCreate(n *tree.ShowCreate) (tree.Statement, erro
return d.showTableDetails(n.Name, showCreateQuery)
}

func (d *delegator) delegateShowDbIndexes(n *tree.ShowDbIndexes) (tree.Statement, error) {
const getAllIndexesQuery = `
SELECT table_name,
index_name,
non_unique::BOOL,
seq_in_index,
column_name,
direction,
storing::BOOL,
implicit::BOOL
FROM %s.information_schema.statistics`
return parse(fmt.Sprintf(getAllIndexesQuery, n.Database))
}

func (d *delegator) delegateShowIndexes(n *tree.ShowIndexes) (tree.Statement, error) {
const getIndexesQuery = `
SELECT table_name,
Expand Down
17 changes: 17 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/show_source
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,20 @@ code 42601
file sql/parser/show_syntax.go
function RunShowSyntax
detail source SQL: foo ^


# Test the SHOW INDEXES FROM DATABSE COMMAND
statement ok
CREATE DATABASE showdbindexestest;

statement ok
CREATE TABLE showdbindexestest.table1 (key1 INT PRIMARY KEY);

statement ok
CREATE TABLE showdbindexestest.table2 (key2 INT PRIMARY KEY);

query TTBITTBB
SHOW INDEXES FROM DATABASE showdbindexestest;
----
table1 primary false 1 key1 ASC false false
table2 primary false 1 key2 ASC false false
14 changes: 13 additions & 1 deletion pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -3347,23 +3347,35 @@ show_grants_stmt:

// %Help: SHOW INDEXES - list indexes
// %Category: DDL
// %Text: SHOW INDEXES FROM <tablename>
// %Text: SHOW INDEXES FROM { <tablename> | DATABASE <database_name> }
// %SeeAlso: WEBDOCS/show-index.html
show_indexes_stmt:
SHOW INDEX FROM table_name
{
$$.val = &tree.ShowIndexes{Table: $4.unresolvedObjectName()}
}
| SHOW INDEX error // SHOW HELP: SHOW INDEXES
| SHOW INDEX FROM DATABASE database_name
{
$$.val = &tree.ShowDbIndexes{Database: $5}
}
| SHOW INDEXES FROM table_name
{
$$.val = &tree.ShowIndexes{Table: $4.unresolvedObjectName()}
}
| SHOW INDEXES FROM DATABASE database_name
{
$$.val = &tree.ShowDbIndexes{Database: $5}
}
| SHOW INDEXES error // SHOW HELP: SHOW INDEXES
| SHOW KEYS FROM table_name
{
$$.val = &tree.ShowIndexes{Table: $4.unresolvedObjectName()}
}
| SHOW KEYS FROM DATABASE database_name
{
$$.val = &tree.ShowDbIndexes{Database: $5}
}
| SHOW KEYS error // SHOW HELP: SHOW INDEXES

// %Help: SHOW CONSTRAINTS - list constraints
Expand Down
11 changes: 11 additions & 0 deletions pkg/sql/sem/tree/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ func (node *ShowIndexes) Format(ctx *FmtCtx) {
ctx.FormatNode(node.Table)
}

// ShowDbIndexes represents a SHOW INDEXES FROM DATABASE statement.
type ShowDbIndexes struct {
Database string
}

// Format implements the NodeFormatter interface.
func (node *ShowDbIndexes) Format(ctx *FmtCtx) {
ctx.WriteString("SHOW INDEXES FROM DATABASE")
ctx.WriteString(node.Database)
}

// ShowQueries represents a SHOW QUERIES statement
type ShowQueries struct {
All bool
Expand Down
7 changes: 7 additions & 0 deletions pkg/sql/sem/tree/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,12 @@ func (*ShowGrants) StatementType() StatementType { return Rows }
// StatementTag returns a short string identifying the type of statement.
func (*ShowGrants) StatementTag() string { return "SHOW GRANTS" }

// StatementType implements the Statement interface.
func (*ShowDbIndexes) StatementType() StatementType { return Rows }

// StatementTag returns a short string identifying the type of statement.
func (*ShowDbIndexes) StatementTag() string { return "SHOW INDEXES FROM DATABASE" }

// StatementType implements the Statement interface.
func (*ShowIndexes) StatementType() StatementType { return Rows }

Expand Down Expand Up @@ -915,6 +921,7 @@ func (n *ShowColumns) String() string { return AsString(n) }
func (n *ShowConstraints) String() string { return AsString(n) }
func (n *ShowCreate) String() string { return AsString(n) }
func (n *ShowDatabases) String() string { return AsString(n) }
func (n *ShowDbIndexes) String() string { return AsString(n) }
func (n *ShowGrants) String() string { return AsString(n) }
func (n *ShowHistogram) String() string { return AsString(n) }
func (n *ShowIndexes) String() string { return AsString(n) }
Expand Down

0 comments on commit b395c80

Please sign in to comment.