-
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
type: fix float over flow when converting a decimal to a float and then converting a float to an uint #10405
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
19e7abe
fix
qw4990 8e7630e
add UT
qw4990 3697382
update comments
qw4990 d8fe6ec
fix
qw4990 8d21e2d
handle scientific notation
qw4990 67a46c8
refmt
qw4990 1993ff6
refmt
qw4990 c57c855
fix comments
qw4990 b909e90
update error
qw4990 842e369
Merge branch 'master' into fix-10181
qw4990 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,6 +173,99 @@ func ConvertFloatToUint(sc *stmtctx.StatementContext, fval float64, upperBound u | |
return uint64(val), nil | ||
} | ||
|
||
// convertScientificNotation converts a decimal string with scientific notation to a normal decimal string. | ||
// 1E6 => 1000000, .12345E+5 => 12345 | ||
func convertScientificNotation(str string) (string, error) { | ||
// https://golang.org/ref/spec#Floating-point_literals | ||
eIdx := -1 | ||
point := -1 | ||
for i := 0; i < len(str); i++ { | ||
if str[i] == '.' { | ||
point = i | ||
} | ||
if str[i] == 'e' || str[i] == 'E' { | ||
eIdx = i | ||
if point == -1 { | ||
point = i | ||
} | ||
break | ||
} | ||
} | ||
if eIdx == -1 { | ||
return str, nil | ||
} | ||
exp, err := strconv.ParseInt(str[eIdx+1:], 10, 64) | ||
if err != nil { | ||
return "", errors.WithStack(err) | ||
} | ||
|
||
f := str[:eIdx] | ||
if exp == 0 { | ||
return f, nil | ||
} else if exp > 0 { // move point right | ||
if point+int(exp) == len(f)-1 { // 123.456 >> 3 = 123456. = 123456 | ||
return f[:point] + f[point+1:], nil | ||
} else if point+int(exp) < len(f)-1 { // 123.456 >> 2 = 12345.6 | ||
return f[:point] + f[point+1:point+1+int(exp)] + "." + f[point+1+int(exp):], nil | ||
} | ||
// 123.456 >> 5 = 12345600 | ||
return f[:point] + f[point+1:] + strings.Repeat("0", point+int(exp)-len(f)+1), nil | ||
} else { // move point left | ||
exp = -exp | ||
if int(exp) < point { // 123.456 << 2 = 1.23456 | ||
return f[:point-int(exp)] + "." + f[point-int(exp):point] + f[point+1:], nil | ||
} | ||
// 123.456 << 5 = 0.00123456 | ||
return "0." + strings.Repeat("0", int(exp)-point) + f[:point] + f[point+1:], nil | ||
} | ||
} | ||
|
||
func convertDecimalStrToUint(sc *stmtctx.StatementContext, str string, upperBound uint64, tp byte) (uint64, error) { | ||
str, err := convertScientificNotation(str) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
var intStr, fracStr string | ||
p := strings.Index(str, ".") | ||
if p == -1 { | ||
intStr = str | ||
} else { | ||
intStr = str[:p] | ||
fracStr = str[p+1:] | ||
} | ||
intStr = strings.TrimLeft(intStr, "0") | ||
if intStr == "" { | ||
intStr = "0" | ||
} | ||
if sc.ShouldClipToZero() && intStr[0] == '-' { | ||
return 0, overflow(str, tp) | ||
} | ||
|
||
var round uint64 | ||
if fracStr != "" && fracStr[0] >= '5' { | ||
round++ | ||
} | ||
|
||
upperBound -= round | ||
zz-jason marked this conversation as resolved.
Show resolved
Hide resolved
|
||
upperStr := strconv.FormatUint(upperBound, 10) | ||
if len(intStr) > len(upperStr) || | ||
(len(intStr) == len(upperStr) && intStr > upperStr) { | ||
return upperBound, overflow(str, tp) | ||
} | ||
|
||
val, err := strconv.ParseUint(intStr, 10, 64) | ||
if err != nil { | ||
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. return val, err |
||
return val, err | ||
} | ||
return val + round, nil | ||
} | ||
|
||
// ConvertDecimalToUint converts a decimal to a uint by converting it to a string first to avoid float overflow (#10181). | ||
func ConvertDecimalToUint(sc *stmtctx.StatementContext, d *MyDecimal, upperBound uint64, tp byte) (uint64, error) { | ||
return convertDecimalStrToUint(sc, string(d.ToString()), upperBound, tp) | ||
} | ||
|
||
// StrToInt converts a string to an integer at the best-effort. | ||
func StrToInt(sc *stmtctx.StatementContext, str string) (int64, error) { | ||
str = strings.TrimSpace(str) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Will
len(intStr) == 0
be true here ? If so, intStr[0] == '-' will cause a panic?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.
I assume that
len(intStr) > 0
here becauseMyDecimal
returns a valid decimal string without scientific notation.But I will let it support scientific notation for safety.