From fc55d1236c3f08e798040e23630554f10b770c65 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Fri, 30 Dec 2022 10:20:17 +0800 Subject: [PATCH] This is an automated cherry-pick of #40235 Signed-off-by: ti-chi-bot --- expression/builtin_compare.go | 2 +- expression/util.go | 2 +- planner/core/expression_rewriter.go | 5 +++++ planner/core/plan_cache.go | 4 ++-- sessionctx/stmtctx/stmtctx.go | 13 ++++++++++++- 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/expression/builtin_compare.go b/expression/builtin_compare.go index 4411f4b6b90ff..6b97258fb29c9 100644 --- a/expression/builtin_compare.go +++ b/expression/builtin_compare.go @@ -1585,7 +1585,7 @@ func (c *compareFunctionClass) refineArgs(ctx sessionctx.Context, args []Express } else { return args } - } else if ctx.GetSessionVars().StmtCtx.SkipPlanCache { + } else if !ctx.GetSessionVars().StmtCtx.UseCache { // We should remove the mutable constant for correctness, because its value may be changed. RemoveMutableConst(ctx, args) } diff --git a/expression/util.go b/expression/util.go index 22637d1b8617f..b7e66b97b24e8 100644 --- a/expression/util.go +++ b/expression/util.go @@ -1239,7 +1239,7 @@ func ContainCorrelatedColumn(exprs []Expression) bool { // TODO: Do more careful check here. func MaybeOverOptimized4PlanCache(ctx sessionctx.Context, exprs []Expression) bool { // If we do not enable plan cache, all the optimization can work correctly. - if !ctx.GetSessionVars().StmtCtx.UseCache || ctx.GetSessionVars().StmtCtx.SkipPlanCache { + if !ctx.GetSessionVars().StmtCtx.UseCache { return false } return containMutableConst(ctx, exprs) diff --git a/planner/core/expression_rewriter.go b/planner/core/expression_rewriter.go index 4b577b88a91ec..2097df5cbb42f 100644 --- a/planner/core/expression_rewriter.go +++ b/planner/core/expression_rewriter.go @@ -1553,8 +1553,13 @@ func (er *expressionRewriter) inToExpression(lLen int, not bool, tp *types.Field if c.GetType().EvalType() == types.ETInt { continue // no need to refine it } +<<<<<<< HEAD er.sctx.GetSessionVars().StmtCtx.SkipPlanCache = true er.sctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("skip plan-cache: '%v' may be converted to INT", c.String())) +======= + } else if !er.sctx.GetSessionVars().StmtCtx.UseCache { + // We should remove the mutable constant for correctness, because its value may be changed. +>>>>>>> ffaf2ac9eee (planner: remove the unnecessary skip-plan-cache flag in StmtCtx (#40235)) expression.RemoveMutableConst(er.sctx, []expression.Expression{c}) } args[i], isExceptional = expression.RefineComparedConstant(er.sctx, *leftFt, c, opcode.EQ) diff --git a/planner/core/plan_cache.go b/planner/core/plan_cache.go index e58e8b6d91708..31655bb498b7a 100644 --- a/planner/core/plan_cache.go +++ b/planner/core/plan_cache.go @@ -276,7 +276,7 @@ func generateNewPlan(ctx context.Context, sctx sessionctx.Context, isGeneralPlan if containTableDual(p) && paramNum > 0 { stmtCtx.SkipPlanCache = true } - if stmtAst.UseCache && !stmtCtx.SkipPlanCache && !ignorePlanCache { + if stmtCtx.UseCache && !ignorePlanCache { // rebuild key to exclude kv.TiFlash when stmt is not read only if _, isolationReadContainTiFlash := sessVars.IsolationReadEngines[kv.TiFlash]; isolationReadContainTiFlash && !IsReadOnly(stmtAst.Stmt, sessVars) { delete(sessVars.IsolationReadEngines, kv.TiFlash) @@ -636,7 +636,7 @@ func CheckPreparedPriv(sctx sessionctx.Context, stmt *PlanCacheStmt, is infosche // short paths for these executions, currently "point select" and "point update" func tryCachePointPlan(_ context.Context, sctx sessionctx.Context, stmt *PlanCacheStmt, _ infoschema.InfoSchema, p Plan) error { - if !sctx.GetSessionVars().StmtCtx.UseCache || sctx.GetSessionVars().StmtCtx.SkipPlanCache { + if !sctx.GetSessionVars().StmtCtx.UseCache { return nil } var ( diff --git a/sessionctx/stmtctx/stmtctx.go b/sessionctx/stmtctx/stmtctx.go index ec30fcb5fee35..9591d065d475b 100644 --- a/sessionctx/stmtctx/stmtctx.go +++ b/sessionctx/stmtctx/stmtctx.go @@ -171,7 +171,6 @@ type StatementContext struct { InNullRejectCheck bool AllowInvalidDate bool IgnoreNoPartition bool - SkipPlanCache bool IgnoreExplainIDSuffix bool SkipUTF8Check bool SkipASCIICheck bool @@ -597,6 +596,18 @@ func (sc *StatementContext) SetPlanHint(hint string) { sc.planHint = hint } +<<<<<<< HEAD +======= +// SetSkipPlanCache sets to skip the plan cache and records the reason. +func (sc *StatementContext) SetSkipPlanCache(reason error) { + if !sc.UseCache { + return // avoid unnecessary warnings + } + sc.UseCache = false + sc.AppendWarning(reason) +} + +>>>>>>> ffaf2ac9eee (planner: remove the unnecessary skip-plan-cache flag in StmtCtx (#40235)) // TableEntry presents table in db. type TableEntry struct { DB string