LuciferYang opened a new pull request, #56740:
URL: https://github.com/apache/spark/pull/56740

   ### What changes were proposed in this pull request?
   
   `LevelDBIterator` and `RocksDBIterator` each have a static `compare(byte[], 
byte[])` used to order keys during iteration, and it compared bytes with `a[i] 
- b[i]`. That had two problems. The `diff += ...` accumulation was dead: the 
loop returns on the first non-zero byte, so it was really just `=`. More 
importantly, the signed byte subtraction disagrees with how LevelDB and RocksDB 
order keys, which is unsigned bytewise (their default comparators use 
`memcmp`). For any key byte `>= 0x80`, such as the UTF-8 of a non-ASCII string 
index value, the comparator's order diverged from the store's. Both methods now 
delegate to `Arrays.compareUnsigned`, the JDK method that performs exactly an 
unsigned lexicographic byte-array comparison, and they are marked 
`@VisibleForTesting` to match the sibling `startsWith` helper.
   
   ### Why are the changes needed?
   
   The comparator should agree with the ordering the underlying store uses. 
With signed bytes it did not, so for non-ASCII keys the iterator could compare 
a key against its bounds incorrectly. ASCII keys are unaffected, which is why 
this never surfaced in practice, but the comparator was still wrong. The method 
is called at four iterator sites that use only the sign and two type-info sites 
that use `!= 0`, all of which stay correct under the change.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No. It only corrects key ordering that was previously wrong, and only for 
keys containing bytes `>= 0x80`.
   
   ### How was this patch tested?
   
   Added `DBIteratorCompareSuite`, which calls both static comparators directly 
(no database, so it runs on every platform) and checks equal arrays, prefix and 
length ordering, and the unsigned cases such as `0x80 > 0x7f` and `0xff > 
0x00`. Reverting either method to `Arrays.compare` (the signed variant) fails 
the suite, so it is a real regression guard. The full kvstore test suite passes 
locally, with the LevelDB suites skipped on Apple Silicon and covered by CI, 
and checkstyle reports no violations.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   No
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to