-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
types: fix string to integer cast #11295
Changes from 12 commits
81fac8d
8bd3eac
9bf4e6a
a2ab02b
7feed83
651e58c
bbcaea3
56ff762
d4c91fe
347572d
2542fbb
5350e14
c964c2f
c486132
b5f0657
b58c2ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -362,11 +362,35 @@ func NumberToDuration(number int64, fsp int) (Duration, error) { | |
|
||
// getValidIntPrefix gets prefix of the string which can be successfully parsed as int. | ||
func getValidIntPrefix(sc *stmtctx.StatementContext, str string) (string, error) { | ||
floatPrefix, err := getValidFloatPrefix(sc, str) | ||
if err != nil { | ||
return floatPrefix, errors.Trace(err) | ||
if !sc.CastStrToIntStrict { | ||
floatPrefix, err := getValidFloatPrefix(sc, str) | ||
if err != nil { | ||
return floatPrefix, errors.Trace(err) | ||
} | ||
return floatStrToIntStr(sc, floatPrefix, str) | ||
} | ||
|
||
validLen := 0 | ||
for i := 0; i < len(str); i++ { | ||
c := str[i] | ||
if c == '+' || c == '-' { | ||
zz-jason marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if i != 0 { | ||
break | ||
} | ||
} else if c >= '0' && c <= '9' { | ||
validLen = i + 1 | ||
} else { | ||
break | ||
} | ||
} | ||
valid := str[:validLen] | ||
if valid == "" { | ||
valid = "0" | ||
} | ||
if validLen == 0 || validLen != len(str) { | ||
return valid, errors.Trace(handleTruncateError(sc, ErrTruncatedWrongVal.GenWithStackByArgs("INTEGER", str))) | ||
} | ||
return floatStrToIntStr(sc, floatPrefix, str) | ||
return valid, nil | ||
} | ||
|
||
// roundIntStr is to round int string base on the number following dot. | ||
|
@@ -625,7 +649,7 @@ func getValidFloatPrefix(sc *stmtctx.StatementContext, s string) (valid string, | |
valid = "0" | ||
} | ||
if validLen == 0 || validLen != len(s) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if len(str) == 0, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked some behavior in MySQL, and get the following result
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so in select and update context, |
||
err = errors.Trace(handleTruncateError(sc)) | ||
err = errors.Trace(handleTruncateError(sc, ErrTruncated)) | ||
} | ||
return valid, err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we return a warning?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning has been added in truncated error handler