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
Chore: update macro docs to include runtime_stage macro (#1580)
* Chore: update macro docs to include runtime_stage macro
* Rephrase
* Rephrase
* update example to use ALTER TYPE
* Fixups
* Rephrase sentence
* Note that runtime_stage is only available to pre/post-statements
* Don't constrain runtime stage usage
* Update test
Copy file name to clipboardExpand all lines: docs/concepts/macros/macro_variables.md
+8-1Lines changed: 8 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,7 @@ We describe SQLMesh's predefined variables below; user-defined macro variables a
37
37
## Predefined Variables
38
38
SQLMesh comes with predefined variables that can be used in your queries. They are automatically set by the SQLMesh runtime.
39
39
40
-
These variables are related to time and comprise a combination of prefixes (start, end, execution) and postfixes (date, ds, ts, epoch, millis).
40
+
These variables are mostly related to time and comprise a combination of prefixes (start, end, execution) and postfixes (date, ds, ts, epoch, millis).
41
41
42
42
SQLMesh uses the python [datetime module](https://docs.python.org/3/library/datetime.html) for handling dates and times. It uses the standard [Unix epoch](https://en.wikipedia.org/wiki/Unix_time) start of 1970-01-01. *All predefined variables with a time component use the [UTC time zone](https://en.wikipedia.org/wiki/Coordinated_Universal_Time).*
43
43
@@ -86,3 +86,10 @@ All predefined macro variables:
86
86
* @start_millis
87
87
* @end_millis
88
88
* @execution_millis
89
+
90
+
Other macro variables:
91
+
92
+
* @runtime_stage - A string value that denotes the current stage of the SQLMesh runtime. It can take one of the following values:
93
+
* 'loading' - The project is currently being loaded into SQLMesh's runtime context.
94
+
* 'creating' - The model tables are being created.
95
+
* 'evaluating' - The models' logic is being evaluated.
Copy file name to clipboardExpand all lines: docs/concepts/macros/sqlmesh_macros.md
+40-1Lines changed: 40 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -260,10 +260,22 @@ It includes three elements:
260
260
2. A value to return if the condition is `TRUE`
261
261
3. A value to return if the condition is `FALSE`[optional]
262
262
263
-
These elements are specified as `@IF([logical condition], [value if TRUE], [value if FALSE])`.
263
+
These elements are specified as:
264
+
265
+
```sql linenums="1"
266
+
@IF([logical condition], [value if TRUE], [value if FALSE])
267
+
```
264
268
265
269
The value to return if the condition is `FALSE` is optional - if it is not provided and the condition is `FALSE`, then the macro has no effect on the resulting query.
266
270
271
+
It's also possible to use this macro in order to conditionally execute pre/post-statements:
272
+
273
+
```sql linenums="1"
274
+
@IF([logical condition], [statement to execute if TRUE]);
275
+
```
276
+
277
+
The `@IF` pre/post-statement itself must end with a semi-colon, but the inner statement argument must not.
278
+
267
279
The logical condition should be written *in SQL* and is evaluated with [SQLGlot's](https://github.com/tobymao/sqlglot) SQL engine. It supports the following operators:
268
280
269
281
- Equality: `=` for equals, `!=` or `<>` for not equals
@@ -318,6 +330,33 @@ SELECT
318
330
FROM table
319
331
```
320
332
333
+
Another example is conditionally executing a pre/post-statement depending on the current [runtime stage](./macro_variables.md#predefined-variables). For instance, the following `@IF` post-statement will only be executed at model evaluation time:
334
+
335
+
```sql linenums="1"
336
+
MODEL (
337
+
name sqlmesh_example.full_model,
338
+
kind FULL,
339
+
cron '@daily',
340
+
grain item_id,
341
+
audits [assert_positive_order_ids],
342
+
);
343
+
344
+
SELECT
345
+
item_id,
346
+
count(distinct id) AS num_orders,
347
+
FROM
348
+
sqlmesh_example.incremental_model
349
+
GROUP BY item_id
350
+
ORDER BY item_id;
351
+
352
+
@IF(
353
+
@runtime_stage ='evaluating',
354
+
ALTERTABLEsqlmesh_example.full_model ALTER item_id TYPE VARCHAR
355
+
);
356
+
```
357
+
358
+
NOTE: we can also, say, alter the type of a column if the `@runtime_stage` is `'creating'`, but that will only have meaningful effects if the corresponding model is of an incremental kind, since `FULL` models are rebuilt on each evaluation and hence any changes made at their creation stage will be overwritten.
359
+
321
360
#### @EVAL
322
361
323
362
`@EVAL` evaluates its arguments with SQLGlot's SQL executor.
0 commit comments