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/sqlmesh_macros.md
+67Lines changed: 67 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1681,6 +1681,73 @@ def some_macro(evaluator):
1681
1681
...
1682
1682
```
1683
1683
1684
+
#### Accessing model, physical table, and virtual layer view names
1685
+
1686
+
All SQLMesh models have a name in their `MODEL` specification. We refer to that as the model's "unresolved" name because it may not correspond to any specific object in the SQL engine.
1687
+
1688
+
When SQLMesh renders and executes a model, it converts the model name into three forms at different stages:
1689
+
1690
+
1. The *fully qualified* name
1691
+
1692
+
- If the model name is of the form `schema.table`, SQLMesh determines the correct catalog and adds it, like `catalog.schema.table`
1693
+
- SQLMesh quotes each component of the name using the SQL engine's quoting and case-sensitivity rules, like `"catalog"."schema"."table"`
1694
+
1695
+
2. The *resolved* physical table name
1696
+
1697
+
- The qualified name of the model's underlying physical table
1698
+
1699
+
3. The *resolved* virtual layer view name
1700
+
1701
+
- The qualified name of the model's virtual layer view in the environment where the model is being executed
1702
+
1703
+
You can access any of these three forms in a Python macro through properties of the `evaluation` context object.
1704
+
1705
+
Access the unresolved, fully-qualified name through the `this_model_fqn` property.
1706
+
1707
+
```python linenums="1"
1708
+
from sqlmesh.core.macros import macro
1709
+
1710
+
@macro()
1711
+
defsome_macro(evaluator):
1712
+
# Example:
1713
+
# Name in model definition: landing.customers
1714
+
# Value returned here: '"datalake"."landing"."customers"'
1715
+
unresolved_model_fqn = evaluator.this_model_fqn
1716
+
...
1717
+
```
1718
+
1719
+
Access the resolved physical table and virtual layer view names through the `this_model` property.
1720
+
1721
+
The `this_model` property returns different names depending on the runtime stage:
1722
+
1723
+
-`promoting` runtime stage: `this_model` resolves to the virtual layer view name
1724
+
1725
+
- Example
1726
+
- Model name is `db.test_model`
1727
+
- `plan` is running in the `dev` environment
1728
+
- `this_model` resolves to `"catalog"."db__dev"."test_model"` (note the `__dev` suffix in the schema name)
1729
+
1730
+
- All other runtime stages: `this_model` resolves to the physical table name
1731
+
1732
+
- Example
1733
+
- Model name is `db.test_model`
1734
+
- `plan` is running in any environment
1735
+
- `this_model` resolves to `"catalog"."sqlmesh__project"."project__test_model__684351896"`
1736
+
1737
+
```python linenums="1"
1738
+
from sqlmesh.core.macros import macro
1739
+
1740
+
@macro()
1741
+
defsome_macro(evaluator):
1742
+
if evaluator.runtime_stage =="promoting":
1743
+
# virtual layer view name '"catalog"."db__dev"."test_model"'
1744
+
resolved_name = evaluator.this_model
1745
+
else:
1746
+
# physical table name '"catalog"."sqlmesh__project"."project__test_model__684351896"'
1747
+
resolved_name = evaluator.this_model
1748
+
...
1749
+
```
1750
+
1684
1751
#### Accessing model schemas
1685
1752
1686
1753
Model schemas can be accessed within a Python macro function through its evaluation context's `column_to_types()` method, if the column types can be statically determined. For instance, a schema of an [external model](../models/external_models.md) can be accessed only after the `sqlmesh create_external_models` command has been executed.
0 commit comments