Skip to content

Commit

Permalink
feat: Expose overflowing cast (pola-rs#16805)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored and Wouittone committed Jun 22, 2024
1 parent 15c57ca commit e1f20ee
Show file tree
Hide file tree
Showing 70 changed files with 570 additions and 273 deletions.
4 changes: 2 additions & 2 deletions crates/polars-arrow/src/compute/cast/binary_to.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use polars_error::PolarsResult;

use super::CastOptions;
use super::CastOptionsImpl;
use crate::array::*;
use crate::datatypes::ArrowDataType;
use crate::offset::{Offset, Offsets};
Expand Down Expand Up @@ -118,7 +118,7 @@ where
pub(super) fn binary_to_primitive_dyn<O: Offset, T>(
from: &dyn Array,
to: &ArrowDataType,
options: CastOptions,
options: CastOptionsImpl,
) -> PolarsResult<Box<dyn Array>>
where
T: NativeType + Parse,
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-arrow/src/compute/cast/binview_to.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use polars_error::PolarsResult;

use crate::array::*;
use crate::compute::cast::binary_to::Parse;
use crate::compute::cast::CastOptions;
use crate::compute::cast::CastOptionsImpl;
#[cfg(feature = "dtype-decimal")]
use crate::compute::decimal::deserialize_decimal;
use crate::datatypes::{ArrowDataType, TimeUnit};
Expand Down Expand Up @@ -77,7 +77,7 @@ where
pub(super) fn binview_to_primitive_dyn<T>(
from: &dyn Array,
to: &ArrowDataType,
options: CastOptions,
options: CastOptionsImpl,
) -> PolarsResult<Box<dyn Array>>
where
T: NativeType + Parse,
Expand Down
8 changes: 4 additions & 4 deletions crates/polars-arrow/src/compute/cast/dictionary_to.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use polars_error::{polars_bail, PolarsResult};

use super::{primitive_as_primitive, primitive_to_primitive, CastOptions};
use super::{primitive_as_primitive, primitive_to_primitive, CastOptionsImpl};
use crate::array::{Array, DictionaryArray, DictionaryKey};
use crate::compute::cast::cast;
use crate::datatypes::ArrowDataType;
Expand Down Expand Up @@ -35,7 +35,7 @@ pub fn dictionary_to_dictionary_values<K: DictionaryKey>(
let values = from.values();
let length = values.len();

let values = cast(values.as_ref(), values_type, CastOptions::default())?;
let values = cast(values.as_ref(), values_type, CastOptionsImpl::default())?;

assert_eq!(values.len(), length); // this is guaranteed by `cast`
unsafe {
Expand All @@ -55,7 +55,7 @@ pub fn wrapping_dictionary_to_dictionary_values<K: DictionaryKey>(
let values = cast(
values.as_ref(),
values_type,
CastOptions {
CastOptionsImpl {
wrapped: true,
partial: false,
},
Expand Down Expand Up @@ -127,7 +127,7 @@ where
pub(super) fn dictionary_cast_dyn<K: DictionaryKey + num_traits::NumCast>(
array: &dyn Array,
to_type: &ArrowDataType,
options: CastOptions,
options: CastOptionsImpl,
) -> PolarsResult<Box<dyn Array>> {
let array = array.as_any().downcast_ref::<DictionaryArray<K>>().unwrap();
let keys = array.keys();
Expand Down
20 changes: 10 additions & 10 deletions crates/polars-arrow/src/compute/cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::temporal_conversions::utf8view_to_timestamp;

/// options defining how Cast kernels behave
#[derive(Clone, Copy, Debug, Default)]
pub struct CastOptions {
pub struct CastOptionsImpl {
/// default to false
/// whether an overflowing cast should be converted to `None` (default), or be wrapped (i.e. `256i16 as u8 = 0` vectorized).
/// Settings this to `true` is 5-6x faster for numeric types.
Expand All @@ -43,7 +43,7 @@ pub struct CastOptions {
pub partial: bool,
}

impl CastOptions {
impl CastOptionsImpl {
pub fn unchecked() -> Self {
Self {
wrapped: true,
Expand All @@ -52,7 +52,7 @@ impl CastOptions {
}
}

impl CastOptions {
impl CastOptionsImpl {
fn with_wrapped(&self, v: bool) -> Self {
let mut option = *self;
option.wrapped = v;
Expand Down Expand Up @@ -82,7 +82,7 @@ macro_rules! primitive_dyn {
fn cast_struct(
array: &StructArray,
to_type: &ArrowDataType,
options: CastOptions,
options: CastOptionsImpl,
) -> PolarsResult<StructArray> {
let values = array.values();
let fields = StructArray::get_fields(to_type);
Expand All @@ -102,7 +102,7 @@ fn cast_struct(
fn cast_list<O: Offset>(
array: &ListArray<O>,
to_type: &ArrowDataType,
options: CastOptions,
options: CastOptionsImpl,
) -> PolarsResult<ListArray<O>> {
let values = array.values();
let new_values = cast(
Expand Down Expand Up @@ -144,7 +144,7 @@ fn cast_large_to_list(array: &ListArray<i64>, to_type: &ArrowDataType) -> ListAr
fn cast_fixed_size_list_to_list<O: Offset>(
fixed: &FixedSizeListArray,
to_type: &ArrowDataType,
options: CastOptions,
options: CastOptionsImpl,
) -> PolarsResult<ListArray<O>> {
let new_values = cast(
fixed.values().as_ref(),
Expand All @@ -170,7 +170,7 @@ fn cast_list_to_fixed_size_list<O: Offset>(
list: &ListArray<O>,
inner: &Field,
size: usize,
options: CastOptions,
options: CastOptionsImpl,
) -> PolarsResult<FixedSizeListArray> {
let null_cnt = list.null_count();
let new_values = if null_cnt == 0 {
Expand Down Expand Up @@ -245,7 +245,7 @@ pub fn cast_default(array: &dyn Array, to_type: &ArrowDataType) -> PolarsResult<
}

pub fn cast_unchecked(array: &dyn Array, to_type: &ArrowDataType) -> PolarsResult<Box<dyn Array>> {
cast(array, to_type, CastOptions::unchecked())
cast(array, to_type, CastOptionsImpl::unchecked())
}

/// Cast `array` to the provided data type and return a new [`Array`] with
Expand Down Expand Up @@ -276,7 +276,7 @@ pub fn cast_unchecked(array: &dyn Array, to_type: &ArrowDataType) -> PolarsResul
pub fn cast(
array: &dyn Array,
to_type: &ArrowDataType,
options: CastOptions,
options: CastOptionsImpl,
) -> PolarsResult<Box<dyn Array>> {
use ArrowDataType::*;
let from_type = array.data_type();
Expand Down Expand Up @@ -760,7 +760,7 @@ pub fn cast(
fn cast_to_dictionary<K: DictionaryKey>(
array: &dyn Array,
dict_value_type: &ArrowDataType,
options: CastOptions,
options: CastOptionsImpl,
) -> PolarsResult<Box<dyn Array>> {
let array = cast(array, dict_value_type, options)?;
let array = array.as_ref();
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-arrow/src/compute/cast/primitive_to.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::hash::Hash;
use num_traits::{AsPrimitive, Float, ToPrimitive};
use polars_error::PolarsResult;

use super::CastOptions;
use super::CastOptionsImpl;
use crate::array::*;
use crate::bitmap::Bitmap;
use crate::compute::arity::unary;
Expand Down Expand Up @@ -142,7 +142,7 @@ where
pub(super) fn primitive_to_primitive_dyn<I, O>(
from: &dyn Array,
to_type: &ArrowDataType,
options: CastOptions,
options: CastOptionsImpl,
) -> PolarsResult<Box<dyn Array>>
where
I: NativeType + num_traits::NumCast + num_traits::AsPrimitive<O>,
Expand Down
Loading

0 comments on commit e1f20ee

Please sign in to comment.