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: support keep pruned conds #34029

Merged
merged 19 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from 13 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
10 changes: 10 additions & 0 deletions executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,16 @@ func (s *testSerialSuite1) TestSetVar(c *C) {
tk.MustQuery(`select @@global.tidb_enable_ordered_result_mode`).Check(testkit.Rows("0"))
tk.MustQuery(`select @@tidb_enable_ordered_result_mode`).Check(testkit.Rows("1"))

// test for tidb_keep_pruned_conds
tk.MustQuery(`select @@tidb_keep_pruned_conds`).Check(testkit.Rows("0"))
tk.MustExec(`set global tidb_keep_pruned_conds = 1`)
tk.MustQuery(`select @@global.tidb_keep_pruned_conds`).Check(testkit.Rows("1"))
tk.MustExec(`set global tidb_keep_pruned_conds = 0`)
tk.MustQuery(`select @@global.tidb_keep_pruned_conds`).Check(testkit.Rows("0"))
tk.MustExec(`set tidb_keep_pruning_conds=1`)
Reminiscent marked this conversation as resolved.
Show resolved Hide resolved
tk.MustQuery(`select @@global.tidb_keep_pruned_conds`).Check(testkit.Rows("0"))
tk.MustQuery(`select @@tidb_keep_pruned_conds`).Check(testkit.Rows("1"))

// test for tidb_opt_enable_correlation_adjustment
tk.MustQuery(`select @@tidb_opt_enable_correlation_adjustment`).Check(testkit.Rows("1"))
tk.MustExec(`set global tidb_opt_enable_correlation_adjustment = 0`)
Expand Down
19 changes: 19 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4406,3 +4406,22 @@ func (s *testIntegrationSerialSuite) TestIssue30271(c *C) {
tk.MustExec("set names utf8mb4 collate utf8mb4_general_ci;")
tk.MustQuery("select * from t where (a>'a' and b='a') or (b = 'A' and a < 'd') order by a,c;").Check(testkit.Rows("b a 1", "b A 2", "c a 3"))
}

func (s *testIntegrationSerialSuite) TestKeepPrunedConds(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@session.tidb_partition_prune_mode = 'static'")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a int, b int, c int, unique index idx(a, b, c)) PARTITION BY RANGE ( `a` ) (PARTITION `PART_202101` VALUES LESS THAN (202102), PARTITION `PART_202102` VALUES LESS THAN (202103), PARTITION `PART_202103` VALUES LESS THAN (202104));")
tk.MustExec("set @@tidb_keep_pruned_conds = 1")
tk.MustQuery("explain format = 'brief' select count(*) from t where a = 202103 and b = 1 and c = 1;").Check(testkit.Rows(""+
"StreamAgg 1.00 root funcs:count(1)->Column#5",
"└─Point_Get 1.00 root table:t, partition:part_202103, index:idx(a, b, c) "))

tk.MustExec("set @@tidb_keep_pruned_conds = 0")
tk.MustQuery("explain format = 'brief' select count(*) from t where a = 202103 and b = 1 and c = 1;").Check(testkit.Rows(""+
"StreamAgg 1.00 root funcs:count(1)->Column#5",
"└─IndexReader 0.01 root index:Selection",
" └─Selection 0.01 cop[tikv] eq(test.t.b, 1), eq(test.t.c, 1)",
" └─IndexFullScan 10000.00 cop[tikv] table:t, partition:part_202103, index:idx(a, b, c) keep order:false, stats:pseudo"))
}
2 changes: 1 addition & 1 deletion planner/core/rule_partition_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ func (s *partitionProcessor) processRangePartition(ds *DataSource, pi *model.Par
if err != nil {
return nil, err
}
if prunedConds != nil {
if !ds.ctx.GetSessionVars().KeepPrunedConds && prunedConds != nil {
ds.pushedDownConds = prunedConds
}
return s.makeUnionAllChildren(ds, pi, used)
Expand Down
3 changes: 3 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,9 @@ type SessionVars struct {
// EnableStableResultMode if stabilize query results.
EnableStableResultMode bool

// KeepPrunedConds indicates whether to keep the pruning conditions after partition pruning.
Reminiscent marked this conversation as resolved.
Show resolved Hide resolved
KeepPrunedConds bool

// LocalTemporaryTables is *infoschema.LocalTemporaryTables, use interface to avoid circle dependency.
// It's nil if there is no local temporary table.
LocalTemporaryTables interface{}
Expand Down
4 changes: 4 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,10 @@ var defaultSysVars = []*SysVar{
s.EnableStableResultMode = TiDBOptOn(val)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBKeepPrunedConds, Value: BoolToOnOff(DefTiDBKeepPrunedConds), Hidden: true, Type: TypeBool, SetSession: func(s *SessionVars, val string) error {
s.KeepPrunedConds = TiDBOptOn(val)
return nil
}},
{Scope: ScopeNone, Name: TiDBAllowFunctionForExpressionIndex, ReadOnly: true, Value: collectAllowFuncName4ExpressionIndex()},
}

Expand Down
4 changes: 4 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,9 @@ const (

// TiDBEnableOrderedResultMode indicates if stabilize query results.
TiDBEnableOrderedResultMode = "tidb_enable_ordered_result_mode"

Reminiscent marked this conversation as resolved.
Show resolved Hide resolved
// TiDBKeepPrunedConds indicates whether to keep the pruning conditions after partition pruning.
Reminiscent marked this conversation as resolved.
Show resolved Hide resolved
TiDBKeepPrunedConds = "tidb_keep_pruned_conds"
)

// TiDB vars that have only global scope
Expand Down Expand Up @@ -740,6 +743,7 @@ const (
DefTMPTableSize = 16777216
DefTiDBEnableLocalTxn = false
DefTiDBEnableOrderedResultMode = false
DefTiDBKeepPrunedConds = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Format the space and tab?

)

// Process global variables.
Expand Down