Skip to content
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

Return null for overflow when casting string to integer under safe option enabled #5398

Merged
merged 6 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions arrow-cast/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4911,6 +4911,25 @@ mod tests {
assert!(c.is_null(2));
}

#[test]
fn test_cast_string_to_integral_overflow() {
let str = Arc::new(StringArray::from(vec![
Some("123"),
Some("-123"),
Some("86374"),
None,
])) as ArrayRef;

let options = CastOptions {
safe: true,
format_options: FormatOptions::default(),
};
let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
let expected =
Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
assert_eq!(&res, &expected);
}

#[test]
fn test_cast_string_to_timestamp() {
let a1 = Arc::new(StringArray::from(vec![
Expand Down
2 changes: 1 addition & 1 deletion arrow-cast/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ macro_rules! parser_primitive {
($t:ty) => {
impl Parser for $t {
fn parse(string: &str) -> Option<Self::Native> {
lexical_core::parse::<Self::Native>(string.as_bytes()).ok()
string.parse::<Self::Native>().ok()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only touched integer parser.

Copy link
Member Author

@viirya viirya Feb 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if floating parser also has same issue. I'm not sure about if there is floating type overflow behavior.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can wait for a fix at the upstream crate. But the ticket is open for more than 6 months, and no progress so far. I think we may need to fix here directly.

}
}
};
Expand Down
Loading