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

*: Selection can control the conditions of the below scan plan. #2834

Merged
merged 30 commits into from
Mar 28, 2017
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5db58a9
*: add indexScan controlled by correlated column
winoros Mar 15, 2017
1fe024d
Merge branch 'master' into yiding/apply_optimize
winoros Mar 15, 2017
5ce9107
tiny change
winoros Mar 15, 2017
364e172
fix merge bug
winoros Mar 15, 2017
36283cf
remove log
winoros Mar 15, 2017
7451287
remove log
winoros Mar 15, 2017
053c2d4
fix bug and tiny change
winoros Mar 16, 2017
e663291
address some comments
winoros Mar 16, 2017
a25a3eb
manage cast in plan build phase
winoros Mar 17, 2017
6aadeaa
address comments and manage cast in executor phase
winoros Mar 17, 2017
ba29a95
address some comments
winoros Mar 20, 2017
02cbcf1
remove useless args
winoros Mar 20, 2017
9a9ae32
change some logic
winoros Mar 20, 2017
ef178a3
fix bug
winoros Mar 20, 2017
8029c1e
refactor
winoros Mar 21, 2017
9771129
address comments
winoros Mar 21, 2017
7251827
tiny change
winoros Mar 21, 2017
e45f669
address some comments
winoros Mar 22, 2017
4e0012b
add test
winoros Mar 22, 2017
f0ec2e0
remove redundant function
winoros Mar 22, 2017
02db63c
move function
winoros Mar 22, 2017
c791ff7
move code
winoros Mar 22, 2017
8674c3f
typo error && add explain
winoros Mar 22, 2017
107a042
Merge branch 'master' into yiding/inlj_1
winoros Mar 23, 2017
3fd0435
remove outdate comment
winoros Mar 23, 2017
8f29d17
add tests and some tiny change
winoros Mar 24, 2017
088e9e6
tiny change
winoros Mar 24, 2017
6351036
add test
winoros Mar 24, 2017
97998fc
address comments
winoros Mar 27, 2017
55f92e6
Merge branch 'master' into yiding/inlj_1
shenli Mar 28, 2017
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
12 changes: 8 additions & 4 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,14 @@ func (b *executorBuilder) buildAggregation(v *plan.PhysicalAggregation) Executor

func (b *executorBuilder) buildSelection(v *plan.Selection) Executor {
exec := &SelectionExec{
Src: b.build(v.Children()[0]),
Condition: expression.ComposeCNFCondition(b.ctx, v.Conditions...),
schema: v.Schema(),
ctx: b.ctx,
Src: b.build(v.Children()[0]),
Condition: expression.ComposeCNFCondition(b.ctx, v.Conditions...),
schema: v.Schema(),
ctx: b.ctx,
scanController: v.ScanController,
accessConditions: v.AccessConditions,
idxFilterConditions: v.IdxConditions,
tblFilterConditions: v.TblConditions,
}
return exec
}
Expand Down
76 changes: 76 additions & 0 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,21 +461,91 @@ func (e *TableDualExec) Close() error {
return nil
}

func substituteCorCol2Constant(expr expression.Expression) (expression.Expression) {
switch x := expr.(type) {
case *expression.ScalarFunction:
newArgs := make([]expression.Expression, 0, len(x.GetArgs()))
for _, arg := range x.GetArgs() {
newArgs = append(newArgs, substituteCorCol2Constant(arg))
}
newSf, _ := expression.NewFunction(x.GetCtx(), x.FuncName.L, x.GetType(), newArgs...)
Copy link
Member

Choose a reason for hiding this comment

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

you may consider the case of "Cast"

Copy link
Member Author

Choose a reason for hiding this comment

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

i will look into this tonight.

return newSf, nil
case *expression.CorrelatedColumn:
return &expression.Constant{Value: x.Data, RetType: x.GetType()}
default:
return x.Clone()
}
}

// SelectionExec represents a filter executor.
type SelectionExec struct {
Src Executor
Condition expression.Expression
ctx context.Context
schema *expression.Schema

// scanController will tell whether this selection need to
// control the condition of below scan executor.
scanController bool
Copy link
Member

Choose a reason for hiding this comment

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

add comments for these two fields.

controllerInit bool
accessConditions []expression.Expression
idxFilterConditions []expression.Expression
tblFilterConditions []expression.Expression
}

// Schema implements the Executor Schema interface.
func (e *SelectionExec) Schema() *expression.Schema {
return e.schema
}

// initController will init the conditions of the below scan executor.
Copy link
Member

Choose a reason for hiding this comment

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

Add more comments for this function.

func (e *SelectionExec) initController() error {
sc := e.ctx.GetSessionVars().StmtCtx
client := e.ctx.GetClient()
accesses := make([]expression.Expression, 0, len(e.accessConditions))
for _, cond := range e.accessConditions {
newCond := substituteCorCol2Constant(cond)
accesses = append(accesses, newCond)
}
switch x := e.Src.(type) {
case *XSelectTableExec:
ranges, err := plan.BuildTableRange(accesses, sc)
if err != nil {
return errors.Trace(err)
}
x.ranges = ranges
tblFilters := make([]expression.Expression, 0, len(e.tblFilterConditions))
for _, cond := range e.tblFilterConditions {
newCond := substituteCorCol2Constant(cond)
tblFilters = append(tblFilters, newCond)
}
var conds []expression.Expression
x.where, _, conds = plan.ExpressionsToPB(sc, tblFilters, client)
e.Condition = expression.ComposeCNFCondition(e.ctx, conds...)
Copy link
Member

Choose a reason for hiding this comment

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

e.Condition don't need to be changed.

return nil
case *XSelectIndexExec:
x.indexPlan.AccessCondition = accesses
err := plan.BuildIndexRange(sc, x.indexPlan)
if err != nil {
return errors.Trace(err)
}
x.indexPlan.IndexConditionPBExpr, _, _ = plan.ExpressionsToPB(sc, e.idxFilterConditions, client)
Copy link
Member

Choose a reason for hiding this comment

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

When do we update the e.idxFilterConditions?

Copy link
Member Author

Choose a reason for hiding this comment

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

e.idxFilterCondtion use the value of *Select.IdxConditions directly, i think this don't need to update in executor phase.

x.indexPlan.TableConditionPBExpr, _, _ = plan.ExpressionsToPB(sc, e.tblFilterConditions, client)
return nil
default:
return errors.New("Error type of PhysicalPlan")
Copy link
Member

Choose a reason for hiding this comment

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

print %T to tout put the plan type

}
}

// Next implements the Executor Next interface.
func (e *SelectionExec) Next() (*Row, error) {
if e.scanController && !e.controllerInit {
err := e.initController()
if err != nil {
return nil, errors.Trace(err)
}
e.controllerInit = true
}
for {
srcRow, err := e.Src.Next()
if err != nil {
Expand All @@ -484,6 +554,9 @@ func (e *SelectionExec) Next() (*Row, error) {
if srcRow == nil {
return nil, nil
}
if e.Condition == nil {
Copy link
Member

Choose a reason for hiding this comment

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

we can remove this.

return srcRow, nil
}
match, err := expression.EvalBool(e.Condition, srcRow.Data, e.ctx)
if err != nil {
return nil, errors.Trace(err)
Expand All @@ -496,6 +569,9 @@ func (e *SelectionExec) Next() (*Row, error) {

// Close implements the Executor Close interface.
func (e *SelectionExec) Close() error {
if e.scanController {
e.controllerInit = false
}
return e.Src.Close()
}

Expand Down
3 changes: 2 additions & 1 deletion plan/expr_to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
"github.com/pingcap/tipb/go-tipb"
)

func expressionsToPB(sc *variable.StatementContext, exprs []expression.Expression, client kv.Client) (pbExpr *tipb.Expr, pushed []expression.Expression, remained []expression.Expression) {
// ExpressionsToPB change expression to tipb.Expr.
Copy link
Member

Choose a reason for hiding this comment

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

change -> converts

func ExpressionsToPB(sc *variable.StatementContext, exprs []expression.Expression, client kv.Client) (pbExpr *tipb.Expr, pushed []expression.Expression, remained []expression.Expression) {
pc := pbConverter{client: client, sc: sc}
for _, expr := range exprs {
v := pc.exprToPB(expr)
Expand Down
7 changes: 7 additions & 0 deletions plan/logical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ type Selection struct {

// onTable means if this selection's child is a table scan or index scan.
onTable bool

// If ScanController is true, then the child of this selection is a scan,
// which use pk or index
Copy link
Member

Choose a reason for hiding this comment

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

add .

ScanController bool
AccessConditions []expression.Expression
IdxConditions []expression.Expression
TblConditions []expression.Expression
}

func (p *Selection) extractCorrelatedCols() []*expression.CorrelatedColumn {
Expand Down
Loading