Skip to content

Commit

Permalink
fix(query): interval total microseconds add overflow check (#17215)
Browse files Browse the repository at this point in the history
  • Loading branch information
TCeason authored Jan 8, 2025
1 parent 1bf05c3 commit dceaa71
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/common/column/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ either = { workspace = true }
ethnum = { workspace = true }
foreign_vec = { workspace = true }
hex = { workspace = true }
log = { workspace = true }
match-template = { workspace = true }
num-traits = { workspace = true }
serde = { workspace = true, features = ["rc"], optional = true }
Expand Down
19 changes: 16 additions & 3 deletions src/common/column/src/types/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use borsh::BorshSerialize;
use bytemuck::Pod;
use bytemuck::Zeroable;
use databend_common_base::base::OrderedFloat;
use log::error;
use serde_derive::Deserialize;
use serde_derive::Serialize;

Expand Down Expand Up @@ -319,9 +320,21 @@ impl months_days_micros {
}

pub fn total_micros(&self) -> i64 {
(self.months() as i64 * MICROS_PER_MONTH)
+ (self.days() as i64 * MICROS_PER_DAY)
+ self.microseconds()
let months_micros = (self.months() as i64).checked_mul(MICROS_PER_MONTH);
let days_micros = (self.days() as i64).checked_mul(MICROS_PER_DAY);

months_micros
.and_then(|months_micros| days_micros.map(|days_micros| months_micros + days_micros))
.and_then(|total_micros| total_micros.checked_add(self.microseconds()))
.unwrap_or_else(|| {
error!(
"interval is out of range: months={}, days={}, micros={}",
self.months(),
self.days(),
self.microseconds()
);
0
})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,15 @@ query T
SELECT to_years(10);
----
10 years

onlyif http
query T
select to_interval('1200000000 months');
----
100000000 years

onlyif http
query T
select to_interval('120000000000 months');
----
00:00:00

0 comments on commit dceaa71

Please sign in to comment.