Skip to content

Commit d017877

Browse files
authored
Merge pull request #128 from cynipe/support-nested-column-for-insert_id
Allow to specify nested column as insertId for push_row
2 parents a3348ed + 3939514 commit d017877

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

bigquery/client.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from hashlib import sha256
77
from io import StringIO
88
from time import sleep, time
9+
from functools import reduce
910

1011
import six
1112
from bigquery.errors import (BigQueryTimeoutException, JobExecutingException,
@@ -1236,7 +1237,8 @@ def push_rows(self, dataset, table, rows, insert_id_key=None,
12361237
rows : list
12371238
A ``list`` of rows (``dict`` objects) to add to the table
12381239
insert_id_key : str, optional
1239-
Key for insertId in row
1240+
Key for insertId in row.
1241+
You can use dot separated key for nested column.
12401242
skip_invalid_rows : bool, optional
12411243
Insert all valid rows of a request, even if invalid rows exist.
12421244
ignore_unknown_values : bool, optional
@@ -1258,8 +1260,11 @@ def push_rows(self, dataset, table, rows, insert_id_key=None,
12581260
for row in rows:
12591261
each_row = {}
12601262
each_row["json"] = row
1261-
if insert_id_key in row:
1262-
each_row["insertId"] = row[insert_id_key]
1263+
if insert_id_key is not None:
1264+
keys = insert_id_key.split('.')
1265+
val = reduce(lambda d, key: d.get(key) if d else None, keys, row)
1266+
if val is not None:
1267+
each_row["insertId"] = val
12631268
rows_data.append(each_row)
12641269

12651270
data = {

bigquery/tests/test_client.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2325,6 +2325,52 @@ def test_request_data_with_options(self):
23252325
tableId=self.table,
23262326
body=expected_body)
23272327

2328+
def test_insert_id_key_with_nested_column(self):
2329+
"""Ensure that dot separated insert_id_key properly extracted with nested column value."""
2330+
rows = [
2331+
{'nested': {'col': 'nested_col1'}, 'val': 1},
2332+
{'nested': {'col': 'nested_col2'}, 'val': 2},
2333+
]
2334+
expected_body = self.data.copy()
2335+
expected_body['rows'] = [
2336+
{'insertId': 'nested_col1', 'json': {'nested': {'col': 'nested_col1'}, 'val': 1}},
2337+
{'insertId': 'nested_col2', 'json': {'nested': {'col': 'nested_col2'}, 'val': 2}},
2338+
]
2339+
2340+
self.client.push_rows(self.dataset, self.table, rows,
2341+
insert_id_key='nested.col')
2342+
self.mock_table_data.insertAll.assert_called_with(
2343+
projectId=self.project,
2344+
datasetId=self.dataset,
2345+
tableId=self.table,
2346+
body=expected_body)
2347+
2348+
expected_body = self.data.copy()
2349+
expected_body['rows'] = [
2350+
{'insertId': 1, 'json': {'nested': {'col': 'nested_col1'}, 'val': 1}},
2351+
{'insertId': 2, 'json': {'nested': {'col': 'nested_col2'}, 'val': 2}},
2352+
]
2353+
self.client.push_rows(self.dataset, self.table, rows,
2354+
insert_id_key='val')
2355+
self.mock_table_data.insertAll.assert_called_with(
2356+
projectId=self.project,
2357+
datasetId=self.dataset,
2358+
tableId=self.table,
2359+
body=expected_body)
2360+
2361+
expected_body = self.data.copy()
2362+
expected_body['rows'] = [
2363+
{'json': {'nested': {'col': 'nested_col1'}, 'val': 1}},
2364+
{'json': {'nested': {'col': 'nested_col2'}, 'val': 2}},
2365+
]
2366+
self.client.push_rows(self.dataset, self.table, rows,
2367+
insert_id_key='no_such.column')
2368+
self.mock_table_data.insertAll.assert_called_with(
2369+
projectId=self.project,
2370+
datasetId=self.dataset,
2371+
tableId=self.table,
2372+
body=expected_body)
2373+
23282374

23292375
class TestGetAllTables(unittest.TestCase):
23302376

0 commit comments

Comments
 (0)