Skip to content

Commit

Permalink
planner: add more test cases for non-prep plan cache (#42015)
Browse files Browse the repository at this point in the history
ref #36598
  • Loading branch information
qw4990 authored and pull[bot] committed Feb 6, 2024
1 parent 2d944ef commit 2748779
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions planner/core/plan_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
31 changes: 31 additions & 0 deletions planner/core/plan_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2748779

Please sign in to comment.