Skip to content

Commit

Permalink
planner: add more test cases for non-prep plan cache (#42901)
Browse files Browse the repository at this point in the history
ref #36598
  • Loading branch information
qw4990 authored Apr 10, 2023
1 parent 856648a commit c282c9c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
2 changes: 2 additions & 0 deletions expression/function_traits.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ var UnCacheableFunctions = map[string]struct{}{
ast.TimeLiteral: {},
ast.DateLiteral: {},
ast.TimestampLiteral: {},
ast.AesEncrypt: {}, // affected by @@block_encryption_mode
ast.AesDecrypt: {},
}

// unFoldableFunctions stores functions which can not be folded duration constant folding stage.
Expand Down
6 changes: 3 additions & 3 deletions planner/core/plan_cache_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var (
paramCtxPool = sync.Pool{New: func() interface{} {
buf := new(strings.Builder)
buf.Reset()
restoreCtx := format.NewRestoreCtx(format.RestoreForNonPrepPlanCache, buf)
restoreCtx := format.NewRestoreCtx(format.RestoreForNonPrepPlanCache|format.RestoreStringWithoutCharset, buf)
return restoreCtx
}}
)
Expand All @@ -55,10 +55,10 @@ type paramReplacer struct {

func (pr *paramReplacer) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
switch n := in.(type) {
case *ast.SelectField, *ast.GroupByClause, *ast.Limit:
case *ast.SelectField, *ast.GroupByClause, *ast.Limit, *ast.OrderByClause:
// Skip replacing values in these case:
// 1. SelectField: to keep the output field names be corresponding to these values.
// 2. GroupByClause: to avoid breaking the full_group_by check.
// 2. GroupByClause, OrderByClause: to avoid breaking the full_group_by check.
// 3. Limit: to generate different plans for queries with different limit values.
return in, true
case *driver.ValueExpr:
Expand Down
18 changes: 11 additions & 7 deletions planner/core/plan_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1469,11 +1469,11 @@ func TestNonPreparedPlanCacheGroupBy(t *testing.T) {

tk.MustExec(`select sum(b) from t group by a+1`)
tk.MustExec(`select sum(b) from t group by a+1`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
tk.MustExec(`select sum(b) from t group by a+2`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
tk.MustExec(`select sum(b) from t group by a+2`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
}

func TestNonPreparedPlanCacheFieldNames(t *testing.T) {
Expand Down Expand Up @@ -1850,7 +1850,7 @@ func TestNonPreparedPlanCacheJoin(t *testing.T) {
}
}

func TestNonPreparedPlanCacheAgg(t *testing.T) {
func TestNonPreparedPlanCacheAggSort(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
Expand All @@ -1862,13 +1862,17 @@ func TestNonPreparedPlanCacheAgg(t *testing.T) {
"select count(*) from t",
"select max(b) from t group by a",
"select count(*), a from t group by a, b",
"select sum(b) from t group by a+1",
"select count(*) from t group by a+b",
"select a from t order by a",
"select a, b, a+b from t order by a, b",
}
unsupported := []string{
"select sum(b) from t group by a+1",
"select count(*) from t group by a+b",
"select a, b, count(*) from t group by 1", // group by position
"select a, b, count(*) from t group by 1, a", // group by position
"select a, b, count(*) from t group by a having b < 1", // not support having-clause
"select a from t order by a+1",
"select a, b, a+b from t order by a + b",
}

for _, sql := range supported {
Expand All @@ -1894,13 +1898,13 @@ func TestNonPreparedPlanCacheOrder(t *testing.T) {
"select a from t order by a",
"select a from t order by a asc",
"select a from t order by a desc",
"select a from t order by a+1,b-2",
"select a from t order by a desc, b+2",
"select a from t order by a,b desc",
}
unsupported := []string{
"select a from t order by 1", // order by position
"select a from t order by a, 1",
"select a from t order by a+1,b-2",
"select a from t order by a desc, b+2",
}

for _, sql := range supported {
Expand Down
8 changes: 4 additions & 4 deletions planner/core/plan_cacheable_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,18 +435,18 @@ func (checker *nonPreparedPlanCacheableChecker) Enter(in ast.Node) (out ast.Node
return in, !checker.cacheable
case *ast.GroupByClause:
for _, item := range node.Items {
if _, isPos := item.Expr.(*ast.PositionExpr); isPos {
if _, isCol := item.Expr.(*ast.ColumnNameExpr); !isCol {
checker.cacheable = false
checker.reason = "query has group by position"
checker.reason = "only support group by {columns}'"
return in, !checker.cacheable
}
}
return in, !checker.cacheable
case *ast.OrderByClause:
for _, item := range node.Items {
if _, isPos := item.Expr.(*ast.PositionExpr); isPos {
if _, isCol := item.Expr.(*ast.ColumnNameExpr); !isCol {
checker.cacheable = false
checker.reason = "query has order by position"
checker.reason = "only support order by {columns}'"
return in, !checker.cacheable
}
}
Expand Down

0 comments on commit c282c9c

Please sign in to comment.