You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2026/09/18 19:10:52 UTC
(datafusion) branch main updated: fix(datetime): floor negative scalar timestamps in date_trunc (#25430)
This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new cb14804859 fix(datetime): floor negative scalar timestamps in date_trunc (#25430)
cb14804859 is described below
commit cb148048597f09fd05172a3e5f98b9e64a2c4a34
Author: Ben Bellick <36...@users.noreply.github.com>
AuthorDate: Fri Sep 18 19:00:33 2026 +0000
fix(datetime): floor negative scalar timestamps in date_trunc (#25430)
## Which issue does this PR close?
Closes #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.
---
datafusion/functions/src/datetime/date_trunc.rs | 42 ++++++++++++++++++++++---
1 file changed, 38 insertions(+), 4 deletions(-)
diff --git a/datafusion/functions/src/datetime/date_trunc.rs b/datafusion/functions/src/datetime/date_trunc.rs
index 0ed61a29b3..e61637c093 100644
--- a/datafusion/functions/src/datetime/date_trunc.rs
+++ b/datafusion/functions/src/datetime/date_trunc.rs
@@ -833,6 +833,11 @@ fn general_date_trunc(
tz,
)?;
+ let truncate_to = |value: i64, unit: i64| {
+ value
+ .checked_sub(value.rem_euclid(unit))
+ .ok_or_else(|| exec_datafusion_err!("Timestamp {value} out of range"))
+ };
let result = match tu {
Second => match granularity {
DatePart::Minute => nano / 1_000_000_000 / 60 * 60,
@@ -846,14 +851,14 @@ fn general_date_trunc(
Microsecond => match granularity {
DatePart::Minute => nano / 1_000 / 1_000_000 / 60 * 60 * 1_000_000,
DatePart::Second => nano / 1_000 / 1_000_000 * 1_000_000,
- DatePart::Millisecond => nano / 1_000 / 1_000 * 1_000,
+ DatePart::Millisecond => truncate_to(nano / 1_000, 1_000)?,
_ => nano / 1_000,
},
_ => match granularity {
DatePart::Minute => nano / 1_000_000_000 / 60 * 1_000_000_000 * 60,
DatePart::Second => nano / 1_000_000_000 * 1_000_000_000,
- DatePart::Millisecond => nano / 1_000_000 * 1_000_000,
- DatePart::Microsecond => nano / 1_000 * 1_000,
+ DatePart::Millisecond => truncate_to(nano, 1_000_000)?,
+ DatePart::Microsecond => truncate_to(nano, 1_000)?,
_ => nano,
},
};
@@ -873,7 +878,7 @@ mod tests {
use std::sync::Arc;
use crate::datetime::date_trunc::{
- DateTruncFunc, NANOS_PER_MICROSECOND, date_trunc_coarse,
+ DateTruncFunc, NANOS_PER_MICROSECOND, date_trunc_coarse, general_date_trunc,
general_date_trunc_array_fine_granularity, parse_granularity,
};
@@ -1361,6 +1366,35 @@ mod tests {
}
}
+ #[test]
+ fn scalar_fine_granularity_floors_negative_timestamps() {
+ for (unit, value, granularity, expected) in [
+ (TimeUnit::Microsecond, -999, DatePart::Millisecond, -1_000),
+ (TimeUnit::Nanosecond, -999, DatePart::Microsecond, -1_000),
+ (
+ TimeUnit::Nanosecond,
+ -999_999,
+ DatePart::Millisecond,
+ -1_000_000,
+ ),
+ ] {
+ assert_eq!(
+ general_date_trunc(unit, value, None, granularity).unwrap(),
+ expected
+ );
+ }
+ }
+
+ #[test]
+ fn scalar_fine_granularity_rejects_underflow() {
+ for granularity in [DatePart::Microsecond, DatePart::Millisecond] {
+ assert!(
+ general_date_trunc(TimeUnit::Nanosecond, i64::MIN, None, granularity,)
+ .is_err()
+ );
+ }
+ }
+
fn assert_fine_granularity_underflow<T: ArrowTimestampType>(
array: PrimitiveArray<T>,
granularity: DatePart,
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org