Skip to content

Commit

Permalink
[Kernel] Fix binary comparator to use the unsigned comparison (#3617)
Browse files Browse the repository at this point in the history
## Description
Fixed binary comparator. Previously, bytes were compared as signed, which was incorrect.

## How was this patch tested?
Tests added to `DefaultExpressionEvaluatorSuite.scala`
  • Loading branch information
ilicmarkodb authored Aug 28, 2024
1 parent f468733 commit 55b1d12
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class DefaultExpressionUtils {
int i = 0;
while (i < leftOp.length && i < rightOp.length) {
if (leftOp[i] != rightOp[i]) {
return Byte.compare(leftOp[i], rightOp[i]);
return Byte.toUnsignedInt(leftOp[i]) - Byte.toUnsignedInt(rightOp[i]);
}
i++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,54 @@ class DefaultExpressionEvaluatorSuite extends AnyFunSuite with ExpressionSuiteBa
ofBinary("apples".getBytes()),
ofNull(BinaryType.BINARY)
),
(
ofBinary(Array[Byte]()),
ofBinary(Array[Byte](5.toByte)),
ofBinary(Array[Byte]()),
ofNull(BinaryType.BINARY)
),
(
ofBinary(Array[Byte](0.toByte)), // 00000000
ofBinary(Array[Byte](-1.toByte)), // 11111111
ofBinary(Array[Byte](0.toByte)),
ofNull(BinaryType.BINARY)
),
(
ofBinary(Array[Byte](127.toByte)), // 01111111
ofBinary(Array[Byte](-1.toByte)), // 11111111
ofBinary(Array[Byte](127.toByte)),
ofNull(BinaryType.BINARY)
),
(
ofBinary(Array[Byte](5.toByte, 10.toByte)),
ofBinary(Array[Byte](6.toByte)),
ofBinary(Array[Byte](5.toByte, 10.toByte)),
ofNull(BinaryType.BINARY)
),
(
ofBinary(Array[Byte](5.toByte, 10.toByte)),
ofBinary(Array[Byte](5.toByte, 100.toByte)),
ofBinary(Array[Byte](5.toByte, 10.toByte)),
ofNull(BinaryType.BINARY)
),
(
ofBinary(Array[Byte](5.toByte, 10.toByte, 5.toByte)), // 00000101 00001010 00000101
ofBinary(Array[Byte](5.toByte, -3.toByte)), // 00000101 11111101
ofBinary(Array[Byte](5.toByte, 10.toByte, 5.toByte)),
ofNull(BinaryType.BINARY)
),
(
ofBinary(Array[Byte](5.toByte, -25.toByte, 5.toByte)), // 00000101 11100111 00000101
ofBinary(Array[Byte](5.toByte, -9.toByte)), // 00000101 11110111
ofBinary(Array[Byte](5.toByte, -25.toByte, 5.toByte)),
ofNull(BinaryType.BINARY)
),
(
ofBinary(Array[Byte](5.toByte, 10.toByte)),
ofBinary(Array[Byte](5.toByte, 10.toByte, 0.toByte)),
ofBinary(Array[Byte](5.toByte, 10.toByte)),
ofNull(BinaryType.BINARY)
),
(
ofDecimal(BigDecimalJ.valueOf(1.12), 7, 3),
ofDecimal(BigDecimalJ.valueOf(5233.232), 7, 3),
Expand Down

0 comments on commit 55b1d12

Please # to comment.