-
Notifications
You must be signed in to change notification settings - Fork 153
/
relation.py
80 lines (62 loc) · 2.23 KB
/
relation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from dataclasses import dataclass
from typing import Optional
from dbt.adapters.base.relation import (
BaseRelation, ComponentName, InformationSchema
)
from dbt.utils import filter_null_values
from typing import TypeVar
Self = TypeVar('Self', bound='BigQueryRelation')
@dataclass(frozen=True, eq=False, repr=False)
class BigQueryRelation(BaseRelation):
quote_character: str = '`'
def matches(
self,
database: Optional[str] = None,
schema: Optional[str] = None,
identifier: Optional[str] = None,
) -> bool:
search = filter_null_values({
ComponentName.Database: database,
ComponentName.Schema: schema,
ComponentName.Identifier: identifier
})
if not search:
# nothing was passed in
pass
for k, v in search.items():
if not self._is_exactish_match(k, v):
return False
return True
@property
def project(self):
return self.database
@property
def dataset(self):
return self.schema
def information_schema(
self, identifier: Optional[str] = None
) -> 'BigQueryInformationSchema':
return BigQueryInformationSchema.from_relation(self, identifier)
@dataclass(frozen=True, eq=False, repr=False)
class BigQueryInformationSchema(InformationSchema):
quote_character: str = '`'
@classmethod
def get_include_policy(cls, relation, information_schema_view):
schema = True
if information_schema_view in ('SCHEMATA', 'SCHEMATA_OPTIONS', None):
schema = False
identifier = True
if information_schema_view == '__TABLES__':
identifier = False
return relation.include_policy.replace(
schema=schema,
identifier=identifier,
)
def replace(self, **kwargs):
if 'information_schema_view' in kwargs:
view = kwargs['information_schema_view']
# we also need to update the include policy, unless the caller did
# in which case it's their problem
if 'include_policy' not in kwargs:
kwargs['include_policy'] = self.get_include_policy(self, view)
return super().replace(**kwargs)