Skip to main content

hamelin_lib/func/defs/
datetime.rs

1//! DateTime functions
2
3use std::any::TypeId;
4
5use crate::func::def::{FunctionDef, ParameterBindingProvider, Parameters};
6use crate::types::matcher::ExactMatcher;
7use crate::types::{Type, DOUBLE, INT, INTERVAL, STRING, TIMESTAMP};
8
9/// now() - Current timestamp.
10#[derive(Default, Clone)]
11pub struct Now;
12
13impl FunctionDef for Now {
14    fn name(&self) -> &'static str {
15        "now"
16    }
17
18    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
19        Ok(TIMESTAMP)
20    }
21
22    fn type_id(&self) -> TypeId {
23        TypeId::of::<Self>()
24    }
25
26    fn is_deterministic(&self) -> bool {
27        false
28    }
29}
30
31/// today() - Start of current day.
32#[derive(Default, Clone)]
33pub struct Today;
34
35impl FunctionDef for Today {
36    fn name(&self) -> &'static str {
37        "today"
38    }
39
40    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
41        Ok(TIMESTAMP)
42    }
43
44    fn type_id(&self) -> TypeId {
45        TypeId::of::<Self>()
46    }
47
48    fn is_deterministic(&self) -> bool {
49        false
50    }
51}
52
53/// yesterday() - Start of previous day.
54#[derive(Default, Clone)]
55pub struct Yesterday;
56
57impl FunctionDef for Yesterday {
58    fn name(&self) -> &'static str {
59        "yesterday"
60    }
61
62    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
63        Ok(TIMESTAMP)
64    }
65
66    fn type_id(&self) -> TypeId {
67        TypeId::of::<Self>()
68    }
69
70    fn is_deterministic(&self) -> bool {
71        false
72    }
73}
74
75/// tomorrow() - Start of next day.
76#[derive(Default, Clone)]
77pub struct Tomorrow;
78
79impl FunctionDef for Tomorrow {
80    fn name(&self) -> &'static str {
81        "tomorrow"
82    }
83
84    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
85        Ok(TIMESTAMP)
86    }
87
88    fn type_id(&self) -> TypeId {
89        TypeId::of::<Self>()
90    }
91
92    fn is_deterministic(&self) -> bool {
93        false
94    }
95}
96
97/// ts(string) - Parse timestamp from string.
98#[derive(Default, Clone)]
99pub struct Ts;
100
101impl FunctionDef for Ts {
102    fn name(&self) -> &'static str {
103        "ts"
104    }
105
106    fn parameters(&self) -> Parameters {
107        Parameters::new().with("timestamp", ExactMatcher::of(STRING))
108    }
109
110    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
111        Ok(TIMESTAMP)
112    }
113
114    fn type_id(&self) -> TypeId {
115        TypeId::of::<Self>()
116    }
117}
118
119/// year(timestamp) - Extract year from timestamp.
120#[derive(Default, Clone)]
121pub struct Year;
122
123impl FunctionDef for Year {
124    fn name(&self) -> &'static str {
125        "year"
126    }
127
128    fn parameters(&self) -> Parameters {
129        Parameters::new().with("timestamp", ExactMatcher::of(TIMESTAMP))
130    }
131
132    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
133        Ok(INT)
134    }
135
136    fn type_id(&self) -> TypeId {
137        TypeId::of::<Self>()
138    }
139}
140
141/// month(timestamp) - Extract month from timestamp.
142#[derive(Default, Clone)]
143pub struct Month;
144
145impl FunctionDef for Month {
146    fn name(&self) -> &'static str {
147        "month"
148    }
149
150    fn parameters(&self) -> Parameters {
151        Parameters::new().with("timestamp", ExactMatcher::of(TIMESTAMP))
152    }
153
154    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
155        Ok(INT)
156    }
157
158    fn type_id(&self) -> TypeId {
159        TypeId::of::<Self>()
160    }
161}
162
163/// day(timestamp) - Extract day from timestamp.
164#[derive(Default, Clone)]
165pub struct Day;
166
167impl FunctionDef for Day {
168    fn name(&self) -> &'static str {
169        "day"
170    }
171
172    fn parameters(&self) -> Parameters {
173        Parameters::new().with("timestamp", ExactMatcher::of(TIMESTAMP))
174    }
175
176    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
177        Ok(INT)
178    }
179
180    fn type_id(&self) -> TypeId {
181        TypeId::of::<Self>()
182    }
183}
184
185/// day_of_week(timestamp) - Extract day of week from timestamp (1=Sunday, 7=Saturday).
186#[derive(Default, Clone)]
187pub struct DayOfWeek;
188
189impl FunctionDef for DayOfWeek {
190    fn name(&self) -> &'static str {
191        "day_of_week"
192    }
193
194    fn parameters(&self) -> Parameters {
195        Parameters::new().with("timestamp", ExactMatcher::of(TIMESTAMP))
196    }
197
198    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
199        Ok(INT)
200    }
201
202    fn type_id(&self) -> TypeId {
203        TypeId::of::<Self>()
204    }
205}
206
207/// hour(timestamp) - Extract hour from timestamp.
208#[derive(Default, Clone)]
209pub struct Hour;
210
211impl FunctionDef for Hour {
212    fn name(&self) -> &'static str {
213        "hour"
214    }
215
216    fn parameters(&self) -> Parameters {
217        Parameters::new().with("timestamp", ExactMatcher::of(TIMESTAMP))
218    }
219
220    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
221        Ok(INT)
222    }
223
224    fn type_id(&self) -> TypeId {
225        TypeId::of::<Self>()
226    }
227}
228
229/// minute(timestamp) - Extract minute from timestamp.
230#[derive(Default, Clone)]
231pub struct Minute;
232
233impl FunctionDef for Minute {
234    fn name(&self) -> &'static str {
235        "minute"
236    }
237
238    fn parameters(&self) -> Parameters {
239        Parameters::new().with("timestamp", ExactMatcher::of(TIMESTAMP))
240    }
241
242    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
243        Ok(INT)
244    }
245
246    fn type_id(&self) -> TypeId {
247        TypeId::of::<Self>()
248    }
249}
250
251/// second(timestamp) - Extract second from timestamp.
252#[derive(Default, Clone)]
253pub struct Second;
254
255impl FunctionDef for Second {
256    fn name(&self) -> &'static str {
257        "second"
258    }
259
260    fn parameters(&self) -> Parameters {
261        Parameters::new().with("timestamp", ExactMatcher::of(TIMESTAMP))
262    }
263
264    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
265        Ok(INT)
266    }
267
268    fn type_id(&self) -> TypeId {
269        TypeId::of::<Self>()
270    }
271}
272
273/// at_timezone(timestamp, timezone) - Convert timestamp to specified timezone.
274#[derive(Default, Clone)]
275pub struct AtTimezone;
276
277impl FunctionDef for AtTimezone {
278    fn name(&self) -> &'static str {
279        "at_timezone"
280    }
281
282    fn parameters(&self) -> Parameters {
283        Parameters::new()
284            .with("timestamp", ExactMatcher::of(TIMESTAMP))
285            .with("timezone", ExactMatcher::of(STRING))
286    }
287
288    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
289        Ok(TIMESTAMP)
290    }
291
292    fn type_id(&self) -> TypeId {
293        TypeId::of::<Self>()
294    }
295}
296
297/// to_millis(interval) - Convert interval to milliseconds.
298#[derive(Default, Clone)]
299pub struct ToMillis;
300
301impl FunctionDef for ToMillis {
302    fn name(&self) -> &'static str {
303        "to_millis"
304    }
305
306    fn parameters(&self) -> Parameters {
307        Parameters::new().with("interval", ExactMatcher::of(INTERVAL))
308    }
309
310    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
311        Ok(INT)
312    }
313
314    fn type_id(&self) -> TypeId {
315        TypeId::of::<Self>()
316    }
317}
318
319/// to_nanos(interval) - Convert interval to nanoseconds.
320#[derive(Default, Clone)]
321pub struct ToNanos;
322
323impl FunctionDef for ToNanos {
324    fn name(&self) -> &'static str {
325        "to_nanos"
326    }
327
328    fn parameters(&self) -> Parameters {
329        Parameters::new().with("interval", ExactMatcher::of(INTERVAL))
330    }
331
332    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
333        Ok(INT)
334    }
335
336    fn type_id(&self) -> TypeId {
337        TypeId::of::<Self>()
338    }
339}
340
341/// from_millis(millis) - Create interval from milliseconds.
342#[derive(Default, Clone)]
343pub struct FromMillis;
344
345impl FunctionDef for FromMillis {
346    fn name(&self) -> &'static str {
347        "from_millis"
348    }
349
350    fn parameters(&self) -> Parameters {
351        Parameters::new().with("millis", ExactMatcher::of(INT))
352    }
353
354    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
355        Ok(INTERVAL)
356    }
357
358    fn type_id(&self) -> TypeId {
359        TypeId::of::<Self>()
360    }
361}
362
363/// from_nanos(nanos) - Create interval from nanoseconds.
364#[derive(Default, Clone)]
365pub struct FromNanos;
366
367impl FunctionDef for FromNanos {
368    fn name(&self) -> &'static str {
369        "from_nanos"
370    }
371
372    fn parameters(&self) -> Parameters {
373        Parameters::new().with("nanos", ExactMatcher::of(INT))
374    }
375
376    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
377        Ok(INTERVAL)
378    }
379
380    fn type_id(&self) -> TypeId {
381        TypeId::of::<Self>()
382    }
383}
384
385/// from_unixtime_seconds(seconds) - Create timestamp from Unix epoch seconds.
386#[derive(Default, Clone)]
387pub struct FromUnixtimeSeconds;
388
389impl FunctionDef for FromUnixtimeSeconds {
390    fn name(&self) -> &'static str {
391        "from_unixtime_seconds"
392    }
393
394    fn parameters(&self) -> Parameters {
395        Parameters::new().with("seconds", ExactMatcher::of(INT))
396    }
397
398    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
399        Ok(TIMESTAMP)
400    }
401
402    fn type_id(&self) -> TypeId {
403        TypeId::of::<Self>()
404    }
405}
406
407/// from_unixtime_millis(millis) - Create timestamp from Unix epoch milliseconds.
408#[derive(Default, Clone)]
409pub struct FromUnixtimeMillis;
410
411impl FunctionDef for FromUnixtimeMillis {
412    fn name(&self) -> &'static str {
413        "from_unixtime_millis"
414    }
415
416    fn parameters(&self) -> Parameters {
417        Parameters::new().with("millis", ExactMatcher::of(INT))
418    }
419
420    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
421        Ok(TIMESTAMP)
422    }
423
424    fn type_id(&self) -> TypeId {
425        TypeId::of::<Self>()
426    }
427}
428
429/// from_unixtime_micros(micros) - Create timestamp from Unix epoch microseconds.
430#[derive(Default, Clone)]
431pub struct FromUnixtimeMicros;
432
433impl FunctionDef for FromUnixtimeMicros {
434    fn name(&self) -> &'static str {
435        "from_unixtime_micros"
436    }
437
438    fn parameters(&self) -> Parameters {
439        Parameters::new().with("micros", ExactMatcher::of(INT))
440    }
441
442    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
443        Ok(TIMESTAMP)
444    }
445
446    fn type_id(&self) -> TypeId {
447        TypeId::of::<Self>()
448    }
449}
450
451/// from_unixtime_nanos(nanos) - Create timestamp from Unix epoch nanoseconds.
452#[derive(Default, Clone)]
453pub struct FromUnixtimeNanos;
454
455impl FunctionDef for FromUnixtimeNanos {
456    fn name(&self) -> &'static str {
457        "from_unixtime_nanos"
458    }
459
460    fn parameters(&self) -> Parameters {
461        Parameters::new().with("nanos", ExactMatcher::of(INT))
462    }
463
464    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
465        Ok(TIMESTAMP)
466    }
467
468    fn type_id(&self) -> TypeId {
469        TypeId::of::<Self>()
470    }
471}
472
473/// to_unixtime(timestamp) - Convert timestamp to Unix epoch seconds as double.
474#[derive(Default, Clone)]
475pub struct ToUnixtime;
476
477impl FunctionDef for ToUnixtime {
478    fn name(&self) -> &'static str {
479        "to_unixtime"
480    }
481
482    fn parameters(&self) -> Parameters {
483        Parameters::new().with("timestamp", ExactMatcher::of(TIMESTAMP))
484    }
485
486    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
487        Ok(DOUBLE)
488    }
489
490    fn type_id(&self) -> TypeId {
491        TypeId::of::<Self>()
492    }
493}