From fd08071f88f96260a287669b96ab667e1c5ed13b Mon Sep 17 00:00:00 2001 From: Anurag Singh Date: Wed, 25 Nov 2026 13:40:02 +0530 Subject: [PATCH 1/2] Types: Serialize geometry and geography without quoted parameters Match the canonical type strings in the Iceberg spec so metadata written by PyIceberg uses the same form as Java. Parsing still accepts the quoted forms. --- mkdocs/docs/geospatial.md | 4 ++-- pyiceberg/types.py | 16 ++++++++-------- tests/test_types.py | 17 +++++++++++------ 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/mkdocs/docs/geospatial.md b/mkdocs/docs/geospatial.md index df5b72c550..0bdfd44c27 100644 --- a/mkdocs/docs/geospatial.md +++ b/mkdocs/docs/geospatial.md @@ -67,8 +67,8 @@ NestedField(1, "point", "geometry", required=True) NestedField(2, "region", "geography", required=True) # With parameters -NestedField(3, "location", "geometry('EPSG:4326')", required=True) -NestedField(4, "boundary", "geography('EPSG:4326', 'planar')", required=True) +NestedField(3, "location", "geometry(EPSG:4326)", required=True) +NestedField(4, "boundary", "geography(EPSG:4326, planar)", required=True) ``` ## Data Representation diff --git a/pyiceberg/types.py b/pyiceberg/types.py index efb7b5bee4..799842bb46 100644 --- a/pyiceberg/types.py +++ b/pyiceberg/types.py @@ -1005,7 +1005,7 @@ class GeometryType(PrimitiveType): >>> GeometryType("EPSG:4326") GeometryType(crs='EPSG:4326') >>> str(GeometryType("EPSG:4326")) - "geometry('EPSG:4326')" + 'geometry(EPSG:4326)' """ root: str = Field(default=DEFAULT_GEOMETRY_CRS) @@ -1018,7 +1018,7 @@ def ser_model(self) -> str: """Serialize the model to a string.""" if self.crs == DEFAULT_GEOMETRY_CRS: return "geometry" - return f"geometry('{self.crs}')" + return f"geometry({self.crs})" @property def crs(self) -> str: @@ -1035,7 +1035,7 @@ def __str__(self) -> str: """Return the string representation.""" if self.crs == DEFAULT_GEOMETRY_CRS: return "geometry" - return f"geometry('{self.crs}')" + return f"geometry({self.crs})" def __hash__(self) -> int: """Return the hash of the CRS.""" @@ -1074,7 +1074,7 @@ class GeographyType(PrimitiveType): >>> GeographyType("EPSG:4326", "planar") GeographyType(crs='EPSG:4326', algorithm='planar') >>> str(GeographyType("EPSG:4326", "planar")) - "geography('EPSG:4326', 'planar')" + 'geography(EPSG:4326, planar)' """ root: tuple[str, str] = Field(default=(DEFAULT_GEOGRAPHY_CRS, DEFAULT_GEOGRAPHY_ALGORITHM)) @@ -1088,8 +1088,8 @@ def ser_model(self) -> str: if self.crs == DEFAULT_GEOGRAPHY_CRS and self.algorithm == DEFAULT_GEOGRAPHY_ALGORITHM: return "geography" if self.algorithm == DEFAULT_GEOGRAPHY_ALGORITHM: - return f"geography('{self.crs}')" - return f"geography('{self.crs}', '{self.algorithm}')" + return f"geography({self.crs})" + return f"geography({self.crs}, {self.algorithm})" @property def crs(self) -> str: @@ -1114,8 +1114,8 @@ def __str__(self) -> str: if self.crs == DEFAULT_GEOGRAPHY_CRS and self.algorithm == DEFAULT_GEOGRAPHY_ALGORITHM: return "geography" if self.algorithm == DEFAULT_GEOGRAPHY_ALGORITHM: - return f"geography('{self.crs}')" - return f"geography('{self.crs}', '{self.algorithm}')" + return f"geography({self.crs})" + return f"geography({self.crs}, {self.algorithm})" def __hash__(self) -> int: """Return the hash of the tuple.""" diff --git a/tests/test_types.py b/tests/test_types.py index 00668ebcb4..90817dc256 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -777,7 +777,7 @@ def test_geometry_type_custom_crs() -> None: """Test GeometryType with custom CRS.""" type_var = GeometryType("EPSG:4326") assert type_var.crs == "EPSG:4326" - assert str(type_var) == "geometry('EPSG:4326')" + assert str(type_var) == "geometry(EPSG:4326)" assert repr(type_var) == "GeometryType(crs='EPSG:4326')" @@ -799,7 +799,8 @@ def test_geometry_type_pickle() -> None: def test_geometry_type_serialization() -> None: """Test GeometryType JSON serialization.""" assert GeometryType().model_dump_json() == '"geometry"' - assert GeometryType("EPSG:4326").model_dump_json() == "\"geometry('EPSG:4326')\"" + assert GeometryType("EPSG:4326").model_dump_json() == '"geometry(EPSG:4326)"' + assert GeometryType.model_validate_json(GeometryType("EPSG:4326").model_dump_json()) == GeometryType("EPSG:4326") def test_geometry_type_deserialization() -> None: @@ -847,7 +848,7 @@ def test_geography_type_custom_crs() -> None: type_var = GeographyType("EPSG:4326") assert type_var.crs == "EPSG:4326" assert type_var.algorithm == DEFAULT_GEOGRAPHY_ALGORITHM - assert str(type_var) == "geography('EPSG:4326')" + assert str(type_var) == "geography(EPSG:4326)" assert repr(type_var) == "GeographyType(crs='EPSG:4326')" @@ -856,7 +857,7 @@ def test_geography_type_custom_crs_and_algorithm() -> None: type_var = GeographyType("EPSG:4326", "planar") assert type_var.crs == "EPSG:4326" assert type_var.algorithm == "planar" - assert str(type_var) == "geography('EPSG:4326', 'planar')" + assert str(type_var) == "geography(EPSG:4326, planar)" assert repr(type_var) == "GeographyType(crs='EPSG:4326', algorithm='planar')" @@ -879,8 +880,12 @@ def test_geography_type_pickle() -> None: def test_geography_type_serialization() -> None: """Test GeographyType JSON serialization.""" assert GeographyType().model_dump_json() == '"geography"' - assert GeographyType("EPSG:4326").model_dump_json() == "\"geography('EPSG:4326')\"" - assert GeographyType("EPSG:4326", "planar").model_dump_json() == "\"geography('EPSG:4326', 'planar')\"" + assert GeographyType("EPSG:4326").model_dump_json() == '"geography(EPSG:4326)"' + assert GeographyType("EPSG:4326", "planar").model_dump_json() == '"geography(EPSG:4326, planar)"' + assert GeographyType.model_validate_json(GeographyType("EPSG:4326").model_dump_json()) == GeographyType("EPSG:4326") + assert GeographyType.model_validate_json(GeographyType("EPSG:4326", "planar").model_dump_json()) == GeographyType( + "EPSG:4326", "planar" + ) def test_geography_type_deserialization() -> None: From 0b80019b450411e9fae23ffa98ca635a44632d45 Mon Sep 17 00:00:00 2001 From: Anurag Singh Date: Tue, 1 Dec 2026 23:45:01 +0530 Subject: [PATCH 2/2] Types: Always write the CRS and algorithm parameters The spec lists geometry() and geography(, ) as the canonical serialized forms, and Java always writes both parameters, including the defaults. Stop special-casing the defaults so geometry and geography serialize the same way in PyIceberg as in Java. Also replace the invalid planar geography algorithm in the docs, docstrings and tests with spec-valid values, since Java cannot load a geography type that uses it. --- mkdocs/docs/geospatial.md | 4 ++-- pyiceberg/types.py | 20 ++++------------- tests/test_types.py | 46 ++++++++++++++++++++------------------- 3 files changed, 30 insertions(+), 40 deletions(-) diff --git a/mkdocs/docs/geospatial.md b/mkdocs/docs/geospatial.md index 0bdfd44c27..3bf767a2c1 100644 --- a/mkdocs/docs/geospatial.md +++ b/mkdocs/docs/geospatial.md @@ -54,7 +54,7 @@ GeographyType() GeographyType("EPSG:4326") # Custom CRS and algorithm -GeographyType("EPSG:4326", "planar") +GeographyType("EPSG:4326", "spherical") ``` ### String Type Syntax @@ -68,7 +68,7 @@ NestedField(2, "region", "geography", required=True) # With parameters NestedField(3, "location", "geometry(EPSG:4326)", required=True) -NestedField(4, "boundary", "geography(EPSG:4326, planar)", required=True) +NestedField(4, "boundary", "geography(EPSG:4326, spherical)", required=True) ``` ## Data Representation diff --git a/pyiceberg/types.py b/pyiceberg/types.py index 799842bb46..74c1f9cc12 100644 --- a/pyiceberg/types.py +++ b/pyiceberg/types.py @@ -1016,8 +1016,6 @@ def __init__(self, crs: str = DEFAULT_GEOMETRY_CRS) -> None: @model_serializer def ser_model(self) -> str: """Serialize the model to a string.""" - if self.crs == DEFAULT_GEOMETRY_CRS: - return "geometry" return f"geometry({self.crs})" @property @@ -1033,8 +1031,6 @@ def __repr__(self) -> str: def __str__(self) -> str: """Return the string representation.""" - if self.crs == DEFAULT_GEOMETRY_CRS: - return "geometry" return f"geometry({self.crs})" def __hash__(self) -> int: @@ -1071,10 +1067,10 @@ class GeographyType(PrimitiveType): 'OGC:CRS84' >>> column_foo.algorithm 'spherical' - >>> GeographyType("EPSG:4326", "planar") - GeographyType(crs='EPSG:4326', algorithm='planar') - >>> str(GeographyType("EPSG:4326", "planar")) - 'geography(EPSG:4326, planar)' + >>> GeographyType("EPSG:4326", "spherical") + GeographyType(crs='EPSG:4326') + >>> str(GeographyType("EPSG:4326", "spherical")) + 'geography(EPSG:4326, spherical)' """ root: tuple[str, str] = Field(default=(DEFAULT_GEOGRAPHY_CRS, DEFAULT_GEOGRAPHY_ALGORITHM)) @@ -1085,10 +1081,6 @@ def __init__(self, crs: str = DEFAULT_GEOGRAPHY_CRS, algorithm: str = DEFAULT_GE @model_serializer def ser_model(self) -> str: """Serialize the model to a string.""" - if self.crs == DEFAULT_GEOGRAPHY_CRS and self.algorithm == DEFAULT_GEOGRAPHY_ALGORITHM: - return "geography" - if self.algorithm == DEFAULT_GEOGRAPHY_ALGORITHM: - return f"geography({self.crs})" return f"geography({self.crs}, {self.algorithm})" @property @@ -1111,10 +1103,6 @@ def __repr__(self) -> str: def __str__(self) -> str: """Return the string representation.""" - if self.crs == DEFAULT_GEOGRAPHY_CRS and self.algorithm == DEFAULT_GEOGRAPHY_ALGORITHM: - return "geography" - if self.algorithm == DEFAULT_GEOGRAPHY_ALGORITHM: - return f"geography({self.crs})" return f"geography({self.crs}, {self.algorithm})" def __hash__(self) -> int: diff --git a/tests/test_types.py b/tests/test_types.py index 90817dc256..f101be503f 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -767,7 +767,7 @@ def test_geometry_type_default() -> None: """Test GeometryType with default CRS.""" type_var = GeometryType() assert type_var.crs == DEFAULT_GEOMETRY_CRS - assert str(type_var) == "geometry" + assert str(type_var) == "geometry(OGC:CRS84)" assert repr(type_var) == "GeometryType()" assert type_var.minimum_format_version() == 3 assert type_var.is_primitive @@ -798,8 +798,9 @@ def test_geometry_type_pickle() -> None: def test_geometry_type_serialization() -> None: """Test GeometryType JSON serialization.""" - assert GeometryType().model_dump_json() == '"geometry"' + assert GeometryType().model_dump_json() == '"geometry(OGC:CRS84)"' assert GeometryType("EPSG:4326").model_dump_json() == '"geometry(EPSG:4326)"' + assert GeometryType.model_validate_json(GeometryType().model_dump_json()) == GeometryType() assert GeometryType.model_validate_json(GeometryType("EPSG:4326").model_dump_json()) == GeometryType("EPSG:4326") @@ -837,7 +838,7 @@ def test_geography_type_default() -> None: type_var = GeographyType() assert type_var.crs == DEFAULT_GEOGRAPHY_CRS assert type_var.algorithm == DEFAULT_GEOGRAPHY_ALGORITHM - assert str(type_var) == "geography" + assert str(type_var) == "geography(OGC:CRS84, spherical)" assert repr(type_var) == "GeographyType()" assert type_var.minimum_format_version() == 3 assert type_var.is_primitive @@ -848,43 +849,44 @@ def test_geography_type_custom_crs() -> None: type_var = GeographyType("EPSG:4326") assert type_var.crs == "EPSG:4326" assert type_var.algorithm == DEFAULT_GEOGRAPHY_ALGORITHM - assert str(type_var) == "geography(EPSG:4326)" + assert str(type_var) == "geography(EPSG:4326, spherical)" assert repr(type_var) == "GeographyType(crs='EPSG:4326')" def test_geography_type_custom_crs_and_algorithm() -> None: """Test GeographyType with custom CRS and algorithm.""" - type_var = GeographyType("EPSG:4326", "planar") + type_var = GeographyType("EPSG:4326", "karney") assert type_var.crs == "EPSG:4326" - assert type_var.algorithm == "planar" - assert str(type_var) == "geography(EPSG:4326, planar)" - assert repr(type_var) == "GeographyType(crs='EPSG:4326', algorithm='planar')" + assert type_var.algorithm == "karney" + assert str(type_var) == "geography(EPSG:4326, karney)" + assert repr(type_var) == "GeographyType(crs='EPSG:4326', algorithm='karney')" def test_geography_type_equality() -> None: """Test GeographyType equality and hashing.""" assert GeographyType() == GeographyType() assert GeographyType("EPSG:4326") == GeographyType("EPSG:4326") - assert GeographyType("EPSG:4326", "planar") == GeographyType("EPSG:4326", "planar") + assert GeographyType("EPSG:4326", "karney") == GeographyType("EPSG:4326", "karney") assert GeographyType() != GeographyType("EPSG:4326") - assert GeographyType("EPSG:4326") != GeographyType("EPSG:4326", "planar") + assert GeographyType("EPSG:4326") != GeographyType("EPSG:4326", "karney") assert hash(GeographyType()) == hash(GeographyType()) def test_geography_type_pickle() -> None: """Test GeographyType pickle round-trip.""" assert GeographyType() == pickle.loads(pickle.dumps(GeographyType())) - assert GeographyType("EPSG:4326", "planar") == pickle.loads(pickle.dumps(GeographyType("EPSG:4326", "planar"))) + assert GeographyType("EPSG:4326", "karney") == pickle.loads(pickle.dumps(GeographyType("EPSG:4326", "karney"))) def test_geography_type_serialization() -> None: """Test GeographyType JSON serialization.""" - assert GeographyType().model_dump_json() == '"geography"' - assert GeographyType("EPSG:4326").model_dump_json() == '"geography(EPSG:4326)"' - assert GeographyType("EPSG:4326", "planar").model_dump_json() == '"geography(EPSG:4326, planar)"' + assert GeographyType().model_dump_json() == '"geography(OGC:CRS84, spherical)"' + assert GeographyType("EPSG:4326").model_dump_json() == '"geography(EPSG:4326, spherical)"' + assert GeographyType("EPSG:4326", "karney").model_dump_json() == '"geography(EPSG:4326, karney)"' + assert GeographyType.model_validate_json(GeographyType().model_dump_json()) == GeographyType() assert GeographyType.model_validate_json(GeographyType("EPSG:4326").model_dump_json()) == GeographyType("EPSG:4326") - assert GeographyType.model_validate_json(GeographyType("EPSG:4326", "planar").model_dump_json()) == GeographyType( - "EPSG:4326", "planar" + assert GeographyType.model_validate_json(GeographyType("EPSG:4326", "karney").model_dump_json()) == GeographyType( + "EPSG:4326", "karney" ) @@ -892,7 +894,7 @@ def test_geography_type_deserialization() -> None: """Test GeographyType JSON deserialization.""" assert GeographyType.model_validate_json('"geography"') == GeographyType() assert GeographyType.model_validate_json("\"geography('EPSG:4326')\"") == GeographyType("EPSG:4326") - assert GeographyType.model_validate_json("\"geography('EPSG:4326', 'planar')\"") == GeographyType("EPSG:4326", "planar") + assert GeographyType.model_validate_json("\"geography('EPSG:4326', 'karney')\"") == GeographyType("EPSG:4326", "karney") for value in ( "geography(OGC:CRS84, spherical)", " GEOGRAPHY ( OGC:CRS84 , SPHERICAL ) ", @@ -929,7 +931,7 @@ def test_geography_type_singleton() -> None: """Test that GeographyType uses singleton pattern for same parameters.""" assert id(GeographyType()) == id(GeographyType()) assert id(GeographyType("EPSG:4326")) == id(GeographyType("EPSG:4326")) - assert id(GeographyType("EPSG:4326", "planar")) == id(GeographyType("EPSG:4326", "planar")) + assert id(GeographyType("EPSG:4326", "karney")) == id(GeographyType("EPSG:4326", "karney")) assert id(GeographyType()) != id(GeographyType("EPSG:4326")) @@ -945,10 +947,10 @@ def test_nested_field_with_geometry() -> None: def test_nested_field_with_geography() -> None: """Test NestedField with GeographyType.""" - field = NestedField(1, "location", GeographyType("EPSG:4326", "planar"), required=True) + field = NestedField(1, "location", GeographyType("EPSG:4326", "karney"), required=True) assert isinstance(field.field_type, GeographyType) assert field.field_type.crs == "EPSG:4326" - assert field.field_type.algorithm == "planar" + assert field.field_type.algorithm == "karney" def test_nested_field_geometry_as_string() -> None: @@ -975,10 +977,10 @@ def test_nested_field_geometry_with_params_as_string() -> None: def test_nested_field_geography_with_params_as_string() -> None: """Test NestedField with parameterized geography type as string.""" - field = NestedField(1, "location", "geography('EPSG:4326', 'planar')", required=True) + field = NestedField(1, "location", "geography('EPSG:4326', 'karney')", required=True) assert isinstance(field.field_type, GeographyType) assert field.field_type.crs == "EPSG:4326" - assert field.field_type.algorithm == "planar" + assert field.field_type.algorithm == "karney" def test_decimal_precision_validation() -> None: