Skip to content

Commit

Permalink
planner: check the ignore-plan-cache hint in insert-stmt (#40080) (#4…
Browse files Browse the repository at this point in the history
…2712)

ref #39717, close #40079
  • Loading branch information
qw4990 authored Mar 30, 2023
1 parent dc1ebbe commit 8b92c83
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
23 changes: 23 additions & 0 deletions executor/prepared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,29 @@ func TestPlanCacheOperators(t *testing.T) {
}
}

func TestIgnoreInsertStmt(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
orgEnable := plannercore.PreparedPlanCacheEnabled()
defer func() {
plannercore.SetPreparedPlanCache(orgEnable)
}()
plannercore.SetPreparedPlanCache(true)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t (a int)")

// ignore-hint in insert-stmt can work
tk.MustExec("prepare st from 'insert into t select * from t'")
tk.MustExec("execute st")
tk.MustExec("execute st")
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
tk.MustExec("prepare st from 'insert /*+ ignore_plan_cache() */ into t select * from t'")
tk.MustExec("execute st")
tk.MustExec("execute st")
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
}

func TestIssue28782(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
Expand Down
18 changes: 18 additions & 0 deletions planner/core/cacheable_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,24 @@ func (checker *cacheableChecker) Enter(in ast.Node) (out ast.Node, skipChildren
return in, true
}
}
case *ast.InsertStmt:
if node.Select == nil {
nRows := len(node.Lists)
nCols := 0
if len(node.Lists) > 0 { // avoid index-out-of-range
nCols = len(node.Lists[0])
}
if nRows*nCols > 200 { // to save memory
checker.cacheable = false
return in, true
}
}
for _, hints := range node.TableHints {
if hints.HintName.L == HintIgnorePlanCache {
checker.cacheable = false
return in, true
}
}
case *ast.VariableExpr, *ast.ExistsSubqueryExpr, *ast.SubqueryExpr:
checker.cacheable = false
return in, true
Expand Down

0 comments on commit 8b92c83

Please sign in to comment.