-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[function](cast)Make string casting to integers more like MySQL's behavior #38847
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -243,6 +243,20 @@ class StringParser { | |
| return true; | ||
| } | ||
|
|
||
| // For strings like "3.0", "3.123", and "3.", can parse them as 3. | ||
| static inline bool is_float_suffix(const char* __restrict s, int len) { | ||
| return (s[0] == '.' && is_all_digit(s + 1, len - 1)); | ||
| } | ||
|
|
||
| static inline bool is_all_digit(const char* __restrict s, int len) { | ||
| for (int i = 0; i < len; ++i) { | ||
| if (!LIKELY(s[i] >= '0' && s[i] <= '9')) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| // Returns the position of the first non-whitespace character in s. | ||
| static inline int skip_leading_whitespace(const char* __restrict s, int len) { | ||
| int i = 0; | ||
|
|
@@ -306,7 +320,8 @@ T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseR | |
| } | ||
| val = val * 10 + digit; | ||
| } else { | ||
| if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { | ||
| if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) && | ||
|
Contributor
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. warning: boolean expression can be simplified by DeMorgan's theorem [readability-simplify-boolean-expr] if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
^Additional contextbe/src/common/compiler_util.h:35: expanded from macro 'UNLIKELY' #define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
^ |
||
| !is_float_suffix(s + i, len - i))))) { | ||
| // Reject the string because either the first char was not a digit, | ||
| // or the remaining chars are not all whitespace | ||
| *result = PARSE_FAILURE; | ||
|
|
@@ -448,7 +463,8 @@ T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, Par | |
| T digit = s[i] - '0'; | ||
| val = val * 10 + digit; | ||
| } else { | ||
| if ((UNLIKELY(!is_all_whitespace(s + i, len - i)))) { | ||
| if ((UNLIKELY(!is_all_whitespace(s + i, len - i) && | ||
|
Contributor
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. warning: boolean expression can be simplified by DeMorgan's theorem [readability-simplify-boolean-expr] if ((UNLIKELY(!is_all_whitespace(s + i, len - i) &&
^Additional contextbe/src/common/compiler_util.h:35: expanded from macro 'UNLIKELY' #define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
^ |
||
| !is_float_suffix(s + i, len - i)))) { | ||
| *result = PARSE_FAILURE; | ||
| return 0; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,3 +98,9 @@ | |
| -- !test -- | ||
| a | ||
|
|
||
| -- !cast_string_to_int -- | ||
| 3 3 0 0 3 \N 3 | ||
|
|
||
| -- !cast_string_to_int -- | ||
| 3 3 0 0 3 \N 3 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,11 +30,11 @@ true | |
| \N | ||
|
|
||
| -- !sql_to_small -- | ||
| \N | ||
| 1212 | ||
|
|
||
| -- !sql_to_int -- | ||
| \N | ||
| 1212 | ||
|
|
||
| -- !sql_to_big -- | ||
| \N | ||
| 1212 | ||
|
|
||
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: boolean expression can be simplified by DeMorgan's theorem [readability-simplify-boolean-expr]
Additional context
be/src/common/compiler_util.h:34: expanded from macro 'LIKELY'