From 4707673275dd98ada6430263e886d34b5233591e Mon Sep 17 00:00:00 2001 From: Steven Phillips Date: Tue, 25 Apr 2017 00:15:56 -0700 Subject: [PATCH] ARROW-895: Fix lastSet in fillEmpties() and copyFrom() --- .../templates/NullableValueVectors.java | 5 ++- .../apache/arrow/vector/TestValueVector.java | 38 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/java/vector/src/main/codegen/templates/NullableValueVectors.java b/java/vector/src/main/codegen/templates/NullableValueVectors.java index 178d5bd9139..31adc2bdd07 100644 --- a/java/vector/src/main/codegen/templates/NullableValueVectors.java +++ b/java/vector/src/main/codegen/templates/NullableValueVectors.java @@ -393,6 +393,7 @@ public void copyFrom(int fromIndex, int thisIndex, ${className} from){ if (!fromAccessor.isNull(fromIndex)) { mutator.set(thisIndex, fromAccessor.get(fromIndex)); } + <#if type.major == "VarLen">mutator.lastSet = thisIndex; } public void copyFromSafe(int fromIndex, int thisIndex, ${valuesName} from){ @@ -401,6 +402,7 @@ public void copyFromSafe(int fromIndex, int thisIndex, ${valuesName} from){ values.copyFromSafe(fromIndex, thisIndex, from); bits.getMutator().setSafeToOne(thisIndex); + <#if type.major == "VarLen">mutator.lastSet = thisIndex; } public void copyFromSafe(int fromIndex, int thisIndex, ${className} from){ @@ -409,6 +411,7 @@ public void copyFromSafe(int fromIndex, int thisIndex, ${className} from){ bits.copyFromSafe(fromIndex, thisIndex, from.bits); values.copyFromSafe(fromIndex, thisIndex, from.values); + <#if type.major == "VarLen">mutator.lastSet = thisIndex; } public final class Accessor extends BaseDataValueVector.BaseAccessor <#if type.major = "VarLen">implements VariableWidthVector.VariableWidthAccessor { @@ -532,7 +535,7 @@ private void fillEmpties(int index){ while(index > bits.getValueCapacity()) { bits.reAlloc(); } - lastSet = index; + lastSet = index - 1; } @Override diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestValueVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestValueVector.java index e6e49ab8d93..63543b09329 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestValueVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestValueVector.java @@ -21,6 +21,7 @@ import static org.apache.arrow.vector.TestUtils.newVector; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.nio.charset.Charset; @@ -473,9 +474,46 @@ public void testFillEmptiesNotOverfill() { vector.getMutator().setSafe(4094, "hello".getBytes(), 0, 5); vector.getMutator().setValueCount(4095); + assertEquals(4096 * 4, vector.getFieldBuffers().get(1).capacity()); } } + @Test + public void testCopyFromWithNulls() { + try (final NullableVarCharVector vector = newVector(NullableVarCharVector.class, EMPTY_SCHEMA_PATH, MinorType.VARCHAR, allocator); + final NullableVarCharVector vector2 = newVector(NullableVarCharVector.class, EMPTY_SCHEMA_PATH, MinorType.VARCHAR, allocator)) { + vector.allocateNew(); + + for (int i = 0; i < 4095; i++) { + if (i % 3 == 0) { + continue; + } + byte[] b = Integer.toString(i).getBytes(); + vector.getMutator().setSafe(i, b, 0, b.length); + } + + vector.getMutator().setValueCount(4095); + + vector2.allocateNew(); + + for (int i = 0; i < 4095; i++) { + vector2.copyFromSafe(i, i, vector); + } + + vector2.getMutator().setValueCount(4095); + + for (int i = 0; i < 4095; i++) { + if (i % 3 == 0) { + assertNull(vector2.getAccessor().getObject(i)); + } else { + assertEquals(Integer.toString(i), vector2.getAccessor().getObject(i).toString()); + } + + } + + + } + } }