1616 */
1717package org .apache .arrow .vector ;
1818
19+ import static org .apache .arrow .vector .extension .UuidType .UUID_BYTE_WIDTH ;
20+
1921import java .nio .ByteBuffer ;
2022import java .util .UUID ;
23+ import org .apache .arrow .memory .ArrowBuf ;
2124import org .apache .arrow .memory .BufferAllocator ;
25+ import org .apache .arrow .memory .util .ArrowBufPointer ;
2226import org .apache .arrow .memory .util .hash .ArrowBufHasher ;
2327import org .apache .arrow .vector .complex .impl .UuidReaderImpl ;
2428import org .apache .arrow .vector .complex .reader .FieldReader ;
2529import org .apache .arrow .vector .extension .UuidType ;
30+ import org .apache .arrow .vector .holders .NullableUuidHolder ;
2631import org .apache .arrow .vector .holders .UuidHolder ;
2732import org .apache .arrow .vector .types .pojo .Field ;
2833import org .apache .arrow .vector .types .pojo .FieldType ;
34+ import org .apache .arrow .vector .util .CallBack ;
2935import org .apache .arrow .vector .util .TransferPair ;
36+ import org .apache .arrow .vector .util .UuidUtility ;
3037
38+ /**
39+ * Vector implementation for UUID values using {@link UuidType}.
40+ *
41+ * <p>Supports setting and retrieving UUIDs with efficient storage and nullable value handling.
42+ *
43+ * <p>Usage:
44+ *
45+ * <pre>{@code
46+ * UuidVector vector = new UuidVector("uuid_col", allocator);
47+ * vector.set(0, UUID.randomUUID());
48+ * UUID value = vector.getObject(0);
49+ * }</pre>
50+ *
51+ * @see {@link UuidType}
52+ * @see {@link UuidHolder}
53+ * @see {@link NullableUuidHolder}
54+ */
3155public class UuidVector extends ExtensionTypeVector <FixedSizeBinaryVector >
32- implements ValueIterableVector <UUID > {
56+ implements ValueIterableVector <UUID >, FixedWidthVector , FixedSizeExtensionType {
3357 private final Field field ;
58+ public static final int TYPE_WIDTH = UUID_BYTE_WIDTH ;
3459
3560 public UuidVector (
3661 String name , BufferAllocator allocator , FixedSizeBinaryVector underlyingVector ) {
3762 super (name , allocator , underlyingVector );
3863 this .field = new Field (name , FieldType .nullable (new UuidType ()), null );
3964 }
4065
66+ public UuidVector (
67+ String name ,
68+ FieldType fieldType ,
69+ BufferAllocator allocator ,
70+ FixedSizeBinaryVector underlyingVector ) {
71+ super (name , allocator , underlyingVector );
72+ this .field = new Field (name , fieldType , null );
73+ }
74+
4175 public UuidVector (String name , BufferAllocator allocator ) {
42- super (name , allocator , new FixedSizeBinaryVector (name , allocator , 16 ));
76+ super (name , allocator , new FixedSizeBinaryVector (name , allocator , UUID_BYTE_WIDTH ));
4377 this .field = new Field (name , FieldType .nullable (new UuidType ()), null );
4478 }
4579
80+ public UuidVector (Field field , BufferAllocator allocator ) {
81+ super (
82+ field .getName (),
83+ allocator ,
84+ new FixedSizeBinaryVector (field .getName (), allocator , UUID_BYTE_WIDTH ));
85+ this .field = field ;
86+ }
87+
4688 @ Override
4789 public UUID getObject (int index ) {
90+ if (isSet (index ) == 0 ) {
91+ return null ;
92+ }
4893 final ByteBuffer bb = ByteBuffer .wrap (getUnderlyingVector ().getObject (index ));
4994 return new UUID (bb .getLong (), bb .getLong ());
5095 }
@@ -59,11 +104,113 @@ public int hashCode(int index, ArrowBufHasher hasher) {
59104 return getUnderlyingVector ().hashCode (index , hasher );
60105 }
61106
62- public void set (int index , UUID uuid ) {
63- ByteBuffer bb = ByteBuffer .allocate (16 );
64- bb .putLong (uuid .getMostSignificantBits ());
65- bb .putLong (uuid .getLeastSignificantBits ());
66- getUnderlyingVector ().set (index , bb .array ());
107+ public int isSet (int index ) {
108+ return getUnderlyingVector ().isSet (index );
109+ }
110+
111+ public ArrowBuf get (int index ) throws IllegalStateException {
112+ if (NullCheckingForGet .NULL_CHECKING_ENABLED && this .isSet (index ) == 0 ) {
113+ throw new IllegalStateException ("Value at index is null" );
114+ } else {
115+ return getBufferSlicePostNullCheck (index );
116+ }
117+ }
118+
119+ public void get (int index , NullableUuidHolder holder ) {
120+ if (NullCheckingForGet .NULL_CHECKING_ENABLED && this .isSet (index ) == 0 ) {
121+ holder .isSet = 0 ;
122+ } else {
123+ holder .isSet = 1 ;
124+ holder .buffer = getBufferSlicePostNullCheck (index );
125+ }
126+ }
127+
128+ public void get (int index , UuidHolder holder ) {
129+ if (NullCheckingForGet .NULL_CHECKING_ENABLED && this .isSet (index ) == 0 ) {
130+ holder .isSet = 0 ;
131+ } else {
132+ holder .isSet = 1 ;
133+ holder .buffer = getBufferSlicePostNullCheck (index );
134+ }
135+ }
136+
137+ public void set (int index , UUID value ) {
138+ if (value != null ) {
139+ set (index , UuidUtility .getBytesFromUUID (value ));
140+ } else {
141+ getUnderlyingVector ().setNull (index );
142+ }
143+ }
144+
145+ public void set (int index , UuidHolder holder ) {
146+ this .set (index , holder .isSet , holder .buffer );
147+ }
148+
149+ public void set (int index , NullableUuidHolder holder ) {
150+ this .set (index , holder .isSet , holder .buffer );
151+ }
152+
153+ public void set (int index , int isSet , ArrowBuf buffer ) {
154+ getUnderlyingVector ().set (index , isSet , buffer );
155+ }
156+
157+ public void set (int index , ArrowBuf value ) {
158+ getUnderlyingVector ().set (index , value );
159+ }
160+
161+ public void set (int index , ArrowBuf source , int sourceOffset ) {
162+ // Copy bytes from source buffer to target vector data buffer
163+ ArrowBuf dataBuffer = getUnderlyingVector ().getDataBuffer ();
164+ dataBuffer .setBytes ((long ) index * UUID_BYTE_WIDTH , source , sourceOffset , UUID_BYTE_WIDTH );
165+ getUnderlyingVector ().setIndexDefined (index );
166+ }
167+
168+ public void set (int index , byte [] value ) {
169+ getUnderlyingVector ().set (index , value );
170+ }
171+
172+ public void setSafe (int index , UUID value ) {
173+ if (value != null ) {
174+ setSafe (index , UuidUtility .getBytesFromUUID (value ));
175+ } else {
176+ getUnderlyingVector ().setNull (index );
177+ }
178+ }
179+
180+ public void setSafe (int index , NullableUuidHolder holder ) {
181+ if (holder != null ) {
182+ getUnderlyingVector ().setSafe (index , holder .isSet , holder .buffer );
183+ } else {
184+ getUnderlyingVector ().setNull (index );
185+ }
186+ }
187+
188+ public void setSafe (int index , UuidHolder holder ) {
189+ if (holder != null ) {
190+ getUnderlyingVector ().setSafe (index , holder .isSet , holder .buffer );
191+ } else {
192+ getUnderlyingVector ().setNull (index );
193+ }
194+ }
195+
196+ public void setSafe (int index , byte [] value ) {
197+ getUnderlyingVector ().setIndexDefined (index );
198+ getUnderlyingVector ().setSafe (index , value );
199+ }
200+
201+ public void setSafe (int index , ArrowBuf value ) {
202+ getUnderlyingVector ().setSafe (index , value );
203+ }
204+
205+ @ Override
206+ public int getTypeWidth () {
207+ return UUID_BYTE_WIDTH ;
208+ }
209+
210+ @ Override
211+ public void copyFrom (int fromIndex , int thisIndex , ValueVector from ) {
212+ getUnderlyingVector ()
213+ .copyFromSafe (fromIndex , thisIndex , ((UuidVector ) from ).getUnderlyingVector ());
67214 }
68215
69216 @ Override
@@ -77,6 +224,26 @@ public Field getField() {
77224 return field ;
78225 }
79226
227+ @ Override
228+ public ArrowBufPointer getDataPointer (int i ) {
229+ return getUnderlyingVector ().getDataPointer (i );
230+ }
231+
232+ @ Override
233+ public ArrowBufPointer getDataPointer (int i , ArrowBufPointer arrowBufPointer ) {
234+ return getUnderlyingVector ().getDataPointer (i , arrowBufPointer );
235+ }
236+
237+ @ Override
238+ public void allocateNew (int valueCount ) {
239+ getUnderlyingVector ().allocateNew (valueCount );
240+ }
241+
242+ @ Override
243+ public void zeroVector () {
244+ getUnderlyingVector ().zeroVector ();
245+ }
246+
80247 @ Override
81248 public TransferPair makeTransferPair (ValueVector to ) {
82249 return new TransferImpl ((UuidVector ) to );
@@ -87,41 +254,67 @@ protected FieldReader getReaderImpl() {
87254 return new UuidReaderImpl (this );
88255 }
89256
90- public void setSafe ( int index , byte [] value ) {
91- getUnderlyingVector (). setIndexDefined ( index );
92- getUnderlyingVector (). setSafe ( index , value );
257+ @ Override
258+ public TransferPair getTransferPair ( Field field , BufferAllocator allocator ) {
259+ return new TransferImpl ( field , allocator );
93260 }
94261
95- public void get (int index , UuidHolder holder ) {
96- holder .value = getUnderlyingVector ().get (index );
97- holder .isSet = 1 ;
262+ @ Override
263+ public TransferPair getTransferPair (Field field , BufferAllocator allocator , CallBack callBack ) {
264+ return getTransferPair (field , allocator );
265+ }
266+
267+ @ Override
268+ public TransferPair getTransferPair (String ref , BufferAllocator allocator ) {
269+ return new TransferImpl (ref , allocator );
98270 }
99271
272+ @ Override
273+ public TransferPair getTransferPair (String ref , BufferAllocator allocator , CallBack callBack ) {
274+ return getTransferPair (ref , allocator );
275+ }
276+
277+ @ Override
278+ public TransferPair getTransferPair (BufferAllocator allocator ) {
279+ return getTransferPair (this .getField ().getName (), allocator );
280+ }
281+
282+ private ArrowBuf getBufferSlicePostNullCheck (int index ) {
283+ return getUnderlyingVector ()
284+ .getDataBuffer ()
285+ .slice ((long ) index * UUID_BYTE_WIDTH , UUID_BYTE_WIDTH );
286+ }
287+
288+ /** {@link TransferPair} for {@link UuidVector}. */
100289 public class TransferImpl implements TransferPair {
101290 UuidVector to ;
102- ValueVector targetUnderlyingVector ;
103- TransferPair tp ;
104291
105292 public TransferImpl (UuidVector to ) {
106293 this .to = to ;
107- targetUnderlyingVector = this .to .getUnderlyingVector ();
108- tp = getUnderlyingVector ().makeTransferPair (targetUnderlyingVector );
294+ }
295+
296+ public TransferImpl (Field field , BufferAllocator allocator ) {
297+ this .to = new UuidVector (field , allocator );
298+ }
299+
300+ public TransferImpl (String ref , BufferAllocator allocator ) {
301+ this .to = new UuidVector (ref , allocator );
109302 }
110303
111304 public UuidVector getTo () {
112305 return this .to ;
113306 }
114307
115308 public void transfer () {
116- tp . transfer ( );
309+ getUnderlyingVector (). transferTo ( to . getUnderlyingVector () );
117310 }
118311
119312 public void splitAndTransfer (int startIndex , int length ) {
120- tp . splitAndTransfer (startIndex , length );
313+ getUnderlyingVector (). splitAndTransferTo (startIndex , length , to . getUnderlyingVector () );
121314 }
122315
123316 public void copyValueSafe (int fromIndex , int toIndex ) {
124- tp . copyValueSafe (fromIndex , toIndex );
317+ to . copyFromSafe (fromIndex , toIndex , ( ValueVector ) UuidVector . this );
125318 }
126319 }
127320}
0 commit comments