Skip to content

BinaryComparator orders two STRINGs by UTF-16 code units while LSM pages order them by UTF-8 bytes, so an indexed range scan can return zero rows #6997

Closed

Description

@ruispereira

Summary

BinaryComparator carries two mutually inconsistent orderings for two STRINGs. LSM pages store and seek STRING keys in unsigned UTF-8 byte order, but the range cursor's stop condition compares the un-encoded bounds through compare(), whose String/String fallback is String.compareTo - UTF-16 code-unit order. For any pair where the two orders disagree, an indexed range scan returns zero rows although both keys are in range.

Where (HEAD 8cfac2923)

engine/src/main/java/com/arcadedb/serializer/BinaryComparator.java:95 - the fallback that decides the cursor's stop condition:

return ((String) value1).compareTo(value2.toString());

The sibling static entry point in the same class does it the other way, :455 - it compares the UTF-8 bytes. So the class answers differently depending on which door you come in.

On the storage side the order is UTF-8 bytes: LSMTreeIndexAbstract.convertKeys() encodes a String bound as string.getBytes(DatabaseFactory.getDefaultCharset()) (:463) and lookupInPage compares with comparator.compareBytes(...) (:566), unsigned since #5321.

The desync is in the cursor: LSMTreeIndexCursor:262-266 builds serializedFromKeys/serializedToKeys as byte[] via convertKeys, but binds typedFromKeys/typedToKeys via convertKeysToDeclaredTypes, which deliberately skips the encoding. :267 then compares plain Strings and :270's compare <= 0 test decides whether to keep going.

Repro

Verified at 8cfac2923. "A" (U+FF21 FULLWIDTH A) and "😀" (U+1F600):

FW  U+FF21  utf8=EF BC A1
EMO U+1F600 utf8=F0 9F 98 80
String.compareTo (UTF-16):  +1     <- FW sorts after
unsigned UTF-8 bytes:       -1     <- FW sorts before

compare(a,TYPE_STRING,b,TYPE_STRING) = +1
BinaryComparator.compareTo(a,b)      = -1     <- same class, opposite answers

With an LSM_TREE index on a STRING property holding both values:

SELECT k FROM S WHERE k <= <emoji>   ->  ROWS=0      (both keys are in range)
SELECT k FROM S                      ->  ROWS=2      (sanity)
SELECT k FROM S WHERE k >= <FW>      ->  ROWS=2

The cursor reaches "A", compare reports > 0, the compare <= 0 test at LSMTreeIndexCursor:270 fails, the page cursor is closed at :273, and the scan ends having emitted nothing.

Every non-BMP character starts with a high surrogate 0xD800-0xDBFF in UTF-16 but a 0xF0-0xF4 lead byte in UTF-8, so it sorts below U+E000..U+FFFF in one order and above it in the other. Mixed CJK-fullwidth and emoji data hits this with no exotic query.

Suggested fix

At :95, when type2 == BinaryTypes.TYPE_STRING, compare the UTF-8 encodings rather than the UTF-16 chars:

compareBytes(((String) value1).getBytes(DatabaseFactory.getDefaultCharset()),
             ((String) value2).getBytes(DatabaseFactory.getDefaultCharset()))

matching :455 and lookupInPage. Keep value2.toString() only for a genuinely non-String type2.

A regression test asserting that compare(a, TYPE_STRING, b, TYPE_STRING) and BinaryComparator.compareTo(a, b) have the same sign for a BMP/non-BMP pair would pin the invariant, and an index range-scan test over those two keys would pin the consequence.

Scope / confidence

High - reproduced. Verified the cursor bounds really are un-encoded (LSMTreeIndexAbstract:463-486's javadoc says the byte[] form is "purely local to this constructor"), and that no existing test pins UTF-16 order: BinaryComparatorNarrowingTest:209-210 asserts only "2" > "10" and "abc" < "abd", both pure ASCII, where the two orders agree. #5321 fixed the signed-vs-unsigned half of this same desync; the UTF-16-vs-UTF-8 half is not filed.

Activity

  1. added
    engine
    storage
    on Sep 1, 2026
  2. added this to the 26.9.1 milestone on Sep 1, 2026
  3. added
    in progress
    on Sep 3, 2026
  4. self-assigned this
    on Sep 3, 2026
  5. added a commit that references this issue on Sep 3, 2026
  6. lvca commented on Sep 3, 2026

    @lvca
    Member

    Fixed in #7090 (merged in main, will be in 26.9.1).

    Two strings are now always compared in the same order the index stores them (UTF-8 byte order), so a range query such as WHERE k <= '😀' returns every key in range even when the data mixes emoji or other non-BMP characters with characters like fullwidth letters. The new comparison is also allocation-free, so index range scans do a little less work than before.

    ❤️ Thanks for helping make ArcadeDB better! ArcadeDB is free and open source, sustained by its community. If it's useful to you or your company, please consider becoming a sponsor to keep development going.

  7. removed
    in progress
    on Sep 3, 2026
  8. 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

bugenginestorage

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions