Skip to content

Commit

Permalink
expression: fix DIV with decimal type (#11804) (#11812)
Browse files Browse the repository at this point in the history
  • Loading branch information
sre-bot authored and zz-jason committed Aug 21, 2019
1 parent 98714ab commit 79d1f0b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
27 changes: 23 additions & 4 deletions expression/builtin_arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,11 +763,30 @@ func (s *builtinArithmeticIntDivideDecimalSig) evalInt(row chunk.Row) (ret int64
return 0, true, errors.Trace(err)
}

ret, err = c.ToInt()
// err returned by ToInt may be ErrTruncated or ErrOverflow, only handle ErrOverflow, ignore ErrTruncated.
if err == types.ErrOverflow {
return 0, true, types.ErrOverflow.GenWithStackByArgs("BIGINT", fmt.Sprintf("(%s DIV %s)", s.args[0].String(), s.args[1].String()))
isLHSUnsigned := mysql.HasUnsignedFlag(s.args[0].GetType().Flag)
isRHSUnsigned := mysql.HasUnsignedFlag(s.args[1].GetType().Flag)

if isLHSUnsigned || isRHSUnsigned {
val, err := c.ToUint()
// err returned by ToUint may be ErrTruncated or ErrOverflow, only handle ErrOverflow, ignore ErrTruncated.
if err == types.ErrOverflow {
v, err := c.ToInt()
// when the final result is at (-1, 0], it should be return 0 instead of the error
if v == 0 && err == types.ErrTruncated {
ret = int64(0)
return ret, false, nil
}
return 0, true, types.ErrOverflow.GenWithStackByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s DIV %s)", s.args[0].String(), s.args[1].String()))
}
ret = int64(val)
} else {
ret, err = c.ToInt()
// err returned by ToInt may be ErrTruncated or ErrOverflow, only handle ErrOverflow, ignore ErrTruncated.
if err == types.ErrOverflow {
return 0, true, types.ErrOverflow.GenWithStackByArgs("BIGINT", fmt.Sprintf("(%s DIV %s)", s.args[0].String(), s.args[1].String()))
}
}

return ret, false, nil
}

Expand Down
8 changes: 8 additions & 0 deletions expression/builtin_arithmetic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,14 @@ func (s *testEvaluatorSuite) TestArithmeticIntDivide(c *C) {
args: []interface{}{int64(-9223372036854775808), float64(-1)},
expect: []interface{}{nil, "*BIGINT value is out of range in '\\(-9223372036854775808 DIV -1\\)'"},
},
{
args: []interface{}{uint64(1), float64(-2)},
expect: []interface{}{0, nil},
},
{
args: []interface{}{uint64(1), float64(-1)},
expect: []interface{}{nil, "*BIGINT UNSIGNED value is out of range in '\\(1 DIV -1\\)'"},
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 79d1f0b

Please sign in to comment.