Skip to content
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

Add 'Query.num_dml_affected_rows' property. #3460

Merged
merged 1 commit into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions bigquery/google/cloud/bigquery/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,20 @@ def total_bytes_processed(self):
if total_bytes_processed is not None:
return int(total_bytes_processed)

@property
def num_dml_affected_rows(self):
"""Total number of rows affected by a DML query.

See:
https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query#numDmlAffectedRows

:rtype: int, or ``NoneType``
:returns: Count generated on the server (None until set by the server).
"""
num_dml_affected_rows = self._properties.get('numDmlAffectedRows')
if num_dml_affected_rows is not None:
return int(num_dml_affected_rows)

@property
def rows(self):
"""Query results.
Expand Down
30 changes: 30 additions & 0 deletions bigquery/tests/unit/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def _makeResource(self, complete=False):
]
resource['pageToken'] = self.TOKEN
resource['totalBytesProcessed'] = 100000
resource['numDmlAffectedRows'] = 123
resource['cacheHit'] = False

return resource
Expand Down Expand Up @@ -124,10 +125,12 @@ def _verifyResourceProperties(self, query, resource):
self.assertEqual(query.complete, resource.get('jobComplete'))
self.assertEqual(query.errors, resource.get('errors'))
self.assertEqual(query.page_token, resource.get('pageToken'))

if 'totalRows' in resource:
self.assertEqual(query.total_rows, int(resource['totalRows']))
else:
self.assertIsNone(query.total_rows)

if 'totalBytesProcessed' in resource:
self.assertEqual(query.total_bytes_processed,
int(resource['totalBytesProcessed']))
Expand All @@ -139,6 +142,12 @@ def _verifyResourceProperties(self, query, resource):
else:
self.assertIsNone(query.name)

if 'numDmlAffectedRows' in resource:
self.assertEqual(query.num_dml_affected_rows,
int(resource['numDmlAffectedRows']))
else:
self.assertIsNone(query.num_dml_affected_rows)

self._verify_udf_resources(query, resource)
self._verifyQueryParameters(query, resource)
self._verifySchema(query, resource)
Expand Down Expand Up @@ -371,6 +380,27 @@ def test_total_bytes_processed_present_string(self):
query._set_properties(resource)
self.assertEqual(query.total_bytes_processed, TOTAL_BYTES_PROCESSED)

def test_num_dml_affected_rows_missing(self):
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
self.assertIsNone(query.num_dml_affected_rows)

def test_num_dml_affected_rows_present_integer(self):
DML_AFFECTED_ROWS = 123456
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
resource = {'numDmlAffectedRows': DML_AFFECTED_ROWS}
query._set_properties(resource)
self.assertEqual(query.num_dml_affected_rows, DML_AFFECTED_ROWS)

def test_num_dml_affected_rows_present_string(self):
DML_AFFECTED_ROWS = 123456
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
resource = {'numDmlAffectedRows': str(DML_AFFECTED_ROWS)}
query._set_properties(resource)
self.assertEqual(query.num_dml_affected_rows, DML_AFFECTED_ROWS)

def test_schema(self):
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
Expand Down