Skip to content

Commit 0f91d2f

Browse files
committed
Merge pull request tylertreat#98 from ruxandraburtica/97-all-tables-for-dataset
Added get_all_tables method
2 parents 97a55bc + 76bb8c2 commit 0f91d2f

File tree

2 files changed

+88
-17
lines changed

2 files changed

+88
-17
lines changed

bigquery/client.py

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,8 +1249,31 @@ def push_rows(self, dataset, table, rows, insert_id_key=None,
12491249
}]
12501250
}
12511251

1252+
def get_all_tables(self, dataset_id):
1253+
"""Retrieve a list of tables for the dataset.
1254+
1255+
Parameters
1256+
----------
1257+
dataset_id : str
1258+
The dataset to retrieve table data for.
1259+
1260+
Returns
1261+
-------
1262+
A ``list`` with all table names
1263+
"""
1264+
tables_data = self._get_all_tables_for_dataset(dataset_id)
1265+
1266+
tables = []
1267+
for table in tables_data['tables']:
1268+
table_name = table.get('tableReference', {}).get('tableId')
1269+
if table_name:
1270+
tables.append(table_name)
1271+
return tables
1272+
12521273
def _get_all_tables(self, dataset_id, cache=False):
1253-
"""Retrieve a list of all tables for the dataset.
1274+
"""Retrieve the list of tables for dataset, that respect the formats:
1275+
* appid_YYYY_MM
1276+
* YYYY_MM_appid
12541277
12551278
Parameters
12561279
----------
@@ -1272,23 +1295,39 @@ def _get_all_tables(self, dataset_id, cache=False):
12721295
do_fetch = False
12731296

12741297
if do_fetch:
1275-
result = self.bigquery.tables().list(
1276-
projectId=self.project_id,
1277-
datasetId=dataset_id).execute()
1278-
1279-
page_token = result.get('nextPageToken')
1280-
while page_token:
1281-
res = self.bigquery.tables().list(
1282-
projectId=self.project_id,
1283-
datasetId=dataset_id,
1284-
pageToken=page_token
1285-
).execute()
1286-
page_token = res.get('nextPageToken')
1287-
result['tables'] += res.get('tables', [])
1298+
result = self._get_all_tables_for_dataset(dataset_id)
12881299
self.cache[dataset_id] = (datetime.now(), result)
12891300

12901301
return self._parse_table_list_response(result)
12911302

1303+
def _get_all_tables_for_dataset(self, dataset_id):
1304+
"""Retrieve a list of all tables for the dataset.
1305+
1306+
Parameters
1307+
----------
1308+
dataset_id : str
1309+
The dataset to retrieve table names for
1310+
1311+
Returns
1312+
-------
1313+
dict
1314+
A ``dict`` containing tables key with all tables
1315+
"""
1316+
result = self.bigquery.tables().list(
1317+
projectId=self.project_id,
1318+
datasetId=dataset_id).execute()
1319+
1320+
page_token = result.get('nextPageToken')
1321+
while page_token:
1322+
res = self.bigquery.tables().list(
1323+
projectId=self.project_id,
1324+
datasetId=dataset_id,
1325+
pageToken=page_token
1326+
).execute()
1327+
page_token = res.get('nextPageToken')
1328+
result['tables'] += res.get('tables', [])
1329+
return result
1330+
12921331
def _parse_table_list_response(self, list_response):
12931332
"""Parse the response received from calling list on tables.
12941333

bigquery/tests/test_client.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,12 +1290,21 @@ def test_not_inside_range(self):
12901290
"tableId": "appspot_6_2013_06"
12911291
}
12921292
},
1293+
{
1294+
"kind": "bigquery#table",
1295+
"id": "project:dataset.table_not_matching_naming",
1296+
"tableReference": {
1297+
"projectId": "project",
1298+
"datasetId": "dataset",
1299+
"tableId": "table_not_matching_naming"
1300+
}
1301+
},
12931302
{
12941303
"kind": "bigquery#table",
12951304
"id": "bad table data"
1296-
}
1305+
},
12971306
],
1298-
"totalItems": 8
1307+
"totalItems": 9
12991308
}
13001309

13011310

@@ -2191,7 +2200,7 @@ def test_request_data_with_options(self):
21912200

21922201
class TestGetAllTables(unittest.TestCase):
21932202

2194-
def test_get_tables(self):
2203+
def test_get_all_tables(self):
21952204
"""Ensure get_all_tables fetches table names from BigQuery."""
21962205

21972206
mock_execute = mock.Mock()
@@ -2205,6 +2214,29 @@ def test_get_tables(self):
22052214

22062215
bq = client.BigQueryClient(mock_bq_service, 'project')
22072216

2217+
expected_result = [
2218+
'2013_05_appspot', '2013_06_appspot_1', '2013_06_appspot_2',
2219+
'2013_06_appspot_3', '2013_06_appspot_4', '2013_06_appspot_5',
2220+
'appspot_6_2013_06', 'table_not_matching_naming'
2221+
]
2222+
2223+
tables = bq.get_all_tables('dataset')
2224+
self.assertEquals(expected_result, tables)
2225+
2226+
def test_get_tables(self):
2227+
"""Ensure _get_all_tables fetches table names from BigQuery."""
2228+
2229+
mock_execute = mock.Mock()
2230+
mock_execute.execute.return_value = FULL_TABLE_LIST_RESPONSE
2231+
2232+
mock_tables = mock.Mock()
2233+
mock_tables.list.return_value = mock_execute
2234+
2235+
mock_bq_service = mock.Mock()
2236+
mock_bq_service.tables.return_value = mock_tables
2237+
2238+
bq = client.BigQueryClient(mock_bq_service, 'project')
2239+
22082240
expected_result = {
22092241
'appspot-3': {'2013_06_appspot_3': 1370044800},
22102242
'appspot-2': {'2013_06_appspot_2': 1370044800},

0 commit comments

Comments
 (0)