Skip to content

Commit

Permalink
expression: implement vectorized evaluation for 'builtinRandWithSeedS…
Browse files Browse the repository at this point in the history
…ig' (#12644)
  • Loading branch information
tsthght authored and qw4990 committed Nov 6, 2019
1 parent 9cc0f64 commit 4bb68c5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
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]))
}
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

0 comments on commit 4bb68c5

Please sign in to comment.