|
19 | 19 | import static org.junit.jupiter.api.Assertions.assertEquals; |
20 | 20 | import static org.junit.jupiter.api.Assertions.assertFalse; |
21 | 21 | import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertSame; |
22 | 25 | import static org.junit.jupiter.api.Assertions.assertThrows; |
23 | 26 | import static org.junit.jupiter.api.Assertions.assertTrue; |
24 | 27 |
|
| 28 | +import java.nio.file.Path; |
25 | 29 | import java.util.ArrayList; |
26 | 30 | import java.util.Arrays; |
27 | 31 | import java.util.List; |
|
30 | 34 | import org.apache.arrow.memory.RootAllocator; |
31 | 35 | import org.apache.arrow.vector.complex.ListVector; |
32 | 36 | import org.apache.arrow.vector.complex.impl.UnionListWriter; |
| 37 | +import org.apache.arrow.vector.ipc.JsonFileReader; |
| 38 | +import org.apache.arrow.vector.ipc.JsonFileWriter; |
33 | 39 | import org.apache.arrow.vector.types.pojo.ArrowType; |
34 | 40 | import org.apache.arrow.vector.types.pojo.Field; |
35 | 41 | import org.apache.arrow.vector.types.pojo.FieldType; |
36 | 42 | import org.apache.arrow.vector.types.pojo.Schema; |
37 | 43 | import org.junit.jupiter.api.AfterEach; |
38 | 44 | import org.junit.jupiter.api.BeforeEach; |
39 | 45 | import org.junit.jupiter.api.Test; |
| 46 | +import org.junit.jupiter.api.io.TempDir; |
40 | 47 |
|
41 | 48 | public class TestVectorSchemaRoot { |
42 | 49 |
|
| 50 | + @TempDir public Path tempDir; |
| 51 | + |
43 | 52 | private BufferAllocator allocator; |
44 | 53 |
|
45 | 54 | @BeforeEach |
@@ -344,4 +353,76 @@ public void testSchemaSync() { |
344 | 353 | assertFalse(schemaRoot.syncSchema()); |
345 | 354 | } |
346 | 355 | } |
| 356 | + |
| 357 | + @Test |
| 358 | + public void testSchemaSyncRefreshesFieldVectorLookup() { |
| 359 | + try (VectorSchemaRoot root = createBatch()) { |
| 360 | + final FieldVector listVector = root.getVector("listCol"); |
| 361 | + // createBatch() writes two list rows but leaves the root row count unset. |
| 362 | + root.setRowCount(listVector.getValueCount()); |
| 363 | + final int rowCount = root.getRowCount(); |
| 364 | + assertEquals(2, rowCount); |
| 365 | + final Field staleField = root.getSchema().getFields().get(0); |
| 366 | + final Object oldValue = listVector.getObject(0); |
| 367 | + |
| 368 | + assertTrue(root.syncSchema()); |
| 369 | + |
| 370 | + // Promotion produced a new Field instance for the same, unchanged vector. |
| 371 | + final Field syncedField = root.getSchema().getFields().get(0); |
| 372 | + assertNotEquals(staleField, syncedField); |
| 373 | + |
| 374 | + // Looking up by the synced Field must resolve to the very same vector instance. |
| 375 | + assertNotNull(root.getVector(syncedField)); |
| 376 | + assertSame(listVector, root.getVector(syncedField)); |
| 377 | + assertSame(listVector, root.getVector("listCol")); |
| 378 | + |
| 379 | + // The stale structural key is gone because the Field itself changed. |
| 380 | + assertNull(root.getVector(staleField)); |
| 381 | + |
| 382 | + // Syncing the schema leaves the row count and the vector data untouched. |
| 383 | + assertEquals(rowCount, root.getRowCount()); |
| 384 | + assertEquals(oldValue, listVector.getObject(0)); |
| 385 | + |
| 386 | + // A second sync is a no-op and the refreshed lookup stays correct. |
| 387 | + assertFalse(root.syncSchema()); |
| 388 | + assertSame(listVector, root.getVector(syncedField)); |
| 389 | + } |
| 390 | + } |
| 391 | + |
| 392 | + @Test |
| 393 | + public void testJsonWriteAndReadAfterSchemaSync() throws Exception { |
| 394 | + final Path jsonFile = tempDir.resolve("promoted-schema.json"); |
| 395 | + try (VectorSchemaRoot root = createBatch()) { |
| 396 | + final FieldVector expectedVector = root.getVector("listCol"); |
| 397 | + // createBatch() writes two list rows but leaves the root row count unset. |
| 398 | + root.setRowCount(expectedVector.getValueCount()); |
| 399 | + final int rowCount = root.getRowCount(); |
| 400 | + assertEquals(2, rowCount); |
| 401 | + assertTrue(root.syncSchema()); |
| 402 | + final Schema syncedSchema = root.getSchema(); |
| 403 | + |
| 404 | + // Writing resolves every vector through VectorSchemaRoot#getVector(Field), so a |
| 405 | + // stale Field-keyed lookup makes this write fail. |
| 406 | + try (JsonFileWriter writer = |
| 407 | + new JsonFileWriter(jsonFile.toFile(), JsonFileWriter.config().pretty(true))) { |
| 408 | + writer.start(syncedSchema, null); |
| 409 | + writer.write(root); |
| 410 | + } |
| 411 | + |
| 412 | + try (BufferAllocator readerAllocator = |
| 413 | + allocator.newChildAllocator("json-reader", 0, Long.MAX_VALUE); |
| 414 | + JsonFileReader reader = new JsonFileReader(jsonFile.toFile(), readerAllocator)) { |
| 415 | + assertEquals(syncedSchema, reader.start()); |
| 416 | + try (VectorSchemaRoot readRoot = reader.read()) { |
| 417 | + assertNotNull(readRoot); |
| 418 | + assertEquals(rowCount, readRoot.getRowCount()); |
| 419 | + final FieldVector actualVector = readRoot.getVector("listCol"); |
| 420 | + // The JSON schema round-trips exactly, so the nested rows compare structurally. |
| 421 | + assertEquals(expectedVector.getObject(0), actualVector.getObject(0)); |
| 422 | + assertEquals(expectedVector.getObject(1), actualVector.getObject(1)); |
| 423 | + } |
| 424 | + assertNull(reader.read()); |
| 425 | + } |
| 426 | + } |
| 427 | + } |
347 | 428 | } |
0 commit comments