Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: add more test cases for non-prepared plan cache #41451

Merged
merged 4 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions planner/core/plan_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"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"
Expand Down Expand Up @@ -320,6 +321,43 @@ 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"))

ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnOthers)
tk.Session().GetSessionVars().InRestrictedSQL = true
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")
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)
Expand Down
6 changes: 4 additions & 2 deletions planner/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down