airflow.contrib.operators.bigquery_check_operator

Module Contents

class airflow.contrib.operators.bigquery_check_operator.BigQueryCheckOperator(sql, bigquery_conn_id='bigquery_default', use_legacy_sql=True, *args, **kwargs)[source]

Bases: airflow.operators.check_operator.CheckOperator

Performs checks against BigQuery. The BigQueryCheckOperator 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.

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

  • bigquery_conn_id (str) – reference to the BigQuery database

  • use_legacy_sql (bool) – Whether to use legacy SQL (true) or standard SQL (false).

template_fields = ['sql'][source]
template_ext = ['.sql'][source]
get_db_hook(self)[source]
class airflow.contrib.operators.bigquery_check_operator.BigQueryValueCheckOperator(sql, pass_value, tolerance=None, bigquery_conn_id='bigquery_default', use_legacy_sql=True, *args, **kwargs)[source]

Bases: airflow.operators.check_operator.ValueCheckOperator

Performs a simple value check using sql code.

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

  • use_legacy_sql (bool) – Whether to use legacy SQL (true) or standard SQL (false).

template_fields = ['sql', 'pass_value'][source]
template_ext = ['.sql'][source]
get_db_hook(self)[source]
class airflow.contrib.operators.bigquery_check_operator.BigQueryIntervalCheckOperator(table, metrics_thresholds, date_filter_column='ds', days_back=-7, bigquery_conn_id='bigquery_default', use_legacy_sql=True, *args, **kwargs)[source]

Bases: airflow.operators.check_operator.IntervalCheckOperator

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

This method constructs a query like so

SELECT {metrics_threshold_dict_key} FROM {table}
WHERE {date_filter_column}=<date>
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

  • metrics_threshold (dict) – a dictionary of ratios indexed by metrics, for example ‘COUNT(*)’: 1.5 would require a 50 percent or less difference between the current day, and the prior days_back.

  • use_legacy_sql (bool) – Whether to use legacy SQL (true) or standard SQL (false).

template_fields = ['table'][source]
get_db_hook(self)[source]