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 BigQueryValueCheckOperator deferrable mode optimisation #34018

Merged
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
10 changes: 6 additions & 4 deletions airflow/providers/common/sql/operators/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,10 +827,7 @@ def __init__(
self.tol = tol if isinstance(tol, float) else None
self.has_tolerance = self.tol is not None

def execute(self, context: Context):
self.log.info("Executing SQL check: %s", self.sql)
records = self.get_db_hook().get_first(self.sql)

def check_value(self, records):
if not records:
self._raise_exception(f"The following query returned zero rows: {self.sql}")

Expand Down Expand Up @@ -862,6 +859,11 @@ def execute(self, context: Context):
if not all(tests):
self._raise_exception(error_msg)

def execute(self, context: Context):
self.log.info("Executing SQL check: %s", self.sql)
records = self.get_db_hook().get_first(self.sql)
self.check_value(records)

def _to_float(self, records):
return [float(record) for record in records]

Expand Down
4 changes: 4 additions & 0 deletions airflow/providers/google/cloud/operators/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,10 @@ def execute(self, context: Context) -> None: # type: ignore[override]
method_name="execute_complete",
)
self._handle_job_error(job)
# job.result() returns a RowIterator. Mypy expects an instance of SupportsNext[Any] for
# the next() call which the RowIterator does not resemble to. Hence, ignore the arg-type error.
records = next(job.result()) # type: ignore[arg-type]
self.check_value(records)
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're checking the values here, can we remove the check_value call in the BigQueryValueCheckTrigger?

hook.value_check(self.sql, self.pass_value, records, self.tolerance)

I'm not really sure why the check_value there wasn't picking up the failed status 🤔 I'd assume that if the check failed it would raise an exception which would then be caught and returned as a failed trigger status:

except Exception as e:
self.log.exception("Exception occurred while checking for query completion")
yield TriggerEvent({"status": "error", "message": str(e)})
return

Copy link
Member Author

@pankajkoti pankajkoti Sep 1, 2023

Choose a reason for hiding this comment

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

I gave the DAG a local run and saw that the task was not getting deferred at all, which means it does not defer after setting deferrrable=True.

This happens because of


The running state of the job evaluates to False and it does not defer. Had it deferred, the trigger would check it well and pass on the execution to execute_complete in the operator.

But in scenarios where it does not defer, I have added this check later to actually achive what the operator is meant to do :)

Sorry I missed explaining this earlier, hope it makes sense now.

And yes, we cannot remove the check from the Trigger code as when it defers it goes to Triggererer where it checks and then returns to execute_complete

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh interesting, nice catch!

self.log.info("Current state of job %s is %s", job.job_id, job.state)

@staticmethod
Expand Down
6 changes: 3 additions & 3 deletions tests/providers/google/cloud/operators/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1919,11 +1919,11 @@ def test_bigquery_value_check_async(self, mock_hook, create_task_instance_of_ope
exc.value.trigger, BigQueryValueCheckTrigger
), "Trigger is not a BigQueryValueCheckTrigger"

@mock.patch("airflow.providers.google.cloud.operators.bigquery.BigQueryValueCheckOperator.execute")
@mock.patch("airflow.providers.google.cloud.operators.bigquery.BigQueryValueCheckOperator.defer")
@mock.patch("airflow.providers.google.cloud.operators.bigquery.BigQueryValueCheckOperator.check_value")
@mock.patch("airflow.providers.google.cloud.operators.bigquery.BigQueryHook")
def test_bigquery_value_check_operator_async_finish_before_deferred(
self, mock_hook, mock_defer, mock_execute, create_task_instance_of_operator
self, mock_hook, mock_check_value, mock_defer, create_task_instance_of_operator
):
job_id = "123456"
hash_ = "hash"
Expand All @@ -1944,7 +1944,7 @@ def test_bigquery_value_check_operator_async_finish_before_deferred(

ti.task.execute(MagicMock())
assert not mock_defer.called
assert mock_execute.called
assert mock_check_value.called

@pytest.mark.parametrize(
"kwargs, expected",
Expand Down