Skip to content

Commit

Permalink
vars at node-level
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelleArk committed Jul 30, 2024
1 parent a1bc80c commit b7d7ad2
Show file tree
Hide file tree
Showing 15 changed files with 195 additions and 214 deletions.
5 changes: 1 addition & 4 deletions core/dbt/artifacts/resources/v1/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,11 @@ def add_macro(self, value: str):
@dataclass
class DependsOn(MacroDependsOn):
nodes: List[str] = field(default_factory=list)
vars: Dict[str, Any] = field(default_factory=dict)

def add_node(self, value: str):
if value not in self.nodes:
self.nodes.append(value)

def add_var(self, var_name: str, var_value: Any) -> None:
self.vars[var_name] = var_value


@dataclass
class RefArgs(dbtClassMixin):
Expand Down Expand Up @@ -200,6 +196,7 @@ class ParsedResource(ParsedResourceMandatory):
config_call_dict: Dict[str, Any] = field(default_factory=dict)
relation_name: Optional[str] = None
raw_code: str = ""
vars: Dict[str, Any] = field(default_factory=dict)

def __post_serialize__(self, dct: Dict, context: Optional[Dict] = None):
dct = super().__post_serialize__(dct, context)
Expand Down
1 change: 1 addition & 0 deletions core/dbt/artifacts/resources/v1/exposure.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Exposure(GraphResource):
tags: List[str] = field(default_factory=list)
config: ExposureConfig = field(default_factory=ExposureConfig)
unrendered_config: Dict[str, Any] = field(default_factory=dict)
vars: Dict[str, Any] = field(default_factory=dict)
url: Optional[str] = None
depends_on: DependsOn = field(default_factory=DependsOn)
refs: List[RefArgs] = field(default_factory=list)
Expand Down
1 change: 1 addition & 0 deletions core/dbt/artifacts/resources/v1/macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ class Macro(BaseResource):
arguments: List[MacroArgument] = field(default_factory=list)
created_at: float = field(default_factory=lambda: time.time())
supported_languages: Optional[List[ModelLanguage]] = None
vars: Dict[str, Any] = field(default_factory=dict)
1 change: 1 addition & 0 deletions core/dbt/artifacts/resources/v1/source_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@ class SourceDefinition(ParsedSourceMandatory):
config: SourceConfig = field(default_factory=SourceConfig)
patch_path: Optional[str] = None
unrendered_config: Dict[str, Any] = field(default_factory=dict)
vars: Dict[str, Any] = field(default_factory=dict)
relation_name: Optional[str] = None
created_at: float = field(default_factory=lambda: time.time())
8 changes: 2 additions & 6 deletions core/dbt/context/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,12 +732,8 @@ def get_missing_var(self, var_name):
def __call__(self, var_name: str, default: Any = ModelConfiguredVar._VAR_NOTSET) -> Any:
var_value = super().__call__(var_name, default)

if (
self._node
and hasattr(self._node, "depends_on")
and hasattr(self._node.depends_on, "add_var")
):
self._node.depends_on.add_var(var_name, var_value)
if self._node and hasattr(self._node, "vars"):
self._node.vars[var_name] = var_value

return var_value

Expand Down
13 changes: 5 additions & 8 deletions core/dbt/contracts/graph/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,9 @@ def same_contract(self, old, adapter_type=None) -> bool:
# This would only apply to seeds
return True

def same_vars(self, old) -> bool:
return self.vars == old.vars

Check warning on line 361 in core/dbt/contracts/graph/nodes.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/contracts/graph/nodes.py#L361

Added line #L361 was not covered by tests

def same_contents(self, old, adapter_type) -> bool:
if old is None:
return False
Expand All @@ -370,6 +373,7 @@ def same_contents(self, old, adapter_type) -> bool:
and self.same_persisted_description(old)
and self.same_fqn(old)
and self.same_database_representation(old)
and self.same_vars(old)
and same_contract
and True
)
Expand Down Expand Up @@ -409,15 +413,8 @@ def depends_on_nodes(self):
def depends_on_macros(self):
return self.depends_on.macros

@property
def depends_on_vars(self):
return self.depends_on.vars

def same_depends_on_vars(self, old):
return self.depends_on_vars == old.depends_on_vars

def same_contents(self, old, adapter_type) -> bool:
return super().same_contents(old, adapter_type) and self.same_depends_on_vars(old)
return super().same_contents(old, adapter_type)

Check warning on line 417 in core/dbt/contracts/graph/nodes.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/contracts/graph/nodes.py#L417

Added line #L417 was not covered by tests


# ====================================
Expand Down
4 changes: 2 additions & 2 deletions core/dbt/graph/selector_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ def _macros_modified(self) -> List[str]:
for uid, macro in new_macros.items():
if uid in old_macros:
old_macro = old_macros[uid]
if macro.macro_sql != old_macro.macro_sql:
if macro.macro_sql != old_macro.macro_sql or macro.vars != old_macro.vars:

Check warning on line 626 in core/dbt/graph/selector_methods.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/graph/selector_methods.py#L626

Added line #L626 was not covered by tests
modified.append(uid)
else:
modified.append(uid)
Expand Down Expand Up @@ -752,7 +752,7 @@ def search(self, included_nodes: Set[UniqueId], selector: str) -> Iterator[Uniqu
"modified.relation": self.check_modified_factory("same_database_representation"),
"modified.macros": self.check_modified_macros,
"modified.contract": self.check_modified_contract("same_contract", adapter_type),
"modified.vars": self.check_modified_factory("same_depends_on_vars"),
"modified.vars": self.check_modified_factory("same_vars"),
}
if selector in state_checks:
checker = state_checks[selector]
Expand Down
4 changes: 4 additions & 0 deletions core/dbt/parser/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def parse_macro(self, block: jinja.BlockTag, base_node: UnparsedMacro, name: str
unique_id = self.generate_unique_id(name)
macro_sql = block.full_block or ""

# TODO:
# statically extract potential "vars" calls
# If there are any, create a parse-time macro env + render macro sql to extract vars

return Macro(
path=base_node.path,
macro_sql=macro_sql,
Expand Down
Loading

0 comments on commit b7d7ad2

Please sign in to comment.