Skip to content

date_trunc array fast path underflows for extreme timestamps #25129

Description

@benbellick

Describe the bug

date_trunc can underflow when its array fast path truncates an extreme timestamp. In debug builds, the unchecked subtraction can panic. In release builds, it can wrap and return an incorrect timestamp.

The scalar path handles out-of-range arithmetic by returning a DataFusion error, but general_date_trunc_array_fine_granularity performs the truncation with unchecked arithmetic:

*v - i64::rem_euclid(*v, unit)

This was identified while reviewing #24501. The optimizer proposed there evaluates scalar boundary values, while normal query execution uses the array path. These paths should have consistent overflow behavior before date_trunc is used to establish partitioning guarantees.

To Reproduce

CREATE TABLE extreme_times AS
SELECT arrow_cast(value, 'Timestamp(Nanosecond, None)') AS ts
FROM (VALUES (-9223372036854775807 - 1)) AS t(value);

SELECT ts, date_trunc('microsecond', ts) AS truncated
FROM extreme_times;

DataFusion fiddle

On DataFusion CLI 54.1.0 in a release build, this returns:

+-------------------------------+-------------------------------+
| ts                            | truncated                     |
+-------------------------------+-------------------------------+
| 1677-09-21T00:12:43.145224192 | 2262-04-11T23:47:16.854775616 |
+-------------------------------+-------------------------------+

Truncating the year 1677 timestamp should not produce a timestamp in 2262.

Expected behavior

Return a DataFusion error when the truncated timestamp cannot be represented by the timestamp's underlying i64, rather than panicking or wrapping.

The array path should behave consistently with the checked scalar path.

Additional context

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions