|
| 1 | +"""Provide a way to cache dagster assets that are loaded with our factory utilities""" |
| 2 | + |
| 3 | +import typing as t |
| 4 | +from inspect import signature |
| 5 | + |
| 6 | +import dagster as dg |
| 7 | +from pydantic import BaseModel |
| 8 | + |
| 9 | + |
| 10 | +class CacheableAssetOptions(BaseModel): |
| 11 | + """Derived from dg.asset.__annotations__ |
| 12 | +
|
| 13 | + For options that are not support by the cache, we set the type to None so |
| 14 | + that attempts to use them will fail at static type checking time. |
| 15 | + """ |
| 16 | + |
| 17 | + name: str | None = None |
| 18 | + key_prefix: t.Union[str, t.Sequence[str]] | None = None |
| 19 | + ins: t.Mapping[str, dg.AssetIn] | None = None |
| 20 | + deps: t.Iterable[t.Union[dg.AssetKey, str, t.Sequence[str], dg.AssetDep]] | None = ( |
| 21 | + None |
| 22 | + ) |
| 23 | + metadata: t.Mapping[str, t.Any] | None = None |
| 24 | + tags: t.Mapping[str, str] | None = None |
| 25 | + description: str | None = None |
| 26 | + config_schema: t.Type[dg.Config] | None = None |
| 27 | + required_resource_keys: t.AbstractSet[str] | None = None |
| 28 | + resource_defs: t.Mapping[str, object] | None = None |
| 29 | + # hooks currently not supported. We set the type to None to force static type failure if used |
| 30 | + hooks: None = None |
| 31 | + io_manager_def: object | None = None |
| 32 | + io_manager_key: str | None = None |
| 33 | + # dagster_type currently not supported |
| 34 | + dagster_type: None = None |
| 35 | + # partitions_def currently not supported |
| 36 | + op_tags: t.Mapping[str, t.Any] | None = None |
| 37 | + group_name: str | None = None |
| 38 | + output_required: bool = False |
| 39 | + automation_condition: None = None |
| 40 | + freshness_policy: None = None |
| 41 | + backfill_policy: None = None |
| 42 | + retry_policy: None = None |
| 43 | + code_version: str | None = None |
| 44 | + key: t.Union[dg.AssetKey, str, t.Sequence[str]] | None = None |
| 45 | + check_specs: None = None |
| 46 | + owners: t.Sequence[str] | None = None |
| 47 | + kinds: t.AbstractSet[str] | None = None |
| 48 | + pool: str | None = None |
| 49 | + |
| 50 | + |
| 51 | +class CacheableMultiAssetOptions(BaseModel): |
| 52 | + """Derived from dg.multi_asset.__annotations__ |
| 53 | +
|
| 54 | + For options that are not support by the cache, we set the type to None so |
| 55 | + that attempts to use them will fail at static type checking time. |
| 56 | + """ |
| 57 | + |
| 58 | + name: str | None = None |
| 59 | + ins: t.Mapping[str, str] | None = None |
| 60 | + deps: t.Iterable[t.Union[str, t.Sequence[str]]] | None = None |
| 61 | + description: str | None = None |
| 62 | + config_schema: dg.Config | None = None |
| 63 | + required_resource_keys: t.AbstractSet[str] | None = None |
| 64 | + internal_asset_deps: t.Mapping[str, set[str]] | None = None |
| 65 | + backfill_policy: dg.BackfillPolicy | None = None |
| 66 | + op_tags: t.Mapping[str, t.Any] | None = None |
| 67 | + can_subset: bool = False |
| 68 | + resource_defs: t.Mapping[str, object] | None = None |
| 69 | + group_name: str | None = None |
| 70 | + retry_policy: dg.RetryPolicy | None = None |
| 71 | + code_version: str | None = None |
| 72 | + specs: t.Sequence[dg.AssetSpec] | None = None |
| 73 | + check_specs: None = None |
| 74 | + pool: str | None = None |
| 75 | + |
| 76 | + |
| 77 | +class CachedAssetOut(BaseModel): |
| 78 | + type: t.Literal["asset_out"] = "asset_out" |
| 79 | + |
| 80 | + model_key: str |
| 81 | + asset_key: str |
| 82 | + tags: t.Mapping[str, str] |
| 83 | + is_required: bool |
| 84 | + group_name: str |
| 85 | + kinds: set[str] | None |
| 86 | + |
| 87 | + def to_asset_out(self) -> dg.AssetOut: |
| 88 | + """Convert to a Dagster AssetOut object""" |
| 89 | + if "kinds" in signature(dg.AssetOut).parameters: |
| 90 | + return dg.AssetOut( |
| 91 | + key=dg.AssetKey.from_user_string(self.asset_key), |
| 92 | + tags=self.tags, |
| 93 | + is_required=self.is_required, |
| 94 | + group_name=self.group_name, |
| 95 | + kinds=self.kinds, |
| 96 | + ) |
| 97 | + return dg.AssetOut( |
| 98 | + key=dg.AssetKey.from_user_string(self.asset_key), |
| 99 | + tags=self.tags, |
| 100 | + is_required=self.is_required, |
| 101 | + group_name=self.group_name, |
| 102 | + ) |
| 103 | + |
| 104 | + @classmethod |
| 105 | + def from_asset_out(cls, model_key: str, asset_out: dg.AssetOut) -> "CachedAssetOut": |
| 106 | + """Create from a Dagster AssetOut object""" |
| 107 | + assert asset_out.key is not None, "AssetOut key must not be None" |
| 108 | + |
| 109 | + return cls( |
| 110 | + model_key=model_key, |
| 111 | + asset_key=asset_out.key.to_user_string(), |
| 112 | + tags=asset_out.tags or {}, |
| 113 | + is_required=asset_out.is_required, |
| 114 | + group_name=asset_out.group_name or "", |
| 115 | + kinds=asset_out.kinds, |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +def cacheable_asset(asset_out: dg.AssetOut) -> CachedAssetOut: |
| 120 | + """Create a asset that can be loaded from cache""" |
| 121 | + return CachedAssetOut.from_asset_out("model_key", asset_out) |
0 commit comments