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

Return row as a namedtuple instead of tuple #1

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
19 changes: 11 additions & 8 deletions trino/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
from datetime import date, datetime, time, timedelta, timezone, tzinfo
from decimal import Decimal
from time import sleep
from typing import Any, Dict, Generic, List, Optional, Tuple, TypeVar, Union

from typing import Any, Dict, Generic, List, Optional, Tuple, TypeVar, Union, NamedTuple
from collections import namedtuple
import pytz
import requests
from pytz.tzinfo import BaseTzInfo
Expand Down Expand Up @@ -1128,15 +1128,17 @@ def map(self, values: List[Any]) -> Optional[List[Any]]:
return [self.mapper.map(value) for value in values]


class RowValueMapper(ValueMapper[Tuple[Optional[Any], ...]]):
def __init__(self, mappers: List[ValueMapper[Any]]):
class RowValueMapper(ValueMapper["RowTuple"]):
def __init__(self, keys: List[str], mappers: List[ValueMapper[Any]]):
self.keys = keys
self.mappers = mappers

def map(self, values: List[Any]) -> Optional[Tuple[Optional[Any], ...]]:
def map(self, values: List[Any]) -> Optional["RowTuple"]:
if values is None:
return None
return tuple(self.mappers[index].map(value) for index, value in enumerate(values))

RowTuple = namedtuple("Row", self.keys)
mapped_values = [self.mappers[index].map(value) for index, value in enumerate(values)]
return RowTuple(*mapped_values)

class MapValueMapper(ValueMapper[Dict[Any, Optional[Any]]]):
def __init__(self, key_mapper: ValueMapper[Any], value_mapper: ValueMapper[Any]):
Expand Down Expand Up @@ -1183,8 +1185,9 @@ def _create_value_mapper(self, column) -> ValueMapper:
value_mapper = self._create_value_mapper(column['arguments'][0]['value'])
return ArrayValueMapper(value_mapper)
elif col_type == 'row':
keys = [arg['value']['fieldName']['name'] for arg in column['arguments']]
mappers = [self._create_value_mapper(arg['value']['typeSignature']) for arg in column['arguments']]
return RowValueMapper(mappers)
return RowValueMapper(keys,mappers)
elif col_type == 'map':
key_mapper = self._create_value_mapper(column['arguments'][0]['value'])
value_mapper = self._create_value_mapper(column['arguments'][1]['value'])
Expand Down