1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *    http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 import org.apache.arrow.memory.ArrowBuf;
19 import org.apache.arrow.vector.types.Types;
20 import org.apache.arrow.vector.types.pojo.ArrowType;
21 import org.apache.drill.common.types.TypeProtos.MinorType;
22 
23 <@pp.dropOutputFile />
24 <@pp.changeOutputFile name="/org/apache/arrow/vector/complex/impl/AbstractPromotableFieldWriter.java" />
25 
26 
27 <#include "/@includes/license.ftl" />
28 
29 package org.apache.arrow.vector.complex.impl;
30 
31 <#include "/@includes/vv_imports.ftl" />
32 
33 /*
34  * A FieldWriter which delegates calls to another FieldWriter. The delegate FieldWriter can be promoted to a new type
35  * when necessary. Classes that extend this class are responsible for handling promotion.
36  *
37  * This class is generated using freemarker and the ${.template_name} template.
38  *
39  */
40 @SuppressWarnings("unused")
41 abstract class AbstractPromotableFieldWriter extends AbstractFieldWriter {
42   /**
43    * Retrieve the FieldWriter, promoting if it is not a FieldWriter of the specified type
44    * @param type the type of the values we want to write
45    * @return the corresponding field writer
46    */
getWriter(MinorType type)47   abstract protected FieldWriter getWriter(MinorType type);
48 
49   /**
50    * @return the current FieldWriter
51    */
getWriter()52   abstract protected FieldWriter getWriter();
53 
54   @Override
start()55   public void start() {
56     getWriter(MinorType.STRUCT).start();
57   }
58 
59   @Override
end()60   public void end() {
61     getWriter(MinorType.STRUCT).end();
62     setPosition(idx() + 1);
63   }
64 
65   @Override
startList()66   public void startList() {
67     getWriter(MinorType.LIST).startList();
68   }
69 
70   @Override
endList()71   public void endList() {
72     getWriter(MinorType.LIST).endList();
73     setPosition(idx() + 1);
74   }
75 
76   <#list vv.types as type><#list type.minor as minor><#assign name = minor.class?cap_first />
77     <#assign fields = minor.fields!type.fields />
78   <#if minor.class != "Decimal">
79   @Override
80   public void write(${name}Holder holder) {
81     getWriter(MinorType.${name?upper_case}).write(holder);
82   }
83 
84   public void write${minor.class}(<#list fields as field>${field.type} ${field.name}<#if field_has_next>, </#if></#list>) {
85     getWriter(MinorType.${name?upper_case}).write${minor.class}(<#list fields as field>${field.name}<#if field_has_next>, </#if></#list>);
86   }
87 
88   <#else>
89   @Override
write(DecimalHolder holder)90   public void write(DecimalHolder holder) {
91     getWriter(MinorType.DECIMAL).write(holder);
92   }
93 
writeDecimal(int start, ArrowBuf buffer, ArrowType arrowType)94   public void writeDecimal(int start, ArrowBuf buffer, ArrowType arrowType) {
95     getWriter(MinorType.DECIMAL).writeDecimal(start, buffer, arrowType);
96   }
97 
writeDecimal(int start, ArrowBuf buffer)98   public void writeDecimal(int start, ArrowBuf buffer) {
99     getWriter(MinorType.DECIMAL).writeDecimal(start, buffer);
100   }
101 
writeBigEndianBytesToDecimal(byte[] value, ArrowType arrowType)102   public void writeBigEndianBytesToDecimal(byte[] value, ArrowType arrowType) {
103     getWriter(MinorType.DECIMAL).writeBigEndianBytesToDecimal(value, arrowType);
104   }
105 
writeBigEndianBytesToDecimal(byte[] value)106   public void writeBigEndianBytesToDecimal(byte[] value) {
107     getWriter(MinorType.DECIMAL).writeBigEndianBytesToDecimal(value);
108   }
109   </#if>
110 
111   </#list></#list>
writeNull()112   public void writeNull() {
113   }
114 
115   @Override
struct()116   public StructWriter struct() {
117     return getWriter(MinorType.LIST).struct();
118   }
119 
120   @Override
list()121   public ListWriter list() {
122     return getWriter(MinorType.LIST).list();
123   }
124 
125   @Override
struct(String name)126   public StructWriter struct(String name) {
127     return getWriter(MinorType.STRUCT).struct(name);
128   }
129 
130   @Override
list(String name)131   public ListWriter list(String name) {
132     return getWriter(MinorType.STRUCT).list(name);
133   }
134 
135   <#list vv.types as type><#list type.minor as minor>
136   <#assign lowerName = minor.class?uncap_first />
137   <#if lowerName == "int" ><#assign lowerName = "integer" /></#if>
138   <#assign upperName = minor.class?upper_case />
139   <#assign capName = minor.class?cap_first />
140 
141   <#if minor.typeParams?? >
142   @Override
143   public ${capName}Writer ${lowerName}(String name<#list minor.typeParams as typeParam>, ${typeParam.type} ${typeParam.name}</#list>) {
144     return getWriter(MinorType.STRUCT).${lowerName}(name<#list minor.typeParams as typeParam>, ${typeParam.name}</#list>);
145   }
146 
147   </#if>
148   @Override
149   public ${capName}Writer ${lowerName}(String name) {
150     return getWriter(MinorType.STRUCT).${lowerName}(name);
151   }
152 
153   @Override
154   public ${capName}Writer ${lowerName}() {
155     return getWriter(MinorType.LIST).${lowerName}();
156   }
157 
158   </#list></#list>
159 
copyReader(FieldReader reader)160   public void copyReader(FieldReader reader) {
161     getWriter().copyReader(reader);
162   }
163 
copyReaderToField(String name, FieldReader reader)164   public void copyReaderToField(String name, FieldReader reader) {
165     getWriter().copyReaderToField(name, reader);
166   }
167 }
168