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 'builtinRandWithSeedSig' #12644

Merged
merged 14 commits into from
Nov 6, 2019
Merged
6 changes: 3 additions & 3 deletions expression/builtin_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,10 +1029,10 @@ func (b *builtinRandWithSeedSig) evalReal(row chunk.Row) (float64, bool, error)
// builtinRandWithSeedSig, the seed is initialized with the value for each
// invocation of RAND().
var randGen *rand.Rand
if isNull {
randGen = rand.New(rand.NewSource(time.Now().UnixNano()))
} else {
if !isNull {
randGen = rand.New(rand.NewSource(seed))
} else {
randGen = rand.New(rand.NewSource(time.Now().UnixNano()))
}
return randGen.Float64(), false, nil
}
Expand Down
27 changes: 25 additions & 2 deletions expression/builtin_math_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import (
"fmt"
"hash/crc32"
"math"
"math/rand"
"strconv"
"time"

"github.com/cznic/mathutil"
"github.com/pingcap/errors"
Expand Down Expand Up @@ -706,11 +708,32 @@ func (b *builtinRandSig) vecEvalReal(input *chunk.Chunk, result *chunk.Column) e
}

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

func (b *builtinRandWithSeedSig) vecEvalReal(input *chunk.Chunk, result *chunk.Column) error {
return errors.Errorf("not implemented")
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[0].VecEvalInt(b.ctx, input, buf); err != nil {
return err
}

result.ResizeFloat64(n, false)
i64s := buf.Int64s()
f64s := result.Float64s()
rander := rand.NewSource(time.Now().UnixNano())
randGen := rand.New(rander)
for i := 0; i < n; i++ {
if !buf.IsNull(i) {
randGen = rand.New(rand.NewSource(i64s[i]))
qw4990 marked this conversation as resolved.
Show resolved Hide resolved
}
f64s[i] = randGen.Float64()
}
return nil
}

func (b *builtinCeilIntToDecSig) vectorized() bool {
Expand Down
3 changes: 3 additions & 0 deletions expression/builtin_math_vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ var vecBuiltinMathCases = map[string][]vecExprBenchCase{
{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}, geners: []dataGenerator{nil, &rangeInt64Gener{-10, 10}}},
{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}, childrenFieldTypes: []*types.FieldType{{Tp: mysql.TypeInt24, Flag: mysql.UnsignedFlag}}, geners: []dataGenerator{nil, &rangeInt64Gener{-10, 10}}},
},
ast.Rand: {
{retEvalType: types.ETReal, childrenTypes: []types.EvalType{types.ETInt}, geners: []dataGenerator{&defaultGener{0, types.ETInt}}},
},
ast.CRC32: {
{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETString}},
},
Expand Down