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

planner: remove keyword prepared in plan cache backend #37023

Merged
merged 4 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ type ExecStmt struct {

// OutputNames will be set if using cached plan
OutputNames []*types.FieldName
PsStmt *plannercore.CachedPrepareStmt
PsStmt *plannercore.PlanCacheStmt
Ti *TelemetryInfo
}

Expand Down
16 changes: 8 additions & 8 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3554,10 +3554,10 @@ func TestPointGetPreparedPlan(t *testing.T) {

pspk1Id, _, _, err := tk.Session().PrepareStmt("select * from t where a = ?")
require.NoError(t, err)
tk.Session().GetSessionVars().PreparedStmts[pspk1Id].(*plannercore.CachedPrepareStmt).PreparedAst.UseCache = false
tk.Session().GetSessionVars().PreparedStmts[pspk1Id].(*plannercore.PlanCacheStmt).PreparedAst.UseCache = false
pspk2Id, _, _, err := tk.Session().PrepareStmt("select * from t where ? = a ")
require.NoError(t, err)
tk.Session().GetSessionVars().PreparedStmts[pspk2Id].(*plannercore.CachedPrepareStmt).PreparedAst.UseCache = false
tk.Session().GetSessionVars().PreparedStmts[pspk2Id].(*plannercore.PlanCacheStmt).PreparedAst.UseCache = false

ctx := context.Background()
// first time plan generated
Expand Down Expand Up @@ -3597,7 +3597,7 @@ func TestPointGetPreparedPlan(t *testing.T) {
// unique index
psuk1Id, _, _, err := tk.Session().PrepareStmt("select * from t where b = ? ")
require.NoError(t, err)
tk.Session().GetSessionVars().PreparedStmts[psuk1Id].(*plannercore.CachedPrepareStmt).PreparedAst.UseCache = false
tk.Session().GetSessionVars().PreparedStmts[psuk1Id].(*plannercore.PlanCacheStmt).PreparedAst.UseCache = false

rs, err = tk.Session().ExecutePreparedStmt(ctx, psuk1Id, expression.Args2Expressions4Test(1))
require.NoError(t, err)
Expand Down Expand Up @@ -3715,7 +3715,7 @@ func TestPointGetPreparedPlanWithCommitMode(t *testing.T) {

pspk1Id, _, _, err := tk1.Session().PrepareStmt("select * from t where a = ?")
require.NoError(t, err)
tk1.Session().GetSessionVars().PreparedStmts[pspk1Id].(*plannercore.CachedPrepareStmt).PreparedAst.UseCache = false
tk1.Session().GetSessionVars().PreparedStmts[pspk1Id].(*plannercore.PlanCacheStmt).PreparedAst.UseCache = false

ctx := context.Background()
// first time plan generated
Expand Down Expand Up @@ -3781,11 +3781,11 @@ func TestPointUpdatePreparedPlan(t *testing.T) {

updateID1, pc, _, err := tk.Session().PrepareStmt(`update t set c = c + 1 where a = ?`)
require.NoError(t, err)
tk.Session().GetSessionVars().PreparedStmts[updateID1].(*plannercore.CachedPrepareStmt).PreparedAst.UseCache = false
tk.Session().GetSessionVars().PreparedStmts[updateID1].(*plannercore.PlanCacheStmt).PreparedAst.UseCache = false
require.Equal(t, 1, pc)
updateID2, pc, _, err := tk.Session().PrepareStmt(`update t set c = c + 2 where ? = a`)
require.NoError(t, err)
tk.Session().GetSessionVars().PreparedStmts[updateID2].(*plannercore.CachedPrepareStmt).PreparedAst.UseCache = false
tk.Session().GetSessionVars().PreparedStmts[updateID2].(*plannercore.PlanCacheStmt).PreparedAst.UseCache = false
require.Equal(t, 1, pc)

ctx := context.Background()
Expand Down Expand Up @@ -3820,7 +3820,7 @@ func TestPointUpdatePreparedPlan(t *testing.T) {
// unique index
updUkID1, _, _, err := tk.Session().PrepareStmt(`update t set c = c + 10 where b = ?`)
require.NoError(t, err)
tk.Session().GetSessionVars().PreparedStmts[updUkID1].(*plannercore.CachedPrepareStmt).PreparedAst.UseCache = false
tk.Session().GetSessionVars().PreparedStmts[updUkID1].(*plannercore.PlanCacheStmt).PreparedAst.UseCache = false
rs, err = tk.Session().ExecutePreparedStmt(ctx, updUkID1, expression.Args2Expressions4Test(3))
require.Nil(t, rs)
require.NoError(t, err)
Expand Down Expand Up @@ -3889,7 +3889,7 @@ func TestPointUpdatePreparedPlanWithCommitMode(t *testing.T) {

ctx := context.Background()
updateID1, _, _, err := tk1.Session().PrepareStmt(`update t set c = c + 1 where a = ?`)
tk1.Session().GetSessionVars().PreparedStmts[updateID1].(*plannercore.CachedPrepareStmt).PreparedAst.UseCache = false
tk1.Session().GetSessionVars().PreparedStmts[updateID1].(*plannercore.PlanCacheStmt).PreparedAst.UseCache = false
require.NoError(t, err)

// first time plan generated
Expand Down
6 changes: 3 additions & 3 deletions executor/prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func (e *PrepareExec) Next(ctx context.Context, req *chunk.Chunk) error {
vars.PreparedStmtNameToID[e.name] = e.ID
}

preparedObj := &plannercore.CachedPrepareStmt{
preparedObj := &plannercore.PlanCacheStmt{
PreparedAst: prepared,
StmtDB: e.ctx.GetSessionVars().CurrentDB,
StmtText: stmt.Text(),
Expand Down Expand Up @@ -295,9 +295,9 @@ func (e *DeallocateExec) Next(ctx context.Context, req *chunk.Chunk) error {
return errors.Trace(plannercore.ErrStmtNotFound)
}
preparedPointer := vars.PreparedStmts[id]
preparedObj, ok := preparedPointer.(*plannercore.CachedPrepareStmt)
preparedObj, ok := preparedPointer.(*plannercore.PlanCacheStmt)
if !ok {
return errors.Errorf("invalid CachedPrepareStmt type")
return errors.Errorf("invalid PlanCacheStmt type")
}
prepared := preparedObj.PreparedAst
delete(vars.PreparedStmtNameToID, e.Name)
Expand Down
6 changes: 3 additions & 3 deletions executor/prepared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1252,8 +1252,8 @@ func TestPrepareStmtAfterIsolationReadChange(t *testing.T) {
require.Equal(t, rows[len(rows)-1][2], "cop[tiflash]")

require.Equal(t, 1, len(tk.Session().GetSessionVars().PreparedStmts))
require.Equal(t, "select * from `t`", tk.Session().GetSessionVars().PreparedStmts[1].(*plannercore.CachedPrepareStmt).NormalizedSQL)
require.Equal(t, "", tk.Session().GetSessionVars().PreparedStmts[1].(*plannercore.CachedPrepareStmt).NormalizedPlan)
require.Equal(t, "select * from `t`", tk.Session().GetSessionVars().PreparedStmts[1].(*plannercore.PlanCacheStmt).NormalizedSQL)
require.Equal(t, "", tk.Session().GetSessionVars().PreparedStmts[1].(*plannercore.PlanCacheStmt).NormalizedPlan)
}

func TestPreparePC4Binding(t *testing.T) {
Expand All @@ -1267,7 +1267,7 @@ func TestPreparePC4Binding(t *testing.T) {

tk.MustExec("prepare stmt from \"select * from t\"")
require.Equal(t, 1, len(tk.Session().GetSessionVars().PreparedStmts))
require.Equal(t, "select * from `test` . `t`", tk.Session().GetSessionVars().PreparedStmts[1].(*plannercore.CachedPrepareStmt).NormalizedSQL4PC)
require.Equal(t, "select * from `test` . `t`", tk.Session().GetSessionVars().PreparedStmts[1].(*plannercore.PlanCacheStmt).NormalizedSQL4PC)

tk.MustQuery("execute stmt")
tk.MustQuery("select @@last_plan_from_binding").Check(testkit.Rows("0"))
Expand Down
10 changes: 5 additions & 5 deletions planner/core/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ func NewPlanCacheValue(plan Plan, names []*types.FieldName, srcMap map[*model.Ta
}
}

// CachedPrepareStmt store prepared ast from PrepareExec and other related fields
type CachedPrepareStmt struct {
// PlanCacheStmt store prepared ast from PrepareExec and other related fields
type PlanCacheStmt struct {
PreparedAst *ast.Prepared
StmtDB string // which DB the statement will be processed over
VisitInfos []visitInfo
Expand All @@ -244,17 +244,17 @@ type CachedPrepareStmt struct {
}

// GetPreparedStmt extract the prepared statement from the execute statement.
func GetPreparedStmt(stmt *ast.ExecuteStmt, vars *variable.SessionVars) (*CachedPrepareStmt, error) {
func GetPreparedStmt(stmt *ast.ExecuteStmt, vars *variable.SessionVars) (*PlanCacheStmt, error) {
if stmt.PrepStmt != nil {
return stmt.PrepStmt.(*CachedPrepareStmt), nil
return stmt.PrepStmt.(*PlanCacheStmt), nil
}
if stmt.Name != "" {
prepStmt, err := vars.GetPreparedStmtByName(stmt.Name)
if err != nil {
return nil, err
}
stmt.PrepStmt = prepStmt
return prepStmt.(*CachedPrepareStmt), nil
return prepStmt.(*PlanCacheStmt), nil
}
return nil, ErrStmtNotFound
}
2 changes: 1 addition & 1 deletion planner/core/common_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ type Execute struct {

Name string
Params []expression.Expression
PrepStmt *CachedPrepareStmt
PrepStmt *PlanCacheStmt
Stmt ast.StmtNode
StmtType string
Plan Plan
Expand Down
Loading