Skip to content

BinaryComparator.compare(): TYPE_STRING vs DATE/DATETIME still falls through to lexicographic comparison #5947

Closed

Description

@lvca

Summary

BinaryComparator.compare()'s TYPE_STRING branch (fixed for numeric/boolean type2 by PR #5922, closing #5900) still falls through to an unconditional lexicographic ((String) value1).compareTo(value2.toString()) when type2 is TYPE_DATE, TYPE_DATETIME, TYPE_DATETIME_SECOND, TYPE_DATETIME_MICROS, or TYPE_DATETIME_NANOS:

switch (type2) {
case BinaryTypes.TYPE_INT:
case BinaryTypes.TYPE_SHORT:
case BinaryTypes.TYPE_BYTE:
case BinaryTypes.TYPE_LONG:
case BinaryTypes.TYPE_FLOAT:
case BinaryTypes.TYPE_DOUBLE:
case BinaryTypes.TYPE_DECIMAL:
case BinaryTypes.TYPE_BOOLEAN:
  return -compare(value2, type2, value1, type1);

default:
  return ((String) value1).compareTo(value2.toString());  // DATE/DATETIME land here
}

This is pre-existing behavior, not a regression from #5922 - the old code did the same unconditional lexicographic comparison for every type2, including dates. It's called out here because #5922 explicitly closed the equivalent gap for every other type2 category (numeric, boolean), so this is the one category left with the old, potentially-inconsistent behavior.

Scope note

Unlike the numeric/boolean fix (a mechanical "delegate to the numeric widening comparator and negate"), fixing this properly requires actual date-string parsing with all the format/timezone ambiguity that implies, so it's a meaningfully different and larger piece of work - not a good fit to fold into #5922's mechanical fix. Filed separately per the review discussion on that PR.

Suggested approach

Decide what a TYPE_STRING vs TYPE_DATE/TYPE_DATETIME comparison should mean (likely: attempt to parse value1 as a date using the same parsing DateConstructorFunction/DateTimeConstructorFunction use, then delegate to the existing TYPE_DATE/TYPE_DATETIME branch's DateUtils.dateTimeToTimestamp comparison; fall back to lexicographic - or a defined error - if the string doesn't parse as a date). Whatever is chosen should be antisymmetric with compare(dateValue, TYPE_DATETIME, dateString, TYPE_STRING), matching the contract #5900/#5922 established for every other branch in this class.

Context

Raised during code review on PR #5922 (fixing #5900). See that PR and BinaryComparatorNarrowingTest for the established test patterns (direct sgn(compare(a,b)) == -sgn(compare(b,a)) assertions) this fix should follow.

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 a commit that references this issue on Aug 8, 2026
  5. added
    engine
    on Sep 2, 2026
  6. added a commit that references this issue on Sep 16, 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