Skip to content

Commit 2043e3c

Browse files
committed
Reverted internal array comparison
1 parent 39206af commit 2043e3c

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

table/src/main/java/tech/ydb/table/values/PrimitiveValue.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,9 @@ public int compareTo(Value<?> other) {
469469
case Double:
470470
return Double.compare(getDouble(), otherValue.getDouble());
471471
case Bytes:
472-
return Arrays.compare(getBytesUnsafe(), otherValue.getBytesUnsafe());
472+
return compareArrays(getBytesUnsafe(), otherValue.getBytesUnsafe());
473473
case Yson:
474-
return Arrays.compare(getYsonUnsafe(), otherValue.getYsonUnsafe());
474+
return compareArrays(getYsonUnsafe(), otherValue.getYsonUnsafe());
475475
case Text:
476476
return getText().compareTo(otherValue.getText());
477477
case Json:
@@ -517,6 +517,24 @@ private static int compareUUID(PrimitiveValue a, PrimitiveValue b) {
517517
return (al != bl) ? Long.compareUnsigned(al, bl) : Long.compareUnsigned(ah, bh);
518518
}
519519

520+
private static int compareArrays(byte[] a, byte[] b) {
521+
if (a == b) {
522+
return 0;
523+
}
524+
525+
int i = 0;
526+
int len = Math.min(a.length, b.length);
527+
while (i < len && a[i] == b[i]) {
528+
i++;
529+
}
530+
531+
if (i < len) {
532+
return Byte.compare(a[i], b[i]);
533+
}
534+
535+
return a.length - b.length;
536+
}
537+
520538
// -- implementations --
521539

522540
private static final class Bool extends PrimitiveValue {

table/src/test/java/tech/ydb/table/values/ValueComparableTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,13 @@ public void testPrimitiveValueComparison() {
8888
assertLess(PrimitiveValue.newFloat(1e-3f), PrimitiveValue.newFloat(1e-2f));
8989
assertLess(PrimitiveValue.newDouble(1e-3d), PrimitiveValue.newDouble(1e-2d));
9090

91-
assertLess(PrimitiveValue.newBytes(new byte[] { 0x01 }), PrimitiveValue.newBytes(new byte[] { 0x02 }));
92-
assertLess(PrimitiveValue.newYson(new byte[] { 0x01 }), PrimitiveValue.newYson(new byte[] { 0x02 }));
91+
byte[] b1 = new byte[] { 0x01, 0x02 };
92+
assertEquals(PrimitiveValue.newBytesOwn(b1), PrimitiveValue.newBytesOwn(b1));
93+
assertEquals(PrimitiveValue.newBytes(b1), PrimitiveValue.newBytes(new byte[] { 0x01, 0x02 }));
94+
assertLess(PrimitiveValue.newBytes(b1), PrimitiveValue.newBytes(new byte[] { 0x01, 0x02, 0x1 }));
95+
assertLess(PrimitiveValue.newBytes(b1), PrimitiveValue.newBytes(new byte[] { 0x02 }));
96+
97+
assertLess(PrimitiveValue.newYson(b1), PrimitiveValue.newYson(new byte[] { 0x01, 0x03 }));
9398

9499
assertLess(PrimitiveValue.newText("abc"), PrimitiveValue.newText("abcd"));
95100
assertLess(PrimitiveValue.newJson("['abc']"), PrimitiveValue.newJson("['abcd']"));

0 commit comments

Comments
 (0)