Skip to content

Commit

Permalink
Acknowledge reception of data in TrinoResult
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesmet committed Aug 16, 2022
1 parent ac4c458 commit 5d59610
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
20 changes: 9 additions & 11 deletions trino/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,8 @@ class TrinoResult(object):

def __init__(self, query, rows=None, experimental_python_types: bool = False):
self._query = query
self._rows = rows or []
# Initial rows from the first POST request
self._rows = rows
self._rownumber = 0
self._experimental_python_types = experimental_python_types

Expand All @@ -603,20 +604,17 @@ def rownumber(self) -> int:
return self._rownumber

def __iter__(self):
# Initial fetch from the first POST request
for row in self._rows:
self._rownumber += 1
yield self._map_row(self._experimental_python_types, row, self._query.columns)
self._rows = None

# Subsequent fetches from GET requests until next_uri is empty.
while not self._query.finished:
rows = self._query.fetch()
for row in rows:
# A query only transitions to a FINISHED state when the results are fully consumed:
# The reception of the data is acknowledged by calling the next_uri before exposing the data through dbapi.
while not self._query.finished or self._rows is not None:
next_rows = self._query.fetch() if not self._query.finished else None
for row in self._rows:
self._rownumber += 1
logger.debug("row %s", row)
yield self._map_row(self._experimental_python_types, row, self._query.columns)

self._rows = next_rows

@property
def response_headers(self):
return self._query.response_headers
Expand Down
2 changes: 1 addition & 1 deletion trino/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def _prepare_statement(self, operation, statement_name):
operation=operation
)

# Send prepare statement. Copy the _request object to avoid poluting the
# Send prepare statement. Copy the _request object to avoid polluting the
# one that is going to be used to execute the actual operation.
query = trino.client.TrinoQuery(copy.deepcopy(self._request), sql=sql,
experimental_python_types=self._experimental_pyton_types)
Expand Down

0 comments on commit 5d59610

Please sign in to comment.