Skip to content

Commit

Permalink
Support UUID type
Browse files Browse the repository at this point in the history
  • Loading branch information
lpoulain committed Apr 6, 2023
1 parent e10bc53 commit 64e9ec5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 2 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
from datetime import date, datetime, time, timedelta, timezone, tzinfo
from decimal import Decimal

Expand Down Expand Up @@ -774,7 +775,7 @@ 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
11 changes: 11 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,14 @@ def map(self, values: Any) -> Optional[Dict[Any, Optional[Any]]]:
}


class UuidValueMapper(ValueMapper[uuid.UUID]):
def map(self, values: Any) -> Optional[uuid.UUID]:
try:
return uuid.UUID(values)
except:
return None


class NoOpRowMapper:
"""
No-op RowMapper which does not perform any transformation
Expand Down Expand Up @@ -1245,6 +1254,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
3 changes: 2 additions & 1 deletion trino/sqlalchemy/datatype.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# limitations under the License.
import json
import re
import uuid
from typing import Any, Dict, Iterator, List, Optional, Tuple, Type, Union

from sqlalchemy import util
Expand Down Expand Up @@ -122,7 +123,7 @@ def get_col_spec(self, **kw):
#
# === Mixed ===
# 'ipaddress': IPADDRESS
# 'uuid': UUID,
'uuid': uuid.UUID,
# 'hyperloglog': HYPERLOGLOG,
# 'p4hyperloglog': P4HYPERLOGLOG,
# 'qdigest': QDIGEST,
Expand Down

0 comments on commit 64e9ec5

Please sign in to comment.