Skip to content
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
5 changes: 2 additions & 3 deletions sql/types/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package types
import (
"context"
"fmt"
"math"
"math/big"
"reflect"
"strings"
Expand Down Expand Up @@ -204,8 +203,8 @@ func (t DecimalType_) ConvertToNullDecimal(v interface{}) (decimal.NullDecimal,
case float32:
return t.ConvertToNullDecimal(decimal.NewFromFloat32(value))
case float64:
if math.IsInf(value, 0) || math.IsNaN(value) {
return decimal.NullDecimal{}, ErrConvertingToDecimal.New(v)
if !IsValidFloat(value) {
return decimal.NullDecimal{}, ErrConvertingToDecimal.New(value)
}
return t.ConvertToNullDecimal(decimal.NewFromFloat(value))
case string:
Expand Down
14 changes: 10 additions & 4 deletions sql/types/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ func convertToInt64(t NumberTypeImpl_, v any, round Round) (int64, sql.ConvertIn
if v < float64(math.MinInt64) {
return math.MinInt64, sql.OutOfRange, nil
}
if math.IsInf(v, 0) || math.IsNaN(v) {
if !IsValidFloat(v) {
return 0, sql.OutOfRange, sql.ErrInvalidValue.New(v, t.String())
}
return int64(math.Round(v)), sql.InRange, nil
Expand Down Expand Up @@ -1294,7 +1294,7 @@ func convertToUint64(t NumberTypeImpl_, v any, round Round) (uint64, sql.Convert
if v < 0 {
return uint64(math.MaxUint64 - uint(-v-1)), sql.OutOfRange, nil
}
if math.IsInf(v, 0) || math.IsNaN(v) {
if !IsValidFloat(v) {
return 0, sql.OutOfRange, sql.ErrInvalidValue.New(v, t.String())
}
return uint64(math.Round(v)), sql.InRange, nil
Expand Down Expand Up @@ -1395,8 +1395,8 @@ func convertToFloat64(t NumberTypeImpl_, v interface{}) (float64, error) {
case float32:
return float64(v), nil
case float64:
if math.IsInf(v, 0) || math.IsNaN(v) {
return 0, sql.ErrInvalidValue.New(v, t.String())
if !IsValidFloat(v) {
return v, sql.ErrInvalidValue.New(v, t.String())
}
return v, nil
case decimal.Decimal:
Expand Down Expand Up @@ -1565,3 +1565,9 @@ func ConvertHexBlobToDecimalForNumericContext(val interface{}, originType sql.Ty
}
return val, nil
}

// IsValidFloat returns false in go-mysql-server if a float is NaN or infinity. Since NaN and infinity values are
// allowed in Doltgres, this function is replaced there.
var IsValidFloat = func(f float64) bool {
return !math.IsNaN(f) && !math.IsInf(f, 0)
}
Loading