Skip to content

Commit

Permalink
Fix BigQueryInsertJobOperator error handling in deferrable mode (#3…
Browse files Browse the repository at this point in the history
…2034)

* Call job error handler when job not deferred

* Add test
  • Loading branch information
phanikumv authored Jun 20, 2023
1 parent d136457 commit 43fa157
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions airflow/providers/google/cloud/operators/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2756,6 +2756,7 @@ def execute(self, context: Any):
method_name="execute_complete",
)
self.log.info("Current state of job %s is %s", job.job_id, job.state)
self._handle_job_error(job)

def execute_complete(self, context: Context, event: dict[str, Any]):
"""
Expand Down
30 changes: 30 additions & 0 deletions tests/providers/google/cloud/operators/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,36 @@ def test_bigquery_insert_job_operator_async_finish_before_deferred(self, mock_ho
assert not mock_defer.called
assert "Current state of job" in caplog.text

@mock.patch("airflow.providers.google.cloud.operators.bigquery.BigQueryInsertJobOperator.defer")
@mock.patch("airflow.providers.google.cloud.operators.bigquery.BigQueryHook")
def test_bigquery_insert_job_operator_async_error_before_deferred(self, mock_hook, mock_defer, caplog):
job_id = "123456"
hash_ = "hash"
real_job_id = f"{job_id}_{hash_}"

configuration = {
"query": {
"query": "SELECT * FROM any",
"useLegacySql": False,
}
}
mock_hook.return_value.insert_job.return_value = MagicMock(job_id=real_job_id, error_result=True)
mock_hook.return_value.insert_job.return_value.running.return_value = False

op = BigQueryInsertJobOperator(
task_id="insert_query_job",
configuration=configuration,
location=TEST_DATASET_LOCATION,
job_id=job_id,
project_id=TEST_GCP_PROJECT_ID,
deferrable=True,
)

with pytest.raises(AirflowException) as exc:
op.execute(MagicMock())

assert str(exc.value) == f"BigQuery job {real_job_id} failed: True"

@mock.patch("airflow.providers.google.cloud.operators.bigquery.BigQueryHook")
def test_bigquery_insert_job_operator_async(self, mock_hook, create_task_instance_of_operator):
"""
Expand Down

0 comments on commit 43fa157

Please sign in to comment.