Skip to content

BinaryComparator: numeric-string date/datetime comparison ignores target precision #5956

Closed

Description

@lvca

Summary

DateUtils.dateTimeToTimestamp()'s String branch, when the string is purely numeric (FileUtils.isLong(string)), treats it as an already-in-the-target-unit timestamp via a direct Long.parseLong(string) - with no regard to which ChronoUnit precision the caller actually asked for:

else if (value instanceof String string) {
  if (FileUtils.isLong(string))
    timestamp = Long.parseLong(value.toString());
  else
    return dateTimeToTimestamp(database, parseIsoDateTime(database, string), precisionToUse);
}

So a numeric string is silently assumed to already be at precisionToUse's precision. When BinaryComparator.compare()'s TYPE_DATE/TYPE_DATETIME* branch computes higherPrecision via DateUtils.getHigherPrecision(value1, value2), that precision is derived purely from the non-String operand's actual runtime type (e.g. Date/Calendar force MILLIS; a LocalDateTime/ZonedDateTime/Instant resolve to their own sub-second precision). A numeric string holding a different precision than what getHigherPrecision picks (e.g. a nanos-epoch string compared against a plain java.util.Date, which forces MILLIS) is misinterpreted by several orders of magnitude.

Scope note

This is pre-existing behavior in DateUtils, not introduced by #5947/PR #5955 - it was already reachable via compare(dateValue, TYPE_DATE, numericString, TYPE_STRING) (type1 = date) before that PR. #5947's fix (delegate TYPE_STRING vs TYPE_DATE* to the reverse call instead of falling through to lexicographic compareTo()) adds a second call shape that reaches the same pre-existing gap (compare(numericString, TYPE_STRING, dateValue, TYPE_DATE)), which is how it surfaced during that PR's review. Flagged separately since fixing it means giving dateTimeToTimestamp a way to know (or infer) a numeric string's own precision, which is a different, larger piece of work than #5947's mechanical delegate-and-negate fix - same reasoning #5947 itself used to split off from #5900/#5922.

As with the sibling BinaryComparator gaps #5900/#5945, BinaryComparator.compare(Object,byte,Object,byte) is currently only invoked from the LSM index hot path with matching types after key normalization, so this is a latent public-API contract gap rather than a currently-reachable SQL bug.

Suggested approach

Decide what precision a bare numeric string should be assumed to carry when compared against a date/datetime value - options include: always MILLIS (today's most common getHigherPrecision default, but silently wrong for a NANOS-precision comparison), inferring precision from the string's own digit count (a 13-digit value looks like millis, 19 digits looks like nanos, etc.), or requiring an explicit typed value (not a bare numeric string) for anything other than MILLIS. Whatever is chosen should keep antisymmetry with the reverse call, per the established BinaryComparatorNarrowingTest pattern.

Context

Raised during code review on PR #5955 (fixing #5947, itself a follow-up to #5900/#5922). See BinaryComparator.compare()'s TYPE_DATE/TYPE_DATETIME* branch and DateUtils.dateTimeToTimestamp()/getHigherPrecision().

Activity

  1. self-assigned this
    on Aug 8, 2026
  2. added theissue type on Aug 8, 2026
  3. added this to the 26.9.1 milestone on Aug 8, 2026
  4. added 2 commits that reference this issue on Aug 8, 2026
  5. added
    engine
    on Sep 2, 2026
  6. added a commit that references this issue on Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugengine

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions