airflow.providers.common.sql.sensors.sql

Exceptions

SqlSensorTriggerError

Raised when the deferred SqlSensor trigger reports a failure.

Classes

SqlSensor

Run a SQL statement repeatedly until a criteria is met.

Module Contents

exception airflow.providers.common.sql.sensors.sql.SqlSensorTriggerError[source]

Bases: airflow.providers.common.compat.sdk.AirflowException

Raised when the deferred SqlSensor trigger reports a failure.

class airflow.providers.common.sql.sensors.sql.SqlSensor(*, conn_id, sql, parameters=None, success=None, failure=None, selector=_first_cell, fail_on_empty=False, hook_params=None, deferrable=conf.getboolean('operators', 'default_deferrable', fallback=False), **kwargs)[source]

Bases: airflow.providers.common.compat.sdk.BaseSensorOperator

Run a SQL statement repeatedly until a criteria is met.

This will keep trying until success or failure criteria are met, or if the first cell is not either 0, '0', '', or None. Optional success and failure callables are called with the first cell returned as the argument.

If success callable is defined, the sensor will keep retrying until the criteria is met. If failure callable is defined, and the criteria is met, the sensor will raise AirflowException. Failure criteria is evaluated before success criteria. A fail_on_empty boolean can also be passed to the sensor in which case it will fail if no rows have been returned.

Parameters:
  • conn_id (str) – The connection to run the sensor against

  • sql (str) – The SQL to run. To pass, it needs to return at least one cell that contains a non-zero / empty string value.

  • parameters (collections.abc.Mapping[str, Any] | None) – The parameters to render the SQL query with (optional).

  • success (collections.abc.Callable[[Any], bool] | None) – Success criteria for the sensor is a Callable that takes the output of selector as the only argument, and returns a boolean (optional).

  • failure (collections.abc.Callable[[Any], bool] | None) – Failure criteria for the sensor is a Callable that takes the output of selector as the only argument and returns a boolean (optional).

  • selector (collections.abc.Callable[[tuple[Any]], Any]) – Function which takes the resulting row and transforms it before it is passed to success or failure (optional). Takes the first cell by default.

  • fail_on_empty (bool) – Explicitly fail on no rows returned.

  • hook_params (collections.abc.Mapping[str, Any] | None) – Extra config params to be passed to the underlying hook. Should match the desired hook constructor params.

  • deferrable (bool) – Wait in the triggerer instead of on a worker. The triggerer only checks the default condition (the first cell is truthy), so the sensor logs a warning and runs in its usual mode instead when success, failure, selector, silent_fail, never_fail or exponential_backoff is set, when a subclass overrides poke or _get_hook, on Airflow 3 before 3.2.2, or on Airflow 2 when parameters holds values JSON cannot keep, such as dates.

template_fields: collections.abc.Sequence[str] = ('sql', 'hook_params', 'parameters', 'conn_id')[source]
template_ext: collections.abc.Sequence[str] = ('.hql', '.sql')[source]
ui_color = '#7c7287'[source]
conn_id[source]
sql[source]
parameters = None[source]
success = None[source]
failure = None[source]
selector[source]
fail_on_empty = False[source]
hook_params = None[source]
deferrable[source]
poke(context)[source]

Override when deriving this class.

execute(context)[source]

Derive when creating an operator.

The main method to execute the task. Context is the same dictionary used as when rendering jinja templates.

Refer to get_template_context for more context.

execute_complete(context, event)[source]

Was this entry helpful?