Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
37942: sql: Adding support for show indexes from database command r=rohany a=rohany

As requested in cockroachdb#37270, support for a show indexes from database command would be helpful. This PR includes support for the command in the parser. Future PR's will implement the functionality of the parsed results.

37977: cli: fix use of IPv6 addresses with RPC client commands r=knz a=knz

Fixes cockroachdb#33008.

(This was actually a regression of my doing, back from cockroachdb#28373. Didn't pick it up back then because we didn't have a test.)

Release note (bug fix): the `cockroach` command line utilities that
internally use a RPC connection (e.g. `cockroach quit`, `cockroach
init`, etc) again properly support passing an IPv6 address literal via
the `--host` argument.

Co-authored-by: Rohan Yadav <[email protected]>
Co-authored-by: Raphael 'kena' Poss <[email protected]>
  • Loading branch information
3 people committed Jun 3, 2019
3 parents 65f2dc7 + eba3b37 + ab4f77a commit b0f95de
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 6 deletions.
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
4 changes: 3 additions & 1 deletion pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type cliTestParams struct {
noServer bool
storeSpecs []base.StoreSpec
locality roachpb.Locality
addr string
}

func (c *cliTest) fail(err interface{}) {
Expand Down Expand Up @@ -135,6 +136,7 @@ func newCLITest(params cliTestParams) cliTest {
SSLCertsDir: c.certsDir,
StoreSpecs: params.storeSpecs,
Locality: params.locality,
Addr: params.addr,
})
if err != nil {
c.fail(err)
Expand Down Expand Up @@ -370,7 +372,7 @@ func (c cliTest) runWithArgsUnredirected(origArgs []string) {
args = append(args, "--insecure=false")
args = append(args, fmt.Sprintf("--certs-dir=%s", c.certsDir))
}
args = append(args, fmt.Sprintf("--host=%s:%s", h, p))
args = append(args, fmt.Sprintf("--host=%s", net.JoinHostPort(h, p)))
}
args = append(args, origArgs[1:]...)

Expand Down
3 changes: 0 additions & 3 deletions pkg/cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -1105,9 +1105,6 @@ func addrWithDefaultHost(addr string) (string, error) {
if host == "" {
host = "localhost"
}
if strings.Contains(host, ":") {
host = "[" + host + "]"
}
return net.JoinHostPort(host, port), nil
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/cli/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,25 @@ func TestGCProfiles(t *testing.T) {
sum -= len(data[:i])
}
}

func TestAddrWithDefaultHost(t *testing.T) {
defer leaktest.AfterTest(t)()

testData := []struct {
inAddr string
outAddr string
}{
{"localhost:123", "localhost:123"},
{":123", "localhost:123"},
{"[::1]:123", "[::1]:123"},
}

for _, test := range testData {
addr, err := addrWithDefaultHost(test.inAddr)
if err != nil {
t.Error(err)
} else if addr != test.outAddr {
t.Errorf("expected %q, got %q", test.outAddr, addr)
}
}
}
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

0 comments on commit b0f95de

Please sign in to comment.