1515package expression
1616
1717import (
18- "encoding/hex"
1918 "fmt"
20- "strconv"
2119 "strings"
2220 "time"
2321
@@ -349,7 +347,7 @@ func convertValue(ctx *sql.Context, val interface{}, castTo string, originType s
349347 }
350348 return d , nil
351349 case ConvertToDecimal :
352- value , err := prepareForNumericContext ( ctx , val , originType , false )
350+ value , err := types . ConvertHexBlobToDecimalForNumericContext ( val , originType )
353351 if err != nil {
354352 return nil , err
355353 }
@@ -363,17 +361,20 @@ func convertValue(ctx *sql.Context, val interface{}, castTo string, originType s
363361 }
364362 return d , nil
365363 case ConvertToFloat :
366- value , err := prepareForNumericContext ( ctx , val , originType , false )
364+ value , err := types . ConvertHexBlobToDecimalForNumericContext ( val , originType )
367365 if err != nil {
368366 return nil , err
369367 }
370368 d , _ , err := types .Float32 .Convert (ctx , value )
371369 if err != nil {
372- return types .Float32 .Zero (), nil
370+ if ! sql .ErrTruncatedIncorrect .Is (err ) {
371+ return types .Float64 .Zero (), nil
372+ }
373+ ctx .Warn (mysql .ERTruncatedWrongValue , "%s" , err .Error ())
373374 }
374375 return d , nil
375376 case ConvertToDouble , ConvertToReal :
376- value , err := prepareForNumericContext ( ctx , val , originType , false )
377+ value , err := types . ConvertHexBlobToDecimalForNumericContext ( val , originType )
377378 if err != nil {
378379 return nil , err
379380 }
@@ -392,7 +393,7 @@ func convertValue(ctx *sql.Context, val interface{}, castTo string, originType s
392393 }
393394 return js , nil
394395 case ConvertToSigned :
395- value , err := prepareForNumericContext ( ctx , val , originType , true )
396+ value , err := types . ConvertHexBlobToDecimalForNumericContext ( val , originType )
396397 if err != nil {
397398 return nil , err
398399 }
@@ -411,7 +412,7 @@ func convertValue(ctx *sql.Context, val interface{}, castTo string, originType s
411412 }
412413 return t , nil
413414 case ConvertToUnsigned :
414- value , err := prepareForNumericContext ( ctx , val , originType , true )
415+ value , err := types . ConvertHexBlobToDecimalForNumericContext ( val , originType )
415416 if err != nil {
416417 return nil , err
417418 }
@@ -427,7 +428,7 @@ func convertValue(ctx *sql.Context, val interface{}, castTo string, originType s
427428 }
428429 return num , nil
429430 case ConvertToYear :
430- value , err := prepareForNumericContext ( ctx , val , originType , true )
431+ value , err := types . ConvertHexBlobToDecimalForNumericContext ( val , originType )
431432 if err != nil {
432433 return nil , err
433434 }
@@ -488,27 +489,3 @@ func createConvertedDecimalType(length, scale int, logErrors bool) sql.DecimalTy
488489 }
489490 return types .InternalDecimalType
490491}
491-
492- // prepareForNumberContext makes necessary preparations to strings and byte arrays for conversions to numbers
493- func prepareForNumericContext (ctx * sql.Context , val interface {}, originType sql.Type , isInt bool ) (interface {}, error ) {
494- if s , isString := val .(string ); isString && types .IsTextOnly (originType ) {
495- return sql .TrimStringToNumberPrefix (ctx , s , isInt ), nil
496- }
497- return convertHexBlobToDecimalForNumericContext (val , originType )
498- }
499-
500- // convertHexBlobToDecimalForNumericContext converts byte array value to unsigned int value if originType is BLOB type.
501- // This function is called when convertTo type is number type only. The hex literal values are parsed into blobs as
502- // binary string as default, but for numeric context, the value should be a number.
503- // Byte arrays of other SQL types are not handled here.
504- func convertHexBlobToDecimalForNumericContext (val interface {}, originType sql.Type ) (interface {}, error ) {
505- if bin , isBinary := val .([]byte ); isBinary && types .IsBlobType (originType ) {
506- stringVal := hex .EncodeToString (bin )
507- decimalNum , err := strconv .ParseUint (stringVal , 16 , 64 )
508- if err != nil {
509- return nil , errors .NewKind ("failed to convert hex blob value to unsigned int" ).New ()
510- }
511- val = decimalNum
512- }
513- return val , nil
514- }
0 commit comments