Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bigquery.py): pass correct project_id to triggerer #35200

Merged
merged 16 commits into from
Dec 17, 2023
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
13 changes: 7 additions & 6 deletions airflow/providers/google/cloud/operators/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2792,6 +2792,8 @@ def execute(self, context: Any):
impersonation_chain=self.impersonation_chain,
)
self.hook = hook
if self.project_id is None:
self.project_id = hook.project_id
Comment on lines +2795 to +2796
Copy link
Member

Choose a reason for hiding this comment

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

to be consistent with L2852, although either is fine

Suggested change
if self.project_id is None:
self.project_id = hook.project_id
self.project_id = self.project_id or self.hook.project_id

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would remove all references that do project_id = self.project_id or self.hook.project_id since this is what caused the whole issue in the first place, IMO.

The style I'm following is the one used for most hooks assignments when mutating the object on execution


self.job_id = hook.generate_job_id(
job_id=self.job_id,
Expand Down Expand Up @@ -2831,8 +2833,7 @@ def execute(self, context: Any):
QueryJob._JOB_TYPE: ["destinationTable"],
}

project_id = self.project_id or hook.project_id
if project_id:
if self.project_id:
for job_type, tables_prop in job_types.items():
job_configuration = job.to_api_repr()["configuration"]
if job_type in job_configuration:
Expand All @@ -2842,7 +2843,7 @@ def execute(self, context: Any):
persist_kwargs = {
"context": context,
"task_instance": self,
"project_id": project_id,
"project_id": self.project_id,
"table_id": table,
}
if not isinstance(table, str):
Expand All @@ -2851,11 +2852,11 @@ def execute(self, context: Any):
persist_kwargs["project_id"] = table["projectId"]
BigQueryTableLink.persist(**persist_kwargs)
self.job_id = job.job_id
project_id = self.project_id or self.hook.project_id
if project_id:

mokshasoul marked this conversation as resolved.
Show resolved Hide resolved
if self.project_id:
job_id_path = convert_job_id(
job_id=self.job_id, # type: ignore[arg-type]
project_id=project_id,
project_id=self.project_id,
location=self.location,
)
context["ti"].xcom_push(key="job_id_path", value=job_id_path)
Expand Down
39 changes: 39 additions & 0 deletions tests/providers/google/cloud/operators/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,45 @@ def test_bigquery_insert_job_operator_async(self, mock_hook, create_task_instanc
exc.value.trigger, BigQueryInsertJobTrigger
), "Trigger is not a BigQueryInsertJobTrigger"

@mock.patch("airflow.providers.google.cloud.operators.bigquery.BigQueryHook")
def test_bigquery_insert_job_operator_async_inherits_hook_project_id_when_non_given(
self, mock_hook, create_task_instance_of_operator
):
"""
Asserts that a deferred task of type BigQueryInsertJobTrigger will assume the project_id
of the hook that is used within the BigQueryInsertJobOperator when there is no
project_id passed to the BigQueryInsertJobOperator.
"""
job_id = "123456"

configuration = {
"query": {
"query": "SELECT * FROM any",
"useLegacySql": False,
}
}
mock_hook.return_value.project_id = TEST_GCP_PROJECT_ID

ti = create_task_instance_of_operator(
BigQueryInsertJobOperator,
dag_id="dag_id",
task_id="insert_query_job",
configuration=configuration,
location=TEST_DATASET_LOCATION,
job_id=job_id,
deferrable=True,
project_id=None,
)

with pytest.raises(TaskDeferred) as exc:
ti.task.execute(MagicMock())

assert isinstance(
exc.value.trigger, BigQueryInsertJobTrigger
), "Trigger is not a BigQueryInsertJobTrigger"

assert exc.value.trigger.project_id == TEST_GCP_PROJECT_ID

def test_bigquery_insert_job_operator_execute_failure(self):
"""Tests that an AirflowException is raised in case of error event"""
configuration = {
Expand Down