You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2018/08/04 17:03:00 UTC

[jira] [Commented] (AIRFLOW-763) Vertica Check Operator

    [ https://issues.apache.org/jira/browse/AIRFLOW-763?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16569240#comment-16569240 ] 

ASF GitHub Bot commented on AIRFLOW-763:
----------------------------------------

ashb closed pull request #1998: [AIRFLOW-763] Add contrib check operator for Vertica
URL: https://github.com/apache/incubator-airflow/pull/1998
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/airflow/contrib/operators/vertica_check_operator.py b/airflow/contrib/operators/vertica_check_operator.py
new file mode 100644
index 0000000000..1f936cab3c
--- /dev/null
+++ b/airflow/contrib/operators/vertica_check_operator.py
@@ -0,0 +1,125 @@
+# -*- coding: utf-8 -*-
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from airflow.contrib.hooks.vertica_hook import VerticaHook
+from airflow.operators.check_operator import CheckOperator, ValueCheckOperator, IntervalCheckOperator
+from airflow.utils.decorators import apply_defaults
+
+class VerticaCheckOperator(CheckOperator):
+    """
+    Performs checks against Vertica. The ``VerticaCheckOperator`` 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.
+
+    :param sql: the sql to be executed
+    :type sql: string
+    :param vertica_conn_id: reference to the Vertica database
+    :type vertica_conn_id: string
+    """
+
+    @apply_defaults
+    def __init__(
+            self,
+            sql,
+            vertica_conn_id='vertica_default',
+            *args,
+            **kwargs):
+        super(VerticaCheckOperator, self).__init__(sql=sql, *args, **kwargs)
+        self.vertica_conn_id = vertica_conn_id
+        self.sql = sql
+
+    def get_db_hook(self):
+        return VerticaHook(vertica_conn_id=self.vertica_conn_id)
+
+
+class VerticaValueCheckOperator(ValueCheckOperator):
+    """
+    Performs a simple value check using sql code.
+
+    :param sql: the sql to be executed
+    :type sql: string
+    """
+
+    @apply_defaults
+    def __init__(
+            self, sql, pass_value, tolerance=None,
+            vertica_conn_id='vertica_default',
+            *args, **kwargs):
+        super(VerticaValueCheckOperator, self).__init__(
+            sql=sql, pass_value=pass_value, tolerance=tolerance,
+            *args, **kwargs)
+        self.vertica_conn_id = vertica_conn_id
+
+    def get_db_hook(self):
+        return VerticaHook(vertica_conn_id=self.vertica_conn_id)
+
+
+class VerticaIntervalCheckOperator(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>
+
+    :param table: the table name
+    :type table: str
+    :param days_back: number of days between ds and the ds we want to check
+        against. Defaults to 7 days
+    :type days_back: int
+    :param metrics_threshold: 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.
+    :type metrics_threshold: dict
+    """
+
+    @apply_defaults
+    def __init__(
+            self, table, metrics_thresholds,
+            date_filter_column='ds', days_back=-7,
+            vertica_conn_id='vertica_default',
+            *args, **kwargs):
+        super(VerticaIntervalCheckOperator, self).__init__(
+            table=table, metrics_thresholds=metrics_thresholds,
+            date_filter_column=date_filter_column, days_back=days_back,
+            *args, **kwargs)
+        self.vertica_conn_id = vertica_conn_id
+
+    def get_db_hook(self):
+        return VerticaHook(vertica_conn_id=self.vertica_conn_id)
+


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> Vertica Check Operator
> ----------------------
>
>                 Key: AIRFLOW-763
>                 URL: https://issues.apache.org/jira/browse/AIRFLOW-763
>             Project: Apache Airflow
>          Issue Type: New Feature
>            Reporter: Anmol Gautam
>            Assignee: Anmol Gautam
>            Priority: Minor
>
> It will be good to have a Vertica Check Operator extending CheckOperator to handle checks on Vertica SQL queries.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)