wgtmac commented on code in PR #45459:
URL: https://github.com/apache/arrow/pull/45459#discussion_r2050086670


##########
cpp/src/parquet/geospatial/statistics.h:
##########
@@ -0,0 +1,165 @@
+// 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.
+
+#pragma once
+
+#include <cstdint>
+#include <memory>
+#include <optional>
+
+#include "parquet/platform.h"
+#include "parquet/types.h"
+
+namespace parquet::geospatial {
+
+/// \brief The maximum number of dimensions represented by a geospatial type
+/// (i.e., X, Y, Z, and M)
+static constexpr int kMaxDimensions = 4;
+
+/// \brief Structure represented encoded statistics to be written to and read 
from Parquet
+/// serialized metadata.
+///
+/// See the Parquet Thrift definition and GeoStatistics for the specific 
definition
+/// of field values.
+struct PARQUET_EXPORT EncodedGeoStatistics {
+  bool writer_calculated_xy_bounds{};
+  double xmin{};

Review Comment:
   Why not initialized to kNaN to minimize the chance of misuse? IMO, we should 
avoid emitting NaN to thrift but it is fine to use NaN at runtime to represent 
invalid data.



##########
cpp/src/parquet/geospatial/statistics.h:
##########
@@ -0,0 +1,165 @@
+// 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.
+
+#pragma once
+
+#include <cstdint>
+#include <memory>
+#include <optional>
+
+#include "parquet/platform.h"
+#include "parquet/types.h"
+
+namespace parquet::geospatial {
+
+/// \brief The maximum number of dimensions represented by a geospatial type
+/// (i.e., X, Y, Z, and M)
+static constexpr int kMaxDimensions = 4;
+
+/// \brief Structure represented encoded statistics to be written to and read 
from Parquet
+/// serialized metadata.
+///
+/// See the Parquet Thrift definition and GeoStatistics for the specific 
definition
+/// of field values.
+struct PARQUET_EXPORT EncodedGeoStatistics {
+  bool writer_calculated_xy_bounds{};
+  double xmin{};
+  double xmax{};
+  double ymin{};
+  double ymax{};
+
+  bool writer_calculated_z_bounds{};

Review Comment:
   Can we simply rename it to `is_z_valid`?



##########
cpp/src/parquet/metadata.cc:
##########
@@ -299,13 +312,25 @@ class ColumnChunkMetaData::ColumnChunkMetaDataImpl {
       return false;
     }
     if (possible_stats_ == nullptr) {
+      // Because we are modifying possible_stats_ in a const method
+      const std::lock_guard<std::mutex> guard(stats_mutex_);
       possible_stats_ = MakeColumnStats(*column_metadata_, descr_);
     }

Review Comment:
   ```suggestion
       {
         // Because we are modifying possible_stats_ in a const method
         const std::lock_guard<std::mutex> guard(stats_mutex_);
         if (possible_stats_ == nullptr) {
           possible_stats_ = MakeColumnStats(*column_metadata_, descr_);
         }
       }
   ```



##########
cpp/src/parquet/geospatial/statistics.cc:
##########
@@ -0,0 +1,393 @@
+// 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.
+
+#include "parquet/geospatial/statistics.h"
+
+#include <cmath>
+#include <memory>
+#include <optional>
+
+#include "arrow/array.h"
+#include "arrow/type.h"
+#include "arrow/util/bit_run_reader.h"
+#include "parquet/exception.h"
+#include "parquet/geospatial/util_internal.h"
+
+using arrow::util::SafeLoad;
+
+namespace parquet::geospatial {
+
+class GeoStatisticsImpl {
+ public:
+  bool Equals(const GeoStatisticsImpl& other) const {
+    return is_valid_ == other.is_valid_ &&
+           bounder_.GeometryTypes() == other.bounder_.GeometryTypes() &&
+           bounder_.Bounds() == other.bounder_.Bounds();
+  }
+
+  void Merge(const GeoStatisticsImpl& other) {
+    is_valid_ = is_valid_ && other.is_valid_;
+    if (!is_valid_) {
+      return;
+    }
+
+    if (is_wraparound_x() || other.is_wraparound_x()) {
+      throw ParquetException("Wraparound X is not supported by 
GeoStatistics::Merge()");
+    }
+
+    bounder_.MergeBox(other.bounder_.Bounds());
+    std::vector<int32_t> other_geometry_types = other.bounder_.GeometryTypes();
+    bounder_.MergeGeometryTypes(other_geometry_types);
+  }
+
+  void Update(const ByteArray* values, int64_t num_values) {
+    if (!is_valid_) {
+      return;
+    }
+
+    if (is_wraparound_x()) {
+      throw ParquetException("Wraparound X is not suppored by 
GeoStatistics::Update()");
+    }
+
+    for (int64_t i = 0; i < num_values; i++) {
+      const ByteArray& item = values[i];
+      try {
+        bounder_.MergeGeometry({reinterpret_cast<const char*>(item.ptr), 
item.len});
+      } catch (ParquetException&) {
+        is_valid_ = false;
+        return;
+      }
+    }
+  }
+
+  void UpdateSpaced(const ByteArray* values, const uint8_t* valid_bits,
+                    int64_t valid_bits_offset, int64_t num_spaced_values,
+                    int64_t num_values) {
+    DCHECK_GT(num_spaced_values, 0);
+
+    if (!is_valid_) {
+      return;
+    }
+
+    if (is_wraparound_x()) {
+      throw ParquetException("Wraparound X is not suppored by 
GeoStatistics::Update()");
+    }
+
+    ::arrow::Status status = ::arrow::internal::VisitSetBitRuns(
+        valid_bits, valid_bits_offset, num_spaced_values,
+        [&](int64_t position, int64_t length) {
+          for (int64_t i = 0; i < length; i++) {
+            ByteArray item = SafeLoad(values + i + position);
+            PARQUET_CATCH_NOT_OK(bounder_.MergeGeometry(
+                {reinterpret_cast<const char*>(item.ptr), item.len}));
+          }
+
+          return ::arrow::Status::OK();
+        });
+
+    if (!status.ok()) {
+      is_valid_ = false;
+    }
+  }
+
+  void Update(const ::arrow::Array& values) {
+    if (!is_valid_) {
+      return;
+    }
+
+    if (is_wraparound_x()) {
+      throw ParquetException("Wraparound X is not suppored by 
GeoStatistics::Update()");
+    }
+
+    // Note that ::arrow::Type::EXTENSION seems to be handled before this is 
called
+    switch (values.type_id()) {
+      case ::arrow::Type::BINARY:
+        UpdateArrayImpl<::arrow::BinaryArray>(values);
+        break;
+      case ::arrow::Type::LARGE_BINARY:
+        UpdateArrayImpl<::arrow::LargeBinaryArray>(values);
+        break;
+      // This does not currently handle run-end encoded, dictionary encodings, 
or views
+      default:
+        throw ParquetException("Unsupported Array type in 
GeoStatistics::Update(Array): ",
+                               values.type()->ToString());
+    }
+  }
+
+  void Reset() {
+    bounder_.Reset();
+    is_valid_ = true;
+  }
+
+  EncodedGeoStatistics Encode() const {
+    if (!is_valid_) {
+      return {};
+    }
+
+    const geospatial::BoundingBox::XYZM& mins = bounder_.Bounds().min;
+    const geospatial::BoundingBox::XYZM& maxes = bounder_.Bounds().max;
+
+    EncodedGeoStatistics out;
+
+    if (has_geometry_types_) {
+      out.geospatial_types = bounder_.GeometryTypes();
+    }
+
+    bool write_x = !bound_empty(0) && bound_valid(0);
+    bool write_y = !bound_empty(1) && bound_valid(1);
+    bool write_z = !bound_empty(2) && bound_valid(2);
+    bool write_m = !bound_empty(3) && bound_valid(3);

Review Comment:
   ```suggestion
       bool write_z = write_x && write_y && !bound_empty(2) && bound_valid(2);
       bool write_m = write_x && write_y && !bound_empty(3) && bound_valid(3);
   ```
   
   Should we drop bbox entirely if x or y is missing as they are `required`?



##########
cpp/src/parquet/thrift_internal.h:
##########
@@ -232,6 +233,81 @@ static inline AadMetadata FromThrift(format::AesGcmCtrV1 
aesGcmCtrV1) {
                      aesGcmCtrV1.supply_aad_prefix};
 }
 
+static inline geospatial::EncodedGeoStatistics FromThrift(
+    const format::GeospatialStatistics& geo_stats) {
+  geospatial::EncodedGeoStatistics out;
+
+  out.geospatial_types = geo_stats.geospatial_types;
+
+  if (geo_stats.__isset.bbox) {
+    out.xmin = geo_stats.bbox.xmin;
+    out.xmax = geo_stats.bbox.xmax;
+    out.ymin = geo_stats.bbox.ymin;
+    out.ymax = geo_stats.bbox.ymax;
+    out.writer_calculated_xy_bounds = true;
+
+    if (geo_stats.bbox.__isset.zmin && geo_stats.bbox.__isset.zmax) {
+      out.zmin = geo_stats.bbox.zmin;
+      out.zmax = geo_stats.bbox.zmax;
+      out.writer_calculated_z_bounds = true;

Review Comment:
   `writer_` prefix looks odd from the perspective of this function (which is 
on the reader side).



##########
cpp/src/generated/parquet_types.h:
##########
@@ -4,3888 +4,3888 @@
  * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
  *  @generated
  */
-#ifndef parquet_TYPES_H
-#define parquet_TYPES_H
+ #ifndef parquet_TYPES_H

Review Comment:
   Why do we still see this file after rebased?



-- 
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: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to