|
15 | 15 | import java.util.ArrayDeque; |
16 | 16 | import java.util.Deque; |
17 | 17 | import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.stream.Collectors; |
| 20 | +import java.util.stream.Stream; |
18 | 21 |
|
19 | 22 | import javax.lang.model.type.TypeMirror; |
20 | 23 |
|
@@ -75,27 +78,6 @@ public class Types { |
75 | 78 | static final ClassName DOUBLE_VECTOR_FIXED_BUILDER = ClassName.get(DATA_PACKAGE, "DoubleVector", "FixedBuilder"); |
76 | 79 | static final ClassName FLOAT_VECTOR_FIXED_BUILDER = ClassName.get(DATA_PACKAGE, "FloatVector", "FixedBuilder"); |
77 | 80 |
|
78 | | - static final ClassName BOOLEAN_ARRAY_VECTOR = ClassName.get(DATA_PACKAGE, "BooleanArrayVector"); |
79 | | - static final ClassName BYTES_REF_ARRAY_VECTOR = ClassName.get(DATA_PACKAGE, "BytesRefArrayVector"); |
80 | | - static final ClassName INT_ARRAY_VECTOR = ClassName.get(DATA_PACKAGE, "IntArrayVector"); |
81 | | - static final ClassName LONG_ARRAY_VECTOR = ClassName.get(DATA_PACKAGE, "LongArrayVector"); |
82 | | - static final ClassName DOUBLE_ARRAY_VECTOR = ClassName.get(DATA_PACKAGE, "DoubleArrayVector"); |
83 | | - static final ClassName FLOAT_ARRAY_VECTOR = ClassName.get(DATA_PACKAGE, "FloatArrayVector"); |
84 | | - |
85 | | - static final ClassName BOOLEAN_ARRAY_BLOCK = ClassName.get(DATA_PACKAGE, "BooleanArrayBlock"); |
86 | | - static final ClassName BYTES_REF_ARRAY_BLOCK = ClassName.get(DATA_PACKAGE, "BytesRefArrayBlock"); |
87 | | - static final ClassName INT_ARRAY_BLOCK = ClassName.get(DATA_PACKAGE, "IntArrayBlock"); |
88 | | - static final ClassName LONG_ARRAY_BLOCK = ClassName.get(DATA_PACKAGE, "LongArrayBlock"); |
89 | | - static final ClassName DOUBLE_ARRAY_BLOCK = ClassName.get(DATA_PACKAGE, "DoubleArrayBlock"); |
90 | | - static final ClassName FLOAT_ARRAY_BLOCK = ClassName.get(DATA_PACKAGE, "FloatArrayBlock"); |
91 | | - |
92 | | - static final ClassName BOOLEAN_CONSTANT_VECTOR = ClassName.get(DATA_PACKAGE, "ConstantBooleanVector"); |
93 | | - static final ClassName BYTES_REF_CONSTANT_VECTOR = ClassName.get(DATA_PACKAGE, "ConstantBytesRefVector"); |
94 | | - static final ClassName INT_CONSTANT_VECTOR = ClassName.get(DATA_PACKAGE, "ConstantIntVector"); |
95 | | - static final ClassName LONG_CONSTANT_VECTOR = ClassName.get(DATA_PACKAGE, "ConstantLongVector"); |
96 | | - static final ClassName DOUBLE_CONSTANT_VECTOR = ClassName.get(DATA_PACKAGE, "ConstantDoubleVector"); |
97 | | - static final ClassName FLOAT_CONSTANT_VECTOR = ClassName.get(DATA_PACKAGE, "ConstantFloatVector"); |
98 | | - |
99 | 81 | static final ClassName AGGREGATOR_FUNCTION = ClassName.get(AGGREGATION_PACKAGE, "AggregatorFunction"); |
100 | 82 | static final ClassName AGGREGATOR_FUNCTION_SUPPLIER = ClassName.get(AGGREGATION_PACKAGE, "AggregatorFunctionSupplier"); |
101 | 83 | static final ClassName GROUPING_AGGREGATOR_FUNCTION = ClassName.get(AGGREGATION_PACKAGE, "GroupingAggregatorFunction"); |
@@ -138,104 +120,57 @@ public class Types { |
138 | 120 | static final ClassName RELEASABLE = ClassName.get("org.elasticsearch.core", "Releasable"); |
139 | 121 | static final ClassName RELEASABLES = ClassName.get("org.elasticsearch.core", "Releasables"); |
140 | 122 |
|
141 | | - static TypeName fromString(String type) { |
142 | | - return switch (type) { |
143 | | - case "boolean", "BOOLEAN" -> TypeName.BOOLEAN; |
144 | | - case "int", "INT" -> TypeName.INT; |
145 | | - case "long", "LONG" -> TypeName.LONG; |
146 | | - case "float", "FLOAT" -> TypeName.FLOAT; |
147 | | - case "double", "DOUBLE" -> TypeName.DOUBLE; |
148 | | - case "org.apache.lucene.util.BytesRef", "BYTES_REF" -> BYTES_REF; |
149 | | - default -> throw new IllegalArgumentException("unknown type [" + type + "]"); |
| 123 | + private record TypeDef(TypeName type, String alias, ClassName block, ClassName vector) { |
| 124 | + |
| 125 | + public static TypeDef of(TypeName type, String alias, String block, String vector) { |
| 126 | + return new TypeDef(type, alias, ClassName.get(DATA_PACKAGE, block), ClassName.get(DATA_PACKAGE, vector)); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private static final Map<String, TypeDef> TYPES = Stream.of( |
| 131 | + TypeDef.of(TypeName.BOOLEAN, "BOOLEAN", "BooleanBlock", "BooleanVector"), |
| 132 | + TypeDef.of(TypeName.INT, "INT", "IntBlock", "IntVector"), |
| 133 | + TypeDef.of(TypeName.LONG, "LONG", "LongBlock", "LongVector"), |
| 134 | + TypeDef.of(TypeName.FLOAT, "FLOAT", "FloatBlock", "FloatVector"), |
| 135 | + TypeDef.of(TypeName.DOUBLE, "DOUBLE", "DoubleBlock", "DoubleVector"), |
| 136 | + TypeDef.of(BYTES_REF, "BYTES_REF", "BytesRefBlock", "BytesRefVector") |
| 137 | + ) |
| 138 | + .flatMap(def -> Stream.of(def.type.toString(), def.type + "[]", def.alias).map(alias -> Map.entry(alias, def))) |
| 139 | + .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 140 | + |
| 141 | + private static TypeDef findRequired(String name, String kind) { |
| 142 | + TypeDef typeDef = TYPES.get(name); |
| 143 | + if (typeDef == null) { |
| 144 | + throw new IllegalArgumentException("unknown " + kind + " type [" + name + "]"); |
| 145 | + } |
| 146 | + return typeDef; |
| 147 | + } |
| 148 | + |
| 149 | + static boolean isPrimitive(String name) { |
| 150 | + return switch (name) { |
| 151 | + case "boolean", "int", "long", "float", "double" -> true; |
| 152 | + default -> false; |
150 | 153 | }; |
151 | 154 | } |
152 | 155 |
|
| 156 | + static TypeName fromString(String type) { |
| 157 | + return findRequired(type, "plain").type; |
| 158 | + } |
| 159 | + |
153 | 160 | static ClassName blockType(TypeName elementType) { |
154 | | - if (elementType.equals(TypeName.BOOLEAN)) { |
155 | | - return BOOLEAN_BLOCK; |
156 | | - } |
157 | | - if (elementType.equals(TypeName.INT)) { |
158 | | - return INT_BLOCK; |
159 | | - } |
160 | | - if (elementType.equals(TypeName.LONG)) { |
161 | | - return LONG_BLOCK; |
162 | | - } |
163 | | - if (elementType.equals(TypeName.FLOAT)) { |
164 | | - return FLOAT_BLOCK; |
165 | | - } |
166 | | - if (elementType.equals(TypeName.DOUBLE)) { |
167 | | - return DOUBLE_BLOCK; |
168 | | - } |
169 | | - if (elementType.equals(BYTES_REF)) { |
170 | | - return BYTES_REF_BLOCK; |
171 | | - } |
172 | | - throw new IllegalArgumentException("unknown block type for [" + elementType + "]"); |
| 161 | + return blockType(elementType.toString()); |
173 | 162 | } |
174 | 163 |
|
175 | 164 | static ClassName blockType(String elementType) { |
176 | | - if (elementType.equalsIgnoreCase(TypeName.BOOLEAN.toString())) { |
177 | | - return BOOLEAN_BLOCK; |
178 | | - } |
179 | | - if (elementType.equalsIgnoreCase("BYTES_REF")) { |
180 | | - return BYTES_REF_BLOCK; |
181 | | - } |
182 | | - if (elementType.equalsIgnoreCase(TypeName.INT.toString())) { |
183 | | - return INT_BLOCK; |
184 | | - } |
185 | | - if (elementType.equalsIgnoreCase(TypeName.LONG.toString())) { |
186 | | - return LONG_BLOCK; |
187 | | - } |
188 | | - if (elementType.equalsIgnoreCase(TypeName.DOUBLE.toString())) { |
189 | | - return DOUBLE_BLOCK; |
190 | | - } |
191 | | - if (elementType.equalsIgnoreCase(TypeName.FLOAT.toString())) { |
192 | | - return FLOAT_BLOCK; |
193 | | - } |
194 | | - throw new IllegalArgumentException("unknown vector type for [" + elementType + "]"); |
| 165 | + return findRequired(elementType, "block").block; |
195 | 166 | } |
196 | 167 |
|
197 | 168 | static ClassName vectorType(TypeName elementType) { |
198 | | - if (elementType.equals(TypeName.BOOLEAN)) { |
199 | | - return BOOLEAN_VECTOR; |
200 | | - } |
201 | | - if (elementType.equals(BYTES_REF)) { |
202 | | - return BYTES_REF_VECTOR; |
203 | | - } |
204 | | - if (elementType.equals(TypeName.INT)) { |
205 | | - return INT_VECTOR; |
206 | | - } |
207 | | - if (elementType.equals(TypeName.LONG)) { |
208 | | - return LONG_VECTOR; |
209 | | - } |
210 | | - if (elementType.equals(TypeName.DOUBLE)) { |
211 | | - return DOUBLE_VECTOR; |
212 | | - } |
213 | | - if (elementType.equals(TypeName.FLOAT)) { |
214 | | - return FLOAT_VECTOR; |
215 | | - } |
216 | | - throw new IllegalArgumentException("unknown vector type for [" + elementType + "]"); |
| 169 | + return vectorType(elementType.toString()); |
217 | 170 | } |
218 | 171 |
|
219 | 172 | static ClassName vectorType(String elementType) { |
220 | | - if (elementType.equalsIgnoreCase(TypeName.BOOLEAN.toString())) { |
221 | | - return BOOLEAN_VECTOR; |
222 | | - } |
223 | | - if (elementType.equalsIgnoreCase("BYTES_REF")) { |
224 | | - return BYTES_REF_VECTOR; |
225 | | - } |
226 | | - if (elementType.equalsIgnoreCase(TypeName.INT.toString())) { |
227 | | - return INT_VECTOR; |
228 | | - } |
229 | | - if (elementType.equalsIgnoreCase(TypeName.LONG.toString())) { |
230 | | - return LONG_VECTOR; |
231 | | - } |
232 | | - if (elementType.equalsIgnoreCase(TypeName.DOUBLE.toString())) { |
233 | | - return DOUBLE_VECTOR; |
234 | | - } |
235 | | - if (elementType.equalsIgnoreCase(TypeName.FLOAT.toString())) { |
236 | | - return FLOAT_VECTOR; |
237 | | - } |
238 | | - throw new IllegalArgumentException("unknown vector type for [" + elementType + "]"); |
| 173 | + return findRequired(elementType, "vector").vector; |
239 | 174 | } |
240 | 175 |
|
241 | 176 | static ClassName builderType(TypeName resultType) { |
@@ -297,63 +232,6 @@ static ClassName vectorFixedBuilderType(TypeName elementType) { |
297 | 232 | throw new IllegalArgumentException("unknown vector fixed builder type for [" + elementType + "]"); |
298 | 233 | } |
299 | 234 |
|
300 | | - static ClassName arrayVectorType(TypeName elementType) { |
301 | | - if (elementType.equals(TypeName.BOOLEAN)) { |
302 | | - return BOOLEAN_ARRAY_VECTOR; |
303 | | - } |
304 | | - if (elementType.equals(BYTES_REF)) { |
305 | | - return BYTES_REF_ARRAY_VECTOR; |
306 | | - } |
307 | | - if (elementType.equals(TypeName.INT)) { |
308 | | - return INT_ARRAY_VECTOR; |
309 | | - } |
310 | | - if (elementType.equals(TypeName.LONG)) { |
311 | | - return LONG_ARRAY_VECTOR; |
312 | | - } |
313 | | - if (elementType.equals(TypeName.DOUBLE)) { |
314 | | - return DOUBLE_ARRAY_VECTOR; |
315 | | - } |
316 | | - throw new IllegalArgumentException("unknown vector type for [" + elementType + "]"); |
317 | | - } |
318 | | - |
319 | | - static ClassName arrayBlockType(TypeName elementType) { |
320 | | - if (elementType.equals(TypeName.BOOLEAN)) { |
321 | | - return BOOLEAN_ARRAY_BLOCK; |
322 | | - } |
323 | | - if (elementType.equals(BYTES_REF)) { |
324 | | - return BYTES_REF_ARRAY_BLOCK; |
325 | | - } |
326 | | - if (elementType.equals(TypeName.INT)) { |
327 | | - return INT_ARRAY_BLOCK; |
328 | | - } |
329 | | - if (elementType.equals(TypeName.LONG)) { |
330 | | - return LONG_ARRAY_BLOCK; |
331 | | - } |
332 | | - if (elementType.equals(TypeName.DOUBLE)) { |
333 | | - return DOUBLE_ARRAY_BLOCK; |
334 | | - } |
335 | | - throw new IllegalArgumentException("unknown vector type for [" + elementType + "]"); |
336 | | - } |
337 | | - |
338 | | - static ClassName constantVectorType(TypeName elementType) { |
339 | | - if (elementType.equals(TypeName.BOOLEAN)) { |
340 | | - return BOOLEAN_CONSTANT_VECTOR; |
341 | | - } |
342 | | - if (elementType.equals(BYTES_REF)) { |
343 | | - return BYTES_REF_CONSTANT_VECTOR; |
344 | | - } |
345 | | - if (elementType.equals(TypeName.INT)) { |
346 | | - return INT_CONSTANT_VECTOR; |
347 | | - } |
348 | | - if (elementType.equals(TypeName.LONG)) { |
349 | | - return LONG_CONSTANT_VECTOR; |
350 | | - } |
351 | | - if (elementType.equals(TypeName.DOUBLE)) { |
352 | | - return DOUBLE_CONSTANT_VECTOR; |
353 | | - } |
354 | | - throw new IllegalArgumentException("unknown vector type for [" + elementType + "]"); |
355 | | - } |
356 | | - |
357 | 235 | static TypeName elementType(TypeName t) { |
358 | 236 | if (t.equals(BOOLEAN_BLOCK) || t.equals(BOOLEAN_VECTOR) || t.equals(BOOLEAN_BLOCK_BUILDER)) { |
359 | 237 | return TypeName.BOOLEAN; |
|
0 commit comments