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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.arrow
.vector
;
20 import static org
.apache
.arrow
.vector
.NullCheckingForGet
.NULL_CHECKING_ENABLED
;
22 import org
.apache
.arrow
.memory
.ArrowBuf
;
23 import org
.apache
.arrow
.memory
.BufferAllocator
;
24 import org
.apache
.arrow
.vector
.complex
.impl
.BigIntReaderImpl
;
25 import org
.apache
.arrow
.vector
.complex
.reader
.FieldReader
;
26 import org
.apache
.arrow
.vector
.holders
.BigIntHolder
;
27 import org
.apache
.arrow
.vector
.holders
.NullableBigIntHolder
;
28 import org
.apache
.arrow
.vector
.types
.Types
.MinorType
;
29 import org
.apache
.arrow
.vector
.types
.pojo
.Field
;
30 import org
.apache
.arrow
.vector
.types
.pojo
.FieldType
;
31 import org
.apache
.arrow
.vector
.util
.TransferPair
;
34 * BigIntVector implements a fixed width vector (8 bytes) of
35 * integer values which could be null. A validity buffer (bit vector) is
36 * maintained to track which elements in the vector are null.
38 public final class BigIntVector
extends BaseFixedWidthVector
implements BaseIntVector
{
39 public static final byte TYPE_WIDTH
= 8;
40 private final FieldReader reader
;
43 * Instantiate a BigIntVector. This doesn't allocate any memory for
46 * @param name name of the vector
47 * @param allocator allocator for memory management.
49 public BigIntVector(String name
, BufferAllocator allocator
) {
50 this(name
, FieldType
.nullable(MinorType
.BIGINT
.getType()), allocator
);
54 * Instantiate a BigIntVector. This doesn't allocate any memory for
57 * @param name name of the vector
58 * @param fieldType type of Field materialized by this vector
59 * @param allocator allocator for memory management.
61 public BigIntVector(String name
, FieldType fieldType
, BufferAllocator allocator
) {
62 this(new Field(name
, fieldType
, null), allocator
);
66 * Instantiate a BigIntVector. This doesn't allocate any memory for
69 * @param field field materialized by this vector
70 * @param allocator allocator for memory management.
72 public BigIntVector(Field field
, BufferAllocator allocator
) {
73 super(field
, allocator
, TYPE_WIDTH
);
74 reader
= new BigIntReaderImpl(BigIntVector
.this);
78 * Get a reader that supports reading values from this vector.
80 * @return Field Reader for this vector
83 public FieldReader
getReader() {
88 * Get minor type for this vector. The vector holds values belonging
89 * to a particular type.
90 * @return {@link org.apache.arrow.vector.types.Types.MinorType}
93 public MinorType
getMinorType() {
94 return MinorType
.BIGINT
;
98 /*----------------------------------------------------------------*
100 | vector value retrieval methods |
102 *----------------------------------------------------------------*/
105 * Get the element at the given index from the vector.
107 * @param index position of element
108 * @return element at given index
110 public long get(int index
) throws IllegalStateException
{
111 if (NULL_CHECKING_ENABLED
&& isSet(index
) == 0) {
112 throw new IllegalStateException("Value at index is null");
114 return valueBuffer
.getLong((long) index
* TYPE_WIDTH
);
118 * Get the element at the given index from the vector and
119 * sets the state in holder. If element at given index
120 * is null, holder.isSet will be zero.
122 * @param index position of element
124 public void get(int index
, NullableBigIntHolder holder
) {
125 if (isSet(index
) == 0) {
130 holder
.value
= valueBuffer
.getLong((long) index
* TYPE_WIDTH
);
134 * Same as {@link #get(int)}.
136 * @param index position of element
137 * @return element at given index
139 public Long
getObject(int index
) {
140 if (isSet(index
) == 0) {
143 return valueBuffer
.getLong((long) index
* TYPE_WIDTH
);
147 /*----------------------------------------------------------------*
149 | vector value setter methods |
151 *----------------------------------------------------------------*/
154 private void setValue(int index
, long value
) {
155 valueBuffer
.setLong((long) index
* TYPE_WIDTH
, value
);
159 * Set the element at the given index to the given value.
161 * @param index position of element
162 * @param value value of element
164 public void set(int index
, long value
) {
165 BitVectorHelper
.setBit(validityBuffer
, index
);
166 setValue(index
, value
);
170 * Set the element at the given index to the value set in data holder.
171 * If the value in holder is not indicated as set, element in the
172 * at the given index will be null.
174 * @param index position of element
175 * @param holder nullable data holder for value of element
177 public void set(int index
, NullableBigIntHolder holder
) throws IllegalArgumentException
{
178 if (holder
.isSet
< 0) {
179 throw new IllegalArgumentException();
180 } else if (holder
.isSet
> 0) {
181 BitVectorHelper
.setBit(validityBuffer
, index
);
182 setValue(index
, holder
.value
);
184 BitVectorHelper
.unsetBit(validityBuffer
, index
);
189 * Set the element at the given index to the value set in data holder.
191 * @param index position of element
192 * @param holder data holder for value of element
194 public void set(int index
, BigIntHolder holder
) {
195 BitVectorHelper
.setBit(validityBuffer
, index
);
196 setValue(index
, holder
.value
);
200 * Same as {@link #set(int, long)} except that it handles the
201 * case when index is greater than or equal to existing
202 * value capacity {@link #getValueCapacity()}.
204 * @param index position of element
205 * @param value value of element
207 public void setSafe(int index
, long value
) {
213 * Same as {@link #set(int, NullableBigIntHolder)} except that it handles the
214 * case when index is greater than or equal to existing
215 * value capacity {@link #getValueCapacity()}.
217 * @param index position of element
218 * @param holder nullable data holder for value of element
220 public void setSafe(int index
, NullableBigIntHolder holder
) throws IllegalArgumentException
{
226 * Same as {@link #set(int, BigIntHolder)} except that it handles the
227 * case when index is greater than or equal to existing
228 * value capacity {@link #getValueCapacity()}.
230 * @param index position of element
231 * @param holder data holder for value of element
233 public void setSafe(int index
, BigIntHolder holder
) {
239 * Store the given value at a particular position in the vector. isSet indicates
240 * whether the value is NULL or not.
241 * @param index position of the new value
242 * @param isSet 0 for NULL value, 1 otherwise
243 * @param value element value
245 public void set(int index
, int isSet
, long value
) {
249 BitVectorHelper
.unsetBit(validityBuffer
, index
);
254 * Same as {@link #set(int, int, long)} except that it handles the case
255 * when index is greater than or equal to current value capacity of the
257 * @param index position of the new value
258 * @param isSet 0 for NULL value, 1 otherwise
259 * @param value element value
261 public void setSafe(int index
, int isSet
, long value
) {
263 set(index
, isSet
, value
);
267 * Given a data buffer, get the value stored at a particular position
270 * <p>This method should not be used externally.
272 * @param buffer data buffer
273 * @param index position of the element.
274 * @return value stored at the index.
276 public static long get(final ArrowBuf buffer
, final int index
) {
277 return buffer
.getLong((long) index
* TYPE_WIDTH
);
281 /*----------------------------------------------------------------*
285 *----------------------------------------------------------------*/
289 * Construct a TransferPair comprising of this and a target vector of
292 * @param ref name of the target vector
293 * @param allocator allocator for the target vector
294 * @return {@link TransferPair}
297 public TransferPair
getTransferPair(String ref
, BufferAllocator allocator
) {
298 return new TransferImpl(ref
, allocator
);
302 * Construct a TransferPair with a desired target vector of the same type.
304 * @param to target vector
305 * @return {@link TransferPair}
308 public TransferPair
makeTransferPair(ValueVector to
) {
309 return new TransferImpl((BigIntVector
) to
);
313 public void setWithPossibleTruncate(int index
, long value
) {
314 this.setSafe(index
, value
);
318 public void setUnsafeWithPossibleTruncate(int index
, long value
) {
319 this.set(index
, value
);
323 public long getValueAsLong(int index
) {
324 return this.get(index
);
327 private class TransferImpl
implements TransferPair
{
330 public TransferImpl(String ref
, BufferAllocator allocator
) {
331 to
= new BigIntVector(ref
, field
.getFieldType(), allocator
);
334 public TransferImpl(BigIntVector to
) {
339 public BigIntVector
getTo() {
344 public void transfer() {
349 public void splitAndTransfer(int startIndex
, int length
) {
350 splitAndTransferTo(startIndex
, length
, to
);
354 public void copyValueSafe(int fromIndex
, int toIndex
) {
355 to
.copyFromSafe(fromIndex
, toIndex
, BigIntVector
.this);