Skip to content

Commit 9cec846

Browse files
georgesittastreysp
andauthored
Chore: document this_model and this_model_fqn (#4524)
Co-authored-by: Trey Spiller <treyspiller@gmail.com>
1 parent b1f9960 commit 9cec846

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

docs/concepts/macros/sqlmesh_macros.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,6 +1681,73 @@ def some_macro(evaluator):
16811681
...
16821682
```
16831683

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+
def some_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+
def some_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+
16841751
#### Accessing model schemas
16851752

16861753
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

Comments
 (0)