Skip to content

Commit

Permalink
Merge pull request #7256 from Akilan1999/show_information_schema
Browse files Browse the repository at this point in the history
added default databases when calling 'show databases'
  • Loading branch information
systay authored Jan 14, 2021
2 parents d9ebaa3 + ebbf34e commit ae24394
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 9 deletions.
2 changes: 2 additions & 0 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,8 @@ func (ty ShowCommandType) ToString() string {
return VariableGlobalStr
case VariableSession:
return VariableSessionStr
case Keyspace:
return KeyspaceStr
default:
return "Unknown ShowCommandType"
}
Expand Down
2 changes: 2 additions & 0 deletions go/vt/sqlparser/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ const (
StatusSessionStr = " status"
VariableGlobalStr = " global variables"
VariableSessionStr = " variables"
KeyspaceStr = " keyspaces"

// DropKeyType strings
PrimaryKeyTypeStr = "primary key"
Expand Down Expand Up @@ -458,6 +459,7 @@ const (
StatusSession
VariableGlobal
VariableSession
Keyspace
)

// DropKeyType constants
Expand Down
4 changes: 2 additions & 2 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1479,10 +1479,10 @@ var (
output: "show variables",
}, {
input: "show vitess_keyspaces",
output: "show databases",
output: "show keyspaces",
}, {
input: "show vitess_keyspaces like '%'",
output: "show databases like '%'",
output: "show keyspaces like '%'",
}, {
input: "show vitess_shards",
}, {
Expand Down
4 changes: 2 additions & 2 deletions go/vt/sqlparser/sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions go/vt/sqlparser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -2344,11 +2344,11 @@ show_statement:
}
| SHOW KEYSPACES like_or_where_opt
{
$$ = &Show{&ShowBasic{Command: Database, Filter: $3}}
$$ = &Show{&ShowBasic{Command: Keyspace, Filter: $3}}
}
| SHOW VITESS_KEYSPACES like_or_where_opt
{
$$ = &Show{&ShowBasic{Command: Database, Filter: $3}}
$$ = &Show{&ShowBasic{Command: Keyspace, Filter: $3}}
}
| SHOW FUNCTION STATUS like_or_where_opt
{
Expand Down
10 changes: 9 additions & 1 deletion go/vt/vtgate/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,11 +437,19 @@ func TestExecutorShow(t *testing.T) {
executor, _, _, sbclookup := createLegacyExecutorEnv()
session := NewSafeSession(&vtgatepb.Session{TargetString: "@master"})

for _, query := range []string{"show databases", "show vitess_keyspaces", "show keyspaces", "show DATABASES", "show schemas", "show SCHEMAS"} {
for _, query := range []string{"show vitess_keyspaces", "show keyspaces"} {
qr, err := executor.Execute(ctx, "TestExecute", session, query, nil)
require.NoError(t, err)
require.EqualValues(t, 5, qr.RowsAffected, fmt.Sprintf("unexpected results running query: %s", query))
}

for _, query := range []string{"show databases", "show DATABASES", "show schemas", "show SCHEMAS"} {
qr, err := executor.Execute(ctx, "TestExecute", session, query, nil)
require.NoError(t, err)
// Showing default tables (5+4[default])
require.EqualValues(t, 9, qr.RowsAffected, fmt.Sprintf("unexpected results running query: %s", query))
}

_, err := executor.Execute(ctx, "TestExecute", session, "show variables", nil)
require.NoError(t, err)
_, err = executor.Execute(ctx, "TestExecute", session, "show collation", nil)
Expand Down
14 changes: 12 additions & 2 deletions go/vt/vtgate/planbuilder/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func buildShowBasicPlan(show *sqlparser.ShowBasic, vschema ContextVSchema) (engi
case sqlparser.Collation, sqlparser.Function, sqlparser.Privilege, sqlparser.Procedure,
sqlparser.VariableGlobal, sqlparser.VariableSession:
return showSendAnywhere(show, vschema)
case sqlparser.Database:
case sqlparser.Database, sqlparser.Keyspace:
ks, err := vschema.AllKeyspace()
if err != nil {
return nil, err
Expand All @@ -73,7 +73,17 @@ func buildShowBasicPlan(show *sqlparser.ShowBasic, vschema ContextVSchema) (engi
filter = regexp.MustCompile(".*")
}

rows := make([][]sqltypes.Value, 0, len(ks))
//rows := make([][]sqltypes.Value, 0, len(ks)+4)
var rows [][]sqltypes.Value

if show.Command == sqlparser.Database {
//Hard code default databases
rows = append(rows, buildVarCharRow("information_schema"))
rows = append(rows, buildVarCharRow("mysql"))
rows = append(rows, buildVarCharRow("sys"))
rows = append(rows, buildVarCharRow("performance_schema"))
}

for _, v := range ks {
if filter.MatchString(v.Name) {
rows = append(rows, buildVarCharRow(v.Name))
Expand Down

0 comments on commit ae24394

Please sign in to comment.