Skip to content

Additionally support Postgis Geography codec under one generic PostgisCodec - #727

Open
mdedetrich wants to merge 2 commits into
pgjdbc:mainfrom
mdedetrich:add-postgis-geography
Open

mdedetrich wants to merge 2 commits into
pgjdbc:mainfrom
mdedetrich:add-postgis-geography

Conversation

@mdedetrich

@mdedetrich mdedetrich commented Jul 8, 2026 •

Copy link
Copy Markdown

Make sure that:

  • You have read the contribution guidelines.
  • You have created a feature request first to discuss your contribution intent. Please reference the feature request ticket number in the pull request.
  • You use the code formatters provided here and have them applied to your changes. Don't submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.

Issue description

Resolves: #528

New Public APIs

PostgisGeographyCodec (if this counts as an API).

Additional context

Note that the implementation does have a fair bit of boilerplate due to PostgisGeographyCodec being final and hence not being able to be subclassed. One alternative would be to have a package private implementation that both PostgisGeographyCodec and PostgisGeometryCodec can share (that way both PostgisGeographyCodec and PostgisGeometryCodec can be final).

As suggested in the linked github issue, the implementation is just a copy paste of PostgisGeometryCodec that also uses the org.locationtech.jts.geom.Geometry but this allows you to decode postgres geography types.

@mdedetrich
mdedetrich force-pushed the add-postgis-geography branch from 3ec52e3 to 64f447d Compare July 8, 2026 18:18
@mdedetrich
mdedetrich force-pushed the add-postgis-geography branch 2 times, most recently from 8d0e343 to 548ad38 Compare July 29, 2026 17:19
@mdedetrich

mdedetrich commented Jul 29, 2026 •

Copy link
Copy Markdown
Author

After realizing an issue with this implementation, specifically that encoding an object using Codec.canEncode(Object value), would have gotten surprising results into how that object would have been encoded depending on what order DefaultCodecLookup.findEncodeCodec finds them due to having multiple codecs sharing the JTS Geography type.

Given that, I opted for a design that is more semantically correct, i.e. treat Geography the same way that String is treated and make a generic PostgisCodec that supports both geography and geometry in the same way String supports multiple types (VARCHAR, TEXT, NAME etc etc).

This did require touching some other internals i.e. BuiltinDynamicCodecs had to be updated to support retrieving multiple oid's from multiple rows and join them into a single codec (currently only supported one oid per row), but I think the end result is cleaner, better and more idiomatic as just like String, the only difference between Geography and Geometry are internal differences in how Postgres calculates distances, but they both fundamentally represent the same type on a client.

@mdedetrich mdedetrich changed the title Add Postgis Geography codec Support Postgis Geography codec under one PostgisCodec Jul 29, 2026
@mdedetrich
mdedetrich force-pushed the add-postgis-geography branch 3 times, most recently from f03ef79 to 8493054 Compare July 29, 2026 18:25
@mdedetrich mdedetrich changed the title Support Postgis Geography codec under one PostgisCodec Additionally support Postgis Geography codec under one generic PostgisCodec Jul 29, 2026
@mdedetrich

Copy link
Copy Markdown
Author

@mp911de When do you have capacity to review this PR? I am asking because there are follow up pull requests based on this one that I want to look at.

@mp911de

mp911de commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator

This is an interesting addition, but the changes touch several places, apparently to avoid registering two codec instances. Honoring the method-provided OID for encoding is a useful improvement on its own right. However, discovery and type translation now have two owners, making the built-in registration mechanism harder to follow.

DiscoveredType, collectList(), and registerCodecs() introduce an intermediate discovery step merely to pair two OIDs. I suggest registering the codec twice, once for each OID, while preserving geometry as the default encoding type to avoid further ceremony. As a separate optimization, PostgisGeometryCodec.geometryFactory could be made static.

A few details could also be simplified: The explanatory comments are rather lengthy for the behavior involved with causing noise only. Such changes lead to overall code quality deterioration as comments vs. behavior diverges over time. Tests calling registerCodecs() directly bypass discovery and tie coverage to the intermediate representation introduced here.

We appreciate community contributions, but we do need them to meet a reasonable baseline for consideration. If LLM-generated code still requires substantial simplification, submitting it largely shifts that work to the maintainers resulting in negative value contributions. There's little difference in maintainers prompting an LLM and then cleaning up AI-generated code bloat vs. someone else prompting an LLM and then maintainers cleaning up AI-generated code bloat.

@mp911de mp911de added the status: on-hold We cannot start working on this issue yet label Sep 17, 2026
@mdedetrich

mdedetrich commented Oct 13, 2026 •

Copy link
Copy Markdown
Author

Thanks for the response!

A few details could also be simplified: The explanatory comments are rather lengthy for the behavior involved with causing noise only. Such changes lead to overall code quality deterioration as comments vs. behavior diverges over time. Tests calling registerCodecs() directly bypass discovery and tie coverage to the intermediate representation introduced here.

We appreciate community contributions, but we do need them to meet a reasonable baseline for consideration. If LLM-generated code still requires substantial simplification, submitting it largely shifts that work to the maintainers resulting in negative value contributions. There's little difference in maintainers prompting an LLM and then cleaning up AI-generated code bloat vs. someone else prompting an LLM and then maintainers cleaning up AI-generated code bloat.

Would like to apologize for creating unnecessary burden, this definitely was not the intention. It is true that I am using LLM (specifically claude), the comments on this PR was already pruned before submitting but clearly its still too verbose (in general this is one big issue with Claude, it tends to create eulogy like code comments and this need to be explicitly cleaned multiple times).

Would you be open to me contributing an AGENTS.md file for this repo (as a separate PR), with it initially placing explicit instructions to not provide verbose comments? This tends to help a lot (from experience in working in other projects), ofc other guidelines/rules can also be placed in there (including attribution) and it would be automatically picked up by anyone that happens to use any kind of LLM.

This is an interesting addition, but the changes touch several places, apparently to avoid registering two codec instances. Honoring the method-provided OID for encoding is a useful improvement on its own right. However, discovery and type translation now have two owners, making the built-in registration mechanism harder to follow.

DiscoveredType, collectList(), and registerCodecs() introduce an intermediate discovery step merely to pair two OIDs. I suggest registering the codec twice, once for each OID, while preserving geometry as the default encoding type to avoid further ceremony. As a separate optimization, PostgisGeometryCodec.geometryFactory could be made static.

This was actually the original solution and then I changed the implementation to use this one thinking it would be a better design, I was using the java String vs various database encodings i.e. text, varchar as a basis but evidently the implementation went off the rails.

I have just done what you instructed and also rebased off of main. Let me know if anything else is needed

Revert the intermediate discovery step (DiscoveredType, collectList(),
registerCodecs()) and register PostgisGeometryCodec once for each OID. The
geography instance is decode-only, so geometry stays the default encoding
type whatever order pg_type returns the rows in.

Use the OID passed to encode(Object, int), make GeometryFactory static, and
trim the verbose comments and the tests that bypassed discovery.
@mdedetrich
mdedetrich force-pushed the add-postgis-geography branch from 98b756f to 9326eba Compare October 13, 2026 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: on-hold We cannot start working on this issue yet type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add geography data type support

2 participants