Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#46831
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
qw4990 authored and ti-chi-bot committed Sep 11, 2023
1 parent ae6d59b commit 58fbc94
Show file tree
Hide file tree
Showing 6 changed files with 419 additions and 22 deletions.
7 changes: 7 additions & 0 deletions executor/explainfor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1288,13 +1288,20 @@ func TestCTE4PlanCache(t *testing.T) {
tk.MustExec("insert into t1 values(1);")
tk.MustExec("insert into t1 values(2);")
tk.MustExec("prepare stmt from 'SELECT * FROM t1 dt WHERE EXISTS(WITH RECURSIVE qn AS (SELECT a*? AS b UNION ALL SELECT b+? FROM qn WHERE b=?) SELECT * FROM qn WHERE b=a);';")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 skip prepared plan-cache: find table test.qn failed: [schema:1146]Table 'test.qn' doesn't exist"))
tk.MustExec("set @a=1, @b=2, @c=3, @d=4, @e=5, @f=0;")

tk.MustQuery("execute stmt using @f, @a, @f").Check(testkit.Rows("1"))
tk.MustQuery("execute stmt using @a, @b, @a").Sort().Check(testkit.Rows("1", "2"))
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("0"))
<<<<<<< HEAD
=======
tk.MustQuery("execute stmt using @a, @b, @a").Sort().Check(testkit.Rows("1", "2"))
//tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 skip prepared plan-cache: PhysicalApply plan is un-cacheable"))
>>>>>>> bc80772052f (planner: Adjust the log level and returned value when `cacheableChecker` check `*ast.TableName` nodes (#46831))

tk.MustExec("prepare stmt from 'with recursive c(p) as (select ?), cte(a, b) as (select 1, 1 union select a+?, 1 from cte, c where a < ?) select * from cte order by 1, 2;';")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 skip prepared plan-cache: find table test.cte failed: [schema:1146]Table 'test.cte' doesn't exist"))
tk.MustQuery("execute stmt using @a, @a, @e;").Check(testkit.Rows("1 1", "2 1", "3 1", "4 1", "5 1"))
tk.MustQuery("execute stmt using @b, @b, @c;").Check(testkit.Rows("1 1", "3 1"))
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("0"))
Expand Down
46 changes: 46 additions & 0 deletions planner/core/plan_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,53 @@ import (
"go.uber.org/zap"
)

<<<<<<< HEAD
func planCachePreprocess(ctx context.Context, sctx sessionctx.Context, isGeneralPlanCache bool, is infoschema.InfoSchema, stmt *PlanCacheStmt, params []expression.Expression) error {
=======
var (
// PlanCacheKeyTestIssue43667 is only for test.
PlanCacheKeyTestIssue43667 struct{}
// PlanCacheKeyTestIssue46760 is only for test.
PlanCacheKeyTestIssue46760 struct{}
)

// SetParameterValuesIntoSCtx sets these parameters into session context.
func SetParameterValuesIntoSCtx(sctx sessionctx.Context, isNonPrep bool, markers []ast.ParamMarkerExpr, params []expression.Expression) error {
vars := sctx.GetSessionVars()
vars.PlanCacheParams.Reset()
for i, usingParam := range params {
val, err := usingParam.Eval(chunk.Row{})
if err != nil {
return err
}
if isGetVarBinaryLiteral(sctx, usingParam) {
binVal, convErr := val.ToBytes()
if convErr != nil {
return convErr
}
val.SetBinaryLiteral(binVal)
}
if markers != nil {
param := markers[i].(*driver.ParamMarkerExpr)
param.Datum = val
param.InExecute = true
}
vars.PlanCacheParams.Append(val)
}
if vars.StmtCtx.EnableOptimizerDebugTrace && len(vars.PlanCacheParams.AllParamValues()) > 0 {
vals := vars.PlanCacheParams.AllParamValues()
valStrs := make([]string, len(vals))
for i, val := range vals {
valStrs[i] = val.String()
}
debugtrace.RecordAnyValuesWithNames(sctx, "Parameter datums for EXECUTE", valStrs)
}
vars.PlanCacheParams.SetForNonPrepCache(isNonPrep)
return nil
}

func planCachePreprocess(ctx context.Context, sctx sessionctx.Context, isNonPrepared bool, is infoschema.InfoSchema, stmt *PlanCacheStmt, params []expression.Expression) error {
>>>>>>> bc80772052f (planner: Adjust the log level and returned value when `cacheableChecker` check `*ast.TableName` nodes (#46831))
vars := sctx.GetSessionVars()
stmtAst := stmt.PreparedAst
vars.StmtCtx.StmtType = stmtAst.StmtType
Expand Down
61 changes: 61 additions & 0 deletions planner/core/plan_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,73 @@ func TestIssue41828(t *testing.T) {
KEY U_M_COL4 (COL1,COL2),
KEY U_M_COL5 (COL3,COL2))`)

<<<<<<< HEAD
tk.MustExec(`INSERT INTO IDT_MULTI15840STROBJSTROBJ VALUES ('zzz',1047,'6115-06-05'),('zzz',-23221,'4250-09-03'),('zzz',27138,'1568-07-30'),('zzz',-30903,'6753-08-21'),('zzz',-26875,'6117-10-10')`)
tk.MustExec(`prepare stmt from 'select * from IDT_MULTI15840STROBJSTROBJ where col3 <=> ? or col1 in (?, ?, ?) and col2 not between ? and ?'`)
tk.MustExec(`set @a="0051-12-23", @b="none", @c="none", @d="none", @e=-32757, @f=-32757`)
tk.MustQuery(`execute stmt using @a,@b,@c,@d,@e,@f`).Check(testkit.Rows())
tk.MustExec(`set @a="9795-01-10", @b="aa", @c="aa", @d="aa", @e=31928, @f=31928`)
tk.MustQuery(`execute stmt using @a,@b,@c,@d,@e,@f`).Check(testkit.Rows())
=======
reasons := []string{
"skip non-prepared plan-cache: queries that have hints, having-clause, window-function are not supported",
"skip non-prepared plan-cache: queries that have hints, having-clause, window-function are not supported",
"skip non-prepared plan-cache: queries that have sub-queries are not supported",
"skip non-prepared plan-cache: query accesses partitioned tables is un-cacheable",
"skip non-prepared plan-cache: query accesses partitioned tables is un-cacheable",
"skip non-prepared plan-cache: query has some filters with JSON, Enum, Set or Bit columns",
"skip non-prepared plan-cache: query has some filters with JSON, Enum, Set or Bit columns",
"skip non-prepared plan-cache: query has some filters with JSON, Enum, Set or Bit columns",
"skip non-prepared plan-cache: query has some filters with JSON, Enum, Set or Bit columns",
"skip non-prepared plan-cache: query has some filters with JSON, Enum, Set or Bit columns",
"skip non-prepared plan-cache: query has some filters with JSON, Enum, Set or Bit columns",
"skip non-prepared plan-cache: query has some filters with JSON, Enum, Set or Bit columns",
"skip non-prepared plan-cache: query has some filters with JSON, Enum, Set or Bit columns",
"skip non-prepared plan-cache: access tables in system schema",
"skip non-prepared plan-cache: query accesses generated columns is un-cacheable",
"skip non-prepared plan-cache: query accesses generated columns is un-cacheable",
"skip non-prepared plan-cache: queries that access views are not supported",
"skip non-prepared plan-cache: query has null constants",
"skip non-prepared plan-cache: some parameters may be overwritten when constant propagation",
}

all := append(supported, unsupported...)

explainFormats := []string{
types.ExplainFormatBrief,
types.ExplainFormatDOT,
types.ExplainFormatHint,
types.ExplainFormatROW,
types.ExplainFormatVerbose,
types.ExplainFormatTraditional,
types.ExplainFormatBinary,
types.ExplainFormatTiDBJSON,
types.ExplainFormatCostTrace,
}
// all cases no warnings use other format
for _, q := range all {
tk.MustExec("explain " + q)
tk.MustQuery("show warnings").Check(testkit.Rows())
tk.MustExec("explain " + q)
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
}
for _, format := range explainFormats {
for _, q := range all {
tk.MustExec(fmt.Sprintf("explain format = '%v' %v", format, q))
//tk.MustQuery("show warnings").Check(testkit.Rows())
tk.MustQuery("show warnings").CheckNotContain("plan cache")
tk.MustExec(fmt.Sprintf("explain format = '%v' %v", format, q))
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
}
}

// unsupported case with warning use 'plan_cache' format
for idx, q := range unsupported {
tk.MustExec("explain format = 'plan_cache'" + q)
warn := tk.MustQuery("show warnings").Rows()[0]
require.Equal(t, reasons[idx], warn[2])
}
>>>>>>> bc80772052f (planner: Adjust the log level and returned value when `cacheableChecker` check `*ast.TableName` nodes (#46831))
}

func TestIssue42150(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions planner/core/plan_cache_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,16 @@ func GeneratePlanCacheStmtWithAST(ctx context.Context, sctx sessionctx.Context,
if !vars.EnablePreparedPlanCache {
prepared.UseCache = false
} else {
<<<<<<< HEAD
cacheable, reason := CacheableWithCtx(sctx, stmt, ret.InfoSchema)
prepared.UseCache = cacheable
=======
if isPrepStmt {
cacheable, reason = IsASTCacheable(ctx, sctx, paramStmt, ret.InfoSchema)
} else {
cacheable = true // it is already checked here
}
>>>>>>> bc80772052f (planner: Adjust the log level and returned value when `cacheableChecker` check `*ast.TableName` nodes (#46831))
if !cacheable {
sctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("skip plan-cache: " + reason))
}
Expand Down
Loading

0 comments on commit 58fbc94

Please sign in to comment.