[SPARK-57662][CORE] Use unsigned byte comparison for KVStore iterator keys#56740
Open
LuciferYang wants to merge 2 commits into
Open
[SPARK-57662][CORE] Use unsigned byte comparison for KVStore iterator keys#56740LuciferYang wants to merge 2 commits into
LuciferYang wants to merge 2 commits into
Conversation
… keys ### 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
uros-b
reviewed
Jun 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
LevelDBIteratorandRocksDBIteratoreach have a staticcompare(byte[], byte[])used to order keys during iteration, and it compared bytes witha[i] - b[i]. That had two problems. Thediff += ...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 usememcmp). 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 toArrays.compareUnsigned, the JDK method that performs exactly an unsigned lexicographic byte-array comparison, and they are marked@VisibleForTestingto match the siblingstartsWithhelper.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 as0x80 > 0x7fand0xff > 0x00. Reverting either method toArrays.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