Skip to content

Commit

Permalink
Merge branch 'master' into wenxuan/alter-compact-table
Browse files Browse the repository at this point in the history
  • Loading branch information
breezewish authored May 18, 2022
2 parents 1df411e + 381e870 commit 9a0cac5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
6 changes: 3 additions & 3 deletions planner/core/common_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ func (e *Execute) getPhysicalPlan(ctx context.Context, sctx sessionctx.Context,
}
}

if prepared.CachedPlan != nil {
if prepared.UseCache && prepared.CachedPlan != nil { // short path for point-get plans
// Rewriting the expression in the select.where condition will convert its
// type from "paramMarker" to "Constant".When Point Select queries are executed,
// the expression in the where condition will not be evaluated,
Expand All @@ -504,7 +504,7 @@ func (e *Execute) getPhysicalPlan(ctx context.Context, sctx sessionctx.Context,
stmtCtx.PointExec = true
return nil
}
if prepared.UseCache {
if prepared.UseCache { // for general plans
if cacheValue, exists := sctx.PreparedPlanCache().Get(cacheKey); exists {
if err := e.checkPreparedPriv(ctx, sctx, preparedStmt, is); err != nil {
return err
Expand Down Expand Up @@ -633,7 +633,7 @@ func containTableDual(p Plan) bool {
// short paths for these executions, currently "point select" and "point update"
func (e *Execute) tryCachePointPlan(ctx context.Context, sctx sessionctx.Context,
preparedStmt *CachedPrepareStmt, is infoschema.InfoSchema, p Plan) error {
if sctx.GetSessionVars().StmtCtx.SkipPlanCache {
if !sctx.GetSessionVars().StmtCtx.UseCache || sctx.GetSessionVars().StmtCtx.SkipPlanCache {
return nil
}
var (
Expand Down
47 changes: 47 additions & 0 deletions planner/core/prepare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,53 @@ func TestIssue29303(t *testing.T) {
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
}

func TestIssue34725(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
orgEnable := core.PreparedPlanCacheEnabled()
defer core.SetPreparedPlanCache(orgEnable)
core.SetPreparedPlanCache(true)
se, err := session.CreateSession4TestWithOpt(store, &session.Opt{
PreparedPlanCache: kvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
})
require.NoError(t, err)
tk := testkit.NewTestKitWithSession(t, store, se)

tk.MustExec(`use test`)
tk.MustExec(`drop table if exists t`)
tk.MustExec(`CREATE TABLE t (
a int(11) DEFAULT NULL,
b int(11) GENERATED ALWAYS AS (a) STORED NOT NULL,
PRIMARY KEY (b))`)
tk.MustExec(`insert into t(a) values(102)`)
tk.MustExec(`prepare stmt from "select * from t where b in (?, ?, ?)"`)
tk.MustExec(`set @a=102, @b=102, @c=102`)
tk.MustQuery(`execute stmt using @a,@b,@c`).Check(testkit.Rows(`102 102`))
tk.MustExec(`set @a=-97, @b=-97, @c=-97`)
tk.MustQuery(`execute stmt using @a,@b,@c`).Check(testkit.Rows())
}

func TestIssue33628(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
orgEnable := core.PreparedPlanCacheEnabled()
defer core.SetPreparedPlanCache(orgEnable)
core.SetPreparedPlanCache(false)
se, err := session.CreateSession4TestWithOpt(store, &session.Opt{
PreparedPlanCache: kvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
})
require.NoError(t, err)
tk := testkit.NewTestKitWithSession(t, store, se)

tk.MustExec(`use test`)
tk.MustExec(`drop table if exists t`)
tk.MustExec(`create table t (a int primary key, b int)`)
tk.MustExec(`prepare stmt from "select * from t where a=10"`) // point-get plan
tk.MustExec(`execute stmt`)
tk.MustExec(`execute stmt`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
}

func TestIssue28942(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
Expand Down
7 changes: 5 additions & 2 deletions sessiontxn/txn_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessiontxn"
"github.com/pingcap/tidb/testkit"
Expand Down Expand Up @@ -523,6 +524,9 @@ func TestTxnContextForStaleRead(t *testing.T) {
func TestTxnContextForPrepareExecute(t *testing.T) {
store, do, deferFunc := setupTxnContextTest(t)
defer deferFunc()
orgEnable := core.PreparedPlanCacheEnabled()
defer core.SetPreparedPlanCache(orgEnable)
core.SetPreparedPlanCache(true)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
se := tk.Session()
Expand Down Expand Up @@ -550,8 +554,7 @@ func TestTxnContextForPrepareExecute(t *testing.T) {
})

// Test PlanCache
path = []string{"assertTxnManagerInCachedPlanExec", "assertTxnManagerInShortPointGetPlan"}
doWithCheckPath(t, se, path, func() {
doWithCheckPath(t, se, nil, func() {
rs, err := se.ExecutePreparedStmt(context.TODO(), stmtID, nil)
require.NoError(t, err)
tk.ResultSetToResult(rs, fmt.Sprintf("%v", rs)).Check(testkit.Rows("1 10"))
Expand Down

0 comments on commit 9a0cac5

Please sign in to comment.