|
37 | 37 | import org.apache.arrow.vector.complex.writer.BaseWriter.StructWriter; |
38 | 38 | import org.apache.arrow.vector.complex.writer.BigIntWriter; |
39 | 39 | import org.apache.arrow.vector.complex.writer.IntWriter; |
| 40 | +import org.apache.arrow.vector.compression.CompressionCodec; |
| 41 | +import org.apache.arrow.vector.compression.CompressionUtil; |
| 42 | +import org.apache.arrow.vector.ipc.message.ArrowBodyCompression; |
40 | 43 | import org.apache.arrow.vector.ipc.message.ArrowFieldNode; |
41 | 44 | import org.apache.arrow.vector.ipc.message.ArrowRecordBatch; |
42 | 45 | import org.apache.arrow.vector.types.pojo.ArrowType; |
@@ -348,4 +351,64 @@ public static VectorUnloader newVectorUnloader(FieldVector root) { |
348 | 351 | VectorSchemaRoot vsr = new VectorSchemaRoot(schema.getFields(), fields, valueCount); |
349 | 352 | return new VectorUnloader(vsr); |
350 | 353 | } |
| 354 | + |
| 355 | + @Test |
| 356 | + public void testLoadReleasesBuffersOnDecompressionFailure() { |
| 357 | + Schema schema = new Schema(asList(Field.nullable("int", new ArrowType.Int(32, true)))); |
| 358 | + CompressionCodec.Factory failingFactory = |
| 359 | + new CompressionCodec.Factory() { |
| 360 | + @Override |
| 361 | + public CompressionCodec createCodec(CompressionUtil.CodecType codecType) { |
| 362 | + return new CompressionCodec() { |
| 363 | + @Override |
| 364 | + public ArrowBuf compress(BufferAllocator allocator, ArrowBuf uncompressedBuffer) { |
| 365 | + throw new UnsupportedOperationException(); |
| 366 | + } |
| 367 | + |
| 368 | + @Override |
| 369 | + public ArrowBuf decompress(BufferAllocator allocator, ArrowBuf compressedBuffer) { |
| 370 | + throw new RuntimeException("simulated decompression failure"); |
| 371 | + } |
| 372 | + |
| 373 | + @Override |
| 374 | + public CompressionUtil.CodecType getCodecType() { |
| 375 | + return codecType; |
| 376 | + } |
| 377 | + }; |
| 378 | + } |
| 379 | + |
| 380 | + @Override |
| 381 | + public CompressionCodec createCodec( |
| 382 | + CompressionUtil.CodecType codecType, int compressionLevel) { |
| 383 | + return createCodec(codecType); |
| 384 | + } |
| 385 | + }; |
| 386 | + |
| 387 | + try (BufferAllocator testAllocator = |
| 388 | + allocator.newChildAllocator("test", 0, Integer.MAX_VALUE)) { |
| 389 | + try (VectorSchemaRoot root = VectorSchemaRoot.create(schema, testAllocator)) { |
| 390 | + VectorLoader loader = new VectorLoader(root, failingFactory); |
| 391 | + ArrowBodyCompression compression = |
| 392 | + new ArrowBodyCompression( |
| 393 | + CompressionUtil.CodecType.LZ4_FRAME.getType(), |
| 394 | + org.apache.arrow.flatbuf.BodyCompressionMethod.BUFFER); |
| 395 | + List<ArrowFieldNode> nodes = asList(new ArrowFieldNode(1, 0)); |
| 396 | + ArrowBuf validityBuf = testAllocator.buffer(8); |
| 397 | + validityBuf.writerIndex(8); |
| 398 | + ArrowBuf dataBuf = testAllocator.buffer(4); |
| 399 | + dataBuf.writerIndex(4); |
| 400 | + try (ArrowRecordBatch batch = |
| 401 | + new ArrowRecordBatch(1, nodes, asList(validityBuf, dataBuf), compression)) { |
| 402 | + RuntimeException ex = |
| 403 | + org.junit.jupiter.api.Assertions.assertThrows( |
| 404 | + RuntimeException.class, () -> loader.load(batch)); |
| 405 | + assertTrue(ex.getMessage().contains("simulated decompression failure")); |
| 406 | + } finally { |
| 407 | + validityBuf.close(); |
| 408 | + dataBuf.close(); |
| 409 | + } |
| 410 | + } |
| 411 | + assertEquals(0, testAllocator.getAllocatedMemory()); |
| 412 | + } |
| 413 | + } |
351 | 414 | } |
0 commit comments