From 3fdeac74c80593ebde7a8eeb148cea9f6e0d1b38 Mon Sep 17 00:00:00 2001 From: Emilio Lahr-Vivaz Date: Wed, 26 Apr 2017 15:16:53 -0400 Subject: [PATCH] ARROW-886 [Java] Fixing reallocation of VariableLengthVector offsets Author: Emilio Lahr-Vivaz Closes #591 from elahrvivaz/ARROW-886 and squashes the following commits: 5f6b4be [Emilio Lahr-Vivaz] ARROW-886 Fixing reallocation of VariableLengthVector offsets --- .../templates/VariableLengthVectors.java | 1 + .../arrow/vector/TestVectorReAlloc.java | 27 ++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/java/vector/src/main/codegen/templates/VariableLengthVectors.java b/java/vector/src/main/codegen/templates/VariableLengthVectors.java index 4a460c5475323..11f0cc894d004 100644 --- a/java/vector/src/main/codegen/templates/VariableLengthVectors.java +++ b/java/vector/src/main/codegen/templates/VariableLengthVectors.java @@ -355,6 +355,7 @@ public void reset() { } public void reAlloc() { + offsetVector.reAlloc(); final long newAllocationSize = allocationSizeInBytes*2L; if (newAllocationSize > MAX_ALLOCATION_SIZE) { throw new OversizedAllocationException("Unable to expand the buffer. Max allowed buffer size is reached."); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestVectorReAlloc.java b/java/vector/src/test/java/org/apache/arrow/vector/TestVectorReAlloc.java index a7c35b6363cf1..40c7bc5ac9add 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestVectorReAlloc.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestVectorReAlloc.java @@ -72,6 +72,31 @@ public void testFixedType() { } } + @Test + public void testVariableLengthType() { + try (final VarCharVector vector = new VarCharVector("", allocator)) { + final VarCharVector.Mutator m = vector.getMutator(); + // note: capacity ends up being - 1 due to offsets vector + vector.setInitialCapacity(511); + vector.allocateNew(); + + assertEquals(511, vector.getValueCapacity()); + + try { + m.set(512, "foo".getBytes(StandardCharsets.UTF_8)); + Assert.fail("Expected out of bounds exception"); + } catch (Exception e) { + // ok + } + + vector.reAlloc(); + assertEquals(1023, vector.getValueCapacity()); + + m.set(512, "foo".getBytes(StandardCharsets.UTF_8)); + assertEquals("foo", new String(vector.getAccessor().get(512), StandardCharsets.UTF_8)); + } + } + @Test public void testNullableType() { try (final NullableVarCharVector vector = new NullableVarCharVector("", allocator)) { @@ -89,7 +114,7 @@ public void testNullableType() { } vector.reAlloc(); - assertEquals(1023, vector.getValueCapacity()); // note: size - 1 for some reason... + assertEquals(1024, vector.getValueCapacity()); m.set(512, "foo".getBytes(StandardCharsets.UTF_8)); assertEquals("foo", new String(vector.getAccessor().get(512), StandardCharsets.UTF_8));