-
Notifications
You must be signed in to change notification settings - Fork 197
Provide rowcount for DML queries #325
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,7 @@ | |
| import threading | ||
| import urllib.parse | ||
| import warnings | ||
| from dataclasses import dataclass | ||
| from datetime import date, datetime, time, timedelta, timezone, tzinfo | ||
| from decimal import Decimal | ||
| from time import sleep | ||
|
|
@@ -290,16 +291,17 @@ def get_roles_values(headers, header): | |
| ] | ||
|
|
||
|
|
||
| class TrinoStatus(object): | ||
| def __init__(self, id, stats, warnings, info_uri, next_uri, update_type, rows, columns=None): | ||
| self.id = id | ||
| self.stats = stats | ||
| self.warnings = warnings | ||
| self.info_uri = info_uri | ||
| self.next_uri = next_uri | ||
| self.update_type = update_type | ||
| self.rows = rows | ||
| self.columns = columns | ||
| @dataclass | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very nice. ❤️ |
||
| class TrinoStatus: | ||
| id: str | ||
| stats: Dict[str, str] | ||
| warnings: List[Any] | ||
| info_uri: str | ||
| next_uri: Optional[str] | ||
| update_type: Optional[str] | ||
| update_count: Optional[int] | ||
| rows: List[Any] | ||
| columns: List[Any] | ||
|
|
||
| def __repr__(self): | ||
| return ( | ||
|
|
@@ -665,6 +667,7 @@ def process(self, http_response) -> TrinoStatus: | |
| info_uri=response["infoUri"], | ||
| next_uri=self._next_uri, | ||
| update_type=response.get("updateType"), | ||
| update_count=response.get("updateCount"), | ||
| rows=response.get("data", []), | ||
| columns=response.get("columns"), | ||
| ) | ||
|
|
@@ -742,6 +745,7 @@ def __init__( | |
| self._cancelled = False | ||
| self._request = request | ||
| self._update_type = None | ||
| self._update_count = None | ||
| self._sql = sql | ||
| self._result: Optional[TrinoResult] = None | ||
| self._legacy_primitive_types = legacy_primitive_types | ||
|
|
@@ -764,6 +768,10 @@ def stats(self): | |
| def update_type(self): | ||
| return self._update_type | ||
|
|
||
| @property | ||
| def update_count(self): | ||
| return self._update_count | ||
|
|
||
| @property | ||
| def warnings(self): | ||
| return self._warnings | ||
|
|
@@ -808,6 +816,7 @@ def execute(self, additional_http_headers=None) -> TrinoResult: | |
| def _update_state(self, status): | ||
| self._stats.update(status.stats) | ||
| self._update_type = status.update_type | ||
| self._update_count = status.update_count | ||
| if not self._row_mapper and status.columns: | ||
| self._row_mapper = RowMapperFactory().create(columns=status.columns, | ||
| legacy_primitive_types=self._legacy_primitive_types) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -315,13 +315,20 @@ def description(self) -> List[ColumnDescription]: | |
|
|
||
| @property | ||
| def rowcount(self): | ||
| """Not supported. | ||
| """The rowcount will be returned for INSERT, UPDATE, DELETE, MERGE | ||
| and CTAS statements based on `update_count` returned by the Trino | ||
| API. | ||
|
|
||
| Trino cannot reliablity determine the number of rows returned by an | ||
| operation. For example, the result of a SELECT query is streamed and | ||
| the number of rows is only knowns when all rows have been retrieved. | ||
| """ | ||
| If the rowcount can't be determined, -1 will be returned. | ||
|
|
||
| Trino cannot reliably determine the number of rows returned for DQL | ||
| queries. For example, the result of a SELECT query is streamed and | ||
| the number of rows is only known when all rows have been retrieved. | ||
|
|
||
| See https://peps.python.org/pep-0249/#rowcount | ||
| """ | ||
| if self._query is not None and self._query.update_count is not None: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment above can be updated to reflect the change. |
||
| return self._query.update_count | ||
| return -1 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not obvious so maybe a link to https://peps.python.org/pep-0249/#rowcount here would be useful i.e. the -1 is what the DB-API requires. |
||
|
|
||
| @property | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we have a utils module under test directory? I see some other such classes and methods which we be useful across tests.