Repository navigation
Description
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
address code review: scope precision inference to comparison context,…
fix(#5956): numeric-string date/datetime comparison infers precision …
fix(#8152): read a numeric-string moment at its own epoch precision, …