Skip to content

Commit 88f57cd

Browse files
Docs: Update multi engine gateway guide
1 parent 466f249 commit 88f57cd

5 files changed

Lines changed: 242 additions & 17 deletions

File tree

docs/guides/configuration.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,33 @@ This may be useful in cases where the name casing needs to be preserved, since t
948948

949949
See [here](https://sqlglot.com/sqlglot/dialects/dialect.html#NormalizationStrategy) to learn more about the supported normalization strategies.
950950

951+
##### Gateway-specific model defaults
952+
953+
You can also define gateway specific `model_defaults` in the `gateways` section, which override the global defaults for that gateway.
954+
955+
```yaml linenums="1"
956+
gateways:
957+
redshift:
958+
connection:
959+
type: redshift
960+
model_defaults:
961+
dialect: "snowflake,normalization_strategy=case_insensitive"
962+
snowflake:
963+
connection:
964+
type: snowflake
965+
966+
default_gateway: snowflake
967+
968+
model_defaults:
969+
dialect: snowflake
970+
start: 2025-02-05
971+
```
972+
973+
This allows you to tailor the behavior of models for each gateway without affecting the global model_defaults.
974+
975+
For example, you can adjust dialect-specific behavior, like the normalization to be case insensitive, to better match the engine’s requirements and avoid compatibility issues.
976+
977+
951978
#### Model Kinds
952979

953980
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.

docs/guides/multi_engine.md

Lines changed: 214 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,36 @@
22

33
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.
44

5-
By decoupling storage from compute and with growing support for open table formats like Apache Iceberg and Hive, different engines can now interact with the same data.
5+
Across the industry, companies are increasingly decoupling storage from compute, demanding interoperability across platforms and tools, focusing on cost efficiency and a growing support for open table formats like Apache Iceberg and Hive.
66

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+
In SQLMesh, you can use multiple engine adapters within a single project, giving you the flexibility to choose the most suitable engine for each task. This allows individual models to run on a specified engine based on their specific requirements.
88

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
1010

11+
Configuring your project to use multiple engines follows a simple process:
1112

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.
1315

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.
1517

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, the `default_gateway` is also responsible to create the views of the virtual layer. This assumes that all engines can read from and write to the same shared catalog.
1719

18-
The [virtual layer](../concepts/glossary.md#virtual-layer) will be created within the engine corresponding to the default gateway.
20+
Alternatively, you can configure the model-specific gateway to create the views of the virtual layer by setting [gateway_managed_virtual_layer](#gateway-managed-virtual-layer) flag in your configuration to true.
1921

20-
### Example
22+
### Shared Virtual Layer
2123

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
2331

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.
2533

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, the DuckDB's [attach](https://duckdb.org/docs/sql/statements/attach.html) feature enables read-write access to the PostgreSQL catalog's physical tables.
2735

2836
=== "YAML"
2937

@@ -81,7 +89,7 @@ Meanwhile, the DuckDB's [attach](https://duckdb.org/docs/sql/statements/attach.h
8189
port=5432,
8290
user="postgres",
8391
password="password",
84-
database="main_db",
92+
database="main_db",
8593
)
8694
),
8795
},
@@ -91,6 +99,7 @@ Meanwhile, the DuckDB's [attach](https://duckdb.org/docs/sql/statements/attach.h
9199

92100
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.
93101

102+
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.
94103

95104
```sql linenums="1"
96105
MODEL (
@@ -100,12 +109,201 @@ MODEL (
100109
);
101110

102111
SELECT
103-
l_orderkey,
112+
l_orderkey,
104113
l_shipdate
105-
FROM
114+
FROM
106115
iceberg_scan('data/bucket/lineitem_iceberg', allow_moved_paths = true);
107116
```
108117

109-
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.
118+
In the `order_ship_date` model, the DuckDB engine is set, which will be used to create the physical table in the PostgreSQL database.
119+
120+
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.
121+
122+
![PostgreSQL + DuckDB](./multi_engine/postgres_duckdb.png)
123+
124+
In models where no gateway is specified, such as the `customer_orders` model, the default PostgreSQL engine will be used to create the physical table as well as to create and manage the views of the virtual layer.
125+
126+
### Gateway-Managed Virtual Layer
127+
128+
For projects where the engines don’t share a catalog or your raw data is located in different warehouses, you may prefer each gateway to manage its own virtual layer. This ensures isolation and each model’s views being created by its respective gateway.
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. To set this you, add the connections to your configuration and set the `gateway_managed_virtual_layer` flag:
135+
136+
=== "YAML"
137+
138+
```yaml linenums="1"
139+
gateways:
140+
redshift:
141+
connection:
142+
type: redshift
143+
user: <redshift_user>
144+
password: <redshift_password>
145+
host: <redshift_host>
146+
database: <redshift_database>
147+
variables:
148+
gw_var: 'redshift'
149+
athena:
150+
connection:
151+
type: athena
152+
aws_access_key_id: <athena_aws_access_key_id>
153+
aws_secret_access_key: <athena_aws_secret_access_key>
154+
s3_warehouse_location: <athena_s3_warehouse_location>
155+
variables:
156+
gw_var: 'athena'
157+
snowflake:
158+
connection:
159+
type: snowflake
160+
account: <snowflake_account>
161+
user: <snowflake_user>
162+
database: <snowflake_database>
163+
warehouse: <snowflake_warehouse>
164+
variables:
165+
gw_var: 'snowflake'
166+
167+
default_gateway: redshift
168+
gateway_managed_virtual_layer: true
169+
170+
variables:
171+
gw_var: 'global'
172+
global_var: 5
173+
```
174+
175+
=== "Python"
176+
177+
```python linenums="1"
178+
from sqlmesh.core.config import (
179+
Config,
180+
ModelDefaultsConfig,
181+
GatewayConfig,
182+
RedshiftConnectionConfig,
183+
AthenaConnectionConfig,
184+
SnowflakeConnectionConfig,
185+
)
186+
187+
config = Config(
188+
model_defaults=ModelDefaultsConfig(dialect="redshift"),
189+
gateways={
190+
"redshift": GatewayConfig(
191+
connection=RedshiftConnectionConfig(
192+
user="<redshift_user>",
193+
password="<redshift_password>",
194+
host="<redshift_host>",
195+
database="<redshift_database>",
196+
),
197+
variables={
198+
"gw_var": "redshift"
199+
},
200+
),
201+
"athena": GatewayConfig(
202+
connection=AthenaConnectionConfig(
203+
aws_access_key_id="<athena_aws_access_key_id>",
204+
aws_secret_access_key="<athena_aws_secret_access_key>",
205+
region_name="<athena_region_name>",
206+
s3_warehouse_location="<athena_s3_warehouse_location>",
207+
),
208+
variables={
209+
"gw_var": "athena"
210+
},
211+
),
212+
"snowflake": GatewayConfig(
213+
connection=SnowflakeConnectionConfig(
214+
account="<snowflake_account>",
215+
user="<snowflake_user>",
216+
database="<snowflake_database>",
217+
warehouse="<snowflake_warehouse>",
218+
),
219+
variables={
220+
"gw_var": "snowflake"
221+
},
222+
),
223+
},
224+
default_gateway="redshift",
225+
gateway_managed_virtual_layer=True,
226+
variables={
227+
"gw_var": "global",
228+
"global_var": 5,
229+
},
230+
)
231+
```
232+
233+
Note that gateway-specific variables take precedence over global ones. In the example above, the `gw_var` used in a model will take the value defined for the respective gateway.
234+
235+
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.
236+
237+
```sql linenums="1"
238+
MODEL (
239+
name redshift_schema.order_dates,
240+
table_format iceberg,
241+
);
242+
243+
SELECT
244+
order_date,
245+
order_id
246+
FROM
247+
bucket.raw_data;
248+
```
249+
250+
In this setup, since the default gateway is set to redshift, omitting the gateway from a model will default to this, as seen in the `order_dates` model above.
251+
252+
```sql linenums="1"
253+
MODEL (
254+
name athena_schema.order_status,
255+
table_format iceberg,
256+
gateway athena,
257+
);
258+
259+
SELECT
260+
order_id,
261+
status
262+
FROM
263+
bucket.raw_data;
264+
```
265+
266+
While in the case of the `athena_schema.order_status` model above, the gateway is specified to athena explicitly.
267+
268+
```sql linenums="1"
269+
MODEL (
270+
name snowflake_schema.customer_orders,
271+
table_format iceberg,
272+
gateway snowflake
273+
);
274+
275+
SELECT
276+
customer_id,
277+
orders
278+
FROM
279+
bronze_schema.customer_data;
280+
```
281+
282+
Finally, specifying the snowflake gateway for the `customer_orders` model ensures it is isolated from the rest and sources from a table within the snowflake database.
283+
284+
![Athena + Redshift + Snowflake](./multi_engine/athena_redshift_snowflake.png)
285+
286+
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 evaluated against the specified engine.
287+
288+
```bash
289+
❯ sqlmesh plan
290+
291+
`prod` environment will be initialized
292+
293+
Models:
294+
└── Added:
295+
├── awsdatacatalog.athena_schema.order_status
296+
├── redshift_schema.order_dates
297+
└── silver.snowflake_schema.customers
298+
Models needing backfill:
299+
├── awsdatacatalog.athena_schema.order_status: [full refresh]
300+
├── redshift_schema.order_dates: [full refresh]
301+
└── silver.snowflake_schema.customers: [full refresh]
302+
Apply - Backfill Tables [y/n]: y
303+
```
304+
305+
The views of the virtual layer will also be created by each corresponding engine.
306+
307+
This approach provides isolation between your models, while maintaining centralized control over your project.
110308

111-
While the PostgreSQL engine is responsible for creating the model's view for the virtual layer.
309+
This allows users to leverage multiple engines within a single SQLMesh project, particularly as the industry shifts toward data lakes, open table formats, and greater interoperability.
125 KB
Loading
75.3 KB
Loading

docs/reference/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Configuration options for SQLMesh environment creation and promotion.
3535
| `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 |
3636
| `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 |
3737
| `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 |
3939
| `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 |
4040
| `log_limit` | The default number of logs to keep (Default: `20`) | int | N |
4141

0 commit comments

Comments
 (0)