Repository navigation
Fix Binary.compareTo to order bytes as unsigned in UDF and Pipe APIs - #19271
Open
amalsaidov wants to merge 1 commit into
Open
amalsaidov wants to merge 1 commit into
amalsaidov wants to merge 1 commit into
Conversation
The Binary type in udf-api and pipe-api compared bytes with a signed subtraction (this.values[i] - other.values[i]). Because a Java byte is signed, any byte with the high bit set (>= 0x80) was treated as negative and sorted before ASCII bytes. That ordering disagrees with TsFile's own Binary.compareTo and with java.lang.String ordering, both of which treat bytes as unsigned. So a value containing non-ASCII UTF-8 (for example "é", encoded as 0xC3 0xA9) or binary data with high bytes sorted incorrectly relative to plain ASCII values when a UDF or pipe plugin compared Binary values. Compare each byte as an unsigned value (& 0xFF) so the result matches the lexicographic ordering used everywhere else. Added unit tests in both modules covering unsigned ordering, agreement with String ordering, ASCII ordering, and prefix/length handling.
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.
Description
Binaryinudf-apiandpipe-apicompared its bytes with a signed subtraction:A Java
byteis signed, so any byte with the high bit set (>= 0x80) is negative. That makescompareTosort such bytes before plain ASCII bytes, which disagrees with:org.apache.tsfile.utils.Binary.compareTo, which compares bytes unsigned (val[index] & 0xff), andjava.lang.Stringordering.Any UDF/UDAF or pipe plugin that sorts or compares
Binaryvalues containing non-ASCII UTF-8 text or binary data with high bytes therefore got the wrong order.Scenario and before/after
"A"is the byte0x41(65)."é"is UTF-80xC3 0xA9, so its first byte is0xC3(195 unsigned, but-61signed).new Binary("A").compareTo(new Binary("é"))< 0(A sorts first)> 0(wrong)new Binary(new byte[]{0x41}).compareTo(new Binary(new byte[]{(byte)0x80}))< 0> 0(wrong)Fix
Compare each byte as an unsigned value, matching the ordering used everywhere else:
ASCII-only comparisons are unchanged (all ASCII bytes are
< 0x80), andequals, which only checks forcompareTo == 0, is unaffected.Tests
Added
BinaryTestto both modules covering unsigned ordering, agreement withStringordering for non-ASCII text, ASCII ordering, and prefix/length handling. The unsigned-ordering and String-agreement cases fail on the old code and pass with the fix.This PR has:
AI-assisted; directed, reviewed and tested by the author (amalsaidov).