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): Correctly support more types in new-streaming sum #18580

Merged
merged 4 commits into from
Sep 6, 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
32 changes: 27 additions & 5 deletions crates/polars-core/src/datatypes/any_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,17 +341,23 @@ impl<'a> Deserialize<'a> for AnyValue<'static> {
}

impl AnyValue<'static> {
pub fn zero(dtype: &DataType) -> Self {
pub fn zero_sum(dtype: &DataType) -> Self {
match dtype {
DataType::String => AnyValue::StringOwned(PlSmallStr::EMPTY),
DataType::Boolean => AnyValue::Boolean(false),
// SAFETY:
// Numeric values are static, inform the compiler of this.
DataType::Binary => AnyValue::BinaryOwned(Vec::new()),
DataType::Boolean => (0 as IdxSize).into(),
// SAFETY: numeric values are static, inform the compiler of this.
d if d.is_numeric() => unsafe {
std::mem::transmute::<AnyValue<'_>, AnyValue<'static>>(
AnyValue::UInt8(0).cast(dtype),
)
},
#[cfg(feature = "dtype-duration")]
DataType::Duration(unit) => AnyValue::Duration(0, *unit),
#[cfg(feature = "dtype-decimal")]
DataType::Decimal(_p, s) => {
AnyValue::Decimal(0, s.expect("unknown scale during execution"))
},
_ => AnyValue::Null,
}
}
Expand Down Expand Up @@ -836,7 +842,23 @@ impl<'a> AnyValue<'a> {
(UInt64(l), UInt64(r)) => UInt64(l + r),
(Float32(l), Float32(r)) => Float32(l + r),
(Float64(l), Float64(r)) => Float64(l + r),
_ => todo!(),
#[cfg(feature = "dtype-duration")]
(Duration(l, lu), Duration(r, ru)) => {
if lu != ru {
unimplemented!("adding durations with different units is not supported here");
}

Duration(l + r, *lu)
},
#[cfg(feature = "dtype-decimal")]
(Decimal(l, ls), Decimal(r, rs)) => {
if ls != rs {
unimplemented!("adding decimals with different scales is not supported here");
}

Decimal(l + r, *ls)
},
_ => unimplemented!(),
}
}

Expand Down
3 changes: 2 additions & 1 deletion crates/polars-expr/src/reduce/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ impl SumReduce {
// returning the empty sum to be consistent.
use DataType::*;
let dtype = match dtype {
Boolean => IDX_DTYPE,
Int8 | UInt8 | Int16 | UInt16 => Int64,
dt => dt,
};
Expand All @@ -22,7 +23,7 @@ impl SumReduce {

impl Reduction for SumReduce {
fn new_reducer(&self) -> Box<dyn ReductionState> {
let value = Scalar::new(self.dtype.clone(), AnyValue::zero(&self.dtype));
let value = Scalar::new(self.dtype.clone(), AnyValue::zero_sum(&self.dtype));
Box::new(SumReduceState { value })
}
}
Expand Down
Loading