Skip to content

Commit 63535d0

Browse files
committed
add an option deferred_fetch to Cursor.execute()
1 parent cbc0e12 commit 63535d0

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

trino/client.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ def result(self):
775775
def info_uri(self):
776776
return self._info_uri
777777

778-
def execute(self, additional_http_headers=None) -> TrinoResult:
778+
def execute(self, additional_http_headers=None, deferred_fetch: bool = False) -> TrinoResult:
779779
"""Initiate a Trino query by sending the SQL statement
780780
781781
This is the first HTTP request sent to the coordinator.
@@ -799,9 +799,11 @@ def execute(self, additional_http_headers=None) -> TrinoResult:
799799
rows = self._row_mapper.map(status.rows) if self._row_mapper else status.rows
800800
self._result = TrinoResult(self, rows)
801801

802-
# Execute should block until at least one row is received or query is finished or cancelled
803-
while not self.finished and not self.cancelled and len(self._result.rows) == 0:
804-
self._result.rows += self.fetch()
802+
if not deferred_fetch:
803+
# Execute should block until at least one row is received or query is finished or cancelled
804+
while not self.finished and not self.cancelled and len(self._result.rows) == 0:
805+
self._result.rows += self.fetch()
806+
805807
return self._result
806808

807809
def _update_state(self, status):

trino/dbapi.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,10 @@ def _deallocate_prepared_statement(self, statement_name: str) -> None:
558558
def _generate_unique_statement_name(self):
559559
return 'st_' + uuid.uuid4().hex.replace('-', '')
560560

561-
def execute(self, operation, params=None):
561+
def execute(self, operation, params=None, **kwargs: Any):
562+
additional_http_headers = kwargs.get("additional_http_headers", None)
563+
deferred_fetch = kwargs.get("deferred_fetch", False)
564+
562565
if params:
563566
assert isinstance(params, (list, tuple)), (
564567
'params must be a list or tuple containing the query '
@@ -575,7 +578,10 @@ def execute(self, operation, params=None):
575578
self._query = self._execute_prepared_statement(
576579
statement_name, params
577580
)
578-
self._iterator = iter(self._query.execute())
581+
self._iterator = iter(self._query.execute(
582+
additional_http_headers=additional_http_headers,
583+
deferred_fetch=deferred_fetch,
584+
))
579585
finally:
580586
# Send deallocate statement
581587
# At this point the query can be deallocated since it has already
@@ -584,12 +590,18 @@ def execute(self, operation, params=None):
584590
self._deallocate_prepared_statement(statement_name)
585591
else:
586592
self._query = self._execute_immediate_statement(operation, params)
587-
self._iterator = iter(self._query.execute())
593+
self._iterator = iter(self._query.execute(
594+
additional_http_headers=additional_http_headers,
595+
deferred_fetch=deferred_fetch,
596+
))
588597

589598
else:
590599
self._query = trino.client.TrinoQuery(self._request, query=operation,
591600
legacy_primitive_types=self._legacy_primitive_types)
592-
self._iterator = iter(self._query.execute())
601+
self._iterator = iter(self._query.execute(
602+
additional_http_headers=additional_http_headers,
603+
deferred_fetch=deferred_fetch,
604+
))
593605
return self
594606

595607
def executemany(self, operation, seq_of_params):

trino/sqlalchemy/dialect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def _get_default_schema_name(self, connection: Connection) -> Optional[str]:
377377
def do_execute(
378378
self, cursor: Cursor, statement: str, parameters: Tuple[Any, ...], context: DefaultExecutionContext = None
379379
):
380-
cursor.execute(statement, parameters)
380+
cursor.execute(statement, parameters, **context.execution_options)
381381

382382
def do_rollback(self, dbapi_connection: trino_dbapi.Connection):
383383
if dbapi_connection.transaction is not None:

0 commit comments

Comments
 (0)