-
Notifications
You must be signed in to change notification settings - Fork 183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: Support SQLMesh project generation from dlt pipeline #3218
Changes from 12 commits
8cfa6e5
a780073
6366a32
b9b0122
eecb1c2
cfb4da5
4605bf3
2b7bb97
d0cba32
a5fc8e9
998572c
d24d2e6
78608f1
83e3954
b77398b
8981192
bec6a31
ca7fdbc
f8c5088
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# dlt | ||
|
||
SQLMesh enables efforless project generation using data ingested through [dlt](https://github.com/dlt-hub/dlt). This involves creating a baseline project scaffolding, generating incremental models to process the data from the pipeline's tables by inspecting its schema and configuring the gateway connection using the pipeline's credentials. | ||
|
||
## Getting started | ||
### Reading from a dlt pipeline | ||
|
||
To load data from a dlt pipeline into SQLMesh, ensure the dlt pipeline has been run or restored locally. Then simply execute the sqlmesh `init` command *within the dlt project root directory* using the `dlt` template option and specifying the pipeline's name with the `dlt-pipeline` option: | ||
|
||
```bash | ||
$ sqlmesh init -t dlt --dlt-pipeline <pipeline-name> dialect | ||
``` | ||
|
||
This will create the configuration file and directories, which are found in all SQLMesh projects: | ||
|
||
- config.yaml | ||
- The file for project configuration. Refer to [configuration](../reference/configuration.md). | ||
- ./models | ||
- SQL and Python models. Refer to [models](../concepts/models/overview.md). | ||
- ./seeds | ||
- Seed files. Refer to [seeds](../concepts/models/seed_models.md). | ||
- ./audits | ||
- Shared audit files. Refer to [auditing](../concepts/audits.md). | ||
- ./tests | ||
- Unit test files. Refer to [testing](../concepts/tests.md). | ||
- ./macros | ||
- Macro files. Refer to [macros](../concepts/macros/overview.md). | ||
|
||
SQLMesh will also automatically generate models to ingest data from the pipeline incrementally. Incremental loading is ideal for large datasets where recomputing entire tables is resource-intensive. In this case utilizing the [`INCREMENTAL_BY_UNIQUE_KEY` model kind](../concepts/models/model_kinds.md#incremental_by_unique_key) with the unique `_dlt_load_id` key present in each dlt table. However, these model definitions can be customized to meet your specific project needs. | ||
|
||
#### Configuration | ||
|
||
SQLMesh will retrieve the data warehouse connection credentials from your dlt project to configure the `config.yaml` file. This configuration can be modified or customized as needed. For more details, refer to the [configuration guide](../guides/configuration.md). | ||
|
||
### Example | ||
|
||
Generating a SQLMesh project dlt is quite simple. In this example, we'll use the example `sushi_pipeline.py` from the [sushi-dlt project](https://github.com/TobikoData/sqlmesh/tree/main/examples/sushi_dlt). | ||
|
||
First, run the pipeline within the project directory: | ||
|
||
```bash | ||
$ python sushi_pipeline.py | ||
Pipeline sushi load step completed in 2.09 seconds | ||
Load package 1728074157.660565 is LOADED and contains no failed jobs | ||
``` | ||
|
||
After the pipeline has run, generate a SQLMesh project by executing: | ||
|
||
```bash | ||
$ sqlmesh init -t dlt --dlt-pipeline sushi duckdb | ||
``` | ||
|
||
Then the SQLMesh project is all set up. You can then proceed to run the SQLMesh `plan` command to ingest the dlt pipeline data and populate the SQLMesh tables: | ||
|
||
```bash | ||
$ sqlmesh plan | ||
New environment `prod` will be created from `prod` | ||
Summary of differences against `prod`: | ||
Models: | ||
└── Added: | ||
├── sushi_dataset_sqlmesh.incremental__dlt_loads | ||
├── sushi_dataset_sqlmesh.incremental_sushi_types | ||
└── sushi_dataset_sqlmesh.incremental_waiters | ||
Models needing backfill (missing dates): | ||
├── sushi_dataset_sqlmesh.incremental__dlt_loads: 2024-10-03 - 2024-10-03 | ||
├── sushi_dataset_sqlmesh.incremental_sushi_types: 2024-10-03 - 2024-10-03 | ||
└── sushi_dataset_sqlmesh.incremental_waiters: 2024-10-03 - 2024-10-03 | ||
Apply - Backfill Tables [y/n]: y | ||
Creating physical table ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100.0% • 3/3 • 0:00:00 | ||
|
||
All model versions have been created successfully | ||
|
||
[1/1] sushi_dataset_sqlmesh.incremental__dlt_loads evaluated in 0.01s | ||
[1/1] sushi_dataset_sqlmesh.incremental_sushi_types evaluated in 0.00s | ||
[1/1] sushi_dataset_sqlmesh.incremental_waiters evaluated in 0.01s | ||
Evaluating models ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100.0% • 3/3 • 0:00:00 | ||
|
||
|
||
All model batches have been executed successfully | ||
|
||
Virtually Updating 'prod' ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100.0% • 0:00:00 | ||
|
||
The target environment has been updated successfully | ||
``` | ||
|
||
Once the models are planned and applied, you can continue as with any SQLMesh project, generating and applying [plans](../concepts/overview.md#make-a-plan), running [tests](../concepts/overview.md#tests) or [audits](../concepts/overview.md#audits), and executing models with a [scheduler](../guides/scheduling.md) if desired. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import typing as t | ||
import dlt | ||
|
||
|
||
# Example sushi_types table | ||
@dlt.resource(name="sushi_types", primary_key="id", write_disposition="merge") | ||
def sushi_types() -> t.Iterator[t.Dict[str, t.Any]]: | ||
yield from [ | ||
{"id": 0, "name": "Tobiko"}, | ||
{"id": 1, "name": "Sashimi"}, | ||
{"id": 2, "name": "Maki"}, | ||
{"id": 3, "name": "Temaki"}, | ||
] | ||
|
||
|
||
# Example waiters table | ||
@dlt.resource(name="waiters", primary_key="id", write_disposition="merge") | ||
def waiters() -> t.Iterator[t.Dict[str, t.Any]]: | ||
yield from [ | ||
{"id": 0, "name": "Toby"}, | ||
{"id": 1, "name": "Tyson"}, | ||
{"id": 2, "name": "Ryan"}, | ||
{"id": 3, "name": "George"}, | ||
{"id": 4, "name": "Chris"}, | ||
{"id": 5, "name": "Max"}, | ||
{"id": 6, "name": "Vincent"}, | ||
{"id": 7, "name": "Iaroslav"}, | ||
{"id": 8, "name": "Emma"}, | ||
{"id": 9, "name": "Maia"}, | ||
] | ||
|
||
|
||
# Run the pipeline | ||
p = dlt.pipeline(pipeline_name="sushi", destination="duckdb") | ||
info = p.run([sushi_types(), waiters()]) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,7 @@ | |
"dbt-duckdb>=1.7.1", | ||
"dbt-snowflake", | ||
"dbt-bigquery", | ||
"dlt", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we want this in 2 places. Let's just add the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, removed it from the |
||
"Faker", | ||
"google-auth", | ||
"google-cloud-bigquery", | ||
|
@@ -105,6 +106,9 @@ | |
"dbt": [ | ||
"dbt-core<2", | ||
], | ||
"dlt": [ | ||
"dlt>=1.1.0", | ||
georgesittas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
], | ||
"gcppostgres": [ | ||
"cloud-sql-python-connector[pg8000]", | ||
], | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think you'll probably also want to define a start date or no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is generated automatically
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes also added a function to extract the start date from the pipeline directly to be set in the sqlmesh
config.yaml