This is an automated email from the ASF dual-hosted git repository.
zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-go.git
The following commit(s) were added to refs/heads/main by this push:
new 58f95650f fix(types): keep an explicit default CRS and algorithm when
writing geometry and geography (#2667)
58f95650f is described below
commit 58f95650fd83d3af536327d955db3014d6a9bf85
Author: Andrei Tserakhau <[email protected]>
AuthorDate: Mon Nov 23 19:59:42 2026 +0100
fix(types): keep an explicit default CRS and algorithm when writing
geometry and geography (#2667)
* fix(types): keep an explicit default CRS and algorithm when writing
geometry and geography
Parsing a type string went through GeometryTypeOf and GeographyTypeOf,
which drop OGC:CRS84 and spherical. A schema that said
geometry(OGC:CRS84) or geography(OGC:CRS84, spherical) was written back
as geometry and geography.
The type-string parser now still validates through those constructors
but stores the CRS and algorithm as written. Bare geometry and geography
are unchanged, and the constructors keep normalizing, so types built
from Arrow schemas are written as before. Equals compares CRS() and
Algorithm(), so an explicit default and an implied one are still the
same type.
This makes the geometry-crs84 and geography-crs84-spherical cases in
iceberg-verification's table-spec/types/geospatial/cases.json pass.
Signed-off-by: laskoviymishka <[email protected]>
* docs(types): note to compare geo types through CRS() and Algorithm()
The parser can now keep an explicit default CRS or algorithm, so the raw
fields no longer imply the default when empty.
Signed-off-by: laskoviymishka <[email protected]>
---------
Signed-off-by: laskoviymishka <[email protected]>
---
types.go | 28 ++++++++++++++++++++-------
types_test.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 82 insertions(+), 7 deletions(-)
diff --git a/types.go b/types.go
index 4ecc1a35f..5440fa40c 100644
--- a/types.go
+++ b/types.go
@@ -236,11 +236,12 @@ func (t *typeIFace) UnmarshalJSON(b []byte) error {
}
if matches[1] != "" {
- geom, err :=
GeometryTypeOf(strings.TrimSpace(matches[1]))
- if err != nil {
+ crs := strings.TrimSpace(matches[1])
+ if _, err := GeometryTypeOf(crs); err
!= nil {
return err
}
- t.Type = geom
+ // keep an explicit default CRS so it
is written back as read
+ t.Type = GeometryType{crs: crs}
} else {
t.Type = GeometryType{}
}
@@ -260,10 +261,18 @@ func (t *typeIFace) UnmarshalJSON(b []byte) error {
algorithm =
strings.TrimSpace(matches[2])
}
- geog, err := GeographyTypeOf(crs, algorithm)
- if err != nil {
+ if _, err := GeographyTypeOf(crs, algorithm);
err != nil {
return err
}
+
+ // keep an explicit default CRS or algorithm so
it is written back as read
+ var geog GeographyType
+ if matches[1] != "" {
+ geog.crs = crs
+ }
+ if matches[2] != "" {
+ geog.algorithm =
strings.ToLower(algorithm)
+ }
t.Type = geog
default:
return fmt.Errorf("%w: unrecognized field
type", ErrInvalidSchema)
@@ -1116,6 +1125,8 @@ func (VariantType) String() string { return "variant" }
// same value.
const DefaultGeoCRS = "OGC:CRS84"
+// GeometryType is the geometry type. The CRS may be held explicitly even when
+// it is the default, so compare values through CRS() rather than the field.
type GeometryType struct {
crs string
}
@@ -1146,7 +1157,7 @@ func (g GeometryType) Equals(other Type) bool {
return false
}
- return g.crs == rhs.crs
+ return g.CRS() == rhs.CRS()
}
func (GeometryType) primitive() {}
@@ -1175,6 +1186,9 @@ func toGeoArrowEdgeInterpolation(s string)
(geoarrow.EdgeInterpolation, error) {
}
}
+// GeographyType is the geography type. The CRS and algorithm may be held
+// explicitly even when they are the defaults, so compare values through CRS()
+// and Algorithm() rather than the fields.
type GeographyType struct {
crs string
algorithm string
@@ -1227,7 +1241,7 @@ func (g GeographyType) Equals(other Type) bool {
return false
}
- return g.crs == rhs.crs && g.algorithm == rhs.algorithm
+ return g.CRS() == rhs.CRS() && g.Algorithm() == rhs.Algorithm()
}
func (GeographyType) primitive() {}
diff --git a/types_test.go b/types_test.go
index b9988d93f..896eea9c9 100644
--- a/types_test.go
+++ b/types_test.go
@@ -1054,6 +1054,21 @@ func TestGeometryType(t *testing.T) {
assert.JSONEq(t, data, string(out))
})
+ t.Run("JSON parsing - explicit default CRS", func(t *testing.T) {
+ data := `{"id": 1, "name": "location", "type":
"geometry(OGC:CRS84)", "required": false}`
+ var n iceberg.NestedField
+ require.NoError(t, json.Unmarshal([]byte(data), &n))
+ geom, ok := n.Type.(iceberg.GeometryType)
+ require.True(t, ok)
+ assert.Equal(t, "OGC:CRS84", geom.CRS())
+ assert.True(t, geom.Equals(iceberg.GeometryType{}))
+ assert.True(t, iceberg.GeometryType{}.Equals(geom))
+
+ out, err := json.Marshal(n)
+ require.NoError(t, err)
+ assert.JSONEq(t, data, string(out))
+ })
+
t.Run("JSON parsing - case insensitive", func(t *testing.T) {
data := `{"id": 1, "name": "location", "type":
"Geometry(SRID:4326)", "required": false}`
var n iceberg.NestedField
@@ -1190,6 +1205,52 @@ func TestGeographyType(t *testing.T) {
assert.JSONEq(t, data, string(out))
})
+ t.Run("JSON parsing - explicit default CRS and algorithm", func(t
*testing.T) {
+ data := `{"id": 1, "name": "area", "type":
"geography(OGC:CRS84, spherical)", "required": false}`
+ var n iceberg.NestedField
+ require.NoError(t, json.Unmarshal([]byte(data), &n))
+ geog, ok := n.Type.(iceberg.GeographyType)
+ require.True(t, ok)
+ assert.Equal(t, "OGC:CRS84", geog.CRS())
+ assert.Equal(t, "spherical", geog.Algorithm())
+ assert.True(t, geog.Equals(iceberg.GeographyType{}))
+ assert.True(t, iceberg.GeographyType{}.Equals(geog))
+
+ out, err := json.Marshal(n)
+ require.NoError(t, err)
+ assert.JSONEq(t, data, string(out))
+ })
+
+ t.Run("JSON parsing - explicit parameters written back as read", func(t
*testing.T) {
+ srid4269, err := iceberg.GeographyTypeOf("srid:4269",
"spherical")
+ require.NoError(t, err)
+
+ tests := []struct {
+ typeStr string
+ expected string
+ equals iceberg.GeographyType
+ }{
+ {"geography(OGC:CRS84)", "geography(OGC:CRS84)",
iceberg.GeographyType{}},
+ {"geography(srid:4269, spherical)",
"geography(srid:4269, spherical)", srid4269},
+ {"Geography(OGC:CRS84, SPHERICAL)",
"geography(OGC:CRS84, spherical)", iceberg.GeographyType{}},
+ }
+ for _, tt := range tests {
+ t.Run(tt.typeStr, func(t *testing.T) {
+ data := `{"id": 1, "name": "area", "type": "` +
tt.typeStr + `", "required": false}`
+ var n iceberg.NestedField
+ require.NoError(t, json.Unmarshal([]byte(data),
&n))
+ geog, ok := n.Type.(iceberg.GeographyType)
+ require.True(t, ok)
+ assert.True(t, geog.Equals(tt.equals))
+ assert.True(t, tt.equals.Equals(geog))
+
+ out, err := json.Marshal(n)
+ require.NoError(t, err)
+ assert.JSONEq(t, `{"id": 1, "name": "area",
"type": "`+tt.expected+`", "required": false}`, string(out))
+ })
+ }
+ })
+
t.Run("JSON parsing - case insensitive", func(t *testing.T) {
data := `{"id": 1, "name": "area", "type":
"Geography(SRID:4269, KARNEY)", "required": false}`
var n iceberg.NestedField