From c9a797fc621f5604b9ca7aa75d38aa638df7d1f1 Mon Sep 17 00:00:00 2001 From: qw4990 Date: Wed, 8 Mar 2023 16:41:55 +0800 Subject: [PATCH 1/3] fixup --- planner/core/plan_cache.go | 1 + planner/core/plan_cache_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 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..840f00fc2f3f3 100644 --- a/planner/core/plan_cache_test.go +++ b/planner/core/plan_cache_test.go @@ -18,6 +18,8 @@ import ( "context" "errors" "fmt" + "github.com/pingcap/tidb/planner" + "github.com/pingcap/tidb/session" "math/rand" "strconv" "strings" @@ -136,6 +138,34 @@ 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))`) + + 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 * from t where a < 1"), "") + require.Equal(t, planString("select * from t where a < 10"), "") + tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1")) + + require.Equal(t, planString("select * from t where b < 1"), "") + require.Equal(t, planString("select * from t where b < 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) From f8435b2cda84736121e1753370656da467d13270 Mon Sep 17 00:00:00 2001 From: qw4990 Date: Wed, 8 Mar 2023 16:47:11 +0800 Subject: [PATCH 2/3] fixup --- planner/core/plan_cache_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/planner/core/plan_cache_test.go b/planner/core/plan_cache_test.go index 840f00fc2f3f3..d6b85b7b34a2c 100644 --- a/planner/core/plan_cache_test.go +++ b/planner/core/plan_cache_test.go @@ -143,6 +143,7 @@ func TestNonPreparedPlanCachePlanString(t *testing.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 { @@ -157,12 +158,12 @@ func TestNonPreparedPlanCachePlanString(t *testing.T) { return plannercore.ToString(p) } - require.Equal(t, planString("select * from t where a < 1"), "") - require.Equal(t, planString("select * from t where a < 10"), "") + 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)])") tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1")) - require.Equal(t, planString("select * from t where b < 1"), "") - require.Equal(t, planString("select * from t where b < 10"), "") + 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)]))") tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1")) } From 9fa95e036e05204a043cb7dc14ecfce4ce0c03fb Mon Sep 17 00:00:00 2001 From: qw4990 Date: Wed, 8 Mar 2023 16:48:16 +0800 Subject: [PATCH 3/3] fixup --- planner/core/plan_cache_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/planner/core/plan_cache_test.go b/planner/core/plan_cache_test.go index d6b85b7b34a2c..f5f9e6b9d228e 100644 --- a/planner/core/plan_cache_test.go +++ b/planner/core/plan_cache_test.go @@ -18,8 +18,6 @@ import ( "context" "errors" "fmt" - "github.com/pingcap/tidb/planner" - "github.com/pingcap/tidb/session" "math/rand" "strconv" "strings" @@ -28,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" @@ -159,11 +159,11 @@ func TestNonPreparedPlanCachePlanString(t *testing.T) { } 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)])") + 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)]))") + 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")) }