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

types: check values equals to NaN or Inf when convert to float #30148

Merged
merged 3 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -1037,8 +1037,7 @@ func (d *Datum) convertToFloat(sc *stmtctx.StatementContext, target *FieldType)
default:
return invalidConv(d, target.Tp)
}
var err1 error
f, err1 = ProduceFloatWithSpecifiedTp(f, target, sc)
f, err1 := ProduceFloatWithSpecifiedTp(f, target, sc)
if err == nil && err1 != nil {
err = err1
}
Expand All @@ -1052,6 +1051,12 @@ func (d *Datum) convertToFloat(sc *stmtctx.StatementContext, target *FieldType)

// ProduceFloatWithSpecifiedTp produces a new float64 according to `flen` and `decimal`.
func ProduceFloatWithSpecifiedTp(f float64, target *FieldType, sc *stmtctx.StatementContext) (_ float64, err error) {
if math.IsNaN(f) {
return 0, overflow(f, target.Tp)
}
if math.IsInf(f, 0) {
return f, overflow(f, target.Tp)
}
// For float and following double type, we will only truncate it for float(M, D) format.
// If no D is set, we will handle it like origin float whether M is set or not.
if target.Flen != UnspecifiedLength && target.Decimal != UnspecifiedLength {
Expand Down
62 changes: 24 additions & 38 deletions types/datum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,59 +152,45 @@ func TestToInt64(t *testing.T) {
testDatumToInt64(t, v, int64(3))
}

func TestToFloat32(t *testing.T) {
t.Parallel()
ft := NewFieldType(mysql.TypeFloat)
var datum = NewFloat64Datum(281.37)
sc := new(stmtctx.StatementContext)
sc.IgnoreTruncate = true
converted, err := datum.ConvertTo(sc, ft)
require.NoError(t, err)
require.Equal(t, KindFloat32, converted.Kind())
require.Equal(t, float32(281.37), converted.GetFloat32())

datum.SetString("281.37", mysql.DefaultCollationName)
converted, err = datum.ConvertTo(sc, ft)
require.NoError(t, err)
require.Equal(t, KindFloat32, converted.Kind())
require.Equal(t, float32(281.37), converted.GetFloat32())

ft = NewFieldType(mysql.TypeDouble)
datum = NewFloat32Datum(281.37)
converted, err = datum.ConvertTo(sc, ft)
require.NoError(t, err)
require.Equal(t, KindFloat64, converted.Kind())
// Convert to float32 and convert back to float64, we will get a different value.
require.NotEqual(t, 281.37, converted.GetFloat64())
require.Equal(t, datum.GetFloat64(), converted.GetFloat64())
}

func TestToFloat64(t *testing.T) {
func TestConvertToFloat(t *testing.T) {
t.Parallel()
testCases := []struct {
d Datum
tp byte
errMsg string
result float64
r64 float64
r32 float32
}{
{NewDatum(float32(3.00)), "", 3.00},
{NewDatum(float64(12345.678)), "", 12345.678},
{NewDatum("12345.678"), "", 12345.678},
{NewDatum([]byte("12345.678")), "", 12345.678},
{NewDatum(int64(12345)), "", 12345},
{NewDatum(uint64(123456)), "", 123456},
{NewDatum(byte(123)), "cannot convert .*", 0},
{NewDatum(float32(3.00)), mysql.TypeDouble, "", 3.00, 3.00},
{NewDatum(float64(12345.678)), mysql.TypeDouble, "", 12345.678, 12345.678},
{NewDatum("12345.678"), mysql.TypeDouble, "", 12345.678, 12345.678},
{NewDatum([]byte("12345.678")), mysql.TypeDouble, "", 12345.678, 12345.678},
{NewDatum(int64(12345)), mysql.TypeDouble, "", 12345, 12345},
{NewDatum(uint64(123456)), mysql.TypeDouble, "", 123456, 123456},
{NewDatum(byte(123)), mysql.TypeDouble, "cannot convert .*", 0, 0},
{NewDatum(math.NaN()), mysql.TypeDouble, "constant .* overflows double", 0, 0},
{NewDatum(math.Inf(-1)), mysql.TypeDouble, "constant .* overflows double", math.Inf(-1), float32(math.Inf(-1))},
{NewDatum(math.Inf(1)), mysql.TypeDouble, "constant .* overflows double", math.Inf(1), float32(math.Inf(1))},
{NewDatum(float32(281.37)), mysql.TypeFloat, "", 281.37, 281.37},
{NewDatum("281.37"), mysql.TypeFloat, "", 281.37, 281.37},
}

sc := new(stmtctx.StatementContext)
sc.IgnoreTruncate = true
for _, testCase := range testCases {
converted, err := testCase.d.ToFloat64(sc)
converted, err := testCase.d.ConvertTo(sc, NewFieldType(testCase.tp))
if testCase.errMsg == "" {
require.NoError(t, err)
} else {
require.Regexp(t, testCase.errMsg, err)
}
require.Equal(t, testCase.result, converted)
require.Equal(t, testCase.r32, converted.GetFloat32())
if testCase.tp == mysql.TypeDouble {
require.Equal(t, testCase.r64, converted.GetFloat64())
} else {
// Convert to float32 and convert back to float64, we will get a different value.
require.NotEqual(t, testCase.r64, converted.GetFloat64())
}
}
}

Expand Down