Skip to content

Commit f32c27b

Browse files
authored
GH-34432: [Java] NoCompressionCodec throws for unsupported codec type (#34580)
### Rationale for this change `NoCompressionCodec` does not complain about an unsupported codec type. `ArrowFileReader` uses `NoCompressionCodec` by default and fails to decompress a compressed arrow file. ### What changes are included in this PR? `NoCompressionCodec` throws if unsupported codec type has been requested. ### Are these changes tested? Make sure all tests pass. ### Are there any user-facing changes? No. * Closes: #34432 Authored-by: Gang Wu <[email protected]> Signed-off-by: David Li <[email protected]>
1 parent 427f1b6 commit f32c27b

2 files changed

Lines changed: 98 additions & 1 deletion

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}

‎java/vector/src/main/java/org/apache/arrow/vector/compression/NoCompressionCodec.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,16 @@ public static class Factory implements CompressionCodec.Factory {
6161

6262
@Override
6363
public CompressionCodec createCodec(CompressionUtil.CodecType codecType) {
64-
return NoCompressionCodec.INSTANCE;
64+
switch (codecType) {
65+
case NO_COMPRESSION:
66+
return NoCompressionCodec.INSTANCE;
67+
case LZ4_FRAME:
68+
case ZSTD:
69+
throw new IllegalArgumentException(
70+
"Please add arrow-compression module to use CommonsCompressionFactory for " + codecType);
71+
default:
72+
throw new IllegalArgumentException("Unsupported codec type: " + codecType);
73+
}
6574
}
6675
}
6776
}

0 commit comments

Comments
 (0)