Skip to content

Commit

Permalink
planner: add more test cases for non-prep cache (#41645)
Browse files Browse the repository at this point in the history
ref #36598
  • Loading branch information
qw4990 authored Feb 22, 2023
1 parent 484aa03 commit 5b0315e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions expression/function_traits.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var NonPreparedPlanCacheableOp = map[string]struct{}{
ast.EQ: {},
ast.LT: {},
ast.GT: {},
ast.In: {},
}

// UnCacheableFunctions stores functions which can not be cached to plan cache.
Expand Down
10 changes: 9 additions & 1 deletion planner/core/plan_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func TestNonPreparedPlanCacheBasically(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, c int, d int, primary key(a), key(b), key(c, d))`)
tk.MustExec(`create table t (a int, b int, c int, d int, key(b), key(c, d))`)
for i := 0; i < 20; i++ {
tk.MustExec(fmt.Sprintf("insert into t values (%v, %v, %v, %v)", i, rand.Intn(20), rand.Intn(20), rand.Intn(20)))
}
Expand All @@ -468,6 +468,14 @@ func TestNonPreparedPlanCacheBasically(t *testing.T) {
"select * from t where d>8",
"select * from t where c=8 and d>10",
"select * from t where a<12 and b<13 and c<12 and d>2",
"select * from t where a in (1, 2, 3)",
"select * from t where a<13 or b<15",
"select * from t where a<13 or b<15 and c=13",
"select * from t where a in (1, 2)",
"select * from t where a in (1, 2) and b in (1, 2, 3)",
"select * from t where a in (1, 2) and b < 15",
"select * from t where a between 1 and 10",
"select * from t where a between 1 and 10 and b < 15",
}

for _, query := range queries {
Expand Down
2 changes: 1 addition & 1 deletion planner/core/plan_cacheable_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ type nonPreparedPlanCacheableChecker struct {
// Enter implements Visitor interface.
func (checker *nonPreparedPlanCacheableChecker) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
switch node := in.(type) {
case *ast.SelectStmt, *ast.FieldList, *ast.SelectField, *ast.TableRefsClause, *ast.Join,
case *ast.SelectStmt, *ast.FieldList, *ast.SelectField, *ast.TableRefsClause, *ast.Join, *ast.BetweenExpr,
*ast.TableSource, *ast.ColumnNameExpr, *ast.ColumnName, *driver.ValueExpr, *ast.PatternInExpr:
return in, !checker.cacheable // skip child if un-cacheable
case *ast.BinaryOperationExpr:
Expand Down
8 changes: 8 additions & 0 deletions planner/core/plan_cacheable_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ func TestNonPreparedPlanCacheable(t *testing.T) {
"select * from t where a in (1, 2, 3)",
"select * from t where a<13 or b<15",
"select * from t where a<13 or b<15 and c=13",
"select * from t where a in (1, 2)",
"select * from t where a in (1, 2) and b in (1, 2, 3)",
"select * from t where a in (1, 2) and b < 15",
"select * from t where a between 1 and 10",
"select * from t where a between 1 and 10 and b < 15",
}

unsupported := []string{
Expand All @@ -306,6 +311,9 @@ func TestNonPreparedPlanCacheable(t *testing.T) {
"insert into t1(a, b) select a, b from t1", // insert into select
"update t1 set a = 1 where b = 2", // update
"delete from t1 where b = 1", // delete
"select * from t1 for update", // lock
"select * from t1 where a in (select a from t)", // uncorrelated sub-query
"select * from t1 where a in (select a from t where a > t1.a)", // correlated sub-query

"select * from t where a+b=13", // '+'
"select * from t where mod(a, 3)=1", // mod
Expand Down

0 comments on commit 5b0315e

Please sign in to comment.