From ca3be24fa5048e397fefb18f2417ed0b2379b6ab Mon Sep 17 00:00:00 2001 From: qw4990 Date: Wed, 15 Feb 2023 17:37:17 +0800 Subject: [PATCH 1/4] fixup --- planner/core/plan_cache_test.go | 20 ++++++++++++++++++++ planner/optimize.go | 6 ++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/planner/core/plan_cache_test.go b/planner/core/plan_cache_test.go index 87d0d75c71742..e829f57d3ab60 100644 --- a/planner/core/plan_cache_test.go +++ b/planner/core/plan_cache_test.go @@ -320,6 +320,26 @@ func TestNonPreparedPlanCacheBasically(t *testing.T) { } } +func TestNonPreparedPlanCacheInternalSQL(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec(`use test`) + tk.MustExec("create table t(a int, index(a))") + tk.MustExec("set tidb_enable_non_prepared_plan_cache=1") + + tk.MustExec("select * from t where a=1") + tk.MustExec("select * from t where a=1") + tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1")) + + tk.Session().GetSessionVars().InRestrictedSQL = true + tk.MustExec("select * from t where a=1") + tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0")) + + tk.Session().GetSessionVars().InRestrictedSQL = false + tk.MustExec("select * from t where a=1") + tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1")) +} + func TestIssue38269(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) diff --git a/planner/optimize.go b/planner/optimize.go index 1ccd1c52f307b..d9dc6c255f201 100644 --- a/planner/optimize.go +++ b/planner/optimize.go @@ -75,8 +75,10 @@ func matchSQLBinding(sctx sessionctx.Context, stmtNode ast.StmtNode) (bindRecord // getPlanFromNonPreparedPlanCache tries to get an available cached plan from the NonPrepared Plan Cache for this stmt. func getPlanFromNonPreparedPlanCache(ctx context.Context, sctx sessionctx.Context, stmt ast.StmtNode, is infoschema.InfoSchema) (p core.Plan, ns types.NameSlice, ok bool, err error) { - if sctx.GetSessionVars().StmtCtx.InPreparedPlanBuilding || // already in cached plan rebuilding phase - !core.NonPreparedPlanCacheableWithCtx(sctx, stmt, is) { + if !sctx.GetSessionVars().EnableNonPreparedPlanCache || // disabled + sctx.GetSessionVars().StmtCtx.InPreparedPlanBuilding || // already in cached plan rebuilding phase + sctx.GetSessionVars().StmtCtx.InRestrictedSQL || // is internal SQL + !core.NonPreparedPlanCacheableWithCtx(sctx, stmt, is) { // not support return nil, nil, false, nil } paramSQL, params, err := core.ParameterizeAST(ctx, sctx, stmt) From 2c66417875a3f524ecc1d1ed3161375d9b5f34ad Mon Sep 17 00:00:00 2001 From: qw4990 Date: Wed, 15 Feb 2023 17:59:38 +0800 Subject: [PATCH 2/4] fixup --- planner/core/plan_cache_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/planner/core/plan_cache_test.go b/planner/core/plan_cache_test.go index e829f57d3ab60..02be7629f2e38 100644 --- a/planner/core/plan_cache_test.go +++ b/planner/core/plan_cache_test.go @@ -18,6 +18,7 @@ import ( "context" "errors" "fmt" + "github.com/pingcap/tidb/kv" "math/rand" "strconv" "strings" @@ -331,9 +332,10 @@ func TestNonPreparedPlanCacheInternalSQL(t *testing.T) { tk.MustExec("select * from t where a=1") tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1")) + ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnOthers) tk.Session().GetSessionVars().InRestrictedSQL = true - tk.MustExec("select * from t where a=1") - tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0")) + tk.MustExecWithContext(ctx, "select * from t where a=1") + tk.MustQueryWithContext(ctx, "select @@last_plan_from_cache").Check(testkit.Rows("0")) tk.Session().GetSessionVars().InRestrictedSQL = false tk.MustExec("select * from t where a=1") From 3744c68a6465abaa1a866ade295a880f37c70462 Mon Sep 17 00:00:00 2001 From: qw4990 Date: Wed, 15 Feb 2023 18:03:40 +0800 Subject: [PATCH 3/4] fixup --- planner/core/plan_cache_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/planner/core/plan_cache_test.go b/planner/core/plan_cache_test.go index 02be7629f2e38..8d310b2aac350 100644 --- a/planner/core/plan_cache_test.go +++ b/planner/core/plan_cache_test.go @@ -342,6 +342,22 @@ func TestNonPreparedPlanCacheInternalSQL(t *testing.T) { tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1")) } +func TestNonPreparedPlanCacheSelectLimit(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec(`use test`) + tk.MustExec("create table t(a int, index(a))") + tk.MustExec("set tidb_enable_non_prepared_plan_cache=1") + + tk.MustExec("select * from t where a=1") + tk.MustExec("select * from t where a=1") + tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1")) + + tk.MustExec("set @@session.sql_select_limit=1") + tk.MustExec("select * from t where a=1") + tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0")) +} + func TestIssue38269(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) From 3f1ba19a10e73c93dfe68ba25be6a3064a5cc489 Mon Sep 17 00:00:00 2001 From: qw4990 Date: Wed, 15 Feb 2023 18:05:20 +0800 Subject: [PATCH 4/4] fixup --- planner/core/plan_cache_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planner/core/plan_cache_test.go b/planner/core/plan_cache_test.go index 8d310b2aac350..0bb27c5623a08 100644 --- a/planner/core/plan_cache_test.go +++ b/planner/core/plan_cache_test.go @@ -18,13 +18,13 @@ import ( "context" "errors" "fmt" - "github.com/pingcap/tidb/kv" "math/rand" "strconv" "strings" "testing" "github.com/pingcap/tidb/expression" + "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/parser/mysql" plannercore "github.com/pingcap/tidb/planner/core" "github.com/pingcap/tidb/testkit"