Skip to content

Commit 5c6ebc5

Browse files
author
adeneche
committed
ARROW-320: ComplexCopier.copy(FieldReader, FieldWriter) should not start a list if reader is not set
1 parent 7fb4d24 commit 5c6ebc5

File tree

2 files changed

+95
-6
lines changed

2 files changed

+95
-6
lines changed

java/vector/src/main/codegen/templates/ComplexCopier.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,25 @@ private static void writeValue(FieldReader reader, FieldWriter writer) {
4747
switch (mt) {
4848

4949
case LIST:
50-
writer.startList();
51-
while (reader.next()) {
52-
writeValue(reader.reader(), getListWriterForReader(reader.reader(), writer));
50+
if (reader.isSet()) {
51+
writer.startList();
52+
while (reader.next()) {
53+
writeValue(reader.reader(), getListWriterForReader(reader.reader(), writer));
54+
}
55+
writer.endList();
5356
}
54-
writer.endList();
5557
break;
5658
case MAP:
57-
writer.start();
5859
if (reader.isSet()) {
60+
writer.start();
5961
for(String name : reader){
6062
FieldReader childReader = reader.reader(name);
6163
if(childReader.isSet()){
6264
writeValue(childReader, getMapWriterForReader(childReader, writer, name));
6365
}
6466
}
67+
writer.end();
6568
}
66-
writer.end();
6769
break;
6870
<#list vv.types as type><#list type.minor as minor><#assign name = minor.class?cap_first />
6971
<#assign fields = minor.fields!type.fields />
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.arrow.vector;
19+
20+
import org.apache.arrow.memory.BufferAllocator;
21+
import org.apache.arrow.vector.complex.ListVector;
22+
import org.apache.arrow.vector.complex.impl.ComplexCopier;
23+
import org.apache.arrow.vector.complex.impl.UnionListReader;
24+
import org.apache.arrow.vector.complex.impl.UnionListWriter;
25+
import org.apache.arrow.vector.complex.reader.FieldReader;
26+
import org.apache.arrow.vector.types.pojo.Field;
27+
import org.junit.After;
28+
import org.junit.Assert;
29+
import org.junit.Before;
30+
import org.junit.Test;
31+
32+
public class TestListVector {
33+
private final static String EMPTY_SCHEMA_PATH = "";
34+
35+
private BufferAllocator allocator;
36+
37+
@Before
38+
public void init() {
39+
allocator = new DirtyRootAllocator(Long.MAX_VALUE, (byte) 100);
40+
}
41+
42+
@After
43+
public void terminate() throws Exception {
44+
allocator.close();
45+
}
46+
47+
@Test
48+
public void testCopyFrom() throws Exception {
49+
try (ListVector inVector = new ListVector("input", allocator, null);
50+
ListVector outVector = new ListVector("output", allocator, null)) {
51+
UnionListWriter writer = inVector.getWriter();
52+
writer.allocate();
53+
54+
// populate input vector with the following records
55+
// [1, 2, 3]
56+
// null
57+
// []
58+
writer.setPosition(0); // optional
59+
writer.startList();
60+
writer.bigInt().writeBigInt(1);
61+
writer.bigInt().writeBigInt(2);
62+
writer.bigInt().writeBigInt(3);
63+
writer.endList();
64+
65+
writer.setPosition(2);
66+
writer.startList();
67+
writer.endList();
68+
69+
writer.setValueCount(3);
70+
71+
// copy values from input to output
72+
outVector.allocateNew();
73+
for (int i = 0; i < 3; i++) {
74+
outVector.copyFrom(i, i, inVector);
75+
}
76+
outVector.getMutator().setValueCount(3);
77+
78+
// assert the output vector is correct
79+
FieldReader reader = outVector.getReader();
80+
Assert.assertTrue("shouldn't be null", reader.isSet());
81+
reader.setPosition(1);
82+
Assert.assertFalse("should be null", reader.isSet());
83+
reader.setPosition(2);
84+
Assert.assertTrue("shouldn't be null", reader.isSet());
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)