Kontinuation commented on code in PR #2971:
URL: https://github.com/apache/parquet-java/pull/2971#discussion_r2073191563


##########
parquet-hadoop/src/main/java/org/apache/parquet/format/converter/ParquetMetadataConverter.java:
##########
@@ -608,6 +611,11 @@ private void addRowGroup(
         
metaData.setSize_statistics(toParquetSizeStatistics(columnMetaData.getSizeStatistics()));
       }
 
+      if (columnMetaData.getGeospatialStatistics() != null) {
+        metaData.setGeospatial_statistics(
+            
toParquetGeospatialStatistics(columnMetaData.getGeospatialStatistics()));
+      }

Review Comment:
   It is better to test if the geospatial statistics is valid here, and skip 
writing it if the geospatial statistics is invalid.



##########
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:
   `geospatialTypes` has a `valid` field, it will be set to `false` once we 
abort the update, no further written geometries can revive it from invalid 
state to valid state.
   
   However, `boundingBox` does not have a flag indicating if are in invalid 
state because of aborting. It could revive to valid state if we write valid 
geometries following up the invalid geometry. This could be dangerous and the 
incorrect bounding box may appear in the geometry statistics of the parquet 
file being written.
   
   Can we handle the invalid state of `boundingBox` and `geospatialTypes` in a 
consistent way? Can we add a test case to show that geospatial statistics won't 
appear in the written parquet file when invalid geometries were mixed into the 
values being written?



-- 
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]

Reply via email to