-
Notifications
You must be signed in to change notification settings - Fork 1.2k
chore(arrow-cast): support suffix white space in arrow-cast parse #10396
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
Merged
alamb
merged 11 commits into
apache:main
from
Rich-T-kid:rich-T-kid/support-suffix-white-space
Jul 25, 2026
+66
−27
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
04b9405
support suffix white space in parse
9038baf
replace buffer look up comparission with two boolean comps
534f92e
exist early for empty cases
750fe85
re-introduce check
d8a346d
leave fast path untouched, add prefix/suffix trim on failure only
Rich-T-kid 483b595
remove comment
Rich-T-kid 769a124
uncomment test
Rich-T-kid ec198ad
fix clippy
Rich-T-kid 5c1d048
add changes for f64
Rich-T-kid 37a66ef
Merge branch 'main' into rich-T-kid/support-suffix-white-space
Jefffrey 2894b35
Merge branch 'main' into rich-T-kid/support-suffix-white-space
alamb 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 hidden or 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 |
|---|---|---|
|
|
@@ -445,7 +445,10 @@ pub trait Parser: ArrowPrimitiveType { | |
|
|
||
| impl Parser for Float16Type { | ||
| fn parse(string: &str) -> Option<f16> { | ||
| let string = truncate_white_space(string); | ||
| if let Ok(raw_float) = lexical_core::parse(string.as_bytes()) { | ||
| return Some(f16::from_f32(raw_float)); | ||
| } | ||
| let string = trim_pre_and_post_whitespace(string); | ||
| lexical_core::parse(string.as_bytes()) | ||
| .ok() | ||
| .map(f16::from_f32) | ||
|
|
@@ -454,49 +457,59 @@ impl Parser for Float16Type { | |
|
|
||
| impl Parser for Float32Type { | ||
| fn parse(string: &str) -> Option<f32> { | ||
| let string = truncate_white_space(string); | ||
| if let Ok(raw_float) = lexical_core::parse(string.as_bytes()) { | ||
| return Some(raw_float); | ||
| } | ||
| let string = trim_pre_and_post_whitespace(string); | ||
| lexical_core::parse(string.as_bytes()).ok() | ||
| } | ||
| } | ||
|
|
||
| impl Parser for Float64Type { | ||
| fn parse(string: &str) -> Option<f64> { | ||
| let string = truncate_white_space(string); | ||
| if let Ok(raw_float) = lexical_core::parse(string.as_bytes()) { | ||
| return Some(raw_float); | ||
| } | ||
| let string = trim_pre_and_post_whitespace(string); | ||
|
Rich-T-kid marked this conversation as resolved.
|
||
| lexical_core::parse(string.as_bytes()).ok() | ||
| } | ||
| } | ||
|
|
||
| /// this is a no-op if the string starts and ends with a digit, otherwise it will trim whitespace from the start and end of the string. | ||
| #[inline] | ||
| fn truncate_white_space(string: &str) -> &str { | ||
| if string | ||
| .as_bytes() | ||
| .first() | ||
| .is_some_and(|first_byte| !first_byte.is_ascii_digit()) | ||
| { | ||
| string.trim_ascii_start() | ||
| } else { | ||
| string | ||
| fn trim_pre_and_post_whitespace(string: &str) -> &str { | ||
| let bytes = string.as_bytes(); | ||
| let prefix = bytes.first().is_some_and(|b| !b.is_ascii_digit()); | ||
| let suffix = bytes.last().is_some_and(|b| !b.is_ascii_digit()); | ||
| match (prefix, suffix) { | ||
| (false, false) => string, | ||
| (true, false) => string.trim_ascii_start(), | ||
| (false, true) => string.trim_ascii_end(), | ||
| (true, true) => string.trim_ascii(), | ||
| } | ||
| } | ||
|
|
||
| macro_rules! parser_primitive { | ||
| ($t:ty) => { | ||
| impl Parser for $t { | ||
| fn parse(mut string: &str) -> Option<Self::Native> { | ||
| if !string.as_bytes().last().is_some_and(|x| x.is_ascii_digit()) { | ||
| return None; | ||
| fn parse(string: &str) -> Option<Self::Native> { | ||
|
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. perhaps we can add one benchmark for i32 just so we have a reference
Contributor
Author
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. added those here #10421 |
||
| let mut raw_bytes = string.as_bytes(); | ||
| if !raw_bytes.last().is_some_and(|x| x.is_ascii_digit()) { | ||
| raw_bytes = raw_bytes.trim_ascii_end(); | ||
| if !raw_bytes.last().is_some_and(|x| x.is_ascii_digit()) { | ||
| return None; | ||
| } | ||
| } | ||
| if string | ||
| .as_bytes() | ||
| .first() | ||
| .is_some_and(|first| !first.is_ascii_digit()) | ||
| { | ||
| string = string.trim_ascii_start(); | ||
| }; | ||
| match atoi::FromRadix10SignedChecked::from_radix_10_signed_checked( | ||
| string.as_bytes(), | ||
| ) { | ||
| (Some(n), x) if x == string.len() => Some(n), | ||
| _ => None, | ||
| match atoi::FromRadix10SignedChecked::from_radix_10_signed_checked(raw_bytes) { | ||
| (Some(n), x) if x == raw_bytes.len() => Some(n), | ||
| _ => { | ||
| let trimmed = raw_bytes.trim_ascii_start(); | ||
| match atoi::FromRadix10SignedChecked::from_radix_10_signed_checked(trimmed) | ||
| { | ||
| (Some(n), x) if x == trimmed.len() => Some(n), | ||
| _ => None, | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -2913,5 +2926,31 @@ mod tests { | |
| assert_eq!(Int32Type::parse("\t\n\t\n\n\n\t1"), Some(1)); | ||
| assert_eq!(Int32Type::parse(" \n-25"), Some(-25)); | ||
| assert_eq!(Int32Type::parse("\t-800"), Some(-800)); | ||
|
|
||
| // suffix whitespace | ||
| assert_eq!(Float64Type::parse("1.5 "), Some(1.5)); | ||
| assert_eq!(Float64Type::parse("40.5123\n"), Some(40.5123)); | ||
| assert_eq!(Float64Type::parse("40.5123\n\t\n\t\n"), Some(40.5123)); | ||
| assert_eq!(Float64Type::parse("-942.5423\t"), Some(-942.5423)); | ||
| assert_eq!(Int32Type::parse("3 "), Some(3)); | ||
| assert_eq!(Int32Type::parse("30 "), Some(30)); | ||
| assert_eq!(Int32Type::parse("-25 \n"), Some(-25)); | ||
| assert_eq!(Int32Type::parse("800\t"), Some(800)); | ||
| // whitespace on both sides | ||
| assert_eq!(Float64Type::parse(" 1.5 "), Some(1.5)); | ||
| assert_eq!(Float64Type::parse("\t\n 20.54 \t"), Some(20.54)); | ||
| assert_eq!(Float64Type::parse("\n-942.5423\n"), Some(-942.5423)); | ||
| assert_eq!(Int32Type::parse(" 3 "), Some(3)); | ||
| assert_eq!(Int32Type::parse("\n \n 100 \n"), Some(100)); | ||
| assert_eq!(Int32Type::parse("\t-800\t\n"), Some(-800)); | ||
|
|
||
| // trailing non-whitespace chars should not parse | ||
| assert_eq!(Float64Type::parse("1.5abc"), None); | ||
| assert_eq!(Float64Type::parse("40.5123x"), None); | ||
| assert_eq!(Int32Type::parse("30x"), None); | ||
| assert_eq!(Int32Type::parse("100px"), None); | ||
| assert_eq!(Int32Type::parse("-25!"), None); | ||
| assert_eq!(Int32Type::parse("3j"), None); | ||
| assert_eq!(Int32Type::parse("3"), Some(3)); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
this is a good strategy -- try the common/ fast path first and then fallback to checking whitespace afterwards