|
19 | 19 | import dev.vortex.api.VortexWriteSummary; |
20 | 20 | import dev.vortex.api.VortexWriter; |
21 | 21 | import dev.vortex.arrow.ArrowAllocation; |
| 22 | +import dev.vortex.io.NativeWritable; |
| 23 | +import java.io.ByteArrayOutputStream; |
22 | 24 | import java.io.IOException; |
23 | 25 | import java.nio.file.Files; |
24 | 26 | import java.nio.file.Path; |
|
30 | 32 | import org.apache.arrow.c.ArrowSchema; |
31 | 33 | import org.apache.arrow.c.Data; |
32 | 34 | import org.apache.arrow.memory.BufferAllocator; |
33 | | -import org.apache.arrow.memory.RootAllocator; |
34 | 35 | import org.apache.arrow.vector.BigIntVector; |
35 | 36 | import org.apache.arrow.vector.IntVector; |
36 | 37 | import org.apache.arrow.vector.VarBinaryVector; |
|
50 | 51 | import org.junit.jupiter.api.Disabled; |
51 | 52 | import org.junit.jupiter.api.Test; |
52 | 53 | import org.junit.jupiter.api.io.TempDir; |
| 54 | +import org.junit.jupiter.params.ParameterizedTest; |
| 55 | +import org.junit.jupiter.params.provider.CsvSource; |
53 | 56 |
|
54 | 57 | public final class JNIWriterTest { |
55 | 58 | private static final String ARROW_EXTENSION_NAME = "ARROW:extension:name"; |
@@ -170,15 +173,55 @@ public void testCreateWriter() throws IOException { |
170 | 173 | source.arrowSchema(allocator)); |
171 | 174 | } |
172 | 175 |
|
173 | | - @Test |
174 | | - public void testCreateWriterReleasesSchemaExport() throws IOException { |
| 176 | + @ParameterizedTest(name = "stream={0}, nested={1}, rejected={2}") |
| 177 | + @CsvSource({ |
| 178 | + "false, false, false", |
| 179 | + "false, true, false", |
| 180 | + "true, false, false", |
| 181 | + "true, true, false", |
| 182 | + "false, false, true", |
| 183 | + "false, true, true", |
| 184 | + "true, false, true", |
| 185 | + "true, true, true" |
| 186 | + }) |
| 187 | + public void testCreateWriterReleasesSchemaExport(boolean stream, boolean nested, boolean rejected) |
| 188 | + throws IOException { |
175 | 189 | String writePath = tempDir.resolve("test_schema_release.vortex").toUri().toString(); |
176 | 190 | Session session = Session.create(); |
177 | | - try (BufferAllocator allocator = new RootAllocator()) { |
178 | | - try (VortexWriter writer = VortexWriter.builder(session, writePath, personSchema(), allocator) |
179 | | - .build()) { |
180 | | - assertEquals(0, allocator.getAllocatedMemory()); |
| 191 | + Schema schema = nested ? mapSchema() : personSchema(); |
| 192 | + ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 193 | + NativeWritable writable = new NativeWritable() { |
| 194 | + @Override |
| 195 | + public void write(byte[] buffer, int offset, int length) { |
| 196 | + output.write(buffer, offset, length); |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public void flush() {} |
| 201 | + |
| 202 | + @Override |
| 203 | + public void close() {} |
| 204 | + }; |
| 205 | + try (BufferAllocator allocator = |
| 206 | + ArrowAllocation.rootAllocator().newChildAllocator("schema-release", 0, Long.MAX_VALUE)) { |
| 207 | + VortexWriter.Builder builder = stream |
| 208 | + ? VortexWriter.builder(session, writable, schema, allocator) |
| 209 | + : VortexWriter.builder(session, writePath, schema, allocator); |
| 210 | + if (rejected) { |
| 211 | + // Native validation rejects this after the schema has been exported, for both destinations. |
| 212 | + builder.putMetadata("k".repeat(1024), new byte[] {1}); |
| 213 | + IOException failure = assertThrows(IOException.class, builder::build); |
| 214 | + assertNotNull(failure.getCause()); |
| 215 | + assertTrue(failure.getCause().getMessage().contains("metadata key")); |
| 216 | + } else { |
| 217 | + try (VortexWriter writer = builder.build()) { |
| 218 | + assertEquals(0, allocator.getAllocatedMemory()); |
| 219 | + } |
| 220 | + if (stream) { |
| 221 | + assertTrue(output.size() > 0, "stream writer should have finalized a file"); |
| 222 | + } |
181 | 223 | } |
| 224 | + assertEquals(0, allocator.getAllocatedMemory()); |
182 | 225 | } |
183 | 226 | } |
184 | 227 |
|
|
0 commit comments