Skip to content

Fix rounding of negative hour transform #1128

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 2 commits into from
Mar 24, 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
10 changes: 8 additions & 2 deletions bindings/python/tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,15 @@ def test_day_transform():


def test_hour_transform():
arr = pa.array([datetime(1970, 1, 1, 19, 1, 23), datetime(2000, 3, 1, 12, 1, 23)])
arr = pa.array(
[
datetime(1970, 1, 1, 19, 1, 23),
datetime(2000, 3, 1, 12, 1, 23),
datetime(22, 5, 1, 22, 1, 1), # Negative
]
)
result = transform.hour(arr)
expected = pa.array([19, 264420], type=pa.int32())
expected = pa.array([19, 264420, -17072905], type=pa.int32())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This example comes from apache/iceberg-python#1833

assert result == expected


Expand Down
12 changes: 8 additions & 4 deletions crates/iceberg/src/transform/temporal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ use super::TransformFunction;
use crate::spec::{Datum, PrimitiveLiteral, PrimitiveType};
use crate::{Error, ErrorKind, Result};

/// Hour in one second.
const HOUR_PER_SECOND: f64 = 1.0_f64 / 3600.0_f64;
/// Microseconds in one hour.
const MICROSECONDS_PER_HOUR: i64 = 3_600_000_000;
/// Nanoseconds in one hour.
const NANOSECONDS_PER_HOUR: i64 = 3_600_000_000_000;
/// Year of unix epoch.
const UNIX_EPOCH_YEAR: i32 = 1970;
/// One second in micros.
Expand Down Expand Up @@ -335,12 +337,12 @@ pub struct Hour;
impl Hour {
#[inline]
fn hour_timestamp_micro(v: i64) -> i32 {
(v as f64 / 1000.0 / 1000.0 * HOUR_PER_SECOND) as i32
(v / MICROSECONDS_PER_HOUR) as i32
}

#[inline]
fn hour_timestamp_nano(v: i64) -> i32 {
(v as f64 / 1_000_000.0 / 1000.0 * HOUR_PER_SECOND) as i32
(v / NANOSECONDS_PER_HOUR) as i32
}
}

Expand Down Expand Up @@ -2761,9 +2763,11 @@ mod test {
// 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 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));
}
}
Loading