Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: Adding support for show indexes from database command #37942

Merged
merged 1 commit into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.ShowDatabaseIndexes:
return d.delegateShowDatabaseIndexes(t)

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

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

func (d *delegator) delegateShowDatabaseIndexes(
n *tree.ShowDatabaseIndexes,
) (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.String()))
}

func (d *delegator) delegateShowIndexes(n *tree.ShowIndexes) (tree.Statement, error) {
const getIndexesQuery = `
SELECT table_name,
Expand Down
32 changes: 32 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,35 @@ 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

statement ok
CREATE DATABASE "$peci@l";

statement ok
CREATE TABLE "$peci@l".table1 (key1 INT PRIMARY KEY);

statement ok
CREATE TABLE "$peci@l".table2 (key2 INT PRIMARY KEY);

query TTBITTBB
SHOW INDEXES FROM DATABASE "$peci@l";
----
table1 primary false 1 key1 ASC false false
table2 primary false 1 key2 ASC false false
1 change: 1 addition & 0 deletions pkg/sql/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ func TestParse(t *testing.T) {
{`SHOW INDEXES FROM a`},
{`EXPLAIN SHOW INDEXES FROM a`},
{`SHOW INDEXES FROM a.b.c`},
{`SHOW INDEXES FROM DATABASE a`},
{`SHOW CONSTRAINTS FROM a`},
{`SHOW CONSTRAINTS FROM a.b.c`},
{`EXPLAIN SHOW CONSTRAINTS FROM a.b.c`},
Expand Down
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.ShowDatabaseIndexes{Database: tree.Name($5)}
}
| SHOW INDEXES FROM table_name
{
$$.val = &tree.ShowIndexes{Table: $4.unresolvedObjectName()}
}
| SHOW INDEXES FROM DATABASE database_name
{
$$.val = &tree.ShowDatabaseIndexes{Database: tree.Name($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.ShowDatabaseIndexes{Database: tree.Name($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)
}

// ShowDatabaseIndexes represents a SHOW INDEXES FROM DATABASE statement.
type ShowDatabaseIndexes struct {
Database Name
}

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

// ShowQueries represents a SHOW QUERIES statement
type ShowQueries struct {
All bool
Expand Down
9 changes: 8 additions & 1 deletion pkg/sql/sem/tree/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,11 +684,17 @@ 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 (*ShowDatabaseIndexes) StatementType() StatementType { return Rows }

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

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

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

// StatementType implements the Statement interface.
func (*ShowQueries) 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 *ShowDatabaseIndexes) 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