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 float64 overflow check in plus/minus real function #24179

Merged
merged 10 commits into from
Jun 24, 2021
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
9 changes: 9 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9761,6 +9761,15 @@ OR Variable_name = 'license' OR Variable_name = 'init_connect'`).Rows(), HasLen,

}

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