From 94c91f4bfb8e341ec7d550b46b31e5bdfdc0f709 Mon Sep 17 00:00:00 2001 From: siddontang Date: Tue, 15 Sep 2015 17:27:47 +0800 Subject: [PATCH] expressions,tidb: unary handes bool type --- expression/expressions/unary.go | 10 ++++++++++ expression/expressions/unary_test.go | 6 ++++-- tidb_test.go | 10 ++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/expression/expressions/unary.go b/expression/expressions/unary.go index b07302dc49..ea238cfb1e 100644 --- a/expression/expressions/unary.go +++ b/expression/expressions/unary.go @@ -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: diff --git a/expression/expressions/unary_test.go b/expression/expressions/unary_test.go index 32734d196a..4aba2df6bc 100644 --- a/expression/expressions/unary_test.go +++ b/expression/expressions/unary_test.go @@ -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}, } diff --git a/tidb_test.go b/tidb_test.go index 75a653b7a2..5673d5d7d7 100644 --- a/tidb_test.go +++ b/tidb_test.go @@ -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)