Skip to content

Commit f3d436e

Browse files
committed
Apply Trey's edits
2 parents b4593c0 + 27bafa8 commit f3d436e

1 file changed

Lines changed: 46 additions & 8 deletions

File tree

docs/concepts/macros/sqlmesh_macros.md

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,32 +1681,70 @@ def some_macro(evaluator):
16811681
...
16821682
```
16831683

1684-
#### Accessing the invoking model's resolved name
1684+
#### Accessing model, physical table, and virtual layer view names
16851685

1686-
The physical table name of the invoking model can be accessed within a Python macro function through its evaluation context's `this_model` property.
1686+
All SQLMesh models have a name in their `MODEL` specification. We refer to that as the model's "unqualified" and "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.
16871706

16881707
```python linenums="1"
16891708
from sqlmesh.core.macros import macro
16901709

16911710
@macro()
16921711
def some_macro(evaluator):
1693-
resolved_model = evaluator.this_model # e.g., '"datalake"."sqlmesh__landing"."landing__customers__2517971505"'
1712+
# Example:
1713+
# Name in model definition: landing.customers
1714+
# Value returned here: '"datalake"."landing"."customers"'
1715+
unresolved_model_fqn = evaluator.this_model_fqn
16941716
...
16951717
```
16961718

1697-
!!! note
1698-
During the "promotion" runtime stage, the model name resolution occurs for the virtual layer. This means that `this_model` resolve to the qualified view name of the invoking model. For instance, when running the plan in an environment named `dev`, `db.test_model` and `this_model` would resolve to `'"db__dev"."test_model"'` and not to the physical table name.
1719+
Access the resolved physical table and virtual layerview 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)
16991729

1700-
#### Accessing the invoking model's unresolved name
1730+
- All other runtime stages: `this_model` resolves to the physical table name
17011731

1702-
The unresolved, fully-qualified name of the invoking model can be accessed within a Python macro function through its evaluation context's `this_model_fqn` property.
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"`
17031736

17041737
```python linenums="1"
17051738
from sqlmesh.core.macros import macro
17061739

17071740
@macro()
17081741
def some_macro(evaluator):
1709-
resolved_model = evaluator.this_model_fqn # e.g., '"datalake"."landing"."customers"'
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
17101748
...
17111749
```
17121750

0 commit comments

Comments
 (0)