Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 39 additions & 10 deletions api/src/main/java/org/apache/iceberg/types/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,33 @@ public int hashCode() {
}
}

/**
* Returns true when two CRS identifiers should be treated as the same geospatial reference for
* type equality. Matching is case-insensitive. In addition, {@code OGC:CRS84} and {@code
* EPSG:4326} are treated as equivalent: Parquet writers that emit the WGS84 lon/lat default as
* {@code OGC:CRS84} must be compatible with Iceberg tables declared as {@code EPSG:4326}.
*
* <p>{@code srid:4326} is <em>not</em> included - that is a different identifier scheme.
*/
static boolean crsEquivalent(String left, String right) {
if (left.equalsIgnoreCase(right)) {
return true;
}
return isWgs84LonLatCrs(left) && isWgs84LonLatCrs(right);
}

private static boolean isWgs84LonLatCrs(String crs) {
return "OGC:CRS84".equalsIgnoreCase(crs) || "EPSG:4326".equalsIgnoreCase(crs);
}

/** Canonical upper-case key for CRS hashing, aligned with {@link #crsEquivalent}. */
static String crsHashKey(String crs) {
if (isWgs84LonLatCrs(crs)) {
return "OGC:CRS84";
}
return crs.toUpperCase(Locale.ROOT);
}

/** A geometry type, optionally parameterized by a CRS. The default CRS is {@code OGC:CRS84}. */
public static class GeometryType extends PrimitiveType {
public static final String DEFAULT_CRS = "OGC:CRS84";
Expand Down Expand Up @@ -606,8 +633,9 @@ public String crs() {
}

/**
* Two geometry types are equal when their CRS match case-insensitively, so {@code OGC:CRS84}
* and {@code ogc:crs84} are equal.
* Two geometry types are equal when their CRS are equivalent: case-insensitive match, or the
* WGS84 lon/lat pair {@code OGC:CRS84} / {@code EPSG:4326} (see {@link
* Types#crsEquivalent(String, String)}).
*/
@Override
public boolean equals(Object o) {
Expand All @@ -618,13 +646,13 @@ public boolean equals(Object o) {
}

GeometryType that = (GeometryType) o;
return crs.equalsIgnoreCase(that.crs);
return crsEquivalent(crs, that.crs);
}

@Override
public int hashCode() {
// hash the upper-cased CRS so it stays consistent with the case-insensitive equals
return Objects.hash(GeometryType.class, crs.toUpperCase(Locale.ROOT));
// hash via crsHashKey so it stays consistent with crsEquivalent
return Objects.hash(GeometryType.class, crsHashKey(crs));
}

@Override
Expand Down Expand Up @@ -683,8 +711,9 @@ public EdgeAlgorithm algorithm() {
}

/**
* Two geography types are equal when their edge algorithms are equal and their CRS match
* case-insensitively, so {@code OGC:CRS84} and {@code ogc:crs84} are equal.
* Two geography types are equal when their edge algorithms are equal and their CRS are
* equivalent: case-insensitive match, or the WGS84 lon/lat pair {@code OGC:CRS84} / {@code
* EPSG:4326} (see {@link Types#crsEquivalent(String, String)}).
*/
@Override
public boolean equals(Object o) {
Expand All @@ -695,13 +724,13 @@ public boolean equals(Object o) {
}

GeographyType that = (GeographyType) o;
return crs.equalsIgnoreCase(that.crs) && Objects.equals(algorithm, that.algorithm);
return crsEquivalent(crs, that.crs) && Objects.equals(algorithm, that.algorithm);
}

@Override
public int hashCode() {
// hash the upper-cased CRS so it stays consistent with the case-insensitive equals
return Objects.hash(GeographyType.class, crs.toUpperCase(Locale.ROOT), algorithm);
// hash via crsHashKey so it stays consistent with crsEquivalent
return Objects.hash(GeographyType.class, crsHashKey(crs), algorithm);
}

@Override
Expand Down
21 changes: 21 additions & 0 deletions api/src/test/java/org/apache/iceberg/types/TestTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,27 @@ public void testGeospatialTypeDefaultNormalization() {
assertThat(Types.GeographyType.of("ogc:crs84"))
.isEqualTo(Types.GeographyType.crs84())
.hasSameHashCodeAs(Types.GeographyType.crs84());

// OGC:CRS84 and EPSG:4326 are interchangeable WGS84 lon/lat identifiers (see #18680).
// Parquet writers that emit the default as OGC:CRS84 must match Iceberg EPSG:4326 tables.
assertThat(Types.GeometryType.of("EPSG:4326"))
.isEqualTo(Types.GeometryType.crs84())
.hasSameHashCodeAs(Types.GeometryType.crs84());
assertThat(Types.GeometryType.of("epsg:4326"))
.isEqualTo(Types.GeometryType.of("OGC:CRS84"))
.hasSameHashCodeAs(Types.GeometryType.of("ogc:crs84"));
assertThat(Types.GeographyType.of("EPSG:4326"))
.isEqualTo(Types.GeographyType.crs84())
.hasSameHashCodeAs(Types.GeographyType.crs84());
assertThat(Types.GeographyType.of("EPSG:4326", EdgeAlgorithm.KARNEY))
.isEqualTo(Types.GeographyType.of("OGC:CRS84", EdgeAlgorithm.KARNEY))
.hasSameHashCodeAs(Types.GeographyType.of("ogc:crs84", EdgeAlgorithm.KARNEY));
// Different algorithm still differs even when CRS identifiers are equivalent
assertThat(Types.GeographyType.of("EPSG:4326", EdgeAlgorithm.KARNEY))
.isNotEqualTo(Types.GeographyType.crs84());
// srid:4326 is a different identifier scheme and must not be equated
assertThat(Types.GeometryType.of("srid:4326")).isNotEqualTo(Types.GeometryType.crs84());
assertThat(Types.GeometryType.of("srid:4326")).isNotEqualTo(Types.GeometryType.of("EPSG:4326"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -865,13 +865,36 @@ public void rejectsMapValueGeometryCrsMismatchWithoutIds() {
}

@Test
public void rejectsGeographyCrsMismatch() {
public void acceptsEquivalentWgs84CrsIdentifiers() {
MessageType fileSchema =
Types.buildMessage()
.optional(PrimitiveTypeName.BINARY)
.as(LogicalTypeAnnotation.geometryType("OGC:CRS84"))
.id(1)
.named("geom")
.optional(PrimitiveTypeName.BINARY)
.as(
LogicalTypeAnnotation.geographyType(
"EPSG:4326", EdgeInterpolationAlgorithm.SPHERICAL))
.id(2)
.named("geog")
.named("table");
Schema projection =
new Schema(
NestedField.optional(1, "geom", GeometryType.of("EPSG:4326")),
NestedField.optional(2, "geog", GeographyType.crs84()));

assertThat(ParquetSchemaUtil.pruneColumns(fileSchema, projection)).isEqualTo(fileSchema);
}

@Test
public void rejectsGeographyCrsMismatch() {
MessageType fileSchema =
Types.buildMessage()
.optional(PrimitiveTypeName.BINARY)
.as(
LogicalTypeAnnotation.geographyType(
"EPSG:4269", EdgeInterpolationAlgorithm.SPHERICAL))
.id(1)
.named("geog")
.named("table");
Expand Down
Loading