Skip to content

Commit

Permalink
xapi/xeval: fix AND OR evaluator. (pingcap#1076)
Browse files Browse the repository at this point in the history
* xapi/xeval: fix AND OR evaluator.

* *: simplify code.
  • Loading branch information
coocood committed Apr 10, 2016
1 parent 8d9bf2f commit 7e27211
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
24 changes: 19 additions & 5 deletions xapi/xeval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,21 +249,35 @@ func (e *Evaluator) evalAnd(expr *tipb.Expr) (types.Datum, error) {
if err != nil {
return types.Datum{}, errors.Trace(err)
}
if leftBool == 1 && rightBool == 1 {
return types.NewIntDatum(1), nil
var d types.Datum
if leftBool == 0 || rightBool == 0 {
d.SetInt64(0)
return d, nil
}
return types.NewIntDatum(0), nil
if leftBool == compareResultNull || rightBool == compareResultNull {
d.SetNull()
return d, nil
}
d.SetInt64(1)
return d, nil
}

func (e *Evaluator) evalOr(expr *tipb.Expr) (types.Datum, error) {
leftBool, rightBool, err := e.evalTwoBoolChildren(expr)
if err != nil {
return types.Datum{}, errors.Trace(err)
}
var d types.Datum
if leftBool == 1 || rightBool == 1 {
return types.NewIntDatum(1), nil
d.SetInt64(1)
return d, nil
}
return types.NewIntDatum(0), nil
if leftBool == compareResultNull || rightBool == compareResultNull {
d.SetNull()
return d, nil
}
d.SetInt64(0)
return d, nil
}

func (e *Evaluator) evalTwoBoolChildren(expr *tipb.Expr) (leftBool, rightBool int64, err error) {
Expand Down
16 changes: 16 additions & 0 deletions xapi/xeval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ func (s *testEvalSuite) TestEval(c *C) {
binaryExpr(types.NewIntDatum(1), types.NewIntDatum(1), tipb.ExprType_And),
types.NewIntDatum(1),
},
{
binaryExpr(types.NewIntDatum(0), types.Datum{}, tipb.ExprType_And),
types.NewIntDatum(0),
},
{
binaryExpr(types.NewIntDatum(1), types.Datum{}, tipb.ExprType_And),
types.Datum{},
},
{
binaryExpr(types.NewIntDatum(0), types.NewIntDatum(0), tipb.ExprType_Or),
types.NewIntDatum(0),
Expand All @@ -175,6 +183,14 @@ func (s *testEvalSuite) TestEval(c *C) {
binaryExpr(types.NewIntDatum(0), types.NewIntDatum(1), tipb.ExprType_Or),
types.NewIntDatum(1),
},
{
binaryExpr(types.NewIntDatum(0), types.Datum{}, tipb.ExprType_Or),
types.Datum{},
},
{
binaryExpr(types.NewIntDatum(1), types.Datum{}, tipb.ExprType_Or),
types.NewIntDatum(1),
},
{
binaryExpr(
binaryExpr(types.NewIntDatum(1), types.NewIntDatum(1), tipb.ExprType_EQ),
Expand Down

0 comments on commit 7e27211

Please sign in to comment.