datatypes/
interval.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use arrow::datatypes::{
16    IntervalDayTime as ArrowIntervalDayTime, IntervalMonthDayNano as ArrowIntervalMonthDayNano,
17};
18use common_time::{IntervalDayTime, IntervalMonthDayNano, IntervalYearMonth};
19use paste::paste;
20
21use crate::prelude::Scalar;
22use crate::scalars::ScalarRef;
23use crate::types::{
24    IntervalDayTimeType, IntervalMonthDayNanoType, IntervalYearMonthType, WrapperType,
25};
26use crate::vectors::{IntervalDayTimeVector, IntervalMonthDayNanoVector, IntervalYearMonthVector};
27
28macro_rules! define_interval_with_unit {
29    ($unit: ident, $native_ty: ty) => {
30        paste! {
31            impl Scalar for [<Interval $unit>] {
32                type VectorType = [<Interval $unit Vector>];
33                type RefType<'a> = [<Interval $unit>];
34
35                fn as_scalar_ref(&self) -> Self::RefType<'_> {
36                    *self
37                }
38
39                fn upcast_gat<'short, 'long: 'short>(
40                    long: Self::RefType<'long>,
41                ) -> Self::RefType<'short> {
42                    long
43                }
44            }
45
46            impl<'a> ScalarRef<'a> for [<Interval $unit>] {
47                type ScalarType = [<Interval $unit>];
48
49                fn to_owned_scalar(&self) -> Self::ScalarType {
50                    *self
51                }
52            }
53
54            impl WrapperType for [<Interval $unit>] {
55                type LogicalType = [<Interval $unit Type>];
56                type Native = $native_ty;
57
58                fn from_native(value: Self::Native) -> Self {
59                    Self::from(value)
60                }
61
62                fn into_native(self) -> Self::Native {
63                    self.into()
64                }
65            }
66        }
67    };
68}
69
70define_interval_with_unit!(YearMonth, i32);
71define_interval_with_unit!(DayTime, ArrowIntervalDayTime);
72define_interval_with_unit!(MonthDayNano, ArrowIntervalMonthDayNano);
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77
78    #[test]
79    fn test_interval_scalar() {
80        let interval = IntervalYearMonth::from(1000);
81        assert_eq!(interval, interval.as_scalar_ref());
82        assert_eq!(interval, interval.to_owned_scalar());
83        assert_eq!(1000, interval.into_native());
84
85        let interval = IntervalDayTime::from(1000);
86        assert_eq!(interval, interval.as_scalar_ref());
87        assert_eq!(interval, interval.to_owned_scalar());
88        assert_eq!(ArrowIntervalDayTime::from(interval), interval.into_native());
89
90        let interval = IntervalMonthDayNano::from(1000);
91        assert_eq!(interval, interval.as_scalar_ref());
92        assert_eq!(interval, interval.to_owned_scalar());
93        assert_eq!(
94            ArrowIntervalMonthDayNano::from(interval),
95            interval.into_native()
96        );
97    }
98
99    #[test]
100    fn test_interval_convert_to_native_type() {
101        let interval = IntervalMonthDayNano::from(1000);
102        let native_value: i128 = interval.into();
103        assert_eq!(native_value, 1000);
104
105        let interval = IntervalDayTime::from(1000);
106        let native_interval: i64 = interval.into();
107        assert_eq!(native_interval, 1000);
108
109        let interval = IntervalYearMonth::from(1000);
110        let native_interval: i32 = interval.into();
111        assert_eq!(native_interval, 1000);
112    }
113}