Skip to content

[SPARK-57662][CORE] Use unsigned byte comparison for KVStore iterator keys#56740

Open
LuciferYang wants to merge 2 commits into
apache:masterfrom
LuciferYang:SPARK-kvstore-iterator-compare
Open

[SPARK-57662][CORE] Use unsigned byte comparison for KVStore iterator keys#56740
LuciferYang wants to merge 2 commits into
apache:masterfrom
LuciferYang:SPARK-kvstore-iterator-compare

Conversation

@LuciferYang

Copy link
Copy Markdown
Contributor

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

… 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 uros-b left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @LuciferYang!

@dongjoon-hyun dongjoon-hyun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants