Skip to content

Commit 90f86e6

Browse files
author
James Cor
committed
refactor
1 parent 58ec4b8 commit 90f86e6

File tree

5 files changed

+7
-14
lines changed

5 files changed

+7
-14
lines changed

sql/errors.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -948,9 +948,8 @@ var (
948948

949949
ErrEnumTypeTruncated = errors.NewKind("new enum type change truncates value")
950950

951-
// ErrDoubleTypeTruncated is thrown when trying to convert a bad value to a double
952-
// TODO: MySQL's exact error message is "Data truncated for column '%s' at row %d"
953-
ErrDoubleTypeTruncated = errors.NewKind("data truncated for '%v'")
951+
// ErrTruncatedIncorrect is thrown when converting a value results in portions of the data to be trimmed.
952+
ErrTruncatedIncorrect = errors.NewKind("Truncated incorrect %s value: %v")
954953
)
955954

956955
// CastSQLError returns a *mysql.SQLError with the error code and in some cases, also a SQL state, populated for the

sql/expression/convert.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,14 +366,13 @@ func convertValue(ctx *sql.Context, val interface{}, castTo string, originType s
366366
}
367367
return d, nil
368368
case ConvertToDouble, ConvertToReal:
369-
//value, err := prepareForNumericContext(val, originType, false)
370-
value, err := types.ConvertOrTruncate(ctx, val, originType)
369+
value, err := prepareForNumericContext(val, originType, false)
371370
if err != nil {
372371
return nil, err
373372
}
374373
d, _, err := types.Float64.Convert(ctx, value)
375374
if err != nil {
376-
if sql.ErrDoubleTypeTruncated.Is(err) {
375+
if sql.ErrTruncatedIncorrect.Is(err) {
377376
ctx.Warn(1265, "%s", err.Error())
378377
return d, nil
379378
}

sql/plan/hash_lookup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (n *HashLookup) GetHashKey(ctx *sql.Context, e sql.Expression, row sql.Row)
170170
// The LHS expression was NullType. This is allowed.
171171
return nil, nil
172172
}
173-
if err != nil && !sql.ErrDoubleTypeTruncated.Is(err) {
173+
if err != nil && !sql.ErrTruncatedIncorrect.Is(err) {
174174
// Truncated warning is already thrown elsewhere.
175175
return nil, err
176176
}

sql/types/conversion.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -768,11 +768,6 @@ func TypeAwareConversion(ctx *sql.Context, val interface{}, originalType sql.Typ
768768
// value is truncated to the Zero value for type |t|. If the value does not convert and the type is not automatically
769769
// coerced, then return an error.
770770
func ConvertOrTruncate(ctx *sql.Context, i interface{}, t sql.Type) (interface{}, error) {
771-
// Do nothing if type is no provided.
772-
if t == nil {
773-
return i, nil
774-
}
775-
776771
converted, _, err := t.Convert(ctx, i)
777772
if err == nil {
778773
return converted, nil

sql/types/number.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ func (t NumberTypeImpl_) SQLUint64(ctx *sql.Context, dest []byte, v interface{})
530530

531531
func (t NumberTypeImpl_) SQLFloat64(ctx *sql.Context, dest []byte, v interface{}) ([]byte, error) {
532532
num, err := convertToFloat64(t, v)
533-
if err != nil && !sql.ErrDoubleTypeTruncated.Is(err) {
533+
if err != nil && !sql.ErrTruncatedIncorrect.Is(err) {
534534
return nil, err
535535
}
536536
dest = strconv.AppendFloat(dest, num, 'g', -1, 64)
@@ -1544,7 +1544,7 @@ func convertToFloat64(t NumberTypeImpl_, v interface{}) (float64, error) {
15441544
// parse the first longest valid numbers
15451545
s := numre.FindString(v)
15461546
i, _ = strconv.ParseFloat(s, 64)
1547-
return i, sql.ErrDoubleTypeTruncated.New(v)
1547+
return i, sql.ErrTruncatedIncorrect.New(t.String(), v)
15481548
}
15491549
return i, nil
15501550
case bool:

0 commit comments

Comments
 (0)