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: fix small regression caused by #53094 #53646

Merged
merged 4 commits into from
May 29, 2024
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
32 changes: 18 additions & 14 deletions pkg/planner/core/plan_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,30 +215,34 @@ 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 bool
if stmt.PointGet.Executor != nil { // if it's PointGet Plan, no need to use MatchOpts
cacheVal, hit = sctx.GetSessionPlanCache().Get(cacheKey, nil)
} 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, 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 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
}
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