Skip to content
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

feat(ingest/snowflake): Support ingesting snowflake tags as structured properties #12285

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -236,6 +236,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 @@ -255,6 +260,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 @@ -671,14 +685,31 @@
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 @@ -723,6 +754,9 @@
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 @@ -756,16 +790,24 @@
)

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(

Check warning on line 794 in metadata-ingestion/src/datahub/ingestion/source/snowflake/snowflake_schema_gen.py

View check run for this annotation

Codecov / codecov/patch

metadata-ingestion/src/datahub/ingestion/source/snowflake/snowflake_schema_gen.py#L794

Added line #L794 was not covered by tests
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 @@ -838,17 +880,52 @@
)

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 @@ -890,13 +967,14 @@
[
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 @@ -983,8 +1061,17 @@
)
),
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 @@ -1036,8 +1123,13 @@
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
Loading