Skip to content

Commit 1d95c9c

Browse files
committed
fix Decimal equals
1 parent 3c9f066 commit 1d95c9c

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

java/vector/src/main/java/org/apache/arrow/vector/DecimalVector.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.math.BigDecimal;
2323

2424
import org.apache.arrow.memory.BufferAllocator;
25+
import org.apache.arrow.memory.util.ByteFunctionHelpers;
2526
import org.apache.arrow.vector.complex.impl.DecimalReaderImpl;
2627
import org.apache.arrow.vector.complex.reader.FieldReader;
2728
import org.apache.arrow.vector.holders.DecimalHolder;
@@ -489,6 +490,36 @@ public void setSafe(int index, int isSet, int start, ArrowBuf buffer) {
489490
set(index, isSet, start, buffer);
490491
}
491492

493+
@Override
494+
public boolean equals(int index, ValueVector to, int toIndex) {
495+
if (to == null) {
496+
return false;
497+
}
498+
if (this.getMinorType() != to.getMinorType()) {
499+
return false;
500+
}
501+
502+
DecimalVector that = (DecimalVector) to;
503+
504+
if (this.scale != that.scale || this.precision != that.precision) {
505+
return false;
506+
}
507+
508+
if (this.isSet(index) != that.isSet(toIndex)) {
509+
return false;
510+
}
511+
512+
int leftStart = TYPE_WIDTH * index;
513+
int leftEnd = TYPE_WIDTH * (index + 1);
514+
515+
int rightStart = TYPE_WIDTH * toIndex;
516+
int rightEnd = TYPE_WIDTH * (toIndex + 1);
517+
518+
int ret = ByteFunctionHelpers.equal(this.getDataBuffer(), leftStart, leftEnd,
519+
that.getDataBuffer(), rightStart, rightEnd);
520+
return ret == 1;
521+
}
522+
492523

493524
/*----------------------------------------------------------------*
494525
| |

java/vector/src/test/java/org/apache/arrow/vector/TestValueVector.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,6 +2282,33 @@ public void testIntVectorEquals() {
22822282
}
22832283
}
22842284

2285+
@Test
2286+
public void testDecimalVectorEquals() {
2287+
try (final DecimalVector vector1 = new DecimalVector("v1", allocator, 3, 3);
2288+
final DecimalVector vector2 = new DecimalVector("v2", allocator, 3, 3);
2289+
final DecimalVector vector3 = new DecimalVector("v3", allocator, 3, 2)) {
2290+
2291+
vector1.allocateNew(2);
2292+
vector1.setValueCount(2);
2293+
vector2.allocateNew(2);
2294+
vector2.setValueCount(2);
2295+
vector3.allocateNew(2);
2296+
vector3.setValueCount(2);
2297+
2298+
vector1.setSafe(0, 100);
2299+
vector1.setSafe(1, 200);
2300+
2301+
vector2.setSafe(0, 100);
2302+
vector2.setSafe(1, 200);
2303+
2304+
vector3.setSafe(0, 100);
2305+
vector3.setSafe(1, 200);
2306+
2307+
assertTrue(vector1.equals(vector2));
2308+
assertFalse(vector1.equals(vector3));
2309+
}
2310+
}
2311+
22852312
@Test
22862313
public void testVarcharVectorEquals() {
22872314
try (final VarCharVector vector1 = new VarCharVector("v1", allocator);

0 commit comments

Comments
 (0)