*: Address comment and code cleanup

This commit is contained in:
shenli
2015-10-26 15:06:08 +08:00
parent 359311972e
commit ebfb3377c8
6 changed files with 15 additions and 22 deletions

View File

@ -110,13 +110,12 @@ func calculateSum(sum interface{}, v interface{}) (interface{}, error) {
err error
)
v = types.RawData(v)
switch y := v.(type) {
case int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64:
data, err = mysql.ConvertToDecimal(v)
case mysql.Decimal:
data = y
case *types.DataItem:
return calculateSum(sum, y.Data)
case nil:
data = nil
default:

View File

@ -28,27 +28,25 @@ import (
// see https://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html
func builtinAbs(args []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
switch x := args[0].(type) {
d := types.RawData(args[0])
switch x := d.(type) {
case nil:
return nil, nil
case uint, uint8, uint16, uint32, uint64:
return x, nil
case int, int8, int16, int32, int64:
// we don't need to handle error here, it must be success
v, _ := types.ToInt64(args[0])
v, _ := types.ToInt64(d)
if v >= 0 {
return x, nil
}
// TODO: handle overflow if x is MinInt64
return -v, nil
case *types.DataItem:
args[0] = x.Data
return builtinAbs(args, ctx)
default:
// we will try to convert other types to float
// TODO: if time has no precision, it will be a integer
f, err := types.ToFloat64(args[0])
f, err := types.ToFloat64(d)
return math.Abs(f), errors.Trace(err)
}
}

View File

@ -87,10 +87,7 @@ func (f *FunctionCast) Eval(ctx context.Context, args map[interface{}]interface{
if err != nil {
return nil, err
}
v, ok := value.(*types.DataItem)
if ok {
value = v.Data
}
value = types.RawData(value)
d := &types.DataItem{Type: f.Tp}
// Casting nil to any type returns null
if value == nil {

View File

@ -87,6 +87,7 @@ func cloneExpressionList(list []Expression) []Expression {
// FastEval evaluates Value and static +/- Unary expression and returns its value.
func FastEval(v interface{}) interface{} {
v = types.RawData(v)
switch x := v.(type) {
case Value:
return x.Val
@ -101,8 +102,6 @@ func FastEval(v interface{}) interface{} {
}
m := map[interface{}]interface{}{}
return Eval(x, nil, m)
case *types.DataItem:
return FastEval(x.Data)
default:
return nil
}
@ -115,10 +114,7 @@ func Eval(v Expression, ctx context.Context, env map[interface{}]interface{}) (y
if err != nil {
panic(err) // panic ok here
}
x, ok := y.(*types.DataItem)
if ok {
y = x.Data
}
y = types.RawData(y)
return
}

View File

@ -104,10 +104,7 @@ func getDefaultValue(c *ConstraintOpt, tp byte, fsp int) (interface{}, error) {
return value, nil
}
v := expression.FastEval(c.Evalue)
if vv, ok := v.(*types.DataItem); ok {
return vv.Data, nil
}
return v, nil
return types.RawData(v), nil
}
func removeOnUpdateNowFlag(c *column.Col) {

View File

@ -136,3 +136,9 @@ func RawData(d interface{}) interface{} {
func IsNil(d interface{}) bool {
return RawData(d) == nil
}
// IsDataItem checks if the interface is a DataItem.
func IsDataItem(d interface{}) bool {
_, ok := d.(*DataItem)
return ok
}