Skip to content

Commit 6247eaa

Browse files
authored
MINOR: Include local file paths in reader diagnostics (#3804)
1 parent efec555 commit 6247eaa

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

‎parquet-common/src/main/java/org/apache/parquet/io/LocalInputFile.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public LocalInputFile(Path file) {
3636
path = file;
3737
}
3838

39+
@Override
40+
public String toString() {
41+
return path.toString();
42+
}
43+
3944
@Override
4045
public long getLength() throws IOException {
4146
if (length == -1) {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.parquet.hadoop;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
23+
24+
import java.io.IOException;
25+
import java.nio.file.Files;
26+
import java.nio.file.Path;
27+
import org.apache.parquet.example.data.Group;
28+
import org.apache.parquet.example.data.simple.SimpleGroupFactory;
29+
import org.apache.parquet.hadoop.example.ExampleParquetWriter;
30+
import org.apache.parquet.io.LocalInputFile;
31+
import org.apache.parquet.io.LocalOutputFile;
32+
import org.apache.parquet.schema.MessageType;
33+
import org.apache.parquet.schema.MessageTypeParser;
34+
import org.junit.jupiter.api.Test;
35+
import org.junit.jupiter.api.io.TempDir;
36+
import org.junit.jupiter.params.ParameterizedTest;
37+
import org.junit.jupiter.params.provider.ValueSource;
38+
39+
public class TestLocalParquetFileReader {
40+
41+
@TempDir
42+
private Path tempDir;
43+
44+
@ParameterizedTest
45+
@ValueSource(ints = {0, 10, 12})
46+
void invalidFileIncludesPath(int length) throws IOException {
47+
Path path = Files.write(tempDir.resolve("invalid.parquet"), new byte[length]);
48+
49+
assertThatThrownBy(() -> {
50+
try (ParquetFileReader ignored = ParquetFileReader.open(new LocalInputFile(path))) {}
51+
})
52+
.isInstanceOf(RuntimeException.class)
53+
.hasMessageStartingWith(path + " is not a Parquet file");
54+
}
55+
56+
@Test
57+
void validFileReportsPath() throws IOException {
58+
Path path = tempDir.resolve("valid.parquet");
59+
MessageType schema = MessageTypeParser.parseMessageType("message test { required int32 value; }");
60+
try (ParquetWriter<Group> writer = ExampleParquetWriter.builder(new LocalOutputFile(path))
61+
.withType(schema)
62+
.build()) {
63+
writer.write(new SimpleGroupFactory(schema).newGroup().append("value", 7));
64+
}
65+
66+
try (ParquetFileReader reader = ParquetFileReader.open(new LocalInputFile(path))) {
67+
assertThat(reader.getFile()).isEqualTo(path.toString());
68+
assertThat(reader.getRecordCount()).isEqualTo(1);
69+
assertThat(reader.getFooter().getFileMetaData().getSchema()).isEqualTo(schema);
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)