Skip to content

Commit

Permalink
expression: implement vectorized evaluation for builtinPowSig (#12242)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsthght authored and sre-bot committed Sep 18, 2019
1 parent f4a3bc5 commit a1a308b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
19 changes: 19 additions & 0 deletions expression/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,25 @@ func (g *defaultGener) gen() interface{} {
return nil
}

// rangeRealGener is used to generate float64 items in [begin, end].
type rangeRealGener struct {
begin float64
end float64

nullRation float64
}

func (g *rangeRealGener) gen() interface{} {
if rand.Float64() < g.nullRation {
return nil
}
if g.end <= g.begin {
g.begin = -100
g.end = 100
}
return rand.Float64()*(g.end-g.begin) + g.begin
}

// rangeInt64Gener is used to generate int64 items in [begin, end).
type rangeInt64Gener struct {
begin int
Expand Down
36 changes: 36 additions & 0 deletions expression/builtin_math_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,39 @@ func (b *builtinRoundDecSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.Co
func (b *builtinRoundDecSig) vectorized() bool {
return true
}

func (b *builtinPowSig) vecEvalReal(input *chunk.Chunk, result *chunk.Column) error {
n := input.NumRows()
buf1, err := b.bufAllocator.get(types.ETReal, n)
if err != nil {
return err
}
defer b.bufAllocator.put(buf1)
if err := b.args[0].VecEvalReal(b.ctx, input, buf1); err != nil {
return err
}

if err := b.args[1].VecEvalReal(b.ctx, input, result); err != nil {
return err
}

x := buf1.Float64s()
y := result.Float64s()
result.MergeNulls(buf1)
f64s := result.Float64s()
for i := 0; i < n; i++ {
if result.IsNull(i) {
continue
}
power := math.Pow(x[i], y[i])
if math.IsInf(power, -1) || math.IsInf(power, 1) || math.IsNaN(power) {
return types.ErrOverflow.GenWithStackByArgs("DOUBLE", fmt.Sprintf("pow(%s, %s)", strconv.FormatFloat(x[i], 'f', -1, 64), strconv.FormatFloat(y[i], 'f', -1, 64)))
}
f64s[i] = power
}
return nil
}

func (b *builtinPowSig) vectorized() bool {
return true
}
3 changes: 3 additions & 0 deletions expression/builtin_math_vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ var vecBuiltinMathCases = map[string][]vecExprBenchCase{
ast.Round: {
{types.ETDecimal, []types.EvalType{types.ETDecimal}, nil},
},
ast.Pow: {
{types.ETReal, []types.EvalType{types.ETReal, types.ETReal}, []dataGenerator{&rangeRealGener{0, 10, 0.5}, &rangeRealGener{0, 100, 0.5}}},
},
}

func (s *testEvaluatorSuite) TestVectorizedBuiltinMathEvalOneVec(c *C) {
Expand Down

0 comments on commit a1a308b

Please sign in to comment.