Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WKB Support for PostGIS Geometry Columns #9951

Closed
wants to merge 18 commits into from
Closed

Add WKB Support for PostGIS Geometry Columns #9951

wants to merge 18 commits into from

Conversation

slhmy
Copy link

@slhmy slhmy commented Nov 13, 2021

Close #5580.

I'm not looking forward to a quick merge, for there is still something I'm not sure about.
Just trying to push works to go on.

About geometry read

This solution needs extra dependency trino-geospatial, and changes are mainly related with the GeometryType in this dependency.
Since GeometryType is not a type in SPI, the using of it is not available in test environment. So if I don't make changes to test, I will fail while installing this plugin. However this change only effects postgresql-connector's test environment. And that means the next time if other connectors need Geometry read ability, we still have to do similar changes.
So, if could, works to contain GeometryType into test environment should be considered.

Also, I'm not sure overriding buildSql method is suggested, but it's necessary for the implementation. It automatically change query like SELECT geom FROM Geometries into SELECT ST_AsBinary(geom) FROM Geometries. ST_AsBinary will make the binary of PostGIS Geometry into a pure WKB, then we can use ColumnMapping like other types do.

About geometry insert

I‘ll do this recently. Basically, it will override getBindExpression for geometryWriteFunction, and this will enables me to have chance to wrap a ST_GeomFromWKB function to wrap around geometry value.

I get the idea from Geotools's PostGISPSDialect, which I think is the way Geotools uses to insert geometries.

About config

I'm deciding adding an enablePostGISFeatures config for connector, currently it's only related with geometryColumnMapping stuff. But I think it's better to manage all the PostGIS features in a same config.

Plan

To see things I finished & haven't finished:

  • Support geometry read
  • Support geometry insert
  • Config to control whether to use these functionality (since not every PG has installed PostGIS extension)
  • Test and code optimize (review wanted 👋)

Regrets

It's my first try in trino, I'm not so familiar with the whole project. Any change will be welcomed. My speed of reply may be a little bit slow, thank for you understanding.❤️️

The implementation of `Geometry` reads needs to map `geometry` ColumnType to `GEOMETRY` Type which is imported from trino-geospatial dependency.

And in order to make the new Connector pass through test environment, `TYPE_MANAGER` of `TestPostgreSqlClient
` and `ConnectorContext` of `TestPostgreSqlPlugin` need to have `GEOMETRY` Type. Also, `PostgreSqlQueryRunner` needs to install `GeoPlugin`.
Specifically, it overrides `buildSql` method from `BaseJdbcClient` to make Query for `geometry` Type column surrounded by `ST_AsBinary` function. Then we will get WKB content from JDBC-driver. After that, we use `stGeomFromBinary` function in `trino-geospatial` plugin to get trino `Geometry` Type from WKB.
@cla-bot
Copy link

cla-bot bot commented Nov 13, 2021

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please submit the signed CLA to cla@trino.io. For more information, see https://github.com/trinodb/cla.

@hashhar hashhar self-requested a review November 15, 2021 06:24
@slhmy
Copy link
Author

slhmy commented Dec 2, 2021

@cla-bot check

@cla-bot
Copy link

cla-bot bot commented Dec 2, 2021

The cla-bot has been summoned, and re-checked this pull request!

@cla-bot cla-bot bot added the cla-signed label Dec 2, 2021
@slhmy slhmy self-assigned this Dec 7, 2021
@slhmy slhmy closed this Dec 7, 2021
@slhmy slhmy deleted the slhmy-postgis-geometry-read branch December 7, 2021 06:37
@slhmy slhmy restored the slhmy-postgis-geometry-read branch December 7, 2021 06:40
@slhmy slhmy reopened this Dec 7, 2021
@slhmy
Copy link
Author

slhmy commented Dec 7, 2021

I'm sorry, it seems that I'm making a mistake on my branch name (I'm supporting things more than read stuff).
But I can't change the branch name..

@hashhar
Copy link
Member

hashhar commented Dec 15, 2021

@slhmy Sorry for the delay. Is this ready to be reviewed? I can take a look later today or tomorrow.

@hashhar
Copy link
Member

hashhar commented Dec 15, 2021

Regarding the idea of adding a config to control this - I'm not sure I understand.

If a Postgres doesn't have PostGIS there would be no columns of geometry type (do I understand incorrectly?) so the type mapping you add won't get invoked - so it shouldn't matter if PostGIS is present or not?

Or if you mean that Postgres can have GEOMETRY columns even without PostGIS then I think a better way to handle this is to check whether the column comes from PostGIS or not and use the correct function accordingly. (I'm not sure if it's possible to detect PostGIS, just an idea.)

The downside of configs is that it's one more toggle for users to enable, to add tests for and doesn't make things "just work".

@slhmy
Copy link
Author

slhmy commented Dec 15, 2021

@slhmy Sorry for the delay. Is this ready to be reviewed? I can take a look later today or tomorrow.

@hashhar If tests are not necessary, I think, yes 😗.

If a Postgres doesn't have PostGIS there would be no columns of geometry type (do I understand incorrectly?) so the type mapping you add won't get invoked - so it shouldn't matter if PostGIS is present or not?

It's true, I agree.

The main reason driving me to the config work is the test context stuff (you can see plugin/trino-postgresql/src/test/java/io/trino/plugin/postgresql/PostgreSqlTestingConnectorContext.java), commonly the testing context does not contains spatial plugin. So, I guess if spatial functionality should be separated.

But if it's okay to put things together, I think I'm worrying too much.

Or if you mean that Postgres can have GEOMETRY columns even without PostGIS then I think a better way to handle this is to check whether the column comes from PostGIS or not and use the correct function accordingly. (I'm not sure if it's possible to detect PostGIS, just an idea.)

Oh I'm not sure, either 😂. For me it's okay not to add config.

if (jdbcTypeHandle.getJdbcTypeName().isPresent() && jdbcTypeHandle.getJdbcTypeName().get().equals("geometry")) {
String columnName = column.getColumnName();
log.debug("Find geometry type, changing '%s' to '%s'", columnName, "ST_AsBinary(\"" + columnName + "\")");
supposedColumnExpressions.put(columnName, "ST_AsBinary(\"" + columnName + "\")");
Copy link
Member

Choose a reason for hiding this comment

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

cc: @ebyhr since you recently were looking into ways to apply functions to columns on the read path. Do you know of a better alternative?

@hashhar hashhar requested review from hashhar, wendigo and ebyhr January 12, 2022 06:49
Copy link
Member

@ebyhr ebyhr left a comment

Choose a reason for hiding this comment

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

Could you add a new type mapping test using https://hub.docker.com/r/postgis/postgis?

  1. Add TestingPostGisServer likes TestingPostgreSqlServer
  2. Add TestPostGisTypeMapping likes TestPostgreSqlTypeMapping

@slhmy
Copy link
Author

slhmy commented Jan 12, 2022

Could you add a new type mapping test using https://hub.docker.com/r/postgis/postgis?

  1. Add TestingPostGisServer likes TestingPostgreSqlServer
  2. Add TestPostGisTypeMapping likes TestPostgreSqlTypeMapping

Thanks for suggestion.

I'll try this and notify you as soon as I finish.

@slhmy
Copy link
Author

slhmy commented Feb 20, 2022

@ebyhr
Sorry for being late, I have added TestingPostGisServer & TestPostGisTypeMapping in the latest commit.
But I didn't succeed in adding geometry mapping test for it, since currently GeometryType is not a compareable type in Trino.

@bitsondatadev
Copy link
Member

👋 @slhmy - this PR has become inactive. If you're still interested in working on it, please let us know, and we can try to get reviewers to help with that.

We're working on closing out old and inactive PRs, so if you're too busy or this has too many merge conflicts to be worth picking back up, we'll be making another pass to close it out in a few weeks.

@colebow
Copy link
Member

colebow commented Nov 30, 2022

Closing this one out due to inactivity, but please reopen if you would like to pick this back up.

@colebow colebow closed this Nov 30, 2022
@umartin umartin mentioned this pull request Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Add WKB Support for PostGIS Geometry Columns
5 participants