-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8cfa6e5
Feat: Support project generation from dlt pipeline
themisvaltinos a780073
Set gateway configs from credentials
themisvaltinos 6366a32
Add quotes around password
themisvaltinos b9b0122
Validate connection configs against corresponding sqlmesh class
themisvaltinos eecb1c2
Fix pydantic tests
themisvaltinos cfb4da5
Refactor
themisvaltinos 4605bf3
Code cleanup and refactors
themisvaltinos 2b7bb97
Add unit tests, example project; update docs; fixes
themisvaltinos d0cba32
Add missing return types
themisvaltinos a5fc8e9
Address mypy error
themisvaltinos 998572c
Generate single model to load from dlt tables
themisvaltinos d24d2e6
Update docs
themisvaltinos 78608f1
Change to incremental by time kind models
themisvaltinos 83e3954
Update docs
themisvaltinos b77398b
Remove version constrain
themisvaltinos 8981192
Dlt requirement
themisvaltinos bec6a31
Add dlt target in makefile
themisvaltinos ca7fdbc
Add dlt target in dev install
themisvaltinos f8c5088
Align columns formatting
themisvaltinos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_TIME_RANGE` model kind](../concepts/models/model_kinds.md#incremental_by_time_range). 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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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