Skip to content

Commit 44f3d60

Browse files
authored
Merge pull request #3268 from dolthub/angela/nan_panic
[no-release-notes] Refactor NaN and infinity checks into separate function
2 parents b43fb09 + c8a68c2 commit 44f3d60

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

sql/types/decimal.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package types
1717
import (
1818
"context"
1919
"fmt"
20-
"math"
2120
"math/big"
2221
"reflect"
2322
"strings"
@@ -204,8 +203,8 @@ func (t DecimalType_) ConvertToNullDecimal(v interface{}) (decimal.NullDecimal,
204203
case float32:
205204
return t.ConvertToNullDecimal(decimal.NewFromFloat32(value))
206205
case float64:
207-
if math.IsInf(value, 0) || math.IsNaN(value) {
208-
return decimal.NullDecimal{}, ErrConvertingToDecimal.New(v)
206+
if !IsValidFloat(value) {
207+
return decimal.NullDecimal{}, ErrConvertingToDecimal.New(value)
209208
}
210209
return t.ConvertToNullDecimal(decimal.NewFromFloat(value))
211210
case string:

sql/types/number.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ func convertToInt64(t NumberTypeImpl_, v any, round Round) (int64, sql.ConvertIn
10931093
if v < float64(math.MinInt64) {
10941094
return math.MinInt64, sql.OutOfRange, nil
10951095
}
1096-
if math.IsInf(v, 0) || math.IsNaN(v) {
1096+
if !IsValidFloat(v) {
10971097
return 0, sql.OutOfRange, sql.ErrInvalidValue.New(v, t.String())
10981098
}
10991099
return int64(math.Round(v)), sql.InRange, nil
@@ -1294,7 +1294,7 @@ func convertToUint64(t NumberTypeImpl_, v any, round Round) (uint64, sql.Convert
12941294
if v < 0 {
12951295
return uint64(math.MaxUint64 - uint(-v-1)), sql.OutOfRange, nil
12961296
}
1297-
if math.IsInf(v, 0) || math.IsNaN(v) {
1297+
if !IsValidFloat(v) {
12981298
return 0, sql.OutOfRange, sql.ErrInvalidValue.New(v, t.String())
12991299
}
13001300
return uint64(math.Round(v)), sql.InRange, nil
@@ -1395,8 +1395,8 @@ func convertToFloat64(t NumberTypeImpl_, v interface{}) (float64, error) {
13951395
case float32:
13961396
return float64(v), nil
13971397
case float64:
1398-
if math.IsInf(v, 0) || math.IsNaN(v) {
1399-
return 0, sql.ErrInvalidValue.New(v, t.String())
1398+
if !IsValidFloat(v) {
1399+
return v, sql.ErrInvalidValue.New(v, t.String())
14001400
}
14011401
return v, nil
14021402
case decimal.Decimal:
@@ -1565,3 +1565,9 @@ func ConvertHexBlobToDecimalForNumericContext(val interface{}, originType sql.Ty
15651565
}
15661566
return val, nil
15671567
}
1568+
1569+
// IsValidFloat returns false in go-mysql-server if a float is NaN or infinity. Since NaN and infinity values are
1570+
// allowed in Doltgres, this function is replaced there.
1571+
var IsValidFloat = func(f float64) bool {
1572+
return !math.IsNaN(f) && !math.IsInf(f, 0)
1573+
}

0 commit comments

Comments
 (0)