From 753363a5d0e0b89499627ba2eb1b56740509bbc1 Mon Sep 17 00:00:00 2001 From: Orson Peters Date: Wed, 16 Oct 2024 23:44:35 +0200 Subject: [PATCH] refactor(rust): Purge unused code --- .../polars-arrow/src/compute/aggregate/sum.rs | 15 +- crates/polars-arrow/src/compute/arity.rs | 213 ------------------ .../src/compute/cast/binary_to.rs | 13 -- .../src/compute/cast/dictionary_to.rs | 97 +------- crates/polars-arrow/src/compute/cast/mod.rs | 2 +- .../src/compute/cast/primitive_to.rs | 167 +------------- crates/polars-arrow/src/compute/temporal.rs | 89 -------- crates/polars-arrow/src/compute/utils.rs | 1 + .../development/contributing/code-style.md | 4 +- 9 files changed, 8 insertions(+), 593 deletions(-) diff --git a/crates/polars-arrow/src/compute/aggregate/sum.rs b/crates/polars-arrow/src/compute/aggregate/sum.rs index 9fbed5f8b1b6..e2098d969e03 100644 --- a/crates/polars-arrow/src/compute/aggregate/sum.rs +++ b/crates/polars-arrow/src/compute/aggregate/sum.rs @@ -6,7 +6,7 @@ use polars_error::PolarsResult; use crate::array::{Array, PrimitiveArray}; use crate::bitmap::utils::{BitChunkIterExact, BitChunksExact}; use crate::bitmap::Bitmap; -use crate::datatypes::{ArrowDataType, PhysicalType, PrimitiveType}; +use crate::datatypes::PhysicalType; use crate::scalar::*; use crate::types::simd::*; use crate::types::NativeType; @@ -102,19 +102,6 @@ where } } -/// Whether [`sum`] supports `dtype` -pub fn can_sum(dtype: &ArrowDataType) -> bool { - if let PhysicalType::Primitive(primitive) = dtype.to_physical_type() { - use PrimitiveType::*; - matches!( - primitive, - Int8 | Int16 | Int64 | Int128 | UInt8 | UInt16 | UInt32 | UInt64 | Float32 | Float64 - ) - } else { - false - } -} - /// Returns the sum of all elements in `array` as a [`Scalar`] of the same physical /// and logical types as `array`. /// # Error diff --git a/crates/polars-arrow/src/compute/arity.rs b/crates/polars-arrow/src/compute/arity.rs index c5b397f6faac..2670cfb4d031 100644 --- a/crates/polars-arrow/src/compute/arity.rs +++ b/crates/polars-arrow/src/compute/arity.rs @@ -1,10 +1,7 @@ //! Defines kernels suitable to perform operations to primitive arrays. -use polars_error::PolarsResult; - use super::utils::{check_same_len, combine_validities_and}; use crate::array::PrimitiveArray; -use crate::bitmap::{Bitmap, MutableBitmap}; use crate::datatypes::ArrowDataType; use crate::types::NativeType; @@ -29,104 +26,6 @@ where PrimitiveArray::::new(dtype, values.into(), array.validity().cloned()) } -/// Version of unary that checks for errors in the closure used to create the -/// buffer -pub fn try_unary( - array: &PrimitiveArray, - op: F, - dtype: ArrowDataType, -) -> PolarsResult> -where - I: NativeType, - O: NativeType, - F: Fn(I) -> PolarsResult, -{ - let values = array - .values() - .iter() - .map(|v| op(*v)) - .collect::>>()? - .into(); - - Ok(PrimitiveArray::::new( - dtype, - values, - array.validity().cloned(), - )) -} - -/// Version of unary that returns an array and bitmap. Used when working with -/// overflowing operations -pub fn unary_with_bitmap( - array: &PrimitiveArray, - op: F, - dtype: ArrowDataType, -) -> (PrimitiveArray, Bitmap) -where - I: NativeType, - O: NativeType, - F: Fn(I) -> (O, bool), -{ - let mut mut_bitmap = MutableBitmap::with_capacity(array.len()); - - let values = array - .values() - .iter() - .map(|v| { - let (res, over) = op(*v); - mut_bitmap.push(over); - res - }) - .collect::>() - .into(); - - ( - PrimitiveArray::::new(dtype, values, array.validity().cloned()), - mut_bitmap.into(), - ) -} - -/// Version of unary that creates a mutable bitmap that is used to keep track -/// of checked operations. The resulting bitmap is compared with the array -/// bitmap to create the final validity array. -pub fn unary_checked( - array: &PrimitiveArray, - op: F, - dtype: ArrowDataType, -) -> PrimitiveArray -where - I: NativeType, - O: NativeType, - F: Fn(I) -> Option, -{ - let mut mut_bitmap = MutableBitmap::with_capacity(array.len()); - - let values = array - .values() - .iter() - .map(|v| match op(*v) { - Some(val) => { - mut_bitmap.push(true); - val - }, - None => { - mut_bitmap.push(false); - O::default() - }, - }) - .collect::>() - .into(); - - // The validity has to be checked against the bitmap created during the - // creation of the values with the iterator. If an error was found during - // the iteration, then the validity is changed to None to mark the value - // as Null - let bitmap: Bitmap = mut_bitmap.into(); - let validity = combine_validities_and(array.validity(), Some(&bitmap)); - - PrimitiveArray::::new(dtype, values, validity) -} - /// Applies a binary operations to two primitive arrays. /// /// This is the fastest way to perform an operation on two primitive array when the benefits of a @@ -169,115 +68,3 @@ where PrimitiveArray::::new(dtype, values, validity) } - -/// Version of binary that checks for errors in the closure used to create the -/// buffer -pub fn try_binary( - lhs: &PrimitiveArray, - rhs: &PrimitiveArray, - dtype: ArrowDataType, - op: F, -) -> PolarsResult> -where - T: NativeType, - D: NativeType, - F: Fn(T, D) -> PolarsResult, -{ - check_same_len(lhs, rhs)?; - - let validity = combine_validities_and(lhs.validity(), rhs.validity()); - - let values = lhs - .values() - .iter() - .zip(rhs.values().iter()) - .map(|(l, r)| op(*l, *r)) - .collect::>>()? - .into(); - - Ok(PrimitiveArray::::new(dtype, values, validity)) -} - -/// Version of binary that returns an array and bitmap. Used when working with -/// overflowing operations -pub fn binary_with_bitmap( - lhs: &PrimitiveArray, - rhs: &PrimitiveArray, - dtype: ArrowDataType, - op: F, -) -> (PrimitiveArray, Bitmap) -where - T: NativeType, - D: NativeType, - F: Fn(T, D) -> (T, bool), -{ - check_same_len(lhs, rhs).unwrap(); - - let validity = combine_validities_and(lhs.validity(), rhs.validity()); - - let mut mut_bitmap = MutableBitmap::with_capacity(lhs.len()); - - let values = lhs - .values() - .iter() - .zip(rhs.values().iter()) - .map(|(l, r)| { - let (res, over) = op(*l, *r); - mut_bitmap.push(over); - res - }) - .collect::>() - .into(); - - ( - PrimitiveArray::::new(dtype, values, validity), - mut_bitmap.into(), - ) -} - -/// Version of binary that creates a mutable bitmap that is used to keep track -/// of checked operations. The resulting bitmap is compared with the array -/// bitmap to create the final validity array. -pub fn binary_checked( - lhs: &PrimitiveArray, - rhs: &PrimitiveArray, - dtype: ArrowDataType, - op: F, -) -> PrimitiveArray -where - T: NativeType, - D: NativeType, - F: Fn(T, D) -> Option, -{ - check_same_len(lhs, rhs).unwrap(); - - let mut mut_bitmap = MutableBitmap::with_capacity(lhs.len()); - - let values = lhs - .values() - .iter() - .zip(rhs.values().iter()) - .map(|(l, r)| match op(*l, *r) { - Some(val) => { - mut_bitmap.push(true); - val - }, - None => { - mut_bitmap.push(false); - T::default() - }, - }) - .collect::>() - .into(); - - let bitmap: Bitmap = mut_bitmap.into(); - let validity = combine_validities_and(lhs.validity(), rhs.validity()); - - // The validity has to be checked against the bitmap created during the - // creation of the values with the iterator. If an error was found during - // the iteration, then the validity is changed to None to mark the value - // as Null - let validity = combine_validities_and(validity.as_ref(), Some(&bitmap)); - - PrimitiveArray::::new(dtype, values, validity) -} diff --git a/crates/polars-arrow/src/compute/cast/binary_to.rs b/crates/polars-arrow/src/compute/cast/binary_to.rs index e14a03040522..6b8c1c59e470 100644 --- a/crates/polars-arrow/src/compute/cast/binary_to.rs +++ b/crates/polars-arrow/src/compute/cast/binary_to.rs @@ -92,19 +92,6 @@ pub fn binary_to_utf8( ) } -/// Conversion to utf8 -/// # Errors -/// This function errors if the values are not valid utf8 -pub fn binary_to_large_utf8( - from: &BinaryArray, - to_dtype: ArrowDataType, -) -> PolarsResult> { - let values = from.values().clone(); - let offsets = from.offsets().into(); - - Utf8Array::::try_new(to_dtype, offsets, values, from.validity().cloned()) -} - /// Casts a [`BinaryArray`] to a [`PrimitiveArray`], making any uncastable value a Null. pub(super) fn binary_to_primitive( from: &BinaryArray, diff --git a/crates/polars-arrow/src/compute/cast/dictionary_to.rs b/crates/polars-arrow/src/compute/cast/dictionary_to.rs index d67a116ca0de..3f6211ad544e 100644 --- a/crates/polars-arrow/src/compute/cast/dictionary_to.rs +++ b/crates/polars-arrow/src/compute/cast/dictionary_to.rs @@ -1,6 +1,6 @@ use polars_error::{polars_bail, PolarsResult}; -use super::{primitive_as_primitive, primitive_to_primitive, CastOptionsImpl}; +use super::{primitive_to_primitive, CastOptionsImpl}; use crate::array::{Array, DictionaryArray, DictionaryKey}; use crate::compute::cast::cast; use crate::datatypes::ArrowDataType; @@ -23,101 +23,6 @@ macro_rules! key_cast { }}; } -/// Casts a [`DictionaryArray`] to a new [`DictionaryArray`] by keeping the -/// keys and casting the values to `values_type`. -/// # Errors -/// This function errors if the values are not castable to `values_type` -pub fn dictionary_to_dictionary_values( - from: &DictionaryArray, - values_type: &ArrowDataType, -) -> PolarsResult> { - let keys = from.keys(); - let values = from.values(); - let length = values.len(); - - let values = cast(values.as_ref(), values_type, CastOptionsImpl::default())?; - - assert_eq!(values.len(), length); // this is guaranteed by `cast` - unsafe { - DictionaryArray::try_new_unchecked(from.dtype().clone(), keys.clone(), values.clone()) - } -} - -/// Similar to dictionary_to_dictionary_values, but overflowing cast is wrapped -pub fn wrapping_dictionary_to_dictionary_values( - from: &DictionaryArray, - values_type: &ArrowDataType, -) -> PolarsResult> { - let keys = from.keys(); - let values = from.values(); - let length = values.len(); - - let values = cast( - values.as_ref(), - values_type, - CastOptionsImpl { - wrapped: true, - partial: false, - }, - )?; - assert_eq!(values.len(), length); // this is guaranteed by `cast` - unsafe { - DictionaryArray::try_new_unchecked(from.dtype().clone(), keys.clone(), values.clone()) - } -} - -/// Casts a [`DictionaryArray`] to a new [`DictionaryArray`] backed by a -/// different physical type of the keys, while keeping the values equal. -/// # Errors -/// Errors if any of the old keys' values is larger than the maximum value -/// supported by the new physical type. -pub fn dictionary_to_dictionary_keys( - from: &DictionaryArray, -) -> PolarsResult> -where - K1: DictionaryKey + num_traits::NumCast, - K2: DictionaryKey + num_traits::NumCast, -{ - let keys = from.keys(); - let values = from.values(); - let is_ordered = from.is_ordered(); - - let casted_keys = primitive_to_primitive::(keys, &K2::PRIMITIVE.into()); - - if casted_keys.null_count() > keys.null_count() { - polars_bail!(ComputeError: "overflow") - } else { - let dtype = - ArrowDataType::Dictionary(K2::KEY_TYPE, Box::new(values.dtype().clone()), is_ordered); - // SAFETY: this is safe because given a type `T` that fits in a `usize`, casting it to type `P` either overflows or also fits in a `usize` - unsafe { DictionaryArray::try_new_unchecked(dtype, casted_keys, values.clone()) } - } -} - -/// Similar to dictionary_to_dictionary_keys, but overflowing cast is wrapped -pub fn wrapping_dictionary_to_dictionary_keys( - from: &DictionaryArray, -) -> PolarsResult> -where - K1: DictionaryKey + num_traits::AsPrimitive, - K2: DictionaryKey, -{ - let keys = from.keys(); - let values = from.values(); - let is_ordered = from.is_ordered(); - - let casted_keys = primitive_as_primitive::(keys, &K2::PRIMITIVE.into()); - - if casted_keys.null_count() > keys.null_count() { - polars_bail!(ComputeError: "overflow") - } else { - let dtype = - ArrowDataType::Dictionary(K2::KEY_TYPE, Box::new(values.dtype().clone()), is_ordered); - // some of the values may not fit in `usize` and thus this needs to be checked - DictionaryArray::try_new(dtype, casted_keys, values.clone()) - } -} - pub(super) fn dictionary_cast_dyn( array: &dyn Array, to_type: &ArrowDataType, diff --git a/crates/polars-arrow/src/compute/cast/mod.rs b/crates/polars-arrow/src/compute/cast/mod.rs index 2a544fc7209f..b64b26527fe2 100644 --- a/crates/polars-arrow/src/compute/cast/mod.rs +++ b/crates/polars-arrow/src/compute/cast/mod.rs @@ -15,7 +15,7 @@ use binview_to::binview_to_primitive_dyn; pub use binview_to::utf8view_to_utf8; pub use boolean_to::*; pub use decimal_to::*; -pub use dictionary_to::*; +use dictionary_to::*; use polars_error::{polars_bail, polars_ensure, polars_err, PolarsResult}; use polars_utils::IdxSize; pub use primitive_to::*; diff --git a/crates/polars-arrow/src/compute/cast/primitive_to.rs b/crates/polars-arrow/src/compute/cast/primitive_to.rs index d017b0a8e212..b0d84d81a3dd 100644 --- a/crates/polars-arrow/src/compute/cast/primitive_to.rs +++ b/crates/polars-arrow/src/compute/cast/primitive_to.rs @@ -8,10 +8,10 @@ use super::CastOptionsImpl; use crate::array::*; use crate::bitmap::Bitmap; use crate::compute::arity::unary; -use crate::datatypes::{ArrowDataType, IntervalUnit, TimeUnit}; +use crate::datatypes::{ArrowDataType, TimeUnit}; use crate::offset::{Offset, Offsets}; use crate::temporal_conversions::*; -use crate::types::{days_ms, f16, months_days_ns, NativeType}; +use crate::types::{f16, NativeType}; pub trait SerPrimitive { fn write(f: &mut Vec, val: Self) -> usize @@ -525,169 +525,6 @@ pub fn timestamp_to_timestamp( } } -fn timestamp_to_utf8_impl( - from: &PrimitiveArray, - time_unit: TimeUnit, - timezone: T, -) -> Utf8Array -where - T::Offset: std::fmt::Display, -{ - match time_unit { - TimeUnit::Nanosecond => { - let iter = from.iter().map(|x| { - x.map(|x| { - let datetime = timestamp_ns_to_datetime(*x); - let offset = timezone.offset_from_utc_datetime(&datetime); - chrono::DateTime::::from_naive_utc_and_offset(datetime, offset).to_rfc3339() - }) - }); - Utf8Array::from_trusted_len_iter(iter) - }, - TimeUnit::Microsecond => { - let iter = from.iter().map(|x| { - x.map(|x| { - let datetime = timestamp_us_to_datetime(*x); - let offset = timezone.offset_from_utc_datetime(&datetime); - chrono::DateTime::::from_naive_utc_and_offset(datetime, offset).to_rfc3339() - }) - }); - Utf8Array::from_trusted_len_iter(iter) - }, - TimeUnit::Millisecond => { - let iter = from.iter().map(|x| { - x.map(|x| { - let datetime = timestamp_ms_to_datetime(*x); - let offset = timezone.offset_from_utc_datetime(&datetime); - chrono::DateTime::::from_naive_utc_and_offset(datetime, offset).to_rfc3339() - }) - }); - Utf8Array::from_trusted_len_iter(iter) - }, - TimeUnit::Second => { - let iter = from.iter().map(|x| { - x.map(|x| { - let datetime = timestamp_s_to_datetime(*x); - let offset = timezone.offset_from_utc_datetime(&datetime); - chrono::DateTime::::from_naive_utc_and_offset(datetime, offset).to_rfc3339() - }) - }); - Utf8Array::from_trusted_len_iter(iter) - }, - } -} - -#[cfg(feature = "chrono-tz")] -#[cfg_attr(docsrs, doc(cfg(feature = "chrono-tz")))] -fn chrono_tz_timestamp_to_utf8( - from: &PrimitiveArray, - time_unit: TimeUnit, - timezone_str: &str, -) -> PolarsResult> { - let timezone = parse_offset_tz(timezone_str)?; - Ok(timestamp_to_utf8_impl::( - from, time_unit, timezone, - )) -} - -#[cfg(not(feature = "chrono-tz"))] -fn chrono_tz_timestamp_to_utf8( - _: &PrimitiveArray, - _: TimeUnit, - timezone_str: &str, -) -> PolarsResult> { - panic!( - "timezone \"{}\" cannot be parsed (feature chrono-tz is not active)", - timezone_str - ) -} - -/// Returns a [`Utf8Array`] where every element is the utf8 representation of the timestamp in the rfc3339 format. -pub fn timestamp_to_utf8( - from: &PrimitiveArray, - time_unit: TimeUnit, - timezone_str: &str, -) -> PolarsResult> { - let timezone = parse_offset(timezone_str); - - if let Ok(timezone) = timezone { - Ok(timestamp_to_utf8_impl::( - from, time_unit, timezone, - )) - } else { - chrono_tz_timestamp_to_utf8(from, time_unit, timezone_str) - } -} - -/// Returns a [`Utf8Array`] where every element is the utf8 representation of the timestamp in the rfc3339 format. -pub fn naive_timestamp_to_utf8( - from: &PrimitiveArray, - time_unit: TimeUnit, -) -> Utf8Array { - match time_unit { - TimeUnit::Nanosecond => { - let iter = from.iter().map(|x| { - x.copied() - .map(timestamp_ns_to_datetime) - .map(|x| x.to_string()) - }); - Utf8Array::from_trusted_len_iter(iter) - }, - TimeUnit::Microsecond => { - let iter = from.iter().map(|x| { - x.copied() - .map(timestamp_us_to_datetime) - .map(|x| x.to_string()) - }); - Utf8Array::from_trusted_len_iter(iter) - }, - TimeUnit::Millisecond => { - let iter = from.iter().map(|x| { - x.copied() - .map(timestamp_ms_to_datetime) - .map(|x| x.to_string()) - }); - Utf8Array::from_trusted_len_iter(iter) - }, - TimeUnit::Second => { - let iter = from.iter().map(|x| { - x.copied() - .map(timestamp_s_to_datetime) - .map(|x| x.to_string()) - }); - Utf8Array::from_trusted_len_iter(iter) - }, - } -} - -#[inline] -fn days_ms_to_months_days_ns_scalar(from: days_ms) -> months_days_ns { - months_days_ns::new(0, from.days(), from.milliseconds() as i64 * 1000) -} - -/// Casts [`days_ms`]s to [`months_days_ns`]. This operation is infalible and lossless. -pub fn days_ms_to_months_days_ns(from: &PrimitiveArray) -> PrimitiveArray { - unary( - from, - days_ms_to_months_days_ns_scalar, - ArrowDataType::Interval(IntervalUnit::MonthDayNano), - ) -} - -#[inline] -fn months_to_months_days_ns_scalar(from: i32) -> months_days_ns { - months_days_ns::new(from, 0, 0) -} - -/// Casts months represented as [`i32`]s to [`months_days_ns`]. This operation is infalible and lossless. -pub fn months_to_months_days_ns(from: &PrimitiveArray) -> PrimitiveArray { - unary( - from, - months_to_months_days_ns_scalar, - ArrowDataType::Interval(IntervalUnit::MonthDayNano), - ) -} - /// Casts f16 into f32 pub fn f16_to_f32(from: &PrimitiveArray) -> PrimitiveArray { unary(from, |x| x.to_f32(), ArrowDataType::Float32) diff --git a/crates/polars-arrow/src/compute/temporal.rs b/crates/polars-arrow/src/compute/temporal.rs index 309493fbbbdb..98bc920caeb3 100644 --- a/crates/polars-arrow/src/compute/temporal.rs +++ b/crates/polars-arrow/src/compute/temporal.rs @@ -75,8 +75,6 @@ macro_rules! date_like { } /// Extracts the years of a temporal array as [`PrimitiveArray`]. -/// -/// Use [`can_year`] to check if this operation is supported for the target [`ArrowDataType`]. pub fn year(array: &dyn Array) -> PolarsResult> { date_like!(year, array, ArrowDataType::Int32) } @@ -84,7 +82,6 @@ pub fn year(array: &dyn Array) -> PolarsResult> { /// Extracts the months of a temporal array as [`PrimitiveArray`]. /// /// Value ranges from 1 to 12. -/// Use [`can_month`] to check if this operation is supported for the target [`ArrowDataType`]. pub fn month(array: &dyn Array) -> PolarsResult> { date_like!(month, array, ArrowDataType::Int8) } @@ -92,7 +89,6 @@ pub fn month(array: &dyn Array) -> PolarsResult> { /// Extracts the days of a temporal array as [`PrimitiveArray`]. /// /// Value ranges from 1 to 32 (Last day depends on month). -/// Use [`can_day`] to check if this operation is supported for the target [`ArrowDataType`]. pub fn day(array: &dyn Array) -> PolarsResult> { date_like!(day, array, ArrowDataType::Int8) } @@ -100,7 +96,6 @@ pub fn day(array: &dyn Array) -> PolarsResult> { /// Extracts weekday of a temporal array as [`PrimitiveArray`]. /// /// Monday is 1, Tuesday is 2, ..., Sunday is 7. -/// Use [`can_weekday`] to check if this operation is supported for the target [`ArrowDataType`] pub fn weekday(array: &dyn Array) -> PolarsResult> { date_like!(i8_weekday, array, ArrowDataType::Int8) } @@ -108,7 +103,6 @@ pub fn weekday(array: &dyn Array) -> PolarsResult> { /// Extracts ISO week of a temporal array as [`PrimitiveArray`]. /// /// Value ranges from 1 to 53 (Last week depends on the year). -/// Use [`can_iso_week`] to check if this operation is supported for the target [`ArrowDataType`] pub fn iso_week(array: &dyn Array) -> PolarsResult> { date_like!(i8_iso_week, array, ArrowDataType::Int8) } @@ -345,86 +339,3 @@ where }, } } - -/// Checks if an array of type `datatype` can perform year operation -/// -/// # Examples -/// ``` -/// use polars_arrow::compute::temporal::can_year; -/// use polars_arrow::datatypes::{ArrowDataType}; -/// -/// assert_eq!(can_year(&ArrowDataType::Date32), true); -/// assert_eq!(can_year(&ArrowDataType::Int8), false); -/// ``` -pub fn can_year(dtype: &ArrowDataType) -> bool { - can_date(dtype) -} - -/// Checks if an array of type `datatype` can perform month operation -pub fn can_month(dtype: &ArrowDataType) -> bool { - can_date(dtype) -} - -/// Checks if an array of type `datatype` can perform day operation -pub fn can_day(dtype: &ArrowDataType) -> bool { - can_date(dtype) -} - -/// Checks if an array of type `dtype` can perform weekday operation -pub fn can_weekday(dtype: &ArrowDataType) -> bool { - can_date(dtype) -} - -/// Checks if an array of type `dtype` can perform ISO week operation -pub fn can_iso_week(dtype: &ArrowDataType) -> bool { - can_date(dtype) -} - -fn can_date(dtype: &ArrowDataType) -> bool { - matches!( - dtype, - ArrowDataType::Date32 | ArrowDataType::Date64 | ArrowDataType::Timestamp(_, _) - ) -} - -/// Checks if an array of type `datatype` can perform hour operation -/// -/// # Examples -/// ``` -/// use polars_arrow::compute::temporal::can_hour; -/// use polars_arrow::datatypes::{ArrowDataType, TimeUnit}; -/// -/// assert_eq!(can_hour(&ArrowDataType::Time32(TimeUnit::Second)), true); -/// assert_eq!(can_hour(&ArrowDataType::Int8), false); -/// ``` -pub fn can_hour(dtype: &ArrowDataType) -> bool { - can_time(dtype) -} - -/// Checks if an array of type `datatype` can perform minute operation -pub fn can_minute(dtype: &ArrowDataType) -> bool { - can_time(dtype) -} - -/// Checks if an array of type `datatype` can perform second operation -pub fn can_second(dtype: &ArrowDataType) -> bool { - can_time(dtype) -} - -/// Checks if an array of type `datatype` can perform nanosecond operation -pub fn can_nanosecond(dtype: &ArrowDataType) -> bool { - can_time(dtype) -} - -fn can_time(dtype: &ArrowDataType) -> bool { - matches!( - dtype, - ArrowDataType::Time32(TimeUnit::Second) - | ArrowDataType::Time32(TimeUnit::Millisecond) - | ArrowDataType::Time64(TimeUnit::Microsecond) - | ArrowDataType::Time64(TimeUnit::Nanosecond) - | ArrowDataType::Date32 - | ArrowDataType::Date64 - | ArrowDataType::Timestamp(_, _) - ) -} diff --git a/crates/polars-arrow/src/compute/utils.rs b/crates/polars-arrow/src/compute/utils.rs index 0b8e1ecd69f4..2402283aa4ef 100644 --- a/crates/polars-arrow/src/compute/utils.rs +++ b/crates/polars-arrow/src/compute/utils.rs @@ -38,6 +38,7 @@ pub fn combine_validities_or(opt_l: Option<&Bitmap>, opt_r: Option<&Bitmap>) -> _ => None, } } + pub fn combine_validities_and_not( opt_l: Option<&Bitmap>, opt_r: Option<&Bitmap>, diff --git a/docs/source/development/contributing/code-style.md b/docs/source/development/contributing/code-style.md index 00ad8a8f726e..c22b9f3ed7ac 100644 --- a/docs/source/development/contributing/code-style.md +++ b/docs/source/development/contributing/code-style.md @@ -30,7 +30,7 @@ use polars::export::arrow::array::*; use polars::export::arrow::compute::arity::binary; use polars::export::arrow::types::NativeType; use polars::prelude::*; -use polars_core::utils::{align_chunks_binary, combine_validities_or}; +use polars_core::utils::{align_chunks_binary, combine_validities_and}; use polars_core::with_match_physical_numeric_polars_type; // Prefer to do the compute closest to the arrow arrays. @@ -45,7 +45,7 @@ where let validity_1 = arr_1.validity(); let validity_2 = arr_2.validity(); - let validity = combine_validities_or(validity_1, validity_2); + let validity = combine_validities_and(validity_1, validity_2); // process the numerical data as if there were no validities let values_1: &[T] = arr_1.values().as_slice();