wgtmac commented on code in PR #2971: URL: https://github.com/apache/parquet-java/pull/2971#discussion_r2050129426
########## parquet-column/src/main/java/org/apache/parquet/column/statistics/geometry/GeospatialTypes.java: ########## @@ -0,0 +1,196 @@ +/* + * 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 java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.parquet.Preconditions; +import org.locationtech.jts.geom.Coordinate; +import org.locationtech.jts.geom.Geometry; + +public class GeospatialTypes { + + private static final int UNKNOWN_TYPE_ID = -1; + private Set<Integer> types = new HashSet<>(); + private boolean valid = true; + + public GeospatialTypes(Set<Integer> types) { + this.types = types; + } + + public GeospatialTypes() {} + + public Set<Integer> getTypes() { + return types; + } + + void update(Geometry geometry) { + if (!valid) { + return; + } + int code = getGeometryTypeCode(geometry); + if (code != UNKNOWN_TYPE_ID) { + types.add(code); + } else { + valid = false; + types.clear(); + } + } + + public void merge(GeospatialTypes other) { + Preconditions.checkArgument(other != null, "Cannot merge with null GeospatialTypes"); + if (!valid) { + return; + } + if (!other.valid) { + valid = false; + types.clear(); + return; + } + types.addAll(other.types); + } + + public void reset() { + types.clear(); + valid = true; + } + + public void abort() { + valid = false; + types.clear(); + } + + public boolean isValid() { + return valid; + } + + public GeospatialTypes copy() { + return new GeospatialTypes(new HashSet<>(types)); + } + + @Override + public String toString() { + return "GeospatialTypes{" + "types=" + + types.stream().map(this::typeIdToString).collect(Collectors.toSet()) + '}'; + } + + private int getGeometryTypeId(Geometry geometry) { + switch (geometry.getGeometryType()) { + case Geometry.TYPENAME_POINT: + return 1; + case Geometry.TYPENAME_LINESTRING: + return 2; + case Geometry.TYPENAME_POLYGON: + return 3; + case Geometry.TYPENAME_MULTIPOINT: + return 4; + case Geometry.TYPENAME_MULTILINESTRING: + return 5; + case Geometry.TYPENAME_MULTIPOLYGON: + return 6; + case Geometry.TYPENAME_GEOMETRYCOLLECTION: + return 7; + default: + return UNKNOWN_TYPE_ID; + } + } + + /** + * Geospatial type codes: + * + * | Type | XY | XYZ | XYM | XYZM | + * | :----------------- | :--- | :--- | :--- | :--: | + * | Point | 0001 | 1001 | 2001 | 3001 | + * | LineString | 0002 | 1002 | 2002 | 3002 | + * | Polygon | 0003 | 1003 | 2003 | 3003 | + * | MultiPoint | 0004 | 1004 | 2004 | 3004 | + * | MultiLineString | 0005 | 1005 | 2005 | 3005 | + * | MultiPolygon | 0006 | 1006 | 2006 | 3006 | + * | GeometryCollection | 0007 | 1007 | 2007 | 3007 | + * + * See https://github.com/apache/parquet-format/blob/master/Geospatial.md#geospatial-types + */ + private int getGeometryTypeCode(Geometry geometry) { + int typeId = getGeometryTypeId(geometry); + if (typeId == UNKNOWN_TYPE_ID) { + return UNKNOWN_TYPE_ID; + } + Coordinate[] coordinates = geometry.getCoordinates(); + boolean hasZ = false; + boolean hasM = false; + for (Coordinate coordinate : coordinates) { + if (!Double.isNaN(coordinate.getZ())) { + hasZ = true; + } + if (!Double.isNaN(coordinate.getM())) { + hasM = true; + } Review Comment: ```suggestion hasM = !Double.isNaN(coordinate.getM()); ``` ########## parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnValueCollector.java: ########## @@ -38,15 +39,18 @@ class ColumnValueCollector { private final ColumnDescriptor path; private final boolean statisticsEnabled; private final boolean sizeStatisticsEnabled; + private final boolean geospatialStatisticsEnabled; private BloomFilterWriter bloomFilterWriter; private BloomFilter bloomFilter; private Statistics<?> statistics; private SizeStatistics.Builder sizeStatisticsBuilder; + private GeospatialStatistics.Builder geospatialStatisticsBuilder; ColumnValueCollector(ColumnDescriptor path, BloomFilterWriter bloomFilterWriter, ParquetProperties props) { this.path = path; this.statisticsEnabled = props.getStatisticsEnabled(path); this.sizeStatisticsEnabled = props.getSizeStatisticsEnabled(path); + this.geospatialStatisticsEnabled = props.getStatisticsEnabled(path); Review Comment: nit: we can remove `geospatialStatisticsEnabled` by using `statisticsEnabled` ########## parquet-hadoop/src/main/java/org/apache/parquet/format/converter/ParquetMetadataConverter.java: ########## @@ -765,6 +801,36 @@ public static Statistics toParquetStatistics( return formatStats; } + private static BoundingBox toParquetBoundingBox(org.apache.parquet.column.statistics.geometry.BoundingBox bbox) { + // Check if any of the required bounding box is valid. + if (!bbox.isValid() || bbox.isEmpty()) { + // According to the Thrift-generated class, these fields are marked as required and must be set explicitly. + // If any of them is NaN, it indicates the bounding box is invalid or uninitialized, + // so we return null to avoid creating a malformed BoundingBox object that would later fail serialization + // or validation. + return null; + } + + // Now we can safely create the BoundingBox object + BoundingBox formatBbox = new BoundingBox(); + formatBbox.setXmin(bbox.getXMin()); + formatBbox.setXmax(bbox.getXMax()); + formatBbox.setYmin(bbox.getYMin()); + formatBbox.setYmax(bbox.getYMax()); + + if (Double.isInfinite(bbox.getZMin()) && Double.isInfinite(bbox.getZMax())) { + formatBbox.setZmin(bbox.getZMin()); + formatBbox.setZmax(bbox.getZMax()); + } + + if (Double.isInfinite(bbox.getMMin()) && Double.isInfinite(bbox.getMMax())) { + formatBbox.setMmin(bbox.getMMin()); + formatBbox.setMmax(bbox.getMMax()); + } Review Comment: ```suggestion if (!Double.isInfinite(bbox.getZMin()) && !Double.isInfinite(bbox.getZMax()) && !Double.isNaN(bbox.getZMin()) && !Double.isNaN(bbox.getZMax())) { formatBbox.setZmin(bbox.getZMin()); formatBbox.setZmax(bbox.getZMax()); } if (!Double.isInfinite(bbox.getMMin()) && !Double.isInfinite(bbox.getMMax()) && !Double.isNaN(bbox.getMMin()) && !Double.isNaN(bbox.getMMax())) { formatBbox.setMmin(bbox.getMMin()); formatBbox.setMmax(bbox.getMMax()); } ``` Shouldn't we write any inf or nan to thrift? ########## parquet-hadoop/src/main/java/org/apache/parquet/format/converter/ParquetMetadataConverter.java: ########## @@ -765,6 +801,36 @@ public static Statistics toParquetStatistics( return formatStats; } + private static BoundingBox toParquetBoundingBox(org.apache.parquet.column.statistics.geometry.BoundingBox bbox) { Review Comment: Could you please add some test cases in `TestParquetMetadataConverter.java` to verify the conversion of type and stats in the bi-direction, especially for invalid bbox? ########## parquet-column/src/main/java/org/apache/parquet/column/statistics/geometry/GeospatialStatistics.java: ########## @@ -0,0 +1,247 @@ +/* + * 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 java.util.Objects; +import org.apache.parquet.Preconditions; +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; + +/** + * A structure for capturing metadata for estimating the unencoded, + * uncompressed size of geospatial data written. + */ +public class GeospatialStatistics { + + // 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) { + abort(); + } + } + + private void update(Geometry geom) { + boundingBox.update(geom); + geospatialTypes.update(geom); + } + + public void abort() { + boundingBox.abort(); + geospatialTypes.abort(); + } + + /** + * Build a GeospatialStatistics from the builder. + * + * @return a new GeospatialStatistics object + */ + public GeospatialStatistics build() { + return new GeospatialStatistics(boundingBox, geospatialTypes, edgeAlgorithm); + } + } + + /** + * Create a new GeospatialStatistics builder with the specified CRS. + * + * @param type the primitive type + * @return a new GeospatialStatistics builder + */ + public static GeospatialStatistics.Builder newBuilder(PrimitiveType type) { + LogicalTypeAnnotation logicalTypeAnnotation = type.getLogicalTypeAnnotation(); + if (logicalTypeAnnotation instanceof LogicalTypeAnnotation.GeometryLogicalTypeAnnotation) { + return new GeospatialStatistics.Builder(); + } else if (logicalTypeAnnotation instanceof LogicalTypeAnnotation.GeographyLogicalTypeAnnotation) { + EdgeInterpolationAlgorithm edgeAlgorithm = + ((LogicalTypeAnnotation.GeographyLogicalTypeAnnotation) logicalTypeAnnotation).getEdgeAlgorithm(); + return new GeospatialStatistics.Builder(edgeAlgorithm); + } else { + return noopBuilder(); + } + } + + /** + * Constructs a GeospatialStatistics object with the specified CRS, bounding box, and geospatial types. + * + * @param boundingBox the bounding box for the geospatial data, or null if not applicable, note that + * - The bounding box (bbox) is omitted only if there are no X or Y values. + * - The Z and/or M statistics are omitted only if there are no Z and/or M values, respectively. + * @param geospatialTypes the geospatial types + */ + public GeospatialStatistics( + BoundingBox boundingBox, GeospatialTypes geospatialTypes, EdgeInterpolationAlgorithm edgeAlgorithm) { + this.boundingBox = boundingBox; + this.geospatialTypes = geospatialTypes; + this.edgeAlgorithm = edgeAlgorithm; + } + + /** + * Constructs a GeospatialStatistics object with the specified CRS. + */ + public GeospatialStatistics() { + this(new BoundingBox(), new GeospatialTypes(), null); + } + + /** + * Constructs a GeospatialStatistics object with the specified CRS and edge interpolation algorithm. + * + * @param crs the coordinate reference system + * @param edgeAlgorithm the edge interpolation algorithm + */ + public GeospatialStatistics(String crs, EdgeInterpolationAlgorithm edgeAlgorithm) { + this.boundingBox = new BoundingBox(); + this.geospatialTypes = new GeospatialTypes(); + this.edgeAlgorithm = edgeAlgorithm; + } + + /** Returns the bounding box. */ + public BoundingBox getBoundingBox() { + return boundingBox; + } + + /** Returns the geometry types. */ + public GeospatialTypes getGeospatialTypes() { + return geospatialTypes; + } + + /** + * @return whether the statistics has valid value. + */ + public boolean isValid() { + if (boundingBox == null && geospatialTypes == null) { + return false; + } + return Objects.requireNonNull(this.boundingBox).isValid() + && Objects.requireNonNull(this.geospatialTypes).isValid(); + } + + public void merge(GeospatialStatistics other) { + Preconditions.checkArgument(other != null, "Cannot merge with null GeometryStatistics"); + + if (boundingBox != null && other.boundingBox != null) { + boundingBox.merge(other.boundingBox); + } + + if (geospatialTypes != null && other.geospatialTypes != null) { + geospatialTypes.merge(other.geospatialTypes); Review Comment: ditto, should we set geospatialTypes to null if it is null on the other side? ########## parquet-column/src/main/java/org/apache/parquet/schema/LogicalTypeAnnotation.java: ########## @@ -1128,6 +1164,128 @@ public int hashCode() { } } + public static class GeometryLogicalTypeAnnotation extends LogicalTypeAnnotation { + private final String crs; + + private GeometryLogicalTypeAnnotation(String crs) { + this.crs = crs; + } + + @Override + @Deprecated + public OriginalType toOriginalType() { + return null; + } + + @Override + public <T> Optional<T> accept(LogicalTypeAnnotationVisitor<T> logicalTypeAnnotationVisitor) { + return logicalTypeAnnotationVisitor.visit(this); + } + + @Override + LogicalTypeToken getType() { + return LogicalTypeToken.GEOMETRY; + } + + @Override + protected String typeParametersAsString() { + StringBuilder sb = new StringBuilder(); + sb.append("(").append(crs != null && !crs.isEmpty() ? crs : "").append(")"); + return sb.toString(); + } + + public String getCrs() { + return crs; + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof GeometryLogicalTypeAnnotation)) { + return false; + } + GeometryLogicalTypeAnnotation other = (GeometryLogicalTypeAnnotation) obj; + return Objects.equals(crs, other.crs); + } + + @Override + public int hashCode() { + return Objects.hash(crs); + } + + @Override + PrimitiveStringifier valueStringifier(PrimitiveType primitiveType) { + return PrimitiveStringifier.WKB_STRINGIFIER; + } + } + + public static class GeographyLogicalTypeAnnotation extends LogicalTypeAnnotation { + private final String crs; + private final EdgeInterpolationAlgorithm edgeAlgorithm; + + private GeographyLogicalTypeAnnotation(String crs, EdgeInterpolationAlgorithm edgeAlgorithm) { + this.crs = crs; + this.edgeAlgorithm = edgeAlgorithm; + } + + @Override + @Deprecated + public OriginalType toOriginalType() { + return null; + } + + @Override + public <T> Optional<T> accept(LogicalTypeAnnotationVisitor<T> logicalTypeAnnotationVisitor) { + return logicalTypeAnnotationVisitor.visit(this); + } + + @Override + LogicalTypeToken getType() { + return LogicalTypeToken.GEOGRAPHY; + } + + @Override + protected String typeParametersAsString() { + StringBuilder sb = new StringBuilder(); + sb.append("("); + if (crs != null && !crs.isEmpty()) { + sb.append(crs); + } + if (edgeAlgorithm != null) { + if (crs != null && !crs.isEmpty()) sb.append(","); + sb.append(edgeAlgorithm); + } + sb.append(")"); + return sb.toString(); Review Comment: ditto, empty string when both are missing? ########## parquet-column/src/main/java/org/apache/parquet/column/statistics/geometry/BoundingBox.java: ########## @@ -0,0 +1,206 @@ +/* + * 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.locationtech.jts.geom.Coordinate; +import org.locationtech.jts.geom.Envelope; +import org.locationtech.jts.geom.Geometry; + +public class BoundingBox { + + private double xMin = Double.POSITIVE_INFINITY; + private double xMax = Double.NEGATIVE_INFINITY; + private double yMin = Double.POSITIVE_INFINITY; + private double yMax = Double.NEGATIVE_INFINITY; + private double zMin = Double.POSITIVE_INFINITY; + private double zMax = Double.NEGATIVE_INFINITY; + private double mMin = Double.POSITIVE_INFINITY; + private double mMax = Double.NEGATIVE_INFINITY; + + public BoundingBox() {} + + public BoundingBox( + double xMin, double xMax, double yMin, double yMax, double zMin, double zMax, double mMin, double mMax) { + this.xMin = xMin; + this.xMax = xMax; + this.yMin = yMin; + this.yMax = yMax; + this.zMin = zMin; + this.zMax = zMax; + this.mMin = mMin; + this.mMax = mMax; + } + + public double getXMin() { + return xMin; + } + + public double getXMax() { + return xMax; + } + + public double getYMin() { + return yMin; + } + + public double getYMax() { + return yMax; + } + + public double getZMin() { + return zMin; + } + + public double getZMax() { + return zMax; + } + + public double getMMin() { + return mMin; + } + + public double getMMax() { + return mMax; + } + + /** + * Checks if the bounding box is valid. + * A bounding box is considered valid if none of the bounds are in their initial state + * (i.e., xMin = +Infinity, xMax = -Infinity, etc.) and none of the bounds contain NaN. + * + * @return true if the bounding box is valid, false otherwise. + */ + public boolean isValid() { + return !(Double.isNaN(xMin) || Double.isNaN(xMax) || Double.isNaN(yMin) || Double.isNaN(yMax)); + } + + /** + * Checks if the bounding box is empty. + * A bounding box is considered empty if any bounds are in their initial state + * + * @return true if the bounding box is empty, false otherwise. + */ + public boolean isEmpty() { + return (Double.isInfinite(xMin) && Double.isInfinite(xMax)) + || (Double.isInfinite(yMin) && Double.isInfinite(yMax)); + } + + /** + * Merges the bounds of another bounding box into this one. + * + * @param other the other BoundingBox to merge + */ + public void merge(BoundingBox other) { + if (other == null || other.isEmpty()) { + return; + } + this.xMin = Math.min(this.xMin, other.xMin); + this.xMax = Math.max(this.xMax, other.xMax); + this.yMin = Math.min(this.yMin, other.yMin); + this.yMax = Math.max(this.yMax, other.yMax); + this.zMin = Math.min(this.zMin, other.zMin); + this.zMax = Math.max(this.zMax, other.zMax); + this.mMin = Math.min(this.mMin, other.mMin); + this.mMax = Math.max(this.mMax, other.mMax); + } + + /** + * Updates the bounding box with the coordinates of the given geometry. + * If the geometry is null or empty, the update is aborted. + */ + public void update(Geometry geometry) { + if (geometry == null || geometry.isEmpty()) { + return; + } + + Envelope envelope = geometry.getEnvelopeInternal(); + updateBounds(envelope.getMinX(), envelope.getMaxX(), envelope.getMinY(), envelope.getMaxY()); + + for (Coordinate coord : geometry.getCoordinates()) { + if (!Double.isNaN(coord.getZ())) { Review Comment: We might need to set an invalid state to zmin/zmax? Same for mmin/mmax ########## parquet-column/src/main/java/org/apache/parquet/schema/LogicalTypeAnnotation.java: ########## @@ -1128,6 +1164,128 @@ public int hashCode() { } } + public static class GeometryLogicalTypeAnnotation extends LogicalTypeAnnotation { + private final String crs; + + private GeometryLogicalTypeAnnotation(String crs) { + this.crs = crs; + } + + @Override + @Deprecated + public OriginalType toOriginalType() { + return null; + } + + @Override + public <T> Optional<T> accept(LogicalTypeAnnotationVisitor<T> logicalTypeAnnotationVisitor) { + return logicalTypeAnnotationVisitor.visit(this); + } + + @Override + LogicalTypeToken getType() { + return LogicalTypeToken.GEOMETRY; + } + + @Override + protected String typeParametersAsString() { + StringBuilder sb = new StringBuilder(); + sb.append("(").append(crs != null && !crs.isEmpty() ? crs : "").append(")"); Review Comment: Should we print an empty string when crs is unavailable? ########## parquet-column/src/main/java/org/apache/parquet/column/statistics/geometry/EdgeInterpolationAlgorithm.java: ########## @@ -0,0 +1,64 @@ +/* + * 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; + +/** + * Edge interpolation algorithm for Geography logical type + */ +public enum EdgeInterpolationAlgorithm { Review Comment: This class should not reside in the statistics package. It is better to be in the org.apache.parquet.column.schema ########## parquet-column/src/main/java/org/apache/parquet/column/statistics/geometry/GeospatialStatistics.java: ########## @@ -0,0 +1,247 @@ +/* + * 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 java.util.Objects; +import org.apache.parquet.Preconditions; +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; + +/** + * A structure for capturing metadata for estimating the unencoded, + * uncompressed size of geospatial data written. + */ +public class GeospatialStatistics { + + // 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) { + abort(); + } + } + + private void update(Geometry geom) { + boundingBox.update(geom); + geospatialTypes.update(geom); + } + + public void abort() { + boundingBox.abort(); + geospatialTypes.abort(); + } + + /** + * Build a GeospatialStatistics from the builder. + * + * @return a new GeospatialStatistics object + */ + public GeospatialStatistics build() { + return new GeospatialStatistics(boundingBox, geospatialTypes, edgeAlgorithm); + } + } + + /** + * Create a new GeospatialStatistics builder with the specified CRS. + * + * @param type the primitive type + * @return a new GeospatialStatistics builder + */ + public static GeospatialStatistics.Builder newBuilder(PrimitiveType type) { + LogicalTypeAnnotation logicalTypeAnnotation = type.getLogicalTypeAnnotation(); + if (logicalTypeAnnotation instanceof LogicalTypeAnnotation.GeometryLogicalTypeAnnotation) { + return new GeospatialStatistics.Builder(); + } else if (logicalTypeAnnotation instanceof LogicalTypeAnnotation.GeographyLogicalTypeAnnotation) { + EdgeInterpolationAlgorithm edgeAlgorithm = + ((LogicalTypeAnnotation.GeographyLogicalTypeAnnotation) logicalTypeAnnotation).getEdgeAlgorithm(); + return new GeospatialStatistics.Builder(edgeAlgorithm); + } else { + return noopBuilder(); + } + } + + /** + * Constructs a GeospatialStatistics object with the specified CRS, bounding box, and geospatial types. + * + * @param boundingBox the bounding box for the geospatial data, or null if not applicable, note that + * - The bounding box (bbox) is omitted only if there are no X or Y values. + * - The Z and/or M statistics are omitted only if there are no Z and/or M values, respectively. + * @param geospatialTypes the geospatial types + */ + public GeospatialStatistics( + BoundingBox boundingBox, GeospatialTypes geospatialTypes, EdgeInterpolationAlgorithm edgeAlgorithm) { + this.boundingBox = boundingBox; + this.geospatialTypes = geospatialTypes; + this.edgeAlgorithm = edgeAlgorithm; + } + + /** + * Constructs a GeospatialStatistics object with the specified CRS. + */ + public GeospatialStatistics() { + this(new BoundingBox(), new GeospatialTypes(), null); + } + + /** + * Constructs a GeospatialStatistics object with the specified CRS and edge interpolation algorithm. + * + * @param crs the coordinate reference system + * @param edgeAlgorithm the edge interpolation algorithm + */ + public GeospatialStatistics(String crs, EdgeInterpolationAlgorithm edgeAlgorithm) { + this.boundingBox = new BoundingBox(); + this.geospatialTypes = new GeospatialTypes(); + this.edgeAlgorithm = edgeAlgorithm; + } + + /** Returns the bounding box. */ + public BoundingBox getBoundingBox() { + return boundingBox; + } + + /** Returns the geometry types. */ + public GeospatialTypes getGeospatialTypes() { + return geospatialTypes; + } + + /** + * @return whether the statistics has valid value. + */ + public boolean isValid() { + if (boundingBox == null && geospatialTypes == null) { + return false; + } + return Objects.requireNonNull(this.boundingBox).isValid() + && Objects.requireNonNull(this.geospatialTypes).isValid(); + } + + public void merge(GeospatialStatistics other) { + Preconditions.checkArgument(other != null, "Cannot merge with null GeometryStatistics"); + + if (boundingBox != null && other.boundingBox != null) { + boundingBox.merge(other.boundingBox); Review Comment: Should we set boundingBox to null if `other.boundingBox` is null? ########## parquet-column/src/main/java/org/apache/parquet/column/statistics/geometry/BoundingBox.java: ########## @@ -0,0 +1,206 @@ +/* + * 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.locationtech.jts.geom.Coordinate; +import org.locationtech.jts.geom.Envelope; +import org.locationtech.jts.geom.Geometry; + +public class BoundingBox { + + private double xMin = Double.POSITIVE_INFINITY; + private double xMax = Double.NEGATIVE_INFINITY; + private double yMin = Double.POSITIVE_INFINITY; + private double yMax = Double.NEGATIVE_INFINITY; + private double zMin = Double.POSITIVE_INFINITY; + private double zMax = Double.NEGATIVE_INFINITY; + private double mMin = Double.POSITIVE_INFINITY; + private double mMax = Double.NEGATIVE_INFINITY; + + public BoundingBox() {} + + public BoundingBox( + double xMin, double xMax, double yMin, double yMax, double zMin, double zMax, double mMin, double mMax) { + this.xMin = xMin; + this.xMax = xMax; + this.yMin = yMin; + this.yMax = yMax; + this.zMin = zMin; + this.zMax = zMax; + this.mMin = mMin; + this.mMax = mMax; + } + + public double getXMin() { + return xMin; + } + + public double getXMax() { + return xMax; + } + + public double getYMin() { + return yMin; + } + + public double getYMax() { + return yMax; + } + + public double getZMin() { + return zMin; + } + + public double getZMax() { + return zMax; + } + + public double getMMin() { + return mMin; + } + + public double getMMax() { + return mMax; + } + + /** + * Checks if the bounding box is valid. + * A bounding box is considered valid if none of the bounds are in their initial state + * (i.e., xMin = +Infinity, xMax = -Infinity, etc.) and none of the bounds contain NaN. + * + * @return true if the bounding box is valid, false otherwise. + */ Review Comment: ```suggestion /** * Checks if the bounding box is valid. * A bounding box is considered valid if none of the bounds contain NaN. * * @return true if the bounding box is valid, false otherwise. */ ``` ########## parquet-column/src/main/java/org/apache/parquet/column/statistics/geometry/GeospatialStatistics.java: ########## @@ -0,0 +1,247 @@ +/* + * 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 java.util.Objects; +import org.apache.parquet.Preconditions; +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; + +/** + * A structure for capturing metadata for estimating the unencoded, + * uncompressed size of geospatial data written. + */ +public class GeospatialStatistics { + + // 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) { + abort(); + } + } + + private void update(Geometry geom) { + boundingBox.update(geom); + geospatialTypes.update(geom); + } + + public void abort() { + boundingBox.abort(); + geospatialTypes.abort(); + } + + /** + * Build a GeospatialStatistics from the builder. + * + * @return a new GeospatialStatistics object + */ + public GeospatialStatistics build() { + return new GeospatialStatistics(boundingBox, geospatialTypes, edgeAlgorithm); + } + } + + /** + * Create a new GeospatialStatistics builder with the specified CRS. + * + * @param type the primitive type + * @return a new GeospatialStatistics builder + */ + public static GeospatialStatistics.Builder newBuilder(PrimitiveType type) { + LogicalTypeAnnotation logicalTypeAnnotation = type.getLogicalTypeAnnotation(); + if (logicalTypeAnnotation instanceof LogicalTypeAnnotation.GeometryLogicalTypeAnnotation) { + return new GeospatialStatistics.Builder(); + } else if (logicalTypeAnnotation instanceof LogicalTypeAnnotation.GeographyLogicalTypeAnnotation) { + EdgeInterpolationAlgorithm edgeAlgorithm = + ((LogicalTypeAnnotation.GeographyLogicalTypeAnnotation) logicalTypeAnnotation).getEdgeAlgorithm(); + return new GeospatialStatistics.Builder(edgeAlgorithm); + } else { + return noopBuilder(); + } + } + + /** + * Constructs a GeospatialStatistics object with the specified CRS, bounding box, and geospatial types. + * + * @param boundingBox the bounding box for the geospatial data, or null if not applicable, note that + * - The bounding box (bbox) is omitted only if there are no X or Y values. + * - The Z and/or M statistics are omitted only if there are no Z and/or M values, respectively. + * @param geospatialTypes the geospatial types + */ + public GeospatialStatistics( + BoundingBox boundingBox, GeospatialTypes geospatialTypes, EdgeInterpolationAlgorithm edgeAlgorithm) { + this.boundingBox = boundingBox; + this.geospatialTypes = geospatialTypes; + this.edgeAlgorithm = edgeAlgorithm; + } + + /** + * Constructs a GeospatialStatistics object with the specified CRS. + */ + public GeospatialStatistics() { + this(new BoundingBox(), new GeospatialTypes(), null); + } + + /** + * Constructs a GeospatialStatistics object with the specified CRS and edge interpolation algorithm. + * + * @param crs the coordinate reference system + * @param edgeAlgorithm the edge interpolation algorithm + */ + public GeospatialStatistics(String crs, EdgeInterpolationAlgorithm edgeAlgorithm) { + this.boundingBox = new BoundingBox(); + this.geospatialTypes = new GeospatialTypes(); + this.edgeAlgorithm = edgeAlgorithm; + } + + /** Returns the bounding box. */ + public BoundingBox getBoundingBox() { + return boundingBox; + } + + /** Returns the geometry types. */ + public GeospatialTypes getGeospatialTypes() { + return geospatialTypes; + } + + /** + * @return whether the statistics has valid value. + */ + public boolean isValid() { + if (boundingBox == null && geospatialTypes == null) { + return false; + } + return Objects.requireNonNull(this.boundingBox).isValid() Review Comment: Does it throw when any is null? ########## parquet-column/src/main/java/org/apache/parquet/column/statistics/geometry/GeospatialTypes.java: ########## @@ -0,0 +1,192 @@ +/* + * 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 java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.parquet.Preconditions; +import org.locationtech.jts.geom.Coordinate; +import org.locationtech.jts.geom.Geometry; + +public class GeospatialTypes { + + private static final int UNKNOWN_TYPE_ID = -1; + private Set<Integer> types = new HashSet<>(); + private boolean valid = true; + + public GeospatialTypes(Set<Integer> types) { + this.types = types; + } + + public GeospatialTypes() {} + + public Set<Integer> getTypes() { + return types; + } + + void update(Geometry geometry) { + if (!valid) { + return; + } + int code = getGeometryTypeCode(geometry); + if (code != UNKNOWN_TYPE_ID) { + types.add(code); + } else { + valid = false; + types.clear(); + } + } + + public void merge(GeospatialTypes other) { + Preconditions.checkArgument(other != null, "Cannot merge with null GeospatialTypes"); + if (!valid) { + return; + } + if (!other.valid) { + valid = false; + types.clear(); + return; + } + types.addAll(other.types); + } + + public void reset() { + types.clear(); + valid = true; + } + + public void abort() { + valid = false; + types.clear(); + } + + public GeospatialTypes copy() { + return new GeospatialTypes(new HashSet<>(types)); + } + + @Override + public String toString() { + return "GeospatialTypes{" + "types=" + + types.stream().map(this::typeIdToString).collect(Collectors.toSet()) + '}'; + } + + private int getGeometryTypeId(Geometry geometry) { + switch (geometry.getGeometryType()) { + case Geometry.TYPENAME_POINT: + return 1; + case Geometry.TYPENAME_LINESTRING: + return 2; + case Geometry.TYPENAME_POLYGON: + return 3; + case Geometry.TYPENAME_MULTIPOINT: + return 4; + case Geometry.TYPENAME_MULTILINESTRING: + return 5; + case Geometry.TYPENAME_MULTIPOLYGON: + return 6; + case Geometry.TYPENAME_GEOMETRYCOLLECTION: + return 7; + default: + return UNKNOWN_TYPE_ID; + } + } + + /** + * Geospatial type codes: + * + * | Type | XY | XYZ | XYM | XYZM | + * | :----------------- | :--- | :--- | :--- | :--: | + * | Point | 0001 | 1001 | 2001 | 3001 | + * | LineString | 0002 | 1002 | 2002 | 3002 | + * | Polygon | 0003 | 1003 | 2003 | 3003 | + * | MultiPoint | 0004 | 1004 | 2004 | 3004 | + * | MultiLineString | 0005 | 1005 | 2005 | 3005 | + * | MultiPolygon | 0006 | 1006 | 2006 | 3006 | + * | GeometryCollection | 0007 | 1007 | 2007 | 3007 | + * + * See https://github.com/apache/parquet-format/blob/master/Geospatial.md#geospatial-types + */ + private int getGeometryTypeCode(Geometry geometry) { + int typeId = getGeometryTypeId(geometry); + if (typeId == UNKNOWN_TYPE_ID) { + return UNKNOWN_TYPE_ID; + } + Coordinate[] coordinates = geometry.getCoordinates(); + boolean hasZ = false; + boolean hasM = false; + for (Coordinate coordinate : coordinates) { + if (!Double.isNaN(coordinate.getZ())) { + hasZ = true; + } + if (!Double.isNaN(coordinate.getM())) { + hasM = true; + } + if (hasZ && hasM) { + break; Review Comment: I mean the current code has to iterate all coordinates for XY dimension which is the most common case I suppose. It is better to check the first coordinate only. ########## parquet-column/src/main/java/org/apache/parquet/column/statistics/geometry/BoundingBox.java: ########## @@ -0,0 +1,206 @@ +/* + * 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.locationtech.jts.geom.Coordinate; +import org.locationtech.jts.geom.Envelope; +import org.locationtech.jts.geom.Geometry; + +public class BoundingBox { + + private double xMin = Double.POSITIVE_INFINITY; + private double xMax = Double.NEGATIVE_INFINITY; + private double yMin = Double.POSITIVE_INFINITY; + private double yMax = Double.NEGATIVE_INFINITY; + private double zMin = Double.POSITIVE_INFINITY; + private double zMax = Double.NEGATIVE_INFINITY; + private double mMin = Double.POSITIVE_INFINITY; + private double mMax = Double.NEGATIVE_INFINITY; + + public BoundingBox() {} + + public BoundingBox( + double xMin, double xMax, double yMin, double yMax, double zMin, double zMax, double mMin, double mMax) { + this.xMin = xMin; + this.xMax = xMax; + this.yMin = yMin; + this.yMax = yMax; + this.zMin = zMin; + this.zMax = zMax; + this.mMin = mMin; + this.mMax = mMax; + } + + public double getXMin() { + return xMin; + } + + public double getXMax() { + return xMax; + } + + public double getYMin() { + return yMin; + } + + public double getYMax() { + return yMax; + } + + public double getZMin() { + return zMin; + } + + public double getZMax() { + return zMax; + } + + public double getMMin() { + return mMin; + } + + public double getMMax() { + return mMax; + } + + /** + * Checks if the bounding box is valid. + * A bounding box is considered valid if none of the bounds are in their initial state + * (i.e., xMin = +Infinity, xMax = -Infinity, etc.) and none of the bounds contain NaN. + * + * @return true if the bounding box is valid, false otherwise. + */ Review Comment: I assume that +/-Inf values are valid in the bbox? If not, we should match the code with the comment. ########## parquet-column/src/main/java/org/apache/parquet/column/statistics/geometry/GeospatialStatistics.java: ########## @@ -0,0 +1,247 @@ +/* + * 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 java.util.Objects; +import org.apache.parquet.Preconditions; +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; + +/** + * A structure for capturing metadata for estimating the unencoded, + * uncompressed size of geospatial data written. + */ +public class GeospatialStatistics { + + // 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) { + abort(); Review Comment: This worths a warn or error log? ########## parquet-column/src/main/java/org/apache/parquet/column/statistics/geometry/BoundingBox.java: ########## @@ -0,0 +1,206 @@ +/* + * 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.locationtech.jts.geom.Coordinate; +import org.locationtech.jts.geom.Envelope; +import org.locationtech.jts.geom.Geometry; + +public class BoundingBox { + + private double xMin = Double.POSITIVE_INFINITY; + private double xMax = Double.NEGATIVE_INFINITY; + private double yMin = Double.POSITIVE_INFINITY; + private double yMax = Double.NEGATIVE_INFINITY; + private double zMin = Double.POSITIVE_INFINITY; + private double zMax = Double.NEGATIVE_INFINITY; + private double mMin = Double.POSITIVE_INFINITY; + private double mMax = Double.NEGATIVE_INFINITY; + + public BoundingBox() {} + + public BoundingBox( + double xMin, double xMax, double yMin, double yMax, double zMin, double zMax, double mMin, double mMax) { + this.xMin = xMin; + this.xMax = xMax; + this.yMin = yMin; + this.yMax = yMax; + this.zMin = zMin; + this.zMax = zMax; + this.mMin = mMin; + this.mMax = mMax; + } + + public double getXMin() { + return xMin; + } + + public double getXMax() { + return xMax; + } + + public double getYMin() { + return yMin; + } + + public double getYMax() { + return yMax; + } + + public double getZMin() { + return zMin; + } + + public double getZMax() { + return zMax; + } + + public double getMMin() { + return mMin; + } + + public double getMMax() { + return mMax; + } + + /** + * Checks if the bounding box is valid. + * A bounding box is considered valid if none of the bounds are in their initial state + * (i.e., xMin = +Infinity, xMax = -Infinity, etc.) and none of the bounds contain NaN. + * + * @return true if the bounding box is valid, false otherwise. + */ Review Comment: BTW, is there any way to check validity of Z and M values? -- 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]
