Skip to content

Commit

Permalink
Add Datum.IsNull() fucntion (pingcap#1298)
Browse files Browse the repository at this point in the history
Add IsNull function for Datum to simplify null check.
  • Loading branch information
shenli committed Jun 7, 2016
1 parent a710057 commit e3597b6
Show file tree
Hide file tree
Showing 32 changed files with 164 additions and 139 deletions.
4 changes: 2 additions & 2 deletions evaluator/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ var Funcs = map[string]Func{
// See: http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_coalesce
func builtinCoalesce(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
for _, d = range args {
if d.Kind() != types.KindNull {
if !d.IsNull() {
return d, nil
}
}
Expand All @@ -158,7 +158,7 @@ func builtinCoalesce(args []types.Datum, ctx context.Context) (d types.Datum, er

// See: https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_isnull
func builtinIsNull(args []types.Datum, _ context.Context) (d types.Datum, err error) {
if args[0].Kind() == types.KindNull {
if args[0].IsNull() {
d.SetInt64(1)
} else {
d.SetInt64(0)
Expand Down
6 changes: 3 additions & 3 deletions evaluator/builtin_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func builtinIf(args []types.Datum, _ context.Context) (d types.Datum, err error)
v2 := args[1]
v3 := args[2]

if v1.Kind() == types.KindNull {
if v1.IsNull() {
return v3, nil
}

Expand All @@ -52,7 +52,7 @@ func builtinIfNull(args []types.Datum, _ context.Context) (d types.Datum, err er
v1 := args[0]
v2 := args[1]

if v1.Kind() != types.KindNull {
if !v1.IsNull() {
return v1, nil
}

Expand All @@ -66,7 +66,7 @@ func builtinNullIf(args []types.Datum, _ context.Context) (d types.Datum, err er
v1 := args[0]
v2 := args[1]

if v1.Kind() == types.KindNull || v2.Kind() == types.KindNull {
if v1.IsNull() || v2.IsNull() {
return v1, nil
}

Expand Down
2 changes: 1 addition & 1 deletion evaluator/builtin_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func builtinAbs(args []types.Datum, _ context.Context) (d types.Datum, err error
}

func builtinRand(args []types.Datum, _ context.Context) (d types.Datum, err error) {
if len(args) == 1 && args[0].Kind() != types.KindNull {
if len(args) == 1 && !args[0].IsNull() {
seed, err := args[0].ToInt64()
if err != nil {
return d, errors.Trace(err)
Expand Down
24 changes: 12 additions & 12 deletions evaluator/builtin_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
func builtinAndAnd(args []types.Datum, _ context.Context) (d types.Datum, err error) {
leftDatum := args[0]
rightDatum := args[1]
if leftDatum.Kind() != types.KindNull {
if !leftDatum.IsNull() {
var x int64
x, err = leftDatum.ToBool()
if err != nil {
Expand All @@ -35,7 +35,7 @@ func builtinAndAnd(args []types.Datum, _ context.Context) (d types.Datum, err er
return
}
}
if rightDatum.Kind() != types.KindNull {
if !rightDatum.IsNull() {
var y int64
y, err = rightDatum.ToBool()
if err != nil {
Expand All @@ -45,7 +45,7 @@ func builtinAndAnd(args []types.Datum, _ context.Context) (d types.Datum, err er
return
}
}
if leftDatum.Kind() == types.KindNull || rightDatum.Kind() == types.KindNull {
if leftDatum.IsNull() || rightDatum.IsNull() {
return
}
d.SetInt64(int64(1))
Expand All @@ -55,7 +55,7 @@ func builtinAndAnd(args []types.Datum, _ context.Context) (d types.Datum, err er
func builtinOrOr(args []types.Datum, _ context.Context) (d types.Datum, err error) {
leftDatum := args[0]
rightDatum := args[1]
if leftDatum.Kind() != types.KindNull {
if !leftDatum.IsNull() {
var x int64
x, err = leftDatum.ToBool()
if err != nil {
Expand All @@ -66,7 +66,7 @@ func builtinOrOr(args []types.Datum, _ context.Context) (d types.Datum, err erro
return
}
}
if rightDatum.Kind() != types.KindNull {
if !rightDatum.IsNull() {
var y int64
y, err = rightDatum.ToBool()
if err != nil {
Expand All @@ -76,7 +76,7 @@ func builtinOrOr(args []types.Datum, _ context.Context) (d types.Datum, err erro
return
}
}
if leftDatum.Kind() == types.KindNull || rightDatum.Kind() == types.KindNull {
if leftDatum.IsNull() || rightDatum.IsNull() {
return
}
d.SetInt64(int64(0))
Expand All @@ -86,7 +86,7 @@ func builtinOrOr(args []types.Datum, _ context.Context) (d types.Datum, err erro
func builtinLogicXor(args []types.Datum, _ context.Context) (d types.Datum, err error) {
leftDatum := args[0]
righDatum := args[1]
if leftDatum.Kind() == types.KindNull || righDatum.Kind() == types.KindNull {
if leftDatum.IsNull() || righDatum.IsNull() {
return
}
x, err := leftDatum.ToBool()
Expand All @@ -109,11 +109,11 @@ func builtinLogicXor(args []types.Datum, _ context.Context) (d types.Datum, err
func compareFuncFactory(op opcode.Op) BuiltinFunc {
return func(args []types.Datum, _ context.Context) (d types.Datum, err error) {
a, b := types.CoerceDatum(args[0], args[1])
if a.Kind() == types.KindNull || b.Kind() == types.KindNull {
if a.IsNull() || b.IsNull() {
// for <=>, if a and b are both nil, return true.
// if a or b is nil, return false.
if op == opcode.NullEQ {
if a.Kind() == types.KindNull && b.Kind() == types.KindNull {
if a.IsNull() && b.IsNull() {
d.SetInt64(oneI64)
} else {
d.SetInt64(zeroI64)
Expand Down Expand Up @@ -155,7 +155,7 @@ func compareFuncFactory(op opcode.Op) BuiltinFunc {
func bitOpFactory(op opcode.Op) BuiltinFunc {
return func(args []types.Datum, _ context.Context) (d types.Datum, err error) {
a, b := types.CoerceDatum(args[0], args[1])
if a.Kind() == types.KindNull || b.Kind() == types.KindNull {
if a.IsNull() || b.IsNull() {
return
}

Expand Down Expand Up @@ -201,7 +201,7 @@ func arithmeticFuncFactory(op opcode.Op) BuiltinFunc {
}

a, b = types.CoerceDatum(a, b)
if a.Kind() == types.KindNull || b.Kind() == types.KindNull {
if a.IsNull() || b.IsNull() {
return
}

Expand Down Expand Up @@ -232,7 +232,7 @@ func unaryOpFactory(op opcode.Op) BuiltinFunc {
}
}()
aDatum := args[0]
if aDatum.Kind() == types.KindNull {
if aDatum.IsNull() {
return
}
switch op {
Expand Down
16 changes: 8 additions & 8 deletions evaluator/builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func builtinASCII(args []types.Datum, _ context.Context) (d types.Datum, err err
func builtinConcat(args []types.Datum, _ context.Context) (d types.Datum, err error) {
var s []byte
for _, a := range args {
if a.Kind() == types.KindNull {
if a.IsNull() {
return d, nil
}
var ss string
Expand All @@ -89,7 +89,7 @@ func builtinConcatWS(args []types.Datum, _ context.Context) (d types.Datum, err
var sep string
s := make([]string, 0, len(args))
for i, a := range args {
if a.Kind() == types.KindNull {
if a.IsNull() {
if i == 0 {
return d, nil
}
Expand Down Expand Up @@ -204,7 +204,7 @@ func builtinUpper(args []types.Datum, _ context.Context) (d types.Datum, err err

// See: https://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html
func builtinStrcmp(args []types.Datum, _ context.Context) (d types.Datum, err error) {
if args[0].Kind() == types.KindNull || args[1].Kind() == types.KindNull {
if args[0].IsNull() || args[1].IsNull() {
return d, nil
}
left, err := args[0].ToString()
Expand All @@ -223,7 +223,7 @@ func builtinStrcmp(args []types.Datum, _ context.Context) (d types.Datum, err er
// See: https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_replace
func builtinReplace(args []types.Datum, _ context.Context) (d types.Datum, err error) {
for _, arg := range args {
if arg.Kind() == types.KindNull {
if arg.IsNull() {
return d, nil
}
}
Expand Down Expand Up @@ -380,15 +380,15 @@ func builtinLocate(args []types.Datum, _ context.Context) (d types.Datum, err er
// args[1] -> Str
// args[2] -> Pos
// eval str
if args[1].Kind() == types.KindNull {
if args[1].IsNull() {
return d, nil
}
str, err := args[1].ToString()
if err != nil {
return d, errors.Trace(err)
}
// eval substr
if args[0].Kind() == types.KindNull {
if args[0].IsNull() {
return d, nil
}
subStr, err := args[0].ToString()
Expand Down Expand Up @@ -433,7 +433,7 @@ func builtinTrim(args []types.Datum, _ context.Context) (d types.Datum, err erro
// args[1] -> RemStr
// args[2] -> Direction
// eval str
if args[0].Kind() == types.KindNull {
if args[0].IsNull() {
return d, nil
}
str, err := args[0].ToString()
Expand Down Expand Up @@ -485,7 +485,7 @@ func builtinTrim(args []types.Datum, _ context.Context) (d types.Datum, err erro
// See: https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_rtrim
func trimFn(fn func(string, string) string, cutset string) BuiltinFunc {
return func(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
if args[0].Kind() == types.KindNull {
if args[0].IsNull() {
return d, nil
}
str, err := args[0].ToString()
Expand Down
Loading

0 comments on commit e3597b6

Please sign in to comment.