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

chore(rust): Rename is_numeric to is_primitive_numeric #20574

Merged
merged 6 commits into from
Jan 7, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub(crate) fn get_fixed_size_list_builder(
) -> PolarsResult<Box<dyn FixedSizeListBuilder>> {
let phys_dtype = inner_type_logical.to_physical();

let builder = if phys_dtype.is_numeric() {
let builder = if phys_dtype.is_primitive_numeric() {
with_match_physical_numeric_type!(phys_dtype, |$T| {
// SAFETY: physical type match logical type
unsafe {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ where
inner_type: DataType,
) -> Self {
debug_assert!(
inner_type.to_physical().is_numeric(),
inner_type.to_physical().is_primitive_numeric(),
"inner type must be primitive, got {}",
inner_type
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ impl LogicalType for CategoricalChunked {
// Otherwise we do nothing
Ok(self.clone().set_ordering(*ordering, true).into_series())
},
dt if dt.is_numeric() => {
dt if dt.is_primitive_numeric() => {
// Apply the cast to the categories and then index into the casted series.
// This has to be local for the gather.
let slf = self.to_local();
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/chunked_array/logical/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl LogicalType for DateChunked {
.into_datetime(*tu, tz.clone())
.into_series())
},
dt if dt.is_numeric() => self.0.cast_with_options(dtype, cast_options),
dt if dt.is_primitive_numeric() => self.0.cast_with_options(dtype, cast_options),
dt => {
polars_bail!(
InvalidOperation:
Expand Down
4 changes: 3 additions & 1 deletion crates/polars-core/src/chunked_array/logical/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ impl LogicalType for DatetimeChunked {
.into_time()
.into_series());
},
dt if dt.is_numeric() => return self.0.cast_with_options(dtype, cast_options),
dt if dt.is_primitive_numeric() => {
return self.0.cast_with_options(dtype, cast_options)
},
dt => {
polars_bail!(
InvalidOperation:
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/chunked_array/logical/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl LogicalType for DurationChunked {
};
Ok(out.into_duration(to_unit).into_series())
},
dt if dt.is_numeric() => self.0.cast_with_options(dtype, cast_options),
dt if dt.is_primitive_numeric() => self.0.cast_with_options(dtype, cast_options),
dt => {
polars_bail!(
InvalidOperation:
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/chunked_array/logical/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl LogicalType for TimeChunked {
self.dtype(), dtype
)
},
dt if dt.is_numeric() => self.0.cast_with_options(dtype, cast_options),
dt if dt.is_primitive_numeric() => self.0.cast_with_options(dtype, cast_options),
_ => {
polars_bail!(
InvalidOperation:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ impl ChunkExplode for ListChunked {
let (indices, new_offsets) = if listarr.null_count() == 0 {
// SPECIALIZED path.
let inner_phys = self.inner_dtype().to_physical();
if inner_phys.is_numeric() || inner_phys.is_null() || inner_phys.is_bool() {
if inner_phys.is_primitive_numeric() || inner_phys.is_null() || inner_phys.is_bool()
{
return Ok(self.specialized(values, offsets, offsets_buf));
}
// Use gather
Expand Down
6 changes: 3 additions & 3 deletions crates/polars-core/src/chunked_array/ops/fill_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ impl Series {
let physical_type = self.dtype().to_physical();

match strategy {
FillNullStrategy::Forward(None) if !physical_type.is_numeric() => {
FillNullStrategy::Forward(None) if !physical_type.is_primitive_numeric() => {
fill_forward_gather(self)
},
FillNullStrategy::Forward(Some(limit)) => fill_forward_gather_limit(self, limit),
FillNullStrategy::Backward(None) if !physical_type.is_numeric() => {
FillNullStrategy::Backward(None) if !physical_type.is_primitive_numeric() => {
fill_backward_gather(self)
},
FillNullStrategy::Backward(Some(limit)) => fill_backward_gather_limit(self, limit),
Expand All @@ -108,7 +108,7 @@ impl Series {
let ca = s.binary().unwrap();
fill_null_binary(ca, strategy).map(|ca| ca.into_series())
},
dt if dt.is_numeric() => {
dt if dt.is_primitive_numeric() => {
with_match_physical_numeric_polars_type!(dt, |$T| {
let ca: &ChunkedArray<$T> = s.as_ref().as_ref().as_ref();
fill_null_numeric(ca, strategy).map(|ca| ca.into_series())
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/chunked_array/ops/reverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl ChunkReverse for StringChunked {
#[cfg(feature = "dtype-array")]
impl ChunkReverse for ArrayChunked {
fn reverse(&self) -> Self {
if !self.inner_dtype().is_numeric() {
if !self.inner_dtype().is_primitive_numeric() {
todo!("reverse for FixedSizeList with non-numeric dtypes not yet supported")
}
let ca = self.rechunk();
Expand Down
16 changes: 10 additions & 6 deletions crates/polars-core/src/datatypes/any_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ impl AnyValue<'static> {
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 {
d if d.is_primitive_numeric() => unsafe {
std::mem::transmute::<AnyValue<'_>, AnyValue<'static>>(
AnyValue::UInt8(0).cast(dtype),
)
Expand Down Expand Up @@ -503,7 +503,7 @@ impl<'a> AnyValue<'a> {
matches!(self, AnyValue::Boolean(_))
}

pub fn is_numeric(&self) -> bool {
pub fn is_primitive_numeric(&self) -> bool {
self.is_integer() || self.is_float()
}

Expand Down Expand Up @@ -611,7 +611,7 @@ impl<'a> AnyValue<'a> {

// to datetime
#[cfg(feature = "dtype-datetime")]
(av, DataType::Datetime(tu, tz)) if av.is_numeric() => {
(av, DataType::Datetime(tu, tz)) if av.is_primitive_numeric() => {
AnyValue::Datetime(av.extract::<i64>()?, *tu, tz.as_ref())
},
#[cfg(all(feature = "dtype-datetime", feature = "dtype-date"))]
Expand Down Expand Up @@ -644,7 +644,9 @@ impl<'a> AnyValue<'a> {

// to date
#[cfg(feature = "dtype-date")]
(av, DataType::Date) if av.is_numeric() => AnyValue::Date(av.extract::<i32>()?),
(av, DataType::Date) if av.is_primitive_numeric() => {
AnyValue::Date(av.extract::<i32>()?)
},
#[cfg(all(feature = "dtype-date", feature = "dtype-datetime"))]
(AnyValue::Datetime(v, tu, _) | AnyValue::DatetimeOwned(v, tu, _), DataType::Date) => {
AnyValue::Date(match tu {
Expand All @@ -656,7 +658,9 @@ impl<'a> AnyValue<'a> {

// to time
#[cfg(feature = "dtype-time")]
(av, DataType::Time) if av.is_numeric() => AnyValue::Time(av.extract::<i64>()?),
(av, DataType::Time) if av.is_primitive_numeric() => {
AnyValue::Time(av.extract::<i64>()?)
},
#[cfg(all(feature = "dtype-time", feature = "dtype-datetime"))]
(AnyValue::Datetime(v, tu, _) | AnyValue::DatetimeOwned(v, tu, _), DataType::Time) => {
AnyValue::Time(match tu {
Expand All @@ -668,7 +672,7 @@ impl<'a> AnyValue<'a> {

// to duration
#[cfg(feature = "dtype-duration")]
(av, DataType::Duration(tu)) if av.is_numeric() => {
(av, DataType::Duration(tu)) if av.is_primitive_numeric() => {
AnyValue::Duration(av.extract::<i64>()?, *tu)
},
#[cfg(all(feature = "dtype-duration", feature = "dtype-time"))]
Expand Down
16 changes: 8 additions & 8 deletions crates/polars-core/src/datatypes/dtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ impl DataType {
if self == to {
return Some(true);
}
if self.is_numeric() && to.is_numeric() {
if self.is_primitive_numeric() && to.is_primitive_numeric() {
return Some(true);
}

Expand All @@ -376,7 +376,7 @@ impl DataType {
(D::Object(_, _), _) | (_, D::Object(_, _)) => false,

(D::Boolean, dt) | (dt, D::Boolean) => match dt {
dt if dt.is_numeric() => true,
dt if dt.is_primitive_numeric() => true,
#[cfg(feature = "dtype-decimal")]
D::Decimal(_, _) => true,
D::String | D::Binary => true,
Expand Down Expand Up @@ -445,7 +445,7 @@ impl DataType {
}

pub fn is_supported_list_arithmetic_input(&self) -> bool {
self.is_numeric() || self.is_bool() || self.is_null()
self.is_primitive_numeric() || self.is_bool() || self.is_null()
}

/// Check if this [`DataType`] is a logical type
Expand All @@ -460,17 +460,17 @@ impl DataType {
}

/// Check if datatype is a primitive type. By that we mean that
/// it is not a container type.
/// it is not a nested or logical type.
pub fn is_primitive(&self) -> bool {
self.is_numeric()
self.is_primitive_numeric()
| matches!(
self,
DataType::Boolean | DataType::String | DataType::Binary
)
}

/// Check if this [`DataType`] is a basic numeric type (excludes Decimal).
pub fn is_numeric(&self) -> bool {
/// Check if this [`DataType`] is a primitive numeric type (excludes Decimal).
pub fn is_primitive_numeric(&self) -> bool {
self.is_float() || self.is_integer()
}

Expand Down Expand Up @@ -588,7 +588,7 @@ impl DataType {
let is_cat = false;

let phys = self.to_physical();
(phys.is_numeric()
(phys.is_primitive_numeric()
|| self.is_decimal()
|| matches!(
phys,
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ impl Display for DataFrame {
for (column_index, column) in table.column_iter_mut().enumerate() {
let dtype = fields[column_index].dtype();
let mut preset = str_preset.as_str();
if dtype.is_numeric() || dtype.is_decimal() {
if dtype.is_primitive_numeric() || dtype.is_decimal() {
preset = num_preset.as_str();
}
match preset {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl Series {
Boolean => s.cast(&Float64).unwrap().agg_mean(groups),
Float32 => SeriesWrap(s.f32().unwrap().clone()).agg_mean(groups),
Float64 => SeriesWrap(s.f64().unwrap().clone()).agg_mean(groups),
dt if dt.is_numeric() => apply_method_physical_integer!(s, agg_mean, groups),
dt if dt.is_primitive_numeric() => apply_method_physical_integer!(s, agg_mean, groups),
#[cfg(feature = "dtype-datetime")]
dt @ Datetime(_, _) => self
.to_physical_repr()
Expand Down Expand Up @@ -193,7 +193,9 @@ impl Series {
Boolean => s.cast(&Float64).unwrap().agg_median(groups),
Float32 => SeriesWrap(s.f32().unwrap().clone()).agg_median(groups),
Float64 => SeriesWrap(s.f64().unwrap().clone()).agg_median(groups),
dt if dt.is_numeric() => apply_method_physical_integer!(s, agg_median, groups),
dt if dt.is_primitive_numeric() => {
apply_method_physical_integer!(s, agg_median, groups)
},
#[cfg(feature = "dtype-datetime")]
dt @ Datetime(_, _) => self
.to_physical_repr()
Expand Down Expand Up @@ -249,7 +251,7 @@ impl Series {
match s.dtype() {
Float32 => s.f32().unwrap().agg_quantile(groups, quantile, method),
Float64 => s.f64().unwrap().agg_quantile(groups, quantile, method),
dt if dt.is_numeric() || dt.is_temporal() => {
dt if dt.is_primitive_numeric() || dt.is_temporal() => {
let ca = s.to_physical_repr();
let physical_type = ca.dtype();
let s = apply_method_physical_integer!(ca, agg_quantile, groups, quantile, method);
Expand Down
14 changes: 10 additions & 4 deletions crates/polars-core/src/frame/row/av_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ impl<'a> AnyValueBuffer<'a> {
#[cfg(feature = "dtype-date")]
(Date(builder), AnyValue::Date(v)) => builder.append_value(v),
#[cfg(feature = "dtype-date")]
(Date(builder), val) if val.is_numeric() => builder.append_value(val.extract()?),
(Date(builder), val) if val.is_primitive_numeric() => {
builder.append_value(val.extract()?)
},
#[cfg(feature = "dtype-datetime")]
(Datetime(builder, _, _), AnyValue::Null) => builder.append_null(),
#[cfg(feature = "dtype-datetime")]
Expand All @@ -100,7 +102,7 @@ impl<'a> AnyValueBuffer<'a> {
builder.append_value(v)
},
#[cfg(feature = "dtype-datetime")]
(Datetime(builder, _, _), val) if val.is_numeric() => {
(Datetime(builder, _, _), val) if val.is_primitive_numeric() => {
builder.append_value(val.extract()?)
},
#[cfg(feature = "dtype-duration")]
Expand All @@ -111,13 +113,17 @@ impl<'a> AnyValueBuffer<'a> {
builder.append_value(v)
},
#[cfg(feature = "dtype-duration")]
(Duration(builder, _), val) if val.is_numeric() => builder.append_value(val.extract()?),
(Duration(builder, _), val) if val.is_primitive_numeric() => {
builder.append_value(val.extract()?)
},
#[cfg(feature = "dtype-time")]
(Time(builder), AnyValue::Time(v)) => builder.append_value(v),
#[cfg(feature = "dtype-time")]
(Time(builder), AnyValue::Null) => builder.append_null(),
#[cfg(feature = "dtype-time")]
(Time(builder), val) if val.is_numeric() => builder.append_value(val.extract()?),
(Time(builder), val) if val.is_primitive_numeric() => {
builder.append_value(val.extract()?)
},
(Null(builder), AnyValue::Null) => builder.append_null(),
// Struct and List can be recursive so use AnyValues for that
(All(_, vals), v) => vals.push(v),
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/scalar/reduce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn mean_reduce(value: Option<f64>, dtype: DataType) -> Scalar {
let val = value.map(|m| m as f32);
Scalar::new(dtype, val.into())
},
dt if dt.is_numeric() || dt.is_decimal() || dt.is_bool() => {
dt if dt.is_primitive_numeric() || dt.is_decimal() || dt.is_bool() => {
Scalar::new(DataType::Float64, value.into())
},
#[cfg(feature = "dtype-date")]
Expand Down
4 changes: 3 additions & 1 deletion crates/polars-core/src/series/arithmetic/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ pub fn coerce_lhs_rhs_owned(lhs: Series, rhs: Series) -> PolarsResult<(Series, S
}

fn is_eligible(lhs: &DataType, rhs: &DataType) -> bool {
!lhs.is_logical() && lhs.to_physical().is_numeric() && rhs.to_physical().is_numeric()
!lhs.is_logical()
&& lhs.to_physical().is_primitive_numeric()
&& rhs.to_physical().is_primitive_numeric()
}

#[cfg(feature = "performant")]
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/series/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ fn validate_types(left: &DataType, right: &DataType) -> PolarsResult<()> {
use DataType::*;

match (left, right) {
(String, dt) | (dt, String) if dt.is_numeric() => {
(String, dt) | (dt, String) if dt.is_primitive_numeric() => {
polars_bail!(ComputeError: "cannot compare string with numeric type ({})", dt)
},
#[cfg(feature = "dtype-categorical")]
Expand Down
6 changes: 3 additions & 3 deletions crates/polars-core/src/series/implementations/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl SeriesTrait for SeriesWrap<ListChunked> {

#[cfg(feature = "algorithm_group_by")]
fn unique(&self) -> PolarsResult<Series> {
if !self.inner_dtype().is_numeric() {
if !self.inner_dtype().is_primitive_numeric() {
polars_bail!(opq = unique, self.dtype());
}
// this can be called in aggregation, so this fast path can be worth a lot
Expand All @@ -200,7 +200,7 @@ impl SeriesTrait for SeriesWrap<ListChunked> {

#[cfg(feature = "algorithm_group_by")]
fn n_unique(&self) -> PolarsResult<usize> {
if !self.inner_dtype().is_numeric() {
if !self.inner_dtype().is_primitive_numeric() {
polars_bail!(opq = n_unique, self.dtype());
}
// this can be called in aggregation, so this fast path can be worth a lot
Expand All @@ -217,7 +217,7 @@ impl SeriesTrait for SeriesWrap<ListChunked> {

#[cfg(feature = "algorithm_group_by")]
fn arg_unique(&self) -> PolarsResult<IdxCa> {
if !self.inner_dtype().is_numeric() {
if !self.inner_dtype().is_primitive_numeric() {
polars_bail!(opq = arg_unique, self.dtype());
}
// this can be called in aggregation, so this fast path can be worth a lot
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/series/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Series {
);
let arr = &*self.chunks()[0];

if phys_dtype.is_numeric() {
if phys_dtype.is_primitive_numeric() {
if arr.null_count() == 0 {
with_match_physical_numeric_type!(phys_dtype, |$T| {
let arr = arr.as_any().downcast_ref::<PrimitiveArray<$T>>().unwrap();
Expand Down
Loading
Loading