Skip to content

Commit

Permalink
planner: rename session plan cache interface (pingcap#54102)
Browse files Browse the repository at this point in the history
  • Loading branch information
qw4990 authored Jun 19, 2024
1 parent 51ccce2 commit d0e775d
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 14 deletions.
7 changes: 4 additions & 3 deletions pkg/planner/core/plan_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ func planCachePreprocess(ctx context.Context, sctx sessionctx.Context, isNonPrep
return nil
}

// GetPlanFromSessionPlanCache is the entry point of Plan Cache.
// It tries to get a valid cached plan from this session's plan cache.
// GetPlanFromPlanCache is the entry point of Plan Cache.
// It tries to get a valid cached plan from plan cache.
// If there is no such a plan, it'll call the optimizer to generate a new one.
// isNonPrepared indicates whether to use the non-prepared plan cache or the prepared plan cache.
func GetPlanFromSessionPlanCache(ctx context.Context, sctx sessionctx.Context,
func GetPlanFromPlanCache(ctx context.Context, sctx sessionctx.Context,
isNonPrepared bool, is infoschema.InfoSchema, stmt *PlanCacheStmt,
params []expression.Expression) (plan base.Plan, names []*types.FieldName, err error) {
if err := planCachePreprocess(ctx, sctx, isNonPrepared, is, stmt, params); err != nil {
Expand Down Expand Up @@ -230,6 +230,7 @@ func GetPlanFromSessionPlanCache(ctx context.Context, sctx sessionctx.Context,
isPointPlan, hit = true, true
} else {
matchOpts = GetMatchOpts(sctx, is, stmt, params)
// TODO: consider instance-level plan cache
cacheVal, hit = sctx.GetSessionPlanCache().Get(cacheKey, matchOpts)
}
if hit {
Expand Down
2 changes: 1 addition & 1 deletion pkg/planner/core/tests/prepare/prepare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ func TestPrepareCacheDeferredFunction(t *testing.T) {
require.True(t, ok)
err = executor.ResetContextOfStmt(tk.Session(), stmt)
require.NoError(t, err)
plan, _, err := core.GetPlanFromSessionPlanCache(ctx, tk.Session(), false, is, execPlan.PrepStmt, execPlan.Params)
plan, _, err := core.GetPlanFromPlanCache(ctx, tk.Session(), false, is, execPlan.PrepStmt, execPlan.Params)
require.NoError(t, err)
planStr[i] = core.ToString(plan)
require.Regexpf(t, expectedPattern, planStr[i], "for %dth %s", i, sql1)
Expand Down
4 changes: 2 additions & 2 deletions pkg/planner/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func getPlanFromNonPreparedPlanCache(ctx context.Context, sctx sessionctx.Contex
}
cachedStmt := val.(*core.PlanCacheStmt)

cachedPlan, names, err := core.GetPlanFromSessionPlanCache(ctx, sctx, true, is, cachedStmt, paramExprs)
cachedPlan, names, err := core.GetPlanFromPlanCache(ctx, sctx, true, is, cachedStmt, paramExprs)
if err != nil {
return nil, nil, false, err
}
Expand Down Expand Up @@ -535,7 +535,7 @@ func OptimizeExecStmt(ctx context.Context, sctx sessionctx.Context,
if !ok {
return nil, nil, errors.Errorf("invalid result plan type, should be Execute")
}
plan, names, err := core.GetPlanFromSessionPlanCache(ctx, sctx, false, is, exec.PrepStmt, exec.Params)
plan, names, err := core.GetPlanFromPlanCache(ctx, sctx, false, is, exec.PrepStmt, exec.Params)
if err != nil {
return nil, nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ type session struct {

store kv.Storage

sessionPlanCache sessionctx.PlanCache
sessionPlanCache sessionctx.SessionPlanCache

sessionVars *variable.SessionVars
sessionManager util.SessionManager
Expand Down Expand Up @@ -390,7 +390,7 @@ func (s *session) SetCollation(coID int) error {
return s.sessionVars.SetSystemVarWithoutValidation(variable.CollationConnection, co)
}

func (s *session) GetSessionPlanCache() sessionctx.PlanCache {
func (s *session) GetSessionPlanCache() sessionctx.SessionPlanCache {
// use the prepared plan cache
if !s.GetSessionVars().EnablePreparedPlanCache && !s.GetSessionVars().EnableNonPreparedPlanCache {
return nil
Expand Down Expand Up @@ -3061,7 +3061,7 @@ func CreateSession4Test(store kv.Storage) (types.Session, error) {

// Opt describes the option for creating session
type Opt struct {
PreparedPlanCache sessionctx.PlanCache
PreparedPlanCache sessionctx.SessionPlanCache
}

// CreateSession4TestWithOpt creates a new session environment for test.
Expand Down
6 changes: 3 additions & 3 deletions pkg/sessionctx/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ type SessionStatesHandler interface {
DecodeSessionStates(context.Context, Context, *sessionstates.SessionStates) error
}

// PlanCache is an interface for prepare and non-prepared plan cache
type PlanCache interface {
// SessionPlanCache is an interface for prepare and non-prepared plan cache
type SessionPlanCache interface {
Get(key string, opts any) (value any, ok bool)
Put(key string, value, opts any)
Delete(key string)
Expand Down Expand Up @@ -131,7 +131,7 @@ type Context interface {
GetStore() kv.Storage

// GetSessionPlanCache returns the session-level cache of the physical plan.
GetSessionPlanCache() PlanCache
GetSessionPlanCache() SessionPlanCache

// UpdateColStatsUsage updates the column stats usage.
UpdateColStatsUsage(predicateColumns []model.TableItemID)
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/mock/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type Context struct {
sessionVars *variable.SessionVars
tblctx *tbctximpl.TableContextImpl
cancel context.CancelFunc
pcache sessionctx.PlanCache
pcache sessionctx.SessionPlanCache
level kvrpcpb.DiskFullOpt
inSandBoxMode bool
isDDLOwner bool
Expand Down Expand Up @@ -384,7 +384,7 @@ func (*Context) SetGlobalSysVar(_ sessionctx.Context, name string, value string)
}

// GetSessionPlanCache implements the sessionctx.Context interface.
func (c *Context) GetSessionPlanCache() sessionctx.PlanCache {
func (c *Context) GetSessionPlanCache() sessionctx.SessionPlanCache {
return c.pcache
}

Expand Down

0 comments on commit d0e775d

Please sign in to comment.