Path: blob/main/crates/polars-time/src/chunkedarray/kernels.rs
6939 views
//! macros that define kernels for extracting1//! `week`, `weekday`, `year`, `hour` etc. from primitive arrays.2use arrow::array::{BooleanArray, PrimitiveArray};3use arrow::compute::arity::unary;4#[cfg(feature = "dtype-time")]5use arrow::temporal_conversions::time64ns_to_time_opt;6use arrow::temporal_conversions::{7date32_to_datetime_opt, timestamp_ms_to_datetime_opt, timestamp_ns_to_datetime_opt,8timestamp_us_to_datetime_opt,9};10use chrono::{Datelike, Timelike};1112use super::super::windows::calendar::*;13use super::*;1415trait PolarsIso {16fn week(&self) -> i8;17fn iso_year(&self) -> i32;18}1920impl PolarsIso for NaiveDateTime {21fn week(&self) -> i8 {22self.iso_week().week().try_into().unwrap()23}24fn iso_year(&self) -> i32 {25self.iso_week().year()26}27}2829impl PolarsIso for NaiveDate {30fn week(&self) -> i8 {31self.iso_week().week().try_into().unwrap()32}33fn iso_year(&self) -> i32 {34self.iso_week().year()35}36}3738macro_rules! to_temporal_unit {39($name: ident, $chrono_method: ident, $to_datetime_fn: expr,40$primitive_in: ty,41$primitive_out: ty,42$dtype_out:expr) => {43pub(crate) fn $name(arr: &PrimitiveArray<$primitive_in>) -> ArrayRef {44Box::new(unary(45arr,46|value| {47$to_datetime_fn(value)48.map(|dt| dt.$chrono_method() as $primitive_out)49.unwrap_or(value as $primitive_out)50},51$dtype_out,52)) as ArrayRef53}54};55}5657macro_rules! to_boolean_temporal_unit {58($name: ident, $chrono_method: ident, $boolean_method: ident, $to_datetime_fn: expr, $dtype_in: ty) => {59pub(crate) fn $name(arr: &PrimitiveArray<$dtype_in>) -> ArrayRef {60let values = arr61.values()62.iter()63.map(|value| {64$to_datetime_fn(*value)65.map(|dt| $boolean_method(dt.$chrono_method()))66.unwrap_or(false)67})68.collect::<Vec<_>>();69Box::new(BooleanArray::new(70ArrowDataType::Boolean,71values.into(),72arr.validity().cloned(),73))74}75};76}7778macro_rules! to_calendar_value {79($name: ident, $dt: ident, $expr: expr, $to_datetime_fn: expr,80$primitive_in: ty,81$primitive_out: ty,82$dtype_out:expr) => {83pub(crate) fn $name(arr: &PrimitiveArray<$primitive_in>) -> ArrayRef {84Box::new(unary(85arr,86|value| {87$to_datetime_fn(value)88.map(|$dt| $expr as $primitive_out)89.unwrap_or(value as $primitive_out)90},91$dtype_out,92)) as ArrayRef93}94};95}9697// Dates98#[cfg(feature = "dtype-date")]99to_temporal_unit!(100date_to_iso_week,101week,102date32_to_datetime_opt,103i32,104i8,105ArrowDataType::Int8106);107#[cfg(feature = "dtype-date")]108to_temporal_unit!(109date_to_iso_year,110iso_year,111date32_to_datetime_opt,112i32,113i32,114ArrowDataType::Int32115);116#[cfg(feature = "dtype-date")]117to_temporal_unit!(118date_to_year,119year,120date32_to_datetime_opt,121i32,122i32,123ArrowDataType::Int32124);125#[cfg(feature = "dtype-date")]126to_boolean_temporal_unit!(127date_to_is_leap_year,128year,129is_leap_year,130date32_to_datetime_opt,131i32132);133#[cfg(feature = "dtype-date")]134to_temporal_unit!(135date_to_month,136month,137date32_to_datetime_opt,138i32,139i8,140ArrowDataType::Int8141);142#[cfg(feature = "dtype-date")]143to_temporal_unit!(144date_to_day,145day,146date32_to_datetime_opt,147i32,148i8,149ArrowDataType::Int8150);151#[cfg(feature = "dtype-date")]152to_temporal_unit!(153date_to_ordinal,154ordinal,155date32_to_datetime_opt,156i32,157i16,158ArrowDataType::Int16159);160#[cfg(feature = "dtype-date")]161to_calendar_value!(162date_to_days_in_month,163dt,164days_in_month(dt.year(), dt.month() as u8),165date32_to_datetime_opt,166i32,167i8,168ArrowDataType::Int8169);170171// Times172#[cfg(feature = "dtype-time")]173to_temporal_unit!(174time_to_hour,175hour,176time64ns_to_time_opt,177i64,178i8,179ArrowDataType::Int8180);181#[cfg(feature = "dtype-time")]182to_temporal_unit!(183time_to_minute,184minute,185time64ns_to_time_opt,186i64,187i8,188ArrowDataType::Int8189);190#[cfg(feature = "dtype-time")]191to_temporal_unit!(192time_to_second,193second,194time64ns_to_time_opt,195i64,196i8,197ArrowDataType::Int8198);199#[cfg(feature = "dtype-time")]200to_temporal_unit!(201time_to_nanosecond,202nanosecond,203time64ns_to_time_opt,204i64,205i32,206ArrowDataType::Int32207);208209#[cfg(feature = "dtype-datetime")]210to_temporal_unit!(211datetime_to_ordinal_ns,212ordinal,213timestamp_ns_to_datetime_opt,214i64,215i16,216ArrowDataType::Int16217);218219#[cfg(feature = "dtype-datetime")]220to_temporal_unit!(221datetime_to_ordinal_ms,222ordinal,223timestamp_ms_to_datetime_opt,224i64,225i16,226ArrowDataType::Int16227);228#[cfg(feature = "dtype-datetime")]229to_temporal_unit!(230datetime_to_ordinal_us,231ordinal,232timestamp_us_to_datetime_opt,233i64,234i16,235ArrowDataType::Int16236);237238#[cfg(feature = "dtype-datetime")]239to_temporal_unit!(240datetime_to_iso_year_ns,241iso_year,242timestamp_ns_to_datetime_opt,243i64,244i32,245ArrowDataType::Int32246);247248#[cfg(feature = "dtype-datetime")]249to_temporal_unit!(250datetime_to_iso_year_us,251iso_year,252timestamp_us_to_datetime_opt,253i64,254i32,255ArrowDataType::Int32256);257258#[cfg(feature = "dtype-datetime")]259to_temporal_unit!(260datetime_to_iso_year_ms,261iso_year,262timestamp_ms_to_datetime_opt,263i64,264i32,265ArrowDataType::Int32266);267#[cfg(feature = "dtype-datetime")]268to_boolean_temporal_unit!(269datetime_to_is_leap_year_ns,270year,271is_leap_year,272timestamp_ns_to_datetime_opt,273i64274);275#[cfg(feature = "dtype-datetime")]276to_boolean_temporal_unit!(277datetime_to_is_leap_year_us,278year,279is_leap_year,280timestamp_us_to_datetime_opt,281i64282);283#[cfg(feature = "dtype-datetime")]284to_boolean_temporal_unit!(285datetime_to_is_leap_year_ms,286year,287is_leap_year,288timestamp_ms_to_datetime_opt,289i64290);291292#[cfg(feature = "dtype-datetime")]293to_calendar_value!(294datetime_to_days_in_month_ns,295dt,296days_in_month(dt.year(), dt.month() as u8),297timestamp_ns_to_datetime_opt,298i64,299i8,300ArrowDataType::Int8301);302#[cfg(feature = "dtype-datetime")]303to_calendar_value!(304datetime_to_days_in_month_us,305dt,306days_in_month(dt.year(), dt.month() as u8),307timestamp_us_to_datetime_opt,308i64,309i8,310ArrowDataType::Int8311);312#[cfg(feature = "dtype-datetime")]313to_calendar_value!(314datetime_to_days_in_month_ms,315dt,316days_in_month(dt.year(), dt.month() as u8),317timestamp_ms_to_datetime_opt,318i64,319i8,320ArrowDataType::Int8321);322323324