szehon-ho commented on code in PR #53227:
URL: https://github.com/apache/spark/pull/53227#discussion_r2663011312


##########
sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/util/geo/GeometryCollection.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.spark.sql.catalyst.util.geo;
+
+import java.util.List;
+
+/**
+ * Represents a GeometryCollection geometry.
+ */
+class GeometryCollection extends GeometryModel {
+  private final List<GeometryModel> geometries;
+  private final boolean hasZ;
+  private final boolean hasM;
+
+  GeometryCollection(List<GeometryModel> geometries, int srid, boolean hasZ, 
boolean hasM) {
+    super(GeoTypeId.GEOMETRY_COLLECTION, srid);
+    this.geometries = geometries;
+    this.hasZ = hasZ;
+    this.hasM = hasM;
+  }
+
+  List<GeometryModel> getGeometries() {
+    return geometries;
+  }
+
+  int getNumGeometries() {
+    return geometries.size();
+  }
+
+  @Override
+  boolean isEmpty() {
+    return geometries.isEmpty() || 
geometries.stream().allMatch(GeometryModel::isEmpty);
+  }
+
+  @Override
+  int getDimensionCount() {
+    return 2 + (hasZ ? 1 : 0) + (hasM ? 1 : 0);
+  }
+
+  @Override
+  boolean hasZ() {
+    return hasZ;
+  }
+
+  @Override
+  boolean hasM() {
+    return hasM;
+  }
+
+  @Override
+  public String toString() {
+    if (isEmpty()) {
+      return "GEOMETRYCOLLECTION EMPTY";
+    }
+    return "GEOMETRYCOLLECTION (" + geometries.size() + " geometries)";
+  }

Review Comment:
   Removed for now, we can tackle wkt later



##########
sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/util/geo/WkbReader.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * 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.spark.sql.catalyst.util.geo;
+
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.apache.spark.sql.catalyst.util.geo.WkbConstants.DEFAULT_SRID;
+
+/**
+ * Reader for parsing Well-Known Binary (WKB) format geometries.
+ * This class implements the OGC Simple Features specification for WKB parsing.
+ */
+public class WkbReader {
+  private ByteBuffer buffer;
+  private final int validationLevel;
+  private byte[] currentWkb;
+
+  public WkbReader() {
+    this(1);
+  }
+
+  public WkbReader(int validationLevel) {
+    this.validationLevel = validationLevel;
+  }
+
+  // ========== Coordinate Validation Helpers ==========
+
+  /**
+   * Returns true if the coordinate value is valid for a non-empty point.
+   * A valid coordinate is finite (not NaN and not Infinity).
+   */
+  private static boolean isValidCoordinate(double value) {
+    return Double.isFinite(value);
+  }
+
+  /**
+   * Returns true if the coordinate value is valid for a point that may be 
empty.
+   * A valid coordinate is either finite or NaN (for empty points).
+   * Infinity values are not allowed.
+   */
+  private static boolean isValidCoordinateAllowEmpty(double value) {
+    return Double.isFinite(value) || Double.isNaN(value);
+  }
+
+  /**
+   * Reads a geometry from WKB bytes.
+   */
+  public GeometryModel read(byte[] wkb) {
+    try {
+      currentWkb = wkb;
+      return readGeometry(DEFAULT_SRID);
+    } finally {
+      // Clear references to allow garbage collection
+      buffer = null;
+      currentWkb = null;
+    }
+  }
+
+  /**
+   * Reads a geometry from WKB bytes with a specified SRID.
+   */
+  public GeometryModel read(byte[] wkb, int srid) {
+    try {
+      currentWkb = wkb;
+      return readGeometry(srid);
+    } finally {
+      // Clear references to allow garbage collection
+      buffer = null;
+      currentWkb = null;
+    }
+  }
+
+  private void checkNotAtEnd(long pos) {
+    if (buffer.position() >= buffer.limit()) {
+      throw new WkbParseException("Unexpected end of WKB buffer", pos, 
currentWkb);
+    }
+  }
+
+  private ByteOrder readEndianness() {
+    checkNotAtEnd(buffer.position());
+    byte endianValue = buffer.get();
+    if (endianValue != WkbConstants.BIG_ENDIAN && endianValue != 
WkbConstants.LITTLE_ENDIAN) {
+      throw new WkbParseException("Invalid byte order " + endianValue, 
buffer.position() - 1,
+        currentWkb);
+    }
+    return endianValue == WkbConstants.LITTLE_ENDIAN ?
+      ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN;
+  }
+
+  private int readInt() {
+    if (buffer.remaining() < WkbConstants.INT_SIZE) {

Review Comment:
   Good catch, fixed



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