Skip to content

Commit 8a61a9b

Browse files
committed
impl Add/AddAssign for months_days_micros
1 parent 89034c8 commit 8a61a9b

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

src/common/column/src/types/native.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use std::cmp::Ordering;
1717
use std::convert::TryFrom;
1818
use std::hash::Hash;
1919
use std::hash::Hasher;
20+
use std::ops::Add;
21+
use std::ops::AddAssign;
2022
use std::ops::Neg;
2123
use std::panic::RefUnwindSafe;
2224

@@ -268,6 +270,26 @@ impl NativeType for days_ms {
268270
#[repr(C)]
269271
pub struct months_days_micros(pub i128);
270272

273+
impl Add for months_days_micros {
274+
type Output = Self;
275+
276+
fn add(self, rhs: Self) -> Self::Output {
277+
let add_months = self.months() + rhs.months();
278+
let add_days = self.days() + rhs.days();
279+
let add_micros = self.microseconds() + rhs.microseconds();
280+
Self::new(add_months, add_days, add_micros)
281+
}
282+
}
283+
284+
impl AddAssign for months_days_micros {
285+
fn add_assign(&mut self, rhs: Self) {
286+
let add_months = self.months() + rhs.months();
287+
let add_days = self.days() + rhs.days();
288+
let add_micros = self.microseconds() + rhs.microseconds();
289+
self.0 = months_days_micros::new(add_months, add_days, add_micros).0
290+
}
291+
}
292+
271293
impl Hash for months_days_micros {
272294
fn hash<H: Hasher>(&self, state: &mut H) {
273295
self.total_micros().hash(state)

src/query/functions/src/aggregates/aggregate_sum.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,7 @@ impl UnaryState<IntervalType, IntervalType> for IntervalSumState {
283283
other: months_days_micros,
284284
_function_data: Option<&dyn FunctionData>,
285285
) -> Result<()> {
286-
let sum = self.value.total_micros() + other.total_micros();
287-
self.value = months_days_micros(sum as i128);
286+
self.value += other;
288287
Ok(())
289288
}
290289

@@ -294,24 +293,23 @@ impl UnaryState<IntervalType, IntervalType> for IntervalSumState {
294293
validity: Option<&Bitmap>,
295294
_function_data: Option<&dyn FunctionData>,
296295
) -> Result<()> {
297-
let mut sum = self.value.total_micros();
298296
let col = IntervalType::upcast_column(other);
299297
let buffer = IntervalType::try_downcast_column(&col).unwrap();
300298
match validity {
301299
Some(validity) if validity.null_count() > 0 => {
302300
buffer.iter().zip(validity.iter()).for_each(|(t, b)| {
303301
if b {
304-
sum += t.total_micros();
302+
self.value += *t;
305303
}
306304
});
307305
}
308306
_ => {
309307
buffer.iter().for_each(|t| {
310-
sum += t.total_micros();
308+
self.value += *t;
311309
});
312310
}
313311
}
314-
self.value = months_days_micros(sum as i128);
312+
315313
Ok(())
316314
}
317315

tests/sqllogictests/suites/base/11_data_type/11_0007_data_type_interval.test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ create or replace table t (c interval);
1515
statement ok
1616
insert into t values('1 year'),('1 months'),('1 day'),('1 hour'),('1 minute'),('1 second'),('1 microsecond');
1717

18+
onlyif http
1819
query T
1920
select * from t order by c;
2021
----
@@ -26,11 +27,13 @@ select * from t order by c;
2627
1 month
2728
1 year
2829

30+
onlyif http
2931
query T
3032
select sum(c) from t;
3133
----
3234
9385:01:01.000001
3335

36+
onlyif http
3437
query T
3538
select sum(c) from t where c <= interval '1 day';
3639
----

0 commit comments

Comments
 (0)