Skip to content

Commit c3b3ba9

Browse files
committed
Fix hour transform
Seems like we're rounding towards zero by default by Rust. While we want to `floor` the value.
1 parent 4efcd86 commit c3b3ba9

File tree

1 file changed

+16
-37
lines changed

1 file changed

+16
-37
lines changed

crates/iceberg/src/transform/temporal.rs

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ use crate::spec::{Datum, PrimitiveLiteral, PrimitiveType};
3131
use crate::{Error, ErrorKind, Result};
3232

3333
/// Microseconds in one hour.
34-
const MICROSECONDS_PER_HOUR: i64 = 3_600_000_000;
34+
const MICROSECONDS_PER_HOUR: f64 = 3_600_000_000_f64;
3535
/// Nanoseconds in one hour.
36-
const NANOSECONDS_PER_HOUR: i64 = 3_600_000_000_000;
36+
const NANOSECONDS_PER_HOUR: f64 = 3_600_000_000_000_f64;
3737
/// Year of unix epoch.
3838
const UNIX_EPOCH_YEAR: i32 = 1970;
3939
/// One second in micros.
@@ -337,12 +337,12 @@ pub struct Hour;
337337
impl Hour {
338338
#[inline]
339339
fn hour_timestamp_micro(v: i64) -> i32 {
340-
(v / MICROSECONDS_PER_HOUR) as i32
340+
(v as f64 / MICROSECONDS_PER_HOUR.as_f64()).floor() as i32
341341
}
342342

343343
#[inline]
344344
fn hour_timestamp_nano(v: i64) -> i32 {
345-
(v / NANOSECONDS_PER_HOUR) as i32
345+
(v as f64 / NANOSECONDS_PER_HOUR.as_f64()).floor() as i32
346346
}
347347
}
348348

@@ -2390,18 +2390,8 @@ mod test {
23902390
transform: &BoxedTransformFunction,
23912391
expect: Datum,
23922392
) {
2393-
let timestamp = Datum::timestamp_micros(
2394-
NaiveDateTime::parse_from_str(time, "%Y-%m-%d %H:%M:%S.%f")
2395-
.unwrap()
2396-
.and_utc()
2397-
.timestamp_micros(),
2398-
);
2399-
let timestamp_tz = Datum::timestamptz_micros(
2400-
NaiveDateTime::parse_from_str(time, "%Y-%m-%d %H:%M:%S.%f")
2401-
.unwrap()
2402-
.and_utc()
2403-
.timestamp_micros(),
2404-
);
2393+
let timestamp = Datum::timestamp_from_str(time).unwrap();
2394+
let timestamp_tz = Datum::timestamptz_from_str(time.to_owned() + " +00:00").unwrap();
24052395
let res = transform.transform_literal(&timestamp).unwrap().unwrap();
24062396
assert_eq!(res, expect);
24072397
let res = transform.transform_literal(&timestamp_tz).unwrap().unwrap();
@@ -2432,20 +2422,8 @@ mod test {
24322422
transform: &BoxedTransformFunction,
24332423
expect: Datum,
24342424
) {
2435-
let timestamp_ns = Datum::timestamp_nanos(
2436-
NaiveDateTime::parse_from_str(time, "%Y-%m-%d %H:%M:%S.%f")
2437-
.unwrap()
2438-
.and_utc()
2439-
.timestamp_nanos_opt()
2440-
.unwrap(),
2441-
);
2442-
let timestamptz_ns = Datum::timestamptz_nanos(
2443-
NaiveDateTime::parse_from_str(time, "%Y-%m-%d %H:%M:%S.%f")
2444-
.unwrap()
2445-
.and_utc()
2446-
.timestamp_nanos_opt()
2447-
.unwrap(),
2448-
);
2425+
let timestamp_ns = Datum::timestamp_from_str(time).unwrap();
2426+
let timestamptz_ns = Datum::timestamptz_from_str(time.to_owned() + " +00:00").unwrap();
24492427
let res = transform.transform_literal(&timestamp_ns).unwrap().unwrap();
24502428
assert_eq!(res, expect);
24512429
let res = transform
@@ -2760,14 +2738,15 @@ mod test {
27602738
fn test_transform_hours_literal() {
27612739
let hour = Box::new(super::Hour) as BoxedTransformFunction;
27622740

2763-
// Test TimestampMicrosecond
2764-
test_timestamp_and_tz_transform("2017-12-01 18:00:00.00", &hour, Datum::int(420042));
2765-
test_timestamp_and_tz_transform("1969-12-31 23:00:00.00", &hour, Datum::int(-1));
2766-
test_timestamp_and_tz_transform("0022-05-01 22:01:01.00", &hour, Datum::int(-17072905));
2741+
test_timestamp_and_tz_transform("2017-12-01T18:00:00.000000", &hour, Datum::int(420042));
2742+
test_timestamp_and_tz_transform("1970-01-01T22:01:01.000000", &hour, Datum::int(22));
2743+
test_timestamp_and_tz_transform("1969-12-31T23:00:00.000000", &hour, Datum::int(-1));
2744+
test_timestamp_and_tz_transform("1969-12-31T22:01:01.000000", &hour, Datum::int(-2));
2745+
test_timestamp_and_tz_transform("0022-05-01T22:01:01.000000", &hour, Datum::int(-17072906));
27672746

27682747
// Test TimestampNanosecond
2769-
test_timestamp_ns_and_tz_transform("2017-12-01 18:00:00.00", &hour, Datum::int(420042));
2770-
test_timestamp_ns_and_tz_transform("1969-12-31 23:00:00.00", &hour, Datum::int(-1));
2771-
test_timestamp_ns_and_tz_transform("1900-05-01 22:01:01.00", &hour, Datum::int(-610705));
2748+
test_timestamp_ns_and_tz_transform("2017-12-01T18:00:00.0000000000", &hour, Datum::int(420042));
2749+
test_timestamp_ns_and_tz_transform("1969-12-31T23:00:00.0000000000", &hour, Datum::int(-1));
2750+
test_timestamp_ns_and_tz_transform("1900-05-01T22:01:01.0000000000", &hour, Datum::int(-610706));
27722751
}
27732752
}

0 commit comments

Comments
 (0)