zhangfengcdt commented on code in PR #2971:
URL: https://github.com/apache/parquet-java/pull/2971#discussion_r2084674027
##########
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:
Sure, I have added the following four test cases to
TestParquetMetadataConverter.java:
- testGeometryLogicalType
- testGeographyLogicalType
- testGeometryLogicalTypeWithMissingCrs
- testGeographyLogicalTypeWithMissingParameters
##########
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:
I think we have discussed this and looks like in order to avoid contagious
geometries to impact the entire statistics (e.g., bbox), we will skip merging
the null bbox and types if they are null. But I want to get @jiayuasu and
@paleolimbot opinions on this again to confirm. Thanks!
##########
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:
Ok, I will change the code to only check the first coordinate.
##########
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:
same as the above
--
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]