Skip to content

Commit

Permalink
feat: Support decimal groupby (#15000)
Browse files Browse the repository at this point in the history
  • Loading branch information
flisky authored Mar 13, 2024
1 parent edd267e commit bc3acf3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
8 changes: 8 additions & 0 deletions crates/polars-core/src/frame/group_by/into_groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ where
};
num_groups_proxy(ca, multithreaded, sorted)
},
#[cfg(feature = "dtype-decimal")]
DataType::Decimal(_, _) => {
// convince the compiler that we are this type.
let ca: &Int128Chunked = unsafe {
&*(self as *const ChunkedArray<T> as *const ChunkedArray<Int128Type>)
};
num_groups_proxy(ca, multithreaded, sorted)
},
#[cfg(all(feature = "performant", feature = "dtype-i8", feature = "dtype-u8"))]
DataType::Int8 => {
// convince the compiler that we are this type.
Expand Down
4 changes: 4 additions & 0 deletions crates/polars-core/src/series/implementations/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ impl private::PrivateSeries for SeriesWrap<DecimalChunked> {
let rhs = rhs.decimal()?;
((&self.0) / rhs).map(|ca| ca.into_series())
}
#[cfg(feature = "algorithm_group_by")]
fn group_tuples(&self, multithreaded: bool, sorted: bool) -> PolarsResult<GroupsProxy> {
self.0.group_tuples(multithreaded, sorted)
}
}

impl SeriesTrait for SeriesWrap<DecimalChunked> {
Expand Down
8 changes: 8 additions & 0 deletions crates/polars-utils/src/hashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ impl_hash_partition_as_u64!(i16);
impl_hash_partition_as_u64!(i32);
impl_hash_partition_as_u64!(i64);

impl DirtyHash for i128 {
fn dirty_hash(&self) -> u64 {
(*self as u64)
.wrapping_mul(RANDOM_ODD)
.wrapping_add((*self >> 64) as u64)
}
}

impl<'a> DirtyHash for BytesHash<'a> {
fn dirty_hash(&self) -> u64 {
self.hash
Expand Down
7 changes: 4 additions & 3 deletions crates/polars/tests/it/lazy/group_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,19 @@ fn test_group_by_agg_list_with_not_aggregated() -> PolarsResult<()> {
}

#[test]
#[cfg(all(feature = "dtype-duration", feature = "dtype-struct"))]
#[cfg(all(feature = "dtype-duration", feature = "dtype-decimal"))]
fn test_logical_mean_partitioned_group_by_block() -> PolarsResult<()> {
let _guard = SINGLE_LOCK.lock();
let df = df![
"a" => [1, 1, 2],
"decimal" => [1, 1, 2],
"duration" => [1000, 2000, 3000]
]?;

let out = df
.lazy()
.with_column(col("decimal").cast(DataType::Decimal(None, Some(2))))
.with_column(col("duration").cast(DataType::Duration(TimeUnit::Microseconds)))
.group_by([col("a")])
.group_by([col("decimal")])
.agg([col("duration").mean()])
.sort("duration", Default::default())
.collect()?;
Expand Down

0 comments on commit bc3acf3

Please sign in to comment.