paleolimbot commented on code in PR #43977:
URL: https://github.com/apache/arrow/pull/43977#discussion_r1765302918


##########
cpp/src/parquet/geometry_util_internal.h:
##########
@@ -0,0 +1,691 @@
+// 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 <algorithm>
+#include <cmath>
+#include <limits>
+#include <string>
+#include <unordered_set>
+
+#include "arrow/util/endian.h"
+#include "arrow/util/logging.h"
+#include "arrow/util/macros.h"
+#include "arrow/util/ubsan.h"
+#include "parquet/exception.h"
+
+namespace parquet::geometry {
+
+constexpr double kInf = std::numeric_limits<double>::infinity();
+
+struct Dimensions {
+  enum dimensions { XY = 0, XYZ = 1, XYM = 2, XYZM = 3 };
+
+  static dimensions FromWKB(uint32_t wkb_geometry_type) {
+    switch (wkb_geometry_type / 1000) {
+      case 0:
+        return XY;
+      case 1:
+        return XYZ;
+      case 2:
+        return XYM;
+      case 3:
+        return XYZM;
+      default:
+        throw ParquetException("Invalid wkb_geometry_type: ", 
wkb_geometry_type);
+    }
+  }
+
+  template <dimensions dims>
+  constexpr static uint32_t size();
+
+  static uint32_t size(dimensions dims);
+
+  // Where to look in a coordinate with this dimension
+  // for the X, Y, Z, and M dimensions, respectively.
+  static std::array<int, 4> ToXYZM(dimensions dims) {
+    switch (dims) {
+      case XY:
+        return {0, 1, -1, -1};
+      case XYZ:
+        return {0, 1, 2, -1};
+      case XYM:
+        return {0, 1, -1, 2};
+      case XYZM:
+        return {0, 1, 2, 3};
+      default:
+        return {-1, -1, -1, -1};
+    }
+  }
+
+  static std::string ToString(dimensions dims) {
+    switch (dims) {
+      case XY:
+        return "XY";
+      case XYZ:
+        return "XYZ";
+      case XYM:
+        return "XYM";
+      case XYZM:
+        return "XYZM";
+      default:
+        return "";
+    }
+  }
+};
+
+template <>
+constexpr uint32_t Dimensions::size<Dimensions::XY>() {
+  return 2;
+}
+
+template <>
+constexpr uint32_t Dimensions::size<Dimensions::XYZ>() {
+  return 3;
+}
+
+template <>
+constexpr uint32_t Dimensions::size<Dimensions::XYM>() {
+  return 3;
+}
+
+template <>
+constexpr uint32_t Dimensions::size<Dimensions::XYZM>() {
+  return 4;
+}
+
+inline uint32_t Dimensions::size(dimensions dims) {
+  switch (dims) {
+    case XY:
+      return size<XY>();
+    case XYZ:
+      return size<XYZ>();
+    case XYM:
+      return size<XYM>();
+    case XYZM:
+      return size<XYZM>();
+    default:
+      return 0;
+  }
+}
+
+struct GeometryType {
+  enum geometry_type {
+    POINT = 1,
+    LINESTRING = 2,
+    POLYGON = 3,
+    MULTIPOINT = 4,
+    MULTILINESTRING = 5,
+    MULTIPOLYGON = 6,
+    GEOMETRYCOLLECTION = 7
+  };
+
+  static geometry_type FromWKB(uint32_t wkb_geometry_type) {
+    switch (wkb_geometry_type % 1000) {
+      case 1:
+        return POINT;
+      case 2:
+        return LINESTRING;
+      case 3:
+        return POLYGON;
+      case 4:
+        return MULTIPOINT;
+      case 5:
+        return MULTILINESTRING;
+      case 6:
+        return MULTIPOLYGON;
+      case 7:
+        return GEOMETRYCOLLECTION;
+      default:
+        throw ParquetException("Invalid wkb_geometry_type: ", 
wkb_geometry_type);
+    }
+  }
+
+  static uint32_t ToWKB(geometry_type geometry_type, bool has_z, bool has_m) {
+    uint32_t wkb_geom_type = 0;
+    switch (geometry_type) {
+      case POINT:
+        wkb_geom_type = 1;
+        break;
+      case LINESTRING:
+        wkb_geom_type = 2;
+        break;
+      case POLYGON:
+        wkb_geom_type = 3;
+        break;
+      case MULTIPOINT:
+        wkb_geom_type = 4;
+        break;
+      case MULTILINESTRING:
+        wkb_geom_type = 5;
+        break;
+      case MULTIPOLYGON:
+        wkb_geom_type = 6;
+        break;
+      case GEOMETRYCOLLECTION:
+        wkb_geom_type = 7;
+        break;
+      default:
+        throw ParquetException("Invalid geometry_type: ", geometry_type);
+    }
+    if (has_z) {
+      wkb_geom_type += 1000;
+    }
+    if (has_m) {
+      wkb_geom_type += 2000;
+    }
+    return wkb_geom_type;
+  }
+
+  static std::string ToString(geometry_type geometry_type) {
+    switch (geometry_type) {
+      case POINT:
+        return "POINT";
+      case LINESTRING:
+        return "LINESTRING";
+      case POLYGON:
+        return "POLYGON";
+      case MULTIPOINT:
+        return "MULTIPOINT";
+      case MULTILINESTRING:
+        return "MULTILINESTRING";
+      case MULTIPOLYGON:
+        return "MULTIPOLYGON";
+      case GEOMETRYCOLLECTION:
+        return "GEOMETRYCOLLECTION";
+      default:
+        return "";
+    }
+  }
+};
+
+class WKBBuffer {
+ public:
+  enum Endianness { WKB_BIG_ENDIAN = 0, WKB_LITTLE_ENDIAN = 1 };
+
+  WKBBuffer() : data_(NULLPTR), size_(0) {}
+  WKBBuffer(const uint8_t* data, int64_t size) : data_(data), size_(size) {}
+
+  void Init(const uint8_t* data, int64_t size) {
+    data_ = data;
+    size_ = size;
+  }
+
+  uint8_t ReadUInt8() {
+    if (size_ < 1) {
+      throw ParquetException("Can't read 1 byte from empty WKBBuffer");
+    }
+
+    size_ -= 1;
+    return *data_++;
+  }
+
+  uint32_t ReadUInt32(bool swap) {
+    if (ARROW_PREDICT_FALSE(swap)) {
+      return ReadUInt32<true>();
+    } else {
+      return ReadUInt32<false>();
+    }
+  }
+
+  template <bool swap>
+  uint32_t ReadUInt32() {
+    if (size_ < sizeof(uint32_t)) {
+      throw ParquetException("Can't read 4 bytes from WKBBuffer with ", size_,
+                             "remaining");
+    }
+
+    uint32_t value;
+    memcpy(&value, data_, sizeof(uint32_t));
+    data_ += sizeof(uint32_t);
+    size_ -= sizeof(uint32_t);
+
+    if constexpr (swap) {
+      value = ::arrow::bit_util::ByteSwap(value);
+    }
+
+    return value;
+  }
+
+  template <bool swap>
+  void ReadDoubles(uint32_t n, double* out) {
+    if (n == 0) {
+      return;
+    }
+
+    size_t total_bytes = n * sizeof(double);
+    if (size_ < total_bytes) {
+      throw ParquetException("Can't read ", total_bytes, " bytes from 
WKBBuffer with ",
+                             size_, "remaining");
+    }
+
+    memcpy(out, data_, total_bytes);
+    data_ += total_bytes;
+    size_ -= total_bytes;
+
+    if constexpr (swap) {
+      for (uint32_t i = 0; i < n; i++) {
+        out[i] = ::arrow::bit_util::ByteSwap(out[i]);
+      }
+    }
+  }
+
+  size_t size() { return size_; }
+
+ private:
+  const uint8_t* data_;
+  size_t size_;
+};
+
+struct BoundingBox {
+  BoundingBox(Dimensions::dimensions dimensions, const std::array<double, 4>& 
mins,
+              const std::array<double, 4>& maxes)
+      : dimensions(dimensions) {
+    std::memcpy(min, mins.data(), sizeof(min));
+    std::memcpy(max, maxes.data(), sizeof(max));
+  }
+  explicit BoundingBox(Dimensions::dimensions dimensions = Dimensions::XYZM)
+      : dimensions(dimensions),
+        min{kInf, kInf, kInf, kInf},
+        max{-kInf, -kInf, -kInf, -kInf} {}
+
+  BoundingBox(const BoundingBox& other) = default;
+  BoundingBox& operator=(const BoundingBox&) = default;
+
+  void Reset() {
+    for (int i = 0; i < 4; i++) {
+      min[i] = kInf;
+      max[i] = -kInf;
+    }
+  }
+
+  void Merge(const BoundingBox& other) {
+    if (ARROW_PREDICT_TRUE(dimensions == other.dimensions)) {
+      for (int i = 0; i < 4; i++) {
+        min[i] = std::min(min[i], other.min[i]);
+        max[i] = std::max(max[i], other.max[i]);
+      }
+
+      return;
+    } else if (dimensions == Dimensions::XYZM) {
+      Merge(other.ToXYZM());

Review Comment:
   This is what gets used to "unscramble" the bounding boxes for XY, XYZ, XYM, 
and XYZM into a canonical XYZM bounding box. It might be clearer to have that 
"canonical" bounding box (i.e., one with named dimensions rather than numbered 
dimensions).
   
   The motivation with that whole system is to avoid the "canonicalize 
dimensions" step for every geometry (instead, do it once whenever some number 
of geometries has been ingested and the statistics have been requested).



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

Reply via email to