You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/02/03 22:29:11 UTC

[GitHub] [airflow] mik-laj commented on a change in pull request #7353: [AIRFLOW-6685] Data Quality Check operators

mik-laj commented on a change in pull request #7353: [AIRFLOW-6685] Data Quality Check operators
URL: https://github.com/apache/airflow/pull/7353#discussion_r374378103
 
 

 ##########
 File path: airflow/operators/data_quality_check.py
 ##########
 @@ -0,0 +1,207 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.utils.decorators import apply_defaults
+from airflow.models import BaseOperator
+from airflow.hooks.base_hook import BaseHook
+from airflow.hooks.postgres_hook import PostgresHook
+from airflow.hooks.mysql_hook import MySqlHook
+from airflow.hooks.hive_hooks import HiveServer2Hook
+from airflow import AirflowException
+
+class BaseDataQualityOperator(BaseOperator):
+    """
+    BaseDataQualityOperator is an abstract base operator class to
+    perform data quality checks
+
+    :param sql: sql (or path to sql) code to be executed
+    :type sql: str
+    :param conn_type: database type
+    :type conn_type: str
+    :param conn_id: connection id of database
+    :type conn_id: str
+    :param push_conn_type: (optional) external database type
+    :type push_conn_type: str
+    :param push_conn_id: (optional) connection id of external database
+    :type push_conn_id: str
+    :param check_description: (optional) description of data quality sql statement
+    :type check_description: str
+    """
+
+    template_fields = ('sql',)
+    template_ext = ('.sql',)
+
+    @apply_defaults
+    def __init__(self,
+                 sql,
+                 conn_id,
+                 push_conn_id=None,
+                 check_description=None,
+                 *args,
+                 **kwargs
+                 ):
+        super().__init__(*args, **kwargs)
+        self.conn_id = conn_id
+        self.push_conn_id = push_conn_id
+        self.sql = sql
+        self.check_description = check_description
+
+    def execute(self, context):
+        """Method where data quality check is performed """
+        raise NotImplementedError
+
+    def push(self, info_dict):
+        """Send data check info and metadata to an external database."""
+        pass
+
+    def send_failure_notification(self, info_dict):
+        """
+        send_failure_notification will throw an AirflowException with logging
+        information and dq check results from the failed task that was just run.
+        """
+        body = f"""Data Quality Check: "{info_dict.get("task_id")}" failed.
+DAG: {self.dag_id}
+Task_id: {info_dict.get("task_id")}
+Check description: {info_dict.get("description")}
+Execution date: {info_dict.get("execution_date")}
+SQL: {self.sql}
+Result: {round(info_dict.get("result"), 2)} is not within thresholds {info_dict.get("min_threshold")} and {info_dict.get("max_threshold")}"""
+        raise AirflowException(body)
+
+    def _get_hook(self, conn_id):
+        """
+        _get_hook is a helper function for get_sql_value. Returns a database
+        hook depending on the conn_type and conn_id specified. Method will raise
+        an exception if hook is not supported.
+        """
+
+        conn_type = BaseHook.get_connection(conn_id).conn_type
 
 Review comment:
   Why didn't you use the Connection.get_hook method?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to 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


With regards,
Apache Git Services