Skip to content
1 change: 1 addition & 0 deletions docs/concepts/macros/macro_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,4 @@ SQLMesh provides two other predefined variables used to modify model behavior ba
* Can be used in model definitions when SQLGlot cannot fully parse a statement and you need to reference the model's underlying physical table directly.
* Can be passed as an argument to macros that access or interact with the underlying physical table.
* @this_env - A string value containing the name of the current [environment](../environments.md). Only available in [`before_all` and `after_all` statements](../../guides/configuration.md#before_all-and-after_all-statements), as well as in macros invoked within them.
* @model_kind_name - A string value containing the name of the current model kind. Intended to be used in scenarios where you need to control the [physical properties in model defaults](../../reference/model_configuration.md#model-defaults).
28 changes: 28 additions & 0 deletions docs/reference/model_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,34 @@ To override `partition_expiration_days`, add a new `creatable_type` property and
)
```

You can also use the `@model_kind_name` variable to fine-tune control over `physical_properties` in `model_defaults`. This holds the current model's kind name and is useful for conditionally assigning a property. For example, to disable `creatable_type` for your project's `VIEW` kind models:

=== "YAML"

```yaml linenums="1"
model_defaults:
dialect: snowflake
start: 2022-01-01
physical_properties:
creatable_type: "@IF(@model_kind_name != 'VIEW', 'TRANSIENT', NULL)"
```

=== "Python"

```python linenums="1"
from sqlmesh.core.config import Config, ModelDefaultsConfig

config = Config(
model_defaults=ModelDefaultsConfig(
dialect="snowflake",
start="2022-01-01",
physical_properties={
"creatable_type": "@IF(@model_kind_name != 'VIEW', 'TRANSIENT', NULL)",
},
),
)
```


The SQLMesh project-level `model_defaults` key supports the following options, described in the [general model properties](#general-model-properties) table above:

Expand Down
10 changes: 5 additions & 5 deletions sqlmesh/core/config/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
model_kind_validator,
on_destructive_change_validator,
)
from sqlmesh.core.model.meta import FunctionCall
from sqlmesh.core.node import IntervalUnit
from sqlmesh.utils.date import TimeLike
from sqlmesh.core.model.meta import FunctionCall
from sqlmesh.utils.pydantic import field_validator


Expand Down Expand Up @@ -56,10 +56,10 @@ class ModelDefaultsConfig(BaseConfig):
virtual_properties: t.Optional[t.Dict[str, t.Any]] = None
session_properties: t.Optional[t.Dict[str, t.Any]] = None
audits: t.Optional[t.List[FunctionCall]] = None
optimize_query: t.Optional[bool] = None
allow_partials: t.Optional[bool] = None
interval_unit: t.Optional[IntervalUnit] = None
enabled: t.Optional[bool] = None
optimize_query: t.Optional[t.Union[str, bool]] = None
allow_partials: t.Optional[t.Union[str, bool]] = None
interval_unit: t.Optional[t.Union[str, IntervalUnit]] = None
enabled: t.Optional[t.Union[str, bool]] = None

_model_kind_validator = model_kind_validator
_on_destructive_change_validator = on_destructive_change_validator
Expand Down
21 changes: 20 additions & 1 deletion sqlmesh/core/model/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
create_sql_model,
create_models_from_blueprints,
get_model_name,
parse_defaults_properties,
render_meta_fields,
render_model_defaults,
)
from sqlmesh.core.model.kind import ModelKindName, _ModelKind
from sqlmesh.utils import registry_decorator, DECORATOR_RETURN_TYPE
Expand Down Expand Up @@ -159,8 +161,25 @@ def model(
if isinstance(rendered_name, exp.Expression):
rendered_fields["name"] = rendered_name.sql(dialect=dialect)

rendered_defaults = (
render_model_defaults(
defaults=defaults,
module_path=module_path,
macros=macros,
jinja_macros=jinja_macros,
variables=variables,
path=path,
dialect=dialect,
default_catalog=default_catalog,
)
if defaults
else {}
)

rendered_defaults = parse_defaults_properties(rendered_defaults, dialect=dialect)

common_kwargs = {
"defaults": defaults,
"defaults": rendered_defaults,
"path": path,
"time_column_format": time_column_format,
"python_env": serialize_env(env, path=module_path),
Expand Down
Loading