Skip to content

[Bug] Negative time-of-day when casting pre-epoch timestamp to TIME #8766

Description

@thswlsqls

Search before asking

  • I searched in the issues and found nothing similar.

Paimon version
master @ 345526e

Compute Engine
Engine-agnostic (core) — casting in paimon-common

Minimal reproduce step
Cast a pre-epoch TIMESTAMP to TIME. TimestampToTimeCastRule.create() (paimon-common/src/main/java/org/apache/paimon/casting/TimestampToTimeCastRule.java line 48) computes the time-of-day as value.getMillisecond() % MILLIS_PER_DAY. For a negative millisecond (before 1970-01-01) Java's % returns a negative remainder, so the TIME internal value falls outside the valid millis-of-day range [0, 86_399_999].

Example: 1969-12-31 23:00:00 has millisecond -3_600_000; -3_600_000 % 86_400_000 = -3_600_000 instead of 82_800_000 (23:00:00).

What doesn't meet your expectations?
Expected the cast to yield 23:00:00 (82_800_000). Actual result is a negative, invalid time-of-day.

Anything else?
The sibling TIMESTAMP_WITH_LOCAL_TIME_ZONE branch delegates to DateTimeUtils.timestampWithLocalZoneToTime (always valid), and DateTimeUtils.formatTimestampMillis already normalizes negatives (while (time < 0) time += MILLIS_PER_DAY). Only the modulo branch omits normalization. Fix: use Math.floorMod.

Are you willing to submit a PR?

  • I'm willing to submit a PR!

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