Skip to content

Commit

Permalink
fix: keep RowIterator.total_rows populated after iteration (#1748)
Browse files Browse the repository at this point in the history
* fix: keep `RowIterator.total_rows` populated after iteration

This was being reset in some cases when
the rows were all available in the
first page of results.

* Update google/cloud/bigquery/table.py

Co-authored-by: Anthonios Partheniou <partheniou@google.com>

---------

Co-authored-by: Anthonios Partheniou <partheniou@google.com>
  • Loading branch information
tswast and parthea committed Dec 8, 2023
1 parent ab73796 commit 8482f47
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions google/cloud/bigquery/table.py
Expand Up @@ -2997,9 +2997,9 @@ def _rows_page_start(iterator, page, response):
page._columns = _row_iterator_page_columns(iterator._schema, response)

total_rows = response.get("totalRows")
# Don't reset total_rows if it's not present in the next API response.
if total_rows is not None:
total_rows = int(total_rows)
iterator._total_rows = total_rows
iterator._total_rows = int(total_rows)


# pylint: enable=unused-argument
Expand Down
11 changes: 10 additions & 1 deletion tests/unit/test_table.py
Expand Up @@ -2201,9 +2201,18 @@ def test_iterate_with_cached_first_page(self):
path = "/foo"
api_request = mock.Mock(return_value={"rows": rows})
row_iterator = self._make_one(
_mock_client(), api_request, path, schema, first_page_response=first_page
_mock_client(),
api_request,
path,
schema,
first_page_response=first_page,
total_rows=4,
)
self.assertEqual(row_iterator.total_rows, 4)
rows = list(row_iterator)
# Total rows should be maintained, even though subsequent API calls
# don't include it.
self.assertEqual(row_iterator.total_rows, 4)
self.assertEqual(len(rows), 4)
self.assertEqual(rows[0].age, 27)
self.assertEqual(rows[1].age, 28)
Expand Down

0 comments on commit 8482f47

Please sign in to comment.