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: refactor builtin function RAND to be compatible with MySQL #9387

Merged
merged 4 commits into from
Feb 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion executor/projection.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (e *ProjectionExec) prepare(ctx context.Context) {
for i := int64(0); i < e.numWorkers; i++ {
e.workers = append(e.workers, &projectionWorker{
sctx: e.ctx,
evaluatorSuit: e.evaluatorSuit,
evaluatorSuit: e.evaluatorSuit.Clone(),
globalFinishCh: e.finishCh,
inputGiveBackCh: e.fetcher.inputCh,
inputCh: make(chan *projectionInput, 1),
Expand Down
37 changes: 26 additions & 11 deletions expression/builtin_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -966,8 +966,23 @@ func (c *randFunctionClass) getFunction(ctx sessionctx.Context, args []Expressio
bt := bf
if len(args) == 0 {
sig = &builtinRandSig{bt, nil}
XuHuaiyu marked this conversation as resolved.
Show resolved Hide resolved
} 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:
// With a constant initializer argument, the seed is initialized once
// when the statement is prepared, prior to execution.
seed, isNull, err := args[0].EvalInt(ctx, chunk.Row{})
if err != nil {
return nil, err
}
if isNull {
bf.args = bf.args[1:]
seed = time.Now().UnixNano()
}
bt.args = bt.args[:1]
XuHuaiyu marked this conversation as resolved.
Show resolved Hide resolved
sig = &builtinRandSig{bt, rand.New(rand.NewSource(seed))}
} else {
sig = &builtinRandWithSeedSig{bt, nil}
sig = &builtinRandWithSeedSig{bt}
}
return sig, nil
}
Expand All @@ -994,11 +1009,10 @@ func (b *builtinRandSig) evalReal(row chunk.Row) (float64, bool, error) {

type builtinRandWithSeedSig struct {
baseBuiltinFunc
randGen *rand.Rand
}

func (b *builtinRandWithSeedSig) Clone() builtinFunc {
newSig := &builtinRandWithSeedSig{randGen: b.randGen}
newSig := &builtinRandWithSeedSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
Expand All @@ -1010,15 +1024,16 @@ func (b *builtinRandWithSeedSig) evalReal(row chunk.Row) (float64, bool, error)
if err != nil {
return 0, true, err
}
if b.randGen == nil {
if isNull {
// When seed is NULL, it is equal to RAND().
b.randGen = rand.New(rand.NewSource(time.Now().UnixNano()))
} else {
b.randGen = rand.New(rand.NewSource(seed))
}
// b.args[0] is promised to be a non-constant(such as a column name) in
// 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 {
randGen = rand.New(rand.NewSource(seed))
}
return b.randGen.Float64(), false, nil
return randGen.Float64(), false, nil
}

type powFunctionClass struct {
Expand Down
23 changes: 23 additions & 0 deletions expression/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ func (e *defaultEvaluator) run(ctx sessionctx.Context, input, output *chunk.Chun
return nil
}

func (e *defaultEvaluator) clone() *defaultEvaluator {
de := &defaultEvaluator{outputIdxes: e.outputIdxes, vectorizable: e.vectorizable}
for i := range e.exprs {
de.exprs = append(de.exprs, e.exprs[i].Clone())
}
return de
}

// EvaluatorSuite is responsible for the evaluation of a list of expressions.
// It separates them to "column" and "other" expressions and evaluates "other"
// expressions before "column" expressions.
Expand Down Expand Up @@ -121,3 +129,18 @@ func (e *EvaluatorSuite) Run(ctx sessionctx.Context, input, output *chunk.Chunk)
}
return nil
}

// Clone clone an EvaluatorSuite.
// Note: This is NOT deep clone, since all the attributes of columnEvaluator and
// defaultEvaluator are READ-ONLY during evaluating except for
// defaultEvaluator.exprs.
func (e *EvaluatorSuite) Clone() *EvaluatorSuite {
if e.defaultEvaluator == nil {
return e
}
es := &EvaluatorSuite{
columnEvaluator: e.columnEvaluator,
defaultEvaluator: e.defaultEvaluator.clone(),
}
return es
}
7 changes: 7 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,13 @@ func (s *testIntegrationSuite) TestMathBuiltin(c *C) {
// for radians
result = tk.MustQuery("SELECT radians(1.0), radians(pi()), radians(pi()/2), radians(180), radians(1.009);")
result.Check(testkit.Rows("0.017453292519943295 0.05483113556160754 0.02741556778080377 3.141592653589793 0.01761037215262278"))

// for rand
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int)")
tk.MustExec("insert into t values(1),(2),(3)")
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"))
}

func (s *testIntegrationSuite) TestStringBuiltin(c *C) {
Expand Down