-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: ritchie <[email protected]>
- Loading branch information
Showing
12 changed files
with
366 additions
and
2 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
crates/polars-ops/src/chunked_array/binary/cast_binary_to_numerical.rs
This file contains 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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use arrow::array::{Array, BinaryViewArray, PrimitiveArray}; | ||
use arrow::datatypes::ArrowDataType; | ||
use arrow::types::NativeType; | ||
use polars_error::PolarsResult; | ||
|
||
/// Trait for casting bytes to a primitive type | ||
pub trait Cast { | ||
fn cast_le(val: &[u8]) -> Option<Self> | ||
where | ||
Self: Sized; | ||
fn cast_be(val: &[u8]) -> Option<Self> | ||
where | ||
Self: Sized; | ||
} | ||
macro_rules! impl_cast { | ||
($primitive_type:ident) => { | ||
impl Cast for $primitive_type { | ||
fn cast_le(val: &[u8]) -> Option<Self> { | ||
Some($primitive_type::from_le_bytes(val.try_into().ok()?)) | ||
} | ||
|
||
fn cast_be(val: &[u8]) -> Option<Self> { | ||
Some($primitive_type::from_be_bytes(val.try_into().ok()?)) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_cast!(i8); | ||
impl_cast!(i16); | ||
impl_cast!(i32); | ||
impl_cast!(i64); | ||
impl_cast!(i128); | ||
impl_cast!(u8); | ||
impl_cast!(u16); | ||
impl_cast!(u32); | ||
impl_cast!(u64); | ||
impl_cast!(u128); | ||
impl_cast!(f32); | ||
impl_cast!(f64); | ||
|
||
/// Casts a [`BinaryArray`] to a [`PrimitiveArray`], making any uncastable value a Null. | ||
pub(super) fn cast_binview_to_primitive<T>( | ||
from: &BinaryViewArray, | ||
to: &ArrowDataType, | ||
is_little_endian: bool, | ||
) -> PrimitiveArray<T> | ||
where | ||
T: Cast + NativeType, | ||
{ | ||
let iter = from.iter().map(|x| { | ||
x.and_then::<T, _>(|x| { | ||
if is_little_endian { | ||
T::cast_le(x) | ||
} else { | ||
T::cast_be(x) | ||
} | ||
}) | ||
}); | ||
|
||
PrimitiveArray::<T>::from_trusted_len_iter(iter).to(to.clone()) | ||
} | ||
|
||
/// Casts a [`BinaryArray`] to a [`PrimitiveArray`], making any uncastable value a Null. | ||
pub(super) fn cast_binview_to_primitive_dyn<T>( | ||
from: &dyn Array, | ||
to: &ArrowDataType, | ||
is_little_endian: bool, | ||
) -> PolarsResult<Box<dyn Array>> | ||
where | ||
T: Cast + NativeType, | ||
{ | ||
let from = from.as_any().downcast_ref().unwrap(); | ||
|
||
Ok(Box::new(cast_binview_to_primitive::<T>( | ||
from, | ||
to, | ||
is_little_endian, | ||
))) | ||
} |
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod cast_binary_to_numerical; | ||
mod namespace; | ||
|
||
pub use namespace::*; | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.