Skip to content

Commit

Permalink
expression: implement vectorized evaluation for builtinLogicOrSig (pi…
Browse files Browse the repository at this point in the history
…ngcap#12365)

* expression: implement vectorized evaluation for builtinLogicOrSig

* Use the generator with predefined test cases for both logical AND and OR operators
  • Loading branch information
k-ye authored and ngaut committed Sep 26, 2019
1 parent 35e308f commit de2df75
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 6 deletions.
39 changes: 37 additions & 2 deletions expression/builtin_op_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,46 @@ func (b *builtinTimeIsNullSig) vecEvalInt(input *chunk.Chunk, result *chunk.Colu
}

func (b *builtinLogicOrSig) vectorized() bool {
return false
return true
}

func (b *builtinLogicOrSig) vecEvalInt(input *chunk.Chunk, result *chunk.Column) error {
return errors.Errorf("not implemented")
if err := b.args[0].VecEvalInt(b.ctx, input, result); err != nil {
return err
}

n := input.NumRows()
buf, err := b.bufAllocator.get(types.ETInt, n)
if err != nil {
return err
}
defer b.bufAllocator.put(buf)
if err := b.args[1].VecEvalInt(b.ctx, input, buf); err != nil {
return err
}

i64s := result.Int64s()
arg1s := buf.Int64s()

for i := 0; i < n; i++ {
isNull0 := result.IsNull(i)
isNull1 := buf.IsNull(i)
// Because buf is used to store the conversion of args[0] in place, it could
// be that args[0] is null and args[1] is nonzero, in which case the result
// is 1. In these cases, we need to clear the null bit mask of the corresponding
// row in result.
// See https://dev.mysql.com/doc/refman/5.7/en/logical-operators.html#operator_or
isNull := false
if (!isNull0 && i64s[i] != 0) || (!isNull1 && arg1s[i] != 0) {
i64s[i] = 1
} else if isNull0 || isNull1 {
isNull = true
} else {
i64s[i] = 0
}
result.SetNull(i, isNull)
}
return nil
}

func (b *builtinBitOrSig) vectorized() bool {
Expand Down
70 changes: 66 additions & 4 deletions expression/builtin_op_vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,79 @@ import (
var vecBuiltinOpCases = map[string][]vecExprBenchCase{
ast.IsTruth: {},
ast.IsFalsity: {},
ast.LogicOr: {},
ast.LogicXor: {},
ast.Xor: {},
ast.LogicOr: {
{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}, geners: makeBinaryLogicOpDataGens()},
},
ast.LogicXor: {},
ast.Xor: {},
ast.LogicAnd: {
{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}},
{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}, geners: makeBinaryLogicOpDataGens()},
},
ast.UnaryNot: {},
ast.UnaryMinus: {},
ast.IsNull: {},
}

// givenValsGener returns the items sequentially from the slice given at
// the construction time. If this slice is exhausted, it falls back to
// the fallback generator.
type givenValsGener struct {
given []interface{}
idx int
fallback dataGenerator
}

func (g *givenValsGener) gen() interface{} {
if g.idx >= len(g.given) {
return g.fallback.gen()
}
v := g.given[g.idx]
g.idx++
return v
}

func makeGivenValsOrDefaultGener(vals []interface{}, eType types.EvalType) *givenValsGener {
g := &givenValsGener{}
g.given = vals
g.fallback = &defaultGener{0.2, eType}
return g
}

func makeBinaryLogicOpDataGens() []dataGenerator {
// TODO: This data generator currently only applies to AND and OR operators,
// so its name may be too generic.
pairs := [][]interface{}{
{nil, nil},
{0, nil},
{nil, 0},
{1, nil},
{nil, 1},
{0, 0},
{0, 1},
{1, 0},
{1, 1},
{-1, 1},
}

maybeToInt64 := func(v interface{}) interface{} {
if v == nil {
return nil
}
return int64(v.(int))
}

n := len(pairs)
arg0s := make([]interface{}, n)
arg1s := make([]interface{}, n)
for i, p := range pairs {
arg0s[i] = maybeToInt64(p[0])
arg1s[i] = maybeToInt64(p[1])
}
return []dataGenerator{
makeGivenValsOrDefaultGener(arg0s, types.ETInt),
makeGivenValsOrDefaultGener(arg1s, types.ETInt)}
}

func (s *testEvaluatorSuite) TestVectorizedBuiltinOpFunc(c *C) {
testVectorizedBuiltinFunc(c, vecBuiltinOpCases)
}
Expand Down

0 comments on commit de2df75

Please sign in to comment.