JDarDagran commented on code in PR #40335:
URL: https://github.com/apache/airflow/pull/40335#discussion_r1677898568


##########
airflow/lineage/hook.py:
##########
@@ -0,0 +1,173 @@
+#
+# 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 __future__ import annotations
+
+from typing import Union
+
+import attr
+
+from airflow.datasets import Dataset
+from airflow.hooks.base import BaseHook
+from airflow.io.store import ObjectStore
+from airflow.providers_manager import ProvidersManager
+from airflow.utils.log.logging_mixin import LoggingMixin
+
+# Store context what sent lineage.
+LineageContext = Union[BaseHook, ObjectStore]
+
+_hook_lineage_collector: HookLineageCollector | None = None
+
+
[email protected]
+class HookLineage:
+    """Holds lineage collected by HookLineageCollector."""
+
+    inputs: list[tuple[Dataset, LineageContext]] = attr.ib(factory=list)
+    outputs: list[tuple[Dataset, LineageContext]] = attr.ib(factory=list)
+
+
+class HookLineageCollector(LoggingMixin):
+    """
+    HookLineageCollector is a base class for collecting hook lineage 
information.
+
+    It is used to collect the input and output datasets of a hook execution.
+    """
+
+    def __init__(self, **kwargs):
+        super().__init__(**kwargs)
+        self.inputs: list[tuple[Dataset, LineageContext]] = []
+        self.outputs: list[tuple[Dataset, LineageContext]] = []
+
+    def create_dataset(
+        self, scheme: str | None, uri: str | None, dataset_kwargs: dict | 
None, dataset_extra: dict | None
+    ) -> Dataset | None:
+        """Create a Dataset instance from the given dataset kwargs."""
+        if uri:
+            # Fallback to default factory using the provided URI
+            return Dataset(uri=uri, extra=dataset_extra)
+
+        if not scheme:
+            self.log.debug(
+                "Missing required parameter: either 'uri' or 'scheme' must be 
provided to create a Dataset."
+            )
+            return None
+
+        dataset_factory = ProvidersManager().dataset_factories.get(scheme)
+        if not dataset_factory:
+            self.log.debug("Unsupported scheme: %s. Please provide a valid URI 
to create a Dataset.", scheme)
+            return None
+
+        dataset_kwargs = dataset_kwargs or {}
+
+        if dataset_extra and "extra" not in dataset_kwargs:
+            dataset_kwargs["extra"] = dataset_extra
+
+        try:
+            return dataset_factory(**dataset_kwargs)
+        except Exception as e:
+            self.log.warning("Failed to create dataset. Skipping. Error: %s", 
e)

Review Comment:
   I agree, missed that.



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