Skip to content

Commit 2b43879

Browse files
committed
Merge pull request #90 from hagino3000/add_stream_insert_options
Support ignoreUnknownValues and skipInvalidRows option for insertAll
2 parents a1af7de + 5a9290a commit 2b43879

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

bigquery/client.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,8 @@ def wait_for_job(self, job, interval=5, timeout=60):
11441144

11451145
return job_resource
11461146

1147-
def push_rows(self, dataset, table, rows, insert_id_key=None):
1147+
def push_rows(self, dataset, table, rows, insert_id_key=None,
1148+
skip_invalid_rows=None, ignore_unknown_values=None):
11481149
"""Upload rows to BigQuery table.
11491150
11501151
Parameters
@@ -1157,6 +1158,10 @@ def push_rows(self, dataset, table, rows, insert_id_key=None):
11571158
A ``list`` of rows (``dict`` objects) to add to the table
11581159
insert_id_key : str, optional
11591160
Key for insertId in row
1161+
skip_invalid_rows : bool, optional
1162+
Insert all valid rows of a request, even if invalid rows exist.
1163+
ignore_unknown_values : bool, optional
1164+
Accept rows that contain values that do not match the schema.
11601165
11611166
Returns
11621167
-------
@@ -1180,6 +1185,12 @@ def push_rows(self, dataset, table, rows, insert_id_key=None):
11801185
"rows": rows_data
11811186
}
11821187

1188+
if skip_invalid_rows is not None:
1189+
data['skipInvalidRows'] = skip_invalid_rows
1190+
1191+
if ignore_unknown_values is not None:
1192+
data['ignoreUnknownValues'] = ignore_unknown_values
1193+
11831194
try:
11841195
response = table_data.insertAll(
11851196
projectId=self.project_id,

bigquery/tests/test_client.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,6 +2104,47 @@ def test_push_success(self):
21042104
self.mock_table_data.insertAll.return_value.execute.assert_has_calls(
21052105
execute_calls)
21062106

2107+
def test_request_data_with_options(self):
2108+
"""Ensure that insertAll body has optional property only when
2109+
the optional parameter of push_rows passed.
2110+
"""
2111+
expected_body = self.data.copy()
2112+
2113+
self.client.push_rows(
2114+
self.dataset, self.table, self.rows,
2115+
insert_id_key='one')
2116+
self.mock_table_data.insertAll.assert_called_with(
2117+
projectId=self.project,
2118+
datasetId=self.dataset,
2119+
tableId=self.table,
2120+
body=expected_body)
2121+
2122+
self.client.push_rows(
2123+
self.dataset, self.table, self.rows,
2124+
insert_id_key='one',
2125+
ignore_unknown_values=False,
2126+
skip_invalid_rows=False)
2127+
expected_body['ignoreUnknownValues'] = False
2128+
expected_body['skipInvalidRows'] = False
2129+
self.mock_table_data.insertAll.assert_called_with(
2130+
projectId=self.project,
2131+
datasetId=self.dataset,
2132+
tableId=self.table,
2133+
body=expected_body)
2134+
2135+
self.client.push_rows(
2136+
self.dataset, self.table, self.rows,
2137+
insert_id_key='one',
2138+
ignore_unknown_values=True,
2139+
skip_invalid_rows=True)
2140+
expected_body['ignoreUnknownValues'] = True
2141+
expected_body['skipInvalidRows'] = True
2142+
self.mock_table_data.insertAll.assert_called_with(
2143+
projectId=self.project,
2144+
datasetId=self.dataset,
2145+
tableId=self.table,
2146+
body=expected_body)
2147+
21072148

21082149
class TestGetAllTables(unittest.TestCase):
21092150

0 commit comments

Comments
 (0)