|
| 1 | +package io.cloudquery.types; |
| 2 | + |
| 3 | +import io.cloudquery.types.UUIDType.UUIDVector; |
| 4 | +import org.apache.arrow.memory.BufferAllocator; |
| 5 | +import org.apache.arrow.memory.RootAllocator; |
| 6 | +import org.apache.arrow.vector.FieldVector; |
| 7 | +import org.apache.arrow.vector.VectorSchemaRoot; |
| 8 | +import org.apache.arrow.vector.ipc.ArrowFileReader; |
| 9 | +import org.apache.arrow.vector.ipc.ArrowFileWriter; |
| 10 | +import org.apache.arrow.vector.types.pojo.ArrowType.ExtensionType; |
| 11 | +import org.apache.arrow.vector.types.pojo.ExtensionTypeRegistry; |
| 12 | +import org.apache.arrow.vector.types.pojo.Field; |
| 13 | +import org.apache.arrow.vector.types.pojo.Schema; |
| 14 | +import org.junit.jupiter.api.AfterAll; |
| 15 | +import org.junit.jupiter.api.BeforeAll; |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.channels.FileChannel; |
| 22 | +import java.nio.channels.WritableByteChannel; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Paths; |
| 25 | +import java.nio.file.StandardOpenOption; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.List; |
| 28 | +import java.util.UUID; |
| 29 | +import java.util.stream.IntStream; |
| 30 | + |
| 31 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 33 | + |
| 34 | +class UUIDTypeTest { |
| 35 | + private static final String FIELD_NAME = "uuid"; |
| 36 | + private static final List<UUID> UUIDS = IntStream.range(0, 10).mapToObj(i -> UUID.randomUUID()).toList(); |
| 37 | + |
| 38 | + private File file; |
| 39 | + |
| 40 | + @BeforeAll |
| 41 | + public static void setUpTest() { |
| 42 | + ExtensionTypeRegistry.register(new UUIDType()); |
| 43 | + } |
| 44 | + |
| 45 | + @AfterAll |
| 46 | + public static void tearDown() { |
| 47 | + ExtensionTypeRegistry.unregister(new UUIDType()); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + @BeforeEach |
| 52 | + void setUp() throws IOException { |
| 53 | + file = File.createTempFile("uuid_test", ".arrow"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void shouldSetUUIDsOnUUIDVector() { |
| 58 | + UUID uuid1 = UUID.randomUUID(); |
| 59 | + UUID uuid2 = UUID.randomUUID(); |
| 60 | + |
| 61 | + try (BufferAllocator allocator = new RootAllocator()) { |
| 62 | + ExtensionType uuidType = ExtensionTypeRegistry.lookup("uuid"); |
| 63 | + try (UUIDVector vector = (UUIDVector) uuidType.getNewVector("vector", null, allocator)) { |
| 64 | + vector.setValueCount(4); |
| 65 | + vector.set(0, uuid1); |
| 66 | + vector.setNull(1); |
| 67 | + vector.set(2, uuid2); |
| 68 | + vector.setNull(3); |
| 69 | + |
| 70 | + // Assert that the values were set correctly |
| 71 | + assertEquals(uuid1, vector.get(0), "UUIDs should match"); |
| 72 | + assertTrue(vector.isNull(1), "Should be null"); |
| 73 | + assertEquals(uuid2, vector.get(2), "UUIDs should match"); |
| 74 | + assertTrue(vector.isNull(3), "Should be null"); |
| 75 | + |
| 76 | + // Assert that the value count and null count are correct |
| 77 | + assertEquals(4, vector.getValueCount(), "Value count should match"); |
| 78 | + assertEquals(2, vector.getNullCount(), "Null count should match"); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void roundTripUUID() throws IOException { |
| 85 | + // Generate some data and write it to a file |
| 86 | + try (BufferAllocator allocator = new RootAllocator(); VectorSchemaRoot root = createVectorSchemaRoot(allocator)) { |
| 87 | + generateDataAndWriteToFile(root); |
| 88 | + } |
| 89 | + |
| 90 | + // Read the data back from the file and assert that it matches what we wrote |
| 91 | + try (BufferAllocator allocator = new RootAllocator(); |
| 92 | + ArrowFileReader reader = new ArrowFileReader(Files.newByteChannel(Paths.get(file.getAbsolutePath())), allocator)) { |
| 93 | + |
| 94 | + reader.loadNextBatch(); |
| 95 | + |
| 96 | + FieldVector fieldVector = reader.getVectorSchemaRoot().getVector(FIELD_NAME); |
| 97 | + assertEquals(UUIDS.size(), fieldVector.getValueCount(), "Value count should match"); |
| 98 | + for (int i = 0; i < UUIDS.size(); i++) { |
| 99 | + assertEquals(UUIDS.get(i), fieldVector.getObject(i), "UUIDs should match"); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private static VectorSchemaRoot createVectorSchemaRoot(BufferAllocator allocator) { |
| 105 | + return VectorSchemaRoot.create(new Schema(Collections.singletonList(Field.nullable(FIELD_NAME, new UUIDType()))), allocator); |
| 106 | + } |
| 107 | + |
| 108 | + private void generateDataAndWriteToFile(VectorSchemaRoot root) throws IOException { |
| 109 | + // Get the vector representing the column |
| 110 | + UUIDVector vector = (UUIDVector) root.getVector(FIELD_NAME); |
| 111 | + |
| 112 | + // Generate some UUIDs |
| 113 | + vector.setValueCount(UUIDS.size()); |
| 114 | + for (int i = 0; i < UUIDS.size(); i++) { |
| 115 | + vector.set(i, UUIDS.get(i)); |
| 116 | + } |
| 117 | + root.setRowCount(UUIDS.size()); |
| 118 | + |
| 119 | + // Write the data to a file |
| 120 | + try (WritableByteChannel channel = FileChannel.open(Paths.get(file.getAbsolutePath()), StandardOpenOption.WRITE); |
| 121 | + ArrowFileWriter writer = new ArrowFileWriter(root, null, channel)) { |
| 122 | + writer.start(); |
| 123 | + writer.writeBatch(); |
| 124 | + writer.end(); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments