Skip to content

Commit 54320bd

Browse files
authored
Fix!: mark vars referenced in metadata macros as metadata (#4936)
1 parent 1237e9a commit 54320bd

File tree

10 files changed

+535
-73
lines changed

10 files changed

+535
-73
lines changed

sqlmesh/core/audit/definition.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,15 @@ def load_audit(
438438

439439
extra_kwargs: t.Dict[str, t.Any] = {}
440440
if is_standalone:
441-
jinja_macro_refrences, used_variables = extract_macro_references_and_variables(
441+
jinja_macro_refrences, referenced_variables = extract_macro_references_and_variables(
442442
*(gen(s) for s in statements),
443443
gen(query),
444444
)
445445
jinja_macros = (jinja_macros or JinjaMacroRegistry()).trim(jinja_macro_refrences)
446446
for jinja_macro in jinja_macros.root_macros.values():
447-
used_variables.update(extract_macro_references_and_variables(jinja_macro.definition)[1])
447+
referenced_variables.update(
448+
extract_macro_references_and_variables(jinja_macro.definition)[1]
449+
)
448450

449451
extra_kwargs["jinja_macros"] = jinja_macros
450452
extra_kwargs["python_env"] = make_python_env(
@@ -453,7 +455,7 @@ def load_audit(
453455
module_path,
454456
macros or macro.get_registry(),
455457
variables=variables,
456-
used_variables=used_variables,
458+
referenced_variables=referenced_variables,
457459
)
458460
extra_kwargs["default_catalog"] = default_catalog
459461
if project is not None:

sqlmesh/core/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@
8080
DEFAULT_SCHEMA = "default"
8181

8282
SQLMESH_VARS = "__sqlmesh__vars__"
83+
SQLMESH_VARS_METADATA = "__sqlmesh__vars__metadata__"
8384
SQLMESH_BLUEPRINT_VARS = "__sqlmesh__blueprint__vars__"
85+
SQLMESH_BLUEPRINT_VARS_METADATA = "__sqlmesh__blueprint__vars__metadata__"
8486

8587
VAR = "var"
8688
BLUEPRINT_VAR = "blueprint_var"

sqlmesh/core/macros.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,12 @@ def __init__(
210210
self.macros[normalize_macro_name(k)] = self.env[k]
211211
elif v.is_value:
212212
value = self.env[k]
213-
if k in (c.SQLMESH_VARS, c.SQLMESH_BLUEPRINT_VARS):
213+
if k in (
214+
c.SQLMESH_VARS,
215+
c.SQLMESH_VARS_METADATA,
216+
c.SQLMESH_BLUEPRINT_VARS,
217+
c.SQLMESH_BLUEPRINT_VARS_METADATA,
218+
):
214219
value = {
215220
var_name: (
216221
self.parse_one(var_value.sql)
@@ -557,17 +562,25 @@ def views(self) -> t.List[str]:
557562

558563
def var(self, var_name: str, default: t.Optional[t.Any] = None) -> t.Optional[t.Any]:
559564
"""Returns the value of the specified variable, or the default value if it doesn't exist."""
560-
return (self.locals.get(c.SQLMESH_VARS) or {}).get(var_name.lower(), default)
565+
return {
566+
**(self.locals.get(c.SQLMESH_VARS) or {}),
567+
**(self.locals.get(c.SQLMESH_VARS_METADATA) or {}),
568+
}.get(var_name.lower(), default)
561569

562570
def blueprint_var(self, var_name: str, default: t.Optional[t.Any] = None) -> t.Optional[t.Any]:
563571
"""Returns the value of the specified blueprint variable, or the default value if it doesn't exist."""
564-
return (self.locals.get(c.SQLMESH_BLUEPRINT_VARS) or {}).get(var_name.lower(), default)
572+
return {
573+
**(self.locals.get(c.SQLMESH_BLUEPRINT_VARS) or {}),
574+
**(self.locals.get(c.SQLMESH_BLUEPRINT_VARS_METADATA) or {}),
575+
}.get(var_name.lower(), default)
565576

566577
@property
567578
def variables(self) -> t.Dict[str, t.Any]:
568579
return {
569580
**self.locals.get(c.SQLMESH_VARS, {}),
581+
**self.locals.get(c.SQLMESH_VARS_METADATA, {}),
570582
**self.locals.get(c.SQLMESH_BLUEPRINT_VARS, {}),
583+
**self.locals.get(c.SQLMESH_BLUEPRINT_VARS_METADATA, {}),
571584
}
572585

573586
def _coerce(self, expr: exp.Expression, typ: t.Any, strict: bool = False) -> t.Any:

0 commit comments

Comments
 (0)