Skip to content

Commit d7ad25e

Browse files
jbatswast
authored andcommitted
bigquery: generate row IDs in create_rows (#4173)
If the user doesn't provide row IDs, create unique IDs for them.
1 parent a06b84b commit d7ad25e

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

bigquery/google/cloud/bigquery/client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ def create_rows(self, table, rows, row_ids=None, selected_fields=None,
860860
861861
:type row_ids: list of string
862862
:param row_ids: (Optional) Unique ids, one per row being inserted.
863-
If not passed, no de-duplication occurs.
863+
If omitted, unique IDs are created.
864864
865865
:type selected_fields: list of :class:`SchemaField`
866866
:param selected_fields:
@@ -923,7 +923,8 @@ def create_rows(self, table, rows, row_ids=None, selected_fields=None,
923923
info = {'json': row_info}
924924
if row_ids is not None:
925925
info['insertId'] = row_ids[index]
926-
926+
else:
927+
info['insertId'] = str(uuid.uuid4())
927928
rows_info.append(info)
928929

929930
if skip_invalid_rows is not None:
@@ -935,6 +936,7 @@ def create_rows(self, table, rows, row_ids=None, selected_fields=None,
935936
if template_suffix is not None:
936937
data['templateSuffix'] = template_suffix
937938

939+
# TODO(jba): use self._call_api here after #4148 is merged.
938940
response = self._connection.api_request(
939941
method='POST',
940942
path='%s/insertAll' % table.path,

bigquery/tests/unit/test_client.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,10 +1889,14 @@ def _row_data(row):
18891889
'joined': joined}
18901890

18911891
SENT = {
1892-
'rows': [{'json': _row_data(row)} for row in ROWS],
1892+
'rows': [{
1893+
'json': _row_data(row),
1894+
'insertId': str(i),
1895+
} for i, row in enumerate(ROWS)],
18931896
}
18941897

1895-
errors = client.create_rows(table, ROWS)
1898+
with mock.patch('uuid.uuid4', side_effect=map(str, range(len(ROWS)))):
1899+
errors = client.create_rows(table, ROWS)
18961900

18971901
self.assertEqual(len(errors), 0)
18981902
self.assertEqual(len(conn._requested), 1)
@@ -1950,10 +1954,14 @@ def _row_data(row):
19501954
return row
19511955

19521956
SENT = {
1953-
'rows': [{'json': _row_data(row)} for row in ROWS],
1957+
'rows': [{
1958+
'json': _row_data(row),
1959+
'insertId': str(i),
1960+
} for i, row in enumerate(ROWS)],
19541961
}
19551962

1956-
errors = client.create_rows(table, ROWS)
1963+
with mock.patch('uuid.uuid4', side_effect=map(str, range(len(ROWS)))):
1964+
errors = client.create_rows(table, ROWS)
19571965

19581966
self.assertEqual(len(errors), 0)
19591967
self.assertEqual(len(conn._requested), 1)
@@ -1990,10 +1998,14 @@ def _row_data(row):
19901998
return {'full_name': row[0], 'age': str(row[1])}
19911999

19922000
SENT = {
1993-
'rows': [{'json': _row_data(row)} for row in ROWS],
2001+
'rows': [{
2002+
'json': _row_data(row),
2003+
'insertId': str(i),
2004+
} for i, row in enumerate(ROWS)],
19942005
}
19952006

1996-
errors = client.create_rows(table, ROWS)
2007+
with mock.patch('uuid.uuid4', side_effect=map(str, range(len(ROWS)))):
2008+
errors = client.create_rows(table, ROWS)
19972009

19982010
self.assertEqual(len(errors), 0)
19992011
self.assertEqual(len(conn._requested), 1)
@@ -2095,10 +2107,14 @@ def _row_data(row):
20952107
'struct': row[1]}
20962108

20972109
SENT = {
2098-
'rows': [{'json': _row_data(row)} for row in ROWS],
2110+
'rows': [{
2111+
'json': _row_data(row),
2112+
'insertId': str(i),
2113+
} for i, row in enumerate(ROWS)],
20992114
}
21002115

2101-
errors = client.create_rows(table, ROWS)
2116+
with mock.patch('uuid.uuid4', side_effect=map(str, range(len(ROWS)))):
2117+
errors = client.create_rows(table, ROWS)
21022118

21032119
self.assertEqual(len(errors), 0)
21042120
self.assertEqual(len(conn._requested), 1)
@@ -2138,11 +2154,15 @@ def _row_data(row):
21382154
'phone': row[1]}
21392155

21402156
SENT = {
2141-
'rows': [{'json': _row_data(row)} for row in ROWS],
2157+
'rows': [{
2158+
'json': _row_data(row),
2159+
'insertId': str(i),
2160+
} for i, row in enumerate(ROWS)],
21422161
}
21432162

2144-
errors = client.create_rows(self.TABLE_REF, ROWS,
2145-
selected_fields=[full_name, phone])
2163+
with mock.patch('uuid.uuid4', side_effect=map(str, range(len(ROWS)))):
2164+
errors = client.create_rows(self.TABLE_REF, ROWS,
2165+
selected_fields=[full_name, phone])
21462166

21472167
self.assertEqual(len(errors), 0)
21482168
self.assertEqual(len(conn._requested), 1)

0 commit comments

Comments
 (0)