Skip to content

Commit

Permalink
Fix: Prepopulate the model's depends on in-memory cache for converted…
Browse files Browse the repository at this point in the history
… dbt models (#3074)
  • Loading branch information
izeigerman committed Aug 30, 2024
1 parent fcc082b commit 4c2f843
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion sqlmesh/dbt/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def to_sqlmesh(self, context: DbtContext) -> Model:
"target_lag": self.target_lag,
}

return create_sql_model(
model = create_sql_model(
self.canonical_name(context),
query,
dialect=model_dialect,
Expand All @@ -431,6 +431,12 @@ def to_sqlmesh(self, context: DbtContext) -> Model:
**optional_kwargs,
**model_kwargs,
)
# Prepopulate the _full_depends_on cache with dependencies sourced directly from the manifest.
# This ensures that we bypass query rendering that would otherwise be required to extract additional
# dependencies from the model's SQL.
# Note: any table dependencies that are not referenced using the `ref` macro will not be included.
model._full_depends_on = model.depends_on_
return model

def _dbt_max_partition_blob(self) -> t.Optional[str]:
"""Returns a SQL blob which declares the _dbt_max_partition variable. Only applicable to BigQuery."""
Expand Down
19 changes: 19 additions & 0 deletions tests/dbt/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,3 +789,22 @@ def test_variable_override():
variables={"yet_another_var": 2, "start": "2021-01-01"},
)
assert project.packages["sushi"].variables["yet_another_var"] == 2


def test_depends_on(assert_exp_eq, sushi_test_project):
# Case 1: using an undefined variable without a default value
context = sushi_test_project.context

model_config = ModelConfig(
alias="sushi.test",
sql="SELECT * FROM {{ ref('waiter_revenue_by_day') }} JOIN other_table",
dependencies=Dependencies(refs=["waiter_revenue_by_day"]),
)

sqlmesh_model = model_config.to_sqlmesh(context)
assert sqlmesh_model.depends_on_ == {'"memory"."sushi"."waiter_revenue_by_day_v2"'}
assert sqlmesh_model.depends_on == {'"memory"."sushi"."waiter_revenue_by_day_v2"'}
assert sqlmesh_model.full_depends_on == {'"memory"."sushi"."waiter_revenue_by_day_v2"'}

# Make sure the query wasn't rendered
assert not sqlmesh_model._query_renderer._cache

0 comments on commit 4c2f843

Please sign in to comment.