Skip to content

Commit 7706f65

Browse files
committed
refactor: make it possible to get a models macroevaluator
1 parent 5285753 commit 7706f65

1 file changed

Lines changed: 98 additions & 46 deletions

File tree

sqlmesh/core/renderer.py

Lines changed: 98 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,87 @@ def __init__(
7272
def update_schema(self, schema: t.Dict[str, t.Any]) -> None:
7373
self.schema = d.normalize_mapping_schema(schema, dialect=self._dialect)
7474

75+
def _create_macro_evaluator(
76+
self,
77+
snapshots: t.Optional[t.Dict[str, Snapshot]] = None,
78+
table_mapping: t.Optional[t.Dict[str, str]] = None,
79+
deployability_index: t.Optional[DeployabilityIndex] = None,
80+
runtime_stage: RuntimeStage = RuntimeStage.LOADING,
81+
start: t.Optional[TimeLike] = None,
82+
end: t.Optional[TimeLike] = None,
83+
execution_time: t.Optional[TimeLike] = None,
84+
environment_naming_info: t.Optional[t.Any] = None,
85+
render_kwargs: t.Optional[t.Dict[str, t.Any]] = None,
86+
variables: t.Optional[t.Dict[str, t.Any]] = None,
87+
) -> MacroEvaluator:
88+
"""Creates a MacroEvaluator instance with the configured settings.
89+
90+
Args:
91+
snapshots: All upstream snapshots (by model name) to use for expansion and mapping of physical locations.
92+
table_mapping: Table mapping of physical locations. Takes precedence over snapshot mappings.
93+
deployability_index: Determines snapshots that are deployable in the context of this evaluation.
94+
runtime_stage: Indicates the current runtime stage.
95+
start: The start datetime to render.
96+
end: The end datetime to render.
97+
execution_time: The date/time time reference to use for execution time.
98+
environment_naming_info: Environment naming information.
99+
render_kwargs: Additional render kwargs to update locals with.
100+
variables: Variables to add to the SQLMESH_VARS in locals.
101+
102+
Returns:
103+
A configured MacroEvaluator instance.
104+
"""
105+
106+
def _resolve_table(table: str | exp.Table) -> str:
107+
return self._resolve_table(
108+
d.normalize_model_name(table, self._default_catalog, self._dialect),
109+
snapshots=snapshots,
110+
table_mapping=table_mapping,
111+
deployability_index=deployability_index,
112+
).sql(dialect=self._dialect, identify=True, comments=False)
113+
114+
macro_evaluator = MacroEvaluator(
115+
self._dialect,
116+
python_env=self._python_env,
117+
schema=self.schema,
118+
runtime_stage=runtime_stage,
119+
resolve_table=_resolve_table,
120+
resolve_tables=lambda e: self._resolve_tables(
121+
e,
122+
snapshots=snapshots,
123+
table_mapping=table_mapping,
124+
deployability_index=deployability_index,
125+
start=start,
126+
end=end,
127+
execution_time=execution_time,
128+
runtime_stage=runtime_stage,
129+
),
130+
snapshots=snapshots,
131+
default_catalog=self._default_catalog,
132+
path=self._path,
133+
environment_naming_info=environment_naming_info,
134+
model_fqn=self._model_fqn,
135+
)
136+
137+
# Update locals with render_kwargs if provided
138+
if render_kwargs:
139+
macro_evaluator.locals.update(render_kwargs)
140+
141+
# Update variables if provided
142+
if variables:
143+
macro_evaluator.locals.setdefault(c.SQLMESH_VARS, {}).update(variables)
144+
145+
# Evaluate macro definitions
146+
for definition in self._macro_definitions:
147+
try:
148+
macro_evaluator.evaluate(definition)
149+
except Exception as ex:
150+
raise_config_error(
151+
f"Failed to evaluate macro '{definition}'.\n\n{ex}\n", self._path
152+
)
153+
154+
return macro_evaluator
155+
75156
def _render(
76157
self,
77158
start: t.Optional[TimeLike] = None,
@@ -141,37 +222,6 @@ def _render(
141222
if this_snapshot and (kind := this_snapshot.model_kind_name):
142223
kwargs["model_kind_name"] = kind.name
143224

144-
def _resolve_table(table: str | exp.Table) -> str:
145-
return self._resolve_table(
146-
d.normalize_model_name(table, self._default_catalog, self._dialect),
147-
snapshots=snapshots,
148-
table_mapping=table_mapping,
149-
deployability_index=deployability_index,
150-
).sql(dialect=self._dialect, identify=True, comments=False)
151-
152-
macro_evaluator = MacroEvaluator(
153-
self._dialect,
154-
python_env=self._python_env,
155-
schema=self.schema,
156-
runtime_stage=runtime_stage,
157-
resolve_table=_resolve_table,
158-
resolve_tables=lambda e: self._resolve_tables(
159-
e,
160-
snapshots=snapshots,
161-
table_mapping=table_mapping,
162-
deployability_index=deployability_index,
163-
start=start,
164-
end=end,
165-
execution_time=execution_time,
166-
runtime_stage=runtime_stage,
167-
),
168-
snapshots=snapshots,
169-
default_catalog=self._default_catalog,
170-
path=self._path,
171-
environment_naming_info=environment_naming_info,
172-
model_fqn=self._model_fqn,
173-
)
174-
175225
start_time, end_time = (
176226
make_inclusive(start or c.EPOCH, end or c.EPOCH, self._dialect)
177227
if not self._only_execution_time
@@ -188,6 +238,22 @@ def _resolve_table(table: str | exp.Table) -> str:
188238
}
189239

190240
variables = kwargs.pop("variables", {})
241+
242+
if this_model:
243+
render_kwargs["this_model"] = this_model
244+
245+
macro_evaluator = self._create_macro_evaluator(
246+
snapshots=snapshots,
247+
table_mapping=table_mapping,
248+
deployability_index=deployability_index,
249+
runtime_stage=runtime_stage,
250+
start=start,
251+
end=end,
252+
execution_time=execution_time,
253+
environment_naming_info=environment_naming_info,
254+
render_kwargs=render_kwargs,
255+
variables=variables,
256+
)
191257
jinja_env_kwargs = {
192258
**{
193259
**render_kwargs,
@@ -199,10 +265,9 @@ def _resolve_table(table: str | exp.Table) -> str:
199265
"deployability_index": deployability_index,
200266
"default_catalog": self._default_catalog,
201267
"runtime_stage": runtime_stage.value,
202-
"resolve_table": _resolve_table,
268+
"resolve_table": macro_evaluator.resolve_table,
203269
}
204270
if this_model:
205-
render_kwargs["this_model"] = this_model
206271
jinja_env_kwargs["this_model"] = this_model.sql(
207272
dialect=self._dialect, identify=True, comments=False
208273
)
@@ -226,19 +291,6 @@ def _resolve_table(table: str | exp.Table) -> str:
226291
f"Could not render or parse jinja at '{self._path}'.\n{ex}"
227292
) from ex
228293

229-
macro_evaluator.locals.update(render_kwargs)
230-
231-
if variables:
232-
macro_evaluator.locals.setdefault(c.SQLMESH_VARS, {}).update(variables)
233-
234-
for definition in self._macro_definitions:
235-
try:
236-
macro_evaluator.evaluate(definition)
237-
except Exception as ex:
238-
raise_config_error(
239-
f"Failed to evaluate macro '{definition}'.\n\n{ex}\n", self._path
240-
)
241-
242294
resolved_expressions: t.List[t.Optional[exp.Expression]] = []
243295

244296
for expression in expressions:

0 commit comments

Comments
 (0)