airflow.operators.sql

Module Contents

airflow.operators.sql.ALLOWED_CONN_TYPE[source]
class airflow.operators.sql.SQLCheckOperator(sql, conn_id=None, *args, **kwargs)[source]

Bases: airflow.models.BaseOperator

Performs checks against a db. The SQLCheckOperator expects a sql query that will return a single row. Each value on that first row is evaluated using python bool casting. If any of the values return False the check is failed and errors out.

Note that Python bool casting evals the following as False:

  • False

  • 0

  • Empty string ("")

  • Empty list ([])

  • Empty dictionary or set ({})

Given a query like SELECT COUNT(*) FROM foo, it will fail only if the count == 0. You can craft much more complex query that could, for instance, check that the table has the same number of rows as the source table upstream, or that the count of today’s partition is greater than yesterday’s partition, or that a set of metrics are less than 3 standard deviation for the 7 day average.

This operator can be used as a data quality check in your pipeline, and depending on where you put it in your DAG, you have the choice to stop the critical path, preventing from publishing dubious data, or on the side and receive email alerts without stopping the progress of the DAG.

Note that this is an abstract class and get_db_hook needs to be defined. Whereas a get_db_hook is hook that gets a single record from an external source.

Parameters

sql (str) – the sql to be executed. (templated)

template_fields :Iterable[str] = ['sql'][source]
template_ext :Iterable[str] = ['.hql', '.sql'][source]
ui_color = #fff7e6[source]
execute(self, context=None)[source]
get_db_hook(self)[source]

Get the database hook for the connection.

Returns

the database hook object.

Return type

DbApiHook

airflow.operators.sql._convert_to_float_if_possible(s)[source]
A small helper function to convert a string to a numeric value
if appropriate
Parameters

s (str) – the string to be converted

class airflow.operators.sql.SQLValueCheckOperator(sql, pass_value, tolerance=None, conn_id=None, *args, **kwargs)[source]

Bases: airflow.models.BaseOperator

Performs a simple value check using sql code.

Note that this is an abstract class and get_db_hook needs to be defined. Whereas a get_db_hook is hook that gets a single record from an external source.

Parameters

sql (str) – the sql to be executed. (templated)

__mapper_args__[source]
template_fields :Iterable[str] = ['sql', 'pass_value'][source]
template_ext :Iterable[str] = ['.hql', '.sql'][source]
ui_color = #fff7e6[source]
execute(self, context=None)[source]
_to_float(self, records)[source]
_get_string_matches(self, records, pass_value_conv)[source]
_get_numeric_matches(self, numeric_records, numeric_pass_value_conv)[source]
get_db_hook(self)[source]

Get the database hook for the connection.

Returns

the database hook object.

Return type

DbApiHook

class airflow.operators.sql.SQLIntervalCheckOperator(table, metrics_thresholds, date_filter_column='ds', days_back=- 7, ratio_formula='max_over_min', ignore_zero=True, conn_id=None, *args, **kwargs)[source]

Bases: airflow.models.BaseOperator

Checks that the values of metrics given as SQL expressions are within a certain tolerance of the ones from days_back before.

Note that this is an abstract class and get_db_hook needs to be defined. Whereas a get_db_hook is hook that gets a single record from an external source.

Parameters
  • table (str) – the table name

  • days_back (int) – number of days between ds and the ds we want to check against. Defaults to 7 days

  • ratio_formula (str) –

    which formula to use to compute the ratio between the two metrics. Assuming cur is the metric of today and ref is the metric to today - days_back.

    max_over_min: computes max(cur, ref) / min(cur, ref) relative_diff: computes abs(cur-ref) / ref

    Default: ‘max_over_min’

  • ignore_zero (bool) – whether we should ignore zero metrics

  • metrics_threshold (dict) – a dictionary of ratios indexed by metrics

__mapper_args__[source]
template_fields :Iterable[str] = ['sql1', 'sql2'][source]
template_ext :Iterable[str] = ['.hql', '.sql'][source]
ui_color = #fff7e6[source]
ratio_formulas[source]
execute(self, context=None)[source]
get_db_hook(self)[source]

Get the database hook for the connection.

Returns

the database hook object.

Return type

DbApiHook

class airflow.operators.sql.SQLThresholdCheckOperator(sql, min_threshold, max_threshold, conn_id=None, *args, **kwargs)[source]

Bases: airflow.models.BaseOperator

Performs a value check using sql code against a mininmum threshold and a maximum threshold. Thresholds can be in the form of a numeric value OR a sql statement that results a numeric.

Note that this is an abstract class and get_db_hook needs to be defined. Whereas a get_db_hook is hook that gets a single record from an external source.

Parameters
  • sql (str) – the sql to be executed. (templated)

  • min_threshold (numeric or str) – numerical value or min threshold sql to be executed (templated)

  • max_threshold (numeric or str) – numerical value or max threshold sql to be executed (templated)

template_fields :Iterable[str] = ['sql', 'min_threshold', 'max_threshold'][source]
template_ext :Iterable[str] = ['.hql', '.sql'][source]
execute(self, context=None)[source]
push(self, meta_data)[source]

Optional: Send data check info and metadata to an external database. Default functionality will log metadata.

get_db_hook(self)[source]

Returns DB hook

class airflow.operators.sql.BranchSQLOperator(sql, follow_task_ids_if_true, follow_task_ids_if_false, conn_id='default_conn_id', database=None, parameters=None, *args, **kwargs)[source]

Bases: airflow.models.BaseOperator, airflow.models.SkipMixin

Executes sql code in a specific database

Parameters
  • sql (Can receive a str representing a sql statement or reference to a template file. Template reference are recognized by str ending in '.sql'. Expected SQL query to return Boolean (True/False), integer (0 = False, Otherwise = 1) or string (true/y/yes/1/on/false/n/no/0/off)) – the sql code to be executed. (templated)

  • follow_task_ids_if_true (str or list) – task id or task ids to follow if query return true

  • follow_task_ids_if_false (str or list) – task id or task ids to follow if query return true

  • conn_id (str) – reference to a specific database

  • database – name of database which overwrite defined one in connection

  • parameters (mapping or iterable) – (optional) the parameters to render the SQL query with.

template_fields = ['sql'][source]
template_ext = ['.sql'][source]
ui_color = #a22034[source]
ui_fgcolor = #F7F7F7[source]
_get_hook(self)[source]
execute(self, context)[source]

Was this entry helpful?