Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expressions: use integer for time in arithmetic op if FSP = 0 #133

Merged
merged 2 commits into from
Sep 14, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions expression/expressions/binop.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,11 +675,21 @@ 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 {
// Integer number 20111111101010 can not overflow int64.
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 {
// Integer number 111111 can not overflow int64.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems 111111 cannot overflow?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

111111 < math.Int64 of course

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.
But if make sure the ret value is valid, this comment is not needed?

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)
}