Skip to content

Commit e4df253

Browse files
authored
Merge pull request tylertreat#113 from yu-sa/master
Add the time partitioning
2 parents a48ce36 + 651200e commit e4df253

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

bigquery/client.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,8 @@ def get_table(self, dataset, table):
520520

521521
return table
522522

523-
def create_table(self, dataset, table, schema, expiration_time=None):
523+
def create_table(self, dataset, table, schema,
524+
expiration_time=None, time_partitioning=False):
524525
"""Create a new table in the dataset.
525526
526527
Parameters
@@ -533,6 +534,8 @@ def create_table(self, dataset, table, schema, expiration_time=None):
533534
The table schema
534535
expiration_time : float, optional
535536
The expiry time in milliseconds since the epoch.
537+
time_partitioning : bool, optional
538+
Create a time partitioning.
536539
537540
Returns
538541
-------
@@ -553,6 +556,9 @@ def create_table(self, dataset, table, schema, expiration_time=None):
553556
if expiration_time is not None:
554557
body['expirationTime'] = expiration_time
555558

559+
if time_partitioning:
560+
body['timePartitioning'] = "DAY"
561+
556562
try:
557563
table = self.bigquery.tables().insert(
558564
projectId=self.project_id,

bigquery/tests/test_client.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,7 @@ def setUp(self):
16751675
'datasetId': self.dataset}
16761676
}
16771677
self.expiration_time = 1437513693000
1678+
self.time_partitioning = True
16781679

16791680
def test_table_create_failed(self):
16801681
"""Ensure that if creating the table fails, False is returned,
@@ -1748,6 +1749,27 @@ def test_table_create_body_with_expiration_time(self):
17481749

17491750
self.mock_tables.insert.return_value.execute.assert_called_with()
17501751

1752+
def test_table_create_body_with_time_partitioning(self):
1753+
"""Ensure that if time_partitioning has specified,
1754+
it passed to the body."""
1755+
1756+
self.mock_tables.insert.return_value.execute.side_effect = [{
1757+
'status': 'foo'}, {'status': 'bar'}]
1758+
1759+
self.client.create_table(self.dataset, self.table,
1760+
self.schema,
1761+
time_partitioning=self.time_partitioning)
1762+
1763+
body = self.body.copy()
1764+
body.update({
1765+
'timePartitioning': "DAY"
1766+
})
1767+
1768+
self.mock_tables.insert.assert_called_with(
1769+
projectId=self.project, datasetId=self.dataset, body=body)
1770+
1771+
self.mock_tables.insert.return_value.execute.assert_called_with()
1772+
17511773

17521774
class TestUpdateTable(unittest.TestCase):
17531775

0 commit comments

Comments
 (0)