func (e *UnaryMinus) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
	child, err := e.Child.Eval(ctx, row)
	if err != nil {
		return nil, err
	}
	if child == nil {
		return nil, nil
	}
	if !sql.IsNumber(e.Child.Type()) {
		child, err = sql.Float64.Convert(child)
		if err != nil {
			child = 0.0
		}
	}
	switch n := child.(type) {
	case float64:
		return -n, nil
	case float32:
		return -n, nil
	case int64:
		return -n, nil
	case uint64:
		return -int64(n), nil
	case int32:
		return -n, nil
	case uint32:
		return -int32(n), nil
	default:
		return nil, sql.ErrInvalidType.New(reflect.TypeOf(n))
	}
}
Eval implements the sql.Expression interface.
package expression
import (
	"fmt"
	"reflect"
	"time"
	errors "gopkg.in/src-d/go-errors.v1"
	"vitess.io/vitess/go/vt/sqlparser"
	"github.com/src-d/go-mysql-server/sql"
)
var (
	errUnableToCast = errors.NewKind("Unable to cast between types: %T, %T")
	errUnableToEval = errors.NewKind("Unable to evaluate an expression: %v %s %v")
)
type Arithmetic struct {
	BinaryExpression
	Op string
}
func NewArithmetic(left, right sql.Expression, op string) *Arithmetic {
	return &Arithmetic{BinaryExpression{Left: left, Right: right}, op}
}
func NewPlus(left, right sql.Expression) *Arithmetic {
	return NewArithmetic(left, right, sqlparser.PlusStr)
}
func NewMinus(left, right sql.Expression) *Arithmetic {
	return NewArithmetic(left, right, sqlparser.MinusStr)
}
func NewMult(left, right sql.Expression) *Arithmetic {
	return NewArithmetic(left, right, sqlparser.MultStr)
}
func NewDiv(left, right sql.Expression) *Arithmetic {
	return NewArithmetic(left, right, sqlparser.DivStr)
}
func NewShiftLeft(left, right sql.Expression) *Arithmetic {
	return NewArithmetic(left, right, sqlparser.ShiftLeftStr)
}
func NewShiftRight(left, right sql.Expression) *Arithmetic {
	return NewArithmetic(left, right, sqlparser.ShiftRightStr)
}
func NewBitAnd(left, right sql.Expression) *Arithmetic {
	return NewArithmetic(left, right, sqlparser.BitAndStr)
}
func NewBitOr(left, right sql.Expression) *Arithmetic {
	return NewArithmetic(left, right, sqlparser.BitOrStr)
}
func NewBitXor(left, right sql.Expression) *Arithmetic {
	return NewArithmetic(left, right, sqlparser.BitXorStr)
}
func NewIntDiv(left, right sql.Expression) *Arithmetic {
	return NewArithmetic(left, right, sqlparser.IntDivStr)
}
func NewMod(left, right sql.Expression) *Arithmetic {
	return NewArithmetic(left, right, sqlparser.ModStr)
}
func (a *Arithmetic) String() string {
	return fmt.Sprintf("%s %s %s", a.Left, a.Op, a.Right)
}
func (a *Arithmetic) IsNullable() bool {
	if a.Type() == sql.Timestamp {
		return true
	}
	return a.BinaryExpression.IsNullable()
}
func (a *Arithmetic) Type() sql.Type {
	switch a.Op {
	case sqlparser.PlusStr, sqlparser.MinusStr, sqlparser.MultStr, sqlparser.DivStr:
		if isInterval(a.Left) || isInterval(a.Right) {
			return sql.Timestamp
		}
		if sql.IsTime(a.Left.Type()) && sql.IsTime(a.Right.Type()) {
			return sql.Int64
		}
		if sql.IsInteger(a.Left.Type()) && sql.IsInteger(a.Right.Type()) {
			if sql.IsUnsigned(a.Left.Type()) && sql.IsUnsigned(a.Right.Type()) {
				return sql.Uint64
			}
			return sql.Int64
		}
		return sql.Float64
	case sqlparser.ShiftLeftStr, sqlparser.ShiftRightStr:
		return sql.Uint64
	case sqlparser.BitAndStr, sqlparser.BitOrStr, sqlparser.BitXorStr, sqlparser.IntDivStr, sqlparser.ModStr:
		if sql.IsUnsigned(a.Left.Type()) && sql.IsUnsigned(a.Right.Type()) {
			return sql.Uint64
		}
		return sql.Int64
	}
	return sql.Float64
}
func isInterval(expr sql.Expression) bool {
	_, ok := expr.(*Interval)
	return ok
}
func (a *Arithmetic) WithChildren(children ...sql.Expression) (sql.Expression, error) {
	if len(children) != 2 {
		return nil, sql.ErrInvalidChildrenNumber.New(a, len(children), 2)
	}
	return NewArithmetic(children[0], children[1], a.Op), nil
}
func (a *Arithmetic) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
	lval, rval, err := a.evalLeftRight(ctx, row)
	if err != nil {
		return nil, err
	}
	if lval == nil || rval == nil {
		return nil, nil
	}
	lval, rval, err = a.convertLeftRight(lval, rval)
	if err != nil {
		return nil, err
	}
	switch a.Op {
	case sqlparser.PlusStr:
		return plus(lval, rval)
	case sqlparser.MinusStr:
		return minus(lval, rval)
	case sqlparser.MultStr:
		return mult(lval, rval)
	case sqlparser.DivStr:
		return div(lval, rval)
	case sqlparser.BitAndStr:
		return bitAnd(lval, rval)
	case sqlparser.BitOrStr:
		return bitOr(lval, rval)
	case sqlparser.BitXorStr:
		return bitXor(lval, rval)
	case sqlparser.ShiftLeftStr:
		return shiftLeft(lval, rval)
	case sqlparser.ShiftRightStr:
		return shiftRight(lval, rval)
	case sqlparser.IntDivStr:
		return intDiv(lval, rval)
	case sqlparser.ModStr:
		return mod(lval, rval)
	}
	return nil, errUnableToEval.New(lval, a.Op, rval)
}
func (a *Arithmetic) evalLeftRight(ctx *sql.Context, row sql.Row) (interface{}, interface{}, error) {
	var lval, rval interface{}
	var err error
	if i, ok := a.Left.(*Interval); ok {
		lval, err = i.EvalDelta(ctx, row)
		if err != nil {
			return nil, nil, err
		}
	} else {
		lval, err = a.Left.Eval(ctx, row)
		if err != nil {
			return nil, nil, err
		}
	}
	if i, ok := a.Right.(*Interval); ok {
		rval, err = i.EvalDelta(ctx, row)
		if err != nil {
			return nil, nil, err
		}
	} else {
		rval, err = a.Right.Eval(ctx, row)
		if err != nil {
			return nil, nil, err
		}
	}
	return lval, rval, nil
}
func (a *Arithmetic) convertLeftRight(left interface{}, right interface{}) (interface{}, interface{}, error) {
	var err error
	typ := a.Type()
	if i, ok := left.(*TimeDelta); ok {
		left = i
	} else {
		left, err = typ.Convert(left)
		if err != nil {
			return nil, nil, err
		}
	}
	if i, ok := right.(*TimeDelta); ok {
		right = i
	} else {
		right, err = typ.Convert(right)
		if err != nil {
			return nil, nil, err
		}
	}
	return left, right, nil
}
func plus(lval, rval interface{}) (interface{}, error) {
	switch l := lval.(type) {
	case uint64:
		switch r := rval.(type) {
		case uint64:
			return l + r, nil
		}
	case int64:
		switch r := rval.(type) {
		case int64:
			return l + r, nil
		}
	case float64:
		switch r := rval.(type) {
		case float64:
			return l + r, nil
		}
	case time.Time:
		switch r := rval.(type) {
		case *TimeDelta:
			return sql.ValidateTime(r.Add(l)), nil
		case time.Time:
			return l.Unix() + r.Unix(), nil
		}
	case *TimeDelta:
		switch r := rval.(type) {
		case time.Time:
			return sql.ValidateTime(l.Add(r)), nil
		}
	}
	return nil, errUnableToCast.New(lval, rval)
}
func minus(lval, rval interface{}) (interface{}, error) {
	switch l := lval.(type) {
	case uint64:
		switch r := rval.(type) {
		case uint64:
			return l - r, nil
		}
	case int64:
		switch r := rval.(type) {
		case int64:
			return l - r, nil
		}
	case float64:
		switch r := rval.(type) {
		case float64:
			return l - r, nil
		}
	case time.Time:
		switch r := rval.(type) {
		case *TimeDelta:
			return sql.ValidateTime(r.Sub(l)), nil
		case time.Time:
			return l.Unix() - r.Unix(), nil
		}
	}
	return nil, errUnableToCast.New(lval, rval)
}
func mult(lval, rval interface{}) (interface{}, error) {
	switch l := lval.(type) {
	case uint64:
		switch r := rval.(type) {
		case uint64:
			return l * r, nil
		}
	case int64:
		switch r := rval.(type) {
		case int64:
			return l * r, nil
		}
	case float64:
		switch r := rval.(type) {
		case float64:
			return l * r, nil
		}
	}
	return nil, errUnableToCast.New(lval, rval)
}
func div(lval, rval interface{}) (interface{}, error) {
	switch l := lval.(type) {
	case uint64:
		switch r := rval.(type) {
		case uint64:
			return l / r, nil
		}
	case int64:
		switch r := rval.(type) {
		case int64:
			return l / r, nil
		}
	case float64:
		switch r := rval.(type) {
		case float64:
			return l / r, nil
		}
	}
	return nil, errUnableToCast.New(lval, rval)
}
func bitAnd(lval, rval interface{}) (interface{}, error) {
	switch l := lval.(type) {
	case uint64:
		switch r := rval.(type) {
		case uint64:
			return l & r, nil
		}
	case int64:
		switch r := rval.(type) {
		case int64:
			return l & r, nil
		}
	}
	return nil, errUnableToCast.New(lval, rval)
}
func bitOr(lval, rval interface{}) (interface{}, error) {
	switch l := lval.(type) {
	case uint64:
		switch r := rval.(type) {
		case uint64:
			return l | r, nil
		}
	case int64:
		switch r := rval.(type) {
		case int64:
			return l | r, nil
		}
	}
	return nil, errUnableToCast.New(lval, rval)
}
func bitXor(lval, rval interface{}) (interface{}, error) {
	switch l := lval.(type) {
	case uint64:
		switch r := rval.(type) {
		case uint64:
			return l ^ r, nil
		}
	case int64:
		switch r := rval.(type) {
		case int64:
			return l ^ r, nil
		}
	}
	return nil, errUnableToCast.New(lval, rval)
}
func shiftLeft(lval, rval interface{}) (interface{}, error) {
	switch l := lval.(type) {
	case uint64:
		switch r := rval.(type) {
		case uint64:
			return l << r, nil
		}
	}
	return nil, errUnableToCast.New(lval, rval)
}
func shiftRight(lval, rval interface{}) (interface{}, error) {
	switch l := lval.(type) {
	case uint64:
		switch r := rval.(type) {
		case uint64:
			return l >> r, nil
		}
	}
	return nil, errUnableToCast.New(lval, rval)
}
func intDiv(lval, rval interface{}) (interface{}, error) {
	switch l := lval.(type) {
	case uint64:
		switch r := rval.(type) {
		case uint64:
			return uint64(l / r), nil
		}
	case int64:
		switch r := rval.(type) {
		case int64:
			return int64(l / r), nil
		}
	}
	return nil, errUnableToCast.New(lval, rval)
}
func mod(lval, rval interface{}) (interface{}, error) {
	switch l := lval.(type) {
	case uint64:
		switch r := rval.(type) {
		case uint64:
			return l % r, nil
		}
	case int64:
		switch r := rval.(type) {
		case int64:
			return l % r, nil
		}
	}
	return nil, errUnableToCast.New(lval, rval)
}
type UnaryMinus struct {
	UnaryExpression
}
func NewUnaryMinus(child sql.Expression) *UnaryMinus {
	return &UnaryMinus{UnaryExpression{Child: child}}
}