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: fix data race of rand function (#11168) #11169

Merged
merged 1 commit into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions expression/builtin_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"math/rand"
"strconv"
"strings"
"sync"
"time"

"github.com/cznic/mathutil"
Expand Down Expand Up @@ -966,7 +967,7 @@ func (c *randFunctionClass) getFunction(ctx sessionctx.Context, args []Expressio
bt := bf
if len(args) == 0 {
seed := time.Now().UnixNano()
sig = &builtinRandSig{bt, rand.New(rand.NewSource(seed))}
sig = &builtinRandSig{bt, &sync.Mutex{}, rand.New(rand.NewSource(seed))}
} else if _, isConstant := args[0].(*Constant); isConstant {
// According to MySQL manual:
// If an integer argument N is specified, it is used as the seed value:
Expand All @@ -979,7 +980,7 @@ func (c *randFunctionClass) getFunction(ctx sessionctx.Context, args []Expressio
if isNull {
seed = time.Now().UnixNano()
}
sig = &builtinRandSig{bt, rand.New(rand.NewSource(seed))}
sig = &builtinRandSig{bt, &sync.Mutex{}, rand.New(rand.NewSource(seed))}
} else {
sig = &builtinRandWithSeedSig{bt}
}
Expand All @@ -988,19 +989,23 @@ func (c *randFunctionClass) getFunction(ctx sessionctx.Context, args []Expressio

type builtinRandSig struct {
baseBuiltinFunc
mu *sync.Mutex
randGen *rand.Rand
}

func (b *builtinRandSig) Clone() builtinFunc {
newSig := &builtinRandSig{randGen: b.randGen}
newSig := &builtinRandSig{randGen: b.randGen, mu: b.mu}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}

// evalReal evals RAND().
// See https://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_rand
func (b *builtinRandSig) evalReal(row chunk.Row) (float64, bool, error) {
return b.randGen.Float64(), false, nil
b.mu.Lock()
res := b.randGen.Float64()
b.mu.Unlock()
return res, false, nil
}

type builtinRandWithSeedSig struct {
Expand Down
2 changes: 2 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,8 @@ func (s *testIntegrationSuite) TestMathBuiltin(c *C) {
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int)")
tk.MustExec("insert into t values(1),(2),(3)")
tk.Se.GetSessionVars().MaxChunkSize = 1
tk.MustQuery("select rand(1) from t").Sort().Check(testkit.Rows("0.6046602879796196", "0.6645600532184904", "0.9405090880450124"))
tk.MustQuery("select rand(a) from t").Check(testkit.Rows("0.6046602879796196", "0.16729663442585624", "0.7199826688373036"))
tk.MustQuery("select rand(1), rand(2), rand(3)").Check(testkit.Rows("0.6046602879796196 0.16729663442585624 0.7199826688373036"))
}
Expand Down