Skip to content

Commit e05e50b

Browse files
authored
Add 'QueryJob.referenced_tables' property. (#3801)
1 parent 06ac65e commit e05e50b

2 files changed

Lines changed: 86 additions & 2 deletions

File tree

bigquery/google/cloud/bigquery/job.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,36 @@ def statement_type(self):
13861386
"""
13871387
return self._job_statistics().get('statementType')
13881388

1389+
@property
1390+
def referenced_tables(self):
1391+
"""Return referenced tables from job statistics, if present.
1392+
1393+
See:
1394+
https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#statistics.query.referencedTables
1395+
1396+
:rtype: list of dict
1397+
:returns: mappings describing the query plan, or an empty list
1398+
if the query has not yet completed.
1399+
"""
1400+
tables = []
1401+
client = self._require_client(None)
1402+
datasets_by_project_name = {}
1403+
1404+
for table in self._job_statistics().get('referencedTables', ()):
1405+
1406+
t_project = table['projectId']
1407+
1408+
ds_name = table['datasetId']
1409+
t_dataset = datasets_by_project_name.get((t_project, ds_name))
1410+
if t_dataset is None:
1411+
t_dataset = client.dataset(ds_name, project=t_project)
1412+
datasets_by_project_name[(t_project, ds_name)] = t_dataset
1413+
1414+
t_name = table['tableId']
1415+
tables.append(t_dataset.table(t_name))
1416+
1417+
return tables
1418+
13891419
def query_results(self):
13901420
"""Construct a QueryResults instance, bound to this job.
13911421

bigquery/tests/unit/test_job.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,60 @@ def test_statement_type(self):
17911791
query_stats['statementType'] = statement_type
17921792
self.assertEqual(job.statement_type, statement_type)
17931793

1794+
def test_referenced_tables(self):
1795+
from google.cloud.bigquery.dataset import Dataset
1796+
from google.cloud.bigquery.table import Table
1797+
1798+
ref_tables_resource = [{
1799+
'projectId': self.PROJECT,
1800+
'datasetId': 'dataset',
1801+
'tableId': 'local1',
1802+
}, {
1803+
1804+
'projectId': self.PROJECT,
1805+
'datasetId': 'dataset',
1806+
'tableId': 'local2',
1807+
}, {
1808+
1809+
'projectId': 'other-project-123',
1810+
'datasetId': 'other-dataset',
1811+
'tableId': 'other-table',
1812+
}]
1813+
client = _Client(self.PROJECT)
1814+
job = self._make_one(self.JOB_NAME, self.QUERY, client)
1815+
self.assertEqual(job.referenced_tables, [])
1816+
1817+
statistics = job._properties['statistics'] = {}
1818+
self.assertEqual(job.referenced_tables, [])
1819+
1820+
query_stats = statistics['query'] = {}
1821+
self.assertEqual(job.referenced_tables, [])
1822+
1823+
query_stats['referencedTables'] = ref_tables_resource
1824+
1825+
local1, local2, remote = job.referenced_tables
1826+
1827+
self.assertIsInstance(local1, Table)
1828+
self.assertEqual(local1.name, 'local1')
1829+
self.assertIsInstance(local1._dataset, Dataset)
1830+
self.assertEqual(local1.dataset_name, 'dataset')
1831+
self.assertEqual(local1.project, self.PROJECT)
1832+
self.assertIs(local1._dataset._client, client)
1833+
1834+
self.assertIsInstance(local2, Table)
1835+
self.assertEqual(local2.name, 'local2')
1836+
self.assertIsInstance(local2._dataset, Dataset)
1837+
self.assertEqual(local2.dataset_name, 'dataset')
1838+
self.assertEqual(local2.project, self.PROJECT)
1839+
self.assertIs(local2._dataset._client, client)
1840+
1841+
self.assertIsInstance(remote, Table)
1842+
self.assertEqual(remote.name, 'other-table')
1843+
self.assertIsInstance(remote._dataset, Dataset)
1844+
self.assertEqual(remote.dataset_name, 'other-dataset')
1845+
self.assertEqual(remote.project, 'other-project-123')
1846+
self.assertIs(remote._dataset._client, client)
1847+
17941848
def test_query_results(self):
17951849
from google.cloud.bigquery.query import QueryResults
17961850

@@ -2413,10 +2467,10 @@ def __init__(self, project='project', connection=None):
24132467
self.project = project
24142468
self._connection = connection
24152469

2416-
def dataset(self, name):
2470+
def dataset(self, name, project=None):
24172471
from google.cloud.bigquery.dataset import Dataset
24182472

2419-
return Dataset(name, client=self)
2473+
return Dataset(name, client=self, project=project)
24202474

24212475

24222476
class _Table(object):

0 commit comments

Comments
 (0)