@@ -24,6 +24,7 @@ import (
24
24
"math/rand"
25
25
"strconv"
26
26
"strings"
27
+ "sync"
27
28
"time"
28
29
29
30
"github.com/cznic/mathutil"
@@ -966,7 +967,7 @@ func (c *randFunctionClass) getFunction(ctx sessionctx.Context, args []Expressio
966
967
bt := bf
967
968
if len (args ) == 0 {
968
969
seed := time .Now ().UnixNano ()
969
- sig = & builtinRandSig {bt , rand .New (rand .NewSource (seed ))}
970
+ sig = & builtinRandSig {bt , & sync. Mutex {}, rand .New (rand .NewSource (seed ))}
970
971
} else if _ , isConstant := args [0 ].(* Constant ); isConstant {
971
972
// According to MySQL manual:
972
973
// If an integer argument N is specified, it is used as the seed value:
@@ -979,7 +980,7 @@ func (c *randFunctionClass) getFunction(ctx sessionctx.Context, args []Expressio
979
980
if isNull {
980
981
seed = time .Now ().UnixNano ()
981
982
}
982
- sig = & builtinRandSig {bt , rand .New (rand .NewSource (seed ))}
983
+ sig = & builtinRandSig {bt , & sync. Mutex {}, rand .New (rand .NewSource (seed ))}
983
984
} else {
984
985
sig = & builtinRandWithSeedSig {bt }
985
986
}
@@ -988,19 +989,23 @@ func (c *randFunctionClass) getFunction(ctx sessionctx.Context, args []Expressio
988
989
989
990
type builtinRandSig struct {
990
991
baseBuiltinFunc
992
+ mu * sync.Mutex
991
993
randGen * rand.Rand
992
994
}
993
995
994
996
func (b * builtinRandSig ) Clone () builtinFunc {
995
- newSig := & builtinRandSig {randGen : b .randGen }
997
+ newSig := & builtinRandSig {randGen : b .randGen , mu : b . mu }
996
998
newSig .cloneFrom (& b .baseBuiltinFunc )
997
999
return newSig
998
1000
}
999
1001
1000
1002
// evalReal evals RAND().
1001
1003
// See https://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_rand
1002
1004
func (b * builtinRandSig ) evalReal (row chunk.Row ) (float64 , bool , error ) {
1003
- return b .randGen .Float64 (), false , nil
1005
+ b .mu .Lock ()
1006
+ res := b .randGen .Float64 ()
1007
+ b .mu .Unlock ()
1008
+ return res , false , nil
1004
1009
}
1005
1010
1006
1011
type builtinRandWithSeedSig struct {
0 commit comments