Merge pull request #161 from pingcap/siddontang/fix-unary-bool

expressions,tidb: unary handes bool type
This commit is contained in:
qiuyesuifeng
2015-09-15 17:48:41 +08:00
3 changed files with 24 additions and 2 deletions

View File

@ -164,6 +164,11 @@ func (u *UnaryOperation) Eval(ctx context.Context, args map[interface{}]interfac
switch x := a.(type) {
case nil:
return nil, nil
case bool:
if x {
return int64(1), nil
}
return int64(0), nil
case float32:
return +x, nil
case float64:
@ -209,6 +214,11 @@ func (u *UnaryOperation) Eval(ctx context.Context, args map[interface{}]interfac
switch x := a.(type) {
case nil:
return nil, nil
case bool:
if x {
return int64(-1), nil
}
return int64(0), nil
case float32:
return -x, nil
case float64:

View File

@ -55,6 +55,8 @@ func (s *testUnaryOperationSuite) TestUnaryOp(c *C) {
{"1.0", opcode.Plus, "1.0"},
{[]byte("1.0"), opcode.Plus, []byte("1.0")},
{mysql.Hex{Value: 1}, opcode.Plus, mysql.Hex{Value: 1}},
{true, opcode.Plus, int64(1)},
{false, opcode.Plus, int64(0)},
// test Minus.
{nil, opcode.Minus, nil},
@ -70,6 +72,8 @@ func (s *testUnaryOperationSuite) TestUnaryOp(c *C) {
{"1.0", opcode.Minus, -1.0},
{[]byte("1.0"), opcode.Minus, -1.0},
{mysql.Hex{Value: 1}, opcode.Minus, -1.0},
{true, opcode.Minus, int64(-1)},
{false, opcode.Minus, int64(0)},
}
for _, t := range tbl {
@ -150,9 +154,7 @@ func (s *testUnaryOperationSuite) TestUnaryOp(c *C) {
{mockExpr{}, opcode.Not},
{mockExpr{}, opcode.BitNeg},
{mockExpr{}, opcode.Plus},
{false, opcode.Plus},
{mockExpr{}, opcode.Minus},
{false, opcode.Minus},
{mockExpr{}, opcode.EQ},
}

View File

@ -678,6 +678,16 @@ func (s *testSessionSuite) TestHexadecimal(c *C) {
match(c, row, 2, 1)
}
func (s *testSessionSuite) TestExpression(c *C) {
store := newStore(c, s.dbName)
se := newSession(c, store, s.dbName)
r := mustExecSQL(c, se, `select + (1 > 0), -(1 >0), + (1 < 0), - (1 < 0)`)
row, err := r.FirstRow()
c.Assert(err, IsNil)
match(c, row, 1, -1, 0, 0)
}
func newSession(c *C, store kv.Storage, dbName string) Session {
se, err := CreateSession(store)
c.Assert(err, IsNil)