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: remove useless predicates after partition pruning #24282

Merged
merged 25 commits into from
May 2, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
dcd93d8
remove useless predicates after partition pruning
zhuo-zhi Apr 25, 2021
2154299
bug fix
zhuo-zhi Apr 26, 2021
1b2bfe9
refine code
zhuo-zhi Apr 26, 2021
b8e1723
bug fix
zhuo-zhi Apr 26, 2021
5f7bd17
add unit tests for partition predicate pruning
zhuo-zhi Apr 27, 2021
ab8e406
fix the unit test
zhuo-zhi Apr 27, 2021
cc43153
refine code style
zhuo-zhi Apr 28, 2021
1e25687
add more unit tests
zhuo-zhi Apr 28, 2021
0354bae
modify function defination
zhuo-zhi Apr 29, 2021
b018889
Merge branch 'master' into dev_partitionTB2
ti-chi-bot Apr 29, 2021
1f2491b
Merge branch 'master' into dev_partitionTB2
ti-chi-bot Apr 29, 2021
b4a7799
Merge branch 'master' into dev_partitionTB2
ti-chi-bot Apr 29, 2021
33e3f1a
Merge branch 'master' into dev_partitionTB2
ti-chi-bot Apr 29, 2021
90aee9e
Merge branch 'master' into dev_partitionTB2
ti-chi-bot Apr 29, 2021
d5aee82
Merge branch 'master' into dev_partitionTB2
ti-chi-bot Apr 29, 2021
4a37d96
Merge branch 'master' into dev_partitionTB2
ti-chi-bot Apr 29, 2021
8cd0fd8
Merge branch 'master' into dev_partitionTB2
ti-chi-bot Apr 30, 2021
0bbe6bb
Merge branch 'master' into dev_partitionTB2
ti-chi-bot Apr 30, 2021
6abb685
Merge branch 'master' into dev_partitionTB2
ti-chi-bot Apr 30, 2021
bdd5407
Merge branch 'master' into dev_partitionTB2
ti-chi-bot Apr 30, 2021
a6f5a16
Merge branch 'master' into dev_partitionTB2
ti-chi-bot Apr 30, 2021
208186a
Merge branch 'master' into dev_partitionTB2
ti-chi-bot Apr 30, 2021
1d7c892
Merge branch 'master' into dev_partitionTB2
ti-chi-bot Apr 30, 2021
b875b35
Merge branch 'master' into dev_partitionTB2
ti-chi-bot May 2, 2021
1d5a90c
Merge branch 'master' into dev_partitionTB2
ti-chi-bot May 2, 2021
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 planner/core/partition_prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func PartitionPruning(ctx sessionctx.Context, tbl table.PartitionedTable, conds
case model.PartitionTypeHash:
return s.pruneHashPartition(ctx, tbl, partitionNames, conds, columns, names)
case model.PartitionTypeRange:
rangeOr, err := s.pruneRangePartition(ctx, pi, tbl, conds, columns, names)
rangeOr, err := s.pruneRangePartition(ctx, pi, tbl, conds, columns, names, nil)
if err != nil {
return nil, err
}
Expand Down
31 changes: 31 additions & 0 deletions planner/core/partition_pruner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,34 @@ partition by range (a) (
tk.MustQuery("select * from t3 where not (a = 1)").Sort().Check(testkit.Rows("11", "12", "13", "2", "3"))
tk.MustQuery("select * from t3 where not (a != 1)").Check(testkit.Rows("1"))
}

//issue 22079
func (s *testPartitionPruneSuit) TestRangePartitionPredicatePruner(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("set @@tidb_partition_prune_mode='" + string(variable.Static) + "'")
tk.MustExec("drop database if exists test_partition;")
tk.MustExec("create database test_partition")
tk.MustExec("use test_partition")
tk.MustExec("drop table if exists t")
tk.Se.GetSessionVars().EnableClusteredIndex = variable.ClusteredIndexDefModeIntOnly
tk.MustExec(`create table t (a int(11) default null) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
partition by range(a) (
partition p0 values less than (1),
partition p1 values less than (2),
partition p2 values less than (3),
partition p_max values less than (maxvalue));`)

var input []string
var output []struct {
SQL string
Result []string
}
s.testData.GetTestCases(c, &input, &output)
for i, tt := range input {
s.testData.OnRecord(func() {
output[i].SQL = tt
output[i].Result = s.testData.ConvertRowsToStrings(tk.MustQuery(tt).Rows())
})
tk.MustQuery(tt).Check(testkit.Rows(output[i].Result...))
}
}
30 changes: 28 additions & 2 deletions planner/core/rule_partition_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ func intersectionRange(start, end, newStart, newEnd int) (int, int) {
}

func (s *partitionProcessor) pruneRangePartition(ctx sessionctx.Context, pi *model.PartitionInfo, tbl table.PartitionedTable, conds []expression.Expression,
columns []*expression.Column, names types.NameSlice) (partitionRangeOR, error) {
columns []*expression.Column, names types.NameSlice, condsToBePruned *[]expression.Expression) (partitionRangeOR, error) {
qw4990 marked this conversation as resolved.
Show resolved Hide resolved
partExpr, err := tbl.(partitionTable).PartitionExpr()
if err != nil {
return nil, err
Expand Down Expand Up @@ -762,11 +762,37 @@ func (s *partitionProcessor) pruneRangePartition(ctx sessionctx.Context, pi *mod
monotonous: mono,
}
result = partitionRangeForCNFExpr(ctx, conds, &pruner, result)

if condsToBePruned == nil {
return result, nil
}
// remove useless predicates after partition pruning
newConds := make([]expression.Expression, 0, len(*condsToBePruned))
for _, cond := range *condsToBePruned {
if dataForPrune, ok := pruner.extractDataForPrune(ctx, cond); ok {
switch dataForPrune.op {
case ast.EQ:
unsigned := mysql.HasUnsignedFlag(pruner.col.RetType.Flag)
start, _ := pruneUseBinarySearch(pruner.lessThan, dataForPrune, unsigned)
// if the type of partition key is Int
if pk, ok := partExpr.Expr.(*expression.Column); ok && pk.RetType.EvalType() == types.ETInt {
// see if can be removed
// see issue #22079: https://github.com/pingcap/tidb/issues/22079 for details
if start > 0 && pruner.lessThan.data[start-1] == dataForPrune.c && (pruner.lessThan.data[start]-1) == dataForPrune.c {
continue
}
}
}
}
newConds = append(newConds, cond)
}
*condsToBePruned = newConds

return result, nil
}

func (s *partitionProcessor) processRangePartition(ds *DataSource, pi *model.PartitionInfo) (LogicalPlan, error) {
used, err := s.pruneRangePartition(ds.ctx, pi, ds.table.(table.PartitionedTable), ds.allConds, ds.TblCols, ds.names)
used, err := s.pruneRangePartition(ds.ctx, pi, ds.table.(table.PartitionedTable), ds.allConds, ds.TblCols, ds.names, &ds.pushedDownConds)
if err != nil {
return nil, err
}
Expand Down
9 changes: 9 additions & 0 deletions planner/core/testdata/partition_pruner_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -496,5 +496,14 @@
"Pruner": "t1: p0,p1; t2: p0"
}
]
},
{
"name": "TestRangePartitionPredicatePruner",
"cases": [
"explain format = 'brief' select * from t where a = 1",
"explain format = 'brief' select * from t where a = 2",
"explain format = 'brief' select * from t where a = 10",
"explain format = 'brief' select * from t where a = -1"
]
}
]
35 changes: 35 additions & 0 deletions planner/core/testdata/partition_pruner_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -2784,5 +2784,40 @@
]
}
]
},
{
"Name": "TestRangePartitionPredicatePruner",
"Cases": [
{
"SQL": "explain format = 'brief' select * from t where a = 1",
"Result": [
"TableReader 10000.00 root data:TableFullScan",
"└─TableFullScan 10000.00 cop[tikv] table:t, partition:p1 keep order:false, stats:pseudo"
]
},
{
"SQL": "explain format = 'brief' select * from t where a = 2",
"Result": [
"TableReader 10000.00 root data:TableFullScan",
"└─TableFullScan 10000.00 cop[tikv] table:t, partition:p2 keep order:false, stats:pseudo"
]
},
{
"SQL": "explain format = 'brief' select * from t where a = 10",
"Result": [
"TableReader 10.00 root data:Selection",
"└─Selection 10.00 cop[tikv] eq(test_partition.t.a, 10)",
" └─TableFullScan 10000.00 cop[tikv] table:t, partition:p_max keep order:false, stats:pseudo"
]
},
{
"SQL": "explain format = 'brief' select * from t where a = -1",
"Result": [
"TableReader 10.00 root data:Selection",
"└─Selection 10.00 cop[tikv] eq(test_partition.t.a, -1)",
" └─TableFullScan 10000.00 cop[tikv] table:t, partition:p0 keep order:false, stats:pseudo"
]
}
]
}
]