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: Manual revert of #24282 (#35298) #37268

Merged
merged 2 commits into from
Aug 22, 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
15 changes: 15 additions & 0 deletions executor/partition_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3600,3 +3600,18 @@ func TestPartitionTableExplain(t *testing.T) {
" └─Selection 1.00 cop[tikv] not(isnull(testpartitiontableexplain.t.b))",
" └─TableRangeScan 1.00 cop[tikv] table:t range:[1,1], keep order:false"))
}

func TestIssue35181(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()

tk := testkit.NewTestKit(t, store)
tk.MustExec("create database TestIssue35181")
tk.MustExec("use TestIssue35181")
tk.MustExec("CREATE TABLE `t` (`a` int(11) DEFAULT NULL, `b` int(11) DEFAULT NULL) PARTITION BY RANGE (`a`) (PARTITION `p0` VALUES LESS THAN (2021), PARTITION `p1` VALUES LESS THAN (3000))")

tk.MustExec("set @@tidb_partition_prune_mode = 'static'")
tk.MustExec(`insert into t select * from t where a=3000`)
tk.MustExec("set @@tidb_partition_prune_mode = 'dynamic'")
tk.MustExec(`insert into t select * from t where a=3000`)
}
2 changes: 1 addition & 1 deletion planner/core/partition_prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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, nil)
rangeOr, err := s.pruneRangePartition(ctx, pi, tbl, conds, columns, names)
if err != nil {
return nil, err
}
Expand Down
47 changes: 12 additions & 35 deletions planner/core/rule_partition_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,26 +824,26 @@ 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, condsToBePruned *[]expression.Expression) (partitionRangeOR, []expression.Expression, error) {
columns []*expression.Column, names types.NameSlice) (partitionRangeOR, error) {
partExpr, err := tbl.(partitionTable).PartitionExpr()
if err != nil {
return nil, nil, err
return nil, err
}

// Partition by range columns.
if len(pi.Columns) > 0 {
result, err := s.pruneRangeColumnsPartition(ctx, conds, pi, partExpr, columns, names)
return result, nil, err
return result, err
}

// Partition by range.
col, fn, mono, err := makePartitionByFnCol(ctx, columns, names, pi.Expr)
if err != nil {
return nil, nil, err
return nil, err
}
result := fullRange(len(pi.Definitions))
if col == nil {
return result, nil, nil
return result, nil
}

// Extract the partition column, if the column is not null, it's possible to prune.
Expand All @@ -858,41 +858,14 @@ func (s *partitionProcessor) pruneRangePartition(ctx sessionctx.Context, pi *mod
}
result = partitionRangeForCNFExpr(ctx, conds, &pruner, result)

if condsToBePruned == nil {
return result, nil, nil
}
// remove useless predicates after 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.GetFlag())
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)
}

return result, newConds, nil
return result, nil
}

func (s *partitionProcessor) processRangePartition(ds *DataSource, pi *model.PartitionInfo, opt *logicalOptimizeOp) (LogicalPlan, error) {
used, prunedConds, err := s.pruneRangePartition(ds.ctx, pi, ds.table.(table.PartitionedTable), ds.allConds, ds.TblCols, ds.names, &ds.pushedDownConds)
used, err := s.pruneRangePartition(ds.ctx, pi, ds.table.(table.PartitionedTable), ds.allConds, ds.TblCols, ds.names)
if err != nil {
return nil, err
}
if prunedConds != nil {
ds.pushedDownConds = prunedConds
}
return s.makeUnionAllChildren(ds, pi, used, opt)
}

Expand Down Expand Up @@ -1279,11 +1252,13 @@ func relaxOP(op string) string {
return op
}

// pruneUseBinarySearch returns the start and end of which partitions will match.
// If no match (i.e. value > last partition) the start partition will be the number of partition, not the first partition!
func pruneUseBinarySearch(lessThan lessThanDataInt, data dataForPrune, unsigned bool) (start int, end int) {
length := lessThan.length()
switch data.op {
case ast.EQ:
// col = 66, lessThan = [4 7 11 14 17] => [5, 6)
// col = 66, lessThan = [4 7 11 14 17] => [5, 5)
// col = 14, lessThan = [4 7 11 14 17] => [4, 5)
// col = 10, lessThan = [4 7 11 14 17] => [2, 3)
// col = 3, lessThan = [4 7 11 14 17] => [0, 1)
Expand Down Expand Up @@ -1589,6 +1564,8 @@ func (p *rangeColumnsPruner) partitionRangeForExpr(sctx sessionctx.Context, expr
return start, end, true
}

// pruneUseBinarySearch returns the start and end of which partitions will match.
// If no match (i.e. value > last partition) the start partition will be the number of partition, not the first partition!
func (p *rangeColumnsPruner) pruneUseBinarySearch(sctx sessionctx.Context, op string, data *expression.Constant) (start int, end int) {
var err error
var isNull bool
Expand Down
15 changes: 9 additions & 6 deletions planner/core/testdata/partition_pruner_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -2843,15 +2843,17 @@
{
"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"
"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: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"
"TableReader 10.00 root data:Selection",
"└─Selection 10.00 cop[tikv] eq(test_partition.t.a, 2)",
" └─TableFullScan 10000.00 cop[tikv] table:t, partition:p2 keep order:false, stats:pseudo"
]
},
{
Expand Down Expand Up @@ -2899,8 +2901,9 @@
{
"SQL": "explain format = 'brief' select * from t where a in (2)",
"Result": [
"TableReader 10000.00 root data:TableFullScan",
"└─TableFullScan 10000.00 cop[tikv] table:t, partition:p2 keep order:false, stats:pseudo"
"TableReader 10.00 root data:Selection",
"└─Selection 10.00 cop[tikv] eq(test_partition.t.a, 2)",
" └─TableFullScan 10000.00 cop[tikv] table:t, partition:p2 keep order:false, stats:pseudo"
]
}
]
Expand Down