You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "gopidesupavan (via GitHub)" <gi...@apache.org> on 2024/05/30 00:16:58 UTC

[PR] Adding Amazon Glue Data Quality Service [airflow]

gopidesupavan opened a new pull request, #39923:
URL: https://github.com/apache/airflow/pull/39923

   <!--
    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.
    -->
   
   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   
   Adding Amazon Glue Data Quality Service. Doc, Hook, Operator, Sensor, Trigger, Waiter, Unit Test, System Test.
   
   GlueDataQualityOperator: Create ruleset or update ruleset
   
   GlueDataQualityRuleSetEvaluationRunOperator: Execute evaluations on multiple rulesets.
   
   
   
   Sample Dag for creating ruleset and execute evaluation:
   
   ```
   from datetime import datetime
   from airflow import DAG
   from airflow.providers.amazon.aws.operators.glue import GlueDataQualityOperator, \
       GlueDataQualityRuleSetEvaluationRunOperator
   
   with DAG(
       dag_id="example_glue_data_quality",
       schedule="@once",
       start_date=datetime(2021, 1, 1),
       tags=["glue data quality ruleset evaluation"],
       catchup=False,
   ) as dag:
   
       rule_set_name = "test_rule_set"
   
       create_rule_set = GlueDataQualityOperator(
           task_id="create_rule_set",
           name=rule_set_name,
           ruleset='Rules = [ColumnLength "name" between 3 and 14]',
           data_quality_ruleset_kwargs={
               "TargetTable": {
                   "TableName": "test_table",
                   "DatabaseName": "test_default",
               }
           }
       )
   
       start_evaluation_run = GlueDataQualityRuleSetEvaluationRunOperator(
           task_id="start_evaluation_run",
           datasource={
               "GlueTable": {
                    "TableName": "test_table",
                   "DatabaseName": "test_default",
               }
           },
           role="arn:aws:iam::{ACCOUNT_ID}:role/GlueDataQuality",
           rule_set_names=[rule_set_name]
       )
   ```
   
   <img width="1769" alt="image" src="https://github.com/apache/airflow/assets/31437079/c86208eb-867e-42bc-993a-1493d2765cb1">
   
   
   
   <!-- Please keep an empty line above the dashes. -->
   ---
   **^ Add meaningful description above**
   Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
   


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Adding Amazon Glue Data Quality Service [airflow]

Posted by "gopidesupavan (via GitHub)" <gi...@apache.org>.
gopidesupavan commented on code in PR #39923:
URL: https://github.com/apache/airflow/pull/39923#discussion_r1620890355


##########
airflow/providers/amazon/aws/operators/glue.py:
##########
@@ -239,3 +243,253 @@ def on_kill(self):
             )
             if not response["SuccessfulSubmissions"]:
                 self.log.error("Failed to stop AWS Glue Job: %s. Run Id: %s", self.job_name, self._job_run_id)
+
+
+class GlueDataQualityOperator(AwsBaseOperator[GlueDataQualityHook]):
+    """
+    Creates a data quality ruleset with DQDL rules applied to a specified Glue table.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the guide:
+        :ref:`howto/operator:GlueDataQualityOperator`
+
+    :param name: A unique name for the data quality ruleset.
+    :param ruleset: A Data Quality Definition Language (DQDL) ruleset.
+        For more information, see the Glue developer guide.
+    :param description: A description of the data quality ruleset.
+    :param update_rule_set: To update existing ruleset, Set this flag to True. (default: False)
+    :param data_quality_ruleset_kwargs: Extra arguments for RuleSet.
+
+    :param aws_conn_id: The Airflow connection used for AWS credentials.
+        If this is ``None`` or empty then the default boto3 behaviour is used. If
+        running Airflow in a distributed manner and aws_conn_id is None or
+        empty, then default boto3 configuration would be used (and must be
+        maintained on each worker node).
+    :param region_name: AWS region_name. If not specified then the default boto3 behaviour is used.
+    :param verify: Whether or not to verify SSL certificates. See:
+        https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html
+    :param botocore_config: Configuration dictionary (key-values) for botocore client. See:
+        https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html
+    """
+
+    aws_hook_class = GlueDataQualityHook
+    template_fields: Sequence[str] = ("name", "ruleset", "data_quality_ruleset_kwargs")
+
+    template_fields_renderers = {
+        "data_quality_ruleset_kwargs": "json",
+    }
+    ui_color = "#ededed"
+
+    def __init__(
+        self,
+        *,
+        name: str,
+        ruleset: str,
+        description: str = "AWS Glue Data Quality Rule Set With Airflow",
+        update_rule_set: bool = False,
+        data_quality_ruleset_kwargs: dict | None = None,
+        aws_conn_id: str | None = "aws_default",
+        **kwargs,
+    ):
+        super().__init__(**kwargs)
+        self.name = name
+        self.ruleset = ruleset.strip()
+        self.description = description
+        self.update_rule_set = update_rule_set
+        self.data_quality_ruleset_kwargs = data_quality_ruleset_kwargs or {}
+        self.aws_conn_id = aws_conn_id
+
+    def validate_inputs(self) -> None:
+        if not self.ruleset.startswith("Rules") or not self.ruleset.endswith("]"):
+            raise AttributeError("RuleSet must starts with Rules = [ and ends with ]")
+
+        if self.data_quality_ruleset_kwargs.get("TargetTable"):
+            target_table = self.data_quality_ruleset_kwargs["TargetTable"]
+
+            if not target_table.get("TableName") or not target_table.get("DatabaseName"):
+                raise AttributeError("Target table must have DatabaseName and TableName")
+
+    def execute(self, context: Context):
+        self.validate_inputs()
+
+        config = {
+            "Name": self.name,
+            "Ruleset": self.ruleset,
+            "Description": self.description,
+            **self.data_quality_ruleset_kwargs,
+        }
+
+        if self.update_rule_set:
+            self.hook.update_glue_data_quality_ruleset(config)
+            self.log.info("AWS Glue data quality ruleset updated successfully")
+        else:
+            self.hook.create_glue_data_quality_ruleset(config)
+            self.log.info("AWS Glue data quality ruleset created successfully")
+
+
+class GlueDataQualityRuleSetEvaluationRunOperator(AwsBaseOperator[GlueDataQualityHook]):
+    """
+    Once you have a ruleset definition (either recommended or your own), you call this operation to evaluate the ruleset against a data source (Glue table).
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the guide:
+        :ref:`howto/operator:GlueDataQualityRuleSetEvaluationRunOperator`
+
+    :param datasource: The data source (Glue table) associated with this run. (templated)
+    :param role: IAM role supplied for job execution. (templated)
+    :param rule_set_names: A list of ruleset names for evaluation. (templated)
+    :param number_of_workers: The number of G.1X workers to be used in the run. (default: 5)
+    :param timeout: The timeout for a run in minutes. This is the maximum time that a run can consume resources
+        before it is terminated and enters TIMEOUT status. (default: 2,880)
+    :param verify_result_status: Validate all the ruleset rules evaluation run results,
+        If any of the rule status is Fail or Error then an exception is thrown. (default: True)
+    :param show_results: Displays all the ruleset rules evaluation run results. (default: True)
+    :param rule_set_evaluation_run_kwargs: Extra arguments for evaluation run. (templated)
+    :param wait_for_completion: Whether to wait for job to stop. (default: True)
+    :param waiter_delay: Time in seconds to wait between status checks. (default: 60)
+    :param waiter_max_attempts: Maximum number of attempts to check for job completion. (default: 20)
+    :param deferrable: If True, the operator will wait asynchronously for the job to stop.
+        This implies waiting for completion. This mode requires aiobotocore module to be installed.
+        (default: False)
+
+    :param aws_conn_id: The Airflow connection used for AWS credentials.
+        If this is ``None`` or empty then the default boto3 behaviour is used. If
+        running Airflow in a distributed manner and aws_conn_id is None or
+        empty, then default boto3 configuration would be used (and must be
+        maintained on each worker node).
+    :param region_name: AWS region_name. If not specified then the default boto3 behaviour is used.
+    :param verify: Whether or not to verify SSL certificates. See:
+        https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html
+    :param botocore_config: Configuration dictionary (key-values) for botocore client. See:
+        https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html
+    """
+
+    aws_hook_class = GlueDataQualityHook
+
+    template_fields: Sequence[str] = (
+        "datasource",
+        "role",
+        "rule_set_names",
+        "rule_set_evaluation_run_kwargs",
+    )
+
+    template_fields_renderers = {"datasource": "json", "rule_set_evaluation_run_kwargs": "json"}
+
+    ui_color = "#ededed"
+
+    def __init__(
+        self,
+        *,
+        datasource: dict,
+        role: str,
+        rule_set_names: list[str],
+        number_of_workers: int = 5,
+        timeout: int = 2880,
+        verify_result_status: bool = True,
+        show_results: bool = True,
+        rule_set_evaluation_run_kwargs: dict[str, Any] | None = None,
+        wait_for_completion: bool = True,
+        waiter_delay: int = 60,
+        waiter_max_attempts: int = 20,
+        deferrable: bool = conf.getboolean("operators", "default_deferrable", fallback=False),
+        aws_conn_id: str | None = "aws_default",
+        **kwargs,
+    ):
+        super().__init__(**kwargs)
+        self.datasource = datasource
+        self.role = role
+        self.rule_set_names = rule_set_names
+        self.number_of_workers = number_of_workers
+        self.timeout = timeout
+        self.verify_result_status = verify_result_status
+        self.show_results = show_results
+        self.rule_set_evaluation_run_kwargs = rule_set_evaluation_run_kwargs or {}
+        self.wait_for_completion = wait_for_completion
+        self.waiter_delay = waiter_delay
+        self.waiter_max_attempts = waiter_max_attempts
+        self.deferrable = deferrable
+        self.aws_conn_id = aws_conn_id
+
+    def validate_inputs(self) -> None:
+        glue_table = self.datasource.get("GlueTable", {})
+
+        if not glue_table.get("DatabaseName") or not glue_table.get("TableName"):
+            raise AttributeError("DataSource glue table must have DatabaseName and TableName")
+
+        not_found_ruleset = []
+
+        for ruleset_name in self.rule_set_names:
+            if not self.hook.has_data_quality_ruleset(ruleset_name):
+                not_found_ruleset.append(ruleset_name)

Review Comment:
   Thank you @vincbeck , Appreciate your feedback 😄 . I will update changes.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Adding Amazon Glue Data Quality Service [airflow]

Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck merged PR #39923:
URL: https://github.com/apache/airflow/pull/39923


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Adding Amazon Glue Data Quality Service [airflow]

Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on code in PR #39923:
URL: https://github.com/apache/airflow/pull/39923#discussion_r1620805262


##########
airflow/providers/amazon/aws/hooks/glue.py:
##########
@@ -430,3 +431,105 @@ def create_or_update_glue_job(self) -> str | None:
             self.conn.create_job(**config)
 
         return self.job_name
+
+
+class GlueDataQualityHook(AwsBaseHook):
+    """
+    Interact with AWS Glue Data Quality.
+
+    Provide thick wrapper around :external+boto3:py:class:`boto3.client("glue") <Glue.Client>`.
+
+    Additional arguments (such as ``aws_conn_id``) may be specified and
+    are passed down to the underlying AwsBaseHook.
+
+    .. seealso::
+        - :class:`airflow.providers.amazon.aws.hooks.base_aws.AwsBaseHook`
+    """
+
+    def __init__(
+        self,
+        *args,
+        **kwargs,
+    ):
+        kwargs["client_type"] = "glue"
+        super().__init__(*args, **kwargs)
+
+    def has_data_quality_ruleset(self, name: str) -> bool:
+        try:
+            self.conn.get_data_quality_ruleset(Name=name)
+            return True
+        except self.conn.exceptions.EntityNotFoundException:
+            return False
+
+    def update_glue_data_quality_ruleset(self, config: dict[str, Any]) -> None:
+        if self.has_data_quality_ruleset(config["Name"]):
+            self.log.info("Updating AWS Glue data quality ruleset with: %s", config)
+            self.conn.update_data_quality_ruleset(**config)
+        else:
+            raise AirflowException(f"AWS Glue data quality ruleset {config['Name']} not exists to update")
+
+    def create_glue_data_quality_ruleset(self, config: dict[str, Any]) -> None:
+        if not self.has_data_quality_ruleset(config["Name"]):
+            self.log.info("Creating AWS Glue data quality ruleset with: %s", config)
+            self.conn.create_data_quality_ruleset(**config)
+        else:
+            raise AirflowException(
+                f"AWS Glue data quality ruleset {config['Name']} already exists with same name."
+            )
+
+    def display_result(self, result: dict[str, Any]) -> None:

Review Comment:
   Do we want it public? It think it is more a private function no? 



##########
airflow/providers/amazon/aws/operators/glue.py:
##########
@@ -239,3 +243,253 @@ def on_kill(self):
             )
             if not response["SuccessfulSubmissions"]:
                 self.log.error("Failed to stop AWS Glue Job: %s. Run Id: %s", self.job_name, self._job_run_id)
+
+
+class GlueDataQualityOperator(AwsBaseOperator[GlueDataQualityHook]):
+    """
+    Creates a data quality ruleset with DQDL rules applied to a specified Glue table.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the guide:
+        :ref:`howto/operator:GlueDataQualityOperator`
+
+    :param name: A unique name for the data quality ruleset.
+    :param ruleset: A Data Quality Definition Language (DQDL) ruleset.
+        For more information, see the Glue developer guide.
+    :param description: A description of the data quality ruleset.
+    :param update_rule_set: To update existing ruleset, Set this flag to True. (default: False)
+    :param data_quality_ruleset_kwargs: Extra arguments for RuleSet.
+
+    :param aws_conn_id: The Airflow connection used for AWS credentials.
+        If this is ``None`` or empty then the default boto3 behaviour is used. If
+        running Airflow in a distributed manner and aws_conn_id is None or
+        empty, then default boto3 configuration would be used (and must be
+        maintained on each worker node).
+    :param region_name: AWS region_name. If not specified then the default boto3 behaviour is used.
+    :param verify: Whether or not to verify SSL certificates. See:
+        https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html
+    :param botocore_config: Configuration dictionary (key-values) for botocore client. See:
+        https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html
+    """
+
+    aws_hook_class = GlueDataQualityHook
+    template_fields: Sequence[str] = ("name", "ruleset", "data_quality_ruleset_kwargs")

Review Comment:
   ```suggestion
       template_fields: Sequence[str] = ("name", "ruleset", "description", "data_quality_ruleset_kwargs")
   ```



##########
airflow/providers/amazon/aws/hooks/glue.py:
##########
@@ -430,3 +431,105 @@ def create_or_update_glue_job(self) -> str | None:
             self.conn.create_job(**config)
 
         return self.job_name
+
+
+class GlueDataQualityHook(AwsBaseHook):
+    """
+    Interact with AWS Glue Data Quality.
+
+    Provide thick wrapper around :external+boto3:py:class:`boto3.client("glue") <Glue.Client>`.
+
+    Additional arguments (such as ``aws_conn_id``) may be specified and
+    are passed down to the underlying AwsBaseHook.
+
+    .. seealso::
+        - :class:`airflow.providers.amazon.aws.hooks.base_aws.AwsBaseHook`
+    """
+
+    def __init__(
+        self,
+        *args,
+        **kwargs,
+    ):
+        kwargs["client_type"] = "glue"
+        super().__init__(*args, **kwargs)
+
+    def has_data_quality_ruleset(self, name: str) -> bool:
+        try:
+            self.conn.get_data_quality_ruleset(Name=name)
+            return True
+        except self.conn.exceptions.EntityNotFoundException:
+            return False
+
+    def update_glue_data_quality_ruleset(self, config: dict[str, Any]) -> None:
+        if self.has_data_quality_ruleset(config["Name"]):
+            self.log.info("Updating AWS Glue data quality ruleset with: %s", config)
+            self.conn.update_data_quality_ruleset(**config)
+        else:
+            raise AirflowException(f"AWS Glue data quality ruleset {config['Name']} not exists to update")
+
+    def create_glue_data_quality_ruleset(self, config: dict[str, Any]) -> None:
+        if not self.has_data_quality_ruleset(config["Name"]):
+            self.log.info("Creating AWS Glue data quality ruleset with: %s", config)
+            self.conn.create_data_quality_ruleset(**config)
+        else:
+            raise AirflowException(
+                f"AWS Glue data quality ruleset {config['Name']} already exists with same name."
+            )
+
+    def display_result(self, result: dict[str, Any]) -> None:

Review Comment:
   `log_results` would makes more sense as name?



##########
airflow/providers/amazon/aws/hooks/glue.py:
##########
@@ -430,3 +431,105 @@ def create_or_update_glue_job(self) -> str | None:
             self.conn.create_job(**config)
 
         return self.job_name
+
+
+class GlueDataQualityHook(AwsBaseHook):
+    """
+    Interact with AWS Glue Data Quality.
+
+    Provide thick wrapper around :external+boto3:py:class:`boto3.client("glue") <Glue.Client>`.
+
+    Additional arguments (such as ``aws_conn_id``) may be specified and
+    are passed down to the underlying AwsBaseHook.
+
+    .. seealso::
+        - :class:`airflow.providers.amazon.aws.hooks.base_aws.AwsBaseHook`
+    """
+
+    def __init__(
+        self,
+        *args,
+        **kwargs,
+    ):
+        kwargs["client_type"] = "glue"
+        super().__init__(*args, **kwargs)
+
+    def has_data_quality_ruleset(self, name: str) -> bool:
+        try:
+            self.conn.get_data_quality_ruleset(Name=name)
+            return True
+        except self.conn.exceptions.EntityNotFoundException:
+            return False
+
+    def update_glue_data_quality_ruleset(self, config: dict[str, Any]) -> None:
+        if self.has_data_quality_ruleset(config["Name"]):
+            self.log.info("Updating AWS Glue data quality ruleset with: %s", config)
+            self.conn.update_data_quality_ruleset(**config)
+        else:
+            raise AirflowException(f"AWS Glue data quality ruleset {config['Name']} not exists to update")
+
+    def create_glue_data_quality_ruleset(self, config: dict[str, Any]) -> None:
+        if not self.has_data_quality_ruleset(config["Name"]):
+            self.log.info("Creating AWS Glue data quality ruleset with: %s", config)
+            self.conn.create_data_quality_ruleset(**config)
+        else:
+            raise AirflowException(
+                f"AWS Glue data quality ruleset {config['Name']} already exists with same name."
+            )

Review Comment:
   These function are only wrapper around boto3 API, I dont think they are necessary. Plus `update_data_quality_ruleset` and `create_data_quality_ruleset` have specific exception when the ruleset does not exist (for `update_data_quality_ruleset`) and when it already exist (for `create_data_quality_ruleset `). Thus, calling `has_data_quality_ruleset` is not needed



##########
airflow/providers/amazon/aws/operators/glue.py:
##########
@@ -239,3 +243,253 @@ def on_kill(self):
             )
             if not response["SuccessfulSubmissions"]:
                 self.log.error("Failed to stop AWS Glue Job: %s. Run Id: %s", self.job_name, self._job_run_id)
+
+
+class GlueDataQualityOperator(AwsBaseOperator[GlueDataQualityHook]):
+    """
+    Creates a data quality ruleset with DQDL rules applied to a specified Glue table.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the guide:
+        :ref:`howto/operator:GlueDataQualityOperator`
+
+    :param name: A unique name for the data quality ruleset.
+    :param ruleset: A Data Quality Definition Language (DQDL) ruleset.
+        For more information, see the Glue developer guide.
+    :param description: A description of the data quality ruleset.
+    :param update_rule_set: To update existing ruleset, Set this flag to True. (default: False)
+    :param data_quality_ruleset_kwargs: Extra arguments for RuleSet.
+
+    :param aws_conn_id: The Airflow connection used for AWS credentials.
+        If this is ``None`` or empty then the default boto3 behaviour is used. If
+        running Airflow in a distributed manner and aws_conn_id is None or
+        empty, then default boto3 configuration would be used (and must be
+        maintained on each worker node).
+    :param region_name: AWS region_name. If not specified then the default boto3 behaviour is used.
+    :param verify: Whether or not to verify SSL certificates. See:
+        https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html
+    :param botocore_config: Configuration dictionary (key-values) for botocore client. See:
+        https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html
+    """
+
+    aws_hook_class = GlueDataQualityHook
+    template_fields: Sequence[str] = ("name", "ruleset", "data_quality_ruleset_kwargs")
+
+    template_fields_renderers = {
+        "data_quality_ruleset_kwargs": "json",
+    }
+    ui_color = "#ededed"
+
+    def __init__(
+        self,
+        *,
+        name: str,
+        ruleset: str,
+        description: str = "AWS Glue Data Quality Rule Set With Airflow",
+        update_rule_set: bool = False,
+        data_quality_ruleset_kwargs: dict | None = None,
+        aws_conn_id: str | None = "aws_default",
+        **kwargs,
+    ):
+        super().__init__(**kwargs)
+        self.name = name
+        self.ruleset = ruleset.strip()
+        self.description = description
+        self.update_rule_set = update_rule_set
+        self.data_quality_ruleset_kwargs = data_quality_ruleset_kwargs or {}
+        self.aws_conn_id = aws_conn_id
+
+    def validate_inputs(self) -> None:
+        if not self.ruleset.startswith("Rules") or not self.ruleset.endswith("]"):
+            raise AttributeError("RuleSet must starts with Rules = [ and ends with ]")
+
+        if self.data_quality_ruleset_kwargs.get("TargetTable"):
+            target_table = self.data_quality_ruleset_kwargs["TargetTable"]
+
+            if not target_table.get("TableName") or not target_table.get("DatabaseName"):
+                raise AttributeError("Target table must have DatabaseName and TableName")
+
+    def execute(self, context: Context):
+        self.validate_inputs()
+
+        config = {
+            "Name": self.name,
+            "Ruleset": self.ruleset,
+            "Description": self.description,
+            **self.data_quality_ruleset_kwargs,
+        }
+
+        if self.update_rule_set:
+            self.hook.update_glue_data_quality_ruleset(config)
+            self.log.info("AWS Glue data quality ruleset updated successfully")
+        else:
+            self.hook.create_glue_data_quality_ruleset(config)
+            self.log.info("AWS Glue data quality ruleset created successfully")
+
+
+class GlueDataQualityRuleSetEvaluationRunOperator(AwsBaseOperator[GlueDataQualityHook]):
+    """
+    Once you have a ruleset definition (either recommended or your own), you call this operation to evaluate the ruleset against a data source (Glue table).

Review Comment:
   It feels weird to describe an operator with a sentence starting by "Once you have a". Suggestion: "Evaluate a ruleset against a data source (Glue table)".



##########
airflow/providers/amazon/aws/hooks/glue.py:
##########
@@ -430,3 +431,105 @@ def create_or_update_glue_job(self) -> str | None:
             self.conn.create_job(**config)
 
         return self.job_name
+
+
+class GlueDataQualityHook(AwsBaseHook):
+    """
+    Interact with AWS Glue Data Quality.
+
+    Provide thick wrapper around :external+boto3:py:class:`boto3.client("glue") <Glue.Client>`.
+
+    Additional arguments (such as ``aws_conn_id``) may be specified and
+    are passed down to the underlying AwsBaseHook.
+
+    .. seealso::
+        - :class:`airflow.providers.amazon.aws.hooks.base_aws.AwsBaseHook`
+    """
+
+    def __init__(
+        self,
+        *args,
+        **kwargs,
+    ):
+        kwargs["client_type"] = "glue"
+        super().__init__(*args, **kwargs)
+
+    def has_data_quality_ruleset(self, name: str) -> bool:
+        try:
+            self.conn.get_data_quality_ruleset(Name=name)
+            return True
+        except self.conn.exceptions.EntityNotFoundException:
+            return False
+
+    def update_glue_data_quality_ruleset(self, config: dict[str, Any]) -> None:
+        if self.has_data_quality_ruleset(config["Name"]):
+            self.log.info("Updating AWS Glue data quality ruleset with: %s", config)
+            self.conn.update_data_quality_ruleset(**config)
+        else:
+            raise AirflowException(f"AWS Glue data quality ruleset {config['Name']} not exists to update")
+
+    def create_glue_data_quality_ruleset(self, config: dict[str, Any]) -> None:
+        if not self.has_data_quality_ruleset(config["Name"]):
+            self.log.info("Creating AWS Glue data quality ruleset with: %s", config)
+            self.conn.create_data_quality_ruleset(**config)
+        else:
+            raise AirflowException(
+                f"AWS Glue data quality ruleset {config['Name']} already exists with same name."
+            )
+
+    def display_result(self, result: dict[str, Any]) -> None:
+        import pandas as pd
+
+        pd.set_option("display.max_rows", None)
+        pd.set_option("display.max_columns", None)
+        pd.set_option("display.width", None)
+        pd.set_option("display.max_colwidth", None)
+
+        self.log.info(
+            "AWS Glue data quality ruleset evaluation result for RulesetName: %s RulesetEvaluationRunId: %s Score: %s",
+            result.get("RulesetName"),
+            result.get("RulesetEvaluationRunId"),
+            result.get("Score"),
+        )
+
+        rule_results = result["RuleResults"]
+        rule_results_df = pd.DataFrame(rule_results)
+        self.log.info(rule_results_df)
+
+    def get_evaluation_run_results(self, run_id: str) -> dict[str, Any]:
+        response = self.conn.get_data_quality_ruleset_evaluation_run(RunId=run_id)
+
+        return self.conn.batch_get_data_quality_result(ResultIds=response["ResultIds"])
+
+    def validate_evaluation_run_results(
+        self, evaluation_run_id: str, show_results: bool = True, verify_result_status: bool = True
+    ) -> None:
+        results = self.get_evaluation_run_results(evaluation_run_id)
+        total_failed_rules = 0
+
+        if results.get("ResultsNotFound"):
+            self.log.info(
+                "AWS Glue data quality ruleset evaluation run, results not found for %s",
+                results["ResultsNotFound"],
+            )
+
+        for result in results["Results"]:
+            rule_results = result["RuleResults"]
+            total_failed_rules = total_failed_rules + sum(
+                1
+                for result in rule_results
+                if result.get("Result") == "FAIL" or result.get("Result") == "ERROR"
+            )

Review Comment:
   ```suggestion
               total_failed_rules += len(filter(lambda result: result.get("Result") == "FAIL" or result.get("Result") == "ERROR", rule_results))
   ```



##########
airflow/providers/amazon/aws/operators/glue.py:
##########
@@ -239,3 +243,253 @@ def on_kill(self):
             )
             if not response["SuccessfulSubmissions"]:
                 self.log.error("Failed to stop AWS Glue Job: %s. Run Id: %s", self.job_name, self._job_run_id)
+
+
+class GlueDataQualityOperator(AwsBaseOperator[GlueDataQualityHook]):
+    """
+    Creates a data quality ruleset with DQDL rules applied to a specified Glue table.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the guide:
+        :ref:`howto/operator:GlueDataQualityOperator`
+
+    :param name: A unique name for the data quality ruleset.
+    :param ruleset: A Data Quality Definition Language (DQDL) ruleset.
+        For more information, see the Glue developer guide.
+    :param description: A description of the data quality ruleset.
+    :param update_rule_set: To update existing ruleset, Set this flag to True. (default: False)
+    :param data_quality_ruleset_kwargs: Extra arguments for RuleSet.
+
+    :param aws_conn_id: The Airflow connection used for AWS credentials.
+        If this is ``None`` or empty then the default boto3 behaviour is used. If
+        running Airflow in a distributed manner and aws_conn_id is None or
+        empty, then default boto3 configuration would be used (and must be
+        maintained on each worker node).
+    :param region_name: AWS region_name. If not specified then the default boto3 behaviour is used.
+    :param verify: Whether or not to verify SSL certificates. See:
+        https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html
+    :param botocore_config: Configuration dictionary (key-values) for botocore client. See:
+        https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html
+    """
+
+    aws_hook_class = GlueDataQualityHook
+    template_fields: Sequence[str] = ("name", "ruleset", "data_quality_ruleset_kwargs")
+
+    template_fields_renderers = {
+        "data_quality_ruleset_kwargs": "json",
+    }
+    ui_color = "#ededed"
+
+    def __init__(
+        self,
+        *,
+        name: str,
+        ruleset: str,
+        description: str = "AWS Glue Data Quality Rule Set With Airflow",
+        update_rule_set: bool = False,
+        data_quality_ruleset_kwargs: dict | None = None,
+        aws_conn_id: str | None = "aws_default",
+        **kwargs,
+    ):
+        super().__init__(**kwargs)
+        self.name = name
+        self.ruleset = ruleset.strip()
+        self.description = description
+        self.update_rule_set = update_rule_set
+        self.data_quality_ruleset_kwargs = data_quality_ruleset_kwargs or {}
+        self.aws_conn_id = aws_conn_id
+
+    def validate_inputs(self) -> None:
+        if not self.ruleset.startswith("Rules") or not self.ruleset.endswith("]"):
+            raise AttributeError("RuleSet must starts with Rules = [ and ends with ]")
+
+        if self.data_quality_ruleset_kwargs.get("TargetTable"):
+            target_table = self.data_quality_ruleset_kwargs["TargetTable"]
+
+            if not target_table.get("TableName") or not target_table.get("DatabaseName"):
+                raise AttributeError("Target table must have DatabaseName and TableName")
+
+    def execute(self, context: Context):
+        self.validate_inputs()
+
+        config = {
+            "Name": self.name,
+            "Ruleset": self.ruleset,
+            "Description": self.description,
+            **self.data_quality_ruleset_kwargs,
+        }
+
+        if self.update_rule_set:
+            self.hook.update_glue_data_quality_ruleset(config)
+            self.log.info("AWS Glue data quality ruleset updated successfully")
+        else:
+            self.hook.create_glue_data_quality_ruleset(config)
+            self.log.info("AWS Glue data quality ruleset created successfully")
+
+
+class GlueDataQualityRuleSetEvaluationRunOperator(AwsBaseOperator[GlueDataQualityHook]):
+    """
+    Once you have a ruleset definition (either recommended or your own), you call this operation to evaluate the ruleset against a data source (Glue table).
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the guide:
+        :ref:`howto/operator:GlueDataQualityRuleSetEvaluationRunOperator`
+
+    :param datasource: The data source (Glue table) associated with this run. (templated)
+    :param role: IAM role supplied for job execution. (templated)
+    :param rule_set_names: A list of ruleset names for evaluation. (templated)
+    :param number_of_workers: The number of G.1X workers to be used in the run. (default: 5)
+    :param timeout: The timeout for a run in minutes. This is the maximum time that a run can consume resources
+        before it is terminated and enters TIMEOUT status. (default: 2,880)
+    :param verify_result_status: Validate all the ruleset rules evaluation run results,
+        If any of the rule status is Fail or Error then an exception is thrown. (default: True)
+    :param show_results: Displays all the ruleset rules evaluation run results. (default: True)
+    :param rule_set_evaluation_run_kwargs: Extra arguments for evaluation run. (templated)
+    :param wait_for_completion: Whether to wait for job to stop. (default: True)
+    :param waiter_delay: Time in seconds to wait between status checks. (default: 60)
+    :param waiter_max_attempts: Maximum number of attempts to check for job completion. (default: 20)
+    :param deferrable: If True, the operator will wait asynchronously for the job to stop.
+        This implies waiting for completion. This mode requires aiobotocore module to be installed.
+        (default: False)
+
+    :param aws_conn_id: The Airflow connection used for AWS credentials.
+        If this is ``None`` or empty then the default boto3 behaviour is used. If
+        running Airflow in a distributed manner and aws_conn_id is None or
+        empty, then default boto3 configuration would be used (and must be
+        maintained on each worker node).
+    :param region_name: AWS region_name. If not specified then the default boto3 behaviour is used.
+    :param verify: Whether or not to verify SSL certificates. See:
+        https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html
+    :param botocore_config: Configuration dictionary (key-values) for botocore client. See:
+        https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html
+    """
+
+    aws_hook_class = GlueDataQualityHook
+
+    template_fields: Sequence[str] = (
+        "datasource",
+        "role",
+        "rule_set_names",
+        "rule_set_evaluation_run_kwargs",
+    )
+
+    template_fields_renderers = {"datasource": "json", "rule_set_evaluation_run_kwargs": "json"}
+
+    ui_color = "#ededed"
+
+    def __init__(
+        self,
+        *,
+        datasource: dict,
+        role: str,
+        rule_set_names: list[str],
+        number_of_workers: int = 5,
+        timeout: int = 2880,
+        verify_result_status: bool = True,
+        show_results: bool = True,
+        rule_set_evaluation_run_kwargs: dict[str, Any] | None = None,
+        wait_for_completion: bool = True,
+        waiter_delay: int = 60,
+        waiter_max_attempts: int = 20,
+        deferrable: bool = conf.getboolean("operators", "default_deferrable", fallback=False),
+        aws_conn_id: str | None = "aws_default",
+        **kwargs,
+    ):
+        super().__init__(**kwargs)
+        self.datasource = datasource
+        self.role = role
+        self.rule_set_names = rule_set_names
+        self.number_of_workers = number_of_workers
+        self.timeout = timeout
+        self.verify_result_status = verify_result_status
+        self.show_results = show_results
+        self.rule_set_evaluation_run_kwargs = rule_set_evaluation_run_kwargs or {}
+        self.wait_for_completion = wait_for_completion
+        self.waiter_delay = waiter_delay
+        self.waiter_max_attempts = waiter_max_attempts
+        self.deferrable = deferrable
+        self.aws_conn_id = aws_conn_id
+
+    def validate_inputs(self) -> None:
+        glue_table = self.datasource.get("GlueTable", {})
+
+        if not glue_table.get("DatabaseName") or not glue_table.get("TableName"):
+            raise AttributeError("DataSource glue table must have DatabaseName and TableName")
+
+        not_found_ruleset = []
+
+        for ruleset_name in self.rule_set_names:
+            if not self.hook.has_data_quality_ruleset(ruleset_name):
+                not_found_ruleset.append(ruleset_name)

Review Comment:
   ```suggestion
           not_found_ruleset = [ruleset_name for ruleset_name in self.rule_set_names if not self.hook.has_data_quality_ruleset(ruleset_name)]
   ```



##########
airflow/providers/amazon/aws/hooks/glue.py:
##########
@@ -430,3 +431,105 @@ def create_or_update_glue_job(self) -> str | None:
             self.conn.create_job(**config)
 
         return self.job_name
+
+
+class GlueDataQualityHook(AwsBaseHook):
+    """
+    Interact with AWS Glue Data Quality.
+
+    Provide thick wrapper around :external+boto3:py:class:`boto3.client("glue") <Glue.Client>`.
+
+    Additional arguments (such as ``aws_conn_id``) may be specified and
+    are passed down to the underlying AwsBaseHook.
+
+    .. seealso::
+        - :class:`airflow.providers.amazon.aws.hooks.base_aws.AwsBaseHook`
+    """
+
+    def __init__(
+        self,
+        *args,
+        **kwargs,
+    ):
+        kwargs["client_type"] = "glue"
+        super().__init__(*args, **kwargs)
+
+    def has_data_quality_ruleset(self, name: str) -> bool:
+        try:
+            self.conn.get_data_quality_ruleset(Name=name)
+            return True
+        except self.conn.exceptions.EntityNotFoundException:
+            return False
+
+    def update_glue_data_quality_ruleset(self, config: dict[str, Any]) -> None:
+        if self.has_data_quality_ruleset(config["Name"]):
+            self.log.info("Updating AWS Glue data quality ruleset with: %s", config)
+            self.conn.update_data_quality_ruleset(**config)
+        else:
+            raise AirflowException(f"AWS Glue data quality ruleset {config['Name']} not exists to update")
+
+    def create_glue_data_quality_ruleset(self, config: dict[str, Any]) -> None:
+        if not self.has_data_quality_ruleset(config["Name"]):
+            self.log.info("Creating AWS Glue data quality ruleset with: %s", config)
+            self.conn.create_data_quality_ruleset(**config)
+        else:
+            raise AirflowException(
+                f"AWS Glue data quality ruleset {config['Name']} already exists with same name."
+            )
+
+    def display_result(self, result: dict[str, Any]) -> None:

Review Comment:
   Some docstring would help as well to understand the function



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Adding Amazon Glue Data Quality Service [airflow]

Posted by "gopidesupavan (via GitHub)" <gi...@apache.org>.
gopidesupavan commented on code in PR #39923:
URL: https://github.com/apache/airflow/pull/39923#discussion_r1621295814


##########
airflow/providers/amazon/aws/operators/glue.py:
##########
@@ -239,3 +243,253 @@ def on_kill(self):
             )
             if not response["SuccessfulSubmissions"]:
                 self.log.error("Failed to stop AWS Glue Job: %s. Run Id: %s", self.job_name, self._job_run_id)
+
+
+class GlueDataQualityOperator(AwsBaseOperator[GlueDataQualityHook]):
+    """
+    Creates a data quality ruleset with DQDL rules applied to a specified Glue table.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the guide:
+        :ref:`howto/operator:GlueDataQualityOperator`
+
+    :param name: A unique name for the data quality ruleset.
+    :param ruleset: A Data Quality Definition Language (DQDL) ruleset.
+        For more information, see the Glue developer guide.
+    :param description: A description of the data quality ruleset.
+    :param update_rule_set: To update existing ruleset, Set this flag to True. (default: False)
+    :param data_quality_ruleset_kwargs: Extra arguments for RuleSet.
+
+    :param aws_conn_id: The Airflow connection used for AWS credentials.
+        If this is ``None`` or empty then the default boto3 behaviour is used. If
+        running Airflow in a distributed manner and aws_conn_id is None or
+        empty, then default boto3 configuration would be used (and must be
+        maintained on each worker node).
+    :param region_name: AWS region_name. If not specified then the default boto3 behaviour is used.
+    :param verify: Whether or not to verify SSL certificates. See:
+        https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html
+    :param botocore_config: Configuration dictionary (key-values) for botocore client. See:
+        https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html
+    """
+
+    aws_hook_class = GlueDataQualityHook
+    template_fields: Sequence[str] = ("name", "ruleset", "data_quality_ruleset_kwargs")
+
+    template_fields_renderers = {
+        "data_quality_ruleset_kwargs": "json",
+    }
+    ui_color = "#ededed"
+
+    def __init__(
+        self,
+        *,
+        name: str,
+        ruleset: str,
+        description: str = "AWS Glue Data Quality Rule Set With Airflow",
+        update_rule_set: bool = False,
+        data_quality_ruleset_kwargs: dict | None = None,
+        aws_conn_id: str | None = "aws_default",
+        **kwargs,
+    ):
+        super().__init__(**kwargs)
+        self.name = name
+        self.ruleset = ruleset.strip()
+        self.description = description
+        self.update_rule_set = update_rule_set
+        self.data_quality_ruleset_kwargs = data_quality_ruleset_kwargs or {}
+        self.aws_conn_id = aws_conn_id
+
+    def validate_inputs(self) -> None:
+        if not self.ruleset.startswith("Rules") or not self.ruleset.endswith("]"):
+            raise AttributeError("RuleSet must starts with Rules = [ and ends with ]")
+
+        if self.data_quality_ruleset_kwargs.get("TargetTable"):
+            target_table = self.data_quality_ruleset_kwargs["TargetTable"]
+
+            if not target_table.get("TableName") or not target_table.get("DatabaseName"):
+                raise AttributeError("Target table must have DatabaseName and TableName")
+
+    def execute(self, context: Context):
+        self.validate_inputs()
+
+        config = {
+            "Name": self.name,
+            "Ruleset": self.ruleset,
+            "Description": self.description,
+            **self.data_quality_ruleset_kwargs,
+        }
+
+        if self.update_rule_set:
+            self.hook.update_glue_data_quality_ruleset(config)
+            self.log.info("AWS Glue data quality ruleset updated successfully")
+        else:
+            self.hook.create_glue_data_quality_ruleset(config)
+            self.log.info("AWS Glue data quality ruleset created successfully")
+
+
+class GlueDataQualityRuleSetEvaluationRunOperator(AwsBaseOperator[GlueDataQualityHook]):
+    """
+    Once you have a ruleset definition (either recommended or your own), you call this operation to evaluate the ruleset against a data source (Glue table).
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the guide:
+        :ref:`howto/operator:GlueDataQualityRuleSetEvaluationRunOperator`
+
+    :param datasource: The data source (Glue table) associated with this run. (templated)
+    :param role: IAM role supplied for job execution. (templated)
+    :param rule_set_names: A list of ruleset names for evaluation. (templated)
+    :param number_of_workers: The number of G.1X workers to be used in the run. (default: 5)
+    :param timeout: The timeout for a run in minutes. This is the maximum time that a run can consume resources
+        before it is terminated and enters TIMEOUT status. (default: 2,880)
+    :param verify_result_status: Validate all the ruleset rules evaluation run results,
+        If any of the rule status is Fail or Error then an exception is thrown. (default: True)
+    :param show_results: Displays all the ruleset rules evaluation run results. (default: True)
+    :param rule_set_evaluation_run_kwargs: Extra arguments for evaluation run. (templated)
+    :param wait_for_completion: Whether to wait for job to stop. (default: True)
+    :param waiter_delay: Time in seconds to wait between status checks. (default: 60)
+    :param waiter_max_attempts: Maximum number of attempts to check for job completion. (default: 20)
+    :param deferrable: If True, the operator will wait asynchronously for the job to stop.
+        This implies waiting for completion. This mode requires aiobotocore module to be installed.
+        (default: False)
+
+    :param aws_conn_id: The Airflow connection used for AWS credentials.
+        If this is ``None`` or empty then the default boto3 behaviour is used. If
+        running Airflow in a distributed manner and aws_conn_id is None or
+        empty, then default boto3 configuration would be used (and must be
+        maintained on each worker node).
+    :param region_name: AWS region_name. If not specified then the default boto3 behaviour is used.
+    :param verify: Whether or not to verify SSL certificates. See:
+        https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html
+    :param botocore_config: Configuration dictionary (key-values) for botocore client. See:
+        https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html
+    """
+
+    aws_hook_class = GlueDataQualityHook
+
+    template_fields: Sequence[str] = (
+        "datasource",
+        "role",
+        "rule_set_names",
+        "rule_set_evaluation_run_kwargs",
+    )
+
+    template_fields_renderers = {"datasource": "json", "rule_set_evaluation_run_kwargs": "json"}
+
+    ui_color = "#ededed"
+
+    def __init__(
+        self,
+        *,
+        datasource: dict,
+        role: str,
+        rule_set_names: list[str],
+        number_of_workers: int = 5,
+        timeout: int = 2880,
+        verify_result_status: bool = True,
+        show_results: bool = True,
+        rule_set_evaluation_run_kwargs: dict[str, Any] | None = None,
+        wait_for_completion: bool = True,
+        waiter_delay: int = 60,
+        waiter_max_attempts: int = 20,
+        deferrable: bool = conf.getboolean("operators", "default_deferrable", fallback=False),
+        aws_conn_id: str | None = "aws_default",
+        **kwargs,
+    ):
+        super().__init__(**kwargs)
+        self.datasource = datasource
+        self.role = role
+        self.rule_set_names = rule_set_names
+        self.number_of_workers = number_of_workers
+        self.timeout = timeout
+        self.verify_result_status = verify_result_status
+        self.show_results = show_results
+        self.rule_set_evaluation_run_kwargs = rule_set_evaluation_run_kwargs or {}
+        self.wait_for_completion = wait_for_completion
+        self.waiter_delay = waiter_delay
+        self.waiter_max_attempts = waiter_max_attempts
+        self.deferrable = deferrable
+        self.aws_conn_id = aws_conn_id
+
+    def validate_inputs(self) -> None:
+        glue_table = self.datasource.get("GlueTable", {})
+
+        if not glue_table.get("DatabaseName") or not glue_table.get("TableName"):
+            raise AttributeError("DataSource glue table must have DatabaseName and TableName")
+
+        not_found_ruleset = []
+
+        for ruleset_name in self.rule_set_names:
+            if not self.hook.has_data_quality_ruleset(ruleset_name):
+                not_found_ruleset.append(ruleset_name)

Review Comment:
   updated the changes.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Adding Amazon Glue Data Quality Service [airflow]

Posted by "gopidesupavan (via GitHub)" <gi...@apache.org>.
gopidesupavan commented on code in PR #39923:
URL: https://github.com/apache/airflow/pull/39923#discussion_r1621290560


##########
airflow/providers/amazon/aws/operators/glue.py:
##########
@@ -239,3 +243,253 @@ def on_kill(self):
             )
             if not response["SuccessfulSubmissions"]:
                 self.log.error("Failed to stop AWS Glue Job: %s. Run Id: %s", self.job_name, self._job_run_id)
+
+
+class GlueDataQualityOperator(AwsBaseOperator[GlueDataQualityHook]):
+    """
+    Creates a data quality ruleset with DQDL rules applied to a specified Glue table.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the guide:
+        :ref:`howto/operator:GlueDataQualityOperator`
+
+    :param name: A unique name for the data quality ruleset.
+    :param ruleset: A Data Quality Definition Language (DQDL) ruleset.
+        For more information, see the Glue developer guide.
+    :param description: A description of the data quality ruleset.
+    :param update_rule_set: To update existing ruleset, Set this flag to True. (default: False)
+    :param data_quality_ruleset_kwargs: Extra arguments for RuleSet.
+
+    :param aws_conn_id: The Airflow connection used for AWS credentials.
+        If this is ``None`` or empty then the default boto3 behaviour is used. If
+        running Airflow in a distributed manner and aws_conn_id is None or
+        empty, then default boto3 configuration would be used (and must be
+        maintained on each worker node).
+    :param region_name: AWS region_name. If not specified then the default boto3 behaviour is used.
+    :param verify: Whether or not to verify SSL certificates. See:
+        https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html
+    :param botocore_config: Configuration dictionary (key-values) for botocore client. See:
+        https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html
+    """
+
+    aws_hook_class = GlueDataQualityHook
+    template_fields: Sequence[str] = ("name", "ruleset", "data_quality_ruleset_kwargs")
+
+    template_fields_renderers = {
+        "data_quality_ruleset_kwargs": "json",
+    }
+    ui_color = "#ededed"
+
+    def __init__(
+        self,
+        *,
+        name: str,
+        ruleset: str,
+        description: str = "AWS Glue Data Quality Rule Set With Airflow",
+        update_rule_set: bool = False,
+        data_quality_ruleset_kwargs: dict | None = None,
+        aws_conn_id: str | None = "aws_default",
+        **kwargs,
+    ):
+        super().__init__(**kwargs)
+        self.name = name
+        self.ruleset = ruleset.strip()
+        self.description = description
+        self.update_rule_set = update_rule_set
+        self.data_quality_ruleset_kwargs = data_quality_ruleset_kwargs or {}
+        self.aws_conn_id = aws_conn_id
+
+    def validate_inputs(self) -> None:
+        if not self.ruleset.startswith("Rules") or not self.ruleset.endswith("]"):
+            raise AttributeError("RuleSet must starts with Rules = [ and ends with ]")
+
+        if self.data_quality_ruleset_kwargs.get("TargetTable"):
+            target_table = self.data_quality_ruleset_kwargs["TargetTable"]
+
+            if not target_table.get("TableName") or not target_table.get("DatabaseName"):
+                raise AttributeError("Target table must have DatabaseName and TableName")
+
+    def execute(self, context: Context):
+        self.validate_inputs()
+
+        config = {
+            "Name": self.name,
+            "Ruleset": self.ruleset,
+            "Description": self.description,
+            **self.data_quality_ruleset_kwargs,
+        }
+
+        if self.update_rule_set:
+            self.hook.update_glue_data_quality_ruleset(config)
+            self.log.info("AWS Glue data quality ruleset updated successfully")
+        else:
+            self.hook.create_glue_data_quality_ruleset(config)
+            self.log.info("AWS Glue data quality ruleset created successfully")
+
+
+class GlueDataQualityRuleSetEvaluationRunOperator(AwsBaseOperator[GlueDataQualityHook]):
+    """
+    Once you have a ruleset definition (either recommended or your own), you call this operation to evaluate the ruleset against a data source (Glue table).

Review Comment:
   Agree, updated the changes.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Adding Amazon Glue Data Quality Service [airflow]

Posted by "gopidesupavan (via GitHub)" <gi...@apache.org>.
gopidesupavan commented on code in PR #39923:
URL: https://github.com/apache/airflow/pull/39923#discussion_r1621294477


##########
airflow/providers/amazon/aws/hooks/glue.py:
##########
@@ -430,3 +431,105 @@ def create_or_update_glue_job(self) -> str | None:
             self.conn.create_job(**config)
 
         return self.job_name
+
+
+class GlueDataQualityHook(AwsBaseHook):
+    """
+    Interact with AWS Glue Data Quality.
+
+    Provide thick wrapper around :external+boto3:py:class:`boto3.client("glue") <Glue.Client>`.
+
+    Additional arguments (such as ``aws_conn_id``) may be specified and
+    are passed down to the underlying AwsBaseHook.
+
+    .. seealso::
+        - :class:`airflow.providers.amazon.aws.hooks.base_aws.AwsBaseHook`
+    """
+
+    def __init__(
+        self,
+        *args,
+        **kwargs,
+    ):
+        kwargs["client_type"] = "glue"
+        super().__init__(*args, **kwargs)
+
+    def has_data_quality_ruleset(self, name: str) -> bool:
+        try:
+            self.conn.get_data_quality_ruleset(Name=name)
+            return True
+        except self.conn.exceptions.EntityNotFoundException:
+            return False
+
+    def update_glue_data_quality_ruleset(self, config: dict[str, Any]) -> None:
+        if self.has_data_quality_ruleset(config["Name"]):
+            self.log.info("Updating AWS Glue data quality ruleset with: %s", config)
+            self.conn.update_data_quality_ruleset(**config)
+        else:
+            raise AirflowException(f"AWS Glue data quality ruleset {config['Name']} not exists to update")
+
+    def create_glue_data_quality_ruleset(self, config: dict[str, Any]) -> None:
+        if not self.has_data_quality_ruleset(config["Name"]):
+            self.log.info("Creating AWS Glue data quality ruleset with: %s", config)
+            self.conn.create_data_quality_ruleset(**config)
+        else:
+            raise AirflowException(
+                f"AWS Glue data quality ruleset {config['Name']} already exists with same name."
+            )

Review Comment:
   Agree , these are kind of wrappers, i have now removed those and calling directly inside operator execute method. `self.hook.conn.create_data_quality_ruleset(**config)`
   `self.hook.conn.update_data_quality_ruleset(**config)`
   
   Thank you.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Adding Amazon Glue Data Quality Service [airflow]

Posted by "gopidesupavan (via GitHub)" <gi...@apache.org>.
gopidesupavan commented on code in PR #39923:
URL: https://github.com/apache/airflow/pull/39923#discussion_r1621290776


##########
airflow/providers/amazon/aws/operators/glue.py:
##########
@@ -239,3 +243,253 @@ def on_kill(self):
             )
             if not response["SuccessfulSubmissions"]:
                 self.log.error("Failed to stop AWS Glue Job: %s. Run Id: %s", self.job_name, self._job_run_id)
+
+
+class GlueDataQualityOperator(AwsBaseOperator[GlueDataQualityHook]):
+    """
+    Creates a data quality ruleset with DQDL rules applied to a specified Glue table.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the guide:
+        :ref:`howto/operator:GlueDataQualityOperator`
+
+    :param name: A unique name for the data quality ruleset.
+    :param ruleset: A Data Quality Definition Language (DQDL) ruleset.
+        For more information, see the Glue developer guide.
+    :param description: A description of the data quality ruleset.
+    :param update_rule_set: To update existing ruleset, Set this flag to True. (default: False)
+    :param data_quality_ruleset_kwargs: Extra arguments for RuleSet.
+
+    :param aws_conn_id: The Airflow connection used for AWS credentials.
+        If this is ``None`` or empty then the default boto3 behaviour is used. If
+        running Airflow in a distributed manner and aws_conn_id is None or
+        empty, then default boto3 configuration would be used (and must be
+        maintained on each worker node).
+    :param region_name: AWS region_name. If not specified then the default boto3 behaviour is used.
+    :param verify: Whether or not to verify SSL certificates. See:
+        https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html
+    :param botocore_config: Configuration dictionary (key-values) for botocore client. See:
+        https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html
+    """
+
+    aws_hook_class = GlueDataQualityHook
+    template_fields: Sequence[str] = ("name", "ruleset", "data_quality_ruleset_kwargs")

Review Comment:
   done changes.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Adding Amazon Glue Data Quality Service [airflow]

Posted by "gopidesupavan (via GitHub)" <gi...@apache.org>.
gopidesupavan commented on code in PR #39923:
URL: https://github.com/apache/airflow/pull/39923#discussion_r1621291444


##########
airflow/providers/amazon/aws/hooks/glue.py:
##########
@@ -430,3 +431,105 @@ def create_or_update_glue_job(self) -> str | None:
             self.conn.create_job(**config)
 
         return self.job_name
+
+
+class GlueDataQualityHook(AwsBaseHook):
+    """
+    Interact with AWS Glue Data Quality.
+
+    Provide thick wrapper around :external+boto3:py:class:`boto3.client("glue") <Glue.Client>`.
+
+    Additional arguments (such as ``aws_conn_id``) may be specified and
+    are passed down to the underlying AwsBaseHook.
+
+    .. seealso::
+        - :class:`airflow.providers.amazon.aws.hooks.base_aws.AwsBaseHook`
+    """
+
+    def __init__(
+        self,
+        *args,
+        **kwargs,
+    ):
+        kwargs["client_type"] = "glue"
+        super().__init__(*args, **kwargs)
+
+    def has_data_quality_ruleset(self, name: str) -> bool:
+        try:
+            self.conn.get_data_quality_ruleset(Name=name)
+            return True
+        except self.conn.exceptions.EntityNotFoundException:
+            return False
+
+    def update_glue_data_quality_ruleset(self, config: dict[str, Any]) -> None:
+        if self.has_data_quality_ruleset(config["Name"]):
+            self.log.info("Updating AWS Glue data quality ruleset with: %s", config)
+            self.conn.update_data_quality_ruleset(**config)
+        else:
+            raise AirflowException(f"AWS Glue data quality ruleset {config['Name']} not exists to update")
+
+    def create_glue_data_quality_ruleset(self, config: dict[str, Any]) -> None:
+        if not self.has_data_quality_ruleset(config["Name"]):
+            self.log.info("Creating AWS Glue data quality ruleset with: %s", config)
+            self.conn.create_data_quality_ruleset(**config)
+        else:
+            raise AirflowException(
+                f"AWS Glue data quality ruleset {config['Name']} already exists with same name."
+            )
+
+    def display_result(self, result: dict[str, Any]) -> None:

Review Comment:
   make sense updated.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Adding Amazon Glue Data Quality Service [airflow]

Posted by "gopidesupavan (via GitHub)" <gi...@apache.org>.
gopidesupavan commented on code in PR #39923:
URL: https://github.com/apache/airflow/pull/39923#discussion_r1621336244


##########
airflow/providers/amazon/aws/hooks/glue.py:
##########
@@ -430,3 +431,105 @@ def create_or_update_glue_job(self) -> str | None:
             self.conn.create_job(**config)
 
         return self.job_name
+
+
+class GlueDataQualityHook(AwsBaseHook):
+    """
+    Interact with AWS Glue Data Quality.
+
+    Provide thick wrapper around :external+boto3:py:class:`boto3.client("glue") <Glue.Client>`.
+
+    Additional arguments (such as ``aws_conn_id``) may be specified and
+    are passed down to the underlying AwsBaseHook.
+
+    .. seealso::
+        - :class:`airflow.providers.amazon.aws.hooks.base_aws.AwsBaseHook`
+    """
+
+    def __init__(
+        self,
+        *args,
+        **kwargs,
+    ):
+        kwargs["client_type"] = "glue"
+        super().__init__(*args, **kwargs)
+
+    def has_data_quality_ruleset(self, name: str) -> bool:
+        try:
+            self.conn.get_data_quality_ruleset(Name=name)
+            return True
+        except self.conn.exceptions.EntityNotFoundException:
+            return False
+
+    def update_glue_data_quality_ruleset(self, config: dict[str, Any]) -> None:
+        if self.has_data_quality_ruleset(config["Name"]):
+            self.log.info("Updating AWS Glue data quality ruleset with: %s", config)
+            self.conn.update_data_quality_ruleset(**config)
+        else:
+            raise AirflowException(f"AWS Glue data quality ruleset {config['Name']} not exists to update")
+
+    def create_glue_data_quality_ruleset(self, config: dict[str, Any]) -> None:
+        if not self.has_data_quality_ruleset(config["Name"]):
+            self.log.info("Creating AWS Glue data quality ruleset with: %s", config)
+            self.conn.create_data_quality_ruleset(**config)
+        else:
+            raise AirflowException(
+                f"AWS Glue data quality ruleset {config['Name']} already exists with same name."
+            )
+
+    def display_result(self, result: dict[str, Any]) -> None:

Review Comment:
   Updated docstring for log_results function.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Adding Amazon Glue Data Quality Service [airflow]

Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on PR #39923:
URL: https://github.com/apache/airflow/pull/39923#issuecomment-2140739492

   > @vincbeck Thank you for all the suggestions, made all the changes please review. Is the docstring sufficient for the log_results?. please let me know happy to refine. 😄 .
   
   Yep, it looks good, it definitely helps to understand the function!


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Adding Amazon Glue Data Quality Service [airflow]

Posted by "gopidesupavan (via GitHub)" <gi...@apache.org>.
gopidesupavan commented on PR #39923:
URL: https://github.com/apache/airflow/pull/39923#issuecomment-2140737009

   @vincbeck Thank you for all the suggestions, made all the changes please review. Is the docstring sufficient for the log_results?. please let me know happy to refine. 😄 . 


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org