Skip to content

Commit 4c2f843

Browse files
committed
Fix: Prepopulate the model's depends on in-memory cache for converted dbt models (#3074)
1 parent fcc082b commit 4c2f843

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

sqlmesh/dbt/model.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ def to_sqlmesh(self, context: DbtContext) -> Model:
422422
"target_lag": self.target_lag,
423423
}
424424

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

435441
def _dbt_max_partition_blob(self) -> t.Optional[str]:
436442
"""Returns a SQL blob which declares the _dbt_max_partition variable. Only applicable to BigQuery."""

tests/dbt/test_config.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,3 +789,22 @@ def test_variable_override():
789789
variables={"yet_another_var": 2, "start": "2021-01-01"},
790790
)
791791
assert project.packages["sushi"].variables["yet_another_var"] == 2
792+
793+
794+
def test_depends_on(assert_exp_eq, sushi_test_project):
795+
# Case 1: using an undefined variable without a default value
796+
context = sushi_test_project.context
797+
798+
model_config = ModelConfig(
799+
alias="sushi.test",
800+
sql="SELECT * FROM {{ ref('waiter_revenue_by_day') }} JOIN other_table",
801+
dependencies=Dependencies(refs=["waiter_revenue_by_day"]),
802+
)
803+
804+
sqlmesh_model = model_config.to_sqlmesh(context)
805+
assert sqlmesh_model.depends_on_ == {'"memory"."sushi"."waiter_revenue_by_day_v2"'}
806+
assert sqlmesh_model.depends_on == {'"memory"."sushi"."waiter_revenue_by_day_v2"'}
807+
assert sqlmesh_model.full_depends_on == {'"memory"."sushi"."waiter_revenue_by_day_v2"'}
808+
809+
# Make sure the query wasn't rendered
810+
assert not sqlmesh_model._query_renderer._cache

0 commit comments

Comments
 (0)