Skip to content

Commit 28404b2

Browse files
committed
Add explain and DuckDB CLI sections
1 parent e77c2fb commit 28404b2

File tree

3 files changed

+107
-25
lines changed

3 files changed

+107
-25
lines changed

docs/quickstart/cli.md

Lines changed: 107 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ Enter a number: 1
124124

125125
SQLMesh's core commands have multiple options that alter their behavior. Some of those options streamline the SQLMesh `plan` workflow and CLI output.
126126

127-
If you prefer a streamlined workflow (no prompts, no diff previews, auto-apply changes), choose the `FLOW` CLI mode to automatically include those options in your project configuration file. If you prefer to see all the output SQLMesh provides, choose `DEFAULT` mode.
127+
If you prefer a streamlined workflow (no prompts, no file diff previews, auto-apply changes), choose the `FLOW` CLI mode to automatically include those options in your project configuration file.
128+
129+
If you prefer to see all the output SQLMesh provides, choose `DEFAULT` mode, which we will use in this quickstart:
128130

129131
``` bash
130132
Choose your SQLMesh CLI experience:
@@ -147,18 +149,18 @@ If you chose a different engine, add your engine's connection information to the
147149
Your SQLMesh project is ready!
148150

149151
Next steps:
150-
Update your gateway connection settings (e.g., username/password) in the project configuration file:
151-
/Users/trey/tobiko/sqlmesh/sqlmesh-example-fresh/test/config.yaml
152-
Run command in CLI: sqlmesh plan
153-
(Optional) Explain a plan: sqlmesh plan --explain
152+
- Update your gateway connection settings (e.g., username/password) in the project configuration file:
153+
/sqlmesh-example/config.yaml
154+
- Run command in CLI: sqlmesh plan
155+
- (Optional) Explain a plan: sqlmesh plan --explain
154156

155157
Quickstart guide:
156158
https://sqlmesh.readthedocs.io/en/stable/quickstart/cli/
157159

158160
Need help?
159-
Docs: https://sqlmesh.readthedocs.io
160-
Slack: https://www.tobikodata.com/slack
161-
GitHub: https://github.com/TobikoData/sqlmesh/issues
161+
- Docs: https://sqlmesh.readthedocs.io
162+
- Slack: https://www.tobikodata.com/slack
163+
- GitHub: https://github.com/TobikoData/sqlmesh/issues
162164
```
163165

164166
??? info "Learn more about the project's configuration: `config.yaml`"
@@ -212,7 +214,7 @@ Need help?
212214

213215
Learn more about SQLMesh project configuration [here](../reference/configuration.md).
214216

215-
The scaffold will also include multiple directories where SQLMesh project files are stored and multiple files that constitute the example project (e.g., SQL models).
217+
The scaffold generator creates multiple directories where SQLMesh project files are stored and multiple files that constitute the example project (e.g., SQL models).
216218

217219
??? info "Learn more about the project directories and files"
218220
SQLMesh uses a scaffold generator to initiate a new project. The generator will create multiple sub-directories and files for organizing your SQLMesh project code.
@@ -245,7 +247,7 @@ The scaffold will also include multiple directories where SQLMesh project files
245247
- ./tests
246248
- test_full_model.yaml
247249

248-
Finally, the scaffold will include data for the example project to use.
250+
Finally, the scaffold generator creates data for the example project to use.
249251

250252
??? info "Learn more about the project's data"
251253
The data used in this example project is contained in the `seed_data.csv` file in the `/seeds` project directory. The data reflects sales of 3 items over 7 days in January 2020.
@@ -291,7 +293,7 @@ The first SQLMesh plan must execute every model to populate the production envir
291293
```bash linenums="1"
292294
$ sqlmesh plan
293295
======================================================================
294-
Successfully Ran 1 tests against duckdb
296+
Successfully Ran 1 tests against duckdb in 0.1 seconds.
295297
----------------------------------------------------------------------
296298

297299
`prod` environment will be initialized
@@ -303,20 +305,18 @@ Models:
303305
└── sqlmesh_example.seed_model
304306
Models needing backfill:
305307
├── sqlmesh_example.full_model: [full refresh]
306-
├── sqlmesh_example.incremental_model: [2020-01-01 - 2025-04-17]
308+
├── sqlmesh_example.incremental_model: [2020-01-01 - 2025-06-22]
307309
└── sqlmesh_example.seed_model: [full refresh]
308310
Apply - Backfill Tables [y/n]:
309311
```
310312

311313
Line 3 of the output notes that `sqlmesh plan` successfully executed the project's test `tests/test_full_model.yaml` with duckdb.
312314

313-
Line 5 describes what environments the plan will affect when applied - a new `prod` environment in this case.
314-
315-
Lines 7-11 of the output show that SQLMesh detected three new models relative to the current empty environment.
315+
Line 6 describes what environments the plan will affect when applied - a new `prod` environment in this case.
316316

317-
Lines 12-16 list each model that will be executed by the plan, along with the date intervals or refresh types. For both `full_model` and `seed_model`, it shows `[full refresh]`, while for `incremental_model` it shows a specific date range `[2020-01-01 - 2025-04-17]`. The incremental model date range begins from 2020-01-01 because the `full` model kind always fully rebuilds its table.
317+
Lines 8-12 of the output show that SQLMesh detected three new models relative to the current empty environment.
318318

319-
The `seed_model` date range begins on the same day the plan was made because `SEED` models have no temporality associated with them other than whether they have been modified since the previous SQLMesh plan.
319+
Lines 13-16 list each model that will be executed by the plan, along with the date intervals or refresh types. For both `full_model` and `seed_model`, it shows `[full refresh]`, while for `incremental_model` it shows a specific date range `[2020-01-01 - 2025-06-22]`. The incremental model date range begins from 2020-01-01 because its definition specifies a model start date of `2020-01-01`.
320320

321321
??? info "Learn more about the project's models"
322322

@@ -393,23 +393,23 @@ The `seed_model` date range begins on the same day the plan was made because `SE
393393
GROUP BY item_id
394394
```
395395

396-
Line 16 asks you whether to proceed with executing the model backfills described in lines 11-14. Enter `y` and press `Enter`, and SQLMesh will execute the models and return this output:
396+
Line 18 asks you whether to proceed with executing the model backfills described in lines 13-16. Enter `y` and press `Enter`, and SQLMesh will execute the models and return this output:
397397

398398
```bash linenums="1"
399399
Apply - Backfill Tables [y/n]: y
400400

401-
Updating physical layer ━━━━━━━━━━━━━━━━━━━━━━━━ 100.0% • 3/3 • 0:00:00
401+
Updating physical layer ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100.0% • 3/3 • 0:00:00
402402

403403
✔ Physical layer updated
404404

405-
[1/1] sqlmesh_example.seed_model [insert seed file] 0.02s
406-
[1/1] sqlmesh_example.incremental_model [insert 2020-01-01 -2025-04-17] 0.03s
407-
[1/1] sqlmesh_example.full_model [full refresh, audits ✔1] 0.05s
408-
Executing model batches ━━━━━━━━━━━━━━━━━━━━━━━━ 100.0% • 3/3 • 0:00:00
405+
[1/1] sqlmesh_example.seed_model [insert seed file] 0.01s
406+
[1/1] sqlmesh_example.incremental_model [insert 2020-01-01 - 2025-06-22] 0.01s
407+
[1/1] sqlmesh_example.full_model [full refresh, audits ✔1] 0.01s
408+
Executing model batches ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100.0% • 3/3 • 0:00:00
409409

410410
✔ Model batches executed
411411

412-
Updating virtual layer ━━━━━━━━━━━━━━━━━━━━━━━━ 100.0% • 3/3 • 0:00:00
412+
Updating virtual layer ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100.0% • 3/3 • 0:00:00
413413

414414
✔ Virtual layer updated
415415
```
@@ -428,7 +428,89 @@ Lines 12-14 show the progress and completion of the second step - executing mode
428428

429429
Lines 16-18 show the progress and completion of the final step - virtually updating the plan's target environment, which makes the data available for querying.
430430

431-
You've now created a new production environment with all of history backfilled.
431+
??? "Learn more about the plan's actions"
432+
433+
Before applying a plan, you can view a detailed description of the actions it will take by passing the explain flag in your `sqlmesh plan` command:
434+
435+
```bash
436+
sqlmesh plan --explain
437+
```
438+
439+
Passing the explain flag for the quickstart example project above adds the following information to the output:
440+
441+
```bash
442+
Explained plan
443+
├── Validate SQL and create physical layer tables and views if they do not exist
444+
│ ├── sqlmesh_example.seed_model -> db.sqlmesh__sqlmesh_example.sqlmesh_example__seed_model__2185867172
445+
│ │ ├── Dry run model query without inserting results
446+
│ │ └── Create table if it doesn't exist
447+
│ ├── sqlmesh_example.full_model -> db.sqlmesh__sqlmesh_example.sqlmesh_example__full_model__2278521865
448+
│ │ ├── Dry run model query without inserting results
449+
│ │ └── Create table if it doesn't exist
450+
│ └── sqlmesh_example.incremental_model -> db.sqlmesh__sqlmesh_example.sqlmesh_example__incremental_model__1880815781
451+
│ ├── Dry run model query without inserting results
452+
│ └── Create table if it doesn't exist
453+
├── Backfill models by running their queries and run standalone audits
454+
│ ├── sqlmesh_example.seed_model -> db.sqlmesh__sqlmesh_example.sqlmesh_example__seed_model__2185867172
455+
│ │ └── Fully refresh table
456+
│ ├── sqlmesh_example.full_model -> db.sqlmesh__sqlmesh_example.sqlmesh_example__full_model__2278521865
457+
│ │ ├── Fully refresh table
458+
│ │ └── Run 'assert_positive_order_ids' audit
459+
│ └── sqlmesh_example.incremental_model -> db.sqlmesh__sqlmesh_example.sqlmesh_example__incremental_model__1880815781
460+
│ └── Fully refresh table
461+
└── Update the virtual layer for environment 'prod'
462+
└── Create or update views in the virtual layer to point at new physical tables and views
463+
├── sqlmesh_example.full_model -> db.sqlmesh__sqlmesh_example.sqlmesh_example__full_model__2278521865
464+
├── sqlmesh_example.seed_model -> db.sqlmesh__sqlmesh_example.sqlmesh_example__seed_model__2185867172
465+
└── sqlmesh_example.incremental_model -> db.sqlmesh__sqlmesh_example.sqlmesh_example__incremental_model__1880815781
466+
```
467+
468+
The explanation has three top-level sections, corresponding to the three types of actions a plan takes:
469+
470+
- Validate SQL and create physical layer tables and views if they do not exist
471+
- Backfill models by running their queries and run standalone audits
472+
- Update the virtual layer for environment 'prod'
473+
474+
Each section lists the affected models and provides more information about what will occur. For example, the first model in the first section is:
475+
476+
```bash
477+
├── sqlmesh_example.seed_model -> db.sqlmesh__sqlmesh_example.sqlmesh_example__seed_model__2185867172
478+
│ ├── Dry run model query without inserting results
479+
│ └── Create table if it doesn't exist
480+
```
481+
482+
The first line shows the model name `sqlmesh_example.seed_model` and the physical layer table SQLMesh will create to store its data: `db.sqlmesh__sqlmesh_example.sqlmesh_example__seed_model__2185867172`. The second and third lines tell us that in this step SQLMesh will dry-run the model query and create the physical layer table if it doesn't exist.
483+
484+
The second section describes what will occur during the backfill step. The second model in this section is:
485+
486+
```bash
487+
├── sqlmesh_example.full_model -> db.sqlmesh__sqlmesh_example.sqlmesh_example__full_model__2278521865
488+
│ ├── Fully refresh table
489+
│ └── Run 'assert_positive_order_ids' audit
490+
```
491+
492+
The first line shows the model name `sqlmesh_example.full_model` and the physical layer table SQLMesh will insert the model's data into: `db.sqlmesh__sqlmesh_example.sqlmesh_example__full_model__2278521865`. The second and third lines tell us that the backfill action will fully refresh the model's physical table and run the `assert_positive_order_ids` audit.
493+
494+
The final section describes SQLMesh's action during the virtual layer update step. The first model in this section is:
495+
496+
```bash
497+
└── Create or update views in the virtual layer to point at new physical tables and views
498+
├── sqlmesh_example.full_model -> db.sqlmesh__sqlmesh_example.sqlmesh_example__full_model__2278521865
499+
```
500+
501+
The virtual layer step will update the `sqlmesh_example.full_model` virtual layer view to `SELECT * FROM` the physical table `db.sqlmesh__sqlmesh_example.sqlmesh_example__full_model__2278521865`.
502+
503+
Let's take a quick look at the project's DuckDB database file to see the objects SQLMesh created. First, we open the built-in DuckDB CLI tool with the `duckdb db.db` command, then run our two queries:
504+
505+
Our first query shows the three physical tables SQLMesh created in the `sqlmesh__sqlmesh_example` schema (one table for each model):
506+
507+
![Example project physical layer tables in the DuckDB CLI](./cli/cli-quickstart_duckdb-tables.png)
508+
509+
Our second query shows that in the `sqlmesh` schema SQLMesh created three virtual layer views that read from the three physical tables:
510+
511+
![Example project virtual layer views in the DuckDB CLI](./cli/cli-quickstart_duckdb-views.png)
512+
513+
You've now created a new production environment with all of history backfilled!
432514

433515
## 3. Update a model
434516

11.2 KB
Loading
7.08 KB
Loading

0 commit comments

Comments
 (0)