Skip to content

Commit 52894db

Browse files
authored
fix: correct date_trunc for times before the epoch (#18356)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #18334. ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> The array-based implementation of date_trunc can produce incorrect results for negative timestamps (i.e. dates before 1970-01-01). Check for any such incorrect values and compensate accordingly. Running the date_trunc benchmark suggests this fix introduces an ~9% performance cost. ``` date_trunc_minute_1000 time: [1.7424 µs 1.7495 µs 1.7583 µs] change: [+7.9289% +8.5950% +9.1955%] (p = 0.00 < 0.05) Performance has regressed. Found 4 outliers among 100 measurements (4.00%) 1 (1.00%) low mild 1 (1.00%) high mild 2 (2.00%) high severe ``` ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Yes, an SLT is added based on the issue. ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 342cfdf commit 52894db

File tree

2 files changed

+56
-28
lines changed

2 files changed

+56
-28
lines changed

datafusion/functions/src/datetime/date_trunc.rs

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717

1818
use std::any::Any;
19+
use std::num::NonZeroI64;
1920
use std::ops::{Add, Sub};
2021
use std::str::FromStr;
2122
use std::sync::Arc;
@@ -28,7 +29,7 @@ use arrow::array::types::{
2829
ArrowTimestampType, TimestampMicrosecondType, TimestampMillisecondType,
2930
TimestampNanosecondType, TimestampSecondType,
3031
};
31-
use arrow::array::{Array, ArrayRef, Int64Array, PrimitiveArray};
32+
use arrow::array::{Array, ArrayRef, PrimitiveArray};
3233
use arrow::datatypes::DataType::{self, Null, Timestamp, Utf8, Utf8View};
3334
use arrow::datatypes::TimeUnit::{self, Microsecond, Millisecond, Nanosecond, Second};
3435
use datafusion_common::cast::as_primitive_array;
@@ -456,37 +457,40 @@ fn general_date_trunc_array_fine_granularity<T: ArrowTimestampType>(
456457
granularity: &str,
457458
) -> Result<ArrayRef> {
458459
let unit = match (tu, granularity) {
459-
(Second, "minute") => Some(Int64Array::new_scalar(60)),
460-
(Second, "hour") => Some(Int64Array::new_scalar(3600)),
461-
(Second, "day") => Some(Int64Array::new_scalar(86400)),
462-
463-
(Millisecond, "second") => Some(Int64Array::new_scalar(1_000)),
464-
(Millisecond, "minute") => Some(Int64Array::new_scalar(60_000)),
465-
(Millisecond, "hour") => Some(Int64Array::new_scalar(3_600_000)),
466-
(Millisecond, "day") => Some(Int64Array::new_scalar(86_400_000)),
467-
468-
(Microsecond, "millisecond") => Some(Int64Array::new_scalar(1_000)),
469-
(Microsecond, "second") => Some(Int64Array::new_scalar(1_000_000)),
470-
(Microsecond, "minute") => Some(Int64Array::new_scalar(60_000_000)),
471-
(Microsecond, "hour") => Some(Int64Array::new_scalar(3_600_000_000)),
472-
(Microsecond, "day") => Some(Int64Array::new_scalar(86_400_000_000)),
473-
474-
(Nanosecond, "microsecond") => Some(Int64Array::new_scalar(1_000)),
475-
(Nanosecond, "millisecond") => Some(Int64Array::new_scalar(1_000_000)),
476-
(Nanosecond, "second") => Some(Int64Array::new_scalar(1_000_000_000)),
477-
(Nanosecond, "minute") => Some(Int64Array::new_scalar(60_000_000_000)),
478-
(Nanosecond, "hour") => Some(Int64Array::new_scalar(3_600_000_000_000)),
479-
(Nanosecond, "day") => Some(Int64Array::new_scalar(86_400_000_000_000)),
460+
(Second, "minute") => NonZeroI64::new(60),
461+
(Second, "hour") => NonZeroI64::new(3600),
462+
(Second, "day") => NonZeroI64::new(86400),
463+
464+
(Millisecond, "second") => NonZeroI64::new(1_000),
465+
(Millisecond, "minute") => NonZeroI64::new(60_000),
466+
(Millisecond, "hour") => NonZeroI64::new(3_600_000),
467+
(Millisecond, "day") => NonZeroI64::new(86_400_000),
468+
469+
(Microsecond, "millisecond") => NonZeroI64::new(1_000),
470+
(Microsecond, "second") => NonZeroI64::new(1_000_000),
471+
(Microsecond, "minute") => NonZeroI64::new(60_000_000),
472+
(Microsecond, "hour") => NonZeroI64::new(3_600_000_000),
473+
(Microsecond, "day") => NonZeroI64::new(86_400_000_000),
474+
475+
(Nanosecond, "microsecond") => NonZeroI64::new(1_000),
476+
(Nanosecond, "millisecond") => NonZeroI64::new(1_000_000),
477+
(Nanosecond, "second") => NonZeroI64::new(1_000_000_000),
478+
(Nanosecond, "minute") => NonZeroI64::new(60_000_000_000),
479+
(Nanosecond, "hour") => NonZeroI64::new(3_600_000_000_000),
480+
(Nanosecond, "day") => NonZeroI64::new(86_400_000_000_000),
480481
_ => None,
481482
};
482483

483484
if let Some(unit) = unit {
484-
let original_type = array.data_type();
485-
let array = arrow::compute::cast(array, &DataType::Int64)?;
486-
let array = arrow::compute::kernels::numeric::div(&array, &unit)?;
487-
let array = arrow::compute::kernels::numeric::mul(&array, &unit)?;
488-
let array = arrow::compute::cast(&array, original_type)?;
489-
Ok(array)
485+
let unit = unit.get();
486+
let array = PrimitiveArray::<T>::from_iter_values_with_nulls(
487+
array
488+
.values()
489+
.iter()
490+
.map(|v| *v - i64::rem_euclid(*v, unit)),
491+
array.nulls().cloned(),
492+
);
493+
Ok(Arc::new(array))
490494
} else {
491495
// truncate to the same or smaller unit
492496
Ok(Arc::new(array.clone()))

datafusion/sqllogictest/test_files/timestamps.slt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,30 @@ SELECT DATE_TRUNC('second', '2022-08-03 14:38:50Z');
16871687
----
16881688
2022-08-03T14:38:50
16891689

1690+
# DATE_TRUNC handling of times before the unix epoch (issue 18334)
1691+
query PPPPPPPPPPP
1692+
SELECT
1693+
d,
1694+
DATE_TRUNC('year', d),
1695+
DATE_TRUNC('quarter', d),
1696+
DATE_TRUNC('month', d),
1697+
DATE_TRUNC('week', d),
1698+
DATE_TRUNC('day', d),
1699+
DATE_TRUNC('hour', d),
1700+
DATE_TRUNC('minute', d),
1701+
DATE_TRUNC('second', d),
1702+
DATE_TRUNC('millisecond', d),
1703+
DATE_TRUNC('microsecond', d),
1704+
FROM (VALUES
1705+
(TIMESTAMP '1900-06-15 07:09:00'),
1706+
(TIMESTAMP '1970-01-01 00:00:00'),
1707+
(TIMESTAMP '2024-12-31 23:39:01.123456789')
1708+
) AS t(d);
1709+
----
1710+
1900-06-15T07:09:00 1900-01-01T00:00:00 1900-04-01T00:00:00 1900-06-01T00:00:00 1900-06-11T00:00:00 1900-06-15T00:00:00 1900-06-15T07:00:00 1900-06-15T07:09:00 1900-06-15T07:09:00 1900-06-15T07:09:00 1900-06-15T07:09:00
1711+
1970-01-01T00:00:00 1970-01-01T00:00:00 1970-01-01T00:00:00 1970-01-01T00:00:00 1969-12-29T00:00:00 1970-01-01T00:00:00 1970-01-01T00:00:00 1970-01-01T00:00:00 1970-01-01T00:00:00 1970-01-01T00:00:00 1970-01-01T00:00:00
1712+
2024-12-31T23:39:01.123456789 2024-01-01T00:00:00 2024-10-01T00:00:00 2024-12-01T00:00:00 2024-12-30T00:00:00 2024-12-31T00:00:00 2024-12-31T23:00:00 2024-12-31T23:39:00 2024-12-31T23:39:01 2024-12-31T23:39:01.123 2024-12-31T23:39:01.123456
1713+
16901714
# Test that interval can add a timestamp
16911715
query P
16921716
SELECT timestamp '2013-07-01 12:00:00' + INTERVAL '8' DAY;

0 commit comments

Comments
 (0)