Skip to content

Commit 0dfa943

Browse files
committed
compare struct child names and add UT
1 parent c7081c2 commit 0dfa943

File tree

4 files changed

+170
-1
lines changed

4 files changed

+170
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ default boolean equals(ValueVector target) {
282282
if (this == target) {
283283
return true;
284284
}
285-
if (target == null || this.getMinorType() != target.getMinorType()) {
285+
if (target == null || !this.getField().getType().equals(target.getField().getType())) {
286286
return false;
287287
}
288288

java/vector/src/main/java/org/apache/arrow/vector/complex/NonNullableStructVector.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,14 @@ public boolean equals(int index, ValueVector to, int toIndex) {
307307
return false;
308308
}
309309
NonNullableStructVector that = (NonNullableStructVector) to;
310+
310311
List<ValueVector> leftChildrens = new ArrayList<>();
311312
List<ValueVector> rightChildrens = new ArrayList<>();
312313

314+
if (!getChildFieldNames().equals(that.getChildFieldNames())) {
315+
return false;
316+
}
317+
313318
for (String child : getChildFieldNames()) {
314319
ValueVector v = getChild(child);
315320
if (v != null) {

java/vector/src/main/java/org/apache/arrow/vector/complex/StructVector.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,51 @@ public int hashCode(int index) {
488488
}
489489
}
490490

491+
@Override
492+
public boolean equals(int index, ValueVector to, int toIndex) {
493+
if (to == null) {
494+
return false;
495+
}
496+
if (!this.getField().getType().equals(to.getField().getType())) {
497+
return false;
498+
}
499+
StructVector that = (StructVector) to;
500+
if (this.isSet(index) != that.isSet(toIndex)) {
501+
return false;
502+
}
503+
List<ValueVector> leftChildrens = new ArrayList<>();
504+
List<ValueVector> rightChildrens = new ArrayList<>();
505+
506+
if (!getChildFieldNames().equals(that.getChildFieldNames())) {
507+
return false;
508+
}
509+
510+
for (String child : getChildFieldNames()) {
511+
ValueVector v = getChild(child);
512+
if (v != null) {
513+
leftChildrens.add(v);
514+
}
515+
}
516+
517+
for (String child : that.getChildFieldNames()) {
518+
ValueVector v = that.getChild(child);
519+
if (v != null) {
520+
rightChildrens.add(v);
521+
}
522+
}
523+
524+
if (leftChildrens.size() != rightChildrens.size()) {
525+
return false;
526+
}
527+
528+
for (int i = 0; i < leftChildrens.size(); i++) {
529+
if (!leftChildrens.get(i).equals(index, rightChildrens.get(i), toIndex)) {
530+
return false;
531+
}
532+
}
533+
return true;
534+
}
535+
491536
@Override
492537
public void get(int index, ComplexHolder holder) {
493538
holder.isSet = isSet(index);

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

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,6 +2254,25 @@ public void testGetNullFromVariableWidthVector() {
22542254
}
22552255
}
22562256

2257+
@Test
2258+
public void testIntVectorEqualsWithNull() {
2259+
try (final IntVector vector1 = new IntVector("v1", allocator);
2260+
final IntVector vector2 = new IntVector("v2", allocator)) {
2261+
2262+
vector1.allocateNew(2);
2263+
vector1.setValueCount(2);
2264+
vector2.allocateNew(2);
2265+
vector2.setValueCount(2);
2266+
2267+
vector1.setSafe(0, 1);
2268+
vector1.setSafe(1, 2);
2269+
2270+
vector2.setSafe(0, 1);
2271+
2272+
assertFalse(vector1.equals(vector2));
2273+
}
2274+
}
2275+
22572276
@Test
22582277
public void testIntVectorEquals() {
22592278
try (final IntVector vector1 = new IntVector("v1", allocator);
@@ -2309,6 +2328,25 @@ public void testDecimalVectorEquals() {
23092328
}
23102329
}
23112330

2331+
@Test
2332+
public void testVarcharVectorEuqalsWithNull() {
2333+
try (final VarCharVector vector1 = new VarCharVector("v1", allocator);
2334+
final VarCharVector vector2 = new VarCharVector("v2", allocator)) {
2335+
2336+
vector1.allocateNew();
2337+
vector2.allocateNew();
2338+
2339+
// set some values
2340+
vector1.setSafe(0, STR1, 0, STR1.length);
2341+
vector1.setSafe(1, STR2, 0, STR2.length);
2342+
vector1.setValueCount(2);
2343+
2344+
vector2.setSafe(0, STR1, 0, STR1.length);
2345+
vector2.setValueCount(2);
2346+
assertFalse(vector1.equals(vector2));
2347+
}
2348+
}
2349+
23122350
@Test
23132351
public void testVarcharVectorEquals() {
23142352
try (final VarCharVector vector1 = new VarCharVector("v1", allocator);
@@ -2359,6 +2397,32 @@ public void testVarBinaryVectorEquals() {
23592397
}
23602398
}
23612399

2400+
@Test
2401+
public void testListVectorEqualsWithNull() {
2402+
try (final ListVector vector1 = ListVector.empty("v1", allocator);
2403+
final ListVector vector2 = ListVector.empty("v2", allocator);) {
2404+
2405+
UnionListWriter writer1 = vector1.getWriter();
2406+
writer1.allocate();
2407+
2408+
//set some values
2409+
writeListVector(writer1, new int[] {1, 2});
2410+
writeListVector(writer1, new int[] {3, 4});
2411+
writeListVector(writer1, new int[] {});
2412+
writer1.setValueCount(3);
2413+
2414+
UnionListWriter writer2 = vector2.getWriter();
2415+
writer2.allocate();
2416+
2417+
//set some values
2418+
writeListVector(writer2, new int[] {1, 2});
2419+
writeListVector(writer2, new int[] {3, 4});
2420+
writer2.setValueCount(3);
2421+
2422+
assertFalse(vector1.equals(vector2));
2423+
}
2424+
}
2425+
23622426
@Test
23632427
public void testListVectorEquals() {
23642428
try (final ListVector vector1 = ListVector.empty("v1", allocator);
@@ -2389,6 +2453,35 @@ public void testListVectorEquals() {
23892453
}
23902454
}
23912455

2456+
@Test
2457+
public void testStructVectorEqualsWithNull() {
2458+
2459+
try (final StructVector vector1 = StructVector.empty("v1", allocator);
2460+
final StructVector vector2 = StructVector.empty("v2", allocator);) {
2461+
vector1.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class);
2462+
vector1.addOrGet("f1", FieldType.nullable(new ArrowType.Int(64, true)), BigIntVector.class);
2463+
vector2.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class);
2464+
vector2.addOrGet("f1", FieldType.nullable(new ArrowType.Int(64, true)), BigIntVector.class);
2465+
2466+
NullableStructWriter writer1 = vector1.getWriter();
2467+
writer1.allocate();
2468+
2469+
writeStructVector(writer1, 1, 10L);
2470+
writeStructVector(writer1, 2, 20L);
2471+
writeStructVector(writer1, 3, 30L);
2472+
writer1.setValueCount(3);
2473+
2474+
NullableStructWriter writer2 = vector2.getWriter();
2475+
writer2.allocate();
2476+
2477+
writeStructVector(writer2, 1, 10L);
2478+
writeStructVector(writer2, 3, 30L);
2479+
writer2.setValueCount(3);
2480+
2481+
assertFalse(vector1.equals(vector2));
2482+
}
2483+
}
2484+
23922485
@Test
23932486
public void testStructVectorEquals() {
23942487
try (final StructVector vector1 = StructVector.empty("v1", allocator);
@@ -2421,6 +2514,32 @@ public void testStructVectorEquals() {
24212514
}
24222515
}
24232516

2517+
@Test
2518+
public void testStructVectorEqualsWithDiffChild() {
2519+
try (final StructVector vector1 = StructVector.empty("v1", allocator);
2520+
final StructVector vector2 = StructVector.empty("v2", allocator);) {
2521+
vector1.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class);
2522+
vector1.addOrGet("f1", FieldType.nullable(new ArrowType.Int(64, true)), BigIntVector.class);
2523+
vector2.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class);
2524+
vector2.addOrGet("f10", FieldType.nullable(new ArrowType.Int(64, true)), BigIntVector.class);
2525+
2526+
NullableStructWriter writer1 = vector1.getWriter();
2527+
writer1.allocate();
2528+
2529+
writeStructVector(writer1, 1, 10L);
2530+
writeStructVector(writer1, 2, 20L);
2531+
writer1.setValueCount(2);
2532+
2533+
NullableStructWriter writer2 = vector2.getWriter();
2534+
writer2.allocate();
2535+
2536+
writeStructVector(writer2, 1, 10L);
2537+
writeStructVector(writer2, 2, 20L);
2538+
writer2.setValueCount(2);
2539+
assertFalse(vector1.equals(vector2));
2540+
}
2541+
}
2542+
24242543
@Test
24252544
public void testUnionVectorEquals() {
24262545
try (final UnionVector vector1 = new UnionVector("v1", allocator, null);

0 commit comments

Comments
 (0)