Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-44332: [Java] Implement VectorAppender for BaseVariableWidthViewVector #44457

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import static org.apache.arrow.memory.util.LargeMemoryUtil.checkedCastToInt;

import java.util.HashSet;
import java.util.List;
import org.apache.arrow.memory.ArrowBuf;
import org.apache.arrow.memory.util.MemoryUtil;
import org.apache.arrow.util.Preconditions;
import org.apache.arrow.vector.BaseFixedWidthVector;
Expand Down Expand Up @@ -247,8 +249,63 @@ public ValueVector visit(BaseLargeVariableWidthVector deltaVector, Void value) {
}

@Override
public ValueVector visit(BaseVariableWidthViewVector left, Void value) {
throw new UnsupportedOperationException("View vectors are not supported.");
public ValueVector visit(BaseVariableWidthViewVector 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 oldTargetValueCount = targetVector.getValueCount();
int newValueCount = oldTargetValueCount + deltaVector.getValueCount();

// make sure there is enough capacity
while (targetVector.getValueCapacity() < newValueCount) {
targetVector.reAlloc();
}

// append validity buffer
BitVectorHelper.concatBits(
targetVector.getValidityBuffer(),
oldTargetValueCount,
deltaVector.getValidityBuffer(),
deltaVector.getValueCount(),
targetVector.getValidityBuffer());

// append data buffers
BaseVariableWidthViewVector targetViewVector = (BaseVariableWidthViewVector) targetVector;
List<ArrowBuf> targetDataBuffers = targetViewVector.getDataBuffers();
final int oldTargetDataBufferCount = targetDataBuffers.size();
List<ArrowBuf> deltaVectorDataBuffers = deltaVector.getDataBuffers();
deltaVectorDataBuffers.forEach(buf -> buf.getReferenceManager().retain());
targetDataBuffers.addAll(deltaVectorDataBuffers);

// append view buffer
ArrowBuf targetViewBuffer = targetVector.getDataBuffer();
MemoryUtil.copyMemory(
deltaVector.getDataBuffer().memoryAddress(),
targetViewBuffer.memoryAddress()
+ (long) BaseVariableWidthViewVector.ELEMENT_SIZE * oldTargetValueCount,
(long) BaseVariableWidthViewVector.ELEMENT_SIZE * deltaVector.getValueCount());

// update view buffer
for (int i = oldTargetValueCount; i < newValueCount; i++) {
if (targetViewVector.isSet(i) > 0
&& targetViewVector.getValueLength(i) > BaseVariableWidthViewVector.INLINE_SIZE) {
long start =
(long) i * BaseVariableWidthViewVector.ELEMENT_SIZE
+ BaseVariableWidthViewVector.LENGTH_WIDTH
+ BaseVariableWidthViewVector.PREFIX_WIDTH;
// shift buf id
int bufferId = targetViewBuffer.getInt(start);
targetViewBuffer.setInt(start, bufferId + oldTargetDataBufferCount);
}
}

targetVector.setValueCount(newValueCount);
return targetVector;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.arrow.vector.LargeVarCharVector;
import org.apache.arrow.vector.ValueVector;
import org.apache.arrow.vector.VarCharVector;
import org.apache.arrow.vector.ViewVarCharVector;
import org.apache.arrow.vector.compare.Range;
import org.apache.arrow.vector.compare.RangeEqualsVisitor;
import org.apache.arrow.vector.compare.TypeEqualsVisitor;
Expand Down Expand Up @@ -171,6 +172,32 @@ public void testAppendVariableWidthVector() {
}
}

@Test
public void testAppendVariableWidthViewVector() {
final int length1 = 10;
final int length2 = 5;
try (ViewVarCharVector target = new ViewVarCharVector("", allocator);
ViewVarCharVector delta = new ViewVarCharVector("", allocator)) {

target.setValueCount(length1);
delta.setValueCount(length2);

for (int i = 0; i < length1; i++) {
target.setSafe(i, (i + "xxxxxxxxxxxx").getBytes());
}

for (int i = 0; i < length2; i++) {
delta.setSafe(i, (i + "xxxxxxxxxxxx").getBytes());
}

VectorAppender appender = new VectorAppender(target);
delta.accept(appender, null);

assertEquals(15, target.getValueCount());
target.accept(new RangeEqualsVisitor(target, delta), new Range(10, 0, 5));
}
}

@Test
public void testAppendEmptyVariableWidthVector() {
try (VarCharVector target = new VarCharVector("", allocator);
Expand Down
Loading