Skip to content

Commit

Permalink
ARROW-11788: [Java] Fix appending empty delta vectors
Browse files Browse the repository at this point in the history
This PR fixes a bug where appending an empty list vector fails with a `NullPointerException`. Instead of attempting to process the delta vector, we just return early with the unmodified target vector, since there is nothing to append to the target vector from the delta vector. In addition, it fixes cases where appending an empty delta vector would've caused the same NPE, or where it'd be an optimization to not attempt processing an empty delta vector. Unit tests are added for all supported `VectorAppender` vector types.

Closes apache#9581 from nbruno/ARROW-11788

Authored-by: Nick Bruno <[email protected]>
Signed-off-by: liyafan82 <[email protected]>
  • Loading branch information
nbruno authored and liyafan82 committed Mar 1, 2021
1 parent f7cf157 commit 137d495
Show file tree
Hide file tree
Showing 2 changed files with 298 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public ValueVector visit(BaseFixedWidthVector deltaVector, Void value) {
Preconditions.checkArgument(targetVector.getField().getType().equals(deltaVector.getField().getType()),
"The targetVector to append must have the same type as the targetVector being appended");

if (deltaVector.getValueCount() == 0) {
return targetVector; // optimization, nothing to append, return
}

int newValueCount = targetVector.getValueCount() + deltaVector.getValueCount();

// make sure there is enough capacity
Expand All @@ -90,6 +94,10 @@ public ValueVector visit(BaseVariableWidthVector deltaVector, Void value) {
Preconditions.checkArgument(targetVector.getField().getType().equals(deltaVector.getField().getType()),
"The targetVector to append must have the same type as the targetVector being appended");

if (deltaVector.getValueCount() == 0) {
return targetVector; // nothing to append, return
}

int newValueCount = targetVector.getValueCount() + deltaVector.getValueCount();

int targetDataSize = targetVector.getOffsetBuffer().getInt(
Expand Down Expand Up @@ -140,6 +148,10 @@ public ValueVector visit(BaseLargeVariableWidthVector deltaVector, Void value) {
Preconditions.checkArgument(targetVector.getField().getType().equals(deltaVector.getField().getType()),
"The targetVector to append must have the same type as the targetVector being appended");

if (deltaVector.getValueCount() == 0) {
return targetVector; // nothing to append, return
}

int newValueCount = targetVector.getValueCount() + deltaVector.getValueCount();

long targetDataSize = targetVector.getOffsetBuffer().getLong(
Expand Down Expand Up @@ -190,6 +202,10 @@ public ValueVector visit(ListVector deltaVector, Void value) {
Preconditions.checkArgument(typeVisitor.equals(deltaVector),
"The targetVector to append must have the same type as the targetVector being appended");

if (deltaVector.getValueCount() == 0) {
return targetVector; // nothing to append, return
}

int newValueCount = targetVector.getValueCount() + deltaVector.getValueCount();

int targetListSize = targetVector.getOffsetBuffer().getInt(
Expand Down Expand Up @@ -241,6 +257,10 @@ public ValueVector visit(LargeListVector deltaVector, Void value) {
Preconditions.checkArgument(typeVisitor.equals(deltaVector),
"The targetVector to append must have the same type as the targetVector being appended");

if (deltaVector.getValueCount() == 0) {
return targetVector; // nothing to append, return
}

int newValueCount = targetVector.getValueCount() + deltaVector.getValueCount();

long targetListSize = targetVector.getOffsetBuffer().getLong(
Expand Down Expand Up @@ -293,6 +313,10 @@ public ValueVector visit(FixedSizeListVector deltaVector, Void value) {
Preconditions.checkArgument(typeVisitor.equals(deltaVector),
"The vector to append must have the same type as the targetVector being appended");

if (deltaVector.getValueCount() == 0) {
return targetVector; // optimization, nothing to append, return
}

FixedSizeListVector targetListVector = (FixedSizeListVector) targetVector;

Preconditions.checkArgument(targetListVector.getListSize() == deltaVector.getListSize(),
Expand Down Expand Up @@ -330,6 +354,10 @@ public ValueVector visit(NonNullableStructVector deltaVector, Void value) {
Preconditions.checkArgument(typeVisitor.equals(deltaVector),
"The vector to append must have the same type as the targetVector being appended");

if (deltaVector.getValueCount() == 0) {
return targetVector; // optimization, nothing to append, return
}

NonNullableStructVector targetStructVector = (NonNullableStructVector) targetVector;
int newValueCount = targetVector.getValueCount() + deltaVector.getValueCount();

Expand Down Expand Up @@ -365,6 +393,10 @@ public ValueVector visit(UnionVector deltaVector, Void value) {
Preconditions.checkArgument(targetVector.getMinorType() == deltaVector.getMinorType(),
"The vector to append must have the same type as the targetVector being appended");

if (deltaVector.getValueCount() == 0) {
return targetVector; // optimization, nothing to append, return
}

UnionVector targetUnionVector = (UnionVector) targetVector;
int newValueCount = targetVector.getValueCount() + deltaVector.getValueCount();

Expand Down Expand Up @@ -424,6 +456,10 @@ public ValueVector visit(DenseUnionVector deltaVector, Void value) {
Preconditions.checkArgument(targetVector.getMinorType() == deltaVector.getMinorType(),
"The vector to append must have the same type as the targetVector being appended");

if (deltaVector.getValueCount() == 0) {
return targetVector; // optimization, nothing to append, return
}

DenseUnionVector targetDenseUnionVector = (DenseUnionVector) targetVector;
int newValueCount = targetVector.getValueCount() + deltaVector.getValueCount();

Expand Down
Loading

0 comments on commit 137d495

Please sign in to comment.