-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathgenerate.py
300 lines (250 loc) · 9.67 KB
/
generate.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import os
import shutil
from datetime import datetime
from typing import Dict, List, Any, Optional, Tuple, Set
from dbt.dataclass_schema import ValidationError
from .compile import CompileTask
from dbt.adapters.factory import get_adapter
from dbt.contracts.graph.compiled import CompileResultNode
from dbt.contracts.graph.manifest import Manifest
from dbt.contracts.results import (
NodeStatus, TableMetadata, CatalogTable, CatalogResults, PrimitiveDict,
CatalogKey, StatsItem, StatsDict, ColumnMetadata, CatalogArtifact
)
from dbt.exceptions import InternalException
from dbt.include.global_project import DOCS_INDEX_FILE_PATH
from dbt.events.functions import fire_event
from dbt.events.types import (
WriteCatalogFailure, CatalogWritten, CannotGenerateDocs, BuildingCatalog
)
from dbt.parser.manifest import ManifestLoader
import dbt.utils
import dbt.compilation
import dbt.exceptions
CATALOG_FILENAME = 'catalog.json'
def get_stripped_prefix(source: Dict[str, Any], prefix: str) -> Dict[str, Any]:
"""Go through source, extracting every key/value pair where the key starts
with the given prefix.
"""
cut = len(prefix)
return {
k[cut:]: v for k, v in source.items()
if k.startswith(prefix)
}
def build_catalog_table(data) -> CatalogTable:
# build the new table's metadata + stats
metadata = TableMetadata.from_dict(get_stripped_prefix(data, 'table_'))
stats = format_stats(get_stripped_prefix(data, 'stats:'))
return CatalogTable(
metadata=metadata,
stats=stats,
columns={},
)
# keys are database name, schema name, table name
class Catalog(Dict[CatalogKey, CatalogTable]):
def __init__(self, columns: List[PrimitiveDict]):
super().__init__()
for col in columns:
self.add_column(col)
def get_table(self, data: PrimitiveDict) -> CatalogTable:
database = data.get('table_database')
if database is None:
dkey: Optional[str] = None
else:
dkey = str(database)
try:
key = CatalogKey(
dkey,
str(data['table_schema']),
str(data['table_name']),
)
except KeyError as exc:
raise dbt.exceptions.CompilationException(
'Catalog information missing required key {} (got {})'
.format(exc, data)
)
table: CatalogTable
if key in self:
table = self[key]
else:
table = build_catalog_table(data)
self[key] = table
return table
def add_column(self, data: PrimitiveDict):
table = self.get_table(data)
column_data = get_stripped_prefix(data, 'column_')
# the index should really never be that big so it's ok to end up
# serializing this to JSON (2^53 is the max safe value there)
column_data['index'] = int(column_data['index'])
column = ColumnMetadata.from_dict(column_data)
table.columns[column.name] = column
def make_unique_id_map(
self, manifest: Manifest
) -> Tuple[Dict[str, CatalogTable], Dict[str, CatalogTable]]:
nodes: Dict[str, CatalogTable] = {}
sources: Dict[str, CatalogTable] = {}
node_map, source_map = get_unique_id_mapping(manifest)
table: CatalogTable
for table in self.values():
key = table.key()
if key in node_map:
unique_id = node_map[key]
nodes[unique_id] = table.replace(unique_id=unique_id)
unique_ids = source_map.get(table.key(), set())
for unique_id in unique_ids:
if unique_id in sources:
dbt.exceptions.raise_ambiguous_catalog_match(
unique_id,
sources[unique_id].to_dict(omit_none=True),
table.to_dict(omit_none=True),
)
else:
sources[unique_id] = table.replace(unique_id=unique_id)
return nodes, sources
def format_stats(stats: PrimitiveDict) -> StatsDict:
"""Given a dictionary following this layout:
{
'encoded:label': 'Encoded',
'encoded:value': 'Yes',
'encoded:description': 'Indicates if the column is encoded',
'encoded:include': True,
'size:label': 'Size',
'size:value': 128,
'size:description': 'Size of the table in MB',
'size:include': True,
}
format_stats will convert the dict into a StatsDict with keys of 'encoded'
and 'size'.
"""
stats_collector: StatsDict = {}
base_keys = {k.split(':')[0] for k in stats}
for key in base_keys:
dct: PrimitiveDict = {'id': key}
for subkey in ('label', 'value', 'description', 'include'):
dct[subkey] = stats['{}:{}'.format(key, subkey)]
try:
stats_item = StatsItem.from_dict(dct)
except ValidationError:
continue
if stats_item.include:
stats_collector[key] = stats_item
# we always have a 'has_stats' field, it's never included
has_stats = StatsItem(
id='has_stats',
label='Has Stats?',
value=len(stats_collector) > 0,
description='Indicates whether there are statistics for this table',
include=False,
)
stats_collector['has_stats'] = has_stats
return stats_collector
def mapping_key(node: CompileResultNode) -> CatalogKey:
dkey = dbt.utils.lowercase(node.database)
return CatalogKey(
dkey, node.schema.lower(), node.identifier.lower()
)
def get_unique_id_mapping(
manifest: Manifest
) -> Tuple[Dict[CatalogKey, str], Dict[CatalogKey, Set[str]]]:
# A single relation could have multiple unique IDs pointing to it if a
# source were also a node.
node_map: Dict[CatalogKey, str] = {}
source_map: Dict[CatalogKey, Set[str]] = {}
for unique_id, node in manifest.nodes.items():
key = mapping_key(node)
node_map[key] = unique_id
for unique_id, source in manifest.sources.items():
key = mapping_key(source)
if key not in source_map:
source_map[key] = set()
source_map[key].add(unique_id)
return node_map, source_map
class GenerateTask(CompileTask):
def _get_manifest(self) -> Manifest:
if self.manifest is None:
raise InternalException(
'manifest should not be None in _get_manifest'
)
return self.manifest
def run(self) -> CatalogArtifact:
compile_results = None
if self.args.compile:
compile_results = CompileTask.run(self)
if any(r.status == NodeStatus.Error for r in compile_results):
fire_event(CannotGenerateDocs())
return CatalogArtifact.from_results(
nodes={},
sources={},
generated_at=datetime.utcnow(),
errors=None,
compile_results=compile_results
)
else:
self.manifest = ManifestLoader.get_full_manifest(self.config)
shutil.copyfile(
DOCS_INDEX_FILE_PATH,
os.path.join(self.config.target_path, 'index.html'))
for asset_path in self.config.asset_paths:
to_asset_path = os.path.join(self.config.target_path, asset_path)
if os.path.exists(to_asset_path):
shutil.rmtree(to_asset_path)
if os.path.exists(asset_path):
shutil.copytree(
asset_path,
to_asset_path)
if self.manifest is None:
raise InternalException(
'self.manifest was None in run!'
)
adapter = get_adapter(self.config)
with adapter.connection_named('generate_catalog'):
fire_event(BuildingCatalog())
catalog_table, exceptions = adapter.get_catalog(self.manifest)
catalog_data: List[PrimitiveDict] = [
dict(zip(catalog_table.column_names, map(dbt.utils._coerce_decimal, row)))
for row in catalog_table
]
catalog = Catalog(catalog_data)
errors: Optional[List[str]] = None
if exceptions:
errors = [str(e) for e in exceptions]
nodes, sources = catalog.make_unique_id_map(self.manifest)
results = self.get_catalog_results(
nodes=nodes,
sources=sources,
generated_at=datetime.utcnow(),
compile_results=compile_results,
errors=errors,
)
path = os.path.join(self.config.target_path, CATALOG_FILENAME)
results.write(path)
if self.args.compile:
self.write_manifest()
if exceptions:
fire_event(WriteCatalogFailure(num_exceptions=len(exceptions)))
fire_event(CatalogWritten(path=os.path.abspath(path)))
return results
def get_catalog_results(
self,
nodes: Dict[str, CatalogTable],
sources: Dict[str, CatalogTable],
generated_at: datetime,
compile_results: Optional[Any],
errors: Optional[List[str]]
) -> CatalogArtifact:
return CatalogArtifact.from_results(
generated_at=generated_at,
nodes=nodes,
sources=sources,
compile_results=compile_results,
errors=errors,
)
def interpret_results(self, results: Optional[CatalogResults]) -> bool:
if results is None:
return False
if results.errors:
return False
compile_results = results._compile_results
if compile_results is None:
return True
return super().interpret_results(compile_results)