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


##########
cpp/src/parquet/types.cc:
##########
@@ -1603,6 +1646,139 @@ class LogicalType::Impl::Float16 final : public 
LogicalType::Impl::Incompatible,
 
 GENERATE_MAKE(Float16)
 
+#define geometry_edges_string(u___)             \
+  ((u___) == LogicalType::GeometryEdges::PLANAR \
+       ? "planar"                               \
+       : ((u___) == LogicalType::GeometryEdges::SPHERICAL ? "spherical" : 
"unknown"))
+
+#define geometry_encoding_string(u___) \
+  ((u___) == LogicalType::GeometryEncoding::WKB ? "wkb" : "unknown")
+
+class LogicalType::Impl::Geometry final : public 
LogicalType::Impl::Incompatible,
+                                          public 
LogicalType::Impl::SimpleApplicable {
+ public:
+  friend class GeometryLogicalType;
+
+  std::string ToString() const override;
+  std::string ToJSON() const override;
+  format::LogicalType ToThrift() const override;
+  bool Equals(const LogicalType& other) const override;
+
+  const std::string& crs() const { return crs_; }
+  LogicalType::GeometryEdges::edges edges() const { return edges_; }
+  LogicalType::GeometryEncoding::geometry_encoding encoding() const { return 
encoding_; }
+  const std::string& metadata() const { return metadata_; }
+
+ private:
+  Geometry(std::string crs, LogicalType::GeometryEdges::edges edges,
+           LogicalType::GeometryEncoding::geometry_encoding encoding,
+           std::string metadata)
+      : LogicalType::Impl(LogicalType::Type::GEOMETRY, SortOrder::UNKNOWN),
+        LogicalType::Impl::SimpleApplicable(parquet::Type::BYTE_ARRAY),
+        crs_(std::move(crs)),
+        edges_(edges),
+        encoding_(encoding),
+        metadata_(std::move(metadata)) {}
+
+  std::string crs_;
+  LogicalType::GeometryEdges::edges edges_;
+  LogicalType::GeometryEncoding::geometry_encoding encoding_;
+  std::string metadata_;
+};
+
+std::string LogicalType::Impl::Geometry::ToString() const {
+  std::stringstream type;
+  type << "Geometry(crs=" << crs_ << ", edges=" << 
geometry_edges_string(edges_)
+       << ", encoding=" << geometry_encoding_string(encoding_)
+       << ", metadata=" << metadata_ << ")";
+  return type.str();
+}
+
+std::string LogicalType::Impl::Geometry::ToJSON() const {
+  std::stringstream json;
+  json << R"({"Type": "Geometry")";
+
+  if (crs_.size() > 0) {
+    // TODO(paleolimbot): we'll need to escape the crs or assume that it's 
valid JSON
+    json << R"(, "crs": )" << crs_;
+  }
+
+  json << R"(, "edges": ")" << geometry_edges_string(edges_) << R"(")";
+  json << R"(, "encoding": ")" << geometry_encoding_string(encoding_) << 
R"(")";
+
+  if (metadata_.size() > 0) {
+    // TODO(paleolimbot): we'll need to escape the metadata or assume that 
it's valid JSON
+    json << R"(, "metadata": )" << crs_;
+  }
+
+  json << "}";
+  return json.str();
+}
+
+format::LogicalType LogicalType::Impl::Geometry::ToThrift() const {
+  format::LogicalType type;
+  format::GeometryType geometry_type;
+
+  // Canonially export crs of "" as an unset CRS
+  if (crs_.size() > 0) {
+    geometry_type.__set_crs(crs_);
+  }
+
+  DCHECK(edges_ != LogicalType::GeometryEdges::UNKNOWN);
+  if (edges_ == LogicalType::GeometryEdges::SPHERICAL) {
+    geometry_type.__set_edges(format::Edges::SPHERICAL);
+  } else {
+    geometry_type.__set_edges(format::Edges::PLANAR);
+  }
+
+  DCHECK_EQ(encoding_, LogicalType::GeometryEncoding::WKB);
+  geometry_type.__set_encoding(format::GeometryEncoding::WKB);
+
+  // Canonically export empty metadata as unset
+  if (metadata_.size() > 0) {
+    geometry_type.__set_metadata(metadata_);
+  }
+
+  type.__set_GEOMETRY(geometry_type);
+  return type;
+}
+
+bool LogicalType::Impl::Geometry::Equals(const LogicalType& other) const {
+  if (other.is_geometry()) {
+    const auto& other_geometry = checked_cast<const 
GeometryLogicalType&>(other);
+    return crs() == other_geometry.crs() && edges() == other_geometry.edges() 
&&
+           encoding() == other_geometry.encoding() &&
+           metadata() == other_geometry.metadata();
+  } else {
+    return false;
+  }
+}
+
+const std::string& GeometryLogicalType::crs() const {
+  return (dynamic_cast<const LogicalType::Impl::Geometry&>(*impl_)).crs();

Review Comment:
   I've reverted  to `dynamic_cast` after seeing CI failures after switching to 
`checked_cast`, since static_casting to classes with virtual base is not 
allowed by some of the compilers.



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