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(rust): Split Reduction into it plus ReductionState #18460

Merged
merged 5 commits into from
Aug 30, 2024
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions crates/polars-core/src/scalar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ pub struct Scalar {
}

impl Scalar {
#[inline(always)]
pub fn new(dtype: DataType, value: AnyValue<'static>) -> Self {
Self { dtype, value }
}

#[inline(always)]
pub fn is_null(&self) -> bool {
self.value.is_null()
}

#[inline(always)]
pub fn value(&self) -> &AnyValue<'static> {
&self.value
}
Expand All @@ -30,10 +37,12 @@ impl Scalar {
Series::from_any_values_and_dtype(name, &[self.as_any_value()], &self.dtype, true).unwrap()
}

#[inline(always)]
pub fn dtype(&self) -> &DataType {
&self.dtype
}

#[inline(always)]
pub fn update(&mut self, value: AnyValue<'static>) {
self.value = value;
}
Expand Down
1 change: 1 addition & 0 deletions crates/polars-expr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ahash = { workspace = true }
arrow = { workspace = true }
bitflags = { workspace = true }
once_cell = { workspace = true }
polars-compute = { workspace = true }
polars-core = { workspace = true, features = ["lazy", "zip_with", "random"] }
polars-io = { workspace = true, features = ["lazy"] }
polars-json = { workspace = true, optional = true }
Expand Down
16 changes: 9 additions & 7 deletions crates/polars-expr/src/reduce/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ use polars_core::error::feature_gated;
use polars_plan::prelude::*;
use polars_utils::arena::{Arena, Node};

use super::extrema::*;
use super::len::LenReduce;
use super::mean::MeanReduce;
use super::min_max::{MaxReduce, MinReduce};
#[cfg(feature = "propagate_nans")]
use super::nan_min_max::{NanMaxReduce, NanMinReduce};
use super::sum::SumReduce;
use super::*;
use crate::reduce::len::LenReduce;
use crate::reduce::mean::MeanReduce;

/// Converts a node into a reduction + its associated selector expression.
pub fn into_reduction(
Expand All @@ -29,8 +31,8 @@ pub fn into_reduction(
if *propagate_nans && field.dtype.is_float() {
feature_gated!("propagate_nans", {
let out: Box<dyn Reduction> = match field.dtype {
DataType::Float32 => Box::new(MinNanReduce::<Float32Type>::new()),
DataType::Float64 => Box::new(MinNanReduce::<Float64Type>::new()),
DataType::Float32 => Box::new(NanMinReduce::<Float32Type>::new()),
DataType::Float64 => Box::new(NanMinReduce::<Float64Type>::new()),
_ => unreachable!(),
};
(out, *input)
Expand All @@ -49,8 +51,8 @@ pub fn into_reduction(
if *propagate_nans && field.dtype.is_float() {
feature_gated!("propagate_nans", {
let out: Box<dyn Reduction> = match field.dtype {
DataType::Float32 => Box::new(MaxNanReduce::<Float32Type>::new()),
DataType::Float64 => Box::new(MaxNanReduce::<Float64Type>::new()),
DataType::Float32 => Box::new(NanMaxReduce::<Float32Type>::new()),
DataType::Float64 => Box::new(NanMaxReduce::<Float64Type>::new()),
_ => unreachable!(),
};
(out, *input)
Expand Down
249 changes: 0 additions & 249 deletions crates/polars-expr/src/reduce/extrema.rs

This file was deleted.

24 changes: 12 additions & 12 deletions crates/polars-expr/src/reduce/len.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@ use polars_core::error::constants::LENGTH_LIMIT_MSG;
use super::*;

#[derive(Clone)]
pub struct LenReduce {
len: u64,
}
pub struct LenReduce {}

impl LenReduce {
pub(crate) fn new() -> Self {
Self { len: 0 }
pub fn new() -> Self {
Self {}
}
}

impl Reduction for LenReduce {
fn init_dyn(&self) -> Box<dyn Reduction> {
Box::new(Self::new())
fn new_reducer(&self) -> Box<dyn ReductionState> {
Box::new(LenReduceState { len: 0 })
}
}

fn reset(&mut self) {
self.len = 0;
}
pub struct LenReduceState {
len: u64,
}

impl ReductionState for LenReduceState {
fn update(&mut self, batch: &Series) -> PolarsResult<()> {
self.len += batch.len() as u64;
Ok(())
}

fn combine(&mut self, other: &dyn Reduction) -> PolarsResult<()> {
fn combine(&mut self, other: &dyn ReductionState) -> PolarsResult<()> {
let other = other.as_any().downcast_ref::<Self>().unwrap();
self.len += other.len;
Ok(())
}

fn finalize(&mut self) -> PolarsResult<Scalar> {
fn finalize(&self) -> PolarsResult<Scalar> {
#[allow(clippy::useless_conversion)]
let as_idx: IdxSize = self.len.try_into().expect(LENGTH_LIMIT_MSG);
Ok(Scalar::new(IDX_DTYPE, as_idx.into()))
Expand Down
Loading
Loading