|
| 1 | +package io.cloudquery.types; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import io.cloudquery.types.JSONType.JSONVector; |
| 5 | +import lombok.AllArgsConstructor; |
| 6 | +import lombok.Data; |
| 7 | +import org.apache.arrow.memory.BufferAllocator; |
| 8 | +import org.apache.arrow.memory.RootAllocator; |
| 9 | +import org.apache.arrow.vector.VectorSchemaRoot; |
| 10 | +import org.apache.arrow.vector.ipc.ArrowFileReader; |
| 11 | +import org.apache.arrow.vector.ipc.ArrowFileWriter; |
| 12 | +import org.apache.arrow.vector.types.pojo.ArrowType; |
| 13 | +import org.apache.arrow.vector.types.pojo.ExtensionTypeRegistry; |
| 14 | +import org.apache.arrow.vector.types.pojo.Field; |
| 15 | +import org.apache.arrow.vector.types.pojo.Schema; |
| 16 | +import org.junit.jupiter.api.AfterAll; |
| 17 | +import org.junit.jupiter.api.BeforeAll; |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import java.io.ByteArrayOutputStream; |
| 22 | +import java.io.File; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.OutputStream; |
| 25 | +import java.nio.channels.FileChannel; |
| 26 | +import java.nio.channels.WritableByteChannel; |
| 27 | +import java.nio.file.Files; |
| 28 | +import java.nio.file.Paths; |
| 29 | +import java.nio.file.StandardOpenOption; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 35 | + |
| 36 | +public class JSONTypeTest { |
| 37 | + private static final String FIELD_NAME = "json"; |
| 38 | + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); |
| 39 | + |
| 40 | + @Data |
| 41 | + @AllArgsConstructor |
| 42 | + public static class Person { |
| 43 | + private String name; |
| 44 | + private Integer age; |
| 45 | + private List<String> hobbies; |
| 46 | + } |
| 47 | + |
| 48 | + private File file; |
| 49 | + private List<String> jsonData; |
| 50 | + |
| 51 | + @BeforeAll |
| 52 | + public static void setUpTest() { |
| 53 | + ExtensionTypeRegistry.register(new JSONType()); |
| 54 | + } |
| 55 | + |
| 56 | + @AfterAll |
| 57 | + public static void tearDown() { |
| 58 | + ExtensionTypeRegistry.unregister(new JSONType()); |
| 59 | + } |
| 60 | + |
| 61 | + @BeforeEach |
| 62 | + void setUp() throws IOException { |
| 63 | + file = File.createTempFile("json_test", ".arrow"); |
| 64 | + jsonData = List.of( |
| 65 | + toJSON(new Person("John", 30, List.of("hiking", "swimming"))), |
| 66 | + toJSON(new Person("Jane", 25, List.of("reading", "cooking"))) |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void shouldSetJSONOnJSONVector() throws IOException { |
| 72 | + String person1 = toJSON(new Person("John", 30, List.of("hiking", "swimming"))); |
| 73 | + String person2 = toJSON(new Person("Jane", 25, List.of("reading", "cooking"))); |
| 74 | + |
| 75 | + try (BufferAllocator allocator = new RootAllocator()) { |
| 76 | + ArrowType.ExtensionType jsonType = ExtensionTypeRegistry.lookup("json"); |
| 77 | + try (JSONVector vector = (JSONVector) jsonType.getNewVector("vector", null, allocator)) { |
| 78 | + vector.set(0, person1); |
| 79 | + vector.setNull(1); |
| 80 | + vector.set(2, person2); |
| 81 | + vector.setNull(3); |
| 82 | + vector.setValueCount(4); |
| 83 | + |
| 84 | + // Assert that the values were set correctly |
| 85 | + assertEquals(person1, vector.get(0), "JSON should match"); |
| 86 | + assertTrue(vector.isNull(1), "Should be null"); |
| 87 | + assertEquals(person2, vector.get(2), "JSON should match"); |
| 88 | + assertTrue(vector.isNull(3), "Should be null"); |
| 89 | + |
| 90 | + // Assert that the value count and null count are correct |
| 91 | + assertEquals(4, vector.getValueCount(), "Value count should match"); |
| 92 | + assertEquals(2, vector.getNullCount(), "Null count should match"); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void roundTripJSON() throws IOException { |
| 99 | + // Generate some data and write it to a file |
| 100 | + try (BufferAllocator allocator = new RootAllocator(); VectorSchemaRoot root = createVectorSchemaRoot(allocator)) { |
| 101 | + generateDataAndWriteToFile(root); |
| 102 | + } |
| 103 | + |
| 104 | + // Read the data back from the file and assert that it matches what we wrote |
| 105 | + try (BufferAllocator allocator = new RootAllocator(); |
| 106 | + ArrowFileReader reader = new ArrowFileReader(Files.newByteChannel(Paths.get(file.getAbsolutePath())), allocator)) { |
| 107 | + |
| 108 | + reader.loadNextBatch(); |
| 109 | + |
| 110 | + JSONVector fieldVector = (JSONVector) reader.getVectorSchemaRoot().getVector(FIELD_NAME); |
| 111 | + assertEquals(jsonData.size(), fieldVector.getValueCount(), "Value count should match"); |
| 112 | + for (int i = 0; i < jsonData.size(); i++) { |
| 113 | + assertEquals(jsonData.get(i), fieldVector.get(i), "JSON should match"); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private static VectorSchemaRoot createVectorSchemaRoot(BufferAllocator allocator) { |
| 119 | + return VectorSchemaRoot.create(new Schema(Collections.singletonList(Field.nullable(FIELD_NAME, new JSONType()))), allocator); |
| 120 | + } |
| 121 | + |
| 122 | + private void generateDataAndWriteToFile(VectorSchemaRoot root) throws IOException { |
| 123 | + // Get the vector representing the column |
| 124 | + JSONVector vector = (JSONVector) root.getVector(FIELD_NAME); |
| 125 | + |
| 126 | + // Generate some JSON data |
| 127 | + vector.setValueCount(jsonData.size()); |
| 128 | + for (int i = 0; i < jsonData.size(); i++) { |
| 129 | + vector.set(i, jsonData.get(i)); |
| 130 | + } |
| 131 | + root.setRowCount(jsonData.size()); |
| 132 | + |
| 133 | + // Write the data to a file |
| 134 | + try (WritableByteChannel channel = FileChannel.open(Paths.get(file.getAbsolutePath()), StandardOpenOption.WRITE); |
| 135 | + ArrowFileWriter writer = new ArrowFileWriter(root, null, channel)) { |
| 136 | + writer.start(); |
| 137 | + writer.writeBatch(); |
| 138 | + writer.end(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private static String toJSON(Object object) throws IOException { |
| 143 | + try (OutputStream outputStream = new ByteArrayOutputStream()) { |
| 144 | + OBJECT_MAPPER.writeValue(outputStream, object); |
| 145 | + return outputStream.toString(); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments