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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

use std::fmt;

use chrono::{
    DateTime, Duration, FixedOffset, NaiveDateTime, NaiveTime, Offset, TimeZone, Timelike, Utc,
};
use mz_lowertest::MzReflect;
use mz_ore::result::ResultExt;
use mz_pgtz::timezone::Timezone;
use mz_repr::adt::date::Date;
use mz_repr::adt::datetime::DateTimeUnits;
use mz_repr::adt::interval::Interval;
use mz_repr::adt::numeric::{DecimalLike, Numeric};
use mz_repr::adt::timestamp::{CheckedTimestamp, TimestampPrecision, MAX_PRECISION};
use mz_repr::{strconv, ColumnType, ScalarType};
use proptest_derive::Arbitrary;
use serde::{Deserialize, Serialize};

use crate::scalar::func::format::DateTimeFormat;
use crate::scalar::func::{EagerUnaryFunc, TimestampLike};
use crate::EvalError;

sqlfunc!(
    #[sqlname = "timestamp_to_text"]
    #[preserves_uniqueness = true]
    #[inverse = to_unary!(super::CastStringToTimestamp(None))]
    fn cast_timestamp_to_string(a: CheckedTimestamp<NaiveDateTime>) -> String {
        let mut buf = String::new();
        strconv::format_timestamp(&mut buf, &a);
        buf
    }
);

sqlfunc!(
    #[sqlname = "timestamp_with_time_zone_to_text"]
    #[preserves_uniqueness = true]
    #[inverse = to_unary!(super::CastStringToTimestampTz(None))]
    fn cast_timestamp_tz_to_string(a: CheckedTimestamp<DateTime<Utc>>) -> String {
        let mut buf = String::new();
        strconv::format_timestamptz(&mut buf, &a);
        buf
    }
);

sqlfunc!(
    #[sqlname = "timestamp_to_date"]
    #[preserves_uniqueness = false]
    #[inverse = to_unary!(super::CastDateToTimestamp(None))]
    #[is_monotone = true]
    fn cast_timestamp_to_date(a: CheckedTimestamp<NaiveDateTime>) -> Result<Date, EvalError> {
        Ok(a.date().try_into()?)
    }
);

sqlfunc!(
    #[sqlname = "timestamp_with_time_zone_to_date"]
    #[preserves_uniqueness = false]
    #[inverse = to_unary!(super::CastDateToTimestampTz(None))]
    #[is_monotone = true]
    fn cast_timestamp_tz_to_date(a: CheckedTimestamp<DateTime<Utc>>) -> Result<Date, EvalError> {
        Ok(a.naive_utc().date().try_into()?)
    }
);

#[derive(
    Arbitrary, Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect,
)]
pub struct CastTimestampToTimestampTz {
    pub from: Option<TimestampPrecision>,
    pub to: Option<TimestampPrecision>,
}

impl<'a> EagerUnaryFunc<'a> for CastTimestampToTimestampTz {
    type Input = CheckedTimestamp<NaiveDateTime>;
    type Output = Result<CheckedTimestamp<DateTime<Utc>>, EvalError>;

    fn call(
        &self,
        a: CheckedTimestamp<NaiveDateTime>,
    ) -> Result<CheckedTimestamp<DateTime<Utc>>, EvalError> {
        let out =
            CheckedTimestamp::try_from(DateTime::<Utc>::from_naive_utc_and_offset(a.into(), Utc))?;
        let updated = out.round_to_precision(self.to)?;
        Ok(updated)
    }

    fn output_type(&self, input: ColumnType) -> ColumnType {
        ScalarType::TimestampTz { precision: self.to }.nullable(input.nullable)
    }

    fn preserves_uniqueness(&self) -> bool {
        let to_p = self.to.map(|p| p.into_u8()).unwrap_or(MAX_PRECISION);
        let from_p = self.from.map(|p| p.into_u8()).unwrap_or(MAX_PRECISION);
        // If it's getting cast to a higher precision, it should preserve uniqueness but not otherwise.
        to_p >= from_p
    }

    fn inverse(&self) -> Option<crate::UnaryFunc> {
        to_unary!(super::CastTimestampTzToTimestamp {
            from: self.from,
            to: self.to
        })
    }

    fn is_monotone(&self) -> bool {
        true
    }
}

impl fmt::Display for CastTimestampToTimestampTz {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("timestamp_to_timestamp_with_time_zone")
    }
}

#[derive(
    Arbitrary, Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect,
)]
pub struct AdjustTimestampPrecision {
    pub from: Option<TimestampPrecision>,
    pub to: Option<TimestampPrecision>,
}

impl<'a> EagerUnaryFunc<'a> for AdjustTimestampPrecision {
    type Input = CheckedTimestamp<NaiveDateTime>;
    type Output = Result<CheckedTimestamp<NaiveDateTime>, EvalError>;

    fn call(
        &self,
        a: CheckedTimestamp<NaiveDateTime>,
    ) -> Result<CheckedTimestamp<NaiveDateTime>, EvalError> {
        // This should never have been called if precisions are same.
        // Adding a soft-assert to flag if there are such instances.
        mz_ore::soft_assert_no_log!(self.to != self.from);

        let updated = a.round_to_precision(self.to)?;
        Ok(updated)
    }

    fn output_type(&self, input: ColumnType) -> ColumnType {
        ScalarType::Timestamp { precision: self.to }.nullable(input.nullable)
    }

    fn preserves_uniqueness(&self) -> bool {
        let to_p = self.to.map(|p| p.into_u8()).unwrap_or(MAX_PRECISION);
        let from_p = self.from.map(|p| p.into_u8()).unwrap_or(MAX_PRECISION);
        // If it's getting cast to a higher precision, it should preserve uniqueness but not otherwise.
        to_p >= from_p
    }

    fn inverse(&self) -> Option<crate::UnaryFunc> {
        None
    }

    fn is_monotone(&self) -> bool {
        true
    }
}

impl fmt::Display for AdjustTimestampPrecision {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("adjust_timestamp_precision")
    }
}

#[derive(
    Arbitrary, Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect,
)]
pub struct CastTimestampTzToTimestamp {
    pub from: Option<TimestampPrecision>,
    pub to: Option<TimestampPrecision>,
}

impl<'a> EagerUnaryFunc<'a> for CastTimestampTzToTimestamp {
    type Input = CheckedTimestamp<DateTime<Utc>>;
    type Output = Result<CheckedTimestamp<NaiveDateTime>, EvalError>;

    fn call(
        &self,
        a: CheckedTimestamp<DateTime<Utc>>,
    ) -> Result<CheckedTimestamp<NaiveDateTime>, EvalError> {
        let out = CheckedTimestamp::try_from(a.naive_utc())?;
        let updated = out.round_to_precision(self.to)?;
        Ok(updated)
    }

    fn output_type(&self, input: ColumnType) -> ColumnType {
        ScalarType::Timestamp { precision: self.to }.nullable(input.nullable)
    }

    fn preserves_uniqueness(&self) -> bool {
        let to_p = self.to.map(|p| p.into_u8()).unwrap_or(MAX_PRECISION);
        let from_p = self.from.map(|p| p.into_u8()).unwrap_or(MAX_PRECISION);
        // If it's getting cast to a higher precision, it should preserve uniqueness but not otherwise.
        to_p >= from_p
    }

    fn inverse(&self) -> Option<crate::UnaryFunc> {
        to_unary!(super::CastTimestampToTimestampTz {
            from: self.from,
            to: self.to
        })
    }

    fn is_monotone(&self) -> bool {
        true
    }
}

impl fmt::Display for CastTimestampTzToTimestamp {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("timestamp_with_time_zone_to_timestamp")
    }
}

#[derive(
    Arbitrary, Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect,
)]
pub struct AdjustTimestampTzPrecision {
    pub from: Option<TimestampPrecision>,
    pub to: Option<TimestampPrecision>,
}

impl<'a> EagerUnaryFunc<'a> for AdjustTimestampTzPrecision {
    type Input = CheckedTimestamp<DateTime<Utc>>;
    type Output = Result<CheckedTimestamp<DateTime<Utc>>, EvalError>;

    fn call(
        &self,
        a: CheckedTimestamp<DateTime<Utc>>,
    ) -> Result<CheckedTimestamp<DateTime<Utc>>, EvalError> {
        // This should never have been called if precisions are same.
        // Adding a soft-assert to flag if there are such instances.
        mz_ore::soft_assert_no_log!(self.to != self.from);

        let updated = a.round_to_precision(self.to)?;
        Ok(updated)
    }

    fn output_type(&self, input: ColumnType) -> ColumnType {
        ScalarType::TimestampTz { precision: self.to }.nullable(input.nullable)
    }

    fn preserves_uniqueness(&self) -> bool {
        let to_p = self.to.map(|p| p.into_u8()).unwrap_or(MAX_PRECISION);
        let from_p = self.from.map(|p| p.into_u8()).unwrap_or(MAX_PRECISION);
        // If it's getting cast to a higher precision, it should preserve uniqueness but not otherwise.
        to_p >= from_p
    }

    fn inverse(&self) -> Option<crate::UnaryFunc> {
        None
    }

    fn is_monotone(&self) -> bool {
        true
    }
}

impl fmt::Display for AdjustTimestampTzPrecision {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("adjust_timestamp_with_time_zone_precision")
    }
}

sqlfunc!(
    #[sqlname = "timestamp_to_time"]
    #[preserves_uniqueness = false]
    fn cast_timestamp_to_time(a: CheckedTimestamp<NaiveDateTime>) -> NaiveTime {
        a.time()
    }
);

sqlfunc!(
    #[sqlname = "timestamp_with_time_zone_to_time"]
    #[preserves_uniqueness = false]
    fn cast_timestamp_tz_to_time(a: CheckedTimestamp<DateTime<Utc>>) -> NaiveTime {
        a.naive_utc().time()
    }
);

pub fn date_part_interval_inner<D>(units: DateTimeUnits, interval: Interval) -> Result<D, EvalError>
where
    D: DecimalLike,
{
    match units {
        DateTimeUnits::Epoch => Ok(interval.as_epoch_seconds()),
        DateTimeUnits::Millennium => Ok(D::from(interval.millennia())),
        DateTimeUnits::Century => Ok(D::from(interval.centuries())),
        DateTimeUnits::Decade => Ok(D::from(interval.decades())),
        DateTimeUnits::Year => Ok(D::from(interval.years())),
        DateTimeUnits::Quarter => Ok(D::from(interval.quarters())),
        DateTimeUnits::Month => Ok(D::from(interval.months())),
        DateTimeUnits::Day => Ok(D::lossy_from(interval.days())),
        DateTimeUnits::Hour => Ok(D::lossy_from(interval.hours())),
        DateTimeUnits::Minute => Ok(D::lossy_from(interval.minutes())),
        DateTimeUnits::Second => Ok(interval.seconds()),
        DateTimeUnits::Milliseconds => Ok(interval.milliseconds()),
        DateTimeUnits::Microseconds => Ok(interval.microseconds()),
        DateTimeUnits::Week
        | DateTimeUnits::Timezone
        | DateTimeUnits::TimezoneHour
        | DateTimeUnits::TimezoneMinute
        | DateTimeUnits::DayOfWeek
        | DateTimeUnits::DayOfYear
        | DateTimeUnits::IsoDayOfWeek
        | DateTimeUnits::IsoDayOfYear => Err(EvalError::Unsupported {
            feature: format!("'{}' timestamp units", units).into(),
            discussion_no: None,
        }),
    }
}

#[derive(
    Arbitrary, Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect,
)]
pub struct ExtractInterval(pub DateTimeUnits);

impl<'a> EagerUnaryFunc<'a> for ExtractInterval {
    type Input = Interval;
    type Output = Result<Numeric, EvalError>;

    fn call(&self, a: Interval) -> Result<Numeric, EvalError> {
        date_part_interval_inner(self.0, a)
    }

    fn output_type(&self, input: ColumnType) -> ColumnType {
        ScalarType::Numeric { max_scale: None }.nullable(input.nullable)
    }
}

impl fmt::Display for ExtractInterval {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "extract_{}_iv", self.0)
    }
}

#[derive(
    Arbitrary, Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect,
)]
pub struct DatePartInterval(pub DateTimeUnits);

impl<'a> EagerUnaryFunc<'a> for DatePartInterval {
    type Input = Interval;
    type Output = Result<f64, EvalError>;

    fn call(&self, a: Interval) -> Result<f64, EvalError> {
        date_part_interval_inner(self.0, a)
    }

    fn output_type(&self, input: ColumnType) -> ColumnType {
        ScalarType::Float64.nullable(input.nullable)
    }
}

impl fmt::Display for DatePartInterval {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "date_part_{}_iv", self.0)
    }
}

pub fn date_part_timestamp_inner<T, D>(units: DateTimeUnits, ts: &T) -> Result<D, EvalError>
where
    T: TimestampLike,
    D: DecimalLike,
{
    match units {
        DateTimeUnits::Epoch => Ok(TimestampLike::extract_epoch(ts)),
        DateTimeUnits::Millennium => Ok(D::from(ts.millennium())),
        DateTimeUnits::Century => Ok(D::from(ts.century())),
        DateTimeUnits::Decade => Ok(D::from(ts.decade())),
        DateTimeUnits::Year => Ok(D::from(ts.year())),
        DateTimeUnits::Quarter => Ok(D::from(ts.quarter())),
        DateTimeUnits::Week => Ok(D::from(ts.iso_week_number())),
        DateTimeUnits::Month => Ok(D::from(ts.month())),
        DateTimeUnits::Day => Ok(D::from(ts.day())),
        DateTimeUnits::DayOfWeek => Ok(D::from(ts.day_of_week())),
        DateTimeUnits::DayOfYear => Ok(D::from(ts.ordinal())),
        DateTimeUnits::IsoDayOfWeek => Ok(D::from(ts.iso_day_of_week())),
        DateTimeUnits::Hour => Ok(D::from(ts.hour())),
        DateTimeUnits::Minute => Ok(D::from(ts.minute())),
        DateTimeUnits::Second => Ok(ts.extract_second()),
        DateTimeUnits::Milliseconds => Ok(ts.extract_millisecond()),
        DateTimeUnits::Microseconds => Ok(ts.extract_microsecond()),
        DateTimeUnits::Timezone
        | DateTimeUnits::TimezoneHour
        | DateTimeUnits::TimezoneMinute
        | DateTimeUnits::IsoDayOfYear => Err(EvalError::Unsupported {
            feature: format!("'{}' timestamp units", units).into(),
            discussion_no: None,
        }),
    }
}

/// Will extracting this unit from the timestamp include the "most significant bits" of
/// the timestamp?
pub(crate) fn most_significant_unit(unit: DateTimeUnits) -> bool {
    match unit {
        DateTimeUnits::Epoch
        | DateTimeUnits::Millennium
        | DateTimeUnits::Century
        | DateTimeUnits::Decade
        | DateTimeUnits::Year => true,
        _ => false,
    }
}

#[derive(
    Arbitrary, Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect,
)]
pub struct ExtractTimestamp(pub DateTimeUnits);

impl<'a> EagerUnaryFunc<'a> for ExtractTimestamp {
    type Input = CheckedTimestamp<NaiveDateTime>;
    type Output = Result<Numeric, EvalError>;

    fn call(&self, a: CheckedTimestamp<NaiveDateTime>) -> Result<Numeric, EvalError> {
        date_part_timestamp_inner(self.0, &*a)
    }

    fn output_type(&self, input: ColumnType) -> ColumnType {
        ScalarType::Numeric { max_scale: None }.nullable(input.nullable)
    }

    fn is_monotone(&self) -> bool {
        most_significant_unit(self.0)
    }
}

impl fmt::Display for ExtractTimestamp {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "extract_{}_ts", self.0)
    }
}

#[derive(
    Arbitrary, Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect,
)]
pub struct ExtractTimestampTz(pub DateTimeUnits);

impl<'a> EagerUnaryFunc<'a> for ExtractTimestampTz {
    type Input = CheckedTimestamp<DateTime<Utc>>;
    type Output = Result<Numeric, EvalError>;

    fn call(&self, a: CheckedTimestamp<DateTime<Utc>>) -> Result<Numeric, EvalError> {
        date_part_timestamp_inner(self.0, &*a)
    }

    fn output_type(&self, input: ColumnType) -> ColumnType {
        ScalarType::Numeric { max_scale: None }.nullable(input.nullable)
    }

    fn is_monotone(&self) -> bool {
        // Unlike the timezone-less timestamp, it's not safe to extract the "high-order bits" like
        // year: year takes timezone into account, and it's quite possible for a different timezone
        // to be in a previous year while having a later UTC-equivalent time.
        self.0 == DateTimeUnits::Epoch
    }
}

impl fmt::Display for ExtractTimestampTz {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "extract_{}_tstz", self.0)
    }
}

#[derive(
    Arbitrary, Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect,
)]
pub struct DatePartTimestamp(pub DateTimeUnits);

impl<'a> EagerUnaryFunc<'a> for DatePartTimestamp {
    type Input = CheckedTimestamp<NaiveDateTime>;
    type Output = Result<f64, EvalError>;

    fn call(&self, a: CheckedTimestamp<NaiveDateTime>) -> Result<f64, EvalError> {
        date_part_timestamp_inner(self.0, &*a)
    }

    fn output_type(&self, input: ColumnType) -> ColumnType {
        ScalarType::Float64.nullable(input.nullable)
    }
}

impl fmt::Display for DatePartTimestamp {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "date_part_{}_ts", self.0)
    }
}

#[derive(
    Arbitrary, Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect,
)]
pub struct DatePartTimestampTz(pub DateTimeUnits);

impl<'a> EagerUnaryFunc<'a> for DatePartTimestampTz {
    type Input = CheckedTimestamp<DateTime<Utc>>;
    type Output = Result<f64, EvalError>;

    fn call(&self, a: CheckedTimestamp<DateTime<Utc>>) -> Result<f64, EvalError> {
        date_part_timestamp_inner(self.0, &*a)
    }

    fn output_type(&self, input: ColumnType) -> ColumnType {
        ScalarType::Float64.nullable(input.nullable)
    }
}

impl fmt::Display for DatePartTimestampTz {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "date_part_{}_tstz", self.0)
    }
}

pub fn date_trunc_inner<T: TimestampLike>(units: DateTimeUnits, ts: &T) -> Result<T, EvalError> {
    match units {
        DateTimeUnits::Millennium => Ok(ts.truncate_millennium()),
        DateTimeUnits::Century => Ok(ts.truncate_century()),
        DateTimeUnits::Decade => Ok(ts.truncate_decade()),
        DateTimeUnits::Year => Ok(ts.truncate_year()),
        DateTimeUnits::Quarter => Ok(ts.truncate_quarter()),
        DateTimeUnits::Week => Ok(ts.truncate_week()?),
        DateTimeUnits::Day => Ok(ts.truncate_day()),
        DateTimeUnits::Hour => Ok(ts.truncate_hour()),
        DateTimeUnits::Minute => Ok(ts.truncate_minute()),
        DateTimeUnits::Second => Ok(ts.truncate_second()),
        DateTimeUnits::Month => Ok(ts.truncate_month()),
        DateTimeUnits::Milliseconds => Ok(ts.truncate_milliseconds()),
        DateTimeUnits::Microseconds => Ok(ts.truncate_microseconds()),
        DateTimeUnits::Epoch
        | DateTimeUnits::Timezone
        | DateTimeUnits::TimezoneHour
        | DateTimeUnits::TimezoneMinute
        | DateTimeUnits::DayOfWeek
        | DateTimeUnits::DayOfYear
        | DateTimeUnits::IsoDayOfWeek
        | DateTimeUnits::IsoDayOfYear => Err(EvalError::Unsupported {
            feature: format!("'{}' timestamp units", units).into(),
            discussion_no: None,
        }),
    }
}

#[derive(
    Arbitrary, Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect,
)]
pub struct DateTruncTimestamp(pub DateTimeUnits);

impl<'a> EagerUnaryFunc<'a> for DateTruncTimestamp {
    type Input = CheckedTimestamp<NaiveDateTime>;
    type Output = Result<CheckedTimestamp<NaiveDateTime>, EvalError>;

    fn call(
        &self,
        a: CheckedTimestamp<NaiveDateTime>,
    ) -> Result<CheckedTimestamp<NaiveDateTime>, EvalError> {
        date_trunc_inner(self.0, &*a)?.try_into().err_into()
    }

    fn output_type(&self, input: ColumnType) -> ColumnType {
        ScalarType::Timestamp { precision: None }.nullable(input.nullable)
    }

    fn is_monotone(&self) -> bool {
        true
    }
}

impl fmt::Display for DateTruncTimestamp {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "date_trunc_{}_ts", self.0)
    }
}

#[derive(
    Arbitrary, Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect,
)]
pub struct DateTruncTimestampTz(pub DateTimeUnits);

impl<'a> EagerUnaryFunc<'a> for DateTruncTimestampTz {
    type Input = CheckedTimestamp<DateTime<Utc>>;
    type Output = Result<CheckedTimestamp<DateTime<Utc>>, EvalError>;

    fn call(
        &self,
        a: CheckedTimestamp<DateTime<Utc>>,
    ) -> Result<CheckedTimestamp<DateTime<Utc>>, EvalError> {
        date_trunc_inner(self.0, &*a)?.try_into().err_into()
    }

    fn output_type(&self, input: ColumnType) -> ColumnType {
        ScalarType::TimestampTz { precision: None }.nullable(input.nullable)
    }

    fn is_monotone(&self) -> bool {
        true
    }
}

impl fmt::Display for DateTruncTimestampTz {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "date_trunc_{}_tstz", self.0)
    }
}

/// Converts the timestamp `dt`, which is assumed to be in the time of the timezone `tz` to a timestamptz in UTC.
/// This operation is fallible because certain timestamps at timezones that observe DST are simply impossible or
/// ambiguous. In case of ambiguity (when a hour repeats) we will prefer the latest variant, and when an hour is
/// impossible, we will attempt to fix it by advancing it. For example, `EST` and `2020-11-11T12:39:14` would return
/// `2020-11-11T17:39:14Z`. A DST observing timezone like `America/New_York` would cause the following DST anomalies:
/// `2020-11-01T00:59:59` -> `2020-11-01T04:59:59Z` and `2020-11-01T01:00:00` -> `2020-11-01T06:00:00Z`
/// `2020-03-08T02:59:59` -> `2020-03-08T07:59:59Z` and `2020-03-08T03:00:00` -> `2020-03-08T07:00:00Z`
pub fn timezone_timestamp(
    tz: Timezone,
    dt: NaiveDateTime,
) -> Result<CheckedTimestamp<DateTime<Utc>>, EvalError> {
    let offset = match tz {
        Timezone::FixedOffset(offset) => offset,
        Timezone::Tz(tz) => match tz.offset_from_local_datetime(&dt).latest() {
            Some(offset) => offset.fix(),
            None => {
                let dt = dt
                    .checked_add_signed(
                        Duration::try_hours(1).ok_or(EvalError::TimestampOutOfRange)?,
                    )
                    .ok_or(EvalError::TimestampOutOfRange)?;
                tz.offset_from_local_datetime(&dt)
                    .latest()
                    .ok_or(EvalError::InvalidTimezoneConversion)?
                    .fix()
            }
        },
    };
    DateTime::from_naive_utc_and_offset(dt - offset, Utc)
        .try_into()
        .err_into()
}

/// Converts the UTC timestamptz `utc` to the local timestamp of the timezone `tz`.
/// For example, `EST` and `2020-11-11T17:39:14Z` would return `2020-11-11T12:39:14`.
pub fn timezone_timestamptz(tz: Timezone, utc: DateTime<Utc>) -> Result<NaiveDateTime, EvalError> {
    let offset = match tz {
        Timezone::FixedOffset(offset) => offset,
        Timezone::Tz(tz) => tz.offset_from_utc_datetime(&utc.naive_utc()).fix(),
    };
    checked_add_with_leapsecond(&utc.naive_utc(), &offset).ok_or(EvalError::TimestampOutOfRange)
}

/// Checked addition that is missing from chrono. Adapt its methods here but add a check.
fn checked_add_with_leapsecond(lhs: &NaiveDateTime, rhs: &FixedOffset) -> Option<NaiveDateTime> {
    // extract and temporarily remove the fractional part and later recover it
    let nanos = lhs.nanosecond();
    let lhs = lhs.with_nanosecond(0).unwrap();
    let rhs = rhs.local_minus_utc();
    lhs.checked_add_signed(match chrono::Duration::try_seconds(i64::from(rhs)) {
        Some(dur) => dur,
        None => return None,
    })
    .map(|dt| dt.with_nanosecond(nanos).unwrap())
}

#[derive(
    Arbitrary, Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect,
)]
pub struct TimezoneTimestamp(pub Timezone);

impl<'a> EagerUnaryFunc<'a> for TimezoneTimestamp {
    type Input = CheckedTimestamp<NaiveDateTime>;
    type Output = Result<CheckedTimestamp<DateTime<Utc>>, EvalError>;

    fn call(
        &self,
        a: CheckedTimestamp<NaiveDateTime>,
    ) -> Result<CheckedTimestamp<DateTime<Utc>>, EvalError> {
        timezone_timestamp(self.0, a.to_naive())
    }

    fn output_type(&self, input: ColumnType) -> ColumnType {
        ScalarType::TimestampTz { precision: None }.nullable(input.nullable)
    }
}

impl fmt::Display for TimezoneTimestamp {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "timezone_{}_ts", self.0)
    }
}

#[derive(
    Arbitrary, Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect,
)]
pub struct TimezoneTimestampTz(pub Timezone);

impl<'a> EagerUnaryFunc<'a> for TimezoneTimestampTz {
    type Input = CheckedTimestamp<DateTime<Utc>>;
    type Output = Result<CheckedTimestamp<NaiveDateTime>, EvalError>;

    fn call(
        &self,
        a: CheckedTimestamp<DateTime<Utc>>,
    ) -> Result<CheckedTimestamp<NaiveDateTime>, EvalError> {
        timezone_timestamptz(self.0, a.into())?
            .try_into()
            .err_into()
    }

    fn output_type(&self, input: ColumnType) -> ColumnType {
        ScalarType::Timestamp { precision: None }.nullable(input.nullable)
    }
}

impl fmt::Display for TimezoneTimestampTz {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "timezone_{}_tstz", self.0)
    }
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, MzReflect)]
pub struct ToCharTimestamp {
    pub format_string: String,
    pub format: DateTimeFormat,
}

impl<'a> EagerUnaryFunc<'a> for ToCharTimestamp {
    type Input = CheckedTimestamp<NaiveDateTime>;
    type Output = String;

    fn call(&self, input: CheckedTimestamp<NaiveDateTime>) -> String {
        self.format.render(&*input)
    }

    fn output_type(&self, input: ColumnType) -> ColumnType {
        ScalarType::String.nullable(input.nullable)
    }
}

impl fmt::Display for ToCharTimestamp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "tocharts[{}]", self.format_string)
    }
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, MzReflect)]
pub struct ToCharTimestampTz {
    pub format_string: String,
    pub format: DateTimeFormat,
}

impl<'a> EagerUnaryFunc<'a> for ToCharTimestampTz {
    type Input = CheckedTimestamp<DateTime<Utc>>;
    type Output = String;

    fn call(&self, input: CheckedTimestamp<DateTime<Utc>>) -> String {
        self.format.render(&*input)
    }

    fn output_type(&self, input: ColumnType) -> ColumnType {
        ScalarType::String.nullable(input.nullable)
    }
}

impl fmt::Display for ToCharTimestampTz {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "tochartstz[{}]", self.format_string)
    }
}