Skip to content

Commit

Permalink
revert the change
Browse files Browse the repository at this point in the history
  • Loading branch information
Reminiscent committed Dec 23, 2021
1 parent 8db3175 commit b858042
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 18 deletions.
3 changes: 1 addition & 2 deletions executor/prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,8 @@ func (e *DeallocateExec) Next(ctx context.Context, req *chunk.Chunk) error {
prepared := preparedObj.PreparedAst
delete(vars.PreparedStmtNameToID, e.Name)
if plannercore.PreparedPlanCacheEnabled() {
bindSQL := planner.GetBindSQL4PlanCache(e.ctx, prepared.Stmt)
e.ctx.PreparedPlanCache().Delete(plannercore.NewPlanCacheKey(
vars, id, prepared.SchemaVersion, bindSQL,
vars, id, prepared.SchemaVersion,
))
}
vars.RemovePreparedStmt(id)
Expand Down
8 changes: 4 additions & 4 deletions planner/core/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ type planCacheKey struct {
timezoneOffset int
isolationReadEngines map[kv.StoreType]struct{}
selectLimit uint64
bindSQL string

hash []byte
}
Expand Down Expand Up @@ -132,7 +131,7 @@ func SetPstmtIDSchemaVersion(key kvcache.Key, pstmtID uint32, schemaVersion int6
}

// NewPlanCacheKey creates a new planCacheKey object.
func NewPlanCacheKey(sessionVars *variable.SessionVars, pstmtID uint32, schemaVersion int64, bindSQL string) kvcache.Key {
func NewPlanCacheKey(sessionVars *variable.SessionVars, pstmtID uint32, schemaVersion int64) kvcache.Key {
timezoneOffset := 0
if sessionVars.TimeZone != nil {
_, timezoneOffset = time.Now().In(sessionVars.TimeZone).Zone()
Expand All @@ -146,7 +145,6 @@ func NewPlanCacheKey(sessionVars *variable.SessionVars, pstmtID uint32, schemaVe
timezoneOffset: timezoneOffset,
isolationReadEngines: make(map[kv.StoreType]struct{}),
selectLimit: sessionVars.SelectLimit,
bindSQL: bindSQL,
}
for k, v := range sessionVars.IsolationReadEngines {
key.isolationReadEngines[k] = v
Expand Down Expand Up @@ -185,10 +183,11 @@ type PlanCacheValue struct {
OutPutNames []*types.FieldName
TblInfo2UnionScan map[*model.TableInfo]bool
UserVarTypes FieldSlice
BindSQL string
}

// NewPlanCacheValue creates a SQLCacheValue.
func NewPlanCacheValue(plan Plan, names []*types.FieldName, srcMap map[*model.TableInfo]bool, userVarTps []*types.FieldType) *PlanCacheValue {
func NewPlanCacheValue(plan Plan, names []*types.FieldName, srcMap map[*model.TableInfo]bool, userVarTps []*types.FieldType, bindSQL string) *PlanCacheValue {
dstMap := make(map[*model.TableInfo]bool)
for k, v := range srcMap {
dstMap[k] = v
Expand All @@ -202,6 +201,7 @@ func NewPlanCacheValue(plan Plan, names []*types.FieldName, srcMap map[*model.Ta
OutPutNames: names,
TblInfo2UnionScan: dstMap,
UserVarTypes: userVarTypes,
BindSQL: bindSQL,
}
}

Expand Down
2 changes: 1 addition & 1 deletion planner/core/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ func TestCacheKey(t *testing.T) {
ctx.GetSessionVars().SQLMode = mysql.ModeNone
ctx.GetSessionVars().TimeZone = time.UTC
ctx.GetSessionVars().ConnectionID = 0
key := NewPlanCacheKey(ctx.GetSessionVars(), 1, 1, "")
key := NewPlanCacheKey(ctx.GetSessionVars(), 1, 1)
require.Equal(t, []byte{0x74, 0x65, 0x73, 0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x74, 0x69, 0x64, 0x62, 0x74, 0x69, 0x6b, 0x76, 0x74, 0x69, 0x66, 0x6c, 0x61, 0x73, 0x68, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, key.Hash())
}
16 changes: 10 additions & 6 deletions planner/core/common_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func (e *Execute) getPhysicalPlan(ctx context.Context, sctx sessionctx.Context,
var bindSQL string
if prepared.UseCache {
bindSQL = GetBindSQL4PlanCache(sctx, prepared.Stmt)
cacheKey = NewPlanCacheKey(sctx.GetSessionVars(), e.ExecID, prepared.SchemaVersion, bindSQL)
cacheKey = NewPlanCacheKey(sctx.GetSessionVars(), e.ExecID, prepared.SchemaVersion)
}
tps := make([]*types.FieldType, len(e.UsingVars))
for i, param := range e.UsingVars {
Expand Down Expand Up @@ -447,6 +447,13 @@ func (e *Execute) getPhysicalPlan(ctx context.Context, sctx sessionctx.Context,
}
cachedVals := cacheValue.([]*PlanCacheValue)
for _, cachedVal := range cachedVals {
if cachedVal.BindSQL != bindSQL {
// When BindSQL does not match, it means that we have added a new binding,
// and the original cached plan will be invalid,
// so the original cached plan can be cleared directly
sctx.PreparedPlanCache().Delete(cacheKey)
break
}
if !cachedVal.UserVarTypes.Equal(tps) {
continue
}
Expand Down Expand Up @@ -510,13 +517,10 @@ REBUILD:
// rebuild key to exclude kv.TiFlash when stmt is not read only
if _, isolationReadContainTiFlash := sessVars.IsolationReadEngines[kv.TiFlash]; isolationReadContainTiFlash && !IsReadOnly(stmt, sessVars) {
delete(sessVars.IsolationReadEngines, kv.TiFlash)
cacheKey = NewPlanCacheKey(sessVars, e.ExecID, prepared.SchemaVersion, sessVars.StmtCtx.BindSQL)
cacheKey = NewPlanCacheKey(sessVars, e.ExecID, prepared.SchemaVersion)
sessVars.IsolationReadEngines[kv.TiFlash] = struct{}{}
} else {
// We need to reconstruct the plan cache key based on the bindSQL.
cacheKey = NewPlanCacheKey(sessVars, e.ExecID, prepared.SchemaVersion, sessVars.StmtCtx.BindSQL)
}
cached := NewPlanCacheValue(p, names, stmtCtx.TblInfo2UnionScan, tps)
cached := NewPlanCacheValue(p, names, stmtCtx.TblInfo2UnionScan, tps, sessVars.StmtCtx.BindSQL)
preparedStmt.NormalizedPlan, preparedStmt.PlanDigest = NormalizePlan(p)
stmtCtx.SetPlanDigest(preparedStmt.NormalizedPlan, preparedStmt.PlanDigest)
if cacheVals, exists := sctx.PreparedPlanCache().Get(cacheKey); exists {
Expand Down
4 changes: 1 addition & 3 deletions server/driver_tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,8 @@ func (ts *TiDBStatement) Close() error {
if !ok {
return errors.Errorf("invalid CachedPrepareStmt type")
}
preparedAst := preparedObj.PreparedAst
bindSQL := planner.GetBindSQL4PlanCache(ts.ctx, preparedAst.Stmt)
ts.ctx.PreparedPlanCache().Delete(core.NewPlanCacheKey(
ts.ctx.GetSessionVars(), ts.id, preparedObj.PreparedAst.SchemaVersion, bindSQL))
ts.ctx.GetSessionVars(), ts.id, preparedObj.PreparedAst.SchemaVersion))
}
ts.ctx.GetSessionVars().RemovePreparedStmt(ts.id)
}
Expand Down
3 changes: 1 addition & 2 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,7 @@ func (s *session) cleanRetryInfo() {
preparedObj, ok := preparedPointer.(*plannercore.CachedPrepareStmt)
if ok {
preparedAst = preparedObj.PreparedAst
bindSQL := planner.GetBindSQL4PlanCache(s, preparedAst.Stmt)
cacheKey = plannercore.NewPlanCacheKey(s.sessionVars, firstStmtID, preparedAst.SchemaVersion, bindSQL)
cacheKey = plannercore.NewPlanCacheKey(s.sessionVars, firstStmtID, preparedAst.SchemaVersion)
}
}
}
Expand Down

0 comments on commit b858042

Please sign in to comment.