I’ve been building some models using staging/intermediate/marts practices. Let’s assume I have some queries in the marts layer that are connected to the intermediate layer, and those queries in the intermediate layer are referenced to the staging layer.
For Question 1, let’s assume I run these two commands:
- Command 1:
dbt run --vars '{ "prefix": "my_prefix", "filter_value": my_filter_value}'
- Command 2:
dbt run --models marts --vars '{ "prefix": "my_prefix", "filter_value": my_filter_value}'
The main difference is that the second command runs only the models in the marts folder. If those models reference any queries in the intermediate layer, they will use the views that are already in the data warehouse. In other words, if I have modified a query in the intermediate layer that feeds one of the marts queries I’m running, that modification will not be included because I did not include the intermediate layer in Command 2. However, if I run Command 1, the modification will be included as dbt will recreate every view from the staging and intermediate layers, including any modifications I have made. Is my understanding correct?
Question 2
After I run dbt run
, dbt creates the view for (for example) the staging layer and stores the executed query in target/run/<project_name>/models/staging/<my_database>/<my_query.sql>
. When I look at that file, the view is created with this command:
create view "<my_database>"."<my_schema>"."stg_<my_database>__<my_table>__dbt_tmp" as (
SELECT feature_1, feature_2, feature_3
FROM "<my_database>"."<my_schema>"."<my_table>"
) ;
If the view already exists, how is dbt able to overwrite the view if it’s not using the command CREATE OR REPLACE VIEW? If I run this command manually, the error “This view already exists” will arise, right? Why doesn’t this happen with dbt? What is dbt doing under the hood that I’m missing?
Question 3
How, when, or where does dbt get rid of the __dbt_tmp
suffix when creating (for example) the view in Question 2? When I look at the data warehouse, the view name is <my_database>"."<my_schema>"."stg_<my_database>__<my_table>
, not <my_database>"."<my_schema>"."stg_<my_database>__<my_table>__dbt_tmp
. So, when, where, and how does that modification happen?