Skip to content

Commit

Permalink
Merge pull request #133 from pingcap/siddontang/time-arithmetic
Browse files Browse the repository at this point in the history
expressions: use integer for time in arithmetic op if FSP = 0
  • Loading branch information
ngaut committed Sep 14, 2015
2 parents aeb5f69 + 23f8e96 commit 550c0a1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
16 changes: 12 additions & 4 deletions expression/expressions/binop.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,11 +675,19 @@ func (o *BinaryOperation) coerceArithmetic(a interface{}) (interface{}, error) {
}
return f, err
case mysql.Time:
// TODO: if time has no precision, return int64
return x.ToNumber(), nil
// if time has no precision, return int64
v := x.ToNumber()
if x.Fsp == 0 {
return v.IntPart(), nil
}
return v, nil
case mysql.Duration:
// TODO: if duration has no precision, return int64
return x.ToNumber(), nil
// if duration has no precision, return int64
v := x.ToNumber()
if x.Fsp == 0 {
return v.IntPart(), nil
}
return v, nil
case []byte:
// []byte is the same as string, converted to float for arithmetic operator.
f, err := types.StrToFloat(string(x))
Expand Down
11 changes: 11 additions & 0 deletions expression/expressions/binop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package expressions

import (
"errors"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/expression"
Expand Down Expand Up @@ -416,6 +417,9 @@ func (s *testBinOpSuite) TestNumericOp(c *C) {
{uint64(1), opcode.Mul, uint64(1), 1},
{mysql.Time{}, opcode.Mul, 0, 0},
{mysql.ZeroDuration, opcode.Mul, 0, 0},
{mysql.Time{Time: time.Now(), Fsp: 0, Type: mysql.TypeDatetime}, opcode.Mul, 0, 0},
{mysql.Time{Time: time.Now(), Fsp: 6, Type: mysql.TypeDatetime}, opcode.Mul, 0, 0},
{mysql.Duration{Duration: 100000000, Fsp: 6}, opcode.Mul, 0, 0},

// div
{1, opcode.Div, float64(1), 1},
Expand Down Expand Up @@ -540,5 +544,12 @@ func (s *testBinOpSuite) TestNumericOp(c *C) {

expr.R = newTestRow(1, 2)
expr.Op = opcode.Plus
_, err = expr.Eval(nil, nil)
c.Assert(err, NotNil)

expr.L = newTestRow(1, 2)
expr.R = newTestRow(1, 2)
expr.Op = opcode.Plus
_, err = expr.Eval(nil, nil)
c.Assert(err, NotNil)
}

0 comments on commit 550c0a1

Please sign in to comment.