Skip to content

Commit

Permalink
cherry pick pingcap#24179 to release-5.1
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <[email protected]>
  • Loading branch information
wshwsh12 authored and ti-srebot committed Jul 29, 2021
1 parent 797bddd commit f91488e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
5 changes: 3 additions & 2 deletions expression/builtin_arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
math2 "github.com/pingcap/tidb/util/math"
"github.com/pingcap/tipb/go-tipb"
)

Expand Down Expand Up @@ -296,7 +297,7 @@ func (s *builtinArithmeticPlusRealSig) evalReal(row chunk.Row) (float64, bool, e
if isLHSNull || isRHSNull {
return 0, true, nil
}
if (a > 0 && b > math.MaxFloat64-a) || (a < 0 && b < -math.MaxFloat64-a) {
if !math2.IsFinite(a + b) {
return 0, true, types.ErrOverflow.GenWithStackByArgs("DOUBLE", fmt.Sprintf("(%s + %s)", s.args[0].String(), s.args[1].String()))
}
return a + b, false, nil
Expand Down Expand Up @@ -365,7 +366,7 @@ func (s *builtinArithmeticMinusRealSig) evalReal(row chunk.Row) (float64, bool,
if isNull || err != nil {
return 0, isNull, err
}
if (a > 0 && -b > math.MaxFloat64-a) || (a < 0 && -b < -math.MaxFloat64-a) {
if !math2.IsFinite(a - b) {
return 0, true, types.ErrOverflow.GenWithStackByArgs("DOUBLE", fmt.Sprintf("(%s - %s)", s.args[0].String(), s.args[1].String()))
}
return a - b, false, nil
Expand Down
5 changes: 3 additions & 2 deletions expression/builtin_arithmetic_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
math2 "github.com/pingcap/tidb/util/math"
)

func (b *builtinArithmeticMultiplyRealSig) vectorized() bool {
Expand Down Expand Up @@ -323,7 +324,7 @@ func (b *builtinArithmeticMinusRealSig) vecEvalReal(input *chunk.Chunk, result *
if result.IsNull(i) {
continue
}
if (x[i] > 0 && -y[i] > math.MaxFloat64-x[i]) || (x[i] < 0 && -y[i] < -math.MaxFloat64-x[i]) {
if !math2.IsFinite(x[i] - y[i]) {
return types.ErrOverflow.GenWithStackByArgs("DOUBLE", fmt.Sprintf("(%s - %s)", b.args[0].String(), b.args[1].String()))
}
x[i] = x[i] - y[i]
Expand Down Expand Up @@ -522,7 +523,7 @@ func (b *builtinArithmeticPlusRealSig) vecEvalReal(input *chunk.Chunk, result *c
if result.IsNull(i) {
continue
}
if (x[i] > 0 && y[i] > math.MaxFloat64-x[i]) || (x[i] < 0 && y[i] < -math.MaxFloat64-x[i]) {
if !math2.IsFinite(x[i] + y[i]) {
return types.ErrOverflow.GenWithStackByArgs("DOUBLE", fmt.Sprintf("(%s + %s)", b.args[0].String(), b.args[1].String()))
}
x[i] = x[i] + y[i]
Expand Down
23 changes: 23 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9734,6 +9734,29 @@ OR Variable_name = 'license' OR Variable_name = 'init_connect'`).Rows(), HasLen,

}

<<<<<<< HEAD
=======
func (s *testIntegrationSuite) TestFloat64Inf(c *C) {
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustQuery("select '1e800' + 1e100;").Check(
testkit.Rows("179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"))
tk.MustQuery("select '-1e800' - 1e100;").Check(
testkit.Rows("-179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"))
}

func (s *testIntegrationSuite) TestCharsetErr(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table charset_test(id int auto_increment primary key, c1 varchar(255) character set ascii)")
err := tk.ExecToErr("insert into charset_test(c1) values ('aaa\xEF\xBF\xBDabcdef')")
c.Assert(err.Error(), Equals, "[table:1366]Incorrect string value '\\xEF\\xBF\\xBDabc...' for column 'c1'")

err = tk.ExecToErr("insert into charset_test(c1) values ('aaa\xEF\xBF\xBD')")
c.Assert(err.Error(), Equals, "[table:1366]Incorrect string value '\\xEF\\xBF\\xBD' for column 'c1'")
}

>>>>>>> 88b587c08... expression: fix float64 overflow check in plus/minus real function (#24179)
func (s *testIntegrationSuite2) TestIssue25591(c *C) {
tk := testkit.NewTestKit(c, s.store)

Expand Down
5 changes: 5 additions & 0 deletions util/math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ func StrLenOfInt64Fast(x int64) int {
func Log2(x float64) float64 {
return math.Log2(x)
}

// IsFinite reports whether f is neither NaN nor an infinity.
func IsFinite(f float64) bool {
return !math.IsNaN(f - f)
}

0 comments on commit f91488e

Please sign in to comment.