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

expression: fix DIV with decimal type (#11804) #11813

Merged
merged 4 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 23 additions & 4 deletions expression/builtin_arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,11 +762,30 @@ func (s *builtinArithmeticIntDivideDecimalSig) evalInt(row chunk.Row) (ret int64
return 0, true, 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