From ebbf34efa457e198e92e9de09081ace9b29d3f36 Mon Sep 17 00:00:00 2001 From: akilan Date: Thu, 7 Jan 2021 13:24:43 +0400 Subject: [PATCH] added default database in 'show database' Signed-off-by: akilan --- go/vt/sqlparser/ast_funcs.go | 2 ++ go/vt/sqlparser/constants.go | 2 ++ go/vt/sqlparser/parse_test.go | 4 ++-- go/vt/sqlparser/sql.go | 4 ++-- go/vt/sqlparser/sql.y | 4 ++-- go/vt/vtgate/executor_test.go | 10 +++++++++- go/vt/vtgate/planbuilder/show.go | 14 ++++++++++++-- 7 files changed, 31 insertions(+), 9 deletions(-) diff --git a/go/vt/sqlparser/ast_funcs.go b/go/vt/sqlparser/ast_funcs.go index 895b6a7c593..40dc63d24bc 100644 --- a/go/vt/sqlparser/ast_funcs.go +++ b/go/vt/sqlparser/ast_funcs.go @@ -1243,6 +1243,8 @@ func (ty ShowCommandType) ToString() string { return VariableGlobalStr case VariableSession: return VariableSessionStr + case Keyspace: + return KeyspaceStr default: return "Unknown ShowCommandType" } diff --git a/go/vt/sqlparser/constants.go b/go/vt/sqlparser/constants.go index 2cccc5b35fe..854586c0118 100644 --- a/go/vt/sqlparser/constants.go +++ b/go/vt/sqlparser/constants.go @@ -219,6 +219,7 @@ const ( StatusSessionStr = " status" VariableGlobalStr = " global variables" VariableSessionStr = " variables" + KeyspaceStr = " keyspaces" // DropKeyType strings PrimaryKeyTypeStr = "primary key" @@ -458,6 +459,7 @@ const ( StatusSession VariableGlobal VariableSession + Keyspace ) // DropKeyType constants diff --git a/go/vt/sqlparser/parse_test.go b/go/vt/sqlparser/parse_test.go index 0aa38e7b596..e8f0892a95c 100644 --- a/go/vt/sqlparser/parse_test.go +++ b/go/vt/sqlparser/parse_test.go @@ -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", }, { diff --git a/go/vt/sqlparser/sql.go b/go/vt/sqlparser/sql.go index 61b6cd24fdf..77b9c1976e2 100644 --- a/go/vt/sqlparser/sql.go +++ b/go/vt/sqlparser/sql.go @@ -7753,13 +7753,13 @@ yydefault: yyDollar = yyS[yypt-3 : yypt+1] //line sql.y:2346 { - yyVAL.statement = &Show{&ShowBasic{Command: Database, Filter: yyDollar[3].showFilter}} + yyVAL.statement = &Show{&ShowBasic{Command: Keyspace, Filter: yyDollar[3].showFilter}} } case 429: yyDollar = yyS[yypt-3 : yypt+1] //line sql.y:2350 { - yyVAL.statement = &Show{&ShowBasic{Command: Database, Filter: yyDollar[3].showFilter}} + yyVAL.statement = &Show{&ShowBasic{Command: Keyspace, Filter: yyDollar[3].showFilter}} } case 430: yyDollar = yyS[yypt-4 : yypt+1] diff --git a/go/vt/sqlparser/sql.y b/go/vt/sqlparser/sql.y index 817ee6bf614..8ee97892f67 100644 --- a/go/vt/sqlparser/sql.y +++ b/go/vt/sqlparser/sql.y @@ -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 { diff --git a/go/vt/vtgate/executor_test.go b/go/vt/vtgate/executor_test.go index 6c6fed83254..c22c0647016 100644 --- a/go/vt/vtgate/executor_test.go +++ b/go/vt/vtgate/executor_test.go @@ -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) diff --git a/go/vt/vtgate/planbuilder/show.go b/go/vt/vtgate/planbuilder/show.go index a94ada32e27..506568beabc 100644 --- a/go/vt/vtgate/planbuilder/show.go +++ b/go/vt/vtgate/planbuilder/show.go @@ -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 @@ -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))