Skip to content

Commit

Permalink
chore: remove try_apply_values (#15336)
Browse files Browse the repository at this point in the history
  • Loading branch information
orlp authored Mar 28, 2024
1 parent e1bba07 commit bdea589
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 148 deletions.
100 changes: 0 additions & 100 deletions crates/polars-core/src/chunked_array/ops/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,6 @@ where
ChunkedArray::from_chunk_iter(self.name(), iter)
}

/// Applies a function to all elements, regardless of whether they
/// are null or not, after which the null mask is copied from the
/// original array.
pub fn try_apply_values_generic<'a, U, K, F, E>(
&'a self,
mut op: F,
) -> Result<ChunkedArray<U>, E>
where
U: PolarsDataType,
F: FnMut(T::Physical<'a>) -> Result<K, E>,
U::Array: ArrayFromIter<K>,
{
let iter = self.downcast_iter().map(|arr| {
let element_iter = arr.values_iter().map(&mut op);
let array: U::Array = element_iter.try_collect_arr()?;
Ok(array.with_validity_typed(arr.validity().cloned()))
});

ChunkedArray::try_from_chunk_iter(self.name(), iter)
}

/// Applies a function only to the non-null elements, propagating nulls.
pub fn apply_nonnull_values_generic<'a, U, K, F>(
&'a self,
Expand Down Expand Up @@ -239,22 +218,6 @@ where
ChunkedArray::from_chunk_iter(self.name(), chunks)
}

fn try_apply_values<F>(&'a self, f: F) -> PolarsResult<Self>
where
F: Fn(T::Native) -> PolarsResult<T::Native> + Copy,
{
let mut ca: ChunkedArray<T> = self
.data_views()
.zip(self.iter_validities())
.map(|(slice, validity)| {
let vec: PolarsResult<Vec<_>> = slice.iter().copied().map(f).collect();
Ok((vec?, validity.cloned()))
})
.collect::<PolarsResult<_>>()?;
ca.rename(self.name());
Ok(ca)
}

fn apply<F>(&'a self, f: F) -> Self
where
F: Fn(Option<T::Native>) -> Option<T::Native> + Copy,
Expand Down Expand Up @@ -311,14 +274,6 @@ impl<'a> ChunkApply<'a, bool> for BooleanChunked {
}
}

fn try_apply_values<F>(&self, f: F) -> PolarsResult<Self>
where
F: Fn(bool) -> PolarsResult<bool> + Copy,
{
// Inefficient implementation but never actually used I believe.
self.try_apply_values_generic(f)
}

fn apply<F>(&'a self, f: F) -> Self
where
F: Fn(Option<bool>) -> Option<bool> + Copy,
Expand Down Expand Up @@ -398,13 +353,6 @@ impl<'a> ChunkApply<'a, &'a str> for StringChunked {
ChunkedArray::apply_values_generic(self, f)
}

fn try_apply_values<F>(&'a self, f: F) -> PolarsResult<Self>
where
F: Fn(&'a str) -> PolarsResult<Cow<'a, str>> + Copy,
{
self.try_apply_values_generic(f)
}

fn apply<F>(&'a self, f: F) -> Self
where
F: Fn(Option<&'a str>) -> Option<Cow<'a, str>> + Copy,
Expand Down Expand Up @@ -441,13 +389,6 @@ impl<'a> ChunkApply<'a, &'a [u8]> for BinaryChunked {
self.apply_values_generic(f)
}

fn try_apply_values<F>(&'a self, f: F) -> PolarsResult<Self>
where
F: Fn(&'a [u8]) -> PolarsResult<Cow<'a, [u8]>> + Copy,
{
self.try_apply_values_generic(f)
}

fn apply<F>(&'a self, f: F) -> Self
where
F: Fn(Option<&'a [u8]>) -> Option<Cow<'a, [u8]>> + Copy,
Expand Down Expand Up @@ -572,40 +513,6 @@ impl<'a> ChunkApply<'a, Series> for ListChunked {
ca
}

fn try_apply_values<F>(&'a self, f: F) -> PolarsResult<Self>
where
F: Fn(Series) -> PolarsResult<Series> + Copy,
{
if self.is_empty() {
return Ok(self.clone());
}

let mut fast_explode = true;
let mut function = |s: Series| {
let out = f(s);
if let Ok(out) = &out {
if out.is_empty() {
fast_explode = false;
}
}
out
};
let ca: PolarsResult<ListChunked> = {
if !self.has_validity() {
self.into_no_null_iter().map(&mut function).collect()
} else {
self.into_iter()
.map(|opt_v| opt_v.map(&mut function).transpose())
.collect()
}
};
let mut ca = ca?;
if fast_explode {
ca.set_fast_explode()
}
Ok(ca)
}

fn apply<F>(&'a self, f: F) -> Self
where
F: Fn(Option<Series>) -> Option<Series> + Copy,
Expand Down Expand Up @@ -653,13 +560,6 @@ where
ca
}

fn try_apply_values<F>(&'a self, _f: F) -> PolarsResult<Self>
where
F: Fn(&'a T) -> PolarsResult<T> + Copy,
{
todo!()
}

fn apply<F>(&'a self, f: F) -> Self
where
F: Fn(Option<&'a T>) -> Option<T> + Copy,
Expand Down
5 changes: 0 additions & 5 deletions crates/polars-core/src/chunked_array/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,6 @@ pub trait ChunkApply<'a, T> {
where
F: Fn(T) -> Self::FuncRet + Copy;

fn try_apply_values<F>(&'a self, f: F) -> PolarsResult<Self>
where
F: Fn(T) -> PolarsResult<Self::FuncRet> + Copy,
Self: Sized;

/// Apply a closure elementwise including null values.
#[must_use]
fn apply<F>(&'a self, f: F) -> Self
Expand Down
14 changes: 6 additions & 8 deletions crates/polars-ops/src/chunked_array/binary/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,13 @@ pub trait BinaryNameSpaceImpl: AsBinary {
fn hex_decode(&self, strict: bool) -> PolarsResult<BinaryChunked> {
let ca = self.as_binary();
if strict {
ca.try_apply_values(|s| {
let bytes = hex::decode(s).map_err(|_| {
ca.try_apply_nonnull_values_generic(|s| {
hex::decode(s).map_err(|_| {
polars_err!(
ComputeError:
"invalid `hex` encoding found; try setting `strict=false` to ignore"
)
})?;
Ok(bytes.into())
})
})
} else {
Ok(ca.apply(|opt_s| opt_s.and_then(|s| hex::decode(s).ok().map(Cow::Owned))))
Expand All @@ -101,14 +100,13 @@ pub trait BinaryNameSpaceImpl: AsBinary {
fn base64_decode(&self, strict: bool) -> PolarsResult<BinaryChunked> {
let ca = self.as_binary();
if strict {
ca.try_apply_values(|s| {
let bytes = general_purpose::STANDARD.decode(s).map_err(|_e| {
ca.try_apply_nonnull_values_generic(|s| {
general_purpose::STANDARD.decode(s).map_err(|_e| {
polars_err!(
ComputeError:
"invalid `base64` encoding found; try setting `strict=false` to ignore"
)
})?;
Ok(bytes.into())
})
})
} else {
Ok(ca.apply(|opt_s| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub fn impl_replace_time_zone_fast(
to_tz: &chrono_tz::Tz,
) -> PolarsResult<Int64Chunked> {
match ambiguous {
Some(ambiguous) => datetime.0.try_apply_values(|timestamp| {
Some(ambiguous) => datetime.0.try_apply_nonnull_values_generic(|timestamp| {
let ndt = timestamp_to_datetime(timestamp);
Ok(datetime_to_timestamp(
convert_to_naive_local(
Expand Down
8 changes: 4 additions & 4 deletions crates/polars-plan/src/dsl/function_expr/temporal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,15 @@ fn apply_offsets_to_datetime(
) -> PolarsResult<Int64Chunked> {
match (datetime.len(), offsets.len()) {
(1, _) => match datetime.0.get(0) {
Some(dt) => offsets.try_apply_values_generic(|offset| {
Some(dt) => offsets.try_apply_nonnull_values_generic(|offset| {
offset_fn(&Duration::parse(offset), dt, time_zone)
}),
_ => Ok(Int64Chunked::full_null(datetime.0.name(), offsets.len())),
},
(_, 1) => match offsets.get(0) {
Some(offset) => datetime
.0
.try_apply_values(|v| offset_fn(&Duration::parse(offset), v, time_zone)),
Some(offset) => datetime.0.try_apply_nonnull_values_generic(|v| {
offset_fn(&Duration::parse(offset), v, time_zone)
}),
_ => Ok(datetime.0.apply(|_| None)),
},
_ => try_binary_elementwise(datetime, offsets, |timestamp_opt, offset_opt| {
Expand Down
25 changes: 12 additions & 13 deletions crates/polars-time/src/month_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl PolarsMonthEnd for DatetimeChunked {
};
Ok(self
.0
.try_apply_values(|t| {
.try_apply_nonnull_values_generic(|t| {
roll_forward(
t,
time_zone,
Expand All @@ -68,17 +68,16 @@ impl PolarsMonthEnd for DatetimeChunked {
impl PolarsMonthEnd for DateChunked {
fn month_end(&self, _time_zone: Option<&Tz>) -> PolarsResult<Self> {
const MSECS_IN_DAY: i64 = MILLISECONDS * SECONDS_IN_DAY;
Ok(self
.0
.try_apply_values(|t| {
Ok((roll_forward(
MSECS_IN_DAY * t as i64,
None,
timestamp_ms_to_datetime,
datetime_to_timestamp_ms,
Duration::add_ms,
)? / MSECS_IN_DAY) as i32)
})?
.into_date())
let ret = self.0.try_apply_nonnull_values_generic(|t| {
let fwd = roll_forward(
MSECS_IN_DAY * t as i64,
None,
timestamp_ms_to_datetime,
datetime_to_timestamp_ms,
Duration::add_ms,
)?;
PolarsResult::Ok((fwd / MSECS_IN_DAY) as i32)
})?;
Ok(ret.into_date())
}
}
23 changes: 11 additions & 12 deletions crates/polars-time/src/month_start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl PolarsMonthStart for DatetimeChunked {
};
Ok(self
.0
.try_apply_values(|t| {
.try_apply_nonnull_values_generic(|t| {
roll_backward(t, tz, timestamp_to_datetime, datetime_to_timestamp)
})?
.into_datetime(self.time_unit(), self.time_zone().clone()))
Expand All @@ -92,16 +92,15 @@ impl PolarsMonthStart for DatetimeChunked {
impl PolarsMonthStart for DateChunked {
fn month_start(&self, _tz: Option<&Tz>) -> PolarsResult<Self> {
const MSECS_IN_DAY: i64 = MILLISECONDS * SECONDS_IN_DAY;
Ok(self
.0
.try_apply_values(|t| {
Ok((roll_backward(
MSECS_IN_DAY * t as i64,
None,
timestamp_ms_to_datetime,
datetime_to_timestamp_ms,
)? / MSECS_IN_DAY) as i32)
})?
.into_date())
let ret = self.0.try_apply_nonnull_values_generic(|t| {
let bwd = roll_backward(
MSECS_IN_DAY * t as i64,
None,
timestamp_ms_to_datetime,
datetime_to_timestamp_ms,
)?;
PolarsResult::Ok((bwd / MSECS_IN_DAY) as i32)
})?;
Ok(ret.into_date())
}
}
6 changes: 3 additions & 3 deletions crates/polars-time/src/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl PolarsRound for DatetimeChunked {
TimeUnit::Milliseconds => Window::round_ms,
};

let out = { self.try_apply_values(|t| func(&w, t, tz)) };
let out = { self.try_apply_nonnull_values_generic(|t| func(&w, t, tz)) };
out.map(|ok| ok.into_datetime(self.time_unit(), self.time_zone().clone()))
}
}
Expand All @@ -37,9 +37,9 @@ impl PolarsRound for DateChunked {

let w = Window::new(every, every, offset);
Ok(self
.try_apply_values(|t| {
.try_apply_nonnull_values_generic(|t| {
const MSECS_IN_DAY: i64 = MILLISECONDS * SECONDS_IN_DAY;
Ok((w.round_ms(MSECS_IN_DAY * t as i64, None)? / MSECS_IN_DAY) as i32)
PolarsResult::Ok((w.round_ms(MSECS_IN_DAY * t as i64, None)? / MSECS_IN_DAY) as i32)
})?
.into_date())
}
Expand Down
5 changes: 3 additions & 2 deletions crates/polars-time/src/truncate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ impl PolarsTruncate for DatetimeChunked {
}

let w = Window::new(every, every, offset);
self.0.try_apply_values(|timestamp| func(&w, timestamp, tz))
self.0
.try_apply_nonnull_values_generic(|timestamp| func(&w, timestamp, tz))
} else {
Ok(Int64Chunked::full_null(self.name(), self.len()))
}
Expand Down Expand Up @@ -71,7 +72,7 @@ impl PolarsTruncate for DateChunked {
}

let w = Window::new(every, every, offset);
self.try_apply_values(|t| {
self.try_apply_nonnull_values_generic(|t| {
const MSECS_IN_DAY: i64 = MILLISECONDS * SECONDS_IN_DAY;
Ok((w.truncate_ms(MSECS_IN_DAY * t as i64, None)? / MSECS_IN_DAY) as i32)
})
Expand Down

0 comments on commit bdea589

Please sign in to comment.