@@ -980,7 +980,7 @@ func (d *Datum) convertToFloat(sc *stmtctx.StatementContext, target *FieldType)
980
980
default :
981
981
return invalidConv (d , target .GetType ())
982
982
}
983
- f , err1 := ProduceFloatWithSpecifiedTp (f , target , sc )
983
+ f , err1 := ProduceFloatWithSpecifiedTp (f , target )
984
984
if err == nil && err1 != nil {
985
985
err = err1
986
986
}
@@ -993,7 +993,7 @@ func (d *Datum) convertToFloat(sc *stmtctx.StatementContext, target *FieldType)
993
993
}
994
994
995
995
// ProduceFloatWithSpecifiedTp produces a new float64 according to `flen` and `decimal`.
996
- func ProduceFloatWithSpecifiedTp (f float64 , target * FieldType , sc * stmtctx. StatementContext ) (_ float64 , err error ) {
996
+ func ProduceFloatWithSpecifiedTp (f float64 , target * FieldType ) (_ float64 , err error ) {
997
997
if math .IsNaN (f ) {
998
998
return 0 , overflow (f , target .GetType ())
999
999
}
@@ -1004,13 +1004,17 @@ func ProduceFloatWithSpecifiedTp(f float64, target *FieldType, sc *stmtctx.State
1004
1004
// If no D is set, we will handle it like origin float whether M is set or not.
1005
1005
if target .GetFlen () != UnspecifiedLength && target .GetDecimal () != UnspecifiedLength {
1006
1006
f , err = TruncateFloat (f , target .GetFlen (), target .GetDecimal ())
1007
- if err = sc .HandleOverflow (err , err ); err != nil {
1008
- return f , errors .Trace (err )
1009
- }
1010
1007
}
1011
1008
if mysql .HasUnsignedFlag (target .GetFlag ()) && f < 0 {
1012
1009
return 0 , overflow (f , target .GetType ())
1013
1010
}
1011
+
1012
+ if err != nil {
1013
+ // We must return the error got from TruncateFloat after checking whether the target is unsigned to make sure
1014
+ // the returned float is not negative when the target type is unsigned.
1015
+ return f , errors .Trace (err )
1016
+ }
1017
+
1014
1018
if target .GetType () == mysql .TypeFloat && (f > math .MaxFloat32 || f < - math .MaxFloat32 ) {
1015
1019
if f > 0 {
1016
1020
return math .MaxFloat32 , overflow (f , target .GetType ())
0 commit comments