|
44 | 44 | from sqlmesh.core import constants as c |
45 | 45 | from sqlmesh.utils.errors import SQLMeshError |
46 | 46 | from sqlmesh.core.config import ModelDefaultsConfig |
47 | | -from sqlmesh.dbt.basemodel import Dependencies |
48 | 47 | from sqlmesh.dbt.builtin import BUILTIN_FILTERS, BUILTIN_GLOBALS, OVERRIDDEN_MACROS |
| 48 | +from sqlmesh.dbt.common import DBT_ALL_MODEL_ATTRS, Dependencies |
49 | 49 | from sqlmesh.dbt.model import ModelConfig |
50 | 50 | from sqlmesh.dbt.package import HookConfig, MacroConfig |
51 | 51 | from sqlmesh.dbt.seed import SeedConfig |
@@ -354,7 +354,9 @@ def _load_models_and_seeds(self) -> None: |
354 | 354 | dependencies = Dependencies( |
355 | 355 | macros=macro_references, refs=_refs(node), sources=_sources(node) |
356 | 356 | ) |
357 | | - dependencies = dependencies.union(self._extra_dependencies(sql, node.package_name)) |
| 357 | + dependencies = dependencies.union( |
| 358 | + self._extra_dependencies(sql, node.package_name, track_all_model_attrs=True) |
| 359 | + ) |
358 | 360 | dependencies = dependencies.union( |
359 | 361 | self._flatten_dependencies_from_macros(dependencies.macros, node.package_name) |
360 | 362 | ) |
@@ -548,15 +550,35 @@ def _flatten_dependencies_from_macros( |
548 | 550 | dependencies = dependencies.union(macro_dependencies) |
549 | 551 | return dependencies |
550 | 552 |
|
551 | | - def _extra_dependencies(self, target: str, package: str) -> Dependencies: |
552 | | - # We sometimes observe that the manifest doesn't capture all macros, refs, and sources within a macro. |
553 | | - # This behavior has been observed with macros like dbt.current_timestamp(), dbt_utils.slugify(), and source(). |
554 | | - # Here we apply our custom extractor to make a best effort to supplement references captured in the manifest. |
| 553 | + def _extra_dependencies( |
| 554 | + self, |
| 555 | + target: str, |
| 556 | + package: str, |
| 557 | + track_all_model_attrs: bool = False, |
| 558 | + ) -> Dependencies: |
| 559 | + """ |
| 560 | + We sometimes observe that the manifest doesn't capture all macros, refs, and sources within a macro. |
| 561 | + This behavior has been observed with macros like dbt.current_timestamp(), dbt_utils.slugify(), and source(). |
| 562 | + Here we apply our custom extractor to make a best effort to supplement references captured in the manifest. |
| 563 | + """ |
555 | 564 | dependencies = Dependencies() |
| 565 | + |
| 566 | + # Whether all `model` attributes (e.g., `model.config`) should be included in the dependencies |
| 567 | + all_model_attrs = False |
| 568 | + |
556 | 569 | for call_name, node in extract_call_names(target, cache=self._calls): |
557 | 570 | if call_name[0] == "config": |
558 | 571 | continue |
559 | | - elif isinstance(node, jinja2.nodes.Getattr): |
| 572 | + |
| 573 | + if ( |
| 574 | + track_all_model_attrs |
| 575 | + and not all_model_attrs |
| 576 | + and isinstance(node, jinja2.nodes.Call) |
| 577 | + and any(isinstance(a, jinja2.nodes.Name) and a.name == "model" for a in node.args) |
| 578 | + ): |
| 579 | + all_model_attrs = True |
| 580 | + |
| 581 | + if isinstance(node, jinja2.nodes.Getattr): |
560 | 582 | if call_name[0] == "model": |
561 | 583 | dependencies.model_attrs.add(call_name[1]) |
562 | 584 | elif call_name[0] == "source": |
@@ -602,6 +624,14 @@ def _extra_dependencies(self, target: str, package: str) -> Dependencies: |
602 | 624 | call_name[0], call_name[1], dependencies.macros.append |
603 | 625 | ) |
604 | 626 |
|
| 627 | + # When `model` is referenced as-is, e.g. it's passed as an argument to a macro call like |
| 628 | + # `{{ foo(model) }}`, we can't easily track the attributes that are actually used, because |
| 629 | + # it may be aliased and hence tracking actual uses of `model` requires a proper data flow |
| 630 | + # analysis. We conservatively deal with this by including all of its supported attributes |
| 631 | + # if a standalone reference is found. |
| 632 | + if all_model_attrs: |
| 633 | + dependencies.model_attrs = {DBT_ALL_MODEL_ATTRS} |
| 634 | + |
605 | 635 | return dependencies |
606 | 636 |
|
607 | 637 |
|
|
0 commit comments