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

rewrite show tables to use the real db name #6452

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 14 additions & 0 deletions go/mysql/fakesqldb/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,3 +695,17 @@ func (db *DB) VerifyAllExecutedOrFail() {
db.t.Errorf("%v: not all expected queries were executed. leftovers: %v", db.name, db.expectedExecuteFetch[db.expectedExecuteFetchIndex:])
}
}

//LastQuery returns the last query
func (db *DB) LastQuery() interface{} {
size := len(db.querylog)
if size == 0 {
return "no queries have been run"
}
return db.querylog[size-1]
}

//Name returns that database name
func (db *DB) Name() string {
return db.name
}
32 changes: 32 additions & 0 deletions go/vt/vttablet/tabletserver/planbuilder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package planbuilder

import (
"strings"

"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vttablet/tabletserver/schema"
Expand Down Expand Up @@ -132,6 +134,36 @@ func analyzeSet(set *sqlparser.Set) (plan *Plan) {
}
}

const tablesIn = "tables_in_"

func analyzeShow(show *sqlparser.Show, keyspace string, dbName string) (plan *Plan) {
rewriteShowStatement(show, keyspace, dbName)
return &Plan{
PlanID: PlanOtherRead,
FullQuery: GenerateFullQuery(show),
}
}

// rewriteShowStatement replaces the keyspace with the database name
func rewriteShowStatement(show *sqlparser.Show, keyspace string, dbName string) {
opt := show.ShowTablesOpt
if opt != nil {
lowerDbName := strings.ToLower(dbName)
if strings.EqualFold(opt.DbName, keyspace) {
opt.DbName = lowerDbName
}
sqlparser.Rewrite(show.ShowTablesOpt.Filter, func(cursor *sqlparser.Cursor) bool {
switch n := cursor.Node().(type) {
case *sqlparser.ColName:
if n.Name.EqualString(tablesIn + keyspace) {
n.Name = sqlparser.NewColIdent(tablesIn + lowerDbName)
}
}
return true
}, nil)
}
}

func lookupTable(tableExprs sqlparser.TableExprs, tables map[string]*schema.Table) *schema.Table {
if len(tableExprs) > 1 {
return nil
Expand Down
6 changes: 3 additions & 3 deletions go/vt/vttablet/tabletserver/planbuilder/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (plan *Plan) TableName() sqlparser.TableIdent {
}

// Build builds a plan based on the schema.
func Build(statement sqlparser.Statement, tables map[string]*schema.Table) (*Plan, error) {
func Build(statement sqlparser.Statement, tables map[string]*schema.Table, keyspace, dbName string) (*Plan, error) {
var plan *Plan

err := checkForPoolingUnsafeConstructs(statement)
Expand Down Expand Up @@ -179,9 +179,9 @@ func Build(statement sqlparser.Statement, tables map[string]*schema.Table) (*Pla
case *sqlparser.DDL:
// DDLs and other statements below don't get fully parsed.
// We have to use the original query at the time of execution.
plan = &Plan{PlanID: PlanDDL}
plan, err = &Plan{PlanID: PlanDDL}, nil
case *sqlparser.Show:
plan, err = &Plan{PlanID: PlanOtherRead}, nil
plan, err = analyzeShow(stmt, keyspace, dbName), nil
case *sqlparser.OtherRead:
plan, err = &Plan{PlanID: PlanOtherRead}, nil
case *sqlparser.OtherAdmin:
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vttablet/tabletserver/planbuilder/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestPlan(t *testing.T) {
var err error
statement, err := sqlparser.Parse(tcase.input)
if err == nil {
plan, err = Build(statement, testSchema)
plan, err = Build(statement, testSchema, "keyspace", "dbName")
}
PassthroughDMLs = false

Expand Down Expand Up @@ -123,7 +123,7 @@ func TestCustom(t *testing.T) {
if err != nil {
t.Fatalf("Got error: %v, parsing sql: %v", err.Error(), tcase.input)
}
plan, err := Build(statement, schem)
plan, err := Build(statement, schem, "keyspace", "dbName")
var out string
if err != nil {
out = err.Error()
Expand Down
19 changes: 18 additions & 1 deletion go/vt/vttablet/tabletserver/planbuilder/testdata/exec_cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,24 @@ options:PassthroughDMLs
"show a"
{
"PlanID": "OtherRead",
"TableName": ""
"TableName": "",
"FullQuery": "show a"
}

# show tables with filter
"show tables from keyspace where Tables_in_keyspace='table1'"
{
"PlanID": "OtherRead",
"TableName": "",
"FullQuery": "show tables from dbname where tables_in_dbname = 'table1'"
}

# show tables with filter no matter case
"show tables from KeYsPaCe where Tables_in_keYSPace='table1'"
{
"PlanID": "OtherRead",
"TableName": "",
"FullQuery": "show tables from dbname where tables_in_dbname = 'table1'"
}

# describe
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vttablet/tabletserver/query_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func (qe *QueryEngine) Close() {
}

// GetPlan returns the TabletPlan that for the query. Plans are cached in a cache.LRUCache.
func (qe *QueryEngine) GetPlan(ctx context.Context, logStats *tabletenv.LogStats, sql string, skipQueryPlanCache bool) (*TabletPlan, error) {
func (qe *QueryEngine) GetPlan(ctx context.Context, logStats *tabletenv.LogStats, sql string, skipQueryPlanCache bool, keyspace string) (*TabletPlan, error) {
span, ctx := trace.NewSpan(ctx, "QueryEngine.GetPlan")
defer span.Finish()

Expand All @@ -305,7 +305,7 @@ func (qe *QueryEngine) GetPlan(ctx context.Context, logStats *tabletenv.LogStats
if err != nil {
return nil, err
}
splan, err := planbuilder.Build(statement, qe.tables)
splan, err := planbuilder.Build(statement, qe.tables, keyspace, qe.env.Config().DB.DBName)
if err != nil {
return nil, err
}
Expand Down
28 changes: 14 additions & 14 deletions go/vt/vttablet/tabletserver/query_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ func TestGetPlanPanicDuetoEmptyQuery(t *testing.T) {
for query, result := range schematest.Queries() {
db.AddQuery(query, result)
}
qe := newTestQueryEngine(10, 10*time.Second, true, newDBConfigs(db))
qe := newTestQueryEngine(10, 10*time.Second, newDBConfigs(db))
qe.se.Open()
qe.Open()
defer qe.Close()

ctx := context.Background()
logStats := tabletenv.NewLogStats(ctx, "GetPlanStats")
_, err := qe.GetPlan(ctx, logStats, "", false)
_, err := qe.GetPlan(ctx, logStats, "", false /*skipQueryPlanCache*/, "keyspace")
want := "empty statement"
if err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("qe.GetPlan: %v, want %s", err, want)
Expand All @@ -111,7 +111,7 @@ func TestGetMessageStreamPlan(t *testing.T) {
for query, result := range schematest.Queries() {
db.AddQuery(query, result)
}
qe := newTestQueryEngine(10, 10*time.Second, true, newDBConfigs(db))
qe := newTestQueryEngine(10, 10*time.Second, newDBConfigs(db))
qe.se.Open()
qe.Open()
defer qe.Close()
Expand Down Expand Up @@ -148,22 +148,22 @@ func TestQueryPlanCache(t *testing.T) {
db.AddQuery("select * from test_table_01 where 1 != 1", &sqltypes.Result{})
db.AddQuery("select * from test_table_02 where 1 != 1", &sqltypes.Result{})

qe := newTestQueryEngine(10, 10*time.Second, true, newDBConfigs(db))
qe := newTestQueryEngine(10, 10*time.Second, newDBConfigs(db))
qe.se.Open()
qe.Open()
defer qe.Close()

ctx := context.Background()
logStats := tabletenv.NewLogStats(ctx, "GetPlanStats")
qe.SetQueryPlanCacheCap(1)
firstPlan, err := qe.GetPlan(ctx, logStats, firstQuery, false)
firstPlan, err := qe.GetPlan(ctx, logStats, firstQuery, false /*skipQueryPlanCache*/, "keyspace")
if err != nil {
t.Fatal(err)
}
if firstPlan == nil {
t.Fatalf("plan should not be nil")
}
secondPlan, err := qe.GetPlan(ctx, logStats, secondQuery, false)
secondPlan, err := qe.GetPlan(ctx, logStats, secondQuery, false /*skipQueryPlanCache*/, "keyspace")
if err != nil {
t.Fatal(err)
}
Expand All @@ -190,15 +190,15 @@ func TestNoQueryPlanCache(t *testing.T) {
db.AddQuery("select * from test_table_01 where 1 != 1", &sqltypes.Result{})
db.AddQuery("select * from test_table_02 where 1 != 1", &sqltypes.Result{})

qe := newTestQueryEngine(10, 10*time.Second, true, newDBConfigs(db))
qe := newTestQueryEngine(10, 10*time.Second, newDBConfigs(db))
qe.se.Open()
qe.Open()
defer qe.Close()

ctx := context.Background()
logStats := tabletenv.NewLogStats(ctx, "GetPlanStats")
qe.SetQueryPlanCacheCap(1)
firstPlan, err := qe.GetPlan(ctx, logStats, firstQuery, true)
firstPlan, err := qe.GetPlan(ctx, logStats, firstQuery, true /*skipQueryPlanCache*/, "keyspace")
if err != nil {
t.Fatal(err)
}
Expand All @@ -222,15 +222,15 @@ func TestNoQueryPlanCacheDirective(t *testing.T) {
db.AddQuery("select /*vt+ SKIP_QUERY_PLAN_CACHE=1 */ * from test_table_01 where 1 != 1", &sqltypes.Result{})
db.AddQuery("select /*vt+ SKIP_QUERY_PLAN_CACHE=1 */ * from test_table_02 where 1 != 1", &sqltypes.Result{})

qe := newTestQueryEngine(10, 10*time.Second, true, newDBConfigs(db))
qe := newTestQueryEngine(10, 10*time.Second, newDBConfigs(db))
qe.se.Open()
qe.Open()
defer qe.Close()

ctx := context.Background()
logStats := tabletenv.NewLogStats(ctx, "GetPlanStats")
qe.SetQueryPlanCacheCap(1)
firstPlan, err := qe.GetPlan(ctx, logStats, firstQuery, false)
firstPlan, err := qe.GetPlan(ctx, logStats, firstQuery, false /*skipQueryPlanCache*/, "keyspace")
if err != nil {
t.Fatal(err)
}
Expand All @@ -251,14 +251,14 @@ func TestStatsURL(t *testing.T) {
}
query := "select * from test_table_01"
db.AddQuery("select * from test_table_01 where 1 != 1", &sqltypes.Result{})
qe := newTestQueryEngine(10, 1*time.Second, true, newDBConfigs(db))
qe := newTestQueryEngine(10, 1*time.Second, newDBConfigs(db))
qe.se.Open()
qe.Open()
defer qe.Close()
// warm up cache
ctx := context.Background()
logStats := tabletenv.NewLogStats(ctx, "GetPlanStats")
qe.GetPlan(ctx, logStats, query, false)
qe.GetPlan(ctx, logStats, query, false /*skipQueryPlanCache*/, "keyspace")

request, _ := http.NewRequest("GET", "/debug/tablet_plans", nil)
response := httptest.NewRecorder()
Expand All @@ -273,7 +273,7 @@ func TestStatsURL(t *testing.T) {
qe.handleHTTPQueryRules(response, request)
}

func newTestQueryEngine(queryCacheSize int, idleTimeout time.Duration, strict bool, dbcfgs *dbconfigs.DBConfigs) *QueryEngine {
func newTestQueryEngine(queryCacheSize int, idleTimeout time.Duration, dbcfgs *dbconfigs.DBConfigs) *QueryEngine {
config := tabletenv.NewDefaultConfig()
config.DB = dbcfgs
config.QueryCacheSize = queryCacheSize
Expand All @@ -291,7 +291,7 @@ func runConsolidatedQuery(t *testing.T, sql string) *QueryEngine {
db := fakesqldb.New(t)
defer db.Close()

qe := newTestQueryEngine(10, 1*time.Second, true, newDBConfigs(db))
qe := newTestQueryEngine(10, 1*time.Second, newDBConfigs(db))
qe.se.Open()
qe.Open()
defer qe.Close()
Expand Down
9 changes: 8 additions & 1 deletion go/vt/vttablet/tabletserver/query_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,14 @@ func (qre *QueryExecutor) execOther() (*sqltypes.Result, error) {
return nil, err
}
defer conn.Recycle()
return qre.execSQL(conn, qre.query, true)
var query string
if qre.plan.FullQuery != nil {
query = qre.plan.FullQuery.Query
} else {
query = qre.query
}

return qre.execSQL(conn, query, true)
}

func (qre *QueryExecutor) getConn() (*connpool.DBConn, error) {
Expand Down
Loading