Skip to content

Commit

Permalink
planner: skip plan-cache if some predicates are converted to True/Fal…
Browse files Browse the repository at this point in the history
…se when constant propagation (#43875)

close #43852
  • Loading branch information
qw4990 committed May 16, 2023
1 parent 5db4b38 commit 0490add
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
5 changes: 5 additions & 0 deletions expression/constant_propagation.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package expression

import (
"errors"

"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/terror"
Expand Down Expand Up @@ -278,6 +280,9 @@ func (s *propConstSolver) propagateColumnEQ() {
}

func (s *propConstSolver) setConds2ConstFalse() {
if MaybeOverOptimized4PlanCache(s.ctx, s.conditions) {
s.ctx.GetSessionVars().StmtCtx.SetSkipPlanCache(errors.New("some parameters may be overwritten when constant propagation"))
}
s.conditions = []Expression{&Constant{
Value: types.NewDatum(false),
RetType: types.NewFieldType(mysql.TypeTiny),
Expand Down
38 changes: 37 additions & 1 deletion planner/core/plan_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,18 @@ func TestNonPreparedPlanParameterType(t *testing.T) {
tk.MustQuery(`show warnings`).Check(testkit.Rows(`Warning 1105 skip non-prepared plan-cache: '1' may be converted to INT`))
}

func TestIssue43852(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`create table t6 (a date, b date, key(a))`)
tk.MustExec(`insert into t6 values ('2023-01-21', '2023-01-05')`)
tk.MustExec(`set tidb_enable_non_prepared_plan_cache=1`)
tk.MustQuery(`select * from t6 where a in (2015, '8')`).Check(testkit.Rows())
tk.MustQuery(`select * from t6 where a in (2009, '2023-01-21')`).Check(testkit.Rows(`2023-01-21 2023-01-05`))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
}

func TestNonPreparedPlanTypeRandomly(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
Expand Down Expand Up @@ -1984,7 +1996,7 @@ func TestNonPreparedPlanExplainWarning(t *testing.T) {
"skip non-prepared plan-cache: queries that have generated columns are not supported",
"skip non-prepared plan-cache: queries that access views are not supported",
"skip non-prepared plan-cache: query has null constants",
"skip non-prepared plan-cache: get a TableDual plan",
"skip non-prepared plan-cache: some parameters may be overwritten when constant propagation",
}

all := append(supported, unsupported...)
Expand Down Expand Up @@ -2280,6 +2292,30 @@ func TestNonPreparedPlanCacheAutoStmtRetry(t *testing.T) {
require.ErrorContains(t, tk2Err, "Duplicate entry")
}

func TestIssue43667Concurrency(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table cycle (pk int key, val int)")
var wg sync.WaitGroup
concurrency := 30
for i := 0; i < concurrency; i++ {
tk.MustExec(fmt.Sprintf("insert into cycle values (%v,%v)", i, i))
wg.Add(1)
go func(id int) {
defer wg.Done()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_non_prepared_plan_cache=1")
query := fmt.Sprintf("select (val) from cycle where pk = %v", id)
for j := 0; j < 5000; j++ {
tk.MustQuery(query).Check(testkit.Rows(fmt.Sprintf("%v", id)))
}
}(i)
}
wg.Wait()
}

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

0 comments on commit 0490add

Please sign in to comment.