From 5c6ebc52ea3fa4e074a7df2d9c6688300e27c1cc Mon Sep 17 00:00:00 2001 From: adeneche Date: Wed, 5 Oct 2016 12:38:45 -0700 Subject: [PATCH] ARROW-320: ComplexCopier.copy(FieldReader, FieldWriter) should not start a list if reader is not set --- .../main/codegen/templates/ComplexCopier.java | 14 +-- .../apache/arrow/vector/TestListVector.java | 87 +++++++++++++++++++ 2 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 java/vector/src/test/java/org/apache/arrow/vector/TestListVector.java diff --git a/java/vector/src/main/codegen/templates/ComplexCopier.java b/java/vector/src/main/codegen/templates/ComplexCopier.java index a5756a47ad785..0dffe5e30bea0 100644 --- a/java/vector/src/main/codegen/templates/ComplexCopier.java +++ b/java/vector/src/main/codegen/templates/ComplexCopier.java @@ -47,23 +47,25 @@ private static void writeValue(FieldReader reader, FieldWriter writer) { switch (mt) { case LIST: - writer.startList(); - while (reader.next()) { - writeValue(reader.reader(), getListWriterForReader(reader.reader(), writer)); + if (reader.isSet()) { + writer.startList(); + while (reader.next()) { + writeValue(reader.reader(), getListWriterForReader(reader.reader(), writer)); + } + writer.endList(); } - writer.endList(); break; case MAP: - writer.start(); if (reader.isSet()) { + writer.start(); for(String name : reader){ FieldReader childReader = reader.reader(name); if(childReader.isSet()){ writeValue(childReader, getMapWriterForReader(childReader, writer, name)); } } + writer.end(); } - writer.end(); break; <#list vv.types as type><#list type.minor as minor><#assign name = minor.class?cap_first /> <#assign fields = minor.fields!type.fields /> diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestListVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestListVector.java new file mode 100644 index 0000000000000..bb7103365557f --- /dev/null +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestListVector.java @@ -0,0 +1,87 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.arrow.vector; + +import org.apache.arrow.memory.BufferAllocator; +import org.apache.arrow.vector.complex.ListVector; +import org.apache.arrow.vector.complex.impl.ComplexCopier; +import org.apache.arrow.vector.complex.impl.UnionListReader; +import org.apache.arrow.vector.complex.impl.UnionListWriter; +import org.apache.arrow.vector.complex.reader.FieldReader; +import org.apache.arrow.vector.types.pojo.Field; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestListVector { + private final static String EMPTY_SCHEMA_PATH = ""; + + private BufferAllocator allocator; + + @Before + public void init() { + allocator = new DirtyRootAllocator(Long.MAX_VALUE, (byte) 100); + } + + @After + public void terminate() throws Exception { + allocator.close(); + } + + @Test + public void testCopyFrom() throws Exception { + try (ListVector inVector = new ListVector("input", allocator, null); + ListVector outVector = new ListVector("output", allocator, null)) { + UnionListWriter writer = inVector.getWriter(); + writer.allocate(); + + // populate input vector with the following records + // [1, 2, 3] + // null + // [] + writer.setPosition(0); // optional + writer.startList(); + writer.bigInt().writeBigInt(1); + writer.bigInt().writeBigInt(2); + writer.bigInt().writeBigInt(3); + writer.endList(); + + writer.setPosition(2); + writer.startList(); + writer.endList(); + + writer.setValueCount(3); + + // copy values from input to output + outVector.allocateNew(); + for (int i = 0; i < 3; i++) { + outVector.copyFrom(i, i, inVector); + } + outVector.getMutator().setValueCount(3); + + // assert the output vector is correct + FieldReader reader = outVector.getReader(); + Assert.assertTrue("shouldn't be null", reader.isSet()); + reader.setPosition(1); + Assert.assertFalse("should be null", reader.isSet()); + reader.setPosition(2); + Assert.assertTrue("shouldn't be null", reader.isSet()); + } + } +}