Skip to content

Commit 635f2e1

Browse files
alivxxxsre-bot
authored andcommitted
bindinfo: set default db for bindings correctly (pingcap#14077)
1 parent 0d7edc7 commit 635f2e1

File tree

7 files changed

+63
-3
lines changed

7 files changed

+63
-3
lines changed

bindinfo/bind_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -603,3 +603,26 @@ func (s *testSuite) TestDefaultSessionVars(c *C) {
603603
"tidb_evolve_plan_baselines off",
604604
"tidb_use_plan_baselines on"))
605605
}
606+
607+
func (s *testSuite) TestDefaultDB(c *C) {
608+
tk := testkit.NewTestKit(c, s.store)
609+
s.cleanBindingEnv(tk)
610+
tk.MustExec("use test")
611+
tk.MustExec("create table t(a int, b int, index idx(a))")
612+
tk.MustExec("create global binding for select * from test.t using select * from test.t use index(idx)")
613+
tk.MustExec("use mysql")
614+
tk.MustQuery("select * from test.t")
615+
// Even in another database, we could still use the bindings.
616+
c.Assert(tk.Se.GetSessionVars().StmtCtx.IndexNames[0], Equals, "t:idx")
617+
tk.MustExec("drop global binding for select * from test.t")
618+
tk.MustQuery("show global bindings").Check(testkit.Rows())
619+
620+
tk.MustExec("use test")
621+
tk.MustExec("create session binding for select * from test.t using select * from test.t use index(idx)")
622+
tk.MustExec("use mysql")
623+
tk.MustQuery("select * from test.t")
624+
// Even in another database, we could still use the bindings.
625+
c.Assert(tk.Se.GetSessionVars().StmtCtx.IndexNames[0], Equals, "t:idx")
626+
tk.MustExec("drop session binding for select * from test.t")
627+
tk.MustQuery("show session bindings").Check(testkit.Rows())
628+
}

executor/bind.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type SQLBindExec struct {
3434
bindSQL string
3535
charset string
3636
collation string
37+
db string
3738
isGlobal bool
3839
bindAst ast.StmtNode
3940
}
@@ -69,9 +70,9 @@ func (e *SQLBindExec) dropSQLBind() error {
6970
}
7071
if !e.isGlobal {
7172
handle := e.ctx.Value(bindinfo.SessionBindInfoKeyType).(*bindinfo.SessionHandle)
72-
return handle.DropBindRecord(e.normdOrigSQL, e.ctx.GetSessionVars().CurrentDB, bindInfo)
73+
return handle.DropBindRecord(e.normdOrigSQL, e.db, bindInfo)
7374
}
74-
return domain.GetDomain(e.ctx).BindHandle().DropBindRecord(e.normdOrigSQL, e.ctx.GetSessionVars().CurrentDB, bindInfo)
75+
return domain.GetDomain(e.ctx).BindHandle().DropBindRecord(e.normdOrigSQL, e.db, bindInfo)
7576
}
7677

7778
func (e *SQLBindExec) createSQLBind() error {
@@ -83,7 +84,7 @@ func (e *SQLBindExec) createSQLBind() error {
8384
}
8485
record := &bindinfo.BindRecord{
8586
OriginalSQL: e.normdOrigSQL,
86-
Db: e.ctx.GetSessionVars().CurrentDB,
87+
Db: e.db,
8788
Bindings: []bindinfo.Binding{bindInfo},
8889
}
8990
if !e.isGlobal {

executor/builder.go

+1
Original file line numberDiff line numberDiff line change
@@ -2535,6 +2535,7 @@ func (b *executorBuilder) buildSQLBindExec(v *plannercore.SQLBindPlan) Executor
25352535
bindSQL: v.BindSQL,
25362536
charset: v.Charset,
25372537
collation: v.Collation,
2538+
db: v.Db,
25382539
isGlobal: v.IsGlobal,
25392540
bindAst: v.BindStmt,
25402541
}

planner/core/common_plans.go

+1
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ type SQLBindPlan struct {
498498
BindSQL string
499499
IsGlobal bool
500500
BindStmt ast.StmtNode
501+
Db string
501502
Charset string
502503
Collation string
503504
}

planner/core/planbuilder.go

+30
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ func (b *PlanBuilder) buildDropBindPlan(v *ast.DropBindingStmt) (Plan, error) {
524524
SQLBindOp: OpSQLBindDrop,
525525
NormdOrigSQL: parser.Normalize(v.OriginSel.Text()),
526526
IsGlobal: v.GlobalScope,
527+
Db: getDefaultDB(b.ctx, v.OriginSel),
527528
}
528529
if v.HintedSel != nil {
529530
p.BindSQL = v.HintedSel.Text()
@@ -540,13 +541,42 @@ func (b *PlanBuilder) buildCreateBindPlan(v *ast.CreateBindingStmt) (Plan, error
540541
BindSQL: v.HintedSel.Text(),
541542
IsGlobal: v.GlobalScope,
542543
BindStmt: v.HintedSel,
544+
Db: getDefaultDB(b.ctx, v.OriginSel),
543545
Charset: charSet,
544546
Collation: collation,
545547
}
546548
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.SuperPriv, "", "", "", nil)
547549
return p, nil
548550
}
549551

552+
func getDefaultDB(ctx sessionctx.Context, sel ast.StmtNode) string {
553+
implicitDB := &implicitDatabase{}
554+
sel.Accept(implicitDB)
555+
if implicitDB.hasImplicit {
556+
return ctx.GetSessionVars().CurrentDB
557+
}
558+
return ""
559+
}
560+
561+
type implicitDatabase struct {
562+
hasImplicit bool
563+
}
564+
565+
func (i *implicitDatabase) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
566+
switch x := in.(type) {
567+
case *ast.TableName:
568+
if x.Schema.L == "" {
569+
i.hasImplicit = true
570+
}
571+
return in, true
572+
}
573+
return in, false
574+
}
575+
576+
func (i *implicitDatabase) Leave(in ast.Node) (out ast.Node, ok bool) {
577+
return in, true
578+
}
579+
550580
// detectSelectAgg detects an aggregate function or GROUP BY clause.
551581
func (b *PlanBuilder) detectSelectAgg(sel *ast.SelectStmt) bool {
552582
if sel.GroupBy != nil {

planner/core/preprocess.go

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ func (p *preprocessor) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
123123
EraseLastSemicolon(node.OriginSel)
124124
EraseLastSemicolon(node.HintedSel)
125125
p.checkBindGrammar(node.OriginSel, node.HintedSel)
126+
return in, true
126127
case *ast.DropBindingStmt:
127128
EraseLastSemicolon(node.OriginSel)
128129
if node.HintedSel != nil {

planner/optimize.go

+3
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ func getBindRecord(ctx sessionctx.Context, stmt ast.StmtNode) (*bindinfo.BindRec
189189
}
190190
sessionHandle := ctx.Value(bindinfo.SessionBindInfoKeyType).(*bindinfo.SessionHandle)
191191
bindRecord := sessionHandle.GetBindRecord(normalizedSQL, ctx.GetSessionVars().CurrentDB)
192+
if bindRecord == nil {
193+
bindRecord = sessionHandle.GetBindRecord(normalizedSQL, "")
194+
}
192195
if bindRecord != nil {
193196
if bindRecord.HasUsingBinding() {
194197
return bindRecord, metrics.ScopeSession

0 commit comments

Comments
 (0)