Skip to content

date_trunc panics (remainder with a divisor of zero) for intervals finer than the timestamp's time unit #7713

Open

Description

@jonasdedden

Environment: daft 0.7.24, Python 3.14, Linux. The code involved is unchanged on main (865feff).

import datetime as dt
import daft
import daft.functions as F
from daft import col

df = daft.from_pydict({"t": [dt.datetime(2021, 3, 1, 12, 34, 56, 49012)]})  # Timestamp[us]
df.select(F.date_trunc("1 nanosecond", col("t"))).collect()
df.select(F.date_trunc("0 days", col("t"))).collect()
print(df.select(F.date_trunc("2500 nanoseconds", col("t"))).to_pydict())

Actual:

  • The first two calls panic with attempt to calculate the remainder with a divisor of zero.
  • The third returns ...56.049012: it truncated to a 2 µs grid instead of 2500 ns.

Expected:

  • 1 nanosecond on a microsecond column: a no-op, since every value is already on that grid.
  • 0 days: a ValueError instead of a panic.
  • 2500 nanoseconds: ...56.049010, the epoch-aligned 2500 ns grid. Alternatively an error saying the interval isn't representable in the column's unit, rather than a silently different interval.

Cause: process_interval converts the interval into the column's time unit with num_microseconds() / num_seconds() etc., which truncates. Any interval finer than the unit becomes 0, and truncate then computes ts % 0. A zero-length interval reaches the same % 0.

Suggested fix: reject non-positive intervals with a ValueError, and compute the truncation in a unit fine enough for the interval (or error when the interval isn't a whole multiple of the column's unit).

Activity

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

Metadata

Metadata

Assignees

No one assigned

    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