You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iceberg.apache.org by sz...@apache.org on 2026/09/01 00:35:15 UTC

(iceberg) branch main updated: Core: Fix geo bounds in v4 legacy stat maps (#17493)

This is an automated email from the ASF dual-hosted git repository.

szehon-ho pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg.git


The following commit(s) were added to refs/heads/main by this push:
     new 17680ce149 Core: Fix geo bounds in v4 legacy stat maps (#17493)
17680ce149 is described below

commit 17680ce149981d3186537a603a26015f634671f9
Author: vishnu prakash <nv...@gmail.com>
AuthorDate: Tue Sep 1 06:05:07 2026 +0530

    Core: Fix geo bounds in v4 legacy stat maps (#17493)
---
 .../org/apache/iceberg/ContentStatsBackedMap.java  |  35 +++++-
 .../apache/iceberg/TestContentStatsBackedMap.java  | 127 +++++++++++++++++++++
 2 files changed, 161 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/apache/iceberg/ContentStatsBackedMap.java b/core/src/main/java/org/apache/iceberg/ContentStatsBackedMap.java
index aa827df529..c8a1c21e85 100644
--- a/core/src/main/java/org/apache/iceberg/ContentStatsBackedMap.java
+++ b/core/src/main/java/org/apache/iceberg/ContentStatsBackedMap.java
@@ -22,6 +22,7 @@ import java.nio.ByteBuffer;
 import java.util.AbstractMap;
 import java.util.Map;
 import java.util.Set;
+import org.apache.iceberg.geospatial.GeospatialBound;
 import org.apache.iceberg.relocated.com.google.common.collect.Sets;
 import org.apache.iceberg.types.Conversions;
 import org.apache.iceberg.types.Type;
@@ -173,7 +174,39 @@ class ContentStatsBackedMap<V> extends AbstractMap<Integer, V> {
 
   private static ByteBuffer bound(FieldStats<?> fieldStats, Object bound, String boundFieldName) {
     Type boundType = fieldStats.type().fieldType(boundFieldName);
+    if (boundType == null) {
+      return null;
+    }
+
+    if (boundType.isStructType()) {
+      // geo bounds are bounding-box structs; convert to the spec's single-point encoding
+      return geoBound((StructLike) bound);
+    }
+
     // toByteBuffer returns null for a null bound
-    return boundType == null ? null : Conversions.toByteBuffer(boundType, bound);
+    return Conversions.toByteBuffer(boundType, bound);
+  }
+
+  /** Encodes a geo bounding-box struct (x, y, z, m) as the spec's single-point bound. */
+  private static ByteBuffer geoBound(StructLike bound) {
+    if (bound == null) {
+      return null;
+    }
+
+    // field order is fixed by the stats schema: x, y, z, m
+    double coordX = bound.get(0, Double.class);
+    double coordY = bound.get(1, Double.class);
+    Double coordZ = bound.get(2, Double.class);
+    Double coordM = bound.get(3, Double.class);
+
+    if (coordZ != null && coordM != null) {
+      return GeospatialBound.createXYZM(coordX, coordY, coordZ, coordM).toByteBuffer();
+    } else if (coordZ != null) {
+      return GeospatialBound.createXYZ(coordX, coordY, coordZ).toByteBuffer();
+    } else if (coordM != null) {
+      return GeospatialBound.createXYM(coordX, coordY, coordM).toByteBuffer();
+    } else {
+      return GeospatialBound.createXY(coordX, coordY).toByteBuffer();
+    }
   }
 }
diff --git a/core/src/test/java/org/apache/iceberg/TestContentStatsBackedMap.java b/core/src/test/java/org/apache/iceberg/TestContentStatsBackedMap.java
index 31e6eb55d1..cd9a3c4387 100644
--- a/core/src/test/java/org/apache/iceberg/TestContentStatsBackedMap.java
+++ b/core/src/test/java/org/apache/iceberg/TestContentStatsBackedMap.java
@@ -25,6 +25,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 import java.nio.ByteBuffer;
 import java.util.List;
 import java.util.Map;
+import org.apache.iceberg.geospatial.GeospatialBound;
 import org.apache.iceberg.types.Conversions;
 import org.apache.iceberg.types.Types;
 import org.junit.jupiter.api.Test;
@@ -111,6 +112,132 @@ public class TestContentStatsBackedMap {
             Map.entry(3, Conversions.toByteBuffer(Types.DoubleType.get(), 9.0)));
   }
 
+  @Test
+  public void testGeoBoundsUseSinglePointEncoding() {
+    Schema geoSchema =
+        new Schema(
+            required(1, "id", Types.LongType.get()),
+            optional(10, "geom", Types.GeometryType.crs84()),
+            optional(11, "geog", Types.GeographyType.crs84()));
+    Types.StructType statsType = StatsUtil.statsReadSchema(geoSchema, List.of(1, 10, 11));
+
+    ContentStatsStruct stats = new ContentStatsStruct(statsType);
+    stats.setStats(
+        1,
+        StatsTestUtil.mockFieldStats(
+            statsType.field("id").type().asStructType(), 1, 1L, 5L, 26L, null, null));
+    stats.setStats(
+        10,
+        StatsTestUtil.mockFieldStats(
+            statsType.field("geom").type().asStructType(),
+            10,
+            TestHelpers.Row.of(1.0, 2.0, null, null),
+            TestHelpers.Row.of(5.0, 6.0, null, null),
+            26L,
+            2L,
+            null));
+    stats.setStats(
+        11,
+        StatsTestUtil.mockFieldStats(
+            statsType.field("geog").type().asStructType(),
+            11,
+            TestHelpers.Row.of(-1.0, -2.0, 3.0, 4.0),
+            TestHelpers.Row.of(7.0, 8.0, 9.0, 10.0),
+            26L,
+            0L,
+            null));
+
+    // geometry and geography bounds are stored as bounding-box structs (x, y, z, m) but must be
+    // presented in the legacy maps using the spec's single-point encoding
+    Map<Integer, ByteBuffer> lower = ContentStatsBackedMap.lowerBounds(stats);
+    assertThat(lower)
+        .containsOnly(
+            Map.entry(1, Conversions.toByteBuffer(Types.LongType.get(), 1L)),
+            Map.entry(10, GeospatialBound.createXY(1.0, 2.0).toByteBuffer()),
+            Map.entry(11, GeospatialBound.createXYZM(-1.0, -2.0, 3.0, 4.0).toByteBuffer()));
+
+    Map<Integer, ByteBuffer> upper = ContentStatsBackedMap.upperBounds(stats);
+    assertThat(upper)
+        .containsOnly(
+            Map.entry(1, Conversions.toByteBuffer(Types.LongType.get(), 5L)),
+            Map.entry(10, GeospatialBound.createXY(5.0, 6.0).toByteBuffer()),
+            Map.entry(11, GeospatialBound.createXYZM(7.0, 8.0, 9.0, 10.0).toByteBuffer()));
+
+    // the encoding must round-trip through the geo conversion used by legacy readers
+    GeospatialBound geomLower =
+        Conversions.fromByteBuffer(Types.GeometryType.crs84(), lower.get(10));
+    assertThat(geomLower).isEqualTo(GeospatialBound.createXY(1.0, 2.0));
+    GeospatialBound geogUpper =
+        Conversions.fromByteBuffer(Types.GeographyType.crs84(), upper.get(11));
+    assertThat(geogUpper).isEqualTo(GeospatialBound.createXYZM(7.0, 8.0, 9.0, 10.0));
+  }
+
+  @Test
+  public void testGeoBoundWithZOnlyAndMOnly() {
+    Schema geoSchema = new Schema(optional(10, "geom", Types.GeometryType.crs84()));
+    Types.StructType statsType = StatsUtil.statsReadSchema(geoSchema, List.of(10));
+
+    ContentStatsStruct stats = new ContentStatsStruct(statsType);
+    stats.setStats(
+        10,
+        StatsTestUtil.mockFieldStats(
+            statsType.field("geom").type().asStructType(),
+            10,
+            TestHelpers.Row.of(1.0, 2.0, 3.0, null),
+            TestHelpers.Row.of(5.0, 6.0, null, 7.0),
+            26L,
+            2L,
+            null));
+
+    Map<Integer, ByteBuffer> lower = ContentStatsBackedMap.lowerBounds(stats);
+    assertThat(lower.get(10)).isEqualTo(GeospatialBound.createXYZ(1.0, 2.0, 3.0).toByteBuffer());
+
+    Map<Integer, ByteBuffer> upper = ContentStatsBackedMap.upperBounds(stats);
+    assertThat(upper.get(10)).isEqualTo(GeospatialBound.createXYM(5.0, 6.0, 7.0).toByteBuffer());
+
+    // the encoding must round-trip through the geo conversion used by legacy readers
+    GeospatialBound geomLower =
+        Conversions.fromByteBuffer(Types.GeometryType.crs84(), lower.get(10));
+    assertThat(geomLower).isEqualTo(GeospatialBound.createXYZ(1.0, 2.0, 3.0));
+    GeospatialBound geomUpper =
+        Conversions.fromByteBuffer(Types.GeometryType.crs84(), upper.get(10));
+    assertThat(geomUpper).isEqualTo(GeospatialBound.createXYM(5.0, 6.0, 7.0));
+  }
+
+  @Test
+  public void testGeoFieldWithoutLowerBoundIsAbsentFromView() {
+    Schema geoSchema =
+        new Schema(
+            required(1, "id", Types.LongType.get()),
+            optional(10, "geom", Types.GeometryType.crs84()));
+    Types.StructType statsType = StatsUtil.statsReadSchema(geoSchema, List.of(1, 10));
+
+    ContentStatsStruct stats = new ContentStatsStruct(statsType);
+    stats.setStats(
+        1,
+        StatsTestUtil.mockFieldStats(
+            statsType.field("id").type().asStructType(), 1, 1L, 5L, 26L, null, null));
+    stats.setStats(
+        10,
+        StatsTestUtil.mockFieldStats(
+            statsType.field("geom").type().asStructType(),
+            10,
+            null,
+            TestHelpers.Row.of(5.0, 6.0, null, null),
+            26L,
+            2L,
+            null));
+
+    Map<Integer, ByteBuffer> lower = ContentStatsBackedMap.lowerBounds(stats);
+    assertThat(lower.get(10)).isNull();
+    assertThat(lower.containsKey(10)).isFalse();
+    assertThat(lower)
+        .containsOnly(Map.entry(1, Conversions.toByteBuffer(Types.LongType.get(), 1L)));
+
+    Map<Integer, ByteBuffer> upper = ContentStatsBackedMap.upperBounds(stats);
+    assertThat(upper.get(10)).isEqualTo(GeospatialBound.createXY(5.0, 6.0).toByteBuffer());
+  }
+
   @Test
   public void testGetReturnsNullForMissingKey() {
     Map<Integer, Long> map = ContentStatsBackedMap.valueCounts(POPULATED_STATS);