Skip to content

Commit

Permalink
planner: fill plan digest when create binding from existing plan (#39557
Browse files Browse the repository at this point in the history
)

ref #39199
  • Loading branch information
fzzf678 authored Dec 1, 2022
1 parent 12e5272 commit ff89ef2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
9 changes: 9 additions & 0 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1428,14 +1428,23 @@ func TestCreateBindingFromHistory(t *testing.T) {
tk.MustExec(bindSQL)
planDigest := tk.MustQuery(fmt.Sprintf("select plan_digest from information_schema.statements_summary where query_sample_text = '%s'", bindSQL)).Rows()
tk.MustExec(fmt.Sprintf("create session binding from history using plan digest '%s'", planDigest[0][0]))
showRes := tk.MustQuery("show bindings").Rows()
require.Equal(t, len(showRes), 1)
require.Equal(t, planDigest[0][0], showRes[0][10])
for _, sql := range testCase.sqls {
tk.MustExec(fmt.Sprintf(sql, ""))
tk.MustQuery("select @@last_plan_from_binding").Check(testkit.Rows("1"))
}
}
showRes := tk.MustQuery("show bindings").Rows()
require.Equal(t, len(showRes), 1)
tk.MustExec(fmt.Sprintf("drop binding for sql digest '%s'", showRes[0][9]))
}

// exception cases
tk.MustGetErrMsg(fmt.Sprintf("create binding from history using plan digest '%s'", "1"), "can't find any plans for '1'")
tk.MustGetErrMsg(fmt.Sprintf("create binding from history using plan digest '%s'", ""), "plan digest is empty")
tk.MustExec("create binding for select * from t1, t2 where t1.id = t2.id using select /*+ merge_join(t1, t2) */ * from t1, t2 where t1.id = t2.id")
showRes := tk.MustQuery("show bindings").Rows()
require.Equal(t, showRes[0][10], "") // plan digest should be nil by create for
}
13 changes: 7 additions & 6 deletions executor/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,13 @@ func (e *SQLBindExec) createSQLBind() error {
}()

bindInfo := bindinfo.Binding{
BindSQL: e.bindSQL,
Charset: e.charset,
Collation: e.collation,
Status: bindinfo.Enabled,
Source: e.source,
SQLDigest: e.sqlDigest,
BindSQL: e.bindSQL,
Charset: e.charset,
Collation: e.collation,
Status: bindinfo.Enabled,
Source: e.source,
SQLDigest: e.sqlDigest,
PlanDigest: e.planDigest,
}
record := &bindinfo.BindRecord{
OriginalSQL: e.normdOrigSQL,
Expand Down
1 change: 1 addition & 0 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4817,6 +4817,7 @@ func (b *executorBuilder) buildSQLBindExec(v *plannercore.SQLBindPlan) Executor
newStatus: v.NewStatus,
source: v.Source,
sqlDigest: v.SQLDigest,
planDigest: v.PlanDigest,
}
return e
}
Expand Down
3 changes: 2 additions & 1 deletion planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1071,8 +1071,9 @@ func (b *PlanBuilder) buildCreateBindPlanFromPlanDigest(v *ast.CreateBindingStmt
Db: utilparser.GetDefaultDB(originNode, bindableStmt.Schema),
Charset: bindableStmt.Charset,
Collation: bindableStmt.Collation,
SQLDigest: sqlDigestWithDB.String(),
Source: bindinfo.History,
SQLDigest: sqlDigestWithDB.String(),
PlanDigest: v.PlanDigest,
}
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.SuperPriv, "", "", "", nil)
return p, nil
Expand Down

0 comments on commit ff89ef2

Please sign in to comment.