|
1 | 1 | from dataclasses import dataclass, field |
2 | | -from typing import Optional |
| 2 | +from typing import Any, Optional, Type |
3 | 3 |
|
4 | | -from dbt.adapters.base.relation import BaseRelation, Policy |
| 4 | +from dbt.adapters.base.relation import BaseRelation, Policy, Self |
| 5 | +from dbt.contracts.graph.nodes import SourceDefinition |
5 | 6 | from dbt.exceptions import DbtRuntimeError |
| 7 | +from dbt.utils import deep_merge |
6 | 8 |
|
7 | 9 |
|
8 | 10 | @dataclass |
@@ -47,3 +49,27 @@ def matches( |
47 | 49 | if schema: |
48 | 50 | raise DbtRuntimeError(f'Passed unexpected schema value {schema} to Relation.matches') |
49 | 51 | return self.database == database and self.identifier == identifier |
| 52 | + |
| 53 | + @classmethod |
| 54 | + def create_from_source(cls: Type[Self], source: SourceDefinition, **kwargs: Any) -> Self: |
| 55 | + source_quoting = source.quoting.to_dict(omit_none=True) |
| 56 | + source_quoting.pop("column", None) |
| 57 | + quote_policy = deep_merge( |
| 58 | + cls.get_default_quote_policy().to_dict(omit_none=True), |
| 59 | + source_quoting, |
| 60 | + kwargs.get("quote_policy", {}), |
| 61 | + ) |
| 62 | + |
| 63 | + # If the database is set, and the source schema is "defaulted" to the source.name, override the |
| 64 | + # schema with the database instead, since that's presumably what's intended for clickhouse |
| 65 | + schema = source.schema |
| 66 | + if schema == source.source_name and source.database: |
| 67 | + schema = source.database |
| 68 | + |
| 69 | + return cls.create( |
| 70 | + database=source.database, |
| 71 | + schema=schema, |
| 72 | + identifier=source.identifier, |
| 73 | + quote_policy=quote_policy, |
| 74 | + **kwargs, |
| 75 | + ) |
0 commit comments