1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
use super::*;
macro_rules! impl_unit_setter {
($fn_name:ident($field:ident)) => {
#[doc = concat!("Set the ", stringify!($field))]
pub fn $fn_name(mut self, n: Expr) -> Self {
self.$field = n.into();
self
}
};
}
/// Arguments used by `datetime` in order to produce an [`Expr`] of Datetime
///
/// Construct a [`DatetimeArgs`] with `DatetimeArgs::new(y, m, d)`. This will set the other time units to `lit(0)`. You
/// can then set the other fields with the `with_*` methods, or use `with_hms` to set `hour`, `minute`, and `second` all
/// at once.
///
/// # Examples
/// ```
/// use polars_plan::prelude::*;
/// // construct a DatetimeArgs set to July 20, 1969 at 20:17
/// let args = DatetimeArgs::new(lit(1969), lit(7), lit(20)).with_hms(lit(20), lit(17), lit(0));
/// // or
/// let args = DatetimeArgs::new(lit(1969), lit(7), lit(20)).with_hour(lit(20)).with_minute(lit(17));
///
/// // construct a DatetimeArgs using existing columns
/// let args = DatetimeArgs::new(lit(2023), col("month"), col("day"));
/// ```
#[derive(Debug, Clone)]
pub struct DatetimeArgs {
pub year: Expr,
pub month: Expr,
pub day: Expr,
pub hour: Expr,
pub minute: Expr,
pub second: Expr,
pub microsecond: Expr,
pub time_unit: TimeUnit,
pub time_zone: Option<TimeZone>,
pub ambiguous: Expr,
}
impl Default for DatetimeArgs {
fn default() -> Self {
Self {
year: lit(1970),
month: lit(1),
day: lit(1),
hour: lit(0),
minute: lit(0),
second: lit(0),
microsecond: lit(0),
time_unit: TimeUnit::Microseconds,
time_zone: None,
ambiguous: lit(String::from("raise")),
}
}
}
impl DatetimeArgs {
/// Construct a new `DatetimeArgs` set to `year`, `month`, `day`
///
/// Other fields default to `lit(0)`. Use the `with_*` methods to set them.
pub fn new(year: Expr, month: Expr, day: Expr) -> Self {
Self {
year,
month,
day,
..Default::default()
}
}
/// Set `hour`, `minute`, and `second`
///
/// Equivalent to
/// ```ignore
/// self.with_hour(hour)
/// .with_minute(minute)
/// .with_second(second)
/// ```
pub fn with_hms(self, hour: Expr, minute: Expr, second: Expr) -> Self {
Self {
hour,
minute,
second,
..self
}
}
impl_unit_setter!(with_year(year));
impl_unit_setter!(with_month(month));
impl_unit_setter!(with_day(day));
impl_unit_setter!(with_hour(hour));
impl_unit_setter!(with_minute(minute));
impl_unit_setter!(with_second(second));
impl_unit_setter!(with_microsecond(microsecond));
pub fn with_time_unit(self, time_unit: TimeUnit) -> Self {
Self { time_unit, ..self }
}
#[cfg(feature = "timezones")]
pub fn with_time_zone(self, time_zone: Option<TimeZone>) -> Self {
Self { time_zone, ..self }
}
#[cfg(feature = "timezones")]
pub fn with_ambiguous(self, ambiguous: Expr) -> Self {
Self { ambiguous, ..self }
}
}
/// Construct a column of `Datetime` from the provided [`DatetimeArgs`].
#[cfg(feature = "temporal")]
pub fn datetime(args: DatetimeArgs) -> Expr {
let year = args.year;
let month = args.month;
let day = args.day;
let hour = args.hour;
let minute = args.minute;
let second = args.second;
let microsecond = args.microsecond;
let time_unit = args.time_unit;
let time_zone = args.time_zone;
let ambiguous = args.ambiguous;
let input = vec![
year,
month,
day,
hour,
minute,
second,
microsecond,
ambiguous,
];
Expr::Function {
input,
function: FunctionExpr::TemporalExpr(TemporalFunction::DatetimeFunction {
time_unit,
time_zone,
}),
options: FunctionOptions {
collect_groups: ApplyOptions::ElementWise,
allow_rename: true,
input_wildcard_expansion: true,
fmt_str: "datetime",
..Default::default()
},
}
}
/// Arguments used by `duration` in order to produce an [`Expr`] of [`Duration`]
///
/// To construct a [`DurationArgs`], use struct literal syntax with `..Default::default()` to leave unspecified fields at
/// their default value of `lit(0)`, as demonstrated below.
///
/// ```
/// # use polars_plan::prelude::*;
/// let args = DurationArgs {
/// days: lit(5),
/// hours: col("num_hours"),
/// minutes: col("num_minutes"),
/// ..Default::default() // other fields are lit(0)
/// };
/// ```
/// If you prefer builder syntax, `with_*` methods are also available.
/// ```
/// # use polars_plan::prelude::*;
/// let args = DurationArgs::new().with_weeks(lit(42)).with_hours(lit(84));
/// ```
#[derive(Debug, Clone)]
pub struct DurationArgs {
pub weeks: Expr,
pub days: Expr,
pub hours: Expr,
pub minutes: Expr,
pub seconds: Expr,
pub milliseconds: Expr,
pub microseconds: Expr,
pub nanoseconds: Expr,
pub time_unit: TimeUnit,
}
impl Default for DurationArgs {
fn default() -> Self {
Self {
weeks: lit(0),
days: lit(0),
hours: lit(0),
minutes: lit(0),
seconds: lit(0),
milliseconds: lit(0),
microseconds: lit(0),
nanoseconds: lit(0),
time_unit: TimeUnit::Microseconds,
}
}
}
impl DurationArgs {
/// Create a new [`DurationArgs`] with all fields set to `lit(0)`. Use the `with_*` methods to set the fields.
pub fn new() -> Self {
Self::default()
}
/// Set `hours`, `minutes`, and `seconds`
///
/// Equivalent to:
///
/// ```ignore
/// self.with_hours(hours)
/// .with_minutes(minutes)
/// .with_seconds(seconds)
/// ```
pub fn with_hms(self, hours: Expr, minutes: Expr, seconds: Expr) -> Self {
Self {
hours,
minutes,
seconds,
..self
}
}
/// Set `milliseconds`, `microseconds`, and `nanoseconds`
///
/// Equivalent to
/// ```ignore
/// self.with_milliseconds(milliseconds)
/// .with_microseconds(microseconds)
/// .with_nanoseconds(nanoseconds)
/// ```
pub fn with_fractional_seconds(
self,
milliseconds: Expr,
microseconds: Expr,
nanoseconds: Expr,
) -> Self {
Self {
milliseconds,
microseconds,
nanoseconds,
..self
}
}
impl_unit_setter!(with_weeks(weeks));
impl_unit_setter!(with_days(days));
impl_unit_setter!(with_hours(hours));
impl_unit_setter!(with_minutes(minutes));
impl_unit_setter!(with_seconds(seconds));
impl_unit_setter!(with_milliseconds(milliseconds));
impl_unit_setter!(with_microseconds(microseconds));
impl_unit_setter!(with_nanoseconds(nanoseconds));
}
/// Construct a column of [`Duration`] from the provided [`DurationArgs`]
#[cfg(feature = "temporal")]
pub fn duration(args: DurationArgs) -> Expr {
Expr::Function {
input: vec![
args.weeks,
args.days,
args.hours,
args.minutes,
args.seconds,
args.milliseconds,
args.microseconds,
args.nanoseconds,
],
function: FunctionExpr::TemporalExpr(TemporalFunction::Duration(args.time_unit)),
options: FunctionOptions {
collect_groups: ApplyOptions::ElementWise,
input_wildcard_expansion: true,
..Default::default()
},
}
}