Skip to content

Commit a298920

Browse files
committed
wip
1 parent 4e8de67 commit a298920

File tree

12 files changed

+412
-63
lines changed

12 files changed

+412
-63
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
package org.apache.arrow.vector;
18+
19+
public interface FixedSizeExtensionType {
20+
public int getTypeWidth();
21+
}

vector/src/main/java/org/apache/arrow/vector/UuidVector.java

Lines changed: 213 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,80 @@
1616
*/
1717
package org.apache.arrow.vector;
1818

19+
import static org.apache.arrow.vector.extension.UuidType.UUID_BYTE_WIDTH;
20+
1921
import java.nio.ByteBuffer;
2022
import java.util.UUID;
23+
import org.apache.arrow.memory.ArrowBuf;
2124
import org.apache.arrow.memory.BufferAllocator;
25+
import org.apache.arrow.memory.util.ArrowBufPointer;
2226
import org.apache.arrow.memory.util.hash.ArrowBufHasher;
2327
import org.apache.arrow.vector.complex.impl.UuidReaderImpl;
2428
import org.apache.arrow.vector.complex.reader.FieldReader;
2529
import org.apache.arrow.vector.extension.UuidType;
30+
import org.apache.arrow.vector.holders.NullableUuidHolder;
2631
import org.apache.arrow.vector.holders.UuidHolder;
2732
import org.apache.arrow.vector.types.pojo.Field;
2833
import org.apache.arrow.vector.types.pojo.FieldType;
34+
import org.apache.arrow.vector.util.CallBack;
2935
import 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+
*/
3155
public 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
}

vector/src/main/java/org/apache/arrow/vector/complex/impl/ExtensionTypeWriterFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import org.apache.arrow.vector.complex.writer.FieldWriter;
2121

2222
/**
23-
* A factory interface for creating instances of {@link AbstractExtensionTypeWriter}. This factory allows
24-
* configuring writer implementations for specific {@link ExtensionTypeVector}.
23+
* A factory interface for creating instances of {@link AbstractExtensionTypeWriter}. This factory
24+
* allows configuring writer implementations for specific {@link ExtensionTypeVector}.
2525
*
2626
* @param <T> the type of writer implementation for a specific {@link ExtensionTypeVector}.
2727
*/

vector/src/main/java/org/apache/arrow/vector/complex/impl/UuidWriterImpl.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
*/
1717
package org.apache.arrow.vector.complex.impl;
1818

19-
import java.nio.ByteBuffer;
20-
import java.util.UUID;
19+
import org.apache.arrow.memory.ArrowBuf;
2120
import org.apache.arrow.vector.UuidVector;
2221
import org.apache.arrow.vector.holders.ExtensionHolder;
22+
import org.apache.arrow.vector.holders.NullableUuidHolder;
2323
import org.apache.arrow.vector.holders.UuidHolder;
2424

2525
public class UuidWriterImpl extends AbstractExtensionTypeWriter<UuidVector> {
@@ -30,18 +30,25 @@ public UuidWriterImpl(UuidVector vector) {
3030

3131
@Override
3232
public void writeExtension(Object value) {
33-
UUID uuid = (UUID) value;
34-
ByteBuffer bb = ByteBuffer.allocate(16);
35-
bb.putLong(uuid.getMostSignificantBits());
36-
bb.putLong(uuid.getLeastSignificantBits());
37-
vector.setSafe(getPosition(), bb.array());
33+
if (value instanceof byte[]) {
34+
vector.setSafe(getPosition(), (byte[]) value);
35+
} else if (value instanceof ArrowBuf) {
36+
vector.setSafe(getPosition(), (ArrowBuf) value);
37+
} else if (value instanceof java.util.UUID) {
38+
vector.setSafe(getPosition(), (java.util.UUID) value);
39+
} else {
40+
throw new IllegalArgumentException("Unsupported value type for UUID: " + value.getClass());
41+
}
3842
vector.setValueCount(getPosition() + 1);
3943
}
4044

4145
@Override
4246
public void write(ExtensionHolder holder) {
43-
UuidHolder uuidHolder = (UuidHolder) holder;
44-
vector.setSafe(getPosition(), uuidHolder.value);
47+
if (holder instanceof UuidHolder) {
48+
vector.setSafe(getPosition(), ((UuidHolder) holder).buffer);
49+
} else if (holder instanceof NullableUuidHolder) {
50+
vector.setSafe(getPosition(), ((NullableUuidHolder) holder).buffer);
51+
}
4552
vector.setValueCount(getPosition() + 1);
4653
}
4754
}

0 commit comments

Comments
 (0)