gopidesupavan commented on code in PR #69575:
URL: https://github.com/apache/airflow/pull/69575#discussion_r3575176390


##########
providers/common/dataquality/src/airflow/providers/common/dataquality/assets.py:
##########
@@ -0,0 +1,172 @@
+# 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.
+"""
+Asset-level data quality declarations.
+
+Quality configuration lives inside ``Asset.extra`` under the 
``airflow.dataquality`` key, so it is
+serialized with the Dag and needs no Airflow core changes. The rules travel 
with the asset
+definition instead of being scattered across the Dags that check it.
+"""
+
+from __future__ import annotations
+
+import logging
+from typing import TYPE_CHECKING, Any
+
+from airflow.providers.common.dataquality.exceptions import 
DQRuleValidationError
+from airflow.providers.common.dataquality.rules import RuleSet
+from airflow.sdk import task
+
+if TYPE_CHECKING:
+    from collections.abc import Mapping, Sequence
+
+    from airflow.sdk import Asset
+    from airflow.sdk.execution_time.comms import 
AssetEventDagRunReferenceResult
+
+log = logging.getLogger(__name__)
+
+DQ_EXTRA_KEY = "airflow.dataquality"
+DQ_RESULT_EXTRA_KEY = "airflow.dataquality.result"
+
+
+def asset_quality(
+    asset: Asset,
+    *,
+    ruleset: RuleSet | dict[str, Any] | str,
+    conn_id: str | None = None,
+    table: str | None = None,
+) -> Asset:
+    """
+    Attach data quality configuration to an asset, returning the same asset.
+
+    :param asset: The asset the rules describe.
+    :param ruleset: A 
:class:`~airflow.providers.common.dataquality.rules.RuleSet`, its dict form, or 
a path
+        to a YAML ruleset file (resolved eagerly, at Dag-parse time).
+    :param conn_id: Default connection a check operator should use for this 
asset.
+    :param table: Default table to check; falls back to the asset name when 
unset.
+
+    Usage::
+
+        orders = asset_quality(
+            Asset("orders", uri="postgres://warehouse/analytics/orders"),
+            ruleset=rules,
+            conn_id="warehouse",
+            table="analytics.orders",
+        )
+        check = DQCheckOperator(task_id="dq", asset=orders)
+    """
+    if isinstance(ruleset, str):
+        ruleset = RuleSet.from_file(ruleset)
+    elif isinstance(ruleset, dict):
+        ruleset = RuleSet.from_dict(ruleset)
+    config: dict[str, Any] = {"ruleset": ruleset.to_dict()}
+    if conn_id:
+        config["conn_id"] = conn_id
+    if table:
+        config["table"] = table
+    asset.extra[DQ_EXTRA_KEY] = config
+    return asset
+
+
+def get_asset_quality_config(asset: Asset) -> dict[str, Any] | None:
+    """Return the raw ``airflow.dataquality`` config attached to an asset, if 
any."""
+    config = asset.extra.get(DQ_EXTRA_KEY)
+    if not isinstance(config, dict):
+        return None
+    return config
+
+
+def get_asset_ruleset(asset: Asset) -> RuleSet:
+    """Return the ruleset attached to an asset, raising when none is 
attached."""
+    config = get_asset_quality_config(asset)
+    if not config or "ruleset" not in config:
+        raise DQRuleValidationError(
+            f"Asset {asset.name!r} has no data quality config; attach one with 
asset_quality()"
+        )
+    return RuleSet.from_dict(config["ruleset"])
+
+
+def _quality_score_passes(
+    asset: Asset,
+    min_score: float,
+    triggering_asset_events: Mapping[Asset, 
Sequence[AssetEventDagRunReferenceResult]],
+) -> bool:
+    """Pure decision logic behind :func:`require_quality`, kept separate so it 
is testable without a Dag."""
+    events = triggering_asset_events.get(asset, [])
+    if not events:
+        log.warning(
+            "require_quality(%s): no triggering event for this run; skipping 
downstream tasks",
+            asset.name,
+        )
+        return False
+
+    summary = events[-1].extra.get(DQ_RESULT_EXTRA_KEY)

Review Comment:
   nice suggestion updated to default all events..



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to