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

Support casting from String to Decimal #3281

Merged
merged 6 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
15 changes: 14 additions & 1 deletion arrow-buffer/src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// under the License.

use num::cast::AsPrimitive;
use num::{BigInt, FromPrimitive, ToPrimitive};
use num::{BigInt, FromPrimitive, Num, ToPrimitive};
use std::cmp::Ordering;

/// A signed 256-bit integer
Expand Down Expand Up @@ -102,6 +102,19 @@ impl i256 {
Self::from_parts(v as u128, v >> 127)
}

/// Create an integer value from its representation as string.
#[inline]
pub fn from_string(value_str: &str) -> Option<Self> {
let numbers = BigInt::from_str_radix(value_str, 10).ok()?;
let (integer, overflow) = Self::from_bigint_with_overflow(numbers);

if overflow {
None
} else {
Some(integer)
}
}

/// Create an optional i256 from the provided `f64`. Returning `None`
/// if overflow occurred
pub fn from_f64(v: f64) -> Option<Self> {
Expand Down
Loading