1use common_time::timestamp::TimeUnit;
16use common_time::Timestamp;
17use paste::paste;
18use serde::{Deserialize, Serialize};
19
20use crate::prelude::{Scalar, Value, ValueRef};
21use crate::scalars::ScalarRef;
22use crate::types::{
23 TimestampMicrosecondType, TimestampMillisecondType, TimestampNanosecondType,
24 TimestampSecondType, WrapperType,
25};
26use crate::vectors::{
27 TimestampMicrosecondVector, TimestampMillisecondVector, TimestampNanosecondVector,
28 TimestampSecondVector,
29};
30
31macro_rules! define_timestamp_with_unit {
32 ($unit: ident) => {
33 paste! {
34 #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
35 pub struct [<Timestamp $unit>](pub Timestamp);
36
37 impl [<Timestamp $unit>] {
38 pub fn new(val: i64) -> Self {
39 Self(Timestamp::new(val, TimeUnit::$unit))
40 }
41 }
42
43 impl Default for [<Timestamp $unit>] {
44 fn default() -> Self {
45 Self::new(0)
46 }
47 }
48
49 impl From<[<Timestamp $unit>]> for Value {
50 fn from(t: [<Timestamp $unit>]) -> Value {
51 Value::Timestamp(t.0)
52 }
53 }
54
55 impl From<[<Timestamp $unit>]> for serde_json::Value {
56 fn from(t: [<Timestamp $unit>]) -> Self {
57 t.0.into()
58 }
59 }
60
61 impl From<[<Timestamp $unit>]> for ValueRef<'static> {
62 fn from(t: [<Timestamp $unit>]) -> Self {
63 ValueRef::Timestamp(t.0)
64 }
65 }
66
67 impl Scalar for [<Timestamp $unit>] {
68 type VectorType = [<Timestamp $unit Vector>];
69 type RefType<'a> = [<Timestamp $unit>];
70
71 fn as_scalar_ref(&self) -> Self::RefType<'_> {
72 *self
73 }
74
75 fn upcast_gat<'short, 'long: 'short>(
76 long: Self::RefType<'long>,
77 ) -> Self::RefType<'short> {
78 long
79 }
80 }
81
82 impl<'a> ScalarRef<'a> for [<Timestamp $unit>] {
83 type ScalarType = [<Timestamp $unit>];
84
85 fn to_owned_scalar(&self) -> Self::ScalarType {
86 *self
87 }
88 }
89
90 impl WrapperType for [<Timestamp $unit>] {
91 type LogicalType = [<Timestamp $unit Type>];
92 type Native = i64;
93
94 fn from_native(value: Self::Native) -> Self {
95 Self::new(value)
96 }
97
98 fn into_native(self) -> Self::Native {
99 self.0.into()
100 }
101 }
102
103 impl From<i64> for [<Timestamp $unit>] {
104 fn from(val: i64) -> Self {
105 [<Timestamp $unit>]::from_native(val)
106 }
107 }
108
109 impl From<[<Timestamp $unit>]> for i64{
110 fn from(val: [<Timestamp $unit>]) -> Self {
111 val.0.value()
112 }
113 }
114
115 impl TryFrom<Value> for Option<[<Timestamp $unit>]> {
116 type Error = $crate::error::Error;
117
118 #[inline]
119 fn try_from(from: Value) -> std::result::Result<Self, Self::Error> {
120 match from {
121 Value::Timestamp(v) if v.unit() == TimeUnit::$unit => {
122 Ok(Some([<Timestamp $unit>](v)))
123 },
124 Value::Null => Ok(None),
125 _ => $crate::error::TryFromValueSnafu {
126 reason: format!("{:?} is not a {}", from, stringify!([<Timestamp $unit>])),
127 }
128 .fail(),
129 }
130 }
131 }
132 }
133 };
134}
135
136define_timestamp_with_unit!(Second);
137define_timestamp_with_unit!(Millisecond);
138define_timestamp_with_unit!(Microsecond);
139define_timestamp_with_unit!(Nanosecond);
140
141#[cfg(test)]
142mod tests {
143 use common_time::timezone::set_default_timezone;
144
145 use super::*;
146
147 #[test]
148 fn test_to_serde_json_value() {
149 set_default_timezone(Some("Asia/Shanghai")).unwrap();
150 let ts = TimestampSecond::new(123);
151 let val = serde_json::Value::from(ts);
152 match val {
153 serde_json::Value::String(s) => {
154 assert_eq!("1970-01-01 08:02:03+0800", s);
155 }
156 _ => unreachable!(),
157 }
158 }
159
160 #[test]
161 fn test_timestamp_scalar() {
162 let ts = TimestampSecond::new(123);
163 assert_eq!(ts, ts.as_scalar_ref());
164 assert_eq!(ts, ts.to_owned_scalar());
165 let ts = TimestampMillisecond::new(123);
166 assert_eq!(ts, ts.as_scalar_ref());
167 assert_eq!(ts, ts.to_owned_scalar());
168 let ts = TimestampMicrosecond::new(123);
169 assert_eq!(ts, ts.as_scalar_ref());
170 assert_eq!(ts, ts.to_owned_scalar());
171 let ts = TimestampNanosecond::new(123);
172 assert_eq!(ts, ts.as_scalar_ref());
173 assert_eq!(ts, ts.to_owned_scalar());
174 }
175}