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

feat: remove lexical (replace with atoi_simd, ryu, and itao). #12512

Merged
merged 2 commits into from
Nov 16, 2023
Merged
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
25 changes: 12 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ hashbrown = { version = "0.14", features = ["rayon", "ahash"] }
hex = "0.4.3"
indexmap = { version = "2", features = ["std"] }
itoa = "1.0.6"
lexical-core = "0.8.5"
atoi_simd = "0.15"
fast-float = { version = "0.2" }
memchr = "2.6"
multiversion = "0.7"
ndarray = { version = "0.15", default-features = false }
Expand Down
7 changes: 5 additions & 2 deletions crates/polars-arrow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ simdutf8 = { workspace = true }
ethnum = { workspace = true }

# To efficiently cast numbers to strings
lexical-core = { workspace = true, optional = true }
atoi_simd = { workspace = true, optional = true }
fast-float = { workspace = true, optional = true }
itoa = { workspace = true, optional = true }
ryu = { workspace = true, optional = true }

regex = { workspace = true, optional = true }
regex-syntax = { version = "0.8", optional = true }
Expand Down Expand Up @@ -134,7 +137,7 @@ compute_arithmetics = ["strength_reduce", "compute_arithmetics_decimal"]
compute_bitwise = []
compute_boolean = []
compute_boolean_kleene = []
compute_cast = ["lexical-core", "compute_take"]
compute_cast = ["compute_take", "ryu", "atoi_simd", "itoa", "fast-float"]
compute_comparison = ["compute_take", "compute_boolean"]
compute_concatenate = []
compute_filter = []
Expand Down
68 changes: 46 additions & 22 deletions crates/polars-arrow/src/compute/cast/binary_to.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,47 @@ use crate::datatypes::ArrowDataType;
use crate::offset::{Offset, Offsets};
use crate::types::NativeType;

pub(super) trait Parse {
fn parse(val: &[u8]) -> Option<Self>
where
Self: Sized;
}

macro_rules! impl_parse {
($primitive_type:ident) => {
impl Parse for $primitive_type {
fn parse(val: &[u8]) -> Option<Self> {
atoi_simd::parse(val).ok()
}
}
};
}
impl_parse!(i8);
impl_parse!(i16);
impl_parse!(i32);
impl_parse!(i64);
impl_parse!(u8);
impl_parse!(u16);
impl_parse!(u32);
impl_parse!(u64);

impl Parse for f32 {
fn parse(val: &[u8]) -> Option<Self>
where
Self: Sized,
{
fast_float::parse(val).ok()
}
}
impl Parse for f64 {
fn parse(val: &[u8]) -> Option<Self>
where
Self: Sized,
{
fast_float::parse(val).ok()
}
}

/// Conversion of binary
pub fn binary_to_large_binary(
from: &BinaryArray<i32>,
Expand Down Expand Up @@ -61,32 +102,15 @@ pub fn binary_to_large_utf8(
Utf8Array::<i64>::try_new(to_data_type, offsets, values, from.validity().cloned())
}

/// Casts a [`BinaryArray`] to a [`PrimitiveArray`] at best-effort using `lexical_core::parse_partial`, making any uncastable value as zero.
pub fn partial_binary_to_primitive<O: Offset, T>(
from: &BinaryArray<O>,
to: &ArrowDataType,
) -> PrimitiveArray<T>
where
T: NativeType + lexical_core::FromLexical,
{
let iter = from
.iter()
.map(|x| x.and_then::<T, _>(|x| lexical_core::parse_partial(x).ok().map(|x| x.0)));

PrimitiveArray::<T>::from_trusted_len_iter(iter).to(to.clone())
}

/// Casts a [`BinaryArray`] to a [`PrimitiveArray`], making any uncastable value a Null.
pub fn binary_to_primitive<O: Offset, T>(
pub(super) fn binary_to_primitive<O: Offset, T>(
from: &BinaryArray<O>,
to: &ArrowDataType,
) -> PrimitiveArray<T>
where
T: NativeType + lexical_core::FromLexical,
T: NativeType + Parse,
{
let iter = from
.iter()
.map(|x| x.and_then::<T, _>(|x| lexical_core::parse(x).ok()));
let iter = from.iter().map(|x| x.and_then::<T, _>(|x| T::parse(x)));

PrimitiveArray::<T>::from_trusted_len_iter(iter).to(to.clone())
}
Expand All @@ -97,11 +121,11 @@ pub(super) fn binary_to_primitive_dyn<O: Offset, T>(
options: CastOptions,
) -> PolarsResult<Box<dyn Array>>
where
T: NativeType + lexical_core::FromLexical,
T: NativeType + Parse,
{
let from = from.as_any().downcast_ref().unwrap();
if options.partial {
Ok(Box::new(partial_binary_to_primitive::<O, T>(from, to)))
unimplemented!()
} else {
Ok(Box::new(binary_to_primitive::<O, T>(from, to)))
}
Expand Down
35 changes: 7 additions & 28 deletions crates/polars-arrow/src/compute/cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,42 +570,21 @@ pub fn cast(
},

(Utf8, _) => match to_type {
UInt8 => utf8_to_primitive_dyn::<i32, u8>(array, to_type, options),
Copy link
Member Author

Choose a reason for hiding this comment

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

Remove branches we never hit in polars.

UInt16 => utf8_to_primitive_dyn::<i32, u16>(array, to_type, options),
UInt32 => utf8_to_primitive_dyn::<i32, u32>(array, to_type, options),
UInt64 => utf8_to_primitive_dyn::<i32, u64>(array, to_type, options),
Int8 => utf8_to_primitive_dyn::<i32, i8>(array, to_type, options),
Int16 => utf8_to_primitive_dyn::<i32, i16>(array, to_type, options),
Int32 => utf8_to_primitive_dyn::<i32, i32>(array, to_type, options),
Int64 => utf8_to_primitive_dyn::<i32, i64>(array, to_type, options),
Float32 => utf8_to_primitive_dyn::<i32, f32>(array, to_type, options),
Float64 => utf8_to_primitive_dyn::<i32, f64>(array, to_type, options),
Date32 => utf8_to_date32_dyn::<i32>(array),
Date64 => utf8_to_date64_dyn::<i32>(array),
LargeUtf8 => Ok(Box::new(utf8_to_large_utf8(
array.as_any().downcast_ref().unwrap(),
))),
Timestamp(time_unit, None) => {
utf8_to_naive_timestamp_dyn::<i32>(array, time_unit.to_owned())
},
Timestamp(time_unit, Some(time_zone)) => {
utf8_to_timestamp_dyn::<i32>(array, time_zone.clone(), time_unit.to_owned())
},
_ => polars_bail!(InvalidOperation:
"casting from {from_type:?} to {to_type:?} not supported",
),
},
(LargeUtf8, _) => match to_type {
UInt8 => utf8_to_primitive_dyn::<i64, u8>(array, to_type, options),
Copy link
Member Author

Choose a reason for hiding this comment

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

Reuse the binary code for this.

UInt16 => utf8_to_primitive_dyn::<i64, u16>(array, to_type, options),
UInt32 => utf8_to_primitive_dyn::<i64, u32>(array, to_type, options),
UInt64 => utf8_to_primitive_dyn::<i64, u64>(array, to_type, options),
Int8 => utf8_to_primitive_dyn::<i64, i8>(array, to_type, options),
Int16 => utf8_to_primitive_dyn::<i64, i16>(array, to_type, options),
Int32 => utf8_to_primitive_dyn::<i64, i32>(array, to_type, options),
Int64 => utf8_to_primitive_dyn::<i64, i64>(array, to_type, options),
Float32 => utf8_to_primitive_dyn::<i64, f32>(array, to_type, options),
Float64 => utf8_to_primitive_dyn::<i64, f64>(array, to_type, options),
UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float32 | Float64 => {
let binary = utf8_to_binary::<i64>(
array.as_any().downcast_ref().unwrap(),
ArrowDataType::LargeBinary,
);
cast(&binary, to_type, options)
},
Date32 => utf8_to_date32_dyn::<i64>(array),
Date64 => utf8_to_date64_dyn::<i64>(array),
Utf8 => utf8_large_to_utf8(array.as_any().downcast_ref().unwrap()).map(|x| x.boxed()),
Expand Down
Loading