Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion sdks/python/apache_beam/io/gcp/bigquery_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,10 @@ def _insert_all_rows(
service_call_metric.call('ok')
else:
for insert_error in errors:
service_call_metric.call(insert_error['errors'][0])
# Record the BigQuery reason (e.g. 'invalid') of the first error for

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe keep the explanation in commit message?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave the comment content about the behavior (specifically recording the first error) but the extended reasoning is unnecessary, removed

# each failed row.
row_errors = insert_error.get('errors') or [{}]
service_call_metric.call(row_errors[0].get('reason') or 'unknown')
except (ClientError, GoogleAPICallError) as e:
# e.code contains the numeric http status code.
service_call_metric.call(e.code)
Expand Down
30 changes: 30 additions & 0 deletions sdks/python/apache_beam/io/gcp/bigquery_tools_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,36 @@ def test_insert_rows_sets_metric_on_failure(self):
self.verify_write_call_metric(
"my_project", "my_dataset", "my_table", "ok", 1)

@unittest.skipIf(ClientError is None, 'GCP dependencies are not installed')
def test_insert_rows_sets_metric_on_row_errors(self):
MetricsEnvironment.process_wide_container().reset()
client = mock.Mock()

def row_error(index, *reasons):
return {
'index': index,
'errors': [{
'reason': r, 'message': 'msg'
} for r in reasons],
}

client.insert_rows_json.return_value = [
row_error(0, 'invalid', 'stopped'),
row_error(1, 'invalid'),
row_error(2, 'stopped'),
]
wrapper = beam.io.gcp.bigquery_tools.BigQueryWrapper(client)
success, errors = wrapper.insert_rows(
"my_project", "my_dataset", "my_table", [{'a': 1}] * 3)

self.assertFalse(success)
self.assertEqual(client.insert_rows_json.return_value, errors)
# One metric per failed row, labelled with the reason of its first error.
self.verify_write_call_metric(
"my_project", "my_dataset", "my_table", "invalid", 2)
self.verify_write_call_metric(
"my_project", "my_dataset", "my_table", "stopped", 1)

@unittest.skipIf(ClientError is None, 'GCP dependencies are not installed')
def test_start_query_job_priority_configuration(self):
client = mock.Mock()
Expand Down
Loading