Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-plan/src/dsl/function_expr/range.rs
8327 views
1
use std::fmt;
2
3
use polars_core::prelude::*;
4
use polars_ops::series::ClosedInterval;
5
#[cfg(feature = "temporal")]
6
use polars_time::{ClosedWindow, Duration};
7
#[cfg(feature = "serde")]
8
use serde::{Deserialize, Serialize};
9
10
use super::{DataTypeExpr, Expr, FunctionExpr};
11
12
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
13
#[cfg_attr(feature = "dsl-schema", derive(schemars::JsonSchema))]
14
#[cfg(any(feature = "dtype-date", feature = "dtype-datetime"))]
15
#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash)]
16
// Date and Datetime range functions requires three of four optional parameters.
17
// The combination of parameters used determines the dtype and usage of the input expressions.
18
pub enum DateRangeArgs {
19
StartEndInterval,
20
StartEndSamples,
21
StartIntervalSamples,
22
EndIntervalSamples,
23
}
24
25
impl DateRangeArgs {
26
pub fn parse(
27
start: Option<Expr>,
28
end: Option<Expr>,
29
interval: Option<Duration>,
30
num_samples: Option<Expr>,
31
) -> PolarsResult<(Vec<Expr>, DateRangeArgs)> {
32
match (start, end, interval, num_samples) {
33
(Some(start), Some(end), Some(_), None) => {
34
Ok((vec![start, end], DateRangeArgs::StartEndInterval))
35
},
36
(Some(start), Some(end), None, Some(num_samples)) => Ok((
37
vec![start, end, num_samples],
38
DateRangeArgs::StartEndSamples,
39
)),
40
(Some(start), None, Some(_), Some(num_samples)) => Ok((
41
vec![start, num_samples],
42
DateRangeArgs::StartIntervalSamples,
43
)),
44
(None, Some(end), Some(_), Some(num_samples)) => {
45
Ok((vec![end, num_samples], DateRangeArgs::EndIntervalSamples))
46
},
47
_ => {
48
polars_bail!(InvalidOperation: "Exactly three of 'start', 'end', 'interval', and 'num_samples' must be supplied.");
49
},
50
}
51
}
52
}
53
54
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
55
#[cfg_attr(feature = "dsl-schema", derive(schemars::JsonSchema))]
56
#[derive(Clone, PartialEq, Debug, Hash)]
57
pub enum RangeFunction {
58
IntRange {
59
step: i64,
60
dtype: DataTypeExpr,
61
},
62
IntRanges {
63
dtype: DataTypeExpr,
64
},
65
LinearSpace {
66
closed: ClosedInterval,
67
},
68
LinearSpaces {
69
closed: ClosedInterval,
70
array_width: Option<usize>,
71
},
72
#[cfg(feature = "dtype-date")]
73
DateRange {
74
interval: Option<Duration>,
75
closed: ClosedWindow,
76
arg_type: DateRangeArgs,
77
},
78
#[cfg(feature = "dtype-date")]
79
DateRanges {
80
interval: Option<Duration>,
81
closed: ClosedWindow,
82
arg_type: DateRangeArgs,
83
},
84
#[cfg(feature = "dtype-datetime")]
85
DatetimeRange {
86
interval: Option<Duration>,
87
closed: ClosedWindow,
88
time_unit: Option<TimeUnit>,
89
time_zone: Option<TimeZone>,
90
arg_type: DateRangeArgs,
91
},
92
#[cfg(feature = "dtype-datetime")]
93
DatetimeRanges {
94
interval: Option<Duration>,
95
closed: ClosedWindow,
96
time_unit: Option<TimeUnit>,
97
time_zone: Option<TimeZone>,
98
arg_type: DateRangeArgs,
99
},
100
#[cfg(feature = "dtype-time")]
101
TimeRange {
102
interval: Duration,
103
closed: ClosedWindow,
104
},
105
#[cfg(feature = "dtype-time")]
106
TimeRanges {
107
interval: Duration,
108
closed: ClosedWindow,
109
},
110
}
111
112
impl From<RangeFunction> for FunctionExpr {
113
fn from(value: RangeFunction) -> Self {
114
Self::Range(value)
115
}
116
}
117
118
impl fmt::Display for RangeFunction {
119
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
120
use RangeFunction::*;
121
let s = match self {
122
IntRange { .. } => "int_range",
123
IntRanges { .. } => "int_ranges",
124
LinearSpace { .. } => "linear_space",
125
LinearSpaces { .. } => "linear_spaces",
126
#[cfg(feature = "dtype-date")]
127
DateRange { .. } => "date_range",
128
#[cfg(feature = "dtype-date")]
129
DateRanges { .. } => "date_ranges",
130
#[cfg(feature = "dtype-datetime")]
131
DatetimeRange { .. } => "datetime_range",
132
#[cfg(feature = "dtype-datetime")]
133
DatetimeRanges { .. } => "datetime_ranges",
134
#[cfg(feature = "dtype-time")]
135
TimeRange { .. } => "time_range",
136
#[cfg(feature = "dtype-time")]
137
TimeRanges { .. } => "time_ranges",
138
};
139
write!(f, "{s}")
140
}
141
}
142
143