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/models/python_models.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -102,6 +102,8 @@ For example, pre/post-statements might modify settings or create indexes. Howeve
102
102
103
103
You can set the `pre_statements` and `post_statements` arguments to a list of SQL strings, SQLGlot expressions, or macro calls to define the model's pre/post-statements.
104
104
105
+
**Project-level defaults:** You can also define pre/post-statements at the project level using `model_defaults` in your configuration. These will be applied to all models in your project and merged with any model-specific statements. Default statements are executed first, followed by model-specific statements. Learn more about this in the [model configuration reference](../../reference/model_configuration.md#model-defaults).
106
+
105
107
```python linenums="1" hl_lines="8-12"
106
108
@model(
107
109
"db.test_model",
@@ -182,6 +184,8 @@ These can be used, for example, to grant privileges on views of the virtual laye
182
184
183
185
Similar to pre/post-statements you can set the `on_virtual_update` argument in the `@model` decorator to a list of SQL strings, SQLGlot expressions, or macro calls.
184
186
187
+
**Project-level defaults:** You can also define on-virtual-update statements at the project level using `model_defaults` in your configuration. These will be applied to all models in your project (including Python models) and merged with any model-specific statements. Default statements are executed first, followed by model-specific statements. Learn more about this in the [model configuration reference](../../reference/model_configuration.md#model-defaults).
Copy file name to clipboardExpand all lines: docs/concepts/models/seed_models.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -203,6 +203,8 @@ ALTER SESSION SET TIMEZONE = 'PST';
203
203
204
204
Seed models also support on-virtual-update statements, which are executed after the completion of the [Virtual Update](#virtual-update).
205
205
206
+
**Project-level defaults:** You can also define on-virtual-update statements at the project level using `model_defaults` in your configuration. These will be applied to all models in your project (including seed models) and merged with any model-specific statements. Default statements are executed first, followed by model-specific statements. Learn more about this in the [model configuration reference](../../reference/model_configuration.md#model-defaults).
207
+
206
208
These must be enclosed within an `ON_VIRTUAL_UPDATE_BEGIN;` ...; `ON_VIRTUAL_UPDATE_END;` block:
Copy file name to clipboardExpand all lines: docs/concepts/models/sql_models.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -67,6 +67,8 @@ For example, pre/post-statements might modify settings or create a table index.
67
67
68
68
Pre/post-statements are just standard SQL commands located before/after the model query. They must end with a semi-colon, and the model query must end with a semi-colon if a post-statement is present. The [example above](#example) contains both pre- and post-statements.
69
69
70
+
**Project-level defaults:** You can also define pre/post-statements at the project level using `model_defaults` in your configuration. These will be applied to all models in your project and merged with any model-specific statements. Default statements are executed first, followed by model-specific statements. Learn more about this in the [model configuration reference](../../reference/model_configuration.md#model-defaults).
71
+
70
72
!!! warning
71
73
72
74
Pre/post-statements are evaluated twice: when a model's table is created and when its query logic is evaluated. Executing statements more than once can have unintended side-effects, so you can [conditionally execute](../macros/sqlmesh_macros.md#prepost-statements) them based on SQLMesh's [runtime stage](../macros/macro_variables.md#runtime-variables).
@@ -97,6 +99,8 @@ The optional on-virtual-update statements allow you to execute SQL commands afte
97
99
98
100
These can be used, for example, to grant privileges on views of the virtual layer.
99
101
102
+
**Project-level defaults:** You can also define on-virtual-update statements at the project level using `model_defaults` in your configuration. These will be applied to all models in your project and merged with any model-specific statements. Default statements are executed first, followed by model-specific statements. Learn more about this in the [model configuration reference](../../reference/model_configuration.md#model-defaults).
103
+
100
104
These SQL statements must be enclosed within an `ON_VIRTUAL_UPDATE_BEGIN;` ...; `ON_VIRTUAL_UPDATE_END;` block like this:
Copy file name to clipboardExpand all lines: docs/guides/configuration.md
+104-2Lines changed: 104 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -320,10 +320,14 @@ The cache directory is automatically created if it doesn't exist. You can clear
320
320
321
321
SQLMesh creates schemas, physical tables, and views in the data warehouse/engine. Learn more about why and how SQLMesh creates schema in the ["Why does SQLMesh create schemas?" FAQ](../faq/faq.md#schema-question).
322
322
323
-
The default SQLMesh behavior described in the FAQ is appropriate for most deployments, but you can override where SQLMesh creates physical tables and views with the `physical_schema_mapping`, `environment_suffix_target`, and `environment_catalog_mapping` configuration options. These options are in the [environments](../reference/configuration.md#environments) section of the configuration reference page.
323
+
The default SQLMesh behavior described in the FAQ is appropriate for most deployments, but you can override *where* SQLMesh creates physical tables and views with the `physical_schema_mapping`, `environment_suffix_target`, and `environment_catalog_mapping` configuration options.
324
+
325
+
You can also override *what* the physical tables are called by using the `physical_table_naming_convention` option.
326
+
327
+
These options are in the [environments](../reference/configuration.md#environments) section of the configuration reference page.
324
328
325
329
#### Physical table schemas
326
-
By default, SQLMesh creates physical tables for a model with a naming convention of `sqlmesh__[model schema]`.
330
+
By default, SQLMesh creates physical schemas for a model with a naming convention of `sqlmesh__[model schema]`.
327
331
328
332
This can be overridden on a per-schema basis using the `physical_schema_mapping` option, which removes the `sqlmesh__` prefix and uses the [regex pattern](https://docs.python.org/3/library/re.html#regular-expression-syntax) you provide to map the schemas defined in your model to their corresponding physical schemas.
329
333
@@ -436,6 +440,104 @@ Given the example of a model called `my_schema.users` with a default catalog of
436
440
- Using `environment_suffix_target: catalog` only works on engines that support querying across different catalogs. If your engine does not support cross-catalog queries then you will need to use `environment_suffix_target: schema` or `environment_suffix_target: table` instead.
437
441
- Automatic catalog creation is not supported on all engines even if they support cross-catalog queries. For engines where it is not supported, the catalogs must be managed externally from SQLMesh and exist prior to invoking SQLMesh.
438
442
443
+
#### Physical table naming convention
444
+
445
+
Out of the box, SQLMesh has the following defaults set:
- no `physical_schema_mapping` overrides, so a `sqlmesh__<model schema>` physical schema will be created for each model schema
450
+
451
+
This means that given a catalog of `warehouse` and a model named `finance_mart.transaction_events_over_threshold`, SQLMesh will create physical tables using the following convention:
This deliberately contains some redundancy with the *model* schema as it's repeated at the physical layer in both the physical schema name as well as the physical table name.
460
+
461
+
This default exists to make the physical table names portable between different configurations. If you were to define a `physical_schema_mapping` that maps all models to the same physical schema, since the model schema is included in the table name as well, there are no naming conflicts.
462
+
463
+
##### Table only
464
+
465
+
Some engines have object name length limitations which cause them to [silently truncate](https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS) table and view names that exceed this limit. This behaviour breaks SQLMesh, so we raise a runtime error if we detect the engine would silently truncate the name of the table we are trying to create.
466
+
467
+
Having redundancy in the physical table names does reduce the number of characters that can be utilised in model names. To increase the number of characters available to model names, you can use `physical_table_naming_convention` like so:
468
+
469
+
=== "YAML"
470
+
471
+
```yaml linenums="1"
472
+
physical_table_naming_convention: table_only
473
+
```
474
+
475
+
=== "Python"
476
+
477
+
```python linenums="1"
478
+
from sqlmesh.core.config import Config, ModelDefaultsConfig, TableNamingConvention
Notice that the model schema name is no longer part of the physical table name. This allows for slightly longer model names on engines with low identifier length limits, which may be useful for your project.
494
+
495
+
In this configuration, it is your responsibility to ensure that any schema overrides in `physical_schema_mapping` result in each model schema getting mapped to a unique physical schema.
496
+
497
+
For example, the following configuration will cause **data corruption**:
498
+
499
+
```yaml
500
+
physical_table_naming_convention: table_only
501
+
physical_schema_mapping:
502
+
'.*': sqlmesh
503
+
```
504
+
505
+
This is because every model schema is mapped to the same physical schema but the model schema name is omitted from the physical table name.
506
+
507
+
##### MD5 hash
508
+
509
+
If you *still* need more characters, you can set `physical_table_naming_convention: hash_md5` like so:
510
+
511
+
=== "YAML"
512
+
513
+
```yaml linenums="1"
514
+
physical_table_naming_convention: hash_md5
515
+
```
516
+
517
+
=== "Python"
518
+
519
+
```python linenums="1"
520
+
from sqlmesh.core.config import Config, ModelDefaultsConfig, TableNamingConvention
This has a downside that now it's much more difficult to determine which table corresponds to which model by just looking at the database with a SQL client. However, the table names have a predictable length so there are no longer any surprises with identfiers exceeding the max length at the physical layer.
540
+
439
541
#### Environment view catalogs
440
542
441
543
By default, SQLMesh creates an environment view in the same [catalog](../concepts/glossary.md#catalog) as the physical table the view points to. The physical table's catalog is determined by either the catalog specified in the model name or the default catalog defined in the connection.
0 commit comments