|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +from dbt.adapters.catalogs import CatalogIntegration, CatalogIntegrationConfig |
| 4 | +from dbt.adapters.contracts.relation import RelationConfig |
| 5 | +from dbt.adapters.databricks import constants, parse_model |
| 6 | +from dbt.adapters.databricks.catalogs._relation import DatabricksCatalogRelation |
| 7 | + |
| 8 | + |
| 9 | +class UnityCatalogIntegration(CatalogIntegration): |
| 10 | + catalog_type = constants.UNITY_CATALOG_TYPE |
| 11 | + allows_writes = True |
| 12 | + |
| 13 | + def __init__(self, config: CatalogIntegrationConfig) -> None: |
| 14 | + super().__init__(config) |
| 15 | + if location_root := config.adapter_properties.get("location_root"): |
| 16 | + self.external_volume: Optional[str] = location_root |
| 17 | + self.file_format: str = config.adapter_properties.get("file_format") |
| 18 | + |
| 19 | + @property |
| 20 | + def location_root(self) -> Optional[str]: |
| 21 | + """ |
| 22 | + Volumes in Databricks are non-tabular datasets, which is something |
| 23 | + different from what we mean by "external_volume" in dbt. |
| 24 | + However, the protocol expects "external_volume" to be set. |
| 25 | + """ |
| 26 | + return self.external_volume |
| 27 | + |
| 28 | + @location_root.setter |
| 29 | + def location_root(self, value: Optional[str]) -> None: |
| 30 | + self.external_volume = value |
| 31 | + |
| 32 | + def build_relation(self, model: RelationConfig) -> DatabricksCatalogRelation: |
| 33 | + """ |
| 34 | + Args: |
| 35 | + model: `config.model` (not `model`) from the jinja context |
| 36 | + """ |
| 37 | + return DatabricksCatalogRelation( |
| 38 | + catalog_type=self.catalog_type, |
| 39 | + catalog_name=self.catalog_name, |
| 40 | + table_format=parse_model.table_format(model) or self.table_format, |
| 41 | + file_format=parse_model.file_format(model) or self.file_format, |
| 42 | + external_volume=parse_model.location_root(model) or self.external_volume, |
| 43 | + location_path=parse_model.location_path(model), |
| 44 | + ) |
0 commit comments