Kontinuation commented on code in PR #12667:
URL: https://github.com/apache/iceberg/pull/12667#discussion_r2371532859


##########
api/src/main/java/org/apache/iceberg/geospatial/BoundingBox.java:
##########
@@ -0,0 +1,135 @@
+/*
+ * 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.iceberg.geospatial;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.Objects;
+import org.apache.iceberg.relocated.com.google.common.base.MoreObjects;
+
+/**
+ * Represents a geospatial bounding box composed of minimum and maximum bounds.
+ *
+ * <p>A bounding box (also called a Minimum Bounding Rectangle or MBR) is 
defined by two points: the
+ * minimum and maximum coordinates that define the box's corners. This 
provides a simple
+ * approximation of a more complex geometry for efficient filtering and data 
skipping.
+ */
+public class BoundingBox {
+  /**
+   * Create a {@link BoundingBox} object from buffers containing min and max 
bounds
+   *
+   * @param min the serialized minimum bound
+   * @param max the serialized maximum bound
+   * @return a BoundingBox instance
+   */
+  public static BoundingBox fromByteBuffers(ByteBuffer min, ByteBuffer max) {
+    return new BoundingBox(
+        GeospatialBound.fromByteBuffer(min), 
GeospatialBound.fromByteBuffer(max));
+  }
+
+  /**
+   * Deserialize a byte buffer as a {@link BoundingBox} object
+   *
+   * @param buffer the serialized bounding box
+   * @return a BoundingBox instance
+   */
+  public static BoundingBox fromByteBuffer(ByteBuffer buffer) {
+    buffer.order(ByteOrder.LITTLE_ENDIAN);
+
+    int minLen = buffer.getInt();
+    ByteBuffer min = buffer.slice();
+    min.limit(minLen);
+    buffer.position(buffer.position() + minLen);
+
+    int maxLen = buffer.getInt();
+    ByteBuffer max = buffer.slice();
+    max.limit(maxLen);
+
+    return fromByteBuffers(min, max);
+  }
+
+  public BoundingBox(GeospatialBound min, GeospatialBound max) {
+    this.min = min;
+    this.max = max;
+  }
+
+  private final GeospatialBound min;
+  private final GeospatialBound max;
+
+  /**
+   * Get the minimum corner of the bounding box.
+   *
+   * @return the minimum bound
+   */
+  public GeospatialBound min() {
+    return min;
+  }
+
+  /**
+   * Get the maximum corner of the bounding box.
+   *
+   * @return the maximum bound
+   */
+  public GeospatialBound max() {
+    return max;
+  }
+
+  /**
+   * Serializes this bounding box to a byte buffer. The serialized byte buffer 
could be deserialized
+   * using {@link #fromByteBuffer(ByteBuffer)}.
+   *
+   * @return a byte buffer containing the serialized bounding box
+   */
+  public ByteBuffer toByteBuffer() {

Review Comment:
   I have moved `toByteBuffer` next to `fromByteBuffer` but before the 
constructor. It is more readable, although Iceberg code style requires that 
only static members can be put before constructors.



##########
api/src/main/java/org/apache/iceberg/geospatial/GeospatialBound.java:
##########
@@ -0,0 +1,313 @@
+/*
+ * 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.iceberg.geospatial;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.Objects;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+
+/**
+ * Represents a geospatial bound (minimum or maximum) for Iceberg tables.
+ *
+ * <p>According to the <a 
href="https://iceberg.apache.org/spec/#bound-serialization";>Bound
+ * serialization section of Iceberg Table spec</a>, geospatial bounds are 
serialized differently
+ * from the regular WKB representation. Geometry and geography bounds are 
single point encoded as a
+ * concatenation of 8-byte little-endian IEEE 754 coordinate values in the 
order X, Y, Z (optional),
+ * M (optional).
+ *
+ * <p>The encoding varies based on which coordinates are present:
+ *
+ * <ul>
+ *   <li>x:y (2 doubles) when both z and m are unset
+ *   <li>x:y:z (3 doubles) when only m is unset
+ *   <li>x:y:NaN:m (4 doubles) when only z is unset
+ *   <li>x:y:z:m (4 doubles) when all coordinates are set
+ * </ul>
+ *
+ * <p>This class represents a lower or upper geospatial bound and handles 
serialization and
+ * deserialization of these bounds to/from byte arrays, conforming to the 
Iceberg specification.
+ */
+public class GeospatialBound {
+  /**
+   * Parses a geospatial bound from a byte buffer according to Iceberg spec.
+   *
+   * <p>Based on the buffer size, this method determines which coordinates are 
present: - 16 bytes
+   * (2 doubles): x and y only - 24 bytes (3 doubles): x, y, and z - 32 bytes 
(4 doubles): x, y, z
+   * (might be NaN), and m
+   *
+   * <p>The ordinates are encoded as 8-byte little-endian IEEE 754 values.
+   *
+   * @param buffer the ByteBuffer containing the serialized geospatial bound
+   * @return a GeospatialBound object representing the parsed bound
+   * @throws IllegalArgumentException if the buffer has an invalid size
+   */
+  public static GeospatialBound fromByteBuffer(ByteBuffer buffer) {
+    // Save original position and byte order to restore them later
+    buffer.order(ByteOrder.LITTLE_ENDIAN);
+    int size = buffer.remaining();
+    Preconditions.checkArgument(
+        size == 2 * Double.BYTES || size == 3 * Double.BYTES || size == 4 * 
Double.BYTES,
+        "Invalid geo spatial bound buffer size: %s. Valid sizes are 16, 24, or 
32 bytes.",
+        size);
+
+    if (size == 2 * Double.BYTES) {
+      // x:y format (2 doubles)
+      double coordX = buffer.getDouble();
+      double coordY = buffer.getDouble();
+      return createXY(coordX, coordY);
+    } else if (size == 3 * Double.BYTES) {
+      // x:y:z format (3 doubles)
+      double coordX = buffer.getDouble();
+      double coordY = buffer.getDouble();
+      double coordZ = buffer.getDouble();
+      return createXYZ(coordX, coordY, coordZ);
+    } else {
+      // x:y:z:m format (4 doubles) - z might be NaN
+      double coordX = buffer.getDouble();
+      double coordY = buffer.getDouble();
+      double coordZ = buffer.getDouble();
+      double coordM = buffer.getDouble();
+      return new GeospatialBound(coordX, coordY, coordZ, coordM);
+    }
+  }
+
+  /**
+   * Parses a geospatial bound from a byte array according to Iceberg spec.
+   *
+   * @param bytes the byte array containing the serialized geospatial bound
+   * @return a GeospatialBound object representing the parsed bound
+   * @throws IllegalArgumentException if the byte array has an invalid length
+   */
+  public static GeospatialBound fromByteArray(byte[] bytes) {
+    return fromByteBuffer(ByteBuffer.wrap(bytes));
+  }
+
+  /**
+   * Creates a GeospatialBound with X and Y coordinates only.
+   *
+   * @param x the X coordinate (longitude/easting)
+   * @param y the Y coordinate (latitude/northing)
+   * @return a GeospatialBound with XY coordinates
+   */
+  @SuppressWarnings("ParameterName")
+  public static GeospatialBound createXY(double x, double y) {
+    return new GeospatialBound(x, y, Double.NaN, Double.NaN);
+  }
+
+  /**
+   * Creates a GeospatialBound with X, Y, and Z coordinates, with no M value.
+   *
+   * @param x the X coordinate (longitude/easting)
+   * @param y the Y coordinate (latitude/northing)
+   * @param z the Z coordinate (elevation)
+   * @return a GeospatialBound with XYZ coordinates
+   */
+  @SuppressWarnings("ParameterName")
+  public static GeospatialBound createXYZ(double x, double y, double z) {
+    return new GeospatialBound(x, y, z, Double.NaN);
+  }
+
+  /**
+   * Creates a GeospatialBound with X, Y, Z, and M coordinates.
+   *
+   * @param x the X coordinate (longitude/easting)
+   * @param y the Y coordinate (latitude/northing)
+   * @param z the Z coordinate (elevation)
+   * @param m the M value (measure)
+   * @return a GeospatialBound with XYZM coordinates
+   */
+  @SuppressWarnings("ParameterName")
+  public static GeospatialBound createXYZM(double x, double y, double z, 
double m) {
+    return new GeospatialBound(x, y, z, m);
+  }
+
+  /**
+   * Creates a GeospatialBound with X, Y, and M values, with no Z coordinate.
+   *
+   * @param x the X coordinate (longitude/easting)
+   * @param y the Y coordinate (latitude/northing)
+   * @param m the M value (measure)
+   * @return a GeospatialBound with XYM coordinates
+   */
+  @SuppressWarnings("ParameterName")
+  public static GeospatialBound createXYM(double x, double y, double m) {
+    return new GeospatialBound(x, y, Double.NaN, m);
+  }
+
+  @SuppressWarnings("MemberName")
+  private final double x;
+
+  @SuppressWarnings("MemberName")
+  private final double y;
+
+  @SuppressWarnings("MemberName")
+  private final double z;
+
+  @SuppressWarnings("MemberName")
+  private final double m;
+
+  /** Private constructor - use factory methods instead. */
+  @SuppressWarnings("ParameterName")
+  private GeospatialBound(double x, double y, double z, double m) {
+    this.x = x;
+    this.y = y;
+    this.z = z;
+    this.m = m;
+  }
+
+  /**
+   * Get the X coordinate (longitude/easting).
+   *
+   * @return X coordinate value
+   */
+  @SuppressWarnings("MethodName")
+  public double x() {
+    return x;
+  }
+
+  /**
+   * Get the Y coordinate (latitude/northing).
+   *
+   * @return Y coordinate value
+   */
+  @SuppressWarnings("MethodName")
+  public double y() {
+    return y;
+  }
+
+  /**
+   * Get the Z coordinate (typically elevation).
+   *
+   * @return Z coordinate value or NaN if not set
+   */
+  @SuppressWarnings("MethodName")
+  public double z() {
+    return z;
+  }
+
+  /**
+   * Get the M value (measure).
+   *
+   * @return M value or NaN if not set
+   */
+  @SuppressWarnings("MethodName")
+  public double m() {
+    return m;
+  }
+
+  /**
+   * Check if this bound has a defined Z coordinate.
+   *
+   * @return true if Z is not NaN
+   */
+  public boolean hasZ() {
+    return !Double.isNaN(z);
+  }
+
+  /**
+   * Check if this bound has a defined M value.
+   *
+   * @return true if M is not NaN
+   */
+  public boolean hasM() {
+    return !Double.isNaN(m);
+  }
+
+  /**
+   * Serializes this geospatial bound to a byte buffer according to Iceberg 
spec.
+   *
+   * <p>Following the Iceberg spec, the bound is serialized based on which 
coordinates are set: -
+   * x:y (2 doubles) when both z and m are unset - x:y:z (3 doubles) when only 
m is unset -
+   * x:y:NaN:m (4 doubles) when only z is unset - x:y:z:m (4 doubles) when all 
coordinates are set
+   *
+   * @return A ByteBuffer containing the serialized geospatial bound
+   */
+  public ByteBuffer toByteBuffer() {

Review Comment:
   I have moved `toByteBuffer` next to `fromByteBuffer` but before the 
constructor. It is more readable, although Iceberg code style requires that 
only static members can be put before constructors.



##########
api/src/test/java/org/apache/iceberg/geospatial/TestGeospatialBound.java:
##########
@@ -0,0 +1,228 @@
+/*
+ * 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.iceberg.geospatial;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.nio.ByteBuffer;
+import org.apache.iceberg.util.ByteBuffers;
+import org.junit.jupiter.api.Test;
+
+public class TestGeospatialBound {
+
+  @Test
+  public void testCreateXY() {
+    GeospatialBound bound = GeospatialBound.createXY(1.0, 2.0);
+    assertThat(bound.x()).isEqualTo(1.0);
+    assertThat(bound.y()).isEqualTo(2.0);
+    assertThat(bound.hasZ()).isFalse();
+    assertThat(bound.hasM()).isFalse();
+    assertThat(Double.isNaN(bound.z())).isTrue();
+    assertThat(Double.isNaN(bound.m())).isTrue();
+  }
+
+  @Test
+  public void testCreateXYZ() {
+    GeospatialBound bound = GeospatialBound.createXYZ(1.0, 2.0, 3.0);
+    assertThat(bound.x()).isEqualTo(1.0);
+    assertThat(bound.y()).isEqualTo(2.0);
+    assertThat(bound.z()).isEqualTo(3.0);
+    assertThat(bound.hasZ()).isTrue();
+    assertThat(bound.hasM()).isFalse();
+    assertThat(Double.isNaN(bound.m())).isTrue();
+  }
+
+  @Test
+  public void testCreateXYM() {
+    GeospatialBound bound = GeospatialBound.createXYM(1.0, 2.0, 4.0);
+    assertThat(bound.x()).isEqualTo(1.0);
+    assertThat(bound.y()).isEqualTo(2.0);
+    assertThat(bound.m()).isEqualTo(4.0);
+    assertThat(bound.hasZ()).isFalse();
+    assertThat(bound.hasM()).isTrue();
+    assertThat(Double.isNaN(bound.z())).isTrue();
+  }
+
+  @Test
+  public void testCreateXYZM() {
+    GeospatialBound bound = GeospatialBound.createXYZM(1.0, 2.0, 3.0, 4.0);
+    assertThat(bound.x()).isEqualTo(1.0);
+    assertThat(bound.y()).isEqualTo(2.0);
+    assertThat(bound.z()).isEqualTo(3.0);
+    assertThat(bound.m()).isEqualTo(4.0);
+    assertThat(bound.hasZ()).isTrue();
+    assertThat(bound.hasM()).isTrue();
+  }
+
+  @Test
+  public void testEqualsAndHashCode() {

Review Comment:
   I still prefer the current approach, where each test method focuses on a 
specific functionality. It is easier to add new tests or change existing tests 
for specific functionality.



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