Skip to content

Commit ea4a8ee

Browse files
committed
reverse bytes before compare
1 parent 3cc12ab commit ea4a8ee

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

java/memory/src/main/java/org/apache/arrow/memory/util/ByteFunctionHelpers.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ private static int memcmp(
140140
long leftLong = PlatformDependent.getLong(lPos);
141141
long rightLong = PlatformDependent.getLong(rPos);
142142
if (leftLong != rightLong) {
143-
return unsignedLongCompare(leftLong, rightLong);
143+
return unsignedLongCompare(Long.reverseBytes(leftLong), Long.reverseBytes(rightLong));
144144
}
145145
lPos += 8;
146146
rPos += 8;
@@ -151,7 +151,7 @@ private static int memcmp(
151151
int leftInt = PlatformDependent.getInt(lPos);
152152
int rightInt = PlatformDependent.getInt(rPos);
153153
if (leftInt != rightInt) {
154-
return unsignedIntCompare(leftInt, rightInt);
154+
return unsignedIntCompare(Integer.reverseBytes(leftInt), Integer.reverseBytes(rightInt));
155155
}
156156
lPos += 4;
157157
rPos += 4;

java/memory/src/test/java/org/apache/arrow/memory/util/TestByteFunctionHelpers.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,26 @@ public void testCompare() {
111111
buffer2.close();
112112

113113
}
114+
115+
@Test
116+
public void testStringCompare() {
117+
String[] leftStrings = {"cat", "cats", "catworld", "dogs", "bags"};
118+
String[] rightStrings = {"dog", "dogs", "dogworld", "dog", "sgab"};
119+
120+
for (int i = 0; i < leftStrings.length; ++i) {
121+
String leftStr = leftStrings[i];
122+
String rightStr = rightStrings[i];
123+
124+
ArrowBuf left = allocator.buffer(SIZE);
125+
left.setBytes(0, leftStr.getBytes());
126+
ArrowBuf right = allocator.buffer(SIZE);
127+
right.setBytes(0, rightStr.getBytes());
128+
129+
assertEquals(leftStr.compareTo(rightStr) < 0 ? -1 : 1,
130+
ByteFunctionHelpers.compare(left, 0, leftStr.length(), right, 0, rightStr.length()));
131+
132+
left.close();
133+
right.close();
134+
}
135+
}
114136
}

0 commit comments

Comments
 (0)