|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.arrow.compression; |
| 19 | + |
| 20 | +import java.io.ByteArrayOutputStream; |
| 21 | +import java.nio.channels.Channels; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import org.apache.arrow.memory.BufferAllocator; |
| 27 | +import org.apache.arrow.memory.RootAllocator; |
| 28 | +import org.apache.arrow.vector.GenerateSampleData; |
| 29 | +import org.apache.arrow.vector.VectorSchemaRoot; |
| 30 | +import org.apache.arrow.vector.compression.CompressionUtil; |
| 31 | +import org.apache.arrow.vector.compression.NoCompressionCodec; |
| 32 | +import org.apache.arrow.vector.ipc.ArrowFileReader; |
| 33 | +import org.apache.arrow.vector.ipc.ArrowFileWriter; |
| 34 | +import org.apache.arrow.vector.ipc.message.IpcOption; |
| 35 | +import org.apache.arrow.vector.types.pojo.ArrowType; |
| 36 | +import org.apache.arrow.vector.types.pojo.Field; |
| 37 | +import org.apache.arrow.vector.types.pojo.FieldType; |
| 38 | +import org.apache.arrow.vector.types.pojo.Schema; |
| 39 | +import org.apache.arrow.vector.util.ByteArrayReadableSeekableByteChannel; |
| 40 | +import org.junit.Assert; |
| 41 | +import org.junit.Test; |
| 42 | + |
| 43 | +public class TestArrowReaderWriterWithCompression { |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testArrowFileZstdRoundTrip() throws Exception { |
| 47 | + // Prepare sample data |
| 48 | + final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); |
| 49 | + List<Field> fields = new ArrayList<>(); |
| 50 | + fields.add(new Field("col", FieldType.notNullable(new ArrowType.Utf8()), new ArrayList<>())); |
| 51 | + VectorSchemaRoot root = VectorSchemaRoot.create(new Schema(fields), allocator); |
| 52 | + final int rowCount = 10; |
| 53 | + GenerateSampleData.generateTestData(root.getVector(0), rowCount); |
| 54 | + root.setRowCount(rowCount); |
| 55 | + |
| 56 | + // Write an in-memory compressed arrow file |
| 57 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 58 | + try (final ArrowFileWriter writer = |
| 59 | + new ArrowFileWriter(root, null, Channels.newChannel(out), new HashMap<>(), |
| 60 | + IpcOption.DEFAULT, CommonsCompressionFactory.INSTANCE, CompressionUtil.CodecType.ZSTD)) { |
| 61 | + writer.start(); |
| 62 | + writer.writeBatch(); |
| 63 | + writer.end(); |
| 64 | + } |
| 65 | + |
| 66 | + // Read the in-memory compressed arrow file with CommonsCompressionFactory provided |
| 67 | + try (ArrowFileReader reader = |
| 68 | + new ArrowFileReader(new ByteArrayReadableSeekableByteChannel(out.toByteArray()), |
| 69 | + allocator, CommonsCompressionFactory.INSTANCE)) { |
| 70 | + Assert.assertEquals(1, reader.getRecordBlocks().size()); |
| 71 | + Assert.assertTrue(reader.loadNextBatch()); |
| 72 | + Assert.assertTrue(root.equals(reader.getVectorSchemaRoot())); |
| 73 | + Assert.assertFalse(reader.loadNextBatch()); |
| 74 | + } |
| 75 | + |
| 76 | + // Read the in-memory compressed arrow file without CompressionFactory provided |
| 77 | + try (ArrowFileReader reader = |
| 78 | + new ArrowFileReader(new ByteArrayReadableSeekableByteChannel(out.toByteArray()), |
| 79 | + allocator, NoCompressionCodec.Factory.INSTANCE)) { |
| 80 | + Assert.assertEquals(1, reader.getRecordBlocks().size()); |
| 81 | + |
| 82 | + Exception exception = Assert.assertThrows(IllegalArgumentException.class, () -> reader.loadNextBatch()); |
| 83 | + String expectedMessage = "Please add arrow-compression module to use CommonsCompressionFactory for ZSTD"; |
| 84 | + Assert.assertEquals(expectedMessage, exception.getMessage()); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments