airflow.providers.common.sql.triggers.sql

Attributes

NO_ROWS_MESSAGE

Classes

SQLExecuteQueryTrigger

A trigger that executes SQL code in async mode.

SqlSensorTrigger

Poll a SQL query until the first cell of the first row is truthy.

Module Contents

airflow.providers.common.sql.triggers.sql.NO_ROWS_MESSAGE = 'No rows returned, raising as per fail_on_empty flag'[source]
class airflow.providers.common.sql.triggers.sql.SQLExecuteQueryTrigger(sql, conn_id, hook_params=None, parameters=None, **kwargs)[source]

Bases: airflow.triggers.base.BaseTrigger

A trigger that executes SQL code in async mode.

Parameters:
  • sql (str | list[str]) – the sql statement to be executed (str) or a list of sql statements to execute

  • conn_id (str) – the connection ID used to connect to the database

  • hook_params (dict | None) – hook parameters

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

sql[source]
conn_id[source]
hook_params = None[source]
parameters = None[source]
serialize()[source]

Serialize the SQLExecuteQueryTrigger arguments and classpath.

get_hook()[source]

Return DbApiHook.

Returns:

DbApiHook for this connection

Return type:

airflow.providers.common.sql.hooks.sql.DbApiHook

async run()[source]

Run the trigger in an asynchronous context.

The trigger should yield an Event whenever it wants to fire off an event, and return None if it is finished. Single-event triggers should thus yield and then immediately return.

If it yields, it is likely that it will be resumed very quickly, but it may not be (e.g. if the workload is being moved to another triggerer process, or a multi-event trigger was being used for a single-event task defer).

In either case, Trigger classes should assume they will be persisted, and then rely on cleanup() being called when they are no longer needed.

class airflow.providers.common.sql.triggers.sql.SqlSensorTrigger(*, sql, conn_id, poke_interval, parameters=None, hook_params=None, fail_on_empty=False, **kwargs)[source]

Bases: SQLExecuteQueryTrigger

Poll a SQL query until the first cell of the first row is truthy.

This is the default SqlSensor condition. The trigger waits poke_interval seconds before each query, because the sensor checks once on the worker before deferring. Each poll, connection lookup included, runs in a thread so a slow query does not block other triggers. The triggerer only serves connection lookups from threads safely on Airflow 2 and on 3.2.2 or later, so SqlSensor only defers there.

An error during a poll ends the wait with a failure event, so the task fails and retries as it would if the same error happened in poke mode.

Parameters:
  • sql (str) – The SQL query to poll.

  • conn_id (str) – The connection ID used to connect to the database.

  • poke_interval (float) – Seconds to wait before each query.

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

  • hook_params (dict | None) – Extra config params to be passed to the underlying hook.

  • fail_on_empty (bool) – Yield a failure event when the query returns no rows.

poke_interval[source]
fail_on_empty = False[source]
serialize()[source]

Serialize the SqlSensorTrigger arguments and classpath.

async run()[source]

Run the trigger in an asynchronous context.

The trigger should yield an Event whenever it wants to fire off an event, and return None if it is finished. Single-event triggers should thus yield and then immediately return.

If it yields, it is likely that it will be resumed very quickly, but it may not be (e.g. if the workload is being moved to another triggerer process, or a multi-event trigger was being used for a single-event task defer).

In either case, Trigger classes should assume they will be persisted, and then rely on cleanup() being called when they are no longer needed.

Was this entry helpful?