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

Feature/external catalog config #334

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions dbt/adapters/base/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from dbt.adapters.base.meta import available
from dbt.adapters.base.column import Column
from dbt.adapters.base.catalog import ExternalCatalogIntegration
from dbt.adapters.base.connections import BaseConnectionManager
from dbt.adapters.base.impl import (
AdapterConfig,
Expand Down
59 changes: 59 additions & 0 deletions dbt/adapters/base/catalog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import abc
from typing import Self, ValuesView

from dbt_config.catalog_config import ExternalCatalog

from dbt.adapters.base import BaseRelation, BaseConnectionManager


class ExternalCatalogIntegration(abc.ABC):
name: str
external_catalog: ExternalCatalog
_connection_manager: BaseConnectionManager
_exists: bool

@classmethod
def create(cls, external_catalog: ExternalCatalog, connection_manager: BaseConnectionManager) -> Self:
integration = ExternalCatalogIntegration()
integration.external_catalog = external_catalog
integration.name = external_catalog.name
_connection_manager = connection_manager
return integration

@abc.abstractmethod
def _exists(self) -> bool:
pass

def exists(self) -> bool:
return self._exists
@abc.abstractmethod
def relation_exists(self, relation: BaseRelation) -> bool:
pass

@abc.abstractmethod
def refresh_relation(self, table_name: str) -> None:
pass

@abc.abstractmethod
def create_relation(self, table_name: str) -> None:
pass


class ExternalCatalogIntegrations:
def get(self, name: str) -> ExternalCatalogIntegration:
return self.integrations[name]

@property
def integrations(self) -> dict[str, ExternalCatalogIntegration]:
return self.integrations

@classmethod
def from_json_strings(cls, json_strings: ValuesView[str],
integration_class: ExternalCatalogIntegration,
connection_manager: BaseConnectionManager) -> Self:
new_instance = cls()
for json_string in json_strings:
external_catalog = ExternalCatalog.model_validate_json(json_string)
integration = integration_class.create(external_catalog, connection_manager)
new_instance.integrations[integration.name] = integration
return new_instance
5 changes: 5 additions & 0 deletions dbt/adapters/base/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
)

from dbt.adapters.base.column import Column as BaseColumn
from dbt.adapters.base.catalog import ExternalCatalogIntegration
from dbt.adapters.base.connections import (
AdapterResponse,
BaseConnectionManager,
Expand Down Expand Up @@ -228,6 +229,7 @@ class BaseAdapter(metaclass=AdapterMeta):
- expand_column_types
- list_relations_without_caching
- is_cancelable
- execute
- create_schema
- drop_schema
- quote
Expand All @@ -241,11 +243,14 @@ class BaseAdapter(metaclass=AdapterMeta):

Macros:
- get_catalog

External Catalog support: Attach an implementation of ExternalCatalogIntegration
"""

Relation: Type[BaseRelation] = BaseRelation
Column: Type[BaseColumn] = BaseColumn
ConnectionManager: Type[BaseConnectionManager]
ExternalCatalogIntegration: Type[ExternalCatalogIntegration]

# A set of clobber config fields accepted by this adapter
# for use in materializations
Expand Down
1 change: 1 addition & 0 deletions dbt/adapters/base/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class BaseRelation(FakeAPIObject, Hashable):
# e.g. adding RelationType.View in dbt-postgres requires that you define:
# include/postgres/macros/relations/view/replace.sql::postgres__get_replace_view_sql()
replaceable_relations: SerializableIterable = field(default_factory=frozenset)
catalog: Optional[str] = None

def _is_exactish_match(self, field: ComponentName, value: str) -> bool:
if self.dbt_created and self.quote_policy.get_part(field) is False:
Expand Down
2 changes: 2 additions & 0 deletions dbt/adapters/capability.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Capability(str, Enum):
"""Indicates support for getting catalog information including table-level and column-level metadata for a single
relation."""

CreateExternalCatalog = "CreateExternalCatalog"


class Support(str, Enum):
Unknown = "Unknown"
Expand Down
3 changes: 3 additions & 0 deletions dbt/adapters/contracts/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
ValidatedStringMixin,
dbtClassMixin,
)
from dbt_config.catalog_config import ExternalCatalogConfig


# TODO: this is a very bad dependency - shared global state
from dbt_common.events.contextvars import get_node_info
Expand Down Expand Up @@ -226,3 +228,4 @@ class AdapterRequiredConfig(HasCredentials, Protocol):
cli_vars: Dict[str, Any]
target_path: str
log_cache_events: bool
catalogs = Optional[ExternalCatalogConfig]
2 changes: 2 additions & 0 deletions dbt/adapters/contracts/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from dbt_common.dataclass_schema import StrEnum, dbtClassMixin
from dbt_common.exceptions import CompilationError, DataclassNotDictError
from dbt_common.utils import deep_merge
from dbt_config.catalog_config import ExternalCatalog
from typing_extensions import Protocol


Expand Down Expand Up @@ -58,6 +59,7 @@ class RelationConfig(Protocol):
tags: List[str]
quoting_dict: Dict[str, bool]
config: Optional[MaterializationConfig]
catalog_name: Optional[str]


class ComponentName(StrEnum):
Expand Down
6 changes: 6 additions & 0 deletions dbt/adapters/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class ConnectionManagerProtocol(Protocol):
class ColumnProtocol(Protocol):
pass

class ExternalCatalogIntegrationProtocol(Protocol):
pass


Self = TypeVar("Self", bound="RelationProtocol")

Expand All @@ -62,6 +65,7 @@ def create_from(
ConnectionManager_T = TypeVar("ConnectionManager_T", bound=ConnectionManagerProtocol)
Relation_T = TypeVar("Relation_T", bound=RelationProtocol)
Column_T = TypeVar("Column_T", bound=ColumnProtocol)
ExtCatInteg_T = TypeVar("ExtCatInteg_T", bound=ExternalCatalogIntegrationProtocol)


class MacroContextGeneratorCallable(Protocol):
Expand All @@ -82,6 +86,7 @@ class AdapterProtocol( # type: ignore[misc]
ConnectionManager_T,
Relation_T,
Column_T,
ExtCatInteg_T,
],
):
# N.B. Technically these are ClassVars, but mypy doesn't support putting type vars in a
Expand All @@ -92,6 +97,7 @@ class AdapterProtocol( # type: ignore[misc]
Relation: Type[Relation_T]
ConnectionManager: Type[ConnectionManager_T]
connections: ConnectionManager_T
ExternalCatalogIntegration: Type[ExtCatInteg_T]

def __init__(self, config: AdapterRequiredConfig) -> None: ...

Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ classifiers = [
]
dependencies = [
"dbt-common>=1.10,<2.0",
"dbt-config<1.0",
"pytz>=2015.7",
# installed via dbt-common but used directly
"agate>=1.0,<2.0",
Expand Down Expand Up @@ -54,6 +55,7 @@ include = ["dbt/adapters", "dbt/include", "dbt/__init__.py"]

[tool.hatch.envs.default]
dependencies = [
"dbt-config @ git+https://github.com/dbt-labs/dbt-common.git@feature/externalCatalogConfig#subdirectory=config",
"dbt_common @ git+https://github.com/dbt-labs/dbt-common.git",
'pre-commit==3.7.0;python_version>="3.9"',
'pre-commit==3.5.0;python_version=="3.8"',
Expand Down
Loading