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 type #354

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tests/integration/test_types_integration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import math
import re
import uuid
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
import uuid
from uuid import UUID

Copy link
Member Author

Choose a reason for hiding this comment

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

The reason why I did this is because I saw elsewhere in the code was importing uuid instead of uuid.UUID. But I can change this.

from datetime import date, datetime, time, timedelta, timezone, tzinfo
from decimal import Decimal

Expand Down Expand Up @@ -774,7 +775,8 @@ def test_ipaddress(trino_connection):
def test_uuid(trino_connection):
SqlTest(trino_connection) \
.add_field(sql="CAST(null AS UUID)", python=None) \
.add_field(sql="UUID '12151fd2-7586-11e9-8f9e-2a86e4085a59'", python='12151fd2-7586-11e9-8f9e-2a86e4085a59') \
.add_field(sql="UUID '12151fd2-7586-11e9-8f9e-2a86e4085a59'",
python=uuid.UUID('12151fd2-7586-11e9-8f9e-2a86e4085a59')) \
.execute()


Expand Down
10 changes: 10 additions & 0 deletions trino/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import re
import threading
import urllib.parse
import uuid
import warnings
from dataclasses import dataclass
from datetime import date, datetime, time, timedelta, timezone, tzinfo
Expand Down Expand Up @@ -1185,6 +1186,13 @@ def map(self, values: Any) -> Optional[Dict[Any, Optional[Any]]]:
}


class UuidValueMapper(ValueMapper[uuid.UUID]):
def map(self, value: Any) -> Optional[uuid.UUID]:
if value is None:
return None
return uuid.UUID(value)


class NoOpRowMapper:
"""
No-op RowMapper which does not perform any transformation
Expand Down Expand Up @@ -1245,6 +1253,8 @@ def _create_value_mapper(self, column) -> ValueMapper:
return DateValueMapper()
elif col_type == 'varbinary':
return BinaryValueMapper()
elif col_type == 'uuid':
return UuidValueMapper()
else:
return NoOpValueMapper()

Expand Down