You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/concepts/macros/jinja_macros.md
+47-8Lines changed: 47 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
# Jinja macros
2
2
3
-
SQLMesh supports macros from the [Jinja](https://jinja.palletsprojects.com/en/3.1.x/) templating system.
3
+
SQLMesh supports macros from the [Jinja](https://jinja.palletsprojects.com/en/3.1.x/) templating system.
4
4
5
-
Jinja's macro approach is pure string substitution. Unlike SQLMesh macros, they assemble SQL query text without building a semantic representation.
5
+
Jinja's macro approach is pure string substitution. Unlike SQLMesh macros, they assemble SQL query text without building a semantic representation.
6
6
7
7
**NOTE:** SQLMesh projects support the standard Jinja function library only - they do **not** support dbt-specific jinja functions like `{{ ref() }}`. dbt-specific functions are allowed in dbt projects being run with the [SQLMesh adapter](../../integrations/dbt.md).
8
8
@@ -16,6 +16,41 @@ The three curly brace symbols are:
16
16
-`{%...%}` creates Jinja statements. Statements give instructions to Jinja, such as setting variable values, control flow with `if`, `for` loops, and defining macro functions.
17
17
-`{#...#}` creates Jinja comments. These comments will not be included in the rendered SQL query.
18
18
19
+
Since Jinja strings are not syntactically valid SQL expressions and cannot be parsed as such, the model query must be wrapped in a special `JINJA_QUERY_BEGIN; ...; JINJA_END;` block in order for SQLMesh to detect it:
20
+
21
+
```sql linenums="1" hl_lines="5 9"
22
+
MODEL (
23
+
name sqlmesh_example.full_model
24
+
);
25
+
26
+
JINJA_QUERY_BEGIN;
27
+
28
+
SELECT {{ 1+1 }};
29
+
30
+
JINJA_END;
31
+
```
32
+
33
+
Similarly, to use Jinja expressions as part of statements that should be evaluated before or after the model query, the `JINJA_STATEMENT_BEGIN; ...; JINJA_END;` block should be used:
34
+
35
+
```sql linenums="1"
36
+
MODEL (
37
+
name sqlmesh_example.full_model
38
+
);
39
+
40
+
JINJA_STATEMENT_BEGIN;
41
+
{{ pre_hook() }}
42
+
JINJA_END;
43
+
44
+
JINJA_QUERY_BEGIN;
45
+
SELECT {{ 1+1 }};
46
+
JINJA_END;
47
+
48
+
JINJA_STATEMENT_BEGIN;
49
+
{{ post_hook() }}
50
+
JINJA_END;
51
+
```
52
+
53
+
19
54
## User-defined variables
20
55
21
56
Define your own variables with the Jinja statement `{% set ... %}`. For example, we could specify the name of the `num_orders` column in the `sqlmesh_example.full_model` like this:
@@ -28,6 +63,8 @@ MODEL (
28
63
audits [assert_positive_order_ids],
29
64
);
30
65
66
+
JINJA_QUERY_BEGIN;
67
+
31
68
{% set my_col ='num_orders' %} -- Jinja definition of variable `my_col`
32
69
33
70
SELECT
@@ -36,6 +73,8 @@ SELECT
36
73
FROM
37
74
sqlmesh_example.incremental_model
38
75
GROUP BY item_id
76
+
77
+
JINJA_END;
39
78
```
40
79
41
80
Note that the Jinja set statement is written after the `MODEL` statement and before the SQL query.
@@ -48,7 +87,7 @@ Jinja variables can be string, integer, or float data types. They can also be an
48
87
49
88
#### for loops
50
89
51
-
For loops let you iterate over a collection of items to condense repetitive code and easily change the values used by the code.
90
+
For loops let you iterate over a collection of items to condense repetitive code and easily change the values used by the code.
52
91
53
92
Jinja for loops begin with `{% for ... %}` and end with `{% endfor %}`. This example demonstrates creating indicator variables with `CASE WHEN` using a Jinja for loop:
54
93
@@ -88,9 +127,9 @@ FROM table
88
127
89
128
The rendered query would be the same as before.
90
129
91
-
#### if
130
+
#### if
92
131
93
-
if statements allow you to take an action (or not) based on some condition.
132
+
if statements allow you to take an action (or not) based on some condition.
94
133
95
134
Jinja if statements begin with `{% if ... %}` and end with `{% endif %}`. The starting `if` statement must contain code that evaluates to `True` or `False`. For example, all of `True`, `1 + 1 == 2`, and `'a' in ['a', 'b']` evaluate to `True`.
96
135
@@ -118,11 +157,11 @@ FROM table
118
157
119
158
## User-defined macro functions
120
159
121
-
User-defined macro functions allow the same macro code to be used in multiple models.
160
+
User-defined macro functions allow the same macro code to be used in multiple models.
122
161
123
162
Jinja macro functions should be placed in `.sql` files in the SQLMesh project's `macros` directory. Multiple functions can be defined in one `.sql` file, or they can be distributed across multiple files.
124
163
125
-
Jinja macro functions are defined with the `{% macro %}` and `{% endmacro %}` statements. The macro function name and arguments are specified in the `{% macro %}` statement.
164
+
Jinja macro functions are defined with the `{% macro %}` and `{% endmacro %}` statements. The macro function name and arguments are specified in the `{% macro %}` statement.
126
165
127
166
For example, a macro function named `print_text` that takes no arguments could be defined with:
128
167
@@ -186,6 +225,6 @@ Some SQL dialects interpret double and single quotes differently. We could repla
186
225
187
226
## Mixing macro systems
188
227
189
-
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.
228
+
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.
190
229
191
230
[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.
0 commit comments