Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: Disable prepared plan cache for partitioned tables in dynamic prune mode #33098

Merged
merged 5 commits into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4023,7 +4023,7 @@ func (h kvRangeBuilderFromRangeAndPartition) buildKeyRangeSeparately(ranges []*r
return pids, ret, nil
}

func (h kvRangeBuilderFromRangeAndPartition) buildKeyRange(_ int64, ranges []*ranger.Range) ([]kv.KeyRange, error) {
func (h kvRangeBuilderFromRangeAndPartition) buildKeyRange(ranges []*ranger.Range) ([]kv.KeyRange, error) {
var ret []kv.KeyRange
for _, p := range h.partitions {
pid := p.GetPhysicalID()
Expand Down
4 changes: 2 additions & 2 deletions executor/table_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (sr selectResultHook) SelectResult(ctx context.Context, sctx sessionctx.Con
}

type kvRangeBuilder interface {
buildKeyRange(pid int64, ranges []*ranger.Range) ([]kv.KeyRange, error)
buildKeyRange(ranges []*ranger.Range) ([]kv.KeyRange, error)
buildKeyRangeSeparately(ranges []*ranger.Range) ([]int64, [][]kv.KeyRange, error)
}

Expand Down Expand Up @@ -376,7 +376,7 @@ func (e *TableReaderExecutor) buildKVReq(ctx context.Context, ranges []*ranger.R
var builder distsql.RequestBuilder
var reqBuilder *distsql.RequestBuilder
if e.kvRangeBuilder != nil {
kvRange, err := e.kvRangeBuilder.buildKeyRange(getPhysicalTableID(e.table), ranges)
kvRange, err := e.kvRangeBuilder.buildKeyRange(ranges)
if err != nil {
return nil, err
}
Expand Down
10 changes: 7 additions & 3 deletions planner/core/cacheable_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,13 @@ func (checker *cacheableChecker) Enter(in ast.Node) (out ast.Node, skipChildren
case *ast.TableName:
if checker.schema != nil {
if checker.isPartitionTable(node) {
if checker.sctx != nil && checker.sctx.GetSessionVars().UseDynamicPartitionPrune() {
return in, false // dynamic-mode for partition tables can use plan-cache
}
// Temporary disable prepared plan cache until https://github.com/pingcap/tidb/issues/33031
// is fixed and additional tests with dynamic partition prune mode has been added.
/*
if checker.sctx != nil && checker.sctx.GetSessionVars().UseDynamicPartitionPrune() {
return in, false // dynamic-mode for partition tables can use plan-cache
}
*/
checker.cacheable = false
return in, true
}
Expand Down
24 changes: 21 additions & 3 deletions planner/core/prepare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1064,8 +1064,8 @@ func TestPrepareCacheForPartition(t *testing.T) {
tk := testkit.NewTestKitWithSession(t, store, se)

tk.MustExec("use test")
for _, val := range []string{string(variable.Static), string(variable.Dynamic)} {
tk.MustExec("set @@tidb_partition_prune_mode = '" + val + "'")
for _, pruneMode := range []string{string(variable.Static), string(variable.Dynamic)} {
tk.MustExec("set @@tidb_partition_prune_mode = '" + pruneMode + "'")
// Test for PointGet and IndexRead.
tk.MustExec("drop table if exists t_index_read")
tk.MustExec("create table t_index_read (id int, k int, c varchar(10), primary key (id, k)) partition by hash(id+k) partitions 10")
Expand Down Expand Up @@ -1166,6 +1166,22 @@ func TestPrepareCacheForPartition(t *testing.T) {
tk.MustQuery("execute stmt8 using @id").Check(testkit.Rows("hij"))
tk.MustExec("set @id=100")
tk.MustQuery("execute stmt8 using @id").Check(testkit.Rows())

// https://github.com/pingcap/tidb/issues/33031
tk.MustExec(`drop table if exists Issue33031`)
tk.MustExec(`CREATE TABLE Issue33031 (COL1 int(16) DEFAULT '29' COMMENT 'NUMERIC UNIQUE INDEX', COL2 bigint(20) DEFAULT NULL, UNIQUE KEY UK_COL1 (COL1)) PARTITION BY RANGE (COL1) (PARTITION P0 VALUES LESS THAN (0))`)
tk.MustExec(`insert into Issue33031 values(-5, 7)`)
tk.MustExec(`prepare stmt from 'select *,? from Issue33031 where col2 < ? and col1 in (?, ?)'`)
tk.MustExec(`set @a=111, @b=1, @c=2, @d=22`)
tk.MustQuery(`execute stmt using @d,@a,@b,@c`).Check(testkit.Rows())
tk.MustExec(`set @a=112, @b=-2, @c=-5, @d=33`)
tk.MustQuery(`execute stmt using @d,@a,@b,@c`).Check(testkit.Rows("-5 7 33"))
if pruneMode == string(variable.Dynamic) {
// When the temporary disabling of prepared plan cache for dynamic partition prune mode is disabled, change this to 1!
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
} else {
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
}
}
}

Expand Down Expand Up @@ -2392,6 +2408,7 @@ func TestPartitionTable(t *testing.T) {
tk.MustExec("create database test_plan_cache")
tk.MustExec("use test_plan_cache")
tk.MustExec("set @@tidb_partition_prune_mode = 'dynamic'")
tk.MustExec("set @@tidb_enable_list_partition = 1")

type testcase struct {
t1Create string
Expand Down Expand Up @@ -2501,7 +2518,8 @@ func TestPartitionTable(t *testing.T) {
for i := 0; i < 100; i++ {
tk.MustExec(fmt.Sprintf("set @a=%v", tc.varGener()))
result1 := tk.MustQuery("execute stmt1 using @a").Sort().Rows()
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
// When https://github.com/pingcap/tidb/pull/33098 is reverted this should be 1 again
tk.MustQuery("select @@last_plan_from_cache /* i=" + strconv.Itoa(i) + " prepared statement: (t1) " + tc.query + "\n-- create table: " + tc.t1Create + "*/").Check(testkit.Rows("0"))
tk.MustQuery("execute stmt2 using @a").Sort().Check(result1)
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
}
Expand Down