Skip to content

Commit 8482f47

Browse files
tswastparthea
andauthored
fix: keep RowIterator.total_rows populated after iteration (#1748)
* 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 <[email protected]> --------- Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent ab73796 commit 8482f47

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

google/cloud/bigquery/table.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2997,9 +2997,9 @@ def _rows_page_start(iterator, page, response):
29972997
page._columns = _row_iterator_page_columns(iterator._schema, response)
29982998

29992999
total_rows = response.get("totalRows")
3000+
# Don't reset total_rows if it's not present in the next API response.
30003001
if total_rows is not None:
3001-
total_rows = int(total_rows)
3002-
iterator._total_rows = total_rows
3002+
iterator._total_rows = int(total_rows)
30033003

30043004

30053005
# pylint: enable=unused-argument

tests/unit/test_table.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -2201,9 +2201,18 @@ def test_iterate_with_cached_first_page(self):
22012201
path = "/foo"
22022202
api_request = mock.Mock(return_value={"rows": rows})
22032203
row_iterator = self._make_one(
2204-
_mock_client(), api_request, path, schema, first_page_response=first_page
2204+
_mock_client(),
2205+
api_request,
2206+
path,
2207+
schema,
2208+
first_page_response=first_page,
2209+
total_rows=4,
22052210
)
2211+
self.assertEqual(row_iterator.total_rows, 4)
22062212
rows = list(row_iterator)
2213+
# Total rows should be maintained, even though subsequent API calls
2214+
# don't include it.
2215+
self.assertEqual(row_iterator.total_rows, 4)
22072216
self.assertEqual(len(rows), 4)
22082217
self.assertEqual(rows[0].age, 27)
22092218
self.assertEqual(rows[1].age, 28)

0 commit comments

Comments
 (0)