Skip to content

Commit

Permalink
expression: change test for string_test. (pingcap#2458)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanfei1991 authored Jan 17, 2017
1 parent 1e34270 commit e79a460
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 139 deletions.
29 changes: 16 additions & 13 deletions expression/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ import (

// baseBuiltinFunc will be contained in every struct that implement builtinFunc interface.
type baseBuiltinFunc struct {
args []Expression
argValues []types.Datum
ctx context.Context
self builtinFunc
args []Expression
argValues []types.Datum
ctx context.Context
deterministic bool
}

func newBaseBuiltinFunc(args []Expression, ctx context.Context) baseBuiltinFunc {
return baseBuiltinFunc{
args: args,
argValues: make([]types.Datum, len(args)),
ctx: ctx,
args: args,
argValues: make([]types.Datum, len(args)),
ctx: ctx,
deterministic: true,
}
}

Expand All @@ -55,7 +56,7 @@ func (b *baseBuiltinFunc) evalArgs(row []types.Datum) (_ []types.Datum, err erro

// isDeterministic will be true by default. Non-deterministic function will override this function.
func (b *baseBuiltinFunc) isDeterministic() bool {
return true
return b.deterministic
}

func (b *baseBuiltinFunc) getArgs() []Expression {
Expand All @@ -65,7 +66,7 @@ func (b *baseBuiltinFunc) getArgs() []Expression {
// equal only checks if both functions are non-deterministic and if these arguments are same.
// Function name will be checked outside.
func (b *baseBuiltinFunc) equal(fun builtinFunc) bool {
if !b.self.isDeterministic() || !fun.isDeterministic() {
if !b.isDeterministic() || !fun.isDeterministic() {
return false
}
funArgs := fun.getArgs()
Expand Down Expand Up @@ -315,13 +316,14 @@ var funcs = map[string]functionClass{
ast.Power: &powFunctionClass{baseFunctionClass{ast.Pow, 2, 2}},
ast.Rand: &randFunctionClass{baseFunctionClass{ast.Rand, 0, 1}},
ast.Round: &roundFunctionClass{baseFunctionClass{ast.Round, 1, 2}},
ast.Sign: &signFunctionClass{baseFunctionClass{ast.Sign, 1, 1}},
ast.Conv: &convFunctionClass{baseFunctionClass{ast.Conv, 3, 3}},
ast.CRC32: &crc32FunctionClass{baseFunctionClass{ast.CRC32, 1, 1}},

// time functions
ast.Curdate: &currentDateFunctionClass{baseFunctionClass{ast.Curdate, 0, 0}},
ast.CurrentDate: &currentDateFunctionClass{baseFunctionClass{ast.CurrentDate, 0, 0}},
ast.CurrentTime: &currentTimeFunctionClass{baseFunctionClass{ast.CurrentTime, 0, 0}},
ast.CurrentTime: &currentTimeFunctionClass{baseFunctionClass{ast.CurrentTime, 0, 1}},
ast.Date: &dateFunctionClass{baseFunctionClass{ast.Date, 1, 1}},
ast.DateDiff: &dateDiffFunctionClass{baseFunctionClass{ast.DateDiff, 2, 2}},
ast.DateAdd: &dateArithFunctionClass{baseFunctionClass{ast.DateAdd, 3, 3}, ast.DateArithAdd},
Expand Down Expand Up @@ -355,6 +357,7 @@ var funcs = map[string]functionClass{
ast.YearWeek: &yearWeekFunctionClass{baseFunctionClass{ast.YearWeek, 1, 2}},
ast.FromUnixTime: &fromUnixTimeFunctionClass{baseFunctionClass{ast.FromUnixTime, 1, 2}},
ast.TimeDiff: &timeDiffFunctionClass{baseFunctionClass{ast.TimeDiff, 2, 2}},
ast.TimestampDiff: &timestampDiffFunctionClass{baseFunctionClass{ast.TimestampDiff, 3, 3}},
ast.UnixTimestamp: &unixTimestampFunctionClass{baseFunctionClass{ast.UnixTimestamp, 0, 1}},

// string functions
Expand Down Expand Up @@ -421,7 +424,7 @@ var funcs = map[string]functionClass{
ast.LE: &compareFunctionClass{baseFunctionClass{ast.LE, 2, 2}, opcode.LE},
ast.EQ: &compareFunctionClass{baseFunctionClass{ast.EQ, 2, 2}, opcode.EQ},
ast.NE: &compareFunctionClass{baseFunctionClass{ast.NE, 2, 2}, opcode.NE},
ast.LT: &compareFunctionClass{baseFunctionClass{ast.LT, 2, 2}, opcode.LE},
ast.LT: &compareFunctionClass{baseFunctionClass{ast.LT, 2, 2}, opcode.LT},
ast.GT: &compareFunctionClass{baseFunctionClass{ast.GT, 2, 2}, opcode.GT},
ast.NullEQ: &compareFunctionClass{baseFunctionClass{ast.NullEQ, 2, 2}, opcode.NullEQ},
ast.Plus: &arithmeticFunctionClass{baseFunctionClass{ast.Plus, 2, 2}, opcode.Plus},
Expand All @@ -435,7 +438,7 @@ var funcs = map[string]functionClass{
ast.And: &bitOpFunctionClass{baseFunctionClass{ast.And, 2, 2}, opcode.And},
ast.Or: &bitOpFunctionClass{baseFunctionClass{ast.Or, 2, 2}, opcode.Or},
ast.Xor: &bitOpFunctionClass{baseFunctionClass{ast.Xor, 2, 2}, opcode.Xor},
ast.LogicXor: &bitOpFunctionClass{baseFunctionClass{ast.LogicXor, 2, 2}, opcode.LogicXor},
ast.LogicXor: &logicXorFunctionClass{baseFunctionClass{ast.LogicXor, 2, 2}},
ast.UnaryNot: &unaryOpFunctionClass{baseFunctionClass{ast.UnaryNot, 1, 1}, opcode.Not},
ast.BitNeg: &unaryOpFunctionClass{baseFunctionClass{ast.BitNeg, 1, 1}, opcode.BitNeg},
ast.UnaryPlus: &unaryOpFunctionClass{baseFunctionClass{ast.UnaryPlus, 1, 1}, opcode.Plus},
Expand All @@ -448,7 +451,7 @@ var funcs = map[string]functionClass{
ast.Case: &caseWhenFunctionClass{baseFunctionClass{ast.Case, 1, -1}},
ast.RowFunc: &rowFunctionClass{baseFunctionClass{ast.RowFunc, 2, -1}},
ast.SetVar: &setVarFunctionClass{baseFunctionClass{ast.SetVar, 2, 2}},
ast.GetVar: &getVarFunctionClass{baseFunctionClass{ast.GetVar, 2, 2}},
ast.GetVar: &getVarFunctionClass{baseFunctionClass{ast.GetVar, 1, 1}},
}

// DynamicFuncs are those functions that
Expand Down
77 changes: 42 additions & 35 deletions expression/builtin_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ type databaseFunctionClass struct {
}

func (c *databaseFunctionClass) getFunction(args []Expression, ctx context.Context) (builtinFunc, error) {
return &builtinDatabaseSig{newBaseBuiltinFunc(args, ctx)}, errors.Trace(c.verifyArgs(args))
if err := errors.Trace(c.verifyArgs(args)); err != nil {
return nil, errors.Trace(err)
}
bt := &builtinDatabaseSig{newBaseBuiltinFunc(args, ctx)}
bt.deterministic = false
return bt, nil
}

type builtinDatabaseSig struct {
Expand All @@ -64,10 +69,6 @@ func (b *builtinDatabaseSig) eval(row []types.Datum) (types.Datum, error) {
return builtinDatabase(args, b.ctx)
}

func (b *builtinDatabaseSig) isDeterministic() bool {
return false
}

// See https://dev.mysql.com/doc/refman/5.7/en/information-functions.html
func builtinDatabase(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
currentDB := ctx.GetSessionVars().CurrentDB
Expand All @@ -83,7 +84,12 @@ type foundRowsFunctionClass struct {
}

func (c *foundRowsFunctionClass) getFunction(args []Expression, ctx context.Context) (builtinFunc, error) {
return &builtinFoundRowsSig{newBaseBuiltinFunc(args, ctx)}, errors.Trace(c.verifyArgs(args))
if err := errors.Trace(c.verifyArgs(args)); err != nil {
return nil, errors.Trace(err)
}
bt := &builtinFoundRowsSig{newBaseBuiltinFunc(args, ctx)}
bt.deterministic = false
return bt, nil
}

type builtinFoundRowsSig struct {
Expand All @@ -98,10 +104,6 @@ func (b *builtinFoundRowsSig) eval(row []types.Datum) (types.Datum, error) {
return builtinFoundRows(args, b.ctx)
}

func (b *builtinFoundRowsSig) isDeterministic() bool {
return false
}

func builtinFoundRows(arg []types.Datum, ctx context.Context) (d types.Datum, err error) {
data := ctx.GetSessionVars()
if data == nil {
Expand All @@ -117,7 +119,12 @@ type currentUserFunctionClass struct {
}

func (c *currentUserFunctionClass) getFunction(args []Expression, ctx context.Context) (builtinFunc, error) {
return &builtinCurrentUserSig{newBaseBuiltinFunc(args, ctx)}, errors.Trace(c.verifyArgs(args))
if err := errors.Trace(c.verifyArgs(args)); err != nil {
return nil, errors.Trace(err)
}
bt := &builtinCurrentUserSig{newBaseBuiltinFunc(args, ctx)}
bt.deterministic = false
return bt, nil
}

type builtinCurrentUserSig struct {
Expand All @@ -132,10 +139,6 @@ func (b *builtinCurrentUserSig) eval(row []types.Datum) (types.Datum, error) {
return builtinCurrentUser(args, b.ctx)
}

func (b *builtinCurrentUserSig) isDeterministic() bool {
return false
}

// See https://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_current-user
// TODO: The value of CURRENT_USER() can differ from the value of USER(). We will finish this after we support grant tables.
func builtinCurrentUser(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
Expand All @@ -153,7 +156,12 @@ type userFunctionClass struct {
}

func (c *userFunctionClass) getFunction(args []Expression, ctx context.Context) (builtinFunc, error) {
return &builtinUserSig{newBaseBuiltinFunc(args, ctx)}, errors.Trace(c.verifyArgs(args))
if err := errors.Trace(c.verifyArgs(args)); err != nil {
return nil, errors.Trace(err)
}
bt := &builtinUserSig{newBaseBuiltinFunc(args, ctx)}
bt.deterministic = false
return bt, nil
}

type builtinUserSig struct {
Expand All @@ -168,10 +176,6 @@ func (b *builtinUserSig) eval(row []types.Datum) (types.Datum, error) {
return builtinUser(args, b.ctx)
}

func (b *builtinUserSig) isDeterministic() bool {
return false
}

func builtinUser(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
data := ctx.GetSessionVars()
if data == nil {
Expand All @@ -187,7 +191,12 @@ type connectionIDFunctionClass struct {
}

func (c *connectionIDFunctionClass) getFunction(args []Expression, ctx context.Context) (builtinFunc, error) {
return &builtinConnectionIDSig{newBaseBuiltinFunc(args, ctx)}, errors.Trace(c.verifyArgs(args))
if err := errors.Trace(c.verifyArgs(args)); err != nil {
return nil, errors.Trace(err)
}
bt := &builtinConnectionIDSig{newBaseBuiltinFunc(args, ctx)}
bt.deterministic = false
return bt, nil
}

type builtinConnectionIDSig struct {
Expand All @@ -202,10 +211,6 @@ func (b *builtinConnectionIDSig) eval(row []types.Datum) (types.Datum, error) {
return builtinConnectionID(args, b.ctx)
}

func (b *builtinConnectionIDSig) isDeterministic() bool {
return false
}

func builtinConnectionID(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
data := ctx.GetSessionVars()
if data == nil {
Expand All @@ -221,7 +226,12 @@ type lastInsertIDFunctionClass struct {
}

func (c *lastInsertIDFunctionClass) getFunction(args []Expression, ctx context.Context) (builtinFunc, error) {
return &builtinLastInsertIDSig{newBaseBuiltinFunc(args, ctx)}, errors.Trace(c.verifyArgs(args))
if err := errors.Trace(c.verifyArgs(args)); err != nil {
return nil, errors.Trace(err)
}
bt := &builtinLastInsertIDSig{newBaseBuiltinFunc(args, ctx)}
bt.deterministic = false
return bt, nil
}

type builtinLastInsertIDSig struct {
Expand All @@ -236,10 +246,6 @@ func (b *builtinLastInsertIDSig) eval(row []types.Datum) (types.Datum, error) {
return builtinLastInsertID(args, b.ctx)
}

func (b *builtinLastInsertIDSig) isDeterministic() bool {
return false
}

// See http://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_last-insert-id
func builtinLastInsertID(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
if len(args) == 1 {
Expand All @@ -259,7 +265,12 @@ type versionFunctionClass struct {
}

func (c *versionFunctionClass) getFunction(args []Expression, ctx context.Context) (builtinFunc, error) {
return &builtinVersionSig{newBaseBuiltinFunc(args, ctx)}, errors.Trace(c.verifyArgs(args))
if err := errors.Trace(c.verifyArgs(args)); err != nil {
return nil, errors.Trace(err)
}
bt := &builtinVersionSig{newBaseBuiltinFunc(args, ctx)}
bt.deterministic = false
return bt, nil
}

type builtinVersionSig struct {
Expand All @@ -274,10 +285,6 @@ func (b *builtinVersionSig) eval(row []types.Datum) (types.Datum, error) {
return builtinVersion(args, b.ctx)
}

func (b *builtinVersionSig) isDeterministic() bool {
return false
}

func builtinVersion(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
d.SetString(mysql.ServerVersion)
return d, nil
Expand Down
8 changes: 5 additions & 3 deletions expression/builtin_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ func (s *testEvaluatorSuite) TestDatabase(c *C) {
c.Assert(d.GetString(), Equals, "test")

// Test case for schema().
f := Funcs[ast.Schema]
c.Assert(f, NotNil)
d, err = f.F(types.MakeDatums(), ctx)
fc := funcs[ast.Schema]
c.Assert(fc, NotNil)
f, err := fc.getFunction(nil, ctx)
c.Assert(err, IsNil)
d, err = f.eval(types.MakeDatums())
c.Assert(err, IsNil)
c.Assert(d.GetString(), Equals, "test")
}
Expand Down
11 changes: 6 additions & 5 deletions expression/builtin_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,12 @@ type randFunctionClass struct {
}

func (c *randFunctionClass) getFunction(args []Expression, ctx context.Context) (builtinFunc, error) {
return &builtinRandSig{newBaseBuiltinFunc(args, ctx)}, errors.Trace(c.verifyArgs(args))
if err := errors.Trace(c.verifyArgs(args)); err != nil {
return nil, errors.Trace(err)
}
bt := &builtinRandSig{newBaseBuiltinFunc(args, ctx)}
bt.deterministic = false
return bt, nil
}

type builtinRandSig struct {
Expand All @@ -291,10 +296,6 @@ func (b *builtinRandSig) eval(row []types.Datum) (types.Datum, error) {
return builtinRand(args, b.ctx)
}

func (b *builtinRandSig) isDeterministic() bool {
return true
}

// See http://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_rand
func builtinRand(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
if len(args) == 1 && !args[0].IsNull() {
Expand Down
Loading

0 comments on commit e79a460

Please sign in to comment.