zhangfengcdt commented on code in PR #2971: URL: https://github.com/apache/parquet-java/pull/2971#discussion_r2074155867
########## parquet-column/src/main/java/org/apache/parquet/column/statistics/geometry/GeospatialStatistics.java: ########## @@ -0,0 +1,250 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.parquet.column.statistics.geometry; + +import org.apache.parquet.Preconditions; +import org.apache.parquet.column.schema.EdgeInterpolationAlgorithm; +import org.apache.parquet.io.api.Binary; +import org.apache.parquet.schema.LogicalTypeAnnotation; +import org.apache.parquet.schema.PrimitiveType; +import org.locationtech.jts.geom.Geometry; +import org.locationtech.jts.io.ParseException; +import org.locationtech.jts.io.WKBReader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A structure for capturing metadata for estimating the unencoded, + * uncompressed size of geospatial data written. + */ +public class GeospatialStatistics { + private static final Logger LOG = LoggerFactory.getLogger(GeospatialStatistics.class); + + // Metadata that may impact the statistics calculation + private final BoundingBox boundingBox; + private final EdgeInterpolationAlgorithm edgeAlgorithm; + private final GeospatialTypes geospatialTypes; + + /** + * Merge the statistics from another GeospatialStatistics object. + * + * @param other the other GeospatialStatistics object + */ + public void mergeStatistics(GeospatialStatistics other) { + if (other == null) { + return; + } + if (this.boundingBox != null && other.boundingBox != null) { + this.boundingBox.merge(other.boundingBox); + } + if (this.geospatialTypes != null && other.geospatialTypes != null) { + this.geospatialTypes.merge(other.geospatialTypes); + } + } + + /** + * Builder to create a GeospatialStatistics. + */ + public static class Builder { + private BoundingBox boundingBox; + private GeospatialTypes geospatialTypes; + private EdgeInterpolationAlgorithm edgeAlgorithm; + private final WKBReader reader = new WKBReader(); + + /** + * Create a builder to create a GeospatialStatistics. + * For Geometry type, edgeAlgorithm is not required. + */ + public Builder() { + this.boundingBox = new BoundingBox(); + this.geospatialTypes = new GeospatialTypes(); + this.edgeAlgorithm = null; + } + + /** + * Create a builder to create a GeospatialStatistics. + * For Geography type, optional edgeAlgorithm can be set. + */ + public Builder(EdgeInterpolationAlgorithm edgeAlgorithm) { + this.boundingBox = new BoundingBox(); + this.geospatialTypes = new GeospatialTypes(); + this.edgeAlgorithm = edgeAlgorithm; + } + + public void update(Binary value) { + if (value == null) { + return; + } + try { + Geometry geom = reader.read(value.getBytes()); + update(geom); + } catch (ParseException e) { + LOG.warn("Failed to parse WKB geometry, aborting statistics update", e); + abort(); + } + } + + private void update(Geometry geom) { + boundingBox.update(geom); + geospatialTypes.update(geom); + } + + public void abort() { + boundingBox.abort(); + geospatialTypes.abort(); + } Review Comment: Thanks @Kontinuation ! I have made the following changes: - add valid flag to BoundingBox impl - check geostatistics validity when writing to parquet - add tests for mix of valid and invalid geometry update -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
