Skip to content

Commit a167995

Browse files
benbellickhaohuaijin
authored andcommitted
fix(datetime): floor negative scalar timestamps in date_trunc (apache#25430)
## Which issue does this PR close? Closes apache#25425. ## Rationale for this change Scalar `date_trunc` rounds negative subsecond timestamps toward zero, while array evaluation floors them to the start of the containing unit. This makes the result depend on whether the same value is evaluated as a scalar or a column. ## What changes are included in this PR? Use checked Euclidean truncation for scalar microsecond-to-millisecond and nanosecond-to-microsecond/millisecond conversions. This matches array semantics and returns an error when flooring would underflow. ## Are these changes tested? Yes. Unit tests cover negative timestamps for all three affected conversions and underflow at the nanosecond lower bound. The full workspace extended test suite and strict all-target, all-feature Clippy checks pass. ## Are there any user-facing changes? Scalar and array `date_trunc` now agree for negative subsecond timestamps. --- Note: This PR was developed with AI assistance. All changes have been reviewed, and I take full responsibility for this contribution.
1 parent 656e425 commit a167995

1 file changed

Lines changed: 38 additions & 4 deletions

File tree

‎datafusion/functions/src/datetime/date_trunc.rs‎

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,11 @@ fn general_date_trunc(
833833
tz,
834834
)?;
835835

836+
let truncate_to = |value: i64, unit: i64| {
837+
value
838+
.checked_sub(value.rem_euclid(unit))
839+
.ok_or_else(|| exec_datafusion_err!("Timestamp {value} out of range"))
840+
};
836841
let result = match tu {
837842
Second => match granularity {
838843
DatePart::Minute => nano / 1_000_000_000 / 60 * 60,
@@ -846,14 +851,14 @@ fn general_date_trunc(
846851
Microsecond => match granularity {
847852
DatePart::Minute => nano / 1_000 / 1_000_000 / 60 * 60 * 1_000_000,
848853
DatePart::Second => nano / 1_000 / 1_000_000 * 1_000_000,
849-
DatePart::Millisecond => nano / 1_000 / 1_000 * 1_000,
854+
DatePart::Millisecond => truncate_to(nano / 1_000, 1_000)?,
850855
_ => nano / 1_000,
851856
},
852857
_ => match granularity {
853858
DatePart::Minute => nano / 1_000_000_000 / 60 * 1_000_000_000 * 60,
854859
DatePart::Second => nano / 1_000_000_000 * 1_000_000_000,
855-
DatePart::Millisecond => nano / 1_000_000 * 1_000_000,
856-
DatePart::Microsecond => nano / 1_000 * 1_000,
860+
DatePart::Millisecond => truncate_to(nano, 1_000_000)?,
861+
DatePart::Microsecond => truncate_to(nano, 1_000)?,
857862
_ => nano,
858863
},
859864
};
@@ -873,7 +878,7 @@ mod tests {
873878
use std::sync::Arc;
874879

875880
use crate::datetime::date_trunc::{
876-
DateTruncFunc, NANOS_PER_MICROSECOND, date_trunc_coarse,
881+
DateTruncFunc, NANOS_PER_MICROSECOND, date_trunc_coarse, general_date_trunc,
877882
general_date_trunc_array_fine_granularity, parse_granularity,
878883
};
879884

@@ -1361,6 +1366,35 @@ mod tests {
13611366
}
13621367
}
13631368

1369+
#[test]
1370+
fn scalar_fine_granularity_floors_negative_timestamps() {
1371+
for (unit, value, granularity, expected) in [
1372+
(TimeUnit::Microsecond, -999, DatePart::Millisecond, -1_000),
1373+
(TimeUnit::Nanosecond, -999, DatePart::Microsecond, -1_000),
1374+
(
1375+
TimeUnit::Nanosecond,
1376+
-999_999,
1377+
DatePart::Millisecond,
1378+
-1_000_000,
1379+
),
1380+
] {
1381+
assert_eq!(
1382+
general_date_trunc(unit, value, None, granularity).unwrap(),
1383+
expected
1384+
);
1385+
}
1386+
}
1387+
1388+
#[test]
1389+
fn scalar_fine_granularity_rejects_underflow() {
1390+
for granularity in [DatePart::Microsecond, DatePart::Millisecond] {
1391+
assert!(
1392+
general_date_trunc(TimeUnit::Nanosecond, i64::MIN, None, granularity,)
1393+
.is_err()
1394+
);
1395+
}
1396+
}
1397+
13641398
fn assert_fine_granularity_underflow<T: ArrowTimestampType>(
13651399
array: PrimitiveArray<T>,
13661400
granularity: DatePart,

0 commit comments

Comments
 (0)