Skip to content

Commit

Permalink
clippy and format
Browse files Browse the repository at this point in the history
  • Loading branch information
coastalwhite committed Sep 11, 2024
1 parent d2f5f5f commit c7a070f
Show file tree
Hide file tree
Showing 42 changed files with 321 additions and 132 deletions.
64 changes: 58 additions & 6 deletions crates/polars-core/src/frame/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Column {
#[inline]
pub fn new_empty(name: PlSmallStr, dtype: &DataType) -> Self {
// @scalar-opt
Self::Series(Series::new_empty(name, &dtype))
Self::Series(Series::new_empty(name, dtype))
}

#[inline]
Expand Down Expand Up @@ -266,41 +266,69 @@ impl Column {
self.as_materialized_series().null_count()
}

/// # Safety
///
/// 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()
}

/// # 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()
}

/// # 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()
}

/// # Safety
///
/// Does no bounds checks, groups must be correct.
#[cfg(feature = "algorithm_group_by")]
pub unsafe fn agg_sum(&self, groups: &GroupsProxy) -> Self {
// @scalar-opt
unsafe { self.as_materialized_series().agg_sum(groups) }.into()
}

/// # Safety
///
/// Does no bounds checks, groups must be correct.
pub unsafe fn agg_first(&self, groups: &GroupsProxy) -> Self {
// @scalar-opt
unsafe { self.as_materialized_series().agg_first(groups) }.into()
}

/// # Safety
///
/// Does no bounds checks, groups must be correct.
pub unsafe fn agg_last(&self, groups: &GroupsProxy) -> Self {
// @scalar-opt
unsafe { self.as_materialized_series().agg_last(groups) }.into()
}

/// # Safety
///
/// Does no bounds checks, groups must be correct.
pub unsafe fn agg_n_unique(&self, groups: &GroupsProxy) -> Self {
// @scalar-opt
unsafe { self.as_materialized_series().agg_n_unique(groups) }.into()
}

/// # Safety
///
/// Does no bounds checks, groups must be correct.
pub unsafe fn agg_quantile(
&self,
groups: &GroupsProxy,
Expand All @@ -315,21 +343,37 @@ impl Column {
.into()
}

/// # Safety
///
/// 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()
}

/// # Safety
///
/// Does no bounds checks, groups must be correct.
#[cfg(feature = "algorithm_group_by")]
pub unsafe fn agg_var(&self, groups: &GroupsProxy, ddof: u8) -> Self {
// @scalar-opt
unsafe { self.as_materialized_series().agg_var(groups, ddof) }.into()
}

pub unsafe fn agg_std(&self, groups: &GroupsProxy, ddof: u8) -> Self {
/// # Safety
///
/// Does no bounds checks, groups must be correct.
#[cfg(feature = "algorithm_group_by")]
pub(crate) unsafe fn agg_std(&self, groups: &GroupsProxy, ddof: u8) -> Self {
// @scalar-opt
unsafe { self.as_materialized_series().agg_std(groups, ddof) }.into()
}

/// # Safety
///
/// Does no bounds checks, groups must be correct.
#[cfg(feature = "algorithm_group_by")]
pub unsafe fn agg_list(&self, groups: &GroupsProxy) -> Self {
// @scalar-opt
unsafe { self.as_materialized_series().agg_list(groups) }.into()
Expand Down Expand Up @@ -398,6 +442,9 @@ impl Column {
.vec_hash_combine(build_hasher, hashes)
}

/// # Safety
///
/// Indexes need to be in bounds.
pub(crate) unsafe fn equal_element(
&self,
idx_self: usize,
Expand Down Expand Up @@ -524,6 +571,9 @@ impl Column {
.map(Column::from)
}

/// # Safety
///
/// This can lead to invalid memory access in downstream code.
pub unsafe fn cast_unchecked(&self, dtype: &DataType) -> PolarsResult<Column> {
// @scalar-opt
unsafe { self.as_materialized_series().cast_unchecked(dtype) }.map(Column::from)
Expand Down Expand Up @@ -709,6 +759,9 @@ impl Column {
self.as_materialized_series().phys_iter()
}

/// # Safety
///
/// Does not perform bounds check on `index`
pub unsafe fn get_unchecked(&self, index: usize) -> AnyValue {
// @scalar-opt
self.as_materialized_series().get_unchecked(index)
Expand Down Expand Up @@ -1008,10 +1061,9 @@ impl From<Column> for _SerdeSeries {
}
}

impl Into<Series> for _SerdeSeries {
impl From<_SerdeSeries> for Series {
#[inline]
fn into(self) -> Series {
self.0
fn from(value: _SerdeSeries) -> Self {
value.0
}
}

28 changes: 18 additions & 10 deletions crates/polars-core/src/frame/group_by/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ impl DataFrame {

let groups = if by.len() == 1 {
let column = &by[0];
column.as_materialized_series().group_tuples(multithreaded, sorted)
column
.as_materialized_series()
.group_tuples(multithreaded, sorted)
} else if by.iter().any(|s| s.dtype().is_object()) {
#[cfg(feature = "object")]
{
Expand Down Expand Up @@ -294,7 +296,7 @@ impl<'df> GroupBy<'df> {
},
}
})
.map(|s| Column::from(s))
.map(Column::from)
.collect()
})
}
Expand Down Expand Up @@ -396,7 +398,7 @@ impl<'df> GroupBy<'df> {
let new_name = fmt_group_by_column(agg_col.name().as_str(), GroupByMethod::Sum);
let mut agg = unsafe { agg_col.agg_sum(&self.groups) };
agg.rename(new_name);
cols.push(agg.into());
cols.push(agg);
}
DataFrame::new(cols)
}
Expand Down Expand Up @@ -433,7 +435,7 @@ impl<'df> GroupBy<'df> {
let new_name = fmt_group_by_column(agg_col.name().as_str(), GroupByMethod::Min);
let mut agg = unsafe { agg_col.agg_min(&self.groups) };
agg.rename(new_name);
cols.push(agg.into());
cols.push(agg);
}
DataFrame::new(cols)
}
Expand Down Expand Up @@ -470,7 +472,7 @@ impl<'df> GroupBy<'df> {
let new_name = fmt_group_by_column(agg_col.name().as_str(), GroupByMethod::Max);
let mut agg = unsafe { agg_col.agg_max(&self.groups) };
agg.rename(new_name);
cols.push(agg.into());
cols.push(agg);
}
DataFrame::new(cols)
}
Expand Down Expand Up @@ -507,7 +509,7 @@ impl<'df> GroupBy<'df> {
let new_name = fmt_group_by_column(agg_col.name().as_str(), GroupByMethod::First);
let mut agg = unsafe { agg_col.agg_first(&self.groups) };
agg.rename(new_name);
cols.push(agg.into());
cols.push(agg);
}
DataFrame::new(cols)
}
Expand Down Expand Up @@ -544,7 +546,7 @@ impl<'df> GroupBy<'df> {
let new_name = fmt_group_by_column(agg_col.name().as_str(), GroupByMethod::Last);
let mut agg = unsafe { agg_col.agg_last(&self.groups) };
agg.rename(new_name);
cols.push(agg.into());
cols.push(agg);
}
DataFrame::new(cols)
}
Expand Down Expand Up @@ -581,7 +583,7 @@ impl<'df> GroupBy<'df> {
let new_name = fmt_group_by_column(agg_col.name().as_str(), GroupByMethod::NUnique);
let mut agg = unsafe { agg_col.agg_n_unique(&self.groups) };
agg.rename(new_name);
cols.push(agg.into());
cols.push(agg);
}
DataFrame::new(cols)
}
Expand Down Expand Up @@ -616,7 +618,7 @@ impl<'df> GroupBy<'df> {
);
let mut agg = unsafe { agg_col.agg_quantile(&self.groups, quantile, interpol) };
agg.rename(new_name);
cols.push(agg.into());
cols.push(agg);
}
DataFrame::new(cols)
}
Expand Down Expand Up @@ -1124,7 +1126,13 @@ mod test {
.unwrap();

assert_eq!(
Vec::from(res.column("bar_sum").unwrap().as_materialized_series().i32().unwrap()),
Vec::from(
res.column("bar_sum")
.unwrap()
.as_materialized_series()
.i32()
.unwrap()
),
&[Some(2), Some(2), Some(1)]
);
}
Expand Down
8 changes: 4 additions & 4 deletions crates/polars-core/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub struct DataFrame {
}

impl DataFrame {
pub fn materialized_column_iter(&self) -> impl Iterator<Item = &Series> + ExactSizeIterator {
pub fn materialized_column_iter(&self) -> impl ExactSizeIterator<Item = &Series> {
self.columns.iter().map(Column::as_materialized_series)
}

Expand Down Expand Up @@ -630,7 +630,7 @@ impl DataFrame {
/// assert_eq!(iterator.next(), None);
/// # Ok::<(), PolarsError>(())
/// ```
pub fn iter(&self) -> impl Iterator<Item = &Series> + ExactSizeIterator {
pub fn iter(&self) -> impl ExactSizeIterator<Item = &Series> {
self.materialized_column_iter()
}

Expand Down Expand Up @@ -2505,8 +2505,8 @@ impl DataFrame {
self.columns[0].clone().as_materialized_series().clone(),
)),
2 => min_fn(
&self.columns[0].as_materialized_series(),
&self.columns[1].as_materialized_series(),
self.columns[0].as_materialized_series(),
self.columns[1].as_materialized_series(),
)
.map(Some),
_ => {
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ pub use crate::datatypes::{ArrayCollectIterExt, *};
pub use crate::error::{
polars_bail, polars_ensure, polars_err, polars_warn, PolarsError, PolarsResult,
};
pub use crate::frame::column::{Column, IntoColumn};
pub use crate::frame::explode::UnpivotArgsIR;
#[cfg(feature = "algorithm_group_by")]
pub(crate) use crate::frame::group_by::aggregations::*;
#[cfg(feature = "algorithm_group_by")]
pub use crate::frame::group_by::*;
pub use crate::frame::{DataFrame, UniqueKeepStrategy};
pub use crate::frame::column::{Column, IntoColumn};
pub use crate::hashing::VecHash;
pub use crate::named_from::{NamedFrom, NamedFromOwned};
pub use crate::scalar::Scalar;
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/scalar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod reduce;
use polars_utils::pl_str::PlSmallStr;

use crate::datatypes::{AnyValue, DataType};
use crate::prelude::{Column, Series, IntoColumn};
use crate::prelude::{Column, IntoColumn, Series};

#[derive(Clone, Debug)]
pub struct Scalar {
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/serde/df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use polars_error::PolarsError;
use serde::de::Error;
use serde::*;

use crate::prelude::{DataFrame, Column};
use crate::prelude::{Column, DataFrame};

// utility to ensure we serde to a struct
// {
Expand Down
1 change: 1 addition & 0 deletions crates/polars-core/src/series/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ impl Series {
/// Cast from physical to logical types without any checks on the validity of the cast.
///
/// # Safety
///
/// This can lead to invalid memory access in downstream code.
pub unsafe fn cast_unchecked(&self, dtype: &DataType) -> PolarsResult<Self> {
match self.dtype() {
Expand Down
16 changes: 16 additions & 0 deletions crates/polars-core/src/series/series_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,17 @@ pub(crate) mod private {
) -> PolarsResult<()> {
polars_bail!(opq = vec_hash_combine, self._dtype());
}

/// # Safety
///
/// Does no bounds checks, groups must be correct.
#[cfg(feature = "algorithm_group_by")]
unsafe fn agg_min(&self, groups: &GroupsProxy) -> Series {
Series::full_null(self._field().name().clone(), groups.len(), self._dtype())
}
/// # Safety
///
/// Does no bounds checks, groups must be correct.
#[cfg(feature = "algorithm_group_by")]
unsafe fn agg_max(&self, groups: &GroupsProxy) -> Series {
Series::full_null(self._field().name().clone(), groups.len(), self._dtype())
Expand All @@ -119,14 +126,23 @@ pub(crate) mod private {
unsafe fn agg_sum(&self, groups: &GroupsProxy) -> Series {
Series::full_null(self._field().name().clone(), groups.len(), self._dtype())
}
/// # Safety
///
/// Does no bounds checks, groups must be correct.
#[cfg(feature = "algorithm_group_by")]
unsafe fn agg_std(&self, groups: &GroupsProxy, _ddof: u8) -> Series {
Series::full_null(self._field().name().clone(), groups.len(), self._dtype())
}
/// # Safety
///
/// Does no bounds checks, groups must be correct.
#[cfg(feature = "algorithm_group_by")]
unsafe fn agg_var(&self, groups: &GroupsProxy, _ddof: u8) -> Series {
Series::full_null(self._field().name().clone(), groups.len(), self._dtype())
}
/// # Safety
///
/// Does no bounds checks, groups must be correct.
#[cfg(feature = "algorithm_group_by")]
unsafe fn agg_list(&self, groups: &GroupsProxy) -> Series {
Series::full_null(self._field().name().clone(), groups.len(), self._dtype())
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-io/src/csv/read/read_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub(crate) fn cast_columns(
(_, dt) => c.cast(dt),
}?;
if !ignore_errors && c.null_count() != out.null_count() {
handle_casting_failures(c.as_materialized_series(), &out.as_materialized_series())?;
handle_casting_failures(c.as_materialized_series(), out.as_materialized_series())?;
}
Ok(out)
};
Expand Down
Loading

0 comments on commit c7a070f

Please sign in to comment.