From 2748779da68a157a18bd405d55acdeb3cc387665 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Wed, 8 Mar 2023 19:37:28 +0800 Subject: [PATCH] planner: add more test cases for non-prep plan cache (#42015) ref pingcap/tidb#36598 --- planner/core/plan_cache.go | 1 + planner/core/plan_cache_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/planner/core/plan_cache.go b/planner/core/plan_cache.go index 69d3ecbbef3f2..b894ae0d977b0 100644 --- a/planner/core/plan_cache.go +++ b/planner/core/plan_cache.go @@ -53,6 +53,7 @@ func planCachePreprocess(ctx context.Context, sctx sessionctx.Context, isNonPrep } // step 2: set parameter values + vars.PreparedParams = vars.PreparedParams[:0] for i, usingParam := range params { val, err := usingParam.Eval(chunk.Row{}) if err != nil { diff --git a/planner/core/plan_cache_test.go b/planner/core/plan_cache_test.go index 78f73a7fe9ef0..f5f9e6b9d228e 100644 --- a/planner/core/plan_cache_test.go +++ b/planner/core/plan_cache_test.go @@ -26,7 +26,9 @@ import ( "github.com/pingcap/tidb/expression" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/parser/mysql" + "github.com/pingcap/tidb/planner" plannercore "github.com/pingcap/tidb/planner/core" + "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/testkit" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util" @@ -136,6 +138,35 @@ func TestIssue40296(t *testing.T) { tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0")) // unary operator '-' is not supported now. } +func TestNonPreparedPlanCachePlanString(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec(`use test`) + tk.MustExec(`create table t (a int, b int, key(a))`) + tk.MustExec(`set @@tidb_enable_non_prepared_plan_cache=1`) + + ctx := tk.Session() + planString := func(sql string) string { + stmts, err := session.Parse(ctx, sql) + require.NoError(t, err) + stmt := stmts[0] + ret := &plannercore.PreprocessorReturn{} + err = plannercore.Preprocess(context.Background(), ctx, stmt, plannercore.WithPreprocessorReturn(ret)) + require.NoError(t, err) + p, _, err := planner.Optimize(context.TODO(), ctx, stmt, ret.InfoSchema) + require.NoError(t, err) + return plannercore.ToString(p) + } + + require.Equal(t, planString("select a from t where a < 1"), "IndexReader(Index(t.a)[[-inf,1)])") + require.Equal(t, planString("select a from t where a < 10"), "IndexReader(Index(t.a)[[-inf,10)])") // range 1 -> 10 + tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1")) + + require.Equal(t, planString("select * from t where b < 1"), "TableReader(Table(t)->Sel([lt(test.t.b, 1)]))") + require.Equal(t, planString("select * from t where b < 10"), "TableReader(Table(t)->Sel([lt(test.t.b, 10)]))") // filter 1 -> 10 + tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1")) +} + func TestNonPreparedPlanCacheJSONFilter(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store)