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
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)
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
}

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