|
80 | 80 | from sqlmesh.core.loader import Loader |
81 | 81 | from sqlmesh.core.linter.definition import AnnotatedRuleViolation, Linter |
82 | 82 | from sqlmesh.core.linter.rules import BUILTIN_RULES |
83 | | -from sqlmesh.core.macros import ExecutableOrMacro, macro |
| 83 | +from sqlmesh.core.macros import ExecutableOrMacro, RuntimeStage, macro |
84 | 84 | from sqlmesh.core.metric import Metric, rewrite |
85 | 85 | from sqlmesh.core.model import Model, update_model_schemas |
86 | 86 | from sqlmesh.core.config.model import ModelDefaultsConfig |
@@ -1286,6 +1286,87 @@ def render( |
1286 | 1286 | **kwargs, |
1287 | 1287 | ) |
1288 | 1288 |
|
| 1289 | + @python_api_analytics |
| 1290 | + def render_audit( |
| 1291 | + self, |
| 1292 | + model_or_snapshot: t.Optional[ModelOrSnapshot] = None, |
| 1293 | + *, |
| 1294 | + audit_name: str, |
| 1295 | + start: t.Optional[TimeLike] = None, |
| 1296 | + end: t.Optional[TimeLike] = None, |
| 1297 | + execution_time: t.Optional[TimeLike] = None, |
| 1298 | + ) -> t.List[exp.Query]: |
| 1299 | + """Render an audit's failure queries without running them. |
| 1300 | +
|
| 1301 | + Args: |
| 1302 | + model_or_snapshot: The audited model, name, or snapshot. Omit for a standalone audit. |
| 1303 | + audit_name: The audit metadata name. All effective invocations on the model are rendered. |
| 1304 | + start: The start of the interval to render. |
| 1305 | + end: The end of the interval to render. |
| 1306 | + execution_time: The execution time reference, defaulting to now for the entire call. |
| 1307 | +
|
| 1308 | + Returns: |
| 1309 | + Failure queries in invocation order, including explicitly requested skipped audits. |
| 1310 | +
|
| 1311 | + Rendering can read column metadata, and user macros retain their normal execution behavior. |
| 1312 | + """ |
| 1313 | + audit_name = audit_name.lower() |
| 1314 | + model = ( |
| 1315 | + self.get_model(model_or_snapshot, raise_if_missing=True) |
| 1316 | + if model_or_snapshot is not None |
| 1317 | + else None |
| 1318 | + ) |
| 1319 | + if model is not None: |
| 1320 | + audits = [ |
| 1321 | + (audit, args) for audit, args in model.audits_with_args if audit.name == audit_name |
| 1322 | + ] |
| 1323 | + if not audits: |
| 1324 | + raise SQLMeshError(f"Audit '{audit_name}' is not attached to model '{model.name}'.") |
| 1325 | + else: |
| 1326 | + standalone_audit = self.standalone_audits.get(audit_name) |
| 1327 | + if standalone_audit is None: |
| 1328 | + from sqlmesh.core.audit.builtin import BUILT_IN_AUDITS |
| 1329 | + |
| 1330 | + if audit_name in BUILT_IN_AUDITS or any( |
| 1331 | + audit.name == audit_name |
| 1332 | + for model in self.models.values() |
| 1333 | + for audit, _ in model.audits_with_args |
| 1334 | + ): |
| 1335 | + raise SQLMeshError(f"Audit '{audit_name}' requires a model. Supply MODEL.") |
| 1336 | + raise SQLMeshError( |
| 1337 | + f"Cannot find standalone audit '{audit_name}'. Model-attached audits require MODEL." |
| 1338 | + ) |
| 1339 | + audits = [(standalone_audit, {})] |
| 1340 | + |
| 1341 | + snapshots = self.snapshots |
| 1342 | + deployability_index = DeployabilityIndex.create(snapshots.values(), start=start) |
| 1343 | + execution_time = now() if execution_time is None else execution_time |
| 1344 | + adapter = self._get_engine_adapter(model.gateway if model is not None else None) |
| 1345 | + queries = [] |
| 1346 | + for audit, audit_args in audits: |
| 1347 | + audit_args = audit_args.copy() |
| 1348 | + audit_args.pop("blocking", None) |
| 1349 | + kwargs: t.Dict[str, t.Any] = { |
| 1350 | + "start": start, |
| 1351 | + "end": end, |
| 1352 | + "execution_time": execution_time, |
| 1353 | + "snapshots": snapshots, |
| 1354 | + "deployability_index": deployability_index, |
| 1355 | + "engine_adapter": adapter, |
| 1356 | + "runtime_stage": RuntimeStage.AUDITING, |
| 1357 | + **audit_args, |
| 1358 | + } |
| 1359 | + if model is not None: |
| 1360 | + snapshot = snapshots.get(model.fqn) |
| 1361 | + # Before the first plan there is no physical table to map to, as in model rendering. |
| 1362 | + if snapshot is not None and snapshot.version is None: |
| 1363 | + kwargs.setdefault("this_model", model.fqn) |
| 1364 | + query = model.render_audit_query(audit, **kwargs) |
| 1365 | + else: |
| 1366 | + query = t.cast(StandaloneAudit, audit).render_audit_query(**kwargs) |
| 1367 | + queries.append(query) |
| 1368 | + return queries |
| 1369 | + |
1289 | 1370 | @python_api_analytics |
1290 | 1371 | def evaluate( |
1291 | 1372 | self, |
|
0 commit comments