Skip to content

Commit

Permalink
planner: add more test cases for non-prep plan cache (#42716)
Browse files Browse the repository at this point in the history
ref #36598
  • Loading branch information
qw4990 authored Mar 31, 2023
1 parent 73d446e commit 6273e22
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
16 changes: 13 additions & 3 deletions planner/core/plan_cache_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,21 @@ type paramReplacer struct {
// This is to make the output field names be corresponding to these values.
// Use int instead of bool to support nested SelectField.
selFieldsCnt int

// Skip all values in GroupByClause since them can affect the full_group_by check, e.g.
// `select a*2 from t group by a*?` cannot pass the full_group_by check.
groupByCnt int
}

func (pr *paramReplacer) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
switch n := in.(type) {
case *ast.SelectField:
pr.selFieldsCnt++
case *ast.GroupByClause:
pr.groupByCnt++
case *driver.ValueExpr:
if pr.selFieldsCnt == 0 { // not in SelectField
if pr.selFieldsCnt == 0 && // not in SelectField
pr.groupByCnt == 0 { // not in GroupBy
pr.params = append(pr.params, n)
param := ast.NewParamMarkerExpr(len(pr.params) - 1) // offset is used as order in non-prepared plan cache.
param.(*driver.ParamMarkerExpr).Datum = *n.Datum.Clone() // init the ParamMakerExpr's Datum
Expand All @@ -76,13 +83,16 @@ func (pr *paramReplacer) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
}

func (pr *paramReplacer) Leave(in ast.Node) (out ast.Node, ok bool) {
if _, ok := in.(*ast.SelectField); ok {
switch in.(type) {
case *ast.SelectField:
pr.selFieldsCnt--
case *ast.GroupByClause:
pr.groupByCnt--
}
return in, true
}

func (pr *paramReplacer) Reset() { pr.params = nil }
func (pr *paramReplacer) Reset() { pr.params, pr.selFieldsCnt, pr.groupByCnt = nil, 0, 0 }

// ParameterizeAST parameterizes this StmtNode.
// e.g. `select * from t where a<10 and b<23` --> `select * from t where a<? and b<?`, [10, 23].
Expand Down
6 changes: 6 additions & 0 deletions planner/core/plan_cache_param_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func TestParameterize(t *testing.T) {
[]interface{}{int64(10)},
"SELECT `a`+1 FROM `t` WHERE `a`<10",
},
{
"select a+1, sum(b) from t where a<10 group by a+1",
"SELECT `a`+1,SUM(`b`) FROM `t` WHERE `a`<? GROUP BY `a`+1",
[]interface{}{int64(10)},
"SELECT `a`+1,SUM(`b`) FROM `t` WHERE `a`<10 GROUP BY `a`+1",
},
// TODO: more test cases
}

Expand Down
16 changes: 16 additions & 0 deletions planner/core/plan_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,22 @@ func TestPlanCacheSubquerySPMEffective(t *testing.T) {
}
}

func TestNonPreparedPlanCacheGroupBy(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, index(a))")
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")

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.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"))
}

func TestNonPreparedPlanCacheFieldNames(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
Expand Down

0 comments on commit 6273e22

Please sign in to comment.