Skip to content

Commit

Permalink
*: refine EvalBool function. (#3139)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanfei1991 authored and shenli committed Apr 25, 2017
1 parent b9e87ee commit 7699d3f
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 148 deletions.
42 changes: 21 additions & 21 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func (b *executorBuilder) buildUnionScanExec(v *plan.PhysicalUnionScan) Executor
case *XSelectTableExec:
us.desc = x.desc
us.dirty = getDirtyDB(b.ctx).getDirtyTable(x.table.Meta().ID)
us.condition = v.Condition
us.conditions = v.Conditions
us.buildAndSortAddedRows(x.table, x.asName)
case *XSelectIndexExec:
us.desc = x.indexPlan.Desc
Expand All @@ -353,7 +353,7 @@ func (b *executorBuilder) buildUnionScanExec(v *plan.PhysicalUnionScan) Executor
}
}
us.dirty = getDirtyDB(b.ctx).getDirtyTable(x.table.Meta().ID)
us.condition = v.Condition
us.conditions = v.Conditions
us.buildAndSortAddedRows(x.table, x.asName)
default:
// The mem table will not be written by sql directly, so we can omit the union scan to avoid err reporting.
Expand All @@ -369,9 +369,9 @@ func (b *executorBuilder) buildMergeJoin(v *plan.PhysicalMergeJoin) Executor {
LeftChild(b.build(v.Children()[0])).
RightChild(b.build(v.Children()[1])).
EqualConditions(v.EqualConditions).
LeftFilter(expression.ComposeCNFCondition(b.ctx, v.LeftConditions...)).
RightFilter(expression.ComposeCNFCondition(b.ctx, v.RightConditions...)).
OtherFilter(expression.ComposeCNFCondition(b.ctx, v.OtherConditions...)).
LeftFilter(v.LeftConditions).
RightFilter(v.RightConditions).
OtherFilter(v.OtherConditions).
Schema(v.Schema()).
JoinType(v.JoinType).
DefaultVals(v.DefaultValues).
Expand Down Expand Up @@ -401,23 +401,23 @@ func (b *executorBuilder) buildHashJoin(v *plan.PhysicalHashJoin) Executor {
}
e := &HashJoinExec{
schema: v.Schema(),
otherFilter: expression.ComposeCNFCondition(b.ctx, v.OtherConditions...),
otherFilter: v.OtherConditions,
prepared: false,
ctx: b.ctx,
targetTypes: targetTypes,
concurrency: v.Concurrency,
defaultValues: v.DefaultValues,
}
if v.SmallTable == 1 {
e.smallFilter = expression.ComposeCNFCondition(b.ctx, v.RightConditions...)
e.bigFilter = expression.ComposeCNFCondition(b.ctx, v.LeftConditions...)
e.smallFilter = v.RightConditions
e.bigFilter = v.LeftConditions
e.smallHashKey = rightHashKey
e.bigHashKey = leftHashKey
e.leftSmall = false
} else {
e.leftSmall = true
e.smallFilter = expression.ComposeCNFCondition(b.ctx, v.LeftConditions...)
e.bigFilter = expression.ComposeCNFCondition(b.ctx, v.RightConditions...)
e.smallFilter = v.LeftConditions
e.bigFilter = v.RightConditions
e.smallHashKey = leftHashKey
e.bigHashKey = rightHashKey
}
Expand All @@ -434,10 +434,10 @@ func (b *executorBuilder) buildHashJoin(v *plan.PhysicalHashJoin) Executor {
for i := 0; i < e.concurrency; i++ {
ctx := &hashJoinCtx{}
if e.bigFilter != nil {
ctx.bigFilter = e.bigFilter.Clone()
ctx.bigFilter = e.bigFilter
}
if e.otherFilter != nil {
ctx.otherFilter = e.otherFilter.Clone()
ctx.otherFilter = e.otherFilter
}
ctx.datumBuffer = make([]types.Datum, len(e.bigHashKey))
ctx.hashKeyBuffer = make([]byte, 0, 10000)
Expand All @@ -458,9 +458,9 @@ func (b *executorBuilder) buildSemiJoin(v *plan.PhysicalHashSemiJoin) *HashSemiJ
}
e := &HashSemiJoinExec{
schema: v.Schema(),
otherFilter: expression.ComposeCNFCondition(b.ctx, v.OtherConditions...),
bigFilter: expression.ComposeCNFCondition(b.ctx, v.LeftConditions...),
smallFilter: expression.ComposeCNFCondition(b.ctx, v.RightConditions...),
otherFilter: v.OtherConditions,
bigFilter: v.LeftConditions,
smallFilter: v.RightConditions,
bigExec: b.build(v.Children()[0]),
smallExec: b.build(v.Children()[1]),
prepared: false,
Expand Down Expand Up @@ -649,9 +649,9 @@ func (b *executorBuilder) buildNestedLoopJoin(v *plan.PhysicalHashJoin) *NestedL
SmallExec: b.build(v.Children()[1]),
BigExec: b.build(v.Children()[0]),
Ctx: b.ctx,
BigFilter: expression.ComposeCNFCondition(b.ctx, v.LeftConditions...),
SmallFilter: expression.ComposeCNFCondition(b.ctx, v.RightConditions...),
OtherFilter: expression.ComposeCNFCondition(b.ctx, append(expression.ScalarFuncs2Exprs(v.EqualConditions), v.OtherConditions...)...),
BigFilter: v.LeftConditions,
SmallFilter: v.RightConditions,
OtherFilter: append(expression.ScalarFuncs2Exprs(v.EqualConditions), v.OtherConditions...),
schema: v.Schema(),
outer: v.JoinType != plan.InnerJoin,
defaultValues: v.DefaultValues,
Expand All @@ -662,9 +662,9 @@ func (b *executorBuilder) buildNestedLoopJoin(v *plan.PhysicalHashJoin) *NestedL
BigExec: b.build(v.Children()[1]),
leftSmall: true,
Ctx: b.ctx,
BigFilter: expression.ComposeCNFCondition(b.ctx, v.RightConditions...),
SmallFilter: expression.ComposeCNFCondition(b.ctx, v.LeftConditions...),
OtherFilter: expression.ComposeCNFCondition(b.ctx, append(expression.ScalarFuncs2Exprs(v.EqualConditions), v.OtherConditions...)...),
BigFilter: v.RightConditions,
SmallFilter: v.LeftConditions,
OtherFilter: append(expression.ScalarFuncs2Exprs(v.EqualConditions), v.OtherConditions...),
schema: v.Schema(),
outer: v.JoinType != plan.InnerJoin,
defaultValues: v.DefaultValues,
Expand Down
22 changes: 7 additions & 15 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,9 @@ func (e *TableDualExec) Close() error {

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

// scanController will tell whether this selection need to
// control the condition of below scan executor.
Expand Down Expand Up @@ -545,18 +544,11 @@ func (e *SelectionExec) Next() (*Row, error) {
if srcRow == nil {
return nil, nil
}
allMatch := true
for _, cond := range e.Conditions {
match, err := expression.EvalBool(cond, srcRow.Data, e.ctx)
if err != nil {
return nil, errors.Trace(err)
}
if !match {
allMatch = false
break
}
match, err := expression.EvalBool(e.Conditions, srcRow.Data, e.ctx)
if err != nil {
return nil, errors.Trace(err)
}
if allMatch {
if match {
return srcRow, nil
}
}
Expand Down
117 changes: 45 additions & 72 deletions executor/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ type HashJoinExec struct {
bigExec Executor
prepared bool
ctx context.Context
smallFilter expression.Expression
bigFilter expression.Expression
otherFilter expression.Expression
smallFilter []expression.Expression
bigFilter []expression.Expression
otherFilter []expression.Expression
schema *expression.Schema
outer bool
leftSmall bool
Expand Down Expand Up @@ -75,8 +75,8 @@ type HashJoinExec struct {

// hashJoinCtx holds the variables needed to do a hash join in one of many concurrent goroutines.
type hashJoinCtx struct {
bigFilter expression.Expression
otherFilter expression.Expression
bigFilter []expression.Expression
otherFilter []expression.Expression
// Buffer used for encode hash keys.
datumBuffer []types.Datum
hashKeyBuffer []byte
Expand Down Expand Up @@ -228,15 +228,12 @@ func (e *HashJoinExec) prepare() error {
break
}

matched := true
if e.smallFilter != nil {
matched, err = expression.EvalBool(e.smallFilter, row.Data, e.ctx)
if err != nil {
return errors.Trace(err)
}
if !matched {
continue
}
matched, err := expression.EvalBool(e.smallFilter, row.Data, e.ctx)
if err != nil {
return errors.Trace(err)
}
if !matched {
continue
}
hasNull, joinKey, err := getJoinKey(sc, e.smallHashKey, row, e.targetTypes, e.hashJoinContexts[0].datumBuffer, nil)
if err != nil {
Expand Down Expand Up @@ -417,18 +414,15 @@ func (e *HashJoinExec) constructMatchedRows(ctx *hashJoinCtx, bigRow *Row) (matc
if err != nil {
return nil, errors.Trace(err)
}
otherMatched := true
var matchedRow *Row
if e.leftSmall {
matchedRow = makeJoinRow(smallRow, bigRow)
} else {
matchedRow = makeJoinRow(bigRow, smallRow)
}
if e.otherFilter != nil {
otherMatched, err = expression.EvalBool(ctx.otherFilter, matchedRow.Data, e.ctx)
if err != nil {
return nil, errors.Trace(err)
}
otherMatched, err := expression.EvalBool(ctx.otherFilter, matchedRow.Data, e.ctx)
if err != nil {
return nil, errors.Trace(err)
}
if otherMatched {
matchedRows = append(matchedRows, matchedRow)
Expand Down Expand Up @@ -509,9 +503,9 @@ type NestedLoopJoinExec struct {
leftSmall bool
prepared bool
Ctx context.Context
SmallFilter expression.Expression
BigFilter expression.Expression
OtherFilter expression.Expression
SmallFilter []expression.Expression
BigFilter []expression.Expression
OtherFilter []expression.Expression
schema *expression.Schema
outer bool
defaultValues []types.Datum
Expand Down Expand Up @@ -545,12 +539,9 @@ func (e *NestedLoopJoinExec) fetchBigRow() (*Row, bool, error) {
return nil, false, e.BigExec.Close()
}

matched := true
if e.BigFilter != nil {
matched, err = expression.EvalBool(e.BigFilter, bigRow.Data, e.Ctx)
if err != nil {
return nil, false, errors.Trace(err)
}
matched, err := expression.EvalBool(e.BigFilter, bigRow.Data, e.Ctx)
if err != nil {
return nil, false, errors.Trace(err)
}
if matched {
return bigRow, true, nil
Expand Down Expand Up @@ -578,15 +569,9 @@ func (e *NestedLoopJoinExec) prepare() error {
return e.SmallExec.Close()
}

matched := true
if e.SmallFilter != nil {
matched, err = expression.EvalBool(e.SmallFilter, row.Data, e.Ctx)
if err != nil {
return errors.Trace(err)
}
if !matched {
continue
}
matched, err := expression.EvalBool(e.SmallFilter, row.Data, e.Ctx)
if err != nil {
return errors.Trace(err)
}
if matched {
e.innerRows = append(e.innerRows, row)
Expand Down Expand Up @@ -621,14 +606,12 @@ func (e *NestedLoopJoinExec) doJoin(bigRow *Row, match bool) ([]*Row, error) {
} else {
mergedRow = makeJoinRow(bigRow, row)
}
if e.OtherFilter != nil {
matched, err := expression.EvalBool(e.OtherFilter, mergedRow.Data, e.Ctx)
if err != nil {
return nil, errors.Trace(err)
}
if !matched {
continue
}
matched, err := expression.EvalBool(e.OtherFilter, mergedRow.Data, e.Ctx)
if err != nil {
return nil, errors.Trace(err)
}
if !matched {
continue
}
e.resultRows = append(e.resultRows, mergedRow)
}
Expand Down Expand Up @@ -673,9 +656,9 @@ type HashSemiJoinExec struct {
bigExec Executor
prepared bool
ctx context.Context
smallFilter expression.Expression
bigFilter expression.Expression
otherFilter expression.Expression
smallFilter []expression.Expression
bigFilter []expression.Expression
otherFilter []expression.Expression
schema *expression.Schema
resultRows []*Row
// In auxMode, the result row always returns with an extra column which stores a boolean
Expand Down Expand Up @@ -725,15 +708,12 @@ func (e *HashSemiJoinExec) prepare() error {
break
}

matched := true
if e.smallFilter != nil {
matched, err = expression.EvalBool(e.smallFilter, row.Data, e.ctx)
if err != nil {
return errors.Trace(err)
}
if !matched {
continue
}
matched, err := expression.EvalBool(e.smallFilter, row.Data, e.ctx)
if err != nil {
return errors.Trace(err)
}
if !matched {
continue
}
hasNull, hashcode, err := getJoinKey(sc, e.smallHashKey, row, e.targetTypes, make([]types.Datum, len(e.smallHashKey)), nil)
if err != nil {
Expand Down Expand Up @@ -769,14 +749,10 @@ func (e *HashSemiJoinExec) rowIsMatched(bigRow *Row) (matched bool, hasNull bool
}
// match eq condition
for _, smallRow := range rows {
matched = true
if e.otherFilter != nil {
var matchedRow *Row
matchedRow = makeJoinRow(bigRow, smallRow)
matched, err = expression.EvalBool(e.otherFilter, matchedRow.Data, e.ctx)
if err != nil {
return false, false, errors.Trace(err)
}
matchedRow := makeJoinRow(bigRow, smallRow)
matched, err = expression.EvalBool(e.otherFilter, matchedRow.Data, e.ctx)
if err != nil {
return false, false, errors.Trace(err)
}
if matched {
return
Expand All @@ -795,12 +771,9 @@ func (e *HashSemiJoinExec) fetchBigRow() (*Row, bool, error) {
return nil, false, errors.Trace(e.bigExec.Close())
}

matched := true
if e.bigFilter != nil {
matched, err = expression.EvalBool(e.bigFilter, bigRow.Data, e.ctx)
if err != nil {
return nil, false, errors.Trace(err)
}
matched, err := expression.EvalBool(e.bigFilter, bigRow.Data, e.ctx)
if err != nil {
return nil, false, errors.Trace(err)
}
if matched {
return bigRow, true, nil
Expand Down
6 changes: 3 additions & 3 deletions executor/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ func (s *testSuite) TestNestedLoopJoin(c *C) {
BigExec: bigExec,
SmallExec: smallExec,
Ctx: mock.NewContext(),
BigFilter: bigFilter,
SmallFilter: smallFilter,
OtherFilter: otherFilter,
BigFilter: []expression.Expression{bigFilter},
SmallFilter: []expression.Expression{smallFilter},
OtherFilter: []expression.Expression{otherFilter},
}
row, err := join.Next()
c.Check(err, IsNil)
Expand Down
Loading

0 comments on commit 7699d3f

Please sign in to comment.