]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/java/vector/src/main/java/org/apache/arrow/vector/TimeStampNanoVector.java
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / java / vector / src / main / java / org / apache / arrow / vector / TimeStampNanoVector.java
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.LocalDateTime;
23
24 import org.apache.arrow.memory.BufferAllocator;
25 import org.apache.arrow.vector.complex.impl.TimeStampNanoReaderImpl;
26 import org.apache.arrow.vector.complex.reader.FieldReader;
27 import org.apache.arrow.vector.holders.NullableTimeStampNanoHolder;
28 import org.apache.arrow.vector.holders.TimeStampNanoHolder;
29 import org.apache.arrow.vector.types.Types.MinorType;
30 import org.apache.arrow.vector.types.pojo.Field;
31 import org.apache.arrow.vector.types.pojo.FieldType;
32 import org.apache.arrow.vector.util.DateUtility;
33 import org.apache.arrow.vector.util.TransferPair;
34
35 /**
36 * TimeStampNanoVector implements a fixed width vector (8 bytes) of
37 * timestamp (nanosecond resolution) 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 TimeStampNanoVector extends TimeStampVector {
41 private final FieldReader reader;
42
43 /**
44 * Instantiate a TimeStampNanoVector. This doesn't allocate any memory for
45 * the data in vector.
46 *
47 * @param name name of the vector
48 * @param allocator allocator for memory management.
49 */
50 public TimeStampNanoVector(String name, BufferAllocator allocator) {
51 this(name, FieldType.nullable(MinorType.TIMESTAMPNANO.getType()), allocator);
52 }
53
54 /**
55 * Instantiate a TimeStampNanoVector. This doesn't allocate any memory for
56 * the data in vector.
57 *
58 * @param name name of the vector
59 * @param fieldType type of Field materialized by this vector
60 * @param allocator allocator for memory management.
61 */
62 public TimeStampNanoVector(String name, FieldType fieldType, BufferAllocator allocator) {
63 super(name, fieldType, allocator);
64 reader = new TimeStampNanoReaderImpl(TimeStampNanoVector.this);
65 }
66
67 /**
68 * Instantiate a TimeStampNanoVector. 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 TimeStampNanoVector(Field field, BufferAllocator allocator) {
75 super(field, allocator);
76 reader = new TimeStampNanoReaderImpl(TimeStampNanoVector.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.TIMESTAMPNANO;
98 }
99
100
101 /*----------------------------------------------------------------*
102 | |
103 | vector value retrieval methods |
104 | |
105 *----------------------------------------------------------------*/
106
107
108 /**
109 * Get the element at the given index from the vector and
110 * sets the state in holder. If element at given index
111 * is null, holder.isSet will be zero.
112 *
113 * @param index position of element
114 */
115 public void get(int index, NullableTimeStampNanoHolder holder) {
116 if (NULL_CHECKING_ENABLED && isSet(index) == 0) {
117 holder.isSet = 0;
118 return;
119 }
120 holder.isSet = 1;
121 holder.value = valueBuffer.getLong((long) index * TYPE_WIDTH);
122 }
123
124 /**
125 * Same as {@link #get(int)}.
126 *
127 * @param index position of element
128 * @return element at given index
129 */
130 public LocalDateTime getObject(int index) {
131 if (isSet(index) == 0) {
132 return null;
133 } else {
134 final long nanos = valueBuffer.getLong((long) index * TYPE_WIDTH);
135 return DateUtility.getLocalDateTimeFromEpochNano(nanos);
136 }
137 }
138
139
140 /*----------------------------------------------------------------*
141 | |
142 | vector value setter methods |
143 | |
144 *----------------------------------------------------------------*/
145
146
147 /**
148 * Set the element at the given index to the value set in data holder.
149 * If the value in holder is not indicated as set, element in the
150 * at the given index will be null.
151 *
152 * @param index position of element
153 * @param holder nullable data holder for value of element
154 */
155 public void set(int index, NullableTimeStampNanoHolder holder) throws IllegalArgumentException {
156 if (holder.isSet < 0) {
157 throw new IllegalArgumentException();
158 } else if (holder.isSet > 0) {
159 BitVectorHelper.setBit(validityBuffer, index);
160 setValue(index, holder.value);
161 } else {
162 BitVectorHelper.unsetBit(validityBuffer, index);
163 }
164 }
165
166 /**
167 * Set the element at the given index to the value set in data holder.
168 *
169 * @param index position of element
170 * @param holder data holder for value of element
171 */
172 public void set(int index, TimeStampNanoHolder holder) {
173 BitVectorHelper.setBit(validityBuffer, index);
174 setValue(index, holder.value);
175 }
176
177 /**
178 * Same as {@link #set(int, NullableTimeStampNanoHolder)} except that it handles the
179 * case when index is greater than or equal to existing
180 * value capacity {@link #getValueCapacity()}.
181 *
182 * @param index position of element
183 * @param holder nullable data holder for value of element
184 */
185 public void setSafe(int index, NullableTimeStampNanoHolder holder) throws IllegalArgumentException {
186 handleSafe(index);
187 set(index, holder);
188 }
189
190 /**
191 * Same as {@link #set(int, TimeStampNanoHolder)} except that it handles the
192 * case when index is greater than or equal to existing
193 * value capacity {@link #getValueCapacity()}.
194 *
195 * @param index position of element
196 * @param holder data holder for value of element
197 */
198 public void setSafe(int index, TimeStampNanoHolder holder) {
199 handleSafe(index);
200 set(index, holder);
201 }
202
203
204 /*----------------------------------------------------------------*
205 | |
206 | vector transfer |
207 | |
208 *----------------------------------------------------------------*/
209
210
211 /**
212 * Construct a TransferPair comprising of this and a target vector of
213 * the same type.
214 *
215 * @param ref name of the target vector
216 * @param allocator allocator for the target vector
217 * @return {@link TransferPair}
218 */
219 @Override
220 public TransferPair getTransferPair(String ref, BufferAllocator allocator) {
221 TimeStampNanoVector to = new TimeStampNanoVector(ref,
222 field.getFieldType(), allocator);
223 return new TransferImpl(to);
224 }
225
226 /**
227 * Construct a TransferPair with a desired target vector of the same type.
228 *
229 * @param to target vector
230 * @return {@link TransferPair}
231 */
232 @Override
233 public TransferPair makeTransferPair(ValueVector to) {
234 return new TransferImpl((TimeStampNanoVector) to);
235 }
236 }