Skip to content

Commit 80893ff

Browse files
committed
Fix mapping of MAP types with boolean or integer keys
1 parent 9121fe9 commit 80893ff

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

Diff for: tests/integration/test_types_integration.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -799,11 +799,9 @@ def test_map(trino_connection):
799799
SqlTest(trino_connection)
800800
.add_field(sql="CAST(null AS MAP(VARCHAR, INTEGER))", python=None)
801801
.add_field(sql="MAP()", python={})
802-
# TODO: boolean map keys get returned as strings
803-
# .add_field(sql="MAP(ARRAY[true, false], ARRAY[false, true])", python={True: False, False: True})
804-
# .add_field(sql="MAP(ARRAY[true, false], ARRAY[true, null])", python={True: True, False: None})
805-
# TODO: integer map keys get returned as strings
806-
# .add_field(sql="MAP(ARRAY[1, 2], ARRAY[1, null])", python={1: 1, 2: None})
802+
.add_field(sql="MAP(ARRAY[true, false], ARRAY[false, true])", python={True: False, False: True})
803+
.add_field(sql="MAP(ARRAY[true, false], ARRAY[true, null])", python={True: True, False: None})
804+
.add_field(sql="MAP(ARRAY[1, 2], ARRAY[1, null])", python={1: 1, 2: None})
807805
.add_field(sql="MAP("
808806
"ARRAY[CAST('NaN' AS REAL), CAST('-Infinity' AS REAL), CAST(3.4028235E38 AS REAL), CAST(1.4E-45 AS REAL), CAST('Infinity' AS REAL), CAST(1 AS REAL)], " # noqa: E501
809807
"ARRAY[CAST('NaN' AS REAL), CAST('-Infinity' AS REAL), CAST(3.4028235E38 AS REAL), CAST(1.4E-45 AS REAL), CAST('Infinity' AS REAL), null])", # noqa: E501

Diff for: trino/mapper.py

+27
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@ def map(self, value: Any) -> Optional[T]:
3131
pass
3232

3333

34+
class BooleanValueMapper(ValueMapper[bool]):
35+
def map(self, value: Any) -> Optional[bool]:
36+
if value is None:
37+
return None
38+
if isinstance(value, bool):
39+
return value
40+
if str(value).lower() == 'true':
41+
return True
42+
if str(value).lower() == 'false':
43+
return False
44+
raise ValueError(f"Server sent unexpected value {value} of type {type(value)} for boolean")
45+
46+
47+
class IntegerValueMapper(ValueMapper[int]):
48+
def map(self, value: Any) -> Optional[int]:
49+
if value is None:
50+
return None
51+
if isinstance(value, int):
52+
return value
53+
# int(3.1) == 3 but server won't send such values for integer types
54+
return int(value)
55+
56+
3457
class DoubleValueMapper(ValueMapper[float]):
3558
def map(self, value) -> Optional[float]:
3659
if value is None:
@@ -221,6 +244,10 @@ def _create_value_mapper(self, column) -> ValueMapper:
221244
col_type = column['rawType']
222245

223246
# primitive types
247+
if col_type == 'boolean':
248+
return BooleanValueMapper()
249+
if col_type in {'tinyint', 'smallint', 'integer', 'bigint'}:
250+
return IntegerValueMapper()
224251
if col_type in {'double', 'real'}:
225252
return DoubleValueMapper()
226253
if col_type == 'decimal':

0 commit comments

Comments
 (0)