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

Support UUID for SQLAlchemy #359

Merged
merged 1 commit into from
Apr 27, 2023
Merged

Conversation

lpoulain
Copy link
Member

@lpoulain lpoulain commented Apr 14, 2023

Description

Provide a SQLAlchemy mapping to Trino's UUID type (resolves issue #331 )

Non-technical explanation

Provide UUID support for SQLAlchemy

Release notes

( ) This is not user-visible or docs only and no release notes are required.
( ) Release notes are required, please propose a release note for me.
(X) Release notes are required, with the following suggested text:

* Support UUID for SQLAlchemy ({issue}`358`)

@cla-bot cla-bot bot added the cla-signed label Apr 14, 2023
@@ -86,6 +86,11 @@ def get_col_spec(self, **kw):
return 'JSON'


class UUID(UserDefinedType):
Copy link
Member

Choose a reason for hiding this comment

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

please add __visit_name__

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@@ -89,13 +91,15 @@ def test_define_and_create_table(trino_connection):
sqla.Table('users',
metadata,
sqla.Column('id', sqla.Integer),
sqla.Column('guid', UUID),
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

This works locally, but when running on GH it fails with AttributeError: module 'sqlalchemy' has no attribute 'Uuid'

Copy link
Contributor

@mdesmet mdesmet Apr 17, 2023

Choose a reason for hiding this comment

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

Uuid only works in v2 of sqlalchemy. So you will need to ensure it only works in that version. also we should probably test with release version of v2 instead of rc (maybe take care of that in a separate commit).

Copy link
Member Author

Choose a reason for hiding this comment

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

So should there be two test_define_and_create_table() tests? One for v2+ and one for older versions?

Comment on lines 89 to 95
class UUID(UserDefinedType):
__visit_name__ = "UUID"

def get_col_spec(self, **kw):
return "UUID"


Copy link
Contributor

Choose a reason for hiding this comment

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

I think this code block should be removed as we don't need to add a custom UserDefinedType

@@ -122,7 +129,7 @@ def get_col_spec(self, **kw):
#
# === Mixed ===
# 'ipaddress': IPADDRESS
# 'uuid': UUID,
"uuid": UUID,
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be mapped to the sqlalchemy uuid type if it's available

Copy link
Member Author

Choose a reason for hiding this comment

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

What if the type is not available? Won't that break the import statement?

Comment on lines 199 to 201
def visit_UUID(self, type, **kw):
return "UUID"

Copy link
Contributor

Choose a reason for hiding this comment

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

This should only be added if the uuid type is available.

Comment on lines 197 to 199
def test_parse_uuid():
actual_type = datatype.parse_sqltype("UUID")
assert type(actual_type) == UUID
Copy link
Contributor

Choose a reason for hiding this comment

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

Test should only be succesful if type is available.

@lpoulain lpoulain force-pushed the support-uuid-for-sqla branch 2 times, most recently from 2ede56f to e5216f5 Compare April 20, 2023 18:47
Copy link
Contributor

@mdesmet mdesmet left a comment

Choose a reason for hiding this comment

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

LGTM except reason

@@ -133,6 +135,60 @@ def test_insert(trino_connection):
metadata.drop_all(engine)


@pytest.mark.skipif(
sqlalchemy_version() < "2.0",
reason="columns argument to select() must be a Python list or other iterable"
Copy link
Contributor

Choose a reason for hiding this comment

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

reason seems not correct.

@@ -122,13 +123,16 @@ def get_col_spec(self, **kw):
#
# === Mixed ===
# 'ipaddress': IPADDRESS
# 'uuid': UUID,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you revert this change

@mdesmet
Copy link
Contributor

mdesmet commented Apr 21, 2023

Please also add that this resolves #331 in the PR description.

Support UUID for SQLAlchemy
@mdesmet mdesmet requested a review from hashhar April 21, 2023 13:56
@hashhar hashhar merged commit 226b216 into trinodb:master Apr 27, 2023
@lpoulain lpoulain mentioned this pull request Apr 27, 2023
1 task
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.

4 participants