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 properties for new job statistics #3721

Closed
wants to merge 15 commits into from
Closed
14 changes: 14 additions & 0 deletions bigquery/google/cloud/bigquery/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,20 @@ def referenced_tables(self):

return tables

@property
def schema(self):
"""Return schema from job statistics, if present.

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

:rtype: list of :class:`~google.cloud.bigquery.schema.SchemaField

This comment was marked as spam.

:returns: fields describing the query's result set, or an empty list
if the query has not yet completed.
"""
query_stats = self._query_statistics()
return _parse_schema_resource(query_stats.get('schema', {}))

@property
def num_dml_affected_rows(self):

This comment was marked as spam.

"""Return total bytes billed from job statistics, if present.
Expand Down
50 changes: 50 additions & 0 deletions bigquery/tests/unit/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -1766,6 +1766,56 @@ def test_referenced_tables(self):
self.assertIsInstance(remote._dataset._client, _Client)
self.assertEqual(remote._dataset._client.project, 'other-project-123')

def test_schema(self):
from google.cloud.bigquery.table import _parse_schema_resource

schema = {
'fields': [{
'name': 'full_name',
'type': 'STRING',
'mode': 'NULLABLE',
'description': 'DESCRIPTION'
}, {
'name': 'phone_number',
'type': 'STRING',
'mode': 'REPEATED',
}, {
'name': 'address',
'type': 'RECORD',
'mode': 'REPEATED',
'fields': [{
'name': 'street_address',
'type': 'STRING',
'mode': 'NULLABLE',
}, {
'name': 'city',
'type': 'STRING',
'mode': 'NULLABLE',
}, {
'name': 'state',
'type': 'STRING',
'mode': 'NULLABLE',
}, {
'name': 'zip',
'type': 'STRING',
'mode': 'NULLABLE',
}],
}],
}
client = _Client(self.PROJECT)
job = self._make_one(self.JOB_NAME, self.QUERY, client)
self.assertEqual(job.schema, ())

statistics = job._properties['statistics'] = {}
self.assertEqual(job.schema, ())

query_stats = statistics['query'] = {}
self.assertEqual(job.schema, ())

query_stats['schema'] = schema

self.assertEqual(job.schema, _parse_schema_resource(schema))

def test_num_dml_affected_rows(self):
num_rows = 1234
client = _Client(self.PROJECT)
Expand Down