|
23 | 23 | import static org.junit.Assert.assertTrue; |
24 | 24 |
|
25 | 25 | import java.io.IOException; |
| 26 | +import java.util.ArrayList; |
26 | 27 | import java.util.Collections; |
27 | 28 | import java.util.List; |
28 | 29 |
|
|
32 | 33 | import org.apache.arrow.vector.complex.impl.ComplexWriterImpl; |
33 | 34 | import org.apache.arrow.vector.complex.reader.FieldReader; |
34 | 35 | import org.apache.arrow.vector.complex.writer.BaseWriter.ComplexWriter; |
| 36 | +import org.apache.arrow.vector.complex.writer.BaseWriter.ListWriter; |
35 | 37 | import org.apache.arrow.vector.complex.writer.BaseWriter.MapWriter; |
36 | 38 | import org.apache.arrow.vector.complex.writer.BigIntWriter; |
37 | 39 | import org.apache.arrow.vector.complex.writer.IntWriter; |
@@ -99,6 +101,79 @@ public void testUnloadLoad() throws IOException { |
99 | 101 | } |
100 | 102 | } |
101 | 103 |
|
| 104 | + @Test |
| 105 | + public void testUnloadLoadAddPadding() throws IOException { |
| 106 | + int count = 10000; |
| 107 | + Schema schema; |
| 108 | + try ( |
| 109 | + BufferAllocator originalVectorsAllocator = allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); |
| 110 | + MapVector parent = new MapVector("parent", originalVectorsAllocator, null)) { |
| 111 | + |
| 112 | + // write some data |
| 113 | + ComplexWriter writer = new ComplexWriterImpl("root", parent); |
| 114 | + MapWriter rootWriter = writer.rootAsMap(); |
| 115 | + ListWriter list = rootWriter.list("list"); |
| 116 | + IntWriter intWriter = list.integer(); |
| 117 | + for (int i = 0; i < count; i++) { |
| 118 | + list.setPosition(i); |
| 119 | + list.startList(); |
| 120 | + for (int j = 0; j < i % 4 + 1; j++) { |
| 121 | + intWriter.writeInt(i); |
| 122 | + } |
| 123 | + list.endList(); |
| 124 | + } |
| 125 | + writer.setValueCount(count); |
| 126 | + |
| 127 | + // unload it |
| 128 | + FieldVector root = parent.getChild("root"); |
| 129 | + schema = new Schema(root.getField().getChildren()); |
| 130 | + VectorUnloader vectorUnloader = newVectorUnloader(root); |
| 131 | + try ( |
| 132 | + ArrowRecordBatch recordBatch = vectorUnloader.getRecordBatch(); |
| 133 | + BufferAllocator finalVectorsAllocator = allocator.newChildAllocator("final vectors", 0, Integer.MAX_VALUE); |
| 134 | + VectorSchemaRoot newRoot = new VectorSchemaRoot(schema, finalVectorsAllocator); |
| 135 | + ) { |
| 136 | + List<ArrowBuf> oldBuffers = recordBatch.getBuffers(); |
| 137 | + List<ArrowBuf> newBuffers = new ArrayList<>(); |
| 138 | + for (ArrowBuf oldBuffer : oldBuffers) { |
| 139 | + int l = oldBuffer.readableBytes(); |
| 140 | + if (l % 64 != 0) { |
| 141 | + // pad |
| 142 | + l = l + 64 - l % 64; |
| 143 | + } |
| 144 | + ArrowBuf newBuffer = allocator.buffer(l); |
| 145 | + for (int i = oldBuffer.readerIndex(); i < oldBuffer.writerIndex(); i++) { |
| 146 | + newBuffer.setByte(i - oldBuffer.readerIndex(), oldBuffer.getByte(i)); |
| 147 | + } |
| 148 | + newBuffer.readerIndex(0); |
| 149 | + newBuffer.writerIndex(l); |
| 150 | + newBuffers.add(newBuffer); |
| 151 | + } |
| 152 | + |
| 153 | + try (ArrowRecordBatch newBatch = new ArrowRecordBatch(recordBatch.getLength(), recordBatch.getNodes(), newBuffers);) { |
| 154 | + // load it |
| 155 | + VectorLoader vectorLoader = new VectorLoader(newRoot); |
| 156 | + |
| 157 | + vectorLoader.load(newBatch); |
| 158 | + |
| 159 | + FieldReader reader = newRoot.getVector("list").getReader(); |
| 160 | + for (int i = 0; i < count; i++) { |
| 161 | + reader.setPosition(i); |
| 162 | + List<Integer> expected = new ArrayList<>(); |
| 163 | + for (int j = 0; j < i % 4 + 1; j++) { |
| 164 | + expected.add(i); |
| 165 | + } |
| 166 | + Assert.assertEquals(expected, reader.readObject()); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + for (ArrowBuf newBuf : newBuffers) { |
| 171 | + newBuf.release(); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
102 | 177 | /** |
103 | 178 | * The validity buffer can be empty if: |
104 | 179 | * - all values are defined |
|
0 commit comments