Skip to content

Commit

Permalink
Fix duplicate warnings for string-to-float truncation
Browse files Browse the repository at this point in the history
Closes: #25829
  • Loading branch information
dveeden committed Jul 13, 2021
1 parent 9abf9d0 commit e25d4d4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 8 additions & 4 deletions types/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func NumberToDuration(number int64, fsp int8) (Duration, error) {
// getValidIntPrefix gets prefix of the string which can be successfully parsed as int.
func getValidIntPrefix(sc *stmtctx.StatementContext, str string, isFuncCast bool) (string, error) {
if !isFuncCast {
floatPrefix, err := getValidFloatPrefix(sc, str, isFuncCast)
floatPrefix, err := getValidFloatPrefix(sc, str, isFuncCast, true)
if err != nil {
return floatPrefix, errors.Trace(err)
}
Expand Down Expand Up @@ -524,7 +524,7 @@ func floatStrToIntStr(sc *stmtctx.StatementContext, validFloat string, oriStr st
// StrToFloat converts a string to a float64 at the best-effort.
func StrToFloat(sc *stmtctx.StatementContext, str string, isFuncCast bool) (float64, error) {
str = strings.TrimSpace(str)
validStr, err := getValidFloatPrefix(sc, str, isFuncCast)
validStr, err := getValidFloatPrefix(sc, str, isFuncCast, false)
f, err1 := strconv.ParseFloat(validStr, 64)
if err1 != nil {
if err2, ok := err1.(*strconv.NumError); ok {
Expand Down Expand Up @@ -662,7 +662,7 @@ func ConvertJSONToDecimal(sc *stmtctx.StatementContext, j json.BinaryJSON) (*MyD
}

// getValidFloatPrefix gets prefix of string which can be successfully parsed as float.
func getValidFloatPrefix(sc *stmtctx.StatementContext, s string, isFuncCast bool) (valid string, err error) {
func getValidFloatPrefix(sc *stmtctx.StatementContext, s string, isFuncCast bool, hideWarning bool) (valid string, err error) {
if isFuncCast && s == "" {
return "0", nil
}
Expand Down Expand Up @@ -710,7 +710,11 @@ func getValidFloatPrefix(sc *stmtctx.StatementContext, s string, isFuncCast bool
valid = "0"
}
if validLen == 0 || validLen != len(s) {
err = errors.Trace(sc.HandleTruncate(ErrTruncatedWrongVal.GenWithStackByArgs("DOUBLE", s)))
if hideWarning {
err = errors.Trace(ErrTruncatedWrongVal.GenWithStackByArgs("DOUBLE", s))
} else {
err = errors.Trace(sc.HandleTruncate(ErrTruncatedWrongVal.GenWithStackByArgs("DOUBLE", s)))
}
}
return valid, err
}
Expand Down
2 changes: 1 addition & 1 deletion types/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ func (s *testTypeConvertSuite) TestGetValidFloat(c *C) {
}
sc := new(stmtctx.StatementContext)
for _, tt := range tests {
prefix, _ := getValidFloatPrefix(sc, tt.origin, false)
prefix, _ := getValidFloatPrefix(sc, tt.origin, false, false)
c.Assert(prefix, Equals, tt.valid)
_, err := strconv.ParseFloat(prefix, 64)
c.Assert(err, IsNil)
Expand Down

0 comments on commit e25d4d4

Please sign in to comment.