@@ -6,6 +6,7 @@ use std::str::FromStr;
66#[ derive( Default , Debug , PartialEq , Clone , Hash , Eq ) ]
77pub struct SqlInterval {
88 pub year : i32 ,
9+ pub quarter : i32 ,
910 pub month : i32 ,
1011 pub week : i32 ,
1112 pub day : i32 ,
@@ -17,6 +18,7 @@ pub struct SqlInterval {
1718impl SqlInterval {
1819 pub fn new (
1920 year : i32 ,
21+ quarter : i32 ,
2022 month : i32 ,
2123 week : i32 ,
2224 day : i32 ,
@@ -26,6 +28,7 @@ impl SqlInterval {
2628 ) -> Self {
2729 Self {
2830 year,
31+ quarter,
2932 month,
3033 week,
3134 day,
@@ -48,12 +51,12 @@ impl SqlInterval {
4851 "week"
4952 } else if self . month != 0 {
5053 "month"
54+ } else if self . quarter != 0 {
55+ "quarter"
5156 } else if self . year != 0 {
5257 "year"
5358 } else {
54- return Err ( CubeError :: internal ( format ! (
55- "Attempt to get granularity from empty SqlInterval"
56- ) ) ) ;
59+ return Err ( CubeError :: internal ( "Attempt to get granularity from empty SqlInterval" . to_string ( ) ) ) ;
5760 } ;
5861 Ok ( res. to_string ( ) )
5962 }
@@ -63,6 +66,9 @@ impl SqlInterval {
6366 if self . year != 0 {
6467 res. push ( format ! ( "{} year" , self . year) ) ;
6568 }
69+ if self . quarter != 0 {
70+ res. push ( format ! ( "{} quarter" , self . quarter) ) ;
71+ }
6672 if self . month != 0 {
6773 res. push ( format ! ( "{} month" , self . month) ) ;
6874 }
@@ -87,6 +93,7 @@ impl SqlInterval {
8793 pub fn inverse ( & self ) -> Self {
8894 Self :: new (
8995 -self . year ,
96+ -self . quarter ,
9097 -self . month ,
9198 -self . week ,
9299 -self . day ,
@@ -102,6 +109,7 @@ impl Add for SqlInterval {
102109 fn add ( self , other : SqlInterval ) -> SqlInterval {
103110 SqlInterval :: new (
104111 self . year + other. year ,
112+ self . quarter + other. quarter ,
105113 self . month + other. month ,
106114 self . week + other. week ,
107115 self . day + other. day ,
@@ -115,6 +123,7 @@ impl Add for SqlInterval {
115123impl AddAssign < & SqlInterval > for SqlInterval {
116124 fn add_assign ( & mut self , other : & SqlInterval ) {
117125 self . year += other. year ;
126+ self . quarter += other. quarter ;
118127 self . month += other. month ;
119128 self . week += other. week ;
120129 self . day += other. day ;
@@ -135,6 +144,7 @@ impl Sub for SqlInterval {
135144 fn sub ( self , other : SqlInterval ) -> SqlInterval {
136145 SqlInterval :: new (
137146 self . year - other. year ,
147+ self . quarter - other. quarter ,
138148 self . month - other. month ,
139149 self . week - other. week ,
140150 self . day - other. day ,
@@ -150,6 +160,7 @@ impl Neg for SqlInterval {
150160 fn neg ( self ) -> SqlInterval {
151161 SqlInterval :: new (
152162 -self . year ,
163+ -self . quarter ,
153164 -self . month ,
154165 -self . week ,
155166 -self . day ,
@@ -175,6 +186,7 @@ impl FromStr for SqlInterval {
175186 "day" | "days" => result. day = value,
176187 "week" | "weeks" => result. week = value,
177188 "month" | "months" => result. month = value,
189+ "quarter" | "quarters" => result. quarter = value,
178190 "year" | "years" => result. year = value,
179191 other => return Err ( CubeError :: user ( format ! ( "Invalid interval unit: {}" , other) ) ) ,
180192 }
@@ -191,28 +203,28 @@ mod tests {
191203 fn test_from_str ( ) {
192204 assert_eq ! (
193205 SqlInterval :: from_str( "1 second" ) . unwrap( ) ,
194- SqlInterval :: new( 0 , 0 , 0 , 0 , 0 , 0 , 1 )
206+ SqlInterval :: new( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 )
195207 ) ;
196208
197209 assert_eq ! (
198210 SqlInterval :: from_str( "1 year 3 months 4 weeks 2 day 4 hours 2 minutes 1 second" )
199211 . unwrap( ) ,
200- SqlInterval :: new( 1 , 3 , 4 , 2 , 4 , 2 , 1 )
212+ SqlInterval :: new( 1 , 0 , 3 , 4 , 2 , 4 , 2 , 1 )
201213 ) ;
202214 }
203215 #[ test]
204216 fn test_arithmetic ( ) {
205217 assert_eq ! (
206- SqlInterval :: new( 1 , 3 , 4 , 2 , 4 , 2 , 1 ) + SqlInterval :: new( 1 , 3 , 4 , 2 , 4 , 2 , 1 ) ,
207- SqlInterval :: new( 2 , 6 , 8 , 4 , 8 , 4 , 2 )
218+ SqlInterval :: new( 1 , 0 , 3 , 4 , 2 , 4 , 2 , 1 ) + SqlInterval :: new( 1 , 0 , 3 , 4 , 2 , 4 , 2 , 1 ) ,
219+ SqlInterval :: new( 2 , 0 , 6 , 8 , 4 , 8 , 4 , 2 )
208220 ) ;
209221 assert_eq ! (
210- SqlInterval :: new( 1 , 3 , 4 , 2 , 4 , 2 , 1 ) - SqlInterval :: new( 1 , 4 , 4 , 2 , 2 , 2 , 1 ) ,
211- SqlInterval :: new( 0 , -1 , 0 , 0 , 2 , 0 , 0 )
222+ SqlInterval :: new( 1 , 0 , 3 , 4 , 2 , 4 , 2 , 1 ) - SqlInterval :: new( 1 , 0 , 4 , 4 , 2 , 2 , 2 , 1 ) ,
223+ SqlInterval :: new( 0 , 0 , -1 , 0 , 0 , 2 , 0 , 0 )
212224 ) ;
213225 assert_eq ! (
214- -SqlInterval :: new( 1 , 3 , -4 , 2 , 4 , 2 , 1 ) ,
215- SqlInterval :: new( -1 , -3 , 4 , -2 , -4 , -2 , -1 )
226+ -SqlInterval :: new( 1 , 0 , 3 , -4 , 2 , 4 , 2 , 1 ) ,
227+ SqlInterval :: new( -1 , 0 , -3 , 4 , -2 , -4 , -2 , -1 )
216228 ) ;
217229 }
218230}
0 commit comments