Skip to content

Commit

Permalink
planner: fix small regression caused by #53094 (#53646)
Browse files Browse the repository at this point in the history
ref #50618
  • Loading branch information
qw4990 authored May 29, 2024
1 parent da49e13 commit 87999d1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
39 changes: 23 additions & 16 deletions pkg/planner/core/plan_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,32 +215,39 @@ func GetPlanFromSessionPlanCache(ctx context.Context, sctx sessionctx.Context,
}
}

matchOpts, err := GetMatchOpts(sctx, is, stmt, params)
if err != nil {
return nil, nil, err
}
var matchOpts *utilpc.PlanCacheMatchOpts
if stmtCtx.UseCache() {
if plan, names, ok, err := getCachedPlan(sctx, isNonPrepared, cacheKey, bindSQL, is, stmt, matchOpts); err != nil || ok {
return plan, names, err
var cacheVal kvcache.Value
var hit, isPointPlan bool
if stmt.PointGet.Executor != nil { // if it's PointGet Plan, no need to use MatchOpts
cacheVal, hit = sctx.GetSessionPlanCache().Get(cacheKey, nil)
isPointPlan = true
} else {
matchOpts = GetMatchOpts(sctx, is, stmt, params)
cacheVal, hit = sctx.GetSessionPlanCache().Get(cacheKey, matchOpts)
}
if hit {
if plan, names, ok, err := adjustCachedPlan(sctx, cacheVal.(*PlanCacheValue), isNonPrepared, isPointPlan, cacheKey, bindSQL, is, stmt); err != nil || ok {
return plan, names, err
}
}
}
if matchOpts == nil {
matchOpts = GetMatchOpts(sctx, is, stmt, params)
}

return generateNewPlan(ctx, sctx, isNonPrepared, is, stmt, cacheKey, latestSchemaVersion, bindSQL, matchOpts)
}

func getCachedPlan(sctx sessionctx.Context, isNonPrepared bool, cacheKey kvcache.Key, bindSQL string,
is infoschema.InfoSchema, stmt *PlanCacheStmt, matchOpts *utilpc.PlanCacheMatchOpts) (base.Plan,
func adjustCachedPlan(sctx sessionctx.Context, cachedVal *PlanCacheValue, isNonPrepared, isPointPlan bool,
cacheKey kvcache.Key, bindSQL string, is infoschema.InfoSchema, stmt *PlanCacheStmt) (base.Plan,
[]*types.FieldName, bool, error) {
sessVars := sctx.GetSessionVars()
stmtCtx := sessVars.StmtCtx

candidate, exist := sctx.GetSessionPlanCache().Get(cacheKey, matchOpts)
if !exist {
return nil, nil, false, nil
}
cachedVal := candidate.(*PlanCacheValue)
if err := checkPreparedPriv(sctx, stmt, is); err != nil {
return nil, nil, false, err
if !isPointPlan { // keep the prior behavior
if err := checkPreparedPriv(sctx, stmt, is); err != nil {
return nil, nil, false, err
}
}
for tblInfo, unionScan := range cachedVal.TblInfo2UnionScan {
if !unionScan && tableHasDirtyContent(sctx.GetPlanCtx(), tblInfo) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/planner/core/plan_cache_lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ func (l *LRUPlanCache) memoryControl() {
// PickPlanFromBucket pick one plan from bucket
func (l *LRUPlanCache) pickFromBucket(bucket map[*list.Element]struct{}, matchOpts *utilpc.PlanCacheMatchOpts) (*list.Element, bool) {
for k := range bucket {
if matchOpts == nil { // for PointGet Plan
return k, true
}

plan := k.Value.(*planCacheEntry).PlanValue.(*PlanCacheValue)
// check param types' compatibility
ok1 := checkTypesCompatibility4PC(plan.matchOpts.ParamTypes, matchOpts.ParamTypes)
Expand Down
4 changes: 2 additions & 2 deletions pkg/planner/core/plan_cache_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ func GetPreparedStmt(stmt *ast.ExecuteStmt, vars *variable.SessionVars) (*PlanCa

// GetMatchOpts get options to fetch plan or generate new plan
// we can add more options here
func GetMatchOpts(sctx sessionctx.Context, is infoschema.InfoSchema, stmt *PlanCacheStmt, params []expression.Expression) (*utilpc.PlanCacheMatchOpts, error) {
func GetMatchOpts(sctx sessionctx.Context, is infoschema.InfoSchema, stmt *PlanCacheStmt, params []expression.Expression) *utilpc.PlanCacheMatchOpts {
var statsVerHash uint64
var limitOffsetAndCount []uint64

Expand Down Expand Up @@ -606,7 +606,7 @@ func GetMatchOpts(sctx sessionctx.Context, is infoschema.InfoSchema, stmt *PlanC
StatsVersionHash: statsVerHash,
ParamTypes: parseParamTypes(sctx, params),
ForeignKeyChecks: sctx.GetSessionVars().ForeignKeyChecks,
}, nil
}
}

// CheckTypesCompatibility4PC compares FieldSlice with []*types.FieldType
Expand Down

0 comments on commit 87999d1

Please sign in to comment.