Skip to content

Commit d6c2f7f

Browse files
author
James Cor
committed
migrate functions
1 parent 0e40322 commit d6c2f7f

File tree

2 files changed

+26
-33
lines changed

2 files changed

+26
-33
lines changed

sql/expression/convert.go

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
package expression
1616

1717
import (
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-
}

sql/types/number.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,3 +1534,19 @@ func TruncateStringToDouble(s string) (string, bool) {
15341534
}
15351535
return s[:i], i != n
15361536
}
1537+
1538+
// ConvertHexBlobToDecimalForNumericContext converts byte array value to unsigned int value if originType is BLOB type.
1539+
// This function is called when convertTo type is number type only. The hex literal values are parsed into blobs as
1540+
// binary string as default, but for numeric context, the value should be a number.
1541+
// Byte arrays of other SQL types are not handled here.
1542+
func ConvertHexBlobToDecimalForNumericContext(val interface{}, originType sql.Type) (interface{}, error) {
1543+
if bin, isBinary := val.([]byte); isBinary && IsBlobType(originType) {
1544+
stringVal := hex.EncodeToString(bin)
1545+
decimalNum, err := strconv.ParseUint(stringVal, 16, 64)
1546+
if err != nil {
1547+
return nil, errors.New("failed to convert hex blob value to unsigned int")
1548+
}
1549+
val = decimalNum
1550+
}
1551+
return val, nil
1552+
}

0 commit comments

Comments
 (0)