Path: blob/main/crates/polars-core/src/series/arithmetic/owned.rs
6940 views
use super::*;1#[cfg(feature = "performant")]2use crate::utils::align_chunks_binary_owned_series;34#[cfg(feature = "performant")]5pub fn coerce_lhs_rhs_owned(lhs: Series, rhs: Series) -> PolarsResult<(Series, Series)> {6let dtype = try_get_supertype(lhs.dtype(), rhs.dtype())?;7let left = if lhs.dtype() == &dtype {8lhs9} else {10lhs.cast(&dtype)?11};12let right = if rhs.dtype() == &dtype {13rhs14} else {15rhs.cast(&dtype)?16};17Ok((left, right))18}1920fn is_eligible(lhs: &DataType, rhs: &DataType) -> bool {21!lhs.is_logical()22&& lhs.to_physical().is_primitive_numeric()23&& rhs.to_physical().is_primitive_numeric()24}2526#[cfg(feature = "performant")]27fn apply_operation_mut<T, F>(mut lhs: Series, mut rhs: Series, op: F) -> Series28where29T: PolarsNumericType,30F: Fn(ChunkedArray<T>, ChunkedArray<T>) -> ChunkedArray<T> + Copy,31{32let lhs_ca: &mut ChunkedArray<T> = lhs._get_inner_mut().as_mut();33let rhs_ca: &mut ChunkedArray<T> = rhs._get_inner_mut().as_mut();3435let lhs = std::mem::take(lhs_ca);36let rhs = std::mem::take(rhs_ca);3738op(lhs, rhs).into_series()39}4041macro_rules! impl_operation {42($operation:ident, $method:ident, $function:expr) => {43impl $operation for Series {44type Output = PolarsResult<Series>;4546fn $method(self, rhs: Self) -> Self::Output {47#[cfg(feature = "performant")]48{49// only physical numeric values take the mutable path50if is_eligible(self.dtype(), rhs.dtype()) {51let (lhs, rhs) = coerce_lhs_rhs_owned(self, rhs).unwrap();52let (lhs, rhs) = align_chunks_binary_owned_series(lhs, rhs);53use DataType::*;54Ok(match lhs.dtype() {55#[cfg(feature = "dtype-i8")]56Int8 => apply_operation_mut::<Int8Type, _>(lhs, rhs, $function),57#[cfg(feature = "dtype-i16")]58Int16 => apply_operation_mut::<Int16Type, _>(lhs, rhs, $function),59Int32 => apply_operation_mut::<Int32Type, _>(lhs, rhs, $function),60Int64 => apply_operation_mut::<Int64Type, _>(lhs, rhs, $function),61#[cfg(feature = "dtype-i128")]62Int128 => apply_operation_mut::<Int128Type, _>(lhs, rhs, $function),63#[cfg(feature = "dtype-u8")]64UInt8 => apply_operation_mut::<UInt8Type, _>(lhs, rhs, $function),65#[cfg(feature = "dtype-u16")]66UInt16 => apply_operation_mut::<UInt16Type, _>(lhs, rhs, $function),67UInt32 => apply_operation_mut::<UInt32Type, _>(lhs, rhs, $function),68UInt64 => apply_operation_mut::<UInt64Type, _>(lhs, rhs, $function),69Float32 => apply_operation_mut::<Float32Type, _>(lhs, rhs, $function),70Float64 => apply_operation_mut::<Float64Type, _>(lhs, rhs, $function),71_ => unreachable!(),72})73} else {74(&self).$method(&rhs)75}76}77#[cfg(not(feature = "performant"))]78{79(&self).$method(&rhs)80}81}82}83};84}8586impl_operation!(Add, add, |a, b| a.add(b));87impl_operation!(Sub, sub, |a, b| a.sub(b));88impl_operation!(Mul, mul, |a, b| a.mul(b));89impl_operation!(Div, div, |a, b| a.div(b));9091impl Series {92pub fn try_add_owned(self, other: Self) -> PolarsResult<Self> {93if is_eligible(self.dtype(), other.dtype()) {94self + other95} else {96std::ops::Add::add(&self, &other)97}98}99100pub fn try_sub_owned(self, other: Self) -> PolarsResult<Self> {101if is_eligible(self.dtype(), other.dtype()) {102self - other103} else {104std::ops::Sub::sub(&self, &other)105}106}107108pub fn try_mul_owned(self, other: Self) -> PolarsResult<Self> {109if is_eligible(self.dtype(), other.dtype()) {110self * other111} else {112std::ops::Mul::mul(&self, &other)113}114}115}116117118