-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathdbt_project.py
More file actions
396 lines (356 loc) · 14.6 KB
/
dbt_project.py
File metadata and controls
396 lines (356 loc) · 14.6 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import json
import os
from contextlib import contextmanager, nullcontext
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Any, Dict, Generator, List, Literal, Optional, Union, overload
from uuid import uuid4
from adapter_query_runner import AdapterQueryRunner, UnsupportedJinjaError
from data_seeder import ClickHouseDirectSeeder, DbtDataSeeder, SparkS3CsvSeeder
from dbt_utils import get_database_and_schema_properties
from elementary.clients.dbt.base_dbt_runner import BaseDbtRunner
from elementary.clients.dbt.factory import RunnerMethod, create_dbt_runner
from logger import get_logger
from ruamel.yaml import YAML
PYTEST_XDIST_WORKER = os.environ.get("PYTEST_XDIST_WORKER", None)
SCHEMA_NAME_SUFFIX = f"_{PYTEST_XDIST_WORKER}" if PYTEST_XDIST_WORKER else ""
_DEFAULT_VARS = {
"disable_dbt_invocation_autoupload": True,
"disable_dbt_artifacts_autoupload": True,
"columns_upload_strategy": "none",
"disable_run_results": True,
"disable_freshness_results": True,
"debug_logs": True,
"schema_name_suffix": SCHEMA_NAME_SUFFIX,
}
DEFAULT_DUMMY_CODE = "SELECT 1 AS col"
logger = get_logger(__name__)
def get_dbt_runner(
target: str, project_dir: str, runner_method: Optional[RunnerMethod] = None
) -> BaseDbtRunner:
return create_dbt_runner(
project_dir,
target=target,
vars=_DEFAULT_VARS.copy(),
raise_on_failure=False,
runner_method=runner_method,
)
class DbtProject:
def __init__(
self,
target: str,
project_dir: str,
runner_method: Optional[RunnerMethod] = None,
):
self.dbt_runner = get_dbt_runner(target, project_dir, runner_method)
self.target = target
self.runner_method = runner_method
self.project_dir_path = Path(project_dir)
self.models_dir_path = self.project_dir_path / "models"
self.tmp_models_dir_path = self.models_dir_path / "tmp"
self.seeds_dir_path = self.project_dir_path / "data"
self._query_runner: Optional[AdapterQueryRunner] = None
def _get_query_runner(self) -> AdapterQueryRunner:
"""Lazily initialize the direct adapter query runner."""
if self._query_runner is None:
self._query_runner = AdapterQueryRunner(
str(self.project_dir_path), self.target
)
return self._query_runner
def run_query(self, prerendered_query: str):
# Fast path: queries that only contain {{ ref() }} / {{ source() }}
# can be executed directly through the adapter, bypassing
# run_operation log parsing entirely.
try:
return self._get_query_runner().run_query(prerendered_query)
except UnsupportedJinjaError:
logger.debug("Query contains complex Jinja; falling back to run_operation")
# Slow path: full Jinja rendering via run_operation.
return self._run_query_with_run_operation(prerendered_query)
def _run_query_with_run_operation(self, prerendered_query: str):
"""Execute a query via run_operation."""
run_operation_results = self.dbt_runner.run_operation(
"elementary.render_run_query",
macro_args={"prerendered_query": prerendered_query},
)
if not run_operation_results:
raise RuntimeError(
f"run_operation('elementary.render_run_query') returned no output. "
f"Query: {prerendered_query!r}"
)
return json.loads(run_operation_results[0])
def read_table_query(
self,
table_name: str,
where: Optional[str] = None,
group_by: Optional[str] = None,
order_by: Optional[str] = None,
limit: Optional[int] = None,
column_names: Optional[List[str]] = None,
):
is_tsql = self.target in ("fabric", "sqlserver")
columns = ", ".join(column_names) if column_names else "*"
top_clause = f"TOP {limit} " if limit and is_tsql else ""
limit_clause = f"LIMIT {limit}" if limit and not is_tsql else ""
return f"""
SELECT {top_clause}{columns}
FROM {{{{ ref('{table_name}') }}}}
{f"WHERE {where}" if where else ""}
{f"GROUP BY {group_by}" if group_by else ""}
{f"ORDER BY {order_by}" if order_by else ""}
{limit_clause}
"""
def read_table(
self,
table_name: str,
where: Optional[str] = None,
group_by: Optional[str] = None,
order_by: Optional[str] = None,
limit: Optional[int] = None,
column_names: Optional[List[str]] = None,
raise_if_empty: bool = True,
) -> List[dict]:
query = self.read_table_query(
table_name, where, group_by, order_by, limit, column_names
)
results = self.run_query(query)
if raise_if_empty and len(results) == 0:
raise ValueError(
f"Table '{table_name}' with the '{where}' condition is empty."
)
return results
@overload
def test(
self,
test_id: str,
dbt_test_name: str,
test_args: Optional[Dict[str, Any]] = None,
test_column: Optional[str] = None,
columns: Optional[List[dict]] = None,
data: Optional[List[dict]] = None,
as_model: bool = False,
table_name: Optional[str] = None,
materialization: str = "table", # Only relevant if as_model=True
test_vars: Optional[dict] = None,
elementary_enabled: bool = True,
model_config: Optional[Dict[str, Any]] = None,
test_config: Optional[Dict[str, Any]] = None,
*,
multiple_results: Literal[False] = False,
) -> Dict[str, Any]:
...
@overload
def test(
self,
test_id: str,
dbt_test_name: str,
test_args: Optional[Dict[str, Any]] = None,
test_column: Optional[str] = None,
columns: Optional[List[dict]] = None,
data: Optional[List[dict]] = None,
as_model: bool = False,
table_name: Optional[str] = None,
materialization: str = "table", # Only relevant if as_model=True
test_vars: Optional[dict] = None,
elementary_enabled: bool = True,
model_config: Optional[Dict[str, Any]] = None,
test_config: Optional[Dict[str, Any]] = None,
*,
multiple_results: Literal[True],
) -> List[Dict[str, Any]]:
...
def test(
self,
test_id: str,
dbt_test_name: str,
test_args: Optional[Dict[str, Any]] = None,
test_column: Optional[str] = None,
columns: Optional[List[dict]] = None,
data: Optional[List[dict]] = None,
as_model: bool = False,
table_name: Optional[str] = None,
materialization: str = "table", # Only relevant if as_model=True
test_vars: Optional[dict] = None,
elementary_enabled: bool = True,
model_config: Optional[Dict[str, Any]] = None,
column_config: Optional[Dict[str, Any]] = None,
test_config: Optional[Dict[str, Any]] = None,
*,
multiple_results: bool = False,
) -> Union[Dict[str, Any], List[Dict[str, Any]]]:
if columns and test_column:
raise ValueError("You can't specify both 'columns' and 'test_column'.")
test_vars = test_vars or {}
test_vars["elementary_enabled"] = elementary_enabled
test_id = test_id.replace("[", "_").replace("]", "_")
if not table_name:
table_name = test_id
test_args = test_args or {}
table_yaml: Dict[str, Any] = {"name": test_id}
if model_config:
table_yaml.update(model_config)
if columns:
table_yaml["columns"] = columns
test_yaml = {dbt_test_name: {"arguments": test_args}}
if test_config:
test_yaml[dbt_test_name]["config"] = test_config
if test_column is None:
table_yaml["tests"] = [test_yaml]
else:
column_def = {
"name": test_column,
"tests": [test_yaml],
}
if column_config:
column_def["config"] = column_config
table_yaml["columns"] = [column_def]
temp_table_ctx: Any
if as_model:
props_yaml = {
"version": 2,
"models": [table_yaml],
}
temp_table_ctx = self.create_temp_model_for_existing_table(
test_id, materialization
)
else:
database_property, schema_property = get_database_and_schema_properties(
self.target
)
source_def: Dict[str, Any] = {
"name": "test_data",
"schema": f"{{{{ target.{schema_property} }}}}{SCHEMA_NAME_SUFFIX}",
"tables": [table_yaml],
}
if database_property is not None:
source_def["database"] = f"{{{{ target.{database_property} }}}}"
props_yaml = {
"version": 2,
"sources": [source_def],
}
temp_table_ctx = nullcontext()
if data:
self.seed(data, table_name)
with temp_table_ctx:
with NamedTemporaryFile(
dir=self.tmp_models_dir_path,
prefix="integration_tests_",
suffix=".yaml",
) as props_file:
YAML().dump(props_yaml, props_file)
relative_props_path = Path(props_file.name).relative_to(
self.project_dir_path
)
test_process_success = self.dbt_runner.test(
select=str(relative_props_path), vars=test_vars
)
if elementary_enabled:
if multiple_results:
return self._read_test_results(test_id)
else:
return self._read_single_test_result(test_id)
else:
# If we disabled elementary, elementary_test_results will also be empty. So we'll simulate the result
# based on the process status (which means we can't differentiate between fail and error)
test_result = {
"status": "pass" if test_process_success else "fail_or_error"
}
return [test_result] if multiple_results else test_result
def _read_profile_schema(self) -> str:
"""Read the base schema name from the rendered dbt profiles.yml."""
profiles_dir = os.environ.get("DBT_PROFILES_DIR", os.path.expanduser("~/.dbt"))
profiles_path = Path(profiles_dir) / "profiles.yml"
if not profiles_path.exists():
raise RuntimeError(f"dbt profiles not found at: {profiles_path}")
yaml = YAML()
with profiles_path.open() as fh:
profiles = yaml.load(fh) or {}
try:
return profiles["elementary_tests"]["outputs"][self.target]["schema"]
except KeyError as exc:
raise RuntimeError(
f"Missing schema for target '{self.target}' in {profiles_path}"
) from exc
def _create_seeder(
self,
) -> Union[DbtDataSeeder, ClickHouseDirectSeeder, SparkS3CsvSeeder]:
"""Return the fastest available seeder for the current target."""
if self.target == "clickhouse":
runner = self._get_query_runner()
schema = runner.schema_name + SCHEMA_NAME_SUFFIX
return ClickHouseDirectSeeder(runner, schema, self.seeds_dir_path)
if self.target == "spark":
# Read schema from dbt profiles directly — avoids creating an
# AdapterQueryRunner (which corrupts dbt global state via
# set_from_args / reset_adapters).
schema = self._read_profile_schema() + SCHEMA_NAME_SUFFIX
return SparkS3CsvSeeder(schema, self.seeds_dir_path)
return DbtDataSeeder(
self.dbt_runner, self.project_dir_path, self.seeds_dir_path
)
def seed(self, data: List[dict], table_name: str):
with self._create_seeder().seed(data, table_name):
self._fix_seed_if_needed(table_name)
def _fix_seed_if_needed(self, table_name: str) -> None:
# Hack for BigQuery - seems like we get empty strings instead of nulls in seeds, so we
# fix them here.
if self.runner_method == RunnerMethod.FUSION and self.target == "bigquery":
self.dbt_runner.run_operation(
"elementary_tests.replace_empty_strings_with_nulls",
macro_args={"table_name": table_name},
)
@contextmanager
def seed_context(
self, data: List[dict], table_name: str
) -> Generator[None, None, None]:
with self._create_seeder().seed(data, table_name):
self._fix_seed_if_needed(table_name)
yield
@contextmanager
def create_temp_model_for_existing_table(
self,
table_name: str,
materialization: Optional[str] = None,
raw_code: Optional[str] = None,
):
model_path = self.tmp_models_dir_path.joinpath(f"{table_name}.sql")
code = raw_code or DEFAULT_DUMMY_CODE
model_text = ""
if materialization:
model_text += f"{{{{ config(materialized='{materialization}') }}}}\n"
model_text += code
model_path.write_text(model_text)
relative_model_path = model_path.relative_to(self.project_dir_path)
try:
yield relative_model_path
finally:
model_path.unlink()
def _read_test_results(self, table_name: str) -> List[Dict[str, Any]]:
test_execution_id_subquery = self.read_table_query(
"elementary_test_results",
where=f"lower(table_name) = lower('{table_name}')",
order_by="created_at DESC",
column_names=["test_execution_id"],
limit=1,
)
return self.read_table(
"elementary_test_results",
where=f"test_execution_id IN ({test_execution_id_subquery})",
)
def _read_single_test_result(self, table_name: str) -> Dict[str, Any]:
results = self._read_test_results(table_name)
if len(results) == 0:
raise Exception(f"No test result found for table {table_name}")
if len(results) > 1:
raise Exception(f"Multiple test results found for table {table_name}")
return results[0]
@contextmanager
def write_yaml(self, content: dict, name: Optional[str] = None):
name = name or f"{uuid4()}.yaml"
path = self.models_dir_path / name
with open(path, "w") as f:
YAML().dump(content, f)
try:
yield path
finally:
path.unlink()