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

Enable dlt asset metadata via translator #22047

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -99,6 +99,7 @@ def github_reactions_dagster_assets(context: AssetExecutionContext, dlt: DltDags
META_KEY_SOURCE: dlt_source,
META_KEY_PIPELINE: dlt_pipeline,
META_KEY_TRANSLATOR: dlt_dagster_translator,
**dlt_dagster_translator.get_metadata(dlt_source_resource),
},
)
for dlt_source_resource in dlt_source.selected_resources.values()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Iterable, Optional
from typing import Any, Iterable, Mapping, Optional

from dagster import (
AssetKey,
Expand Down Expand Up @@ -51,3 +51,15 @@ def get_auto_materialize_policy(self, resource: DltResource) -> Optional[AutoMat

"""
return None

@public
def get_metadata(self, resource: DltResource) -> Mapping[str, Any]:
"""Defines resource specific metadata.

Args:
resource (DltResource): dlt resource / transformer

Returns:
Mapping[str, Any]: The custom metadata entries for this resource.
"""
return {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from typing import Any, Mapping, Optional

import duckdb
from dagster import (
Expand Down Expand Up @@ -205,3 +205,46 @@ def example_pipeline_assets(
assert not res.success
asset_materizations = res.get_asset_materialization_events()
assert len(asset_materizations) == 0


def test_asset_metadata(dlt_pipeline: Pipeline) -> None:
class CustomDagsterDltTranslator(DagsterDltTranslator):
metadata_by_resource_name = {
"repos": {"mode": "upsert", "primary_key": "id"},
"repo_issues": {"mode": "upsert", "primary_key": ["repo_id", "issue_id"]},
}

def get_metadata(self, resource: DltResource) -> Mapping[str, Any]:
return self.metadata_by_resource_name.get(resource.name, {})

@dlt_assets(
dlt_source=pipeline(),
dlt_pipeline=dlt_pipeline,
dlt_dagster_translator=CustomDagsterDltTranslator(),
)
def example_pipeline_assets(
context: AssetExecutionContext, dlt_pipeline_resource: DagsterDltResource
):
yield from dlt_pipeline_resource.run(context=context)

first_asset_metadata = next(iter(example_pipeline_assets.metadata_by_key.values()))
dagster_dlt_source = first_asset_metadata.get("dagster_dlt/source")
dagster_dlt_pipeline = first_asset_metadata.get("dagster_dlt/pipeline")
dagster_dlt_translator = first_asset_metadata.get("dagster_dlt/translator")

assert example_pipeline_assets.metadata_by_key == {
AssetKey("dlt_pipeline_repos"): {
"dagster_dlt/source": dagster_dlt_source,
"dagster_dlt/pipeline": dagster_dlt_pipeline,
"dagster_dlt/translator": dagster_dlt_translator,
"mode": "upsert",
"primary_key": "id",
},
AssetKey("dlt_pipeline_repo_issues"): {
"dagster_dlt/source": dagster_dlt_source,
"dagster_dlt/pipeline": dagster_dlt_pipeline,
"dagster_dlt/translator": dagster_dlt_translator,
"mode": "upsert",
"primary_key": ["repo_id", "issue_id"],
},
}