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

expression: implement vectorized evaluation for builtinPowSig #12242

Merged
merged 11 commits into from
Sep 18, 2019
18 changes: 18 additions & 0 deletions expression/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,24 @@ 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
46 changes: 46 additions & 0 deletions expression/builtin_math_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
package expression

import (
"fmt"
qw4990 marked this conversation as resolved.
Show resolved Hide resolved
"math"
"strconv"

"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
Expand Down Expand Up @@ -223,3 +225,47 @@ 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)
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
defer b.bufAllocator.put(buf1)
if err := b.args[0].VecEvalReal(b.ctx, input, buf1); err != nil {
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
return err
}

buf2, err := b.bufAllocator.get(types.ETReal, n)
if err != nil {
return err
}
defer b.bufAllocator.put(buf2)
if err := b.args[1].VecEvalReal(b.ctx, input, buf2); err != nil {
return err
}
x := buf1.Float64s()
y := buf2.Float64s()
result.ResizeFloat64(n, false)
f64s := result.Float64s()
for i := 0; i < n; i++ {
if (buf1.IsNull(i) || buf2.IsNull(i)) {
result.SetNull(i, true)
continue
}
power := math.Pow(x[i], y[i])
if math.IsInf(power, -1) || math.IsInf(power, 1) || math.IsNaN(power) {
err := types.ErrOverflow.GenWithStackByArgs("DOUBLE", fmt.Sprintf("pow(%s, %s)", strconv.FormatFloat(x[i], 'f', -1, 64), strconv.FormatFloat(y[i], 'f', -1, 64)))
b.ctx.GetSessionVars().StmtCtx.HandleOverflow(err, err)
f64s[i] = 0
} else {
f64s[i] = power
}
}
return nil
}

func (b *builtinPowSig) vectorized() bool {
return true
}
4 changes: 4 additions & 0 deletions expression/builtin_math_vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ 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}}},

zz-jason marked this conversation as resolved.
Show resolved Hide resolved
},
}

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