Skip to content

Commit e7db596

Browse files
remove conversion of cron to identifier; add doc clarification
1 parent b70220b commit e7db596

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

docs/concepts/models/python_models.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,35 @@ It's also possible to use the `@EACH` macro, combined with a global list variabl
394394
...
395395
```
396396

397+
## Using macros in model properties
398+
399+
Python models support macro variables in model properties. However, special care must be taken when the macro variable appears within a string.
400+
401+
For example when using macro variables inside cron expressions, you need to wrap the entire expression in quotes and prefix it with `@` to ensure proper parsing:
402+
403+
```python linenums="1"
404+
# Correct: Wrap the cron expression containing a macro variable
405+
@model(
406+
"my_model",
407+
cron="@'*/@{mins} * * * *'", # Note the @'...' syntax
408+
...
409+
)
410+
411+
# This also works with blueprint variables
412+
@model(
413+
"@{customer}.scheduled_model",
414+
cron="@'0 @{hour} * * *'",
415+
blueprints=[
416+
{"customer": "customer_1", "hour": 2}, # Runs at 2 AM
417+
{"customer": "customer_2", "hour": 8}, # Runs at 8 AM
418+
],
419+
...
420+
)
421+
422+
```
423+
424+
This is necessary because cron expressions often use `@` for aliases (like `@daily`, `@hourly`), which can conflict with SQLMesh's macro syntax.
425+
397426
## Examples
398427
### Basic
399428
The following is an example of a Python model returning a static Pandas DataFrame.

sqlmesh/core/model/definition.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2786,10 +2786,6 @@ def render_field_value(value: t.Any) -> t.Any:
27862786
) or field_value is None:
27872787
continue
27882788

2789-
# If it contains a macro reference we need to render it similar to sql models
2790-
if field == "cron" and isinstance(field_value, str):
2791-
field_value = exp.to_identifier(field_value)
2792-
27932789
if field in RUNTIME_RENDERED_MODEL_FIELDS:
27942790
fields[field] = parse_strings_with_macro_refs(field_value, dialect)
27952791
continue

tests/core/test_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10772,7 +10772,7 @@ def test_python_model_cron_with_blueprints(tmp_path: Path) -> None:
1077210772
@model(
1077310773
"@{customer}.some_table",
1077410774
kind="FULL",
10775-
cron="*/@{min} * * * *",
10775+
cron="@'*/@{min} * * * *'",
1077610776
blueprints=[
1077710777
{"customer": "customer1", "field_a": "x", "field_b": "y", "min": 5},
1077810778
{"customer": "customer2", "field_a": "z", "field_b": "w", "min": 10},

0 commit comments

Comments
 (0)