How does dbt actually compile queries?

Use Blip to create an article or summary from any YouTube video.

In the latest dbt video, we explored how to create a source and use it to build a new model. In this article, we'll delve deeper into the underlying SQL that dbt generates and how to access it for debugging purposes.

When you're working with dbt, it's not always immediately clear what's happening behind the scenes. While the model code you write in dbt is easy to read and understand, it's not always clear what SQL code dbt is actually running. Fortunately, dbt compiles your model code into SQL and stores it separately, making it easy to access and debug.

To find the compiled SQL code, navigate to the "target" folder in your dbt project. This folder contains the compiled SQL code for each of your models. When you first initialize the project, this folder will be empty, but it will populate with SQL code as you start running models.

Once you've located the compiled SQL code, you can open it up and see exactly what dbt is running. The code is broken down into individual SQL statements, making it easy to read and understand. For example, let's say you have a model with several CTEs and custom logic written in Jinja functions. You can find the compiled SQL code for this model and see exactly what dbt is running.

This is especially helpful when it comes to debugging. As your dbt project becomes more complex, you may start using sources and references to other models. When you use the "ref" function in dbt, it refers to another model by name. By looking at the compiled SQL code, you can see exactly which model dbt is referring to and how it's using it.

Overall, dbt does a great job of compiling your models and making it easy to debug. By using the different yaml files and templates, dbt puts together your model code and generates SQL code that you can actually run in Snowflake. So the next time you're working with dbt and want to see what's happening under the hood, be sure to check out the compiled SQL code!