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,expression: use constraint propagation in partition pruning #8885

Merged
merged 26 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6cbf528
expression: constraint propagate for '>' and monotonous function
tiancaiamao Dec 10, 2018
f966b75
Merge branch 'master' into propagate-pruning
tiancaiamao Dec 11, 2018
e11f0ed
Merge master
tiancaiamao Dec 11, 2018
12d9212
gofmt
tiancaiamao Dec 11, 2018
3376571
fix CI
tiancaiamao Dec 11, 2018
9a3659b
fix CI
tiancaiamao Dec 11, 2018
b3287fc
address comment
tiancaiamao Dec 29, 2018
0f08c3e
Merge branch 'master' into propagate-pruning
tiancaiamao Dec 29, 2018
934bddd
planner,expression: use constraint propagation in partition pruning
tiancaiamao Dec 29, 2018
bdcf799
make the code more general and remove useless part
tiancaiamao Jan 2, 2019
c661aa7
Merge branch 'propagate-pruning' into re-partition-prune
tiancaiamao Jan 2, 2019
c3bddb8
update test
tiancaiamao Jan 2, 2019
1939df5
Merge branch 'master' into re-partition-prune
tiancaiamao Jan 4, 2019
32dae16
handle filter conditions that do not push down in partition pruning
tiancaiamao Jan 4, 2019
4a5735e
update partition pruning test in explain
tiancaiamao Jan 4, 2019
bf90c2a
address comment
tiancaiamao Jan 7, 2019
10fb9b1
fix ci
tiancaiamao Jan 7, 2019
084ce7a
address comment
tiancaiamao Jan 7, 2019
8518cc0
address comment
tiancaiamao Jan 7, 2019
03aff65
address comment
tiancaiamao Jan 8, 2019
5bec8fc
address comment
tiancaiamao Jan 15, 2019
7d57f56
Merge branch 'master' into re-partition-prune
tiancaiamao Jan 15, 2019
0a52d72
fix typo
tiancaiamao Jan 15, 2019
5f5acb1
Merge branch 'master' into re-partition-prune
tiancaiamao Jan 15, 2019
1b0825b
address comment
tiancaiamao Jan 16, 2019
89e597f
Merge branch 'master' into re-partition-prune
tiancaiamao Jan 17, 2019
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
3,924 changes: 1,446 additions & 2,478 deletions cmd/explaintest/r/partition_pruning.result

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion expression/constraint_propagation.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func ruleColumnOPConst(ctx sessionctx.Context, i, j int, exprs *exprSet) {
return
}
}
if !col1.Equal(ctx, col2) {
if col1.ColName.L != col2.ColName.L {
winoros marked this conversation as resolved.
Show resolved Hide resolved
return
}
v, isNull, err := compareConstant(ctx, negOP(OP2), fc1, con2)
Expand Down Expand Up @@ -302,3 +302,8 @@ func compareConstant(ctx sessionctx.Context, fn string, c1, c2 Expression) (int6
}
return cmp.EvalInt(ctx, chunk.Row{})
}

// NewPartitionPruneSolver returns a constraintSolver for partition pruning.
func NewPartitionPruneSolver() constraintSolver {
return newConstraintSolver(ruleColumnOPConst)
}
2 changes: 2 additions & 0 deletions planner/core/logical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ type DataSource struct {

// pushedDownConds are the conditions that will be pushed down to coprocessor.
pushedDownConds []expression.Expression
// allConds include those conditions that can be pushed down and can't be.
tiancaiamao marked this conversation as resolved.
Show resolved Hide resolved
allConds []expression.Expression

// relevantIndices means the indices match the push down conditions
relevantIndices []bool
Expand Down
61 changes: 61 additions & 0 deletions planner/core/partition_pruning_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// // Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package core_test

import (
. "github.com/pingcap/check"
"github.com/pingcap/parser"
"github.com/pingcap/parser/ast"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/planner/core"
// "github.com/pingcap/tidb/sessionctx"
// "github.com/pingcap/tidb/table/tables"
tiancaiamao marked this conversation as resolved.
Show resolved Hide resolved
"github.com/pingcap/tidb/util/mock"
)

var _ = Suite(&testPartitionPruningSuite{})

type testPartitionPruningSuite struct{}

func (s *testPartitionPruningSuite) TestCanBePrune(c *C) {
p := parser.New()
stmt, err := p.ParseOneStmt("create table t (d datetime not null)", "", "")
c.Assert(err, IsNil)
tblInfo, err := ddl.BuildTableInfoFromAST(stmt.(*ast.CreateTableStmt))
c.Assert(err, IsNil)

// For the following case:
// CREATE TABLE t1 ( recdate DATETIME NOT NULL )
// PARTITION BY RANGE( TO_DAYS(recdate) ) (
// PARTITION p0 VALUES LESS THAN ( TO_DAYS('2007-03-08') ),
// PARTITION p1 VALUES LESS THAN ( TO_DAYS('2007-04-01') )
// );
// SELECT * FROM t1 WHERE recdate < '2007-03-08 00:00:00';
// SELECT * FROM t1 WHERE recdate > '2018-03-08 00:00:00';

ctx := mock.NewContext()
partitionExpr, err := expression.ParseSimpleExprWithTableInfo(ctx, "to_days(d) < to_days('2007-03-08') and to_days(d) >= to_days('2007-03-07')", tblInfo)
winoros marked this conversation as resolved.
Show resolved Hide resolved
c.Assert(err, IsNil)
queryExpr, err := expression.ParseSimpleExprWithTableInfo(ctx, "d < '2000-03-08 00:00:00'", tblInfo)
c.Assert(err, IsNil)
succ, err := core.CanBePruned(ctx, partitionExpr, []expression.Expression{queryExpr})
c.Assert(err, IsNil)
c.Assert(succ, IsTrue)

queryExpr, err = expression.ParseSimpleExprWithTableInfo(ctx, "d > '2018-03-08 00:00:00'", tblInfo)
c.Assert(err, IsNil)
succ, err = core.CanBePruned(ctx, partitionExpr, []expression.Expression{queryExpr})
c.Assert(err, IsNil)
c.Assert(succ, IsTrue)
}
8 changes: 8 additions & 0 deletions planner/core/planbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ import (
. "github.com/pingcap/check"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/model"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/sessionctx"
)

// CanBePruned is provided for testing.
func CanBePruned(sctx sessionctx.Context, partitionExpr expression.Expression, filterExprs []expression.Expression) (bool, error) {
tiancaiamao marked this conversation as resolved.
Show resolved Hide resolved
var s partitionProcessor
return s.canBePruned(sctx, nil, partitionExpr, filterExprs)
}

var _ = Suite(&testPlanBuilderSuite{})

func (s *testPlanBuilderSuite) SetUpSuite(c *C) {
Expand Down
60 changes: 37 additions & 23 deletions planner/core/rule_partition_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package core

import (
"github.com/pingcap/errors"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/table/tables"
Expand All @@ -37,9 +38,6 @@ import (
type partitionProcessor struct{}

func (s *partitionProcessor) optimize(lp LogicalPlan) (LogicalPlan, error) {
// NOTE: partitionProcessor will assume all filter conditions are pushed down to
// DataSource, there will not be a Selection->DataSource case, so the rewrite just
// handle the DataSource node.
return s.rewriteDataSource(lp)
}

Expand Down Expand Up @@ -87,15 +85,13 @@ func (s *partitionProcessor) prune(ds *DataSource) (LogicalPlan, error) {
// partitions according to the filter conditions pushed to the DataSource.
children := make([]LogicalPlan, 0, len(pi.Definitions))
for i, expr := range partitionExprs {
if col != nil {
// If the selection condition would never be satisified, prune that partition.
prune, err := s.canBePrune(ds.context(), col, expr, ds.pushedDownConds)
if err != nil {
return nil, errors.Trace(err)
}
if prune {
continue
}
// If the selection condition would never be satisified, prune that partition.
tiancaiamao marked this conversation as resolved.
Show resolved Hide resolved
prune, err := s.canBePruned(ds.context(), col, expr, ds.allConds)
tiancaiamao marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, errors.Trace(err)
}
if prune {
continue
}

// Not a deep copy.
Expand Down Expand Up @@ -126,19 +122,37 @@ func (s *partitionProcessor) prune(ds *DataSource) (LogicalPlan, error) {
return unionAll, nil
}

// canBePrune checks if partition expression will never meets the selection condition.
var solver = expression.NewPartitionPruneSolver()

// canBePruned checks if partition expression will never meets the selection condition.
// For example, partition by column a > 3, and select condition is a < 3, then canBePrune returns true.
func (s *partitionProcessor) canBePrune(ctx sessionctx.Context, col *expression.Column, partitionCond expression.Expression, copConds []expression.Expression) (bool, error) {
conds := make([]expression.Expression, 0, 1+len(copConds))
conds = append(conds, partitionCond)
conds = append(conds, copConds...)
conds = expression.PropagateConstant(ctx, conds)
func (s *partitionProcessor) canBePruned(sctx sessionctx.Context, partCol *expression.Column, partitionExpr expression.Expression, filterExprs []expression.Expression) (bool, error) {
tiancaiamao marked this conversation as resolved.
Show resolved Hide resolved
conds := make([]expression.Expression, 0, 1+len(filterExprs))
conds = append(conds, partitionExpr)
conds = append(conds, filterExprs...)
conds = expression.PropagateConstant(sctx, conds)
conds = solver.Solve(sctx, conds)

if len(conds) == 1 {
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
// Constant false.
if c, ok := conds[0].(*expression.Constant); ok {
if c.GetType().Tp == mysql.TypeTiny {
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
if c.Value.GetInt64() == 0 {
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
return true, nil
}
}
}
}

// Calculate the column range to prune.
accessConds := ranger.ExtractAccessConditionsForColumn(conds, col.UniqueID)
r, err := ranger.BuildColumnRange(accessConds, ctx.GetSessionVars().StmtCtx, col.RetType)
if err != nil {
return false, errors.Trace(err)
// TODO: Remove prune by calculating range.
if partCol != nil {
accessConds := ranger.ExtractAccessConditionsForColumn(filterExprs, partCol.UniqueID)
r, err := ranger.BuildColumnRange(accessConds, sctx.GetSessionVars().StmtCtx, partCol.RetType)
if err != nil {
return false, errors.Trace(err)
}
return len(r) == 0, nil
}
return len(r) == 0, nil
return false, nil
}
1 change: 1 addition & 0 deletions planner/core/rule_predicate_push_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (p *LogicalUnionScan) PredicatePushDown(predicates []expression.Expression)

// PredicatePushDown implements LogicalPlan PredicatePushDown interface.
func (ds *DataSource) PredicatePushDown(predicates []expression.Expression) ([]expression.Expression, LogicalPlan) {
ds.allConds = predicates
_, ds.pushedDownConds, predicates = expression.ExpressionsToPB(ds.ctx.GetSessionVars().StmtCtx, predicates, ds.ctx.GetClient())
return predicates, ds
}
Expand Down