Skip to content

Commit

Permalink
normalize decimals in extract
Browse files Browse the repository at this point in the history
Signed-off-by: Runji Wang <[email protected]>
  • Loading branch information
wangrunji0408 committed Mar 30, 2023
1 parent d609da0 commit 8e70256
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/expr/src/vector_op/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ fn extract_time(time: impl Timelike, unit: &str) -> Option<Decimal> {
} else if unit.eq_ignore_ascii_case("minute") {
time.minute().into()
} else if unit.eq_ignore_ascii_case("second") {
Decimal::from_i128_with_scale(nanoseconds() as i128, 9)
Decimal::from_i128_with_scale(nanoseconds() as i128, 9).normalize()
} else if unit.eq_ignore_ascii_case("millisecond") {
Decimal::from_i128_with_scale(nanoseconds() as i128, 6)
Decimal::from_i128_with_scale(nanoseconds() as i128, 6).normalize()
} else if unit.eq_ignore_ascii_case("microsecond") {
Decimal::from_i128_with_scale(nanoseconds() as i128, 3)
Decimal::from_i128_with_scale(nanoseconds() as i128, 3).normalize()
} else {
return None;
})
Expand All @@ -86,10 +86,12 @@ pub fn extract_from_time(unit: &str, time: Time) -> Result<Decimal> {
#[function("extract(varchar, timestamp) -> decimal")]
pub fn extract_from_timestamp(unit: &str, timestamp: Timestamp) -> Result<Decimal> {
if unit.eq_ignore_ascii_case("epoch") {
let epoch = Decimal::from_i128_with_scale(timestamp.0.timestamp_nanos() as i128, 9);
let epoch =
Decimal::from_i128_with_scale(timestamp.0.timestamp_nanos() as i128, 9).normalize();
return Ok(epoch);
} else if unit.eq_ignore_ascii_case("julian") {
let epoch = Decimal::from_i128_with_scale(timestamp.0.timestamp_nanos() as i128, 9);
let epoch =
Decimal::from_i128_with_scale(timestamp.0.timestamp_nanos() as i128, 9).normalize();
return Ok(epoch / (24 * 60 * 60).into() + 2_440_588.into());
};
extract_date(timestamp.0, unit)
Expand All @@ -100,7 +102,7 @@ pub fn extract_from_timestamp(unit: &str, timestamp: Timestamp) -> Result<Decima
#[function("extract(varchar, timestamptz) -> decimal")]
pub fn extract_from_timestamptz(unit: &str, usecs: i64) -> Result<Decimal> {
match unit {
"EPOCH" => Ok(Decimal::from_i128_with_scale(usecs as i128, 6)),
"EPOCH" => Ok(Decimal::from_i128_with_scale(usecs as i128, 6).normalize()),
// TODO(#5826): all other units depend on implicit session TimeZone
_ => Err(invalid_unit("timestamp with time zone units", unit)),
}
Expand All @@ -127,9 +129,9 @@ pub fn extract_from_interval(unit: &str, interval: Interval) -> Result<Decimal>
} else if unit.eq_ignore_ascii_case("minute") {
interval.minutes().into()
} else if unit.eq_ignore_ascii_case("second") {
Decimal::from_i128_with_scale(interval.seconds_in_microseconds() as i128, 6)
Decimal::from_i128_with_scale(interval.seconds_in_microseconds() as i128, 6).normalize()
} else if unit.eq_ignore_ascii_case("millisecond") {
Decimal::from_i128_with_scale(interval.seconds_in_microseconds() as i128, 3)
Decimal::from_i128_with_scale(interval.seconds_in_microseconds() as i128, 3).normalize()
} else if unit.eq_ignore_ascii_case("microsecond") {
interval.seconds_in_microseconds().into()
} else if unit.eq_ignore_ascii_case("epoch") {
Expand Down

0 comments on commit 8e70256

Please sign in to comment.