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
This allows you to tailor the behavior of models for each gateway without affecting the global `model_defaults`.
891
+
892
+
For example, in some SQL engines identifiers like table and column names are case-sensitive, but they are case-insensitive in other engines. By default, a project that uses both types of engines would need to ensure the models for each engine aligned with the engine's normalization behavior, which makes project maintenance and debugging more challenging.
893
+
894
+
Gateway-specific `model_defaults` allow you to change how SQLMesh performs identifier normalization *by engine* to align the different engines' behavior.
895
+
896
+
In the example above, the project's default dialect is `snowflake` (line 14). The `redshift` gateway configuration overrides that global default dialect with `"snowflake,normalization_strategy=case_insensitive"` (line 6).
897
+
898
+
That value tells SQLMesh that the `redshift` gateway's models will be written in the Snowflake SQL dialect (so need to be transpiled from Snowflake to Redshift), but that the resulting Redshift SQL should treat identifiers as case-insensitive to match Snowflake's behavior.
899
+
900
+
868
901
#### Model Kinds
869
902
870
903
Model kinds are required in each model file's `MODEL` DDL statement. They may optionally be used to specify a default kind in the model defaults configuration key.
Organizations typically connect to a data warehouse through a single engine to ensure data consistency. However, there are cases where the processing capabilities of one engine may be better suited to specific tasks than another.
4
4
5
-
By decoupling storage from compute and with growing support for open table formats like Apache Icebergand Hive, different engines can now interact with the same data.
5
+
Companies are increasingly decoupling how/where data is stored from the how computations are run on the data, requiring interoperability across platforms and tools. Open table formats like Apache Iceberg, Delta Lake, and Hive provide a common storage format that can be used by multiple SQL engines.
6
6
7
-
With SQLMesh's new multi-engine feature, users can leverage multiple engine adapters within a single project, offering the flexibility to choose the best engine for each task.
7
+
SQLMesh enables this decoupling by supporting multiple engine adapters within a single project, giving you the flexibility to choose the best engine for each computational task. You can specify the engine each model uses, based on what computations the model performs or other organization-specific considerations.
8
8
9
-
This feature allows you to run each model on a specified engine, provided the data catalog is shared and the engines support read/write operations on it.
9
+
## Configuring a Project with Multiple Engines
10
10
11
+
Configuring your project to use multiple engines follows a simple process:
11
12
12
-
## Configuring project with multiple engines
13
+
- Include all required [gateway connections](../reference/configuration.md#connection) in your configuration.
14
+
- Specify the `gateway` to be used for execution in the `MODEL` DDL.
13
15
14
-
To configure a SQLMesh project with multiple engines, simply include all required gateway [connections](../reference/configuration.md#connection) in your configuration.
16
+
If no gateway is explicitly defined for a model, the [default_gateway](../reference/configuration.md#default-gateway) of the project is used.
15
17
16
-
Next, specify the appropriate `gateway`in the `MODEL` DDL for each model. If no gateway is explicitly defined, the default gateway will be used.
18
+
By default, virtual layer views are created in the `default_gateway`. This approach requires that all engines can read from and write to the same shared catalog, so a view in the `default_gateway` can access a table in another gateway.
17
19
18
-
The [virtual layer](../concepts/glossary.md#virtual-layer) will be created within the engine corresponding to the default gateway.
20
+
Alternatively, each gateway can create the virtual layer views for the models it runs. Use this approach by setting the [gateway_managed_virtual_layer](#gateway-managed-virtual-layer) flag to `true` in your project configuration.
19
21
20
-
### Example
22
+
### Shared Virtual Layer
21
23
22
-
Below is a simple example of setting up a project with connections to both DuckDB and PostgreSQL.
24
+
To dive deeper, in SQLMesh the [physical layer](../concepts/glossary.md#physical-layer) is the concrete data storage layer, where it stores and manages data in database tables and materialized views.
25
+
26
+
While, the [virtual layer](../concepts/glossary.md#virtual-layer) consists of views, one for each model, each pointing to a snapshot table in the physical layer.
27
+
28
+
In a multi-engine project with a shared data catalog, the model-specific gateway is responsible for the physical layer, while the default gateway is used for managing the virtual layer.
29
+
30
+
#### Example: DuckDB + PostgreSQL
23
31
24
-
In this setup, the PostgreSQL engine is set as the default, so it will be used to manage views in the virtual layer.
32
+
Below is a simple example of setting up a project with connections to both DuckDB and PostgreSQL.
25
33
26
-
Meanwhile, the DuckDB's [attach](https://duckdb.org/docs/sql/statements/attach.html) feature enables read-write access to the PostgreSQL catalog's physical tables.
34
+
In this setup, the PostgreSQL engine is set as the default, so it will be used to manage views in the virtual layer. Meanwhile, DuckDB's [attach](https://duckdb.org/docs/sql/statements/attach.html) feature enables read-write access to the PostgreSQL catalog's physical tables.
27
35
28
36
=== "YAML"
29
37
@@ -81,16 +89,15 @@ Meanwhile, the DuckDB's [attach](https://duckdb.org/docs/sql/statements/attach.h
81
89
port=5432,
82
90
user="postgres",
83
91
password="password",
84
-
database="main_db",
92
+
database="main_db",
85
93
)
86
94
),
87
95
},
88
96
default_gateway="postgres",
89
97
)
90
98
```
91
99
92
-
Given this configuration, when a model’s gateway is set to duckdb, it will be materialized within the PostgreSQL `main_db` catalog, but it will be evaluated using DuckDB’s engine.
93
-
100
+
Given this configuration, when a model’s gateway is set to DuckDB, the DuckDB engine will perform the calculations before materializing the physical table in the PostgreSQL `main_db` catalog.
In this model, the DuckDB engine can be used to scan and load data from an iceberg table and create the physical table in the PostgreSQL database.
116
+
The `order_ship_date` model specifies the DuckDB engine, which will perform the computations used to create the physical table in the PostgreSQL database.
117
+
118
+
This allows you to efficiently scan data from an Iceberg table, or even query tables directly from S3 when used with the [HTTPFS](https://duckdb.org/docs/stable/extensions/httpfs/overview.html) extension.
In models where no gateway is specified, such as the `customer_orders` model, the default PostgreSQL engine will both create the physical table and the views in the virtual layer.
123
+
124
+
### Gateway-Managed Virtual Layer
125
+
126
+
By default, all virtual layer views are created in the project's default gateway.
127
+
128
+
If your project's engines don’t have a mutually accessible catalog or your raw data is located in different engines, you may prefer for each model's virtual layer view to exist in the gateway that ran the model. This allows a single SQLMesh project to manage isolated sets of models in different gateways, which is sometimes necessary for data governance or security concerns.
129
+
130
+
To enable this, set `gateway_managed_virtual_layer` to `true` in your configuration. By default, this flag is set to false.
131
+
132
+
#### Example: Redshift + Athena + Snowflake
133
+
134
+
Consider a scenario where you need to create a project with models in Redshift, Athena and Snowflake, where each engine hosts its models' virtual layer views.
135
+
136
+
First, add the connections to your configuration and set the `gateway_managed_virtual_layer` flag to `true`:
Note that gateway-specific variables take precedence over global ones. In the example above, the `gw_var` used in a model will resolve to the value specified in the model's gateway.
236
+
237
+
For further customization, you can also enable [gateway-specific model defaults](../guides/configuration.md#gateway-specific-model-defaults). This allows you to define custom behaviors, such as specifying a dialect with case-insensitivity normalization.
238
+
239
+
The default gateway is `redshift` In the example configuration above, so all models without a `gateway` specification will run on redshift, as in this `order_dates` model:
240
+
241
+
```sql linenums="1"
242
+
MODEL (
243
+
name redshift_schema.order_dates,
244
+
table_format iceberg,
245
+
);
246
+
247
+
SELECT
248
+
order_date,
249
+
order_id
250
+
FROM
251
+
bucket.raw_data;
252
+
```
253
+
254
+
For the `athena_schema.order_status` model, we explicitly specify the `athena` gateway:
255
+
256
+
```sql linenums="1" hl_lines="4"
257
+
MODEL (
258
+
name athena_schema.order_status,
259
+
table_format iceberg,
260
+
gateway athena,
261
+
);
262
+
263
+
SELECT
264
+
order_id,
265
+
status
266
+
FROM
267
+
bucket.raw_data;
268
+
```
269
+
270
+
Finally, specifying the `snowflake` gateway for the `customer_orders` model ensures it is isolated from the rest and reads from a table within the Snowflake database:
When you run the plan, the catalogs for each model will be set automatically based on the gateway’s connection and each corresponding model will be executed by the specified engine:
290
+
291
+
```bash
292
+
❯ sqlmesh plan
293
+
294
+
`prod` environment will be initialized
295
+
296
+
Models:
297
+
└── Added:
298
+
├── awsdatacatalog.athena_schema.order_status # each model uses its gateway's catalog and schema
Copy file name to clipboardExpand all lines: docs/reference/configuration.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,7 +35,7 @@ Configuration options for SQLMesh environment creation and promotion.
35
35
|`physical_schema_override`| (Deprecated) Use `physical_schema_mapping` instead. A mapping from model schema names to names of schemas in which physical tables for the corresponding models will be placed. | dict[string, string]| N |
36
36
|`physical_schema_mapping`| A mapping from regular expressions to names of schemas in which physical tables for the corresponding models [will be placed](../guides/configuration.md#physical-table-schemas). (Default physical schema name: `sqlmesh__[model schema]`) | dict[string, string]| N |
37
37
|`environment_suffix_target`| Whether SQLMesh views should append their environment name to the `schema` or `table` - [additional details](../guides/configuration.md#view-schema-override). (Default: `schema`) | string | N |
38
-
|`gateway_managed_virtual_layer`| Whether SQLMesh views of the virtual layer will be created by the default gateway or model specified gateways - [additional details](../guides/configuration.md#view-schema-override). (Default: False) | boolean | N |
38
+
|`gateway_managed_virtual_layer`| Whether SQLMesh views of the virtual layer will be created by the default gateway or model specified gateways - [additional details](../guides/multi_engine.md#gateway-managed-virtual-layer). (Default: False) | boolean | N |
39
39
|`environment_catalog_mapping`| A mapping from regular expressions to catalog names. The catalog name is used to determine the target catalog for a given environment. | dict[string, string]| N |
40
40
|`log_limit`| The default number of logs to keep (Default: `20`) | int | N |
0 commit comments