Skip to content

Commit 3c24421

Browse files
committed
Chore!: deprecate support for mixing Jinja & SQLMesh macros
1 parent e4ea4c8 commit 3c24421

File tree

4 files changed

+51
-39
lines changed

4 files changed

+51
-39
lines changed

docs/concepts/macros/jinja_macros.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,6 @@ Some SQL dialects interpret double and single quotes differently. We could repla
321321

322322
## Mixing macro systems
323323

324-
SQLMesh supports both the Jinja and [SQLMesh](./sqlmesh_macros.md) macro systems. We strongly recommend using only one system in a single model - if both are present, they may fail or behave in unintuitive ways.
324+
SQLMesh supports both the Jinja and [SQLMesh](./sqlmesh_macros.md) macro systems.
325325

326-
[Predefined SQLMesh macro variables](./macro_variables.md) can be used in a query containing user-defined Jinja variables and functions. However, predefined variables passed as arguments to a user-defined Jinja macro function must use the Jinja curly brace syntax `{{ start_ds }}` instead of the SQLMesh macro `@` prefix syntax `@start_ds`. Note that curly brace syntax may require quoting to generate the equivalent of the `@` syntax.
326+
Mixing the two systems is not supported, i.e., a SQL model may either use Jinja syntax, or SQLMesh macro syntax, but not both.

docs/concepts/macros/sqlmesh_macros.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2119,4 +2119,6 @@ Typed macros in SQLMesh not only enhance the development experience by making ma
21192119

21202120
## Mixing macro systems
21212121

2122-
SQLMesh supports both SQLMesh and [Jinja](./jinja_macros.md) macro systems. We strongly recommend using only one system in a model - if both are present, they may fail or behave in unintuitive ways.
2122+
SQLMesh supports both the Jinja and [SQLMesh](./sqlmesh_macros.md) macro systems.
2123+
2124+
Mixing the two systems is not supported, i.e., a SQL model may either use Jinja syntax, or SQLMesh macro syntax, but not both.

examples/sushi/models/waiter_as_customer_by_day.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ SELECT
2727
FROM sushi.waiters AS w
2828
JOIN sushi.customers as c ON w.waiter_id = c.customer_id
2929
JOIN sushi.waiter_names as wn ON w.waiter_id = wn.id
30-
WHERE w.event_date BETWEEN @start_date AND @end_date;
30+
WHERE w.event_date BETWEEN CAST('{{ start_date }}' AS DATE) AND CAST('{{ end_date }}' AS DATE);
3131

3232
JINJA_END;

sqlmesh/core/renderer.py

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -188,32 +188,38 @@ def _resolve_table(table: str | exp.Table) -> str:
188188
**kwargs,
189189
}
190190

191+
jinja_env_kwargs = render_kwargs.copy()
191192
variables = kwargs.pop("variables", {})
192-
jinja_env_kwargs = {
193-
**{
194-
**render_kwargs,
195-
**_prepare_python_env_for_jinja(macro_evaluator, self._python_env),
196-
**variables,
197-
},
198-
"snapshots": snapshots or {},
199-
"table_mapping": table_mapping,
200-
"deployability_index": deployability_index,
201-
"default_catalog": self._default_catalog,
202-
"runtime_stage": runtime_stage.value,
203-
"resolve_table": _resolve_table,
204-
}
193+
205194
if this_model:
206195
render_kwargs["this_model"] = this_model
207196
jinja_env_kwargs["this_model"] = this_model.sql(
208197
dialect=self._dialect, identify=True, comments=False
209198
)
210199

211-
jinja_env = self._jinja_macro_registry.build_environment(**jinja_env_kwargs)
212-
213200
expressions = [self._expression]
214201
if isinstance(self._expression, d.Jinja):
202+
# Mixing Jinja and SQLMesh macros is not supported
203+
render_sqlmesh_macros = False
204+
205+
jinja_env_kwargs.update(
206+
{
207+
**{
208+
**_prepare_python_env_for_jinja(macro_evaluator, self._python_env),
209+
**variables,
210+
},
211+
"snapshots": snapshots or {},
212+
"table_mapping": table_mapping,
213+
"deployability_index": deployability_index,
214+
"default_catalog": self._default_catalog,
215+
"runtime_stage": runtime_stage.value,
216+
"resolve_table": _resolve_table,
217+
}
218+
)
215219
try:
216220
expressions = []
221+
222+
jinja_env = self._jinja_macro_registry.build_environment(**jinja_env_kwargs)
217223
rendered_expression = jinja_env.from_string(self._expression.name).render()
218224
logger.debug(
219225
f"Rendered Jinja expression for model '{self._model_fqn}' at '{self._path}': '{rendered_expression}'"
@@ -229,30 +235,34 @@ def _resolve_table(table: str | exp.Table) -> str:
229235
raise ConfigError(
230236
f"Could not render or parse jinja at '{self._path}'.\n{ex}"
231237
) from ex
232-
233-
macro_evaluator.locals.update(render_kwargs)
234-
235-
if variables:
236-
macro_evaluator.locals.setdefault(c.SQLMESH_VARS, {}).update(variables)
237-
238-
for definition in self._macro_definitions:
239-
try:
240-
macro_evaluator.evaluate(definition)
241-
except Exception as ex:
242-
raise_config_error(
243-
f"Failed to evaluate macro '{definition}'.\n\n{ex}\n", self._path
244-
)
238+
else:
239+
render_sqlmesh_macros = True
240+
macro_evaluator.locals.update(render_kwargs)
241+
242+
if variables:
243+
macro_evaluator.locals.setdefault(c.SQLMESH_VARS, {}).update(variables)
244+
245+
for definition in self._macro_definitions:
246+
try:
247+
macro_evaluator.evaluate(definition)
248+
except Exception as ex:
249+
raise_config_error(
250+
f"Failed to evaluate macro '{definition}'.\n\n{ex}\n", self._path
251+
)
245252

246253
resolved_expressions: t.List[t.Optional[exp.Expression]] = []
247254

248255
for expression in expressions:
249-
try:
250-
transformed_expressions = ensure_list(macro_evaluator.transform(expression))
251-
except Exception as ex:
252-
raise_config_error(
253-
f"Failed to resolve macros for\n\n{expression.sql(dialect=self._dialect, pretty=True)}\n\n{ex}\n",
254-
self._path,
255-
)
256+
if render_sqlmesh_macros:
257+
try:
258+
transformed_expressions = ensure_list(macro_evaluator.transform(expression))
259+
except Exception as ex:
260+
raise_config_error(
261+
f"Failed to resolve macros for\n\n{expression.sql(dialect=self._dialect, pretty=True)}\n\n{ex}\n",
262+
self._path,
263+
)
264+
else:
265+
transformed_expressions = [expression]
256266

257267
for expression in t.cast(t.List[exp.Expression], transformed_expressions):
258268
with self._normalize_and_quote(expression) as expression:

0 commit comments

Comments
 (0)