diff --git a/expression/builtin_math.go b/expression/builtin_math.go index db24c77015652..91ffdc16c7757 100644 --- a/expression/builtin_math.go +++ b/expression/builtin_math.go @@ -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 } diff --git a/expression/builtin_math_vec.go b/expression/builtin_math_vec.go index def5de04b6922..b9c83fcc5eda0 100644 --- a/expression/builtin_math_vec.go +++ b/expression/builtin_math_vec.go @@ -17,7 +17,9 @@ import ( "fmt" "hash/crc32" "math" + "math/rand" "strconv" + "time" "github.com/cznic/mathutil" "github.com/pingcap/errors" @@ -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 { diff --git a/expression/builtin_math_vec_test.go b/expression/builtin_math_vec_test.go index 230372109d405..7f6e43833c529 100644 --- a/expression/builtin_math_vec_test.go +++ b/expression/builtin_math_vec_test.go @@ -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}}, },