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
// 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, Datelike, NaiveDate, NaiveDateTime, Utc};
use mz_lowertest::MzReflect;
use mz_repr::adt::date::Date;
use mz_repr::adt::datetime::DateTimeUnits;
use mz_repr::adt::numeric::Numeric;
use mz_repr::adt::timestamp::{CheckedTimestamp, DateLike, TimestampPrecision};
use mz_repr::{strconv, ColumnType, ScalarType};
use proptest_derive::Arbitrary;
use serde::{Deserialize, Serialize};

use crate::func::most_significant_unit;
use crate::scalar::func::EagerUnaryFunc;
use crate::EvalError;

sqlfunc!(
    #[sqlname = "date_to_text"]
    #[preserves_uniqueness = true]
    #[inverse = to_unary!(super::CastStringToDate)]
    fn cast_date_to_string(a: Date) -> String {
        let mut buf = String::new();
        strconv::format_date(&mut buf, a);
        buf
    }
);

#[derive(
    Arbitrary, Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect,
)]
pub struct CastDateToTimestamp(pub Option<TimestampPrecision>);

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

    fn call(&self, a: Date) -> Result<CheckedTimestamp<NaiveDateTime>, EvalError> {
        let out =
            CheckedTimestamp::from_timestamplike(NaiveDate::from(a).and_hms_opt(0, 0, 0).unwrap())?;
        let updated = out.round_to_precision(self.0)?;
        Ok(updated)
    }

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

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

    fn inverse(&self) -> Option<crate::UnaryFunc> {
        to_unary!(super::CastTimestampToDate)
    }

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

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

#[derive(
    Arbitrary, Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect,
)]
pub struct CastDateToTimestampTz(pub Option<TimestampPrecision>);

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

    fn call(&self, a: Date) -> Result<CheckedTimestamp<DateTime<Utc>>, EvalError> {
        let out =
            CheckedTimestamp::from_timestamplike(DateTime::<Utc>::from_naive_utc_and_offset(
                NaiveDate::from(a).and_hms_opt(0, 0, 0).unwrap(),
                Utc,
            ))?;
        let updated = out.round_to_precision(self.0)?;
        Ok(updated)
    }

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

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

    fn inverse(&self) -> Option<crate::UnaryFunc> {
        to_unary!(super::CastTimestampTzToDate)
    }

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

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

pub fn extract_date_inner(units: DateTimeUnits, date: NaiveDate) -> Result<Numeric, EvalError> {
    match units {
        DateTimeUnits::Epoch => Ok(Numeric::from(date.extract_epoch())),
        DateTimeUnits::Millennium => Ok(Numeric::from(date.millennium())),
        DateTimeUnits::Century => Ok(Numeric::from(date.century())),
        DateTimeUnits::Decade => Ok(Numeric::from(date.decade())),
        DateTimeUnits::Year => Ok(Numeric::from(date.year())),
        DateTimeUnits::Quarter => Ok(Numeric::from(date.quarter())),
        DateTimeUnits::Week => Ok(Numeric::from(date.iso_week_number())),
        DateTimeUnits::Month => Ok(Numeric::from(date.month())),
        DateTimeUnits::Day => Ok(Numeric::from(date.day())),
        DateTimeUnits::DayOfWeek => Ok(Numeric::from(date.day_of_week())),
        DateTimeUnits::DayOfYear => Ok(Numeric::from(date.ordinal())),
        DateTimeUnits::IsoDayOfWeek => Ok(Numeric::from(date.iso_day_of_week())),
        DateTimeUnits::Hour
        | DateTimeUnits::Minute
        | DateTimeUnits::Second
        | DateTimeUnits::Milliseconds
        | DateTimeUnits::Microseconds => Err(EvalError::UnsupportedUnits(
            format!("{}", units).into(),
            "date".into(),
        )),
        DateTimeUnits::Timezone
        | DateTimeUnits::TimezoneHour
        | DateTimeUnits::TimezoneMinute
        | 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 ExtractDate(pub DateTimeUnits);

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

    fn call(&self, a: Date) -> Result<Numeric, EvalError> {
        extract_date_inner(self.0, a.into())
    }

    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 ExtractDate {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "extract_{}_d", self.0)
    }
}