diff --git a/paimon-common/src/main/java/org/apache/paimon/predicate/CompareUtils.java b/paimon-common/src/main/java/org/apache/paimon/predicate/CompareUtils.java index 9f64d316a04b..4d7fb056bb44 100644 --- a/paimon-common/src/main/java/org/apache/paimon/predicate/CompareUtils.java +++ b/paimon-common/src/main/java/org/apache/paimon/predicate/CompareUtils.java @@ -38,7 +38,7 @@ public static int compareLiteral(DataType type, Object v1, Object v2) { private static int compare(byte[] first, byte[] second) { for (int x = 0; x < min(first.length, second.length); x++) { - int cmp = first[x] - second[x]; + int cmp = Byte.toUnsignedInt(first[x]) - Byte.toUnsignedInt(second[x]); if (cmp != 0) { return cmp; } diff --git a/paimon-common/src/test/java/org/apache/paimon/predicate/PredicateTest.java b/paimon-common/src/test/java/org/apache/paimon/predicate/PredicateTest.java index 0e67372d7280..a33112d34077 100644 --- a/paimon-common/src/test/java/org/apache/paimon/predicate/PredicateTest.java +++ b/paimon-common/src/test/java/org/apache/paimon/predicate/PredicateTest.java @@ -64,6 +64,26 @@ public void testEqual() { assertThat(predicate.negate().orElse(null)).isEqualTo(builder.notEqual(0, 5)); } + @Test + public void testBinaryUnsignedComparison() { + PredicateBuilder builder = new PredicateBuilder(RowType.of(DataTypes.BYTES())); + + Predicate greaterThan = builder.greaterThan(0, new byte[] {(byte) 0x7F}); + assertThat(greaterThan.test(GenericRow.of((Object) new byte[] {(byte) 0x80}))).isTrue(); + assertThat(greaterThan.test(GenericRow.of((Object) new byte[] {(byte) 0x7E}))).isFalse(); + + Predicate greaterThanLow = builder.greaterThan(0, new byte[] {(byte) 0x01}); + assertThat(greaterThanLow.test(GenericRow.of((Object) new byte[] {(byte) 0xFF}))).isTrue(); + + Predicate lessThan = builder.lessThan(0, new byte[] {(byte) 0x80}); + assertThat(lessThan.test(GenericRow.of((Object) new byte[] {(byte) 0x01}))).isTrue(); + assertThat(lessThan.test(GenericRow.of((Object) new byte[] {(byte) 0xFF}))).isFalse(); + + Predicate between = builder.between(0, new byte[] {(byte) 0x70}, new byte[] {(byte) 0x90}); + assertThat(between.test(GenericRow.of((Object) new byte[] {(byte) 0x80}))).isTrue(); + assertThat(between.test(GenericRow.of((Object) new byte[] {(byte) 0x60}))).isFalse(); + } + @Test public void testEqualNull() { PredicateBuilder builder = new PredicateBuilder(RowType.of(new IntType()));