Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
orlp committed Oct 23, 2024
1 parent df94f68 commit a6159ea
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 30 deletions.
2 changes: 1 addition & 1 deletion crates/polars-compute/src/var_cov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl VarState {
dp: alg_sum(x.iter().map(|&xi| (xi - mean) * (xi - mean))),
}
}

pub fn add_one(&mut self, x: f64) {
// Just a specialized version of
// self.combine(&Self { weight: 1.0, mean: x, dp: 0.0 })
Expand Down
8 changes: 6 additions & 2 deletions crates/polars-expr/src/reduce/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ pub fn into_reduction(
propagate_nans,
input,
} => (new_max_reduction(get_dt(*input)?, *propagate_nans), *input),
IRAggExpr::Var(input, ddof) => (new_var_std_reduction(get_dt(*input)?, false, *ddof), *input),
IRAggExpr::Std(input, ddof) => (new_var_std_reduction(get_dt(*input)?, true, *ddof), *input),
IRAggExpr::Var(input, ddof) => {
(new_var_std_reduction(get_dt(*input)?, false, *ddof), *input)
},
IRAggExpr::Std(input, ddof) => {
(new_var_std_reduction(get_dt(*input)?, true, *ddof), *input)
},
_ => todo!(),
},
AExpr::Len => {
Expand Down
16 changes: 13 additions & 3 deletions crates/polars-expr/src/reduce/mean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ where
v.1 += ca.len() - ca.null_count();
}

fn finish(&self, v: Vec<Self::Value>, m: Option<Bitmap>, dtype: &DataType) -> PolarsResult<Series> {
fn finish(
&self,
v: Vec<Self::Value>,
m: Option<Bitmap>,
dtype: &DataType,
) -> PolarsResult<Series> {
assert!(m.is_none());
Ok(finish_output(v, dtype))
}
Expand All @@ -121,7 +126,7 @@ impl Reducer for BoolMeanReducer {
type Value = (usize, usize);

#[inline(always)]
fn init(&self, ) -> Self::Value {
fn init(&self) -> Self::Value {
(0, 0)
}

Expand All @@ -142,7 +147,12 @@ impl Reducer for BoolMeanReducer {
v.1 += ca.len() - ca.null_count();
}

fn finish(&self, v: Vec<Self::Value>, m: Option<Bitmap>, dtype: &DataType) -> PolarsResult<Series> {
fn finish(
&self,
v: Vec<Self::Value>,
m: Option<Bitmap>,
dtype: &DataType,
) -> PolarsResult<Series> {
assert!(m.is_none());
assert!(dtype == &DataType::Boolean);
let ca: Float64Chunked = v
Expand Down
34 changes: 26 additions & 8 deletions crates/polars-expr/src/reduce/min_max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ pub fn new_min_reduction(dtype: DataType, propagate_nans: bool) -> Box<dyn Group
match dtype {
Boolean => Box::new(BoolMinGroupedReduction::default()),
#[cfg(feature = "propagate_nans")]
Float32 if propagate_nans => Box::new(VMGR::new(dtype, NumReducer::<NanMin<Float32Type>>::new())),
Float32 if propagate_nans => {
Box::new(VMGR::new(dtype, NumReducer::<NanMin<Float32Type>>::new()))
},
#[cfg(feature = "propagate_nans")]
Float64 if propagate_nans => Box::new(VMGR::new(dtype, NumReducer::<NanMin<Float64Type>>::new())),
Float64 if propagate_nans => {
Box::new(VMGR::new(dtype, NumReducer::<NanMin<Float64Type>>::new()))
},
Float32 => Box::new(VMGR::new(dtype, NumReducer::<Min<Float32Type>>::new())),
Float64 => Box::new(VMGR::new(dtype, NumReducer::<Min<Float64Type>>::new())),
String | Binary => Box::new(VecGroupedReduction::new(dtype, BinaryMinReducer)),
Expand All @@ -41,9 +45,13 @@ pub fn new_max_reduction(dtype: DataType, propagate_nans: bool) -> Box<dyn Group
match dtype {
Boolean => Box::new(BoolMaxGroupedReduction::default()),
#[cfg(feature = "propagate_nans")]
Float32 if propagate_nans => Box::new(VMGR::new(dtype, NumReducer::<NanMax<Float32Type>>::new())),
Float32 if propagate_nans => {
Box::new(VMGR::new(dtype, NumReducer::<NanMax<Float32Type>>::new()))
},
#[cfg(feature = "propagate_nans")]
Float64 if propagate_nans => Box::new(VMGR::new(dtype, NumReducer::<NanMax<Float64Type>>::new())),
Float64 if propagate_nans => {
Box::new(VMGR::new(dtype, NumReducer::<NanMax<Float64Type>>::new()))
},
Float32 => Box::new(VMGR::new(dtype, NumReducer::<Max<Float32Type>>::new())),
Float64 => Box::new(VMGR::new(dtype, NumReducer::<Max<Float64Type>>::new())),
String | Binary => Box::new(VecGroupedReduction::new(dtype, BinaryMaxReducer)),
Expand Down Expand Up @@ -171,7 +179,7 @@ impl Reducer for BinaryMinReducer {
type Dtype = BinaryType;
type Value = Option<Vec<u8>>; // TODO: evaluate SmallVec<u8>.

fn init(&self, ) -> Self::Value {
fn init(&self) -> Self::Value {
None
}

Expand Down Expand Up @@ -201,7 +209,12 @@ impl Reducer for BinaryMinReducer {
self.reduce_one(v, ca.min_binary())
}

fn finish(&self, v: Vec<Self::Value>, m: Option<Bitmap>, dtype: &DataType) -> PolarsResult<Series> {
fn finish(
&self,
v: Vec<Self::Value>,
m: Option<Bitmap>,
dtype: &DataType,
) -> PolarsResult<Series> {
assert!(m.is_none()); // This should only be used with VecGroupedReduction.
let ca: BinaryChunked = v.into_iter().collect_ca(PlSmallStr::EMPTY);
ca.into_series().cast(dtype)
Expand All @@ -213,7 +226,7 @@ impl Reducer for BinaryMaxReducer {
type Value = Option<Vec<u8>>; // TODO: evaluate SmallVec<u8>.

#[inline(always)]
fn init(&self, ) -> Self::Value {
fn init(&self) -> Self::Value {
None
}

Expand Down Expand Up @@ -247,7 +260,12 @@ impl Reducer for BinaryMaxReducer {
}

#[inline(always)]
fn finish(&self, v: Vec<Self::Value>, m: Option<Bitmap>, dtype: &DataType) -> PolarsResult<Series> {
fn finish(
&self,
v: Vec<Self::Value>,
m: Option<Bitmap>,
dtype: &DataType,
) -> PolarsResult<Series> {
assert!(m.is_none()); // This should only be used with VecGroupedReduction.
let ca: BinaryChunked = v.into_iter().collect_ca(PlSmallStr::EMPTY);
ca.into_series().cast(dtype)
Expand Down
34 changes: 27 additions & 7 deletions crates/polars-expr/src/reduce/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,18 @@ pub trait Reducer: Send + Sync + Clone + 'static {
Cow::Borrowed(s)
}
fn combine(&self, a: &mut Self::Value, b: &Self::Value);
fn reduce_one(&self, a: &mut Self::Value, b: Option<<Self::Dtype as PolarsDataType>::Physical<'_>>);
fn reduce_one(
&self,
a: &mut Self::Value,
b: Option<<Self::Dtype as PolarsDataType>::Physical<'_>>,
);
fn reduce_ca(&self, v: &mut Self::Value, ca: &ChunkedArray<Self::Dtype>);
fn finish(&self, v: Vec<Self::Value>, m: Option<Bitmap>, dtype: &DataType) -> PolarsResult<Series>;
fn finish(
&self,
v: Vec<Self::Value>,
m: Option<Bitmap>,
dtype: &DataType,
) -> PolarsResult<Series>;
}

pub trait NumericReduction: Send + Sync + 'static {
Expand Down Expand Up @@ -118,7 +127,11 @@ impl<R: NumericReduction> Reducer for NumReducer<R> {
}

#[inline(always)]
fn reduce_one(&self, a: &mut Self::Value, b: Option<<Self::Dtype as PolarsDataType>::Physical<'_>>) {
fn reduce_one(
&self,
a: &mut Self::Value,
b: Option<<Self::Dtype as PolarsDataType>::Physical<'_>>,
) {
if let Some(b) = b {
*a = <R as NumericReduction>::combine(*a, b);
}
Expand All @@ -131,7 +144,12 @@ impl<R: NumericReduction> Reducer for NumReducer<R> {
}
}

fn finish(&self, v: Vec<Self::Value>, m: Option<Bitmap>, dtype: &DataType) -> PolarsResult<Series> {
fn finish(
&self,
v: Vec<Self::Value>,
m: Option<Bitmap>,
dtype: &DataType,
) -> PolarsResult<Series> {
let arr = Box::new(PrimitiveArray::<Self::Value>::from_vec(v).with_validity(m));
Ok(unsafe { Series::from_chunks_and_dtype_unchecked(PlSmallStr::EMPTY, vec![arr], dtype) })
}
Expand Down Expand Up @@ -173,7 +191,8 @@ where
assert!(values.dtype() == &self.in_dtype);
let values = self.reducer.cast_series(values);
let ca: &ChunkedArray<R::Dtype> = values.as_ref().as_ref().as_ref();
self.reducer.reduce_ca(&mut self.values[group_idx as usize], ca);
self.reducer
.reduce_ca(&mut self.values[group_idx as usize], ca);
Ok(())
}

Expand All @@ -196,7 +215,7 @@ where
} else {
let mut offset = 0;
for arr in ca.downcast_iter() {
let subgroup = &group_idxs[offset..offset+arr.len()];
let subgroup = &group_idxs[offset..offset + arr.len()];
for (g, v) in subgroup.iter().zip(arr.values_iter()) {
let grp = self.values.get_unchecked_mut(*g as usize);
self.reducer.reduce_one(grp, Some(v));
Expand Down Expand Up @@ -278,7 +297,8 @@ where
assert!(values.dtype() == &self.in_dtype);
let values = values.to_physical_repr();
let ca: &ChunkedArray<R::Dtype> = values.as_ref().as_ref().as_ref();
self.reducer.reduce_ca(&mut self.values[group_idx as usize], ca);
self.reducer
.reduce_ca(&mut self.values[group_idx as usize], ca);
if ca.len() != ca.null_count() {
self.mask.set(group_idx as usize, true);
}
Expand Down
16 changes: 9 additions & 7 deletions crates/polars-expr/src/reduce/var_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ pub fn new_var_std_reduction(dtype: DataType, is_std: bool, ddof: u8) -> Box<dyn
})
},
#[cfg(feature = "dtype-decimal")]
Decimal(_, _) => Box::new(VGR::new(dtype, VarStdReducer::<Float64Type> {
is_std,
ddof,
needs_cast: true,
_phantom: PhantomData,
})),
Decimal(_, _) => Box::new(VGR::new(
dtype,
VarStdReducer::<Float64Type> {
is_std,
ddof,
needs_cast: true,
_phantom: PhantomData,
},
)),
Duration(..) => todo!(),
_ => unimplemented!(),
}
Expand Down Expand Up @@ -106,7 +109,6 @@ impl<T: PolarsNumericType> Reducer for VarStdReducer<T> {
}
}


#[derive(Clone)]
struct BoolVarStdReducer {
is_std: bool,
Expand Down
3 changes: 1 addition & 2 deletions crates/polars-stream/src/physical_plan/lower_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,7 @@ fn lower_exprs_with_ctx(
| IRAggExpr::Sum(ref mut inner)
| IRAggExpr::Mean(ref mut inner)
| IRAggExpr::Var(ref mut inner, _ /* ddof */)
| IRAggExpr::Std(ref mut inner, _ /* ddof */)
=> {
| IRAggExpr::Std(ref mut inner, _ /* ddof */) => {
let (trans_input, trans_exprs) = lower_exprs_with_ctx(input, &[*inner], ctx)?;
*inner = trans_exprs[0];

Expand Down

0 comments on commit a6159ea

Please sign in to comment.