-
Notifications
You must be signed in to change notification settings - Fork 3.5k
fix(ingest/snowflake): resolve external stage lineage via DataHub graph #17358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alokr-dhub
wants to merge
7
commits into
master
Choose a base branch
from
fix/snowflake_stages_external_lineage
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,103
−70
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3c31b51
fix: snowpipes upstream lineage handling
alokr-dhub 252b394
docs(ingest/snowflake): document resolve_external_stage_lineage_via_g…
alokr-dhub f1bf3e3
Merge branch 'master' into fix/snowflake_stages_external_lineage
alokr-dhub 59e1a42
Merge branch 'master' into fix/snowflake_stages_external_lineage
alokr-dhub ed7157a
fix(ingest/snowflake): fix unclosed parenthesis in external_stage_pla…
alokr-dhub e0388aa
refactor(ingest/snowflake): tighten external-stage lineage types and …
alokr-dhub 2b87fae
Merge branch 'master' into fix/snowflake_stages_external_lineage
alokr-dhub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
metadata-ingestion/src/datahub/ingestion/source/data_lake_common/path_urn_resolver.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import logging | ||
| from dataclasses import dataclass, field | ||
| from typing import TYPE_CHECKING, Dict, List, Optional, Tuple | ||
|
|
||
| if TYPE_CHECKING: | ||
| from datahub.ingestion.graph.client import DataHubGraph | ||
| from datahub.ingestion.graph.filters import RawSearchFilterRule | ||
|
|
||
| logger: logging.Logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class DataLakeUrnLookup: | ||
| matched_urns: Tuple[str, ...] = () | ||
| transient_error: Optional[Exception] = None | ||
|
|
||
| def __post_init__(self) -> None: | ||
| if self.transient_error is not None and self.matched_urns: | ||
| raise ValueError( | ||
| "DataLakeUrnLookup is either a transient-failure result or a " | ||
| "(possibly empty) match — never both." | ||
| ) | ||
|
|
||
|
|
||
| @dataclass | ||
| class DataLakePathResolver: | ||
| """Resolve a data-lake storage path to existing dataset URNs rooted at it. | ||
|
|
||
| Bulk-fetches every dataset URN under a bucket once per | ||
| ``(platform, platform_instance, bucket)`` and matches client-side, so N paths | ||
| under a bucket cost a single graph call rather than one wildcard query each. | ||
| Transient failures are surfaced via ``DataLakeUrnLookup.transient_error`` so | ||
| callers can log and count without this module owning a report type. | ||
| """ | ||
|
|
||
| graph: "DataHubGraph" | ||
| env: str | ||
| _bucket_index: Dict[Tuple[str, Optional[str], str], Tuple[str, ...]] = field( | ||
| default_factory=dict | ||
| ) | ||
|
|
||
| def resolve_datasets_under_path( | ||
| self, | ||
| *, | ||
| platform: str, | ||
| bucket: str, | ||
| path: str, | ||
| platform_instance: Optional[str] = None, | ||
| ) -> DataLakeUrnLookup: | ||
| """Return existing dataset URNs whose path equals or sits under ``path``.""" | ||
| key = (platform, platform_instance, bucket) | ||
| bucket_urns = self._bucket_index.get(key) | ||
| if bucket_urns is None: | ||
| try: | ||
| bucket_urns = self._bulk_fetch_bucket( | ||
| platform=platform, | ||
| platform_instance=platform_instance, | ||
| bucket=bucket, | ||
| ) | ||
| except Exception as e: | ||
| logger.warning( | ||
| f"Transient failure fetching {platform} dataset URNs for bucket " | ||
| f"{bucket!r}; lookup will not be cached.", | ||
| exc_info=True, | ||
| ) | ||
| return DataLakeUrnLookup(transient_error=e) | ||
| self._bucket_index[key] = bucket_urns | ||
|
|
||
| path_prefix = self._urn_prefix(platform, platform_instance, path) | ||
| matches = tuple( | ||
| u for u in bucket_urns if dataset_path_is_rooted_at(u, path_prefix) | ||
| ) | ||
| return DataLakeUrnLookup(matched_urns=matches) | ||
|
|
||
| def _bulk_fetch_bucket( | ||
| self, *, platform: str, platform_instance: Optional[str], bucket: str | ||
| ) -> Tuple[str, ...]: | ||
| bucket_prefix = self._urn_prefix(platform, platform_instance, bucket) | ||
| extra_filters: List["RawSearchFilterRule"] = [ | ||
| {"field": "urn", "condition": "START_WITH", "values": [bucket_prefix]} | ||
| ] | ||
| candidate_urns = self.graph.get_urns_by_filter( | ||
| entity_types=["dataset"], | ||
| platform=platform, | ||
| platform_instance=platform_instance, | ||
| env=self.env, | ||
| extraFilters=extra_filters, | ||
| ) | ||
| # The START_WITH wildcard is case-insensitive and prefix-only, so it can | ||
| # over-match sibling buckets (e.g. `bucket` vs `bucket-other`). Re-check the | ||
| # bucket boundary case-sensitively before caching. | ||
| return tuple( | ||
| urn | ||
| for urn in candidate_urns | ||
| if dataset_path_is_rooted_at(urn, bucket_prefix) | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _urn_prefix(platform: str, platform_instance: Optional[str], path: str) -> str: | ||
| name = f"{platform_instance}.{path}" if platform_instance else path | ||
| return f"urn:li:dataset:(urn:li:dataPlatform:{platform},{name}" | ||
|
|
||
|
|
||
| def dataset_path_is_rooted_at(dataset_urn: str, urn_prefix: str) -> bool: | ||
| """Whether ``dataset_urn``'s path equals or sits strictly under ``urn_prefix``. | ||
|
|
||
| Rejects false positives where a plain prefix match would treat a sibling path as | ||
| a child (e.g. ``foo`` matching ``foobar``): the character immediately after the | ||
| prefix must be ``/`` (a child path) or ``,`` (the URN's env separator, i.e. an | ||
| exact match). | ||
| """ | ||
| if not dataset_urn.startswith(urn_prefix): | ||
| return False | ||
| return dataset_urn[len(urn_prefix) : len(urn_prefix) + 1] in ("/", ",") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.