Skip to content

Commit

Permalink
perf: Scalar-opt for certain aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
coastalwhite committed Nov 12, 2024
1 parent 632bcde commit 3edaa24
Showing 1 changed file with 56 additions and 18 deletions.
74 changes: 56 additions & 18 deletions crates/polars-core/src/frame/column/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,26 +591,41 @@ impl Column {
/// Does no bounds checks, groups must be correct.
#[cfg(feature = "algorithm_group_by")]
pub unsafe fn agg_min(&self, groups: &GroupsProxy) -> Self {
// @scalar-opt
unsafe { self.as_materialized_series().agg_min(groups) }.into()
match self {
Column::Series(s) => unsafe { s.agg_min(groups) }.into_column(),
Column::Partitioned(s) => {
unsafe { s.as_materialized_series().agg_min(groups) }.into_column()
},
Column::Scalar(s) => s.resize(groups.len()).into_column(),
}
}

/// # Safety
///
/// Does no bounds checks, groups must be correct.
#[cfg(feature = "algorithm_group_by")]
pub unsafe fn agg_max(&self, groups: &GroupsProxy) -> Self {
// @scalar-opt
unsafe { self.as_materialized_series().agg_max(groups) }.into()
match self {
Column::Series(s) => unsafe { s.agg_max(groups) }.into_column(),
Column::Partitioned(s) => {
unsafe { s.as_materialized_series().agg_max(groups) }.into_column()
},
Column::Scalar(s) => s.resize(groups.len()).into_column(),
}
}

/// # Safety
///
/// Does no bounds checks, groups must be correct.
#[cfg(feature = "algorithm_group_by")]
pub unsafe fn agg_mean(&self, groups: &GroupsProxy) -> Self {
// @scalar-opt
unsafe { self.as_materialized_series().agg_mean(groups) }.into()
match self {
Column::Series(s) => unsafe { s.agg_mean(groups) }.into_column(),
Column::Partitioned(s) => {
unsafe { s.as_materialized_series().agg_mean(groups) }.into_column()
},
Column::Scalar(s) => s.resize(groups.len()).into_column(),
}
}

/// # Safety
Expand All @@ -627,17 +642,27 @@ impl Column {
/// Does no bounds checks, groups must be correct.
#[cfg(feature = "algorithm_group_by")]
pub unsafe fn agg_first(&self, groups: &GroupsProxy) -> Self {
// @scalar-opt
unsafe { self.as_materialized_series().agg_first(groups) }.into()
match self {
Column::Series(s) => unsafe { s.agg_first(groups) }.into_column(),
Column::Partitioned(s) => {
unsafe { s.as_materialized_series().agg_first(groups) }.into_column()
},
Column::Scalar(s) => s.resize(groups.len()).into_column(),
}
}

/// # Safety
///
/// Does no bounds checks, groups must be correct.
#[cfg(feature = "algorithm_group_by")]
pub unsafe fn agg_last(&self, groups: &GroupsProxy) -> Self {
// @scalar-opt
unsafe { self.as_materialized_series().agg_last(groups) }.into()
match self {
Column::Series(s) => unsafe { s.agg_last(groups) }.into_column(),
Column::Partitioned(s) => {
unsafe { s.as_materialized_series().agg_last(groups) }.into_column()
},
Column::Scalar(s) => s.resize(groups.len()).into_column(),
}
}

/// # Safety
Expand Down Expand Up @@ -672,8 +697,13 @@ impl Column {
/// Does no bounds checks, groups must be correct.
#[cfg(feature = "algorithm_group_by")]
pub unsafe fn agg_median(&self, groups: &GroupsProxy) -> Self {
// @scalar-opt
unsafe { self.as_materialized_series().agg_median(groups) }.into()
match self {
Column::Series(s) => unsafe { s.agg_median(groups) }.into_column(),
Column::Partitioned(s) => {
unsafe { s.as_materialized_series().agg_median(groups) }.into_column()
},
Column::Scalar(s) => s.resize(groups.len()).into_column(),
}
}

/// # Safety
Expand Down Expand Up @@ -718,18 +748,26 @@ impl Column {
/// Does no bounds checks, groups must be correct.
#[cfg(feature = "algorithm_group_by")]
pub fn agg_and(&self, groups: &GroupsProxy) -> Self {
// @partition-opt
// @scalar-opt
unsafe { self.as_materialized_series().agg_and(groups) }.into()
match self {
Column::Series(s) => unsafe { s.agg_and(groups) }.into_column(),
Column::Partitioned(s) => {
unsafe { s.as_materialized_series().agg_and(groups) }.into_column()
},
Column::Scalar(s) => s.resize(groups.len()).into_column(),
}
}
/// # Safety
///
/// Does no bounds checks, groups must be correct.
#[cfg(feature = "algorithm_group_by")]
pub fn agg_or(&self, groups: &GroupsProxy) -> Self {
// @partition-opt
// @scalar-opt
unsafe { self.as_materialized_series().agg_or(groups) }.into()
match self {
Column::Series(s) => unsafe { s.agg_or(groups) }.into_column(),
Column::Partitioned(s) => {
unsafe { s.as_materialized_series().agg_or(groups) }.into_column()
},
Column::Scalar(s) => s.resize(groups.len()).into_column(),
}
}
/// # Safety
///
Expand Down

0 comments on commit 3edaa24

Please sign in to comment.