Skip to content

Commit

Permalink
cherry pick #18961 to release-4.0 (#18967)
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <[email protected]>

Co-authored-by: HuaiyuXu <[email protected]>
  • Loading branch information
ti-srebot and XuHuaiyu authored Aug 4, 2020
1 parent d79de8b commit df16c56
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
8 changes: 0 additions & 8 deletions expression/builtin_arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,14 +793,6 @@ func (s *builtinArithmeticIntDivideDecimalSig) evalInt(row chunk.Row) (ret int64
var num [2]*types.MyDecimal
for i, arg := range s.args {
num[i], isNull, err = arg.EvalDecimal(s.ctx, row)
// Its behavior is consistent with MySQL.
if terror.ErrorEqual(err, types.ErrTruncated) {
err = nil
}
if terror.ErrorEqual(err, types.ErrOverflow) {
newErr := errTruncatedWrongValue.GenWithStackByArgs("DECIMAL", arg)
err = sc.HandleOverflow(newErr, newErr)
}
if isNull || err != nil {
return 0, isNull, err
}
Expand Down
8 changes: 0 additions & 8 deletions expression/builtin_arithmetic_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,14 +665,6 @@ func (b *builtinArithmeticIntDivideDecimalSig) vecEvalInt(input *chunk.Chunk, re
defer b.bufAllocator.put(buf[i])

err = arg.VecEvalDecimal(b.ctx, input, buf[i])
// Its behavior is consistent with MySQL.
if terror.ErrorEqual(err, types.ErrTruncated) {
err = nil
}
if terror.ErrorEqual(err, types.ErrOverflow) {
newErr := errTruncatedWrongValue.GenWithStackByArgs("DECIMAL", arg)
err = sc.HandleOverflow(newErr, newErr)
}
if err != nil {
return err
}
Expand Down
7 changes: 7 additions & 0 deletions expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,13 @@ func (b *builtinCastRealAsDecimalSig) evalDecimal(row chunk.Row) (res *types.MyD
res = new(types.MyDecimal)
if !b.inUnion || val >= 0 {
err = res.FromFloat64(val)
if types.ErrOverflow.Equal(err) {
warnErr := types.ErrTruncatedWrongVal.GenWithStackByArgs("DECIMAL", b.args[0])
err = b.ctx.GetSessionVars().StmtCtx.HandleOverflow(err, warnErr)
} else if types.ErrTruncated.Equal(err) {
// This behavior is consistent with MySQL.
err = nil
}
if err != nil {
return res, false, err
}
Expand Down
11 changes: 10 additions & 1 deletion expression/builtin_cast_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,16 @@ func (b *builtinCastRealAsDecimalSig) vecEvalDecimal(input *chunk.Chunk, result
for i := 0; i < n; i++ {
if !b.inUnion || bufreal[i] >= 0 {
if err = resdecimal[i].FromFloat64(bufreal[i]); err != nil {
return err
if types.ErrOverflow.Equal(err) {
warnErr := types.ErrTruncatedWrongVal.GenWithStackByArgs("DECIMAL", b.args[0])
err = b.ctx.GetSessionVars().StmtCtx.HandleOverflow(err, warnErr)
} else if types.ErrTruncated.Equal(err) {
// This behavior is consistent with MySQL.
err = nil
}
if err != nil {
return err
}
}
}
dec, err := types.ProduceDecWithSpecifiedTp(&resdecimal[i], b.tp, b.ctx.GetSessionVars().StmtCtx)
Expand Down
6 changes: 3 additions & 3 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3228,9 +3228,9 @@ func (s *testIntegrationSuite) TestArithmeticBuiltin(c *C) {
result = tk.MustQuery("SELECT 1.175494351E-37 div 1.7976931348623157E+308, 1.7976931348623157E+308 div -1.7976931348623157E+307, 1 div 1e-82;")
result.Check(testkit.Rows("0 -1 <nil>"))
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|",
"Warning|1292|Truncated incorrect DECIMAL value: 'cast(1.7976931348623157e+308, decimal(309,0) BINARY)'",
"Warning|1292|Truncated incorrect DECIMAL value: 'cast(1.7976931348623157e+308, decimal(309,0) BINARY)'",
"Warning|1292|Truncated incorrect DECIMAL value: 'cast(-1.7976931348623158e+307, decimal(309,0) BINARY)'",
"Warning|1292|Truncated incorrect DECIMAL value: '1.7976931348623157e+308'",
"Warning|1292|Truncated incorrect DECIMAL value: '1.7976931348623157e+308'",
"Warning|1292|Truncated incorrect DECIMAL value: '-1.7976931348623158e+307'",
"Warning|1365|Division by 0"))
rs, err = tk.Exec("select 1e300 DIV 1.5")
c.Assert(err, IsNil)
Expand Down

0 comments on commit df16c56

Please sign in to comment.