Skip to content

Commit

Permalink
feat: enforce deprecation of offset arg in truncate and round
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Jun 1, 2024
1 parent 318ec40 commit ead2e81
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 179 deletions.
9 changes: 4 additions & 5 deletions crates/polars-plan/src/dsl/dt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ impl DateLikeNameSpace {
}

/// Truncate the Datetime/Date range into buckets.
pub fn truncate(self, every: Expr, offset: String) -> Expr {
pub fn truncate(self, every: Expr) -> Expr {
self.0.map_many_private(
FunctionExpr::TemporalExpr(TemporalFunction::Truncate(offset)),
FunctionExpr::TemporalExpr(TemporalFunction::Truncate),
&[every],
false,
false,
Expand Down Expand Up @@ -241,10 +241,9 @@ impl DateLikeNameSpace {
}

/// Round the Datetime/Date range into buckets.
pub fn round<S: AsRef<str>>(self, every: Expr, offset: S) -> Expr {
let offset = offset.as_ref().into();
pub fn round(self, every: Expr) -> Expr {
self.0.map_many_private(
FunctionExpr::TemporalExpr(TemporalFunction::Round(offset)),
FunctionExpr::TemporalExpr(TemporalFunction::Round),
&[every],
false,
false,
Expand Down
36 changes: 14 additions & 22 deletions crates/polars-plan/src/dsl/function_expr/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub enum TemporalFunction {
#[cfg(feature = "timezones")]
ConvertTimeZone(TimeZone),
TimeStamp(TimeUnit),
Truncate(String),
Truncate,
#[cfg(feature = "date_offset")]
MonthStart,
#[cfg(feature = "date_offset")]
Expand All @@ -56,7 +56,7 @@ pub enum TemporalFunction {
BaseUtcOffset,
#[cfg(feature = "timezones")]
DSTOffset,
Round(String),
Round,
#[cfg(feature = "timezones")]
ReplaceTimeZone(Option<TimeZone>, NonExistent),
Combine(TimeUnit),
Expand Down Expand Up @@ -100,7 +100,7 @@ impl TemporalFunction {
DataType::Datetime(tu, _) => Ok(DataType::Datetime(*tu, None)),
dtype => polars_bail!(ComputeError: "expected Datetime, got {}", dtype),
}),
Truncate(_) => mapper.with_same_dtype(),
Truncate => mapper.with_same_dtype(),
#[cfg(feature = "date_offset")]
MonthStart => mapper.with_same_dtype(),
#[cfg(feature = "date_offset")]
Expand All @@ -109,7 +109,7 @@ impl TemporalFunction {
BaseUtcOffset => mapper.with_dtype(DataType::Duration(TimeUnit::Milliseconds)),
#[cfg(feature = "timezones")]
DSTOffset => mapper.with_dtype(DataType::Duration(TimeUnit::Milliseconds)),
Round(..) => mapper.with_same_dtype(),
Round => mapper.with_same_dtype(),
#[cfg(feature = "timezones")]
ReplaceTimeZone(tz, _non_existent) => mapper.map_datetime_dtype_timezone(tz.as_ref()),
DatetimeFunction {
Expand Down Expand Up @@ -168,7 +168,7 @@ impl Display for TemporalFunction {
CastTimeUnit(_) => "cast_time_unit",
WithTimeUnit(_) => "with_time_unit",
TimeStamp(tu) => return write!(f, "dt.timestamp({tu})"),
Truncate(..) => "truncate",
Truncate => "truncate",
#[cfg(feature = "date_offset")]
MonthStart => "month_start",
#[cfg(feature = "date_offset")]
Expand All @@ -177,7 +177,7 @@ impl Display for TemporalFunction {
BaseUtcOffset => "base_utc_offset",
#[cfg(feature = "timezones")]
DSTOffset => "dst_offset",
Round(..) => "round",
Round => "round",
#[cfg(feature = "timezones")]
ReplaceTimeZone(_, _) => "replace_time_zone",
DatetimeFunction { .. } => return write!(f, "dt.datetime"),
Expand Down Expand Up @@ -372,7 +372,7 @@ pub(super) fn cast_time_unit(s: &Series, tu: TimeUnit) -> PolarsResult<Series> {
}
}

pub(super) fn truncate(s: &[Series], offset: &str) -> PolarsResult<Series> {
pub(super) fn truncate(s: &[Series]) -> PolarsResult<Series> {
let time_series = &s[0];
let every = s[1].str()?;

Expand All @@ -381,17 +381,11 @@ pub(super) fn truncate(s: &[Series], offset: &str) -> PolarsResult<Series> {
#[cfg(feature = "timezones")]
Some(tz) => time_series
.datetime()?
.truncate(tz.parse::<Tz>().ok().as_ref(), every, offset)?
.into_series(),
_ => time_series
.datetime()?
.truncate(None, every, offset)?
.truncate(tz.parse::<Tz>().ok().as_ref(), every)?
.into_series(),
_ => time_series.datetime()?.truncate(None, every)?.into_series(),
},
DataType::Date => time_series
.date()?
.truncate(None, every, offset)?
.into_series(),
DataType::Date => time_series.date()?.truncate(None, every)?.into_series(),
dt => polars_bail!(opq = round, got = dt, expected = "date/datetime"),
};
out.set_sorted_flag(time_series.is_sorted_flag());
Expand Down Expand Up @@ -465,9 +459,7 @@ pub(super) fn dst_offset(s: &Series) -> PolarsResult<Series> {
}
}

pub(super) fn round(s: &[Series], offset: &str) -> PolarsResult<Series> {
let offset = Duration::parse(offset);

pub(super) fn round(s: &[Series]) -> PolarsResult<Series> {
let time_series = &s[0];
let every = s[1].str()?;

Expand All @@ -477,18 +469,18 @@ pub(super) fn round(s: &[Series], offset: &str) -> PolarsResult<Series> {
Some(tz) => time_series
.datetime()
.unwrap()
.round(every, offset, tz.parse::<Tz>().ok().as_ref())?
.round(every, tz.parse::<Tz>().ok().as_ref())?
.into_series(),
_ => time_series
.datetime()
.unwrap()
.round(every, offset, None)?
.round(every, None)?
.into_series(),
},
DataType::Date => time_series
.date()
.unwrap()
.round(every, offset, None)?
.round(every, None)?
.into_series(),
dt => polars_bail!(opq = round, got = dt, expected = "date/datetime"),
})
Expand Down
6 changes: 3 additions & 3 deletions crates/polars-plan/src/dsl/function_expr/temporal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ impl From<TemporalFunction> for SpecialEq<Arc<dyn SeriesUdf>> {
ConvertTimeZone(tz) => map!(datetime::convert_time_zone, &tz),
WithTimeUnit(tu) => map!(datetime::with_time_unit, tu),
CastTimeUnit(tu) => map!(datetime::cast_time_unit, tu),
Truncate(offset) => {
map_as_slice!(datetime::truncate, &offset)
Truncate => {
map_as_slice!(datetime::truncate)
},
#[cfg(feature = "date_offset")]
MonthStart => map!(datetime::month_start),
Expand All @@ -57,7 +57,7 @@ impl From<TemporalFunction> for SpecialEq<Arc<dyn SeriesUdf>> {
BaseUtcOffset => map!(datetime::base_utc_offset),
#[cfg(feature = "timezones")]
DSTOffset => map!(datetime::dst_offset),
Round(offset) => map_as_slice!(datetime::round, &offset),
Round => map_as_slice!(datetime::round),
#[cfg(feature = "timezones")]
ReplaceTimeZone(tz, non_existent) => {
map_as_slice!(dispatch::replace_time_zone, tz.as_deref(), non_existent)
Expand Down
20 changes: 5 additions & 15 deletions crates/polars-time/src/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@ use polars_utils::cache::FastFixedCache;
use crate::prelude::*;

pub trait PolarsRound {
fn round(&self, every: &StringChunked, offset: Duration, tz: Option<&Tz>) -> PolarsResult<Self>
fn round(&self, every: &StringChunked, tz: Option<&Tz>) -> PolarsResult<Self>
where
Self: Sized;
}

impl PolarsRound for DatetimeChunked {
fn round(
&self,
every: &StringChunked,
offset: Duration,
tz: Option<&Tz>,
) -> PolarsResult<Self> {
fn round(&self, every: &StringChunked, tz: Option<&Tz>) -> PolarsResult<Self> {
let mut duration_cache = FastFixedCache::new((every.len() as f64).sqrt() as usize);
let out = broadcast_try_binary_elementwise(self, every, |opt_t, opt_every| {
match (opt_t, opt_every) {
Expand All @@ -30,7 +25,7 @@ impl PolarsRound for DatetimeChunked {
polars_bail!(ComputeError: "Cannot round a Datetime to a negative duration")
}

let w = Window::new(every, every, offset);
let w = Window::new(every, every, Duration::parse("0ns"));

let func = match self.time_unit() {
TimeUnit::Nanoseconds => Window::round_ns,
Expand All @@ -47,12 +42,7 @@ impl PolarsRound for DatetimeChunked {
}

impl PolarsRound for DateChunked {
fn round(
&self,
every: &StringChunked,
offset: Duration,
_tz: Option<&Tz>,
) -> PolarsResult<Self> {
fn round(&self, every: &StringChunked, _tz: Option<&Tz>) -> PolarsResult<Self> {
let mut duration_cache = FastFixedCache::new((every.len() as f64).sqrt() as usize);
const MSECS_IN_DAY: i64 = MILLISECONDS * SECONDS_IN_DAY;
let out = broadcast_try_binary_elementwise(&self.0, every, |opt_t, opt_every| {
Expand All @@ -64,7 +54,7 @@ impl PolarsRound for DateChunked {
polars_bail!(ComputeError: "Cannot round a Date to a negative duration")
}

let w = Window::new(every, every, offset);
let w = Window::new(every, every, Duration::parse("0ns"));
Ok(Some(
(w.round_ms(MSECS_IN_DAY * t as i64, None)? / MSECS_IN_DAY) as i32,
))
Expand Down
17 changes: 5 additions & 12 deletions crates/polars-time/src/truncate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ use polars_utils::cache::FastFixedCache;
use crate::prelude::*;

pub trait PolarsTruncate {
fn truncate(&self, tz: Option<&Tz>, every: &StringChunked, offset: &str) -> PolarsResult<Self>
fn truncate(&self, tz: Option<&Tz>, every: &StringChunked) -> PolarsResult<Self>
where
Self: Sized;
}

impl PolarsTruncate for DatetimeChunked {
fn truncate(&self, tz: Option<&Tz>, every: &StringChunked, offset: &str) -> PolarsResult<Self> {
let offset: Duration = Duration::parse(offset);
fn truncate(&self, tz: Option<&Tz>, every: &StringChunked) -> PolarsResult<Self> {
let time_zone = self.time_zone();
let mut duration_cache_opt: Option<FastFixedCache<String, Duration>> = None;

Expand Down Expand Up @@ -82,7 +81,7 @@ impl PolarsTruncate for DatetimeChunked {
polars_bail!(ComputeError: "cannot truncate a Datetime to a negative duration")
}

let w = Window::new(every, every, offset);
let w = Window::new(every, every, Duration::parse("0ns"));
func(&w, timestamp, tz).map(Some)
},
_ => Ok(None),
Expand All @@ -92,13 +91,7 @@ impl PolarsTruncate for DatetimeChunked {
}

impl PolarsTruncate for DateChunked {
fn truncate(
&self,
_tz: Option<&Tz>,
every: &StringChunked,
offset: &str,
) -> PolarsResult<Self> {
let offset = Duration::parse(offset);
fn truncate(&self, _tz: Option<&Tz>, every: &StringChunked) -> PolarsResult<Self> {
// A sqrt(n) cache is not too small, not too large.
let mut duration_cache = FastFixedCache::new((every.len() as f64).sqrt() as usize);
let out = broadcast_try_binary_elementwise(&self.0, every, |opt_t, opt_every| {
Expand All @@ -111,7 +104,7 @@ impl PolarsTruncate for DateChunked {
polars_bail!(ComputeError: "cannot truncate a Date to a negative duration")
}

let w = Window::new(every, every, offset);
let w = Window::new(every, every, Duration::parse("0ns"));
Ok(Some(
(w.truncate_ms(MSECS_IN_DAY * t as i64, None)? / MSECS_IN_DAY) as i32,
))
Expand Down
20 changes: 4 additions & 16 deletions crates/polars-time/src/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,31 +62,16 @@ impl Window {

/// Truncate the given ns timestamp by the window boundary.
pub fn truncate_ns(&self, t: i64, tz: Option<&Tz>) -> PolarsResult<i64> {
let t = self.every.truncate_ns(t, tz)?;
self.offset.add_ns(t, tz)
}

pub fn truncate_no_offset_ns(&self, t: i64, tz: Option<&Tz>) -> PolarsResult<i64> {
self.every.truncate_ns(t, tz)
}

/// Truncate the given us timestamp by the window boundary.
pub fn truncate_us(&self, t: i64, tz: Option<&Tz>) -> PolarsResult<i64> {
let t = self.every.truncate_us(t, tz)?;
self.offset.add_us(t, tz)
}

pub fn truncate_no_offset_us(&self, t: i64, tz: Option<&Tz>) -> PolarsResult<i64> {
self.every.truncate_us(t, tz)
}

/// Truncate the given ms timestamp by the window boundary.
pub fn truncate_ms(&self, t: i64, tz: Option<&Tz>) -> PolarsResult<i64> {
let t = self.every.truncate_ms(t, tz)?;
self.offset.add_ms(t, tz)
}

#[inline]
pub fn truncate_no_offset_ms(&self, t: i64, tz: Option<&Tz>) -> PolarsResult<i64> {
self.every.truncate_ms(t, tz)
}

Expand Down Expand Up @@ -120,6 +105,7 @@ impl Window {
tz: Option<&Tz>,
) -> PolarsResult<Bounds> {
let start = self.truncate_ns(t, tz)?;
let start = self.offset.add_ns(start, tz)?;
ensure_t_in_or_in_front_of_window(
self.every,
t,
Expand All @@ -138,6 +124,7 @@ impl Window {
tz: Option<&Tz>,
) -> PolarsResult<Bounds> {
let start = self.truncate_us(t, tz)?;
let start = self.offset.add_us(start, tz)?;
ensure_t_in_or_in_front_of_window(
self.every,
t,
Expand All @@ -156,6 +143,7 @@ impl Window {
tz: Option<&Tz>,
) -> PolarsResult<Bounds> {
let start = self.truncate_ms(t, tz)?;
let start = self.offset.add_ms(start, tz)?;
ensure_t_in_or_in_front_of_window(
self.every,
t,
Expand Down
Loading

0 comments on commit ead2e81

Please sign in to comment.