]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
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 | package org.apache.arrow.vector; | |
19 | ||
20 | import static org.apache.arrow.vector.NullCheckingForGet.NULL_CHECKING_ENABLED; | |
21 | ||
22 | import java.time.Period; | |
23 | ||
24 | import org.apache.arrow.memory.ArrowBuf; | |
25 | import org.apache.arrow.memory.BufferAllocator; | |
26 | import org.apache.arrow.vector.complex.impl.IntervalYearReaderImpl; | |
27 | import org.apache.arrow.vector.complex.reader.FieldReader; | |
28 | import org.apache.arrow.vector.holders.IntervalYearHolder; | |
29 | import org.apache.arrow.vector.holders.NullableIntervalYearHolder; | |
30 | import org.apache.arrow.vector.types.Types.MinorType; | |
31 | import org.apache.arrow.vector.types.pojo.Field; | |
32 | import org.apache.arrow.vector.types.pojo.FieldType; | |
33 | import org.apache.arrow.vector.util.TransferPair; | |
34 | ||
35 | /** | |
36 | * IntervalYearVector implements a fixed width (4 bytes) vector of | |
37 | * interval (years and months) values which could be null. A validity buffer | |
38 | * (bit vector) is maintained to track which elements in the vector are null. | |
39 | */ | |
40 | public final class IntervalYearVector extends BaseFixedWidthVector { | |
41 | public static final byte TYPE_WIDTH = 4; | |
42 | private final FieldReader reader; | |
43 | ||
44 | /** | |
45 | * Instantiate a IntervalYearVector. This doesn't allocate any memory for | |
46 | * the data in vector. | |
47 | * | |
48 | * @param name name of the vector | |
49 | * @param allocator allocator for memory management. | |
50 | */ | |
51 | public IntervalYearVector(String name, BufferAllocator allocator) { | |
52 | this(name, FieldType.nullable(MinorType.INTERVALYEAR.getType()), allocator); | |
53 | } | |
54 | ||
55 | /** | |
56 | * Instantiate a IntervalYearVector. This doesn't allocate any memory for | |
57 | * the data in vector. | |
58 | * | |
59 | * @param name name of the vector | |
60 | * @param fieldType type of Field materialized by this vector | |
61 | * @param allocator allocator for memory management. | |
62 | */ | |
63 | public IntervalYearVector(String name, FieldType fieldType, BufferAllocator allocator) { | |
64 | this(new Field(name, fieldType, null), allocator); | |
65 | } | |
66 | ||
67 | /** | |
68 | * Instantiate a IntervalYearVector. This doesn't allocate any memory for | |
69 | * the data in vector. | |
70 | * | |
71 | * @param field field materialized by this vector | |
72 | * @param allocator allocator for memory management. | |
73 | */ | |
74 | public IntervalYearVector(Field field, BufferAllocator allocator) { | |
75 | super(field, allocator, TYPE_WIDTH); | |
76 | reader = new IntervalYearReaderImpl(IntervalYearVector.this); | |
77 | } | |
78 | ||
79 | /** | |
80 | * Get a reader that supports reading values from this vector. | |
81 | * | |
82 | * @return Field Reader for this vector | |
83 | */ | |
84 | @Override | |
85 | public FieldReader getReader() { | |
86 | return reader; | |
87 | } | |
88 | ||
89 | /** | |
90 | * Get minor type for this vector. The vector holds values belonging | |
91 | * to a particular type. | |
92 | * | |
93 | * @return {@link org.apache.arrow.vector.types.Types.MinorType} | |
94 | */ | |
95 | @Override | |
96 | public MinorType getMinorType() { | |
97 | return MinorType.INTERVALYEAR; | |
98 | } | |
99 | ||
100 | ||
101 | /*----------------------------------------------------------------* | |
102 | | | | |
103 | | vector value retrieval methods | | |
104 | | | | |
105 | *----------------------------------------------------------------*/ | |
106 | ||
107 | ||
108 | /** | |
109 | * Given a data buffer, get the value stored at a particular position | |
110 | * in the vector. | |
111 | * | |
112 | * <p>This method should not be used externally. | |
113 | * | |
114 | * @param buffer data buffer | |
115 | * @param index position of the element. | |
116 | * @return value stored at the index. | |
117 | */ | |
118 | public static int getTotalMonths(final ArrowBuf buffer, final int index) { | |
119 | return buffer.getInt((long) index * TYPE_WIDTH); | |
120 | } | |
121 | ||
122 | /** | |
123 | * Get the element at the given index from the vector. | |
124 | * | |
125 | * @param index position of element | |
126 | * @return element at given index | |
127 | */ | |
128 | public int get(int index) throws IllegalStateException { | |
129 | if (NULL_CHECKING_ENABLED && isSet(index) == 0) { | |
130 | throw new IllegalStateException("Value at index is null"); | |
131 | } | |
132 | return valueBuffer.getInt((long) index * TYPE_WIDTH); | |
133 | } | |
134 | ||
135 | /** | |
136 | * Get the element at the given index from the vector and | |
137 | * sets the state in holder. If element at given index | |
138 | * is null, holder.isSet will be zero. | |
139 | * | |
140 | * @param index position of element | |
141 | */ | |
142 | public void get(int index, NullableIntervalYearHolder holder) { | |
143 | if (isSet(index) == 0) { | |
144 | holder.isSet = 0; | |
145 | return; | |
146 | } | |
147 | holder.isSet = 1; | |
148 | holder.value = valueBuffer.getInt((long) index * TYPE_WIDTH); | |
149 | } | |
150 | ||
151 | /** | |
152 | * Same as {@link #get(int)}. | |
153 | * | |
154 | * @param index position of element | |
155 | * @return element at given index | |
156 | */ | |
157 | public Period getObject(int index) { | |
158 | if (isSet(index) == 0) { | |
159 | return null; | |
160 | } else { | |
161 | final int interval = valueBuffer.getInt((long) index * TYPE_WIDTH); | |
162 | // TODO: verify interval is in months | |
163 | return Period.ofMonths(interval); | |
164 | } | |
165 | } | |
166 | ||
167 | /** | |
168 | * Get the Interval value at a given index as a {@link StringBuilder} object. | |
169 | * | |
170 | * @param index position of the element | |
171 | * @return String Builder object with Interval value as | |
172 | * [years, months] | |
173 | */ | |
174 | public StringBuilder getAsStringBuilder(int index) { | |
175 | if (isSet(index) == 0) { | |
176 | return null; | |
177 | } else { | |
178 | return getAsStringBuilderHelper(index); | |
179 | } | |
180 | } | |
181 | ||
182 | private StringBuilder getAsStringBuilderHelper(int index) { | |
183 | int value = valueBuffer.getInt((long) index * TYPE_WIDTH); | |
184 | ||
185 | final int years = (value / org.apache.arrow.vector.util.DateUtility.yearsToMonths); | |
186 | final int months = (value % org.apache.arrow.vector.util.DateUtility.yearsToMonths); | |
187 | ||
188 | final String yearString = (Math.abs(years) == 1) ? " year " : " years "; | |
189 | final String monthString = (Math.abs(months) == 1) ? " month " : " months "; | |
190 | ||
191 | return (new StringBuilder() | |
192 | .append(years) | |
193 | .append(yearString) | |
194 | .append(months) | |
195 | .append(monthString)); | |
196 | } | |
197 | ||
198 | /*----------------------------------------------------------------* | |
199 | | | | |
200 | | vector value setter methods | | |
201 | | | | |
202 | *----------------------------------------------------------------*/ | |
203 | ||
204 | ||
205 | private void setValue(int index, int value) { | |
206 | valueBuffer.setInt((long) index * TYPE_WIDTH, value); | |
207 | } | |
208 | ||
209 | /** | |
210 | * Set the element at the given index to the given value. | |
211 | * | |
212 | * @param index position of element | |
213 | * @param value value of element | |
214 | */ | |
215 | public void set(int index, int value) { | |
216 | BitVectorHelper.setBit(validityBuffer, index); | |
217 | setValue(index, value); | |
218 | } | |
219 | ||
220 | /** | |
221 | * Set the element at the given index to the value set in data holder. | |
222 | * If the value in holder is not indicated as set, element in the | |
223 | * at the given index will be null. | |
224 | * | |
225 | * @param index position of element | |
226 | * @param holder nullable data holder for value of element | |
227 | */ | |
228 | public void set(int index, NullableIntervalYearHolder holder) throws IllegalArgumentException { | |
229 | if (holder.isSet < 0) { | |
230 | throw new IllegalArgumentException(); | |
231 | } else if (holder.isSet > 0) { | |
232 | BitVectorHelper.setBit(validityBuffer, index); | |
233 | setValue(index, holder.value); | |
234 | } else { | |
235 | BitVectorHelper.unsetBit(validityBuffer, index); | |
236 | } | |
237 | } | |
238 | ||
239 | /** | |
240 | * Set the element at the given index to the value set in data holder. | |
241 | * | |
242 | * @param index position of element | |
243 | * @param holder data holder for value of element | |
244 | */ | |
245 | public void set(int index, IntervalYearHolder holder) { | |
246 | BitVectorHelper.setBit(validityBuffer, index); | |
247 | setValue(index, holder.value); | |
248 | } | |
249 | ||
250 | /** | |
251 | * Same as {@link #set(int, int)} except that it handles the | |
252 | * case when index is greater than or equal to existing | |
253 | * value capacity {@link #getValueCapacity()}. | |
254 | * | |
255 | * @param index position of element | |
256 | * @param value value of element | |
257 | */ | |
258 | public void setSafe(int index, int value) { | |
259 | handleSafe(index); | |
260 | set(index, value); | |
261 | } | |
262 | ||
263 | /** | |
264 | * Same as {@link #set(int, NullableIntervalYearHolder)} except that it handles the | |
265 | * case when index is greater than or equal to existing | |
266 | * value capacity {@link #getValueCapacity()}. | |
267 | * | |
268 | * @param index position of element | |
269 | * @param holder nullable data holder for value of element | |
270 | */ | |
271 | public void setSafe(int index, NullableIntervalYearHolder holder) throws IllegalArgumentException { | |
272 | handleSafe(index); | |
273 | set(index, holder); | |
274 | } | |
275 | ||
276 | /** | |
277 | * Same as {@link #set(int, IntervalYearHolder)} except that it handles the | |
278 | * case when index is greater than or equal to existing | |
279 | * value capacity {@link #getValueCapacity()}. | |
280 | * | |
281 | * @param index position of element | |
282 | * @param holder data holder for value of element | |
283 | */ | |
284 | public void setSafe(int index, IntervalYearHolder holder) { | |
285 | handleSafe(index); | |
286 | set(index, holder); | |
287 | } | |
288 | ||
289 | /** | |
290 | * Store the given value at a particular position in the vector. isSet indicates | |
291 | * whether the value is NULL or not. | |
292 | * | |
293 | * @param index position of the new value | |
294 | * @param isSet 0 for NULL value, 1 otherwise | |
295 | * @param value element value | |
296 | */ | |
297 | public void set(int index, int isSet, int value) { | |
298 | if (isSet > 0) { | |
299 | set(index, value); | |
300 | } else { | |
301 | BitVectorHelper.unsetBit(validityBuffer, index); | |
302 | } | |
303 | } | |
304 | ||
305 | /** | |
306 | * Same as {@link #set(int, int, int)} except that it handles the case | |
307 | * when index is greater than or equal to current value capacity of the | |
308 | * vector. | |
309 | * | |
310 | * @param index position of the new value | |
311 | * @param isSet 0 for NULL value, 1 otherwise | |
312 | * @param value element value | |
313 | */ | |
314 | public void setSafe(int index, int isSet, int value) { | |
315 | handleSafe(index); | |
316 | set(index, isSet, value); | |
317 | } | |
318 | ||
319 | ||
320 | /*----------------------------------------------------------------* | |
321 | | | | |
322 | | vector transfer | | |
323 | | | | |
324 | *----------------------------------------------------------------*/ | |
325 | ||
326 | ||
327 | /** | |
328 | * Construct a TransferPair comprising of this and a target vector of | |
329 | * the same type. | |
330 | * | |
331 | * @param ref name of the target vector | |
332 | * @param allocator allocator for the target vector | |
333 | * @return {@link TransferPair} | |
334 | */ | |
335 | @Override | |
336 | public TransferPair getTransferPair(String ref, BufferAllocator allocator) { | |
337 | return new TransferImpl(ref, allocator); | |
338 | } | |
339 | ||
340 | /** | |
341 | * Construct a TransferPair with a desired target vector of the same type. | |
342 | * | |
343 | * @param to target vector | |
344 | * @return {@link TransferPair} | |
345 | */ | |
346 | @Override | |
347 | public TransferPair makeTransferPair(ValueVector to) { | |
348 | return new TransferImpl((IntervalYearVector) to); | |
349 | } | |
350 | ||
351 | private class TransferImpl implements TransferPair { | |
352 | IntervalYearVector to; | |
353 | ||
354 | public TransferImpl(String ref, BufferAllocator allocator) { | |
355 | to = new IntervalYearVector(ref, field.getFieldType(), allocator); | |
356 | } | |
357 | ||
358 | public TransferImpl(IntervalYearVector to) { | |
359 | this.to = to; | |
360 | } | |
361 | ||
362 | @Override | |
363 | public IntervalYearVector getTo() { | |
364 | return to; | |
365 | } | |
366 | ||
367 | @Override | |
368 | public void transfer() { | |
369 | transferTo(to); | |
370 | } | |
371 | ||
372 | @Override | |
373 | public void splitAndTransfer(int startIndex, int length) { | |
374 | splitAndTransferTo(startIndex, length, to); | |
375 | } | |
376 | ||
377 | @Override | |
378 | public void copyValueSafe(int fromIndex, int toIndex) { | |
379 | to.copyFromSafe(fromIndex, toIndex, IntervalYearVector.this); | |
380 | } | |
381 | } | |
382 | } |