forked from apache/arrow-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnionReader.java
More file actions
243 lines (199 loc) · 6.96 KB
/
UnionReader.java
File metadata and controls
243 lines (199 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* 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.
*/
import org.apache.arrow.vector.types.Types.MinorType;
import org.apache.arrow.vector.types.pojo.Field;
<@pp.dropOutputFile />
<@pp.changeOutputFile name="/org/apache/arrow/vector/complex/impl/UnionReader.java" />
<#include "/@includes/license.ftl" />
package org.apache.arrow.vector.complex.impl;
<#include "/@includes/vv_imports.ftl" />
<#function is_timestamp_tz type>
<#return type?starts_with("TimeStamp") && type?ends_with("TZ")>
</#function>
/**
* Source code generated using FreeMarker template ${.template_name}
*/
@SuppressWarnings("unused")
public class UnionReader extends AbstractFieldReader {
private static final int NUM_SUPPORTED_TYPES = 51;
private BaseReader[] readers = new BaseReader[NUM_SUPPORTED_TYPES];
public UnionVector data;
public UnionReader(UnionVector data) {
this.data = data;
}
public MinorType getMinorType() {
return TYPES[data.getTypeValue(idx())];
}
private static MinorType[] TYPES = new MinorType[NUM_SUPPORTED_TYPES];
static {
for (MinorType minorType : MinorType.values()) {
TYPES[minorType.ordinal()] = minorType;
}
}
@Override
public Field getField() {
return data.getField();
}
public boolean isSet(){
return !data.isNull(idx());
}
public void read(UnionHolder holder) {
holder.reader = this;
holder.isSet = this.isSet() ? 1 : 0;
}
public void read(int index, UnionHolder holder) {
getList().read(index, holder);
}
private FieldReader getReaderForIndex(int index) {
int typeValue = data.getTypeValue(index);
FieldReader reader = (FieldReader) readers[typeValue];
if (reader != null) {
return reader;
}
switch (MinorType.values()[typeValue]) {
case NULL:
return NullReader.INSTANCE;
case STRUCT:
return (FieldReader) getStruct();
case LIST:
return (FieldReader) getList();
case LISTVIEW:
return (FieldReader) getListView();
case MAP:
return (FieldReader) getMap();
<#list vv.types as type>
<#list type.minor as minor>
<#assign name = minor.class?cap_first />
<#assign uncappedName = name?uncap_first/>
<#if !minor.typeParams?? || minor.class?starts_with("Decimal") || is_timestamp_tz(minor.class) || minor.class == "Duration" || minor.class == "FixedSizeBinary">
case ${name?upper_case}:
return (FieldReader) get${name}();
</#if>
</#list>
</#list>
default:
throw new UnsupportedOperationException("Unsupported type: " + MinorType.values()[typeValue]);
}
}
private SingleStructReaderImpl structReader;
private StructReader getStruct() {
if (structReader == null) {
structReader = (SingleStructReaderImpl) data.getStruct().getReader();
structReader.setPosition(idx());
readers[MinorType.STRUCT.ordinal()] = structReader;
}
return structReader;
}
private UnionListReader listReader;
private FieldReader getList() {
if (listReader == null) {
listReader = new UnionListReader(data.getList());
listReader.setPosition(idx());
readers[MinorType.LIST.ordinal()] = listReader;
}
return listReader;
}
private UnionListViewReader listViewReader;
private FieldReader getListView() {
if (listViewReader == null) {
listViewReader = new UnionListViewReader(data.getListView());
listViewReader.setPosition(idx());
readers[MinorType.LISTVIEW.ordinal()] = listViewReader;
}
return listViewReader;
}
private UnionMapReader mapReader;
private FieldReader getMap() {
if (mapReader == null) {
mapReader = new UnionMapReader(data.getMap());
mapReader.setPosition(idx());
readers[MinorType.MAP.ordinal()] = mapReader;
}
return mapReader;
}
@Override
public java.util.Iterator<String> iterator() {
return getStruct().iterator();
}
@Override
public void copyAsValue(UnionWriter writer) {
writer.data.copyFrom(idx(), writer.idx(), data);
}
<#list ["Object", "BigDecimal", "Short", "Integer", "Long", "Boolean",
"LocalDateTime", "Duration", "Period", "Double", "Float",
"Character", "Text", "Byte", "byte[]", "PeriodDuration"] as friendlyType>
<#assign safeType=friendlyType />
<#if safeType=="byte[]"><#assign safeType="ByteArray" /></#if>
@Override
public ${friendlyType} read${safeType}() {
return getReaderForIndex(idx()).read${safeType}();
}
</#list>
public int size() {
return getReaderForIndex(idx()).size();
}
<#list vv.types as type>
<#list type.minor as minor>
<#assign name = minor.class?cap_first />
<#assign uncappedName = name?uncap_first/>
<#assign boxedType = (minor.boxedType!type.boxedType) />
<#assign javaType = (minor.javaType!type.javaType) />
<#assign friendlyType = (minor.friendlyType!minor.boxedType!type.boxedType) />
<#assign safeType=friendlyType />
<#if safeType=="byte[]"><#assign safeType="ByteArray" /></#if>
<#if !minor.typeParams?? || minor.class?starts_with("Decimal") || is_timestamp_tz(minor.class) || minor.class == "Duration" || minor.class == "FixedSizeBinary">
private ${name}ReaderImpl ${uncappedName}Reader;
private ${name}ReaderImpl get${name}() {
if (${uncappedName}Reader == null) {
${uncappedName}Reader = new ${name}ReaderImpl(data.get${name}Vector());
${uncappedName}Reader.setPosition(idx());
readers[MinorType.${name?upper_case}.ordinal()] = ${uncappedName}Reader;
}
return ${uncappedName}Reader;
}
public void read(Nullable${name}Holder holder){
getReaderForIndex(idx()).read(holder);
}
public void copyAsValue(${name}Writer writer){
getReaderForIndex(idx()).copyAsValue(writer);
}
</#if>
</#list>
</#list>
@Override
public void copyAsValue(ListWriter writer) {
ComplexCopier.copy(this, (FieldWriter) writer);
}
@Override
public void setPosition(int index) {
super.setPosition(index);
for (BaseReader reader : readers) {
if (reader != null) {
reader.setPosition(index);
}
}
}
public FieldReader reader(String name){
return getStruct().reader(name);
}
public FieldReader reader() {
return getList().reader();
}
public boolean next() {
return getReaderForIndex(idx()).next();
}
}