Skip to content

Fix hour transform #1146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bindings/python/tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_hour_transform():
]
)
result = transform.hour(arr)
expected = pa.array([19, 264420, -17072905], type=pa.int32())
expected = pa.array([19, 264420, -17072906], type=pa.int32())
assert result == expected


Expand Down
81 changes: 34 additions & 47 deletions crates/iceberg/src/transform/temporal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,12 @@ pub struct Hour;
impl Hour {
#[inline]
fn hour_timestamp_micro(v: i64) -> i32 {
(v / MICROSECONDS_PER_HOUR) as i32
v.div_euclid(MICROSECONDS_PER_HOUR) as i32
}

#[inline]
fn hour_timestamp_nano(v: i64) -> i32 {
(v / NANOSECONDS_PER_HOUR) as i32
v.div_euclid(NANOSECONDS_PER_HOUR) as i32
}
}

Expand Down Expand Up @@ -2390,18 +2390,8 @@ mod test {
transform: &BoxedTransformFunction,
expect: Datum,
) {
let timestamp = Datum::timestamp_micros(
NaiveDateTime::parse_from_str(time, "%Y-%m-%d %H:%M:%S.%f")
.unwrap()
.and_utc()
.timestamp_micros(),
);
let timestamp_tz = Datum::timestamptz_micros(
NaiveDateTime::parse_from_str(time, "%Y-%m-%d %H:%M:%S.%f")
.unwrap()
.and_utc()
.timestamp_micros(),
);
let timestamp = Datum::timestamp_from_str(time).unwrap();
let timestamp_tz = Datum::timestamptz_from_str(time.to_owned() + " +00:00").unwrap();
let res = transform.transform_literal(&timestamp).unwrap().unwrap();
assert_eq!(res, expect);
let res = transform.transform_literal(&timestamp_tz).unwrap().unwrap();
Expand Down Expand Up @@ -2432,20 +2422,8 @@ mod test {
transform: &BoxedTransformFunction,
expect: Datum,
) {
let timestamp_ns = Datum::timestamp_nanos(
NaiveDateTime::parse_from_str(time, "%Y-%m-%d %H:%M:%S.%f")
.unwrap()
.and_utc()
.timestamp_nanos_opt()
.unwrap(),
);
let timestamptz_ns = Datum::timestamptz_nanos(
NaiveDateTime::parse_from_str(time, "%Y-%m-%d %H:%M:%S.%f")
.unwrap()
.and_utc()
.timestamp_nanos_opt()
.unwrap(),
);
let timestamp_ns = Datum::timestamp_from_str(time).unwrap();
let timestamptz_ns = Datum::timestamptz_from_str(time.to_owned() + " +00:00").unwrap();
let res = transform.transform_literal(&timestamp_ns).unwrap().unwrap();
assert_eq!(res, expect);
let res = transform
Expand Down Expand Up @@ -2485,15 +2463,15 @@ mod test {
&year,
Datum::int(1970 - super::UNIX_EPOCH_YEAR),
);
test_timestamp_and_tz_transform("1969-01-01 00:00:00.00", &year, Datum::int(-1));
test_timestamp_and_tz_transform("1969-01-01T00:00:00.000000", &year, Datum::int(-1));

// Test TimestampNanosecond
test_timestamp_ns_and_tz_transform_using_i64(
186280000000,
&year,
Datum::int(1970 - super::UNIX_EPOCH_YEAR),
);
test_timestamp_ns_and_tz_transform("1969-01-01 00:00:00.00", &year, Datum::int(-1));
test_timestamp_ns_and_tz_transform("1969-01-01T00:00:00.000000", &year, Datum::int(-1));
}

#[test]
Expand Down Expand Up @@ -2584,21 +2562,21 @@ mod test {
&month,
Datum::int((1970 - super::UNIX_EPOCH_YEAR) * 12),
);
test_timestamp_and_tz_transform("1969-12-01 23:00:00.00", &month, Datum::int(-1));
test_timestamp_and_tz_transform("2017-12-01 00:00:00.00", &month, Datum::int(575));
test_timestamp_and_tz_transform("1970-01-01 00:00:00.00", &month, Datum::int(0));
test_timestamp_and_tz_transform("1969-12-31 00:00:00.00", &month, Datum::int(-1));
test_timestamp_and_tz_transform("1969-12-01T23:00:00.000000", &month, Datum::int(-1));
test_timestamp_and_tz_transform("2017-12-01T00:00:00.000000", &month, Datum::int(575));
test_timestamp_and_tz_transform("1970-01-01T00:00:00.000000", &month, Datum::int(0));
test_timestamp_and_tz_transform("1969-12-31T00:00:00.000000", &month, Datum::int(-1));

// Test TimestampNanosecond
test_timestamp_ns_and_tz_transform_using_i64(
186280000000,
&month,
Datum::int((1970 - super::UNIX_EPOCH_YEAR) * 12),
);
test_timestamp_ns_and_tz_transform("1969-12-01 23:00:00.00", &month, Datum::int(-1));
test_timestamp_ns_and_tz_transform("2017-12-01 00:00:00.00", &month, Datum::int(575));
test_timestamp_ns_and_tz_transform("1970-01-01 00:00:00.00", &month, Datum::int(0));
test_timestamp_ns_and_tz_transform("1969-12-31 00:00:00.00", &month, Datum::int(-1));
test_timestamp_ns_and_tz_transform("1969-12-01T23:00:00.000000", &month, Datum::int(-1));
test_timestamp_ns_and_tz_transform("2017-12-01T00:00:00.000000", &month, Datum::int(575));
test_timestamp_ns_and_tz_transform("1970-01-01T00:00:00.000000", &month, Datum::int(0));
test_timestamp_ns_and_tz_transform("1969-12-31T00:00:00.000000", &month, Datum::int(-1));
}

#[test]
Expand Down Expand Up @@ -2689,12 +2667,12 @@ mod test {
// Test TimestampMicrosecond
test_timestamp_and_tz_transform_using_i64(1512151975038194, &day, Datum::date(17501));
test_timestamp_and_tz_transform_using_i64(-115200000000, &day, Datum::date(-2));
test_timestamp_and_tz_transform("2017-12-01 10:30:42.123", &day, Datum::date(17501));
test_timestamp_and_tz_transform("2017-12-01T10:30:42.123000", &day, Datum::date(17501));

// Test TimestampNanosecond
test_timestamp_ns_and_tz_transform_using_i64(1512151975038194, &day, Datum::date(17));
test_timestamp_ns_and_tz_transform_using_i64(-115200000000, &day, Datum::date(-1));
test_timestamp_ns_and_tz_transform("2017-12-01 10:30:42.123", &day, Datum::date(17501));
test_timestamp_ns_and_tz_transform("2017-12-01T10:30:42.123000", &day, Datum::date(17501));
}

#[test]
Expand Down Expand Up @@ -2760,14 +2738,23 @@ mod test {
fn test_transform_hours_literal() {
let hour = Box::new(super::Hour) as BoxedTransformFunction;

// Test TimestampMicrosecond
test_timestamp_and_tz_transform("2017-12-01 18:00:00.00", &hour, Datum::int(420042));
test_timestamp_and_tz_transform("1969-12-31 23:00:00.00", &hour, Datum::int(-1));
test_timestamp_and_tz_transform("0022-05-01 22:01:01.00", &hour, Datum::int(-17072905));
test_timestamp_and_tz_transform("2017-12-01T18:00:00.000000", &hour, Datum::int(420042));
test_timestamp_and_tz_transform("1970-01-01T22:01:01.000000", &hour, Datum::int(22));
test_timestamp_and_tz_transform("1969-12-31T23:00:00.000000", &hour, Datum::int(-1));
test_timestamp_and_tz_transform("1969-12-31T22:01:01.000000", &hour, Datum::int(-2));
test_timestamp_and_tz_transform("0022-05-01T22:01:01.000000", &hour, Datum::int(-17072906));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-17_072_905.98 but rounded up 👍


// Test TimestampNanosecond
test_timestamp_ns_and_tz_transform("2017-12-01 18:00:00.00", &hour, Datum::int(420042));
test_timestamp_ns_and_tz_transform("1969-12-31 23:00:00.00", &hour, Datum::int(-1));
test_timestamp_ns_and_tz_transform("1900-05-01 22:01:01.00", &hour, Datum::int(-610705));
test_timestamp_ns_and_tz_transform(
"2017-12-01T18:00:00.0000000000",
&hour,
Datum::int(420042),
);
test_timestamp_ns_and_tz_transform("1969-12-31T23:00:00.0000000000", &hour, Datum::int(-1));
test_timestamp_ns_and_tz_transform(
"1900-05-01T22:01:01.0000000000",
&hour,
Datum::int(-610706),
);
}
}
Loading