Skip to content

Commit 4cd8dfd

Browse files
committed
add ShowGeospatialStatisticsCommand and tests
1 parent 0114701 commit 4cd8dfd

4 files changed

Lines changed: 150 additions & 1 deletion

File tree

parquet-cli/src/main/java/org/apache/parquet/cli/Main.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.apache.parquet.cli.commands.ShowColumnIndexCommand;
5151
import org.apache.parquet.cli.commands.ShowDictionaryCommand;
5252
import org.apache.parquet.cli.commands.ShowFooterCommand;
53+
import org.apache.parquet.cli.commands.ShowGeospatialStatisticsCommand;
5354
import org.apache.parquet.cli.commands.ShowPagesCommand;
5455
import org.apache.parquet.cli.commands.ShowSizeStatisticsCommand;
5556
import org.apache.parquet.cli.commands.ToAvroCommand;
@@ -107,6 +108,7 @@ public class Main extends Configured implements Tool {
107108
jc.addCommand("scan", new ScanCommand(console));
108109
jc.addCommand("rewrite", new RewriteCommand(console));
109110
jc.addCommand("size-stats", new ShowSizeStatisticsCommand(console));
111+
jc.addCommand("geospatial-stats", new ShowGeospatialStatisticsCommand(console));
110112
}
111113

112114
@Override
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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.cli.commands;
20+
21+
import com.beust.jcommander.Parameter;
22+
import com.beust.jcommander.Parameters;
23+
import com.google.common.base.Preconditions;
24+
import com.google.common.collect.Lists;
25+
import java.io.IOException;
26+
import java.util.List;
27+
import org.apache.commons.text.TextStringBuilder;
28+
import org.apache.parquet.cli.BaseCommand;
29+
import org.apache.parquet.column.statistics.geometry.GeospatialStatistics;
30+
import org.apache.parquet.hadoop.ParquetFileReader;
31+
import org.apache.parquet.hadoop.metadata.BlockMetaData;
32+
import org.apache.parquet.hadoop.metadata.ColumnChunkMetaData;
33+
import org.apache.parquet.hadoop.metadata.ParquetMetadata;
34+
import org.apache.parquet.schema.MessageType;
35+
import org.slf4j.Logger;
36+
37+
@Parameters(commandDescription = "Print geospatial statistics for a Parquet file")
38+
public class ShowGeospatialStatisticsCommand extends BaseCommand {
39+
40+
public ShowGeospatialStatisticsCommand(Logger console) {
41+
super(console);
42+
}
43+
44+
@Parameter(description = "<parquet path>")
45+
List<String> targets;
46+
47+
@Override
48+
@SuppressWarnings("unchecked")
49+
public int run() throws IOException {
50+
Preconditions.checkArgument(targets != null && !targets.isEmpty(), "A Parquet file is required.");
51+
Preconditions.checkArgument(targets.size() == 1, "Cannot process multiple Parquet files.");
52+
53+
String source = targets.get(0);
54+
try (ParquetFileReader reader = ParquetFileReader.open(getConf(), qualifiedPath(source))) {
55+
ParquetMetadata footer = reader.getFooter();
56+
MessageType schema = footer.getFileMetaData().getSchema();
57+
58+
console.info("\nFile path: {}", source);
59+
60+
List<BlockMetaData> rowGroups = footer.getBlocks();
61+
for (int index = 0, n = rowGroups.size(); index < n; index++) {
62+
printRowGroupGeospatialStats(console, index, rowGroups.get(index), schema);
63+
console.info("");
64+
}
65+
}
66+
67+
return 0;
68+
}
69+
70+
private void printRowGroupGeospatialStats(Logger console, int index, BlockMetaData rowGroup, MessageType schema) {
71+
int maxColumnWidth = Math.max(
72+
"column".length(),
73+
rowGroup.getColumns().stream()
74+
.map(col -> col.getPath().toString().length())
75+
.max(Integer::compare)
76+
.orElse(0));
77+
78+
console.info(String.format("\nRow group %d\n%s", index, new TextStringBuilder(80).appendPadding(80, '-')));
79+
80+
String formatString = String.format("%%-%ds %%-15s %%-40s", maxColumnWidth);
81+
console.info(String.format(formatString, "column", "bounding box", "geospatial types"));
82+
83+
for (ColumnChunkMetaData column : rowGroup.getColumns()) {
84+
printColumnGeospatialStats(console, column, schema, maxColumnWidth);
85+
}
86+
}
87+
88+
private void printColumnGeospatialStats(
89+
Logger console, ColumnChunkMetaData column, MessageType schema, int columnWidth) {
90+
GeospatialStatistics stats = column.getGeospatialStatistics();
91+
92+
if (stats != null && stats.isValid()) {
93+
String boundingBox =
94+
stats.getBoundingBox() != null ? stats.getBoundingBox().toString() : "-";
95+
String geospatialTypes = stats.getGeospatialTypes() != null
96+
? stats.getGeospatialTypes().toString()
97+
: "-";
98+
String formatString = String.format("%%-%ds %%-15s %%-40s", columnWidth);
99+
console.info(String.format(formatString, column.getPath(), boundingBox, geospatialTypes));
100+
} else {
101+
String formatString = String.format("%%-%ds %%-15s %%-40s", columnWidth);
102+
console.info(String.format(formatString, column.getPath(), "-", "-"));
103+
}
104+
}
105+
106+
@Override
107+
public List<String> getExamples() {
108+
return Lists.newArrayList("# Show geospatial statistics for a Parquet file", "sample.parquet");
109+
}
110+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.cli.commands;
20+
21+
import java.io.File;
22+
import java.io.IOException;
23+
import java.util.Arrays;
24+
import org.apache.hadoop.conf.Configuration;
25+
import org.junit.Assert;
26+
import org.junit.Test;
27+
28+
public class ShowGeospatialStatisticsCommandTest extends ParquetFileTest {
29+
@Test
30+
public void testShowGeospatialStatisticsCommand() throws IOException {
31+
File file = parquetFile();
32+
ShowGeospatialStatisticsCommand command = new ShowGeospatialStatisticsCommand(createLogger());
33+
command.targets = Arrays.asList(file.getAbsolutePath());
34+
command.setConf(new Configuration());
35+
Assert.assertEquals(0, command.run());
36+
}
37+
}

parquet-column/src/main/java/org/apache/parquet/schema/LogicalTypeAnnotation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ public boolean equals(Object obj) {
11821182
return false;
11831183
}
11841184
GeometryLogicalTypeAnnotation other = (GeometryLogicalTypeAnnotation) obj;
1185-
return crs.equals(other.crs);
1185+
return Objects.equals(crs, other.crs);
11861186
}
11871187

11881188
@Override

0 commit comments

Comments
 (0)