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

bindinfo: set default db for bindings correctly #14077

Merged
merged 3 commits into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 23 additions & 0 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,26 @@ func (s *testSuite) TestDefaultSessionVars(c *C) {
"tidb_evolve_plan_baselines off",
"tidb_use_plan_baselines on"))
}

func (s *testSuite) TestDefaultDB(c *C) {
tk := testkit.NewTestKit(c, s.store)
s.cleanBindingEnv(tk)
tk.MustExec("use test")
tk.MustExec("create table t(a int, b int, index idx(a))")
tk.MustExec("create global binding for select * from test.t using select * from test.t use index(idx)")
tk.MustExec("use mysql")
tk.MustQuery("select * from test.t")
// Even in another database, we could still use the bindings.
c.Assert(tk.Se.GetSessionVars().StmtCtx.IndexNames[0], Equals, "t:idx")
tk.MustExec("drop global binding for select * from test.t")
tk.MustQuery("show global bindings").Check(testkit.Rows())

tk.MustExec("use test")
tk.MustExec("create session binding for select * from test.t using select * from test.t use index(idx)")
tk.MustExec("use mysql")
tk.MustQuery("select * from test.t")
// Even in another database, we could still use the bindings.
c.Assert(tk.Se.GetSessionVars().StmtCtx.IndexNames[0], Equals, "t:idx")
tk.MustExec("drop session binding for select * from test.t")
tk.MustQuery("show session bindings").Check(testkit.Rows())
}
7 changes: 4 additions & 3 deletions executor/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type SQLBindExec struct {
bindSQL string
charset string
collation string
db string
isGlobal bool
bindAst ast.StmtNode
}
Expand Down Expand Up @@ -69,9 +70,9 @@ func (e *SQLBindExec) dropSQLBind() error {
}
if !e.isGlobal {
handle := e.ctx.Value(bindinfo.SessionBindInfoKeyType).(*bindinfo.SessionHandle)
return handle.DropBindRecord(e.normdOrigSQL, e.ctx.GetSessionVars().CurrentDB, bindInfo)
return handle.DropBindRecord(e.normdOrigSQL, e.db, bindInfo)
}
return domain.GetDomain(e.ctx).BindHandle().DropBindRecord(e.normdOrigSQL, e.ctx.GetSessionVars().CurrentDB, bindInfo)
return domain.GetDomain(e.ctx).BindHandle().DropBindRecord(e.normdOrigSQL, e.db, bindInfo)
}

func (e *SQLBindExec) createSQLBind() error {
Expand All @@ -83,7 +84,7 @@ func (e *SQLBindExec) createSQLBind() error {
}
record := &bindinfo.BindRecord{
OriginalSQL: e.normdOrigSQL,
Db: e.ctx.GetSessionVars().CurrentDB,
Db: e.db,
Bindings: []bindinfo.Binding{bindInfo},
}
if !e.isGlobal {
Expand Down
1 change: 1 addition & 0 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2442,6 +2442,7 @@ func (b *executorBuilder) buildSQLBindExec(v *plannercore.SQLBindPlan) Executor
bindSQL: v.BindSQL,
charset: v.Charset,
collation: v.Collation,
db: v.Db,
isGlobal: v.IsGlobal,
bindAst: v.BindStmt,
}
Expand Down
1 change: 1 addition & 0 deletions planner/core/common_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ type SQLBindPlan struct {
BindSQL string
IsGlobal bool
BindStmt ast.StmtNode
Db string
Charset string
Collation string
}
Expand Down
30 changes: 30 additions & 0 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ func (b *PlanBuilder) buildDropBindPlan(v *ast.DropBindingStmt) (Plan, error) {
SQLBindOp: OpSQLBindDrop,
NormdOrigSQL: parser.Normalize(v.OriginSel.Text()),
IsGlobal: v.GlobalScope,
Db: getDefaultDB(b.ctx, v.OriginSel),
}
if v.HintedSel != nil {
p.BindSQL = v.HintedSel.Text()
Expand All @@ -539,13 +540,42 @@ func (b *PlanBuilder) buildCreateBindPlan(v *ast.CreateBindingStmt) (Plan, error
BindSQL: v.HintedSel.Text(),
IsGlobal: v.GlobalScope,
BindStmt: v.HintedSel,
Db: getDefaultDB(b.ctx, v.OriginSel),
Charset: charSet,
Collation: collation,
}
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.SuperPriv, "", "", "", nil)
return p, nil
}

func getDefaultDB(ctx sessionctx.Context, sel ast.StmtNode) string {
implicitDB := &implicitDatabase{}
sel.Accept(implicitDB)
if implicitDB.hasImplicit {
return ctx.GetSessionVars().CurrentDB
}
return ""
}

type implicitDatabase struct {
hasImplicit bool
}

func (i *implicitDatabase) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
switch x := in.(type) {
case *ast.TableName:
if x.Schema.L == "" {
i.hasImplicit = true
}
return in, true
}
return in, false
}

func (i *implicitDatabase) Leave(in ast.Node) (out ast.Node, ok bool) {
return in, true
}

// detectSelectAgg detects an aggregate function or GROUP BY clause.
func (b *PlanBuilder) detectSelectAgg(sel *ast.SelectStmt) bool {
if sel.GroupBy != nil {
Expand Down
1 change: 1 addition & 0 deletions planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func (p *preprocessor) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
EraseLastSemicolon(node.OriginSel)
EraseLastSemicolon(node.HintedSel)
p.checkBindGrammar(node.OriginSel, node.HintedSel)
return in, true
case *ast.DropBindingStmt:
EraseLastSemicolon(node.OriginSel)
if node.HintedSel != nil {
Expand Down
3 changes: 3 additions & 0 deletions planner/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ func getBindRecord(ctx sessionctx.Context, stmt ast.StmtNode) (*bindinfo.BindRec
}
sessionHandle := ctx.Value(bindinfo.SessionBindInfoKeyType).(*bindinfo.SessionHandle)
bindRecord := sessionHandle.GetBindRecord(normalizedSQL, ctx.GetSessionVars().CurrentDB)
if bindRecord == nil {
bindRecord = sessionHandle.GetBindRecord(normalizedSQL, "")
}
if bindRecord != nil {
if bindRecord.HasUsingBinding() {
return bindRecord, metrics.ScopeSession
Expand Down