Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Get Column into polars-expr #19660

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions crates/polars-core/src/chunked_array/from_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ where
}
}

impl FromIterator<Option<Column>> for ListChunked {
fn from_iter<T: IntoIterator<Item = Option<Column>>>(iter: T) -> Self {
ListChunked::from_iter(
iter.into_iter()
.map(|c| c.map(|c| c.take_materialized_series())),
)
}
}

impl FromIterator<Option<Series>> for ListChunked {
#[inline]
fn from_iter<I: IntoIterator<Item = Option<Series>>>(iter: I) -> Self {
Expand Down
69 changes: 3 additions & 66 deletions crates/polars-core/src/frame/column/arithmetic.rs
Original file line number Diff line number Diff line change
@@ -1,70 +1,7 @@
use num_traits::{Num, NumCast};
use polars_error::{polars_bail, PolarsResult};
use polars_error::PolarsResult;

use super::{Column, ScalarColumn, Series};
use crate::utils::Container;

fn output_length(a: &Column, b: &Column) -> PolarsResult<usize> {
match (a.len(), b.len()) {
// broadcasting
(1, o) | (o, 1) => Ok(o),
// equal
(a, b) if a == b => Ok(a),
// unequal
(a, b) => {
polars_bail!(InvalidOperation: "cannot do arithmetic operation on series of different lengths: got {} and {}", a, b)
},
}
}

fn unit_series_op<F: Fn(&Series, &Series) -> PolarsResult<Series>>(
l: &Series,
r: &Series,
op: F,
length: usize,
) -> PolarsResult<Column> {
debug_assert!(l.len() <= 1);
debug_assert!(r.len() <= 1);

op(l, r)
.map(|s| ScalarColumn::from_single_value_series(s, length))
.map(Column::from)
}

fn op_with_broadcast<F: Fn(&Series, &Series) -> PolarsResult<Series>>(
l: &Column,
r: &Column,
op: F,
) -> PolarsResult<Column> {
// Here we rely on the underlying broadcast operations.

let length = output_length(l, r)?;
match (l, r) {
(Column::Series(l), Column::Scalar(r)) => {
let r = r.as_single_value_series();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was not functioning properly for series with length=0, so I replaced it with Column::try_apply_broadcasting_binary_elementwise.

if l.len() == 1 {
unit_series_op(l, &r, op, length)
} else {
op(l, &r).map(Column::from)
}
},
(Column::Scalar(l), Column::Series(r)) => {
let l = l.as_single_value_series();
if r.len() == 1 {
unit_series_op(&l, r, op, length)
} else {
op(&l, r).map(Column::from)
}
},
(Column::Scalar(l), Column::Scalar(r)) => unit_series_op(
&l.as_single_value_series(),
&r.as_single_value_series(),
op,
length,
),
(l, r) => op(l.as_materialized_series(), r.as_materialized_series()).map(Column::from),
}
}

fn num_op_with_broadcast<T: Num + NumCast, F: Fn(&Series, T) -> Series>(
c: &'_ Column,
Expand All @@ -90,7 +27,7 @@ macro_rules! broadcastable_ops {

#[inline]
fn $op(self, rhs: Self) -> Self::Output {
op_with_broadcast(&self, &rhs, |l, r| l.$op(r))
self.try_apply_broadcasting_binary_elementwise(&rhs, |l, r| l.$op(r))
}
}

Expand All @@ -99,7 +36,7 @@ macro_rules! broadcastable_ops {

#[inline]
fn $op(self, rhs: Self) -> Self::Output {
op_with_broadcast(self, rhs, |l, r| l.$op(r))
self.try_apply_broadcasting_binary_elementwise(rhs, |l, r| l.$op(r))
}
}
)+
Expand Down
Loading