Skip to content

Commit

Permalink
feat(ingest/snowflake): Support ingesting snowflake tags as structure…
Browse files Browse the repository at this point in the history
…d properties (#12285)
  • Loading branch information
asikowitz authored Jan 14, 2025
1 parent e1532a7 commit ebbadf6
Show file tree
Hide file tree
Showing 11 changed files with 4,708 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
rest_emitter = DatahubRestEmitter(gms_server="http://localhost:8080")

# first, let's make an open ended structured property that allows one text value
text_property_urn = StructuredPropertyUrn("openTextProperty")
text_property_urn = StructuredPropertyUrn("io.acryl.openTextProperty")
text_property_definition = StructuredPropertyDefinitionClass(
qualifiedName="io.acryl.openTextProperty",
displayName="Open Text Property",
Expand All @@ -39,7 +39,7 @@

# next, let's make a property that allows for multiple datahub entity urns as values
# This example property could be used to reference other users or groups in datahub
urn_property_urn = StructuredPropertyUrn("dataSteward")
urn_property_urn = StructuredPropertyUrn("io.acryl.dataManagement.dataSteward")
urn_property_definition = StructuredPropertyDefinitionClass(
qualifiedName="io.acryl.dataManagement.dataSteward",
displayName="Data Steward",
Expand All @@ -63,7 +63,7 @@
rest_emitter.emit(event_prop_2)

# finally, let's make a single select number property with a few allowed options
number_property_urn = StructuredPropertyUrn("replicationSLA")
number_property_urn = StructuredPropertyUrn("io.acryl.dataManagement.replicationSLA")
number_property_definition = StructuredPropertyDefinitionClass(
qualifiedName="io.acryl.dataManagement.replicationSLA",
displayName="Retention Time",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def get_emitter() -> Union[DataHubRestEmitter, DatahubKafkaEmitter]:


# input your unique structured property ID
property_urn = StructuredPropertyUrn("dataSteward")
property_urn = StructuredPropertyUrn("io.acryl.dataManagement.dataSteward")

with get_emitter() as emitter:
for patch_mcp in (
Expand Down
27 changes: 27 additions & 0 deletions metadata-ingestion/src/datahub/emitter/mcp_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@
OwnershipClass,
OwnershipTypeClass,
StatusClass,
StructuredPropertiesClass,
StructuredPropertyValueAssignmentClass,
SubTypesClass,
TagAssociationClass,
)
from datahub.metadata.urns import StructuredPropertyUrn

# In https://github.com/datahub-project/datahub/pull/11214, we added a
# new env field to container properties. However, populating this field
Expand Down Expand Up @@ -187,12 +190,31 @@ def add_tags_to_entity_wu(
).as_workunit()


def add_structured_properties_to_entity_wu(
entity_urn: str, structured_properties: Dict[StructuredPropertyUrn, str]
) -> Iterable[MetadataWorkUnit]:
aspect = StructuredPropertiesClass(
properties=[
StructuredPropertyValueAssignmentClass(
propertyUrn=urn.urn(),
values=[value],
)
for urn, value in structured_properties.items()
]
)
yield MetadataChangeProposalWrapper(
entityUrn=entity_urn,
aspect=aspect,
).as_workunit()


def gen_containers(
container_key: KeyType,
name: str,
sub_types: List[str],
parent_container_key: Optional[ContainerKey] = None,
extra_properties: Optional[Dict[str, str]] = None,
structured_properties: Optional[Dict[StructuredPropertyUrn, str]] = None,
domain_urn: Optional[str] = None,
description: Optional[str] = None,
owner_urn: Optional[str] = None,
Expand Down Expand Up @@ -282,6 +304,11 @@ def gen_containers(
tags=sorted(tags),
)

if structured_properties:
yield from add_structured_properties_to_entity_wu(
entity_urn=container_urn, structured_properties=structured_properties
)


def add_dataset_to_container(
container_key: KeyType, dataset_urn: str
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ class SnowflakeV2Config(
description="""Optional. Allowed values are `without_lineage`, `with_lineage`, and `skip` (default). `without_lineage` only extracts tags that have been applied directly to the given entity. `with_lineage` extracts both directly applied and propagated tags, but will be significantly slower. See the [Snowflake documentation](https://docs.snowflake.com/en/user-guide/object-tagging.html#tag-lineage) for information about tag lineage/propagation. """,
)

extract_tags_as_structured_properties: bool = Field(
default=False,
description="If enabled along with `extract_tags`, extracts snowflake's key-value tags as DataHub structured properties instead of DataHub tags.",
)

include_external_url: bool = Field(
default=True,
description="Whether to populate Snowsight url for Snowflake Objects",
Expand All @@ -263,6 +268,14 @@ class SnowflakeV2Config(
description="List of regex patterns for tags to include in ingestion. Only used if `extract_tags` is enabled.",
)

structured_property_pattern: AllowDenyPattern = Field(
default=AllowDenyPattern.allow_all(),
description=(
"List of regex patterns for structured properties to include in ingestion."
" Only used if `extract_tags` and `extract_tags_as_structured_properties` are enabled."
),
)

# This is required since access_history table does not capture whether the table was temporary table.
temporary_tables_pattern: List[str] = Field(
default=DEFAULT_TEMP_TABLES_PATTERNS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,18 @@ class SnowflakeTag:
name: str
value: str

def display_name(self) -> str:
def tag_display_name(self) -> str:
return f"{self.name}: {self.value}"

def identifier(self) -> str:
def tag_identifier(self) -> str:
return f"{self._id_prefix_as_str()}:{self.value}"

def _id_prefix_as_str(self) -> str:
return f"{self.database}.{self.schema}.{self.name}"

def structured_property_identifier(self) -> str:
return f"snowflake.{self.database}.{self.schema}.{self.name}"


@dataclass
class SnowflakeColumn(BaseColumn):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

from datahub.configuration.pattern_utils import is_schema_allowed
from datahub.emitter.mce_builder import (
get_sys_time,
make_data_platform_urn,
make_dataset_urn_with_platform_instance,
make_schema_field_urn,
make_tag_urn,
)
from datahub.emitter.mcp import MetadataChangeProposalWrapper
from datahub.emitter.mcp_builder import add_structured_properties_to_entity_wu
from datahub.ingestion.api.source import SourceReport
from datahub.ingestion.api.workunit import MetadataWorkUnit
from datahub.ingestion.glossary.classification_mixin import (
Expand Down Expand Up @@ -72,6 +74,7 @@
PROFILING,
)
from datahub.metadata.com.linkedin.pegasus2avro.common import (
AuditStamp,
GlobalTags,
Status,
SubTypes,
Expand All @@ -98,7 +101,18 @@
StringType,
TimeType,
)
from datahub.metadata.com.linkedin.pegasus2avro.structured import (
StructuredPropertyDefinition,
)
from datahub.metadata.com.linkedin.pegasus2avro.tag import TagProperties
from datahub.metadata.urns import (
ContainerUrn,
DatasetUrn,
DataTypeUrn,
EntityTypeUrn,
SchemaFieldUrn,
StructuredPropertyUrn,
)
from datahub.sql_parsing.sql_parsing_aggregator import (
KnownLineageMapping,
SqlParsingAggregator,
Expand Down Expand Up @@ -673,14 +687,31 @@ def _process_view(
yield from self.gen_dataset_workunits(view, schema_name, db_name)

def _process_tag(self, tag: SnowflakeTag) -> Iterable[MetadataWorkUnit]:
tag_identifier = tag.identifier()
use_sp = self.config.extract_tags_as_structured_properties
identifier = (
self.snowflake_identifier(tag.structured_property_identifier())
if use_sp
else tag.tag_identifier()
)

if self.report.is_tag_processed(tag_identifier):
if self.report.is_tag_processed(identifier):
return

self.report.report_tag_processed(tag_identifier)

yield from self.gen_tag_workunits(tag)
self.report.report_tag_processed(identifier)
if use_sp:
yield from self.gen_tag_as_structured_property_workunits(tag)
else:
yield from self.gen_tag_workunits(tag)

def _format_tags_as_structured_properties(
self, tags: List[SnowflakeTag]
) -> Dict[StructuredPropertyUrn, str]:
return {
StructuredPropertyUrn(
self.snowflake_identifier(tag.structured_property_identifier())
): tag.value
for tag in tags
}

def gen_dataset_workunits(
self,
Expand Down Expand Up @@ -725,6 +756,9 @@ def gen_dataset_workunits(
env=self.config.env,
)

if self.config.extract_tags_as_structured_properties:
yield from self.gen_column_tags_as_structured_properties(dataset_urn, table)

yield from add_table_to_schema_container(
dataset_urn=dataset_urn,
parent_container_key=schema_container_key,
Expand Down Expand Up @@ -758,16 +792,24 @@ def gen_dataset_workunits(
)

if table.tags:
tag_associations = [
TagAssociation(
tag=make_tag_urn(self.snowflake_identifier(tag.identifier()))
if self.config.extract_tags_as_structured_properties:
yield from add_structured_properties_to_entity_wu(
dataset_urn,
self._format_tags_as_structured_properties(table.tags),
)
for tag in table.tags
]
global_tags = GlobalTags(tag_associations)
yield MetadataChangeProposalWrapper(
entityUrn=dataset_urn, aspect=global_tags
).as_workunit()
else:
tag_associations = [
TagAssociation(
tag=make_tag_urn(
self.snowflake_identifier(tag.tag_identifier())
)
)
for tag in table.tags
]
global_tags = GlobalTags(tag_associations)
yield MetadataChangeProposalWrapper(
entityUrn=dataset_urn, aspect=global_tags
).as_workunit()

if isinstance(table, SnowflakeView) and table.view_definition is not None:
view_properties_aspect = ViewProperties(
Expand Down Expand Up @@ -840,17 +882,52 @@ def get_dataset_properties(
)

def gen_tag_workunits(self, tag: SnowflakeTag) -> Iterable[MetadataWorkUnit]:
tag_urn = make_tag_urn(self.snowflake_identifier(tag.identifier()))
tag_urn = make_tag_urn(self.snowflake_identifier(tag.tag_identifier()))

tag_properties_aspect = TagProperties(
name=tag.display_name(),
name=tag.tag_display_name(),
description=f"Represents the Snowflake tag `{tag._id_prefix_as_str()}` with value `{tag.value}`.",
)

yield MetadataChangeProposalWrapper(
entityUrn=tag_urn, aspect=tag_properties_aspect
).as_workunit()

def gen_tag_as_structured_property_workunits(
self, tag: SnowflakeTag
) -> Iterable[MetadataWorkUnit]:
identifier = self.snowflake_identifier(tag.structured_property_identifier())
urn = StructuredPropertyUrn(identifier).urn()
aspect = StructuredPropertyDefinition(
qualifiedName=identifier,
displayName=tag.name,
valueType=DataTypeUrn("datahub.string").urn(),
entityTypes=[
EntityTypeUrn(f"datahub.{ContainerUrn.ENTITY_TYPE}").urn(),
EntityTypeUrn(f"datahub.{DatasetUrn.ENTITY_TYPE}").urn(),
EntityTypeUrn(f"datahub.{SchemaFieldUrn.ENTITY_TYPE}").urn(),
],
lastModified=AuditStamp(
time=get_sys_time(), actor="urn:li:corpuser:datahub"
),
)
yield MetadataChangeProposalWrapper(
entityUrn=urn,
aspect=aspect,
).as_workunit()

def gen_column_tags_as_structured_properties(
self, dataset_urn: str, table: Union[SnowflakeTable, SnowflakeView]
) -> Iterable[MetadataWorkUnit]:
for column_name in table.column_tags:
schema_field_urn = SchemaFieldUrn(dataset_urn, column_name).urn()
yield from add_structured_properties_to_entity_wu(
schema_field_urn,
self._format_tags_as_structured_properties(
table.column_tags[column_name]
),
)

def gen_schema_metadata(
self,
table: Union[SnowflakeTable, SnowflakeView],
Expand Down Expand Up @@ -892,13 +969,14 @@ def gen_schema_metadata(
[
TagAssociation(
make_tag_urn(
self.snowflake_identifier(tag.identifier())
self.snowflake_identifier(tag.tag_identifier())
)
)
for tag in table.column_tags[col.name]
]
)
if col.name in table.column_tags
and not self.config.extract_tags_as_structured_properties
else None
),
)
Expand Down Expand Up @@ -985,8 +1063,17 @@ def gen_database_containers(
)
),
tags=(
[self.snowflake_identifier(tag.identifier()) for tag in database.tags]
[
self.snowflake_identifier(tag.tag_identifier())
for tag in database.tags
]
if database.tags
and not self.config.extract_tags_as_structured_properties
else None
),
structured_properties=(
self._format_tags_as_structured_properties(database.tags)
if database.tags and self.config.extract_tags_as_structured_properties
else None
),
)
Expand Down Expand Up @@ -1038,8 +1125,13 @@ def gen_schema_containers(
else None
),
tags=(
[self.snowflake_identifier(tag.identifier()) for tag in schema.tags]
if schema.tags
[self.snowflake_identifier(tag.tag_identifier()) for tag in schema.tags]
if schema.tags and not self.config.extract_tags_as_structured_properties
else None
),
structured_properties=(
self._format_tags_as_structured_properties(schema.tags)
if schema.tags and self.config.extract_tags_as_structured_properties
else None
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,20 @@ def _filter_tags(

allowed_tags = []
for tag in tags:
tag_identifier = tag.identifier()
self.report.report_entity_scanned(tag_identifier, "tag")
if not self.config.tag_pattern.allowed(tag_identifier):
self.report.report_dropped(tag_identifier)
identifier = (
tag._id_prefix_as_str()
if self.config.extract_tags_as_structured_properties
else tag.tag_identifier()
)
self.report.report_entity_scanned(identifier, "tag")

pattern = (
self.config.structured_property_pattern
if self.config.extract_tags_as_structured_properties
else self.config.tag_pattern
)
if not pattern.allowed(identifier):
self.report.report_dropped(identifier)
else:
allowed_tags.append(tag)
return allowed_tags
Loading

0 comments on commit ebbadf6

Please sign in to comment.