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

Replace usage of lexical-core crate with libcore for parsing strings as floats #4785

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
10 changes: 4 additions & 6 deletions arrow-cast/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,29 +433,27 @@ pub trait Parser: ArrowPrimitiveType {

impl Parser for Float16Type {
fn parse(string: &str) -> Option<f16> {
lexical_core::parse(string.as_bytes())
.ok()
.map(f16::from_f32)
string.parse::<f16>().ok()
}
}

impl Parser for Float32Type {
fn parse(string: &str) -> Option<f32> {
lexical_core::parse(string.as_bytes()).ok()
string.parse::<f32>().ok()
}
}

impl Parser for Float64Type {
fn parse(string: &str) -> Option<f64> {
lexical_core::parse(string.as_bytes()).ok()
string.parse::<f64>().ok()
}
}

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()
}
}
};
Expand Down
27 changes: 21 additions & 6 deletions arrow-json/src/reader/primitive_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@ macro_rules! primitive_parse {
($($t:ty),+) => {
$(impl ParseJsonNumber for $t {
fn parse(s: &[u8]) -> Option<Self> {
match lexical_core::parse::<Self>(s) {
Ok(f) => Some(f),
Err(_) => lexical_core::parse::<f64>(s).ok().and_then(NumCast::from),
match std::str::from_utf8(s) {
Ok(s) => {
if let Ok(res) = s.parse::<$t>() {
Some(res)
} else {
return s.parse::<f64>().ok().and_then(NumCast::from)
}
}
Err(_) => None,
}
}
})+
Expand All @@ -57,19 +63,28 @@ primitive_parse!(i8, i16, i32, i64, u8, u16, u32, u64);

impl ParseJsonNumber for f16 {
fn parse(s: &[u8]) -> Option<Self> {
lexical_core::parse::<f32>(s).ok().map(f16::from_f32)
match std::str::from_utf8(s) {
Ok(s) => s.parse::<f16>().ok(),
Err(_) => None,
}
}
}

impl ParseJsonNumber for f32 {
fn parse(s: &[u8]) -> Option<Self> {
lexical_core::parse::<Self>(s).ok()
match std::str::from_utf8(s) {
Ok(s) => s.parse::<f32>().ok(),
Err(_) => None,
}
}
}

impl ParseJsonNumber for f64 {
fn parse(s: &[u8]) -> Option<Self> {
lexical_core::parse::<Self>(s).ok()
match std::str::from_utf8(s) {
Ok(s) => s.parse::<f64>().ok(),
Err(_) => None,
}
}
}

Expand Down
7 changes: 3 additions & 4 deletions arrow-json/src/reader/timestamp_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,15 @@ where
}
TapeElement::Number(idx) => {
let s = tape.get_string(idx);
let b = s.as_bytes();
let value = lexical_core::parse::<i64>(b)
.or_else(|_| lexical_core::parse::<f64>(b).map(|x| x as i64))
let value = s
.parse::<i64>()
.or_else(|_| s.parse::<f64>().map(|x| x as i64))
.map_err(|_| {
ArrowError::JsonError(format!(
"failed to parse {s} as {}",
self.data_type
))
})?;

builder.append_value(value)
}
_ => return Err(tape.error(*p, "primitive")),
Expand Down
Loading