Skip to content

Commit 141b586

Browse files
committed
Return int from 'total_rows'/'total_bytes_processed', if present.
Closes #3004.
1 parent cefe717 commit 141b586

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

bigquery/google/cloud/bigquery/query.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,9 @@ def total_rows(self):
208208
:rtype: int, or ``NoneType``
209209
:returns: Count generated on the server (None until set by the server).
210210
"""
211-
return self._properties.get('totalRows')
211+
total_rows = self._properties.get('totalRows')
212+
if total_rows is not None:
213+
return int(total_rows)
212214

213215
@property
214216
def total_bytes_processed(self):
@@ -220,7 +222,9 @@ def total_bytes_processed(self):
220222
:rtype: int, or ``NoneType``
221223
:returns: Count generated on the server (None until set by the server).
222224
"""
223-
return self._properties.get('totalBytesProcessed')
225+
total_bytes_processed = self._properties.get('totalBytesProcessed')
226+
if total_bytes_processed is not None:
227+
return int(total_bytes_processed)
224228

225229
@property
226230
def rows(self):

bigquery/unit_tests/test_query.py

+25-3
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,15 @@ def _verifyResourceProperties(self, query, resource):
124124
self.assertEqual(query.complete, resource.get('jobComplete'))
125125
self.assertEqual(query.errors, resource.get('errors'))
126126
self.assertEqual(query.page_token, resource.get('pageToken'))
127-
self.assertEqual(query.total_rows, resource.get('totalRows'))
128-
self.assertEqual(query.total_bytes_processed,
129-
resource.get('totalBytesProcessed'))
127+
if 'totalRows' in resource:
128+
self.assertEqual(query.total_rows, int(resource['totalRows']))
129+
else:
130+
self.assertIsNone(query.total_rows)
131+
if 'totalBytesProcessed' in resource:
132+
self.assertEqual(query.total_bytes_processed,
133+
int(resource['totalBytesProcessed']))
134+
else:
135+
self.assertIsNone(query.total_bytes_processed)
130136

131137
if 'jobReference' in resource:
132138
self.assertEqual(query.name, resource['jobReference']['jobId'])
@@ -336,6 +342,14 @@ def test_total_rows_present_integer(self):
336342
query._set_properties(resource)
337343
self.assertEqual(query.total_rows, TOTAL_ROWS)
338344

345+
def test_total_rows_present_string(self):
346+
TOTAL_ROWS = 42
347+
client = _Client(self.PROJECT)
348+
query = self._make_one(self.QUERY, client)
349+
resource = {'totalRows': str(TOTAL_ROWS)}
350+
query._set_properties(resource)
351+
self.assertEqual(query.total_rows, TOTAL_ROWS)
352+
339353
def test_total_bytes_processed_missing(self):
340354
client = _Client(self.PROJECT)
341355
query = self._make_one(self.QUERY, client)
@@ -349,6 +363,14 @@ def test_total_bytes_processed_present_integer(self):
349363
query._set_properties(resource)
350364
self.assertEqual(query.total_bytes_processed, TOTAL_BYTES_PROCESSED)
351365

366+
def test_total_bytes_processed_present_string(self):
367+
TOTAL_BYTES_PROCESSED = 123456
368+
client = _Client(self.PROJECT)
369+
query = self._make_one(self.QUERY, client)
370+
resource = {'totalBytesProcessed': str(TOTAL_BYTES_PROCESSED)}
371+
query._set_properties(resource)
372+
self.assertEqual(query.total_bytes_processed, TOTAL_BYTES_PROCESSED)
373+
352374
def test_schema(self):
353375
client = _Client(self.PROJECT)
354376
query = self._make_one(self.QUERY, client)

0 commit comments

Comments
 (0)