Skip to content

Commit

Permalink
Merge pull request #39 from pingcap/siddontang/op-expr-with-bytes
Browse files Browse the repository at this point in the history
expressions: operator supports []byte type
  • Loading branch information
siddontang committed Sep 7, 2015
2 parents 5e6413b + d97c747 commit b8dae9a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions expression/expressions/binop.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,13 @@ func (o *BinaryOperation) coerceArithmetic(a interface{}) (interface{}, error) {
case mysql.Duration:
// TODO: if duration has no precision, return int64
return x.ToNumber(), nil
case []byte:
// []byte is the same as string, converted to float for arithmetic operator.
f, err := types.StrToFloat(string(x))
if err != nil {
return nil, err
}
return f, err
default:
return x, nil
}
Expand Down
7 changes: 7 additions & 0 deletions expression/expressions/binop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ func (s *testBinOpSuite) TestNumericOp(c *C) {
{1, opcode.Plus, mysql.NewDecimalFromInt(1, 0), 2},
{uint64(1), opcode.Plus, 1, 2},
{uint64(1), opcode.Plus, uint64(1), 2},
{1, opcode.Plus, []byte("1"), 2},

// minus
{1, opcode.Minus, 1, 0},
Expand All @@ -404,6 +405,7 @@ func (s *testBinOpSuite) TestNumericOp(c *C) {
{uint64(1), opcode.Minus, 1, 0},
{uint64(1), opcode.Minus, uint64(1), 0},
{mysql.NewDecimalFromInt(1, 0), opcode.Minus, 1, 0},
{"1", opcode.Minus, []byte("1"), 0},

// mul
{1, opcode.Mul, 1, 1},
Expand Down Expand Up @@ -511,6 +513,11 @@ func (s *testBinOpSuite) TestNumericOp(c *C) {
_, err = expr.evalArithmeticOp(nil, nil)
c.Assert(err, NotNil)

expr.L = Value{[]byte("abc")}
expr.R = Value{1}
_, err = expr.evalArithmeticOp(nil, nil)
c.Assert(err, NotNil)

expr.L = Value{1}
expr.R = Value{"abc"}
_, err = expr.evalArithmeticOp(nil, nil)
Expand Down
5 changes: 5 additions & 0 deletions expression/expressions/unary.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ func (u *UnaryOperation) Eval(ctx context.Context, args map[interface{}]interfac
return x, nil
case mysql.Decimal:
return x, nil
case []byte:
return x, nil
default:
return types.UndOp(a, op)
}
Expand Down Expand Up @@ -236,6 +238,9 @@ func (u *UnaryOperation) Eval(ctx context.Context, args map[interface{}]interfac
case mysql.Decimal:
f, _ := x.Float64()
return mysql.NewDecimalFromFloat(-f), nil
case []byte:
f, err := types.StrToFloat(string(x))
return -f, err
default:
return types.UndOp(a, op)
}
Expand Down
4 changes: 3 additions & 1 deletion expression/expressions/unary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (s *testUnaryOperationSuite) TestUnaryOp(c *C) {
{uint32(1), opcode.Plus, uint32(1)},
{uint64(1), opcode.Plus, uint64(1)},
{"1.0", opcode.Plus, "1.0"},
{[]byte("1.0"), opcode.Plus, []byte("1.0")},

// test Minus.
{nil, opcode.Minus, nil},
Expand All @@ -75,6 +76,7 @@ func (s *testUnaryOperationSuite) TestUnaryOp(c *C) {
{uint32(1), opcode.Minus, -int64(1)},
{uint64(1), opcode.Minus, -int64(1)},
{"1.0", opcode.Minus, -1.0},
{[]byte("1.0"), opcode.Minus, -1.0},
}

for _, t := range tbl {
Expand All @@ -85,7 +87,7 @@ func (s *testUnaryOperationSuite) TestUnaryOp(c *C) {

result, err := exprc.Eval(nil, nil)
c.Assert(err, IsNil)
c.Assert(result, Equals, t.result)
c.Assert(result, DeepEquals, t.result)
}

tbl = []struct {
Expand Down

0 comments on commit b8dae9a

Please sign in to comment.