Skip to content

Types: Serialize geometry and geography without quoted parameters - #4482

Open
Anurag-S1ngh wants to merge 1 commit into
apache:mainfrom
Anurag-S1ngh:fix/4079-geo-type-unquoted
Open

Anurag-S1ngh wants to merge 1 commit into
apache:mainfrom
Anurag-S1ngh:fix/4079-geo-type-unquoted

Conversation

@Anurag-S1ngh

Copy link
Copy Markdown
Contributor

Rationale for this change

GeometryType and GeographyType serialize non-default parameters with quotes, for example geometry('EPSG:4326'). The Iceberg type serialization table defines the canonical form without quotes, for example geometry(srid:4326) and geography(srid:4326, spherical).

Parsing the unquoted form already landed in #4422. This completes that item by emitting the spec form, while keeping the quoted forms readable for metadata written by older versions.

Part of #4079.

Are these changes tested?

Yes. Updated the geometry and geography serialization tests and added JSON round-trip assertions for the new output. The existing quoted-input parsing tests remain in place.

  • pytest tests/test_types.py: 291 passed
  • pytest tests/ -m unit --ignore=tests/integration: 4882 passed, 6 skipped
  • ruff check and ruff format --check pass

Are there any user-facing changes?

Yes. Type strings written to table metadata now use the canonical unquoted form. Older quoted metadata remains readable.

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.

@Fokko Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up! Checked against the spec and Java: dropping the quotes is right and fixes reading these tables from Java. A few suggestions to fully reach the canonical form and drop planar, which isn't a valid geography algorithm.

Comment thread pyiceberg/types.py
Comment on lines 1019 to +1021
if self.crs == DEFAULT_GEOMETRY_CRS:
return "geometry"
return f"geometry('{self.crs}')"
return f"geometry({self.crs})"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spec's type serialization table lists "geometry(<C>)" as the canonical form ("The schema JSON type strings in this table are the canonical serialized forms"). Java also always writes the CRS, including the default (String.format("%s(%s)", NAME, crs())). Can we drop the default branch so GeometryType() serializes as geometry(OGC:CRS84)?

Suggested change
if self.crs == DEFAULT_GEOMETRY_CRS:
return "geometry"
return f"geometry('{self.crs}')"
return f"geometry({self.crs})"
return f"geometry({self.crs})"

Same for __str__ below; Java uses a single toString() for both.

Comment thread pyiceberg/types.py
Comment on lines 1088 to +1092
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})"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here: the canonical form is "geography(<C>, <A>)", and Java always writes both parameters (String.format("%s(%s, %s)", NAME, crs(), algorithm())), so the default becomes geography(OGC:CRS84, spherical).

Suggested change
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})"
return f"geography({self.crs}, {self.algorithm})"

And the same for __str__.

Comment thread pyiceberg/types.py
Comment on lines 1074 to +1077
>>> GeographyType("EPSG:4326", "planar")
GeographyType(crs='EPSG:4326', algorithm='planar')
>>> str(GeographyType("EPSG:4326", "planar"))
"geography('EPSG:4326', 'planar')"
'geography(EPSG:4326, planar)'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

planar isn't a valid edge-interpolation algorithm for geography. The spec only allows spherical, vincenty, thomas, andoyer and karney (planar edges are what geometry uses). Java's EdgeAlgorithm.fromName("planar") throws, so Java can't load a table with this type. Can we use spherical here?

Suggested change
>>> GeographyType("EPSG:4326", "planar")
GeographyType(crs='EPSG:4326', algorithm='planar')
>>> str(GeographyType("EPSG:4326", "planar"))
"geography('EPSG:4326', 'planar')"
'geography(EPSG:4326, planar)'
>>> GeographyType("EPSG:4326", "spherical")
GeographyType(crs='EPSG:4326')
>>> str(GeographyType("EPSG:4326", "spherical"))
'geography(EPSG:4326, spherical)'

Comment thread mkdocs/docs/geospatial.md
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the docstring: planar isn't a spec-valid geography algorithm. The GeographyType("EPSG:4326", "planar") example further up (line 57) needs the same change.

Suggested change
NestedField(4, "boundary", "geography(EPSG:4326, planar)", required=True)
NestedField(4, "boundary", "geography(EPSG:4326, spherical)", required=True)

Comment thread tests/test_types.py
Comment on lines 801 to +803
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")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the canonical form, the default includes the CRS as well:

Suggested change
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")
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")

If __str__ changes too, the str() asserts in test_geometry_type_default and test_geography_type_default need updating.

Comment thread tests/test_types.py
Comment on lines 882 to +888
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"
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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"
)
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", "karney").model_dump_json()) == GeographyType(
"EPSG:4326", "karney"
)

Comment thread tests/test_types.py
Comment on lines 857 to 861
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')"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

planar isn't spec-valid here either. spherical is the default, so to keep covering a non-default algorithm this needs another valid value, e.g. karney. The other planar uses in this file (the equality, pickle, singleton and NestedField tests) need the same change.

Suggested change
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')"
type_var = GeographyType("EPSG:4326", "karney")
assert type_var.crs == "EPSG:4326"
assert type_var.algorithm == "karney"
assert str(type_var) == "geography(EPSG:4326, karney)"
assert repr(type_var) == "GeographyType(crs='EPSG:4326', algorithm='karney')"

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants