Skip to content

Commit 1196691

Browse files
ARROW-326: Initialize nested writers in MapWriter based on the underlying MapVector's field
Closes #163
1 parent 2d8e820 commit 1196691

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ public class ${mode}MapWriter extends AbstractFieldWriter {
5656
}
5757
</#if>
5858
this.container = container;
59+
for (Field child : container.getField().getChildren()) {
60+
switch (Types.getMinorTypeForArrowType(child.getType())) {
61+
case MAP:
62+
map(child.getName());
63+
break;
64+
case LIST:
65+
list(child.getName());
66+
break;
67+
case UNION:
68+
UnionWriter writer = new UnionWriter(container.addOrGet(child.getName(), MinorType.UNION, UnionVector.class));
69+
fields.put(child.getName().toLowerCase(), writer);
70+
break;
71+
<#list vv.types as type><#list type.minor as minor>
72+
<#assign lowerName = minor.class?uncap_first />
73+
<#if lowerName == "int" ><#assign lowerName = "integer" /></#if>
74+
<#assign upperName = minor.class?upper_case />
75+
case ${upperName}:
76+
${lowerName}(child.getName());
77+
break;
78+
</#list></#list>
79+
}
80+
}
5981
}
6082
6183
@Override

java/vector/src/test/java/org/apache/arrow/vector/complex/impl/TestPromotableWriter.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@
2121
import static org.junit.Assert.assertFalse;
2222
import static org.junit.Assert.assertTrue;
2323

24+
import org.apache.arrow.flatbuf.Type;
2425
import org.apache.arrow.memory.BufferAllocator;
2526
import org.apache.arrow.vector.DirtyRootAllocator;
2627
import org.apache.arrow.vector.complex.AbstractMapVector;
2728
import org.apache.arrow.vector.complex.MapVector;
2829
import org.apache.arrow.vector.complex.NullableMapVector;
2930
import org.apache.arrow.vector.complex.UnionVector;
31+
import org.apache.arrow.vector.complex.writer.BaseWriter.MapWriter;
3032
import org.apache.arrow.vector.types.Types.MinorType;
33+
import org.apache.arrow.vector.types.pojo.Field;
3134
import org.junit.After;
3235
import org.junit.Before;
3336
import org.junit.Test;
@@ -50,7 +53,7 @@ public void terminate() throws Exception {
5053
@Test
5154
public void testPromoteToUnion() throws Exception {
5255

53-
try (final AbstractMapVector container = new MapVector(EMPTY_SCHEMA_PATH, allocator, null);
56+
try (final MapVector container = new MapVector(EMPTY_SCHEMA_PATH, allocator, null);
5457
final NullableMapVector v = container.addOrGet("test", MinorType.MAP, NullableMapVector.class);
5558
final PromotableWriter writer = new PromotableWriter(v, container)) {
5659

@@ -92,6 +95,22 @@ public void testPromoteToUnion() throws Exception {
9295

9396
assertFalse("4 shouldn't be null", accessor.isNull(4));
9497
assertEquals(100, accessor.getObject(4));
98+
99+
container.clear();
100+
container.allocateNew();
101+
102+
ComplexWriterImpl newWriter = new ComplexWriterImpl(EMPTY_SCHEMA_PATH, container);
103+
104+
MapWriter newMapWriter = newWriter.rootAsMap();
105+
106+
newMapWriter.start();
107+
108+
newMapWriter.setPosition(2);
109+
newMapWriter.integer("A").writeInt(10);
110+
111+
Field childField = container.getField().getChildren().get(0).getChildren().get(0);
112+
assertEquals("Child field should be union type: " + childField.getName(), Type.Union, childField.getType().getTypeType());
113+
95114
}
96115
}
97116
}

0 commit comments

Comments
 (0)