Skip to content

Commit 285b8f2

Browse files
authored
fix: parse column from cast expression for ScdType2 models (#5475)
1 parent 683133a commit 285b8f2

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

sqlmesh/dbt/model.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,22 @@ def _validate_check_cols(cls, v: t.Union[str, t.List[str]]) -> t.Union[str, t.Li
172172
return "*"
173173
return ensure_list(v)
174174

175+
@field_validator("updated_at", mode="before")
176+
@classmethod
177+
def _validate_updated_at(cls, v: t.Optional[str]) -> t.Optional[str]:
178+
"""
179+
Extract column name if updated_at contains a cast.
180+
181+
SCDType2ByTimeKind and SCDType2ByColumnKind expect a column, and the casting is done later.
182+
"""
183+
if v is None:
184+
return None
185+
parsed = d.parse_one(v)
186+
if isinstance(parsed, exp.Cast) and isinstance(parsed.this, exp.Column):
187+
return parsed.this.name
188+
189+
return v
190+
175191
@field_validator("sql", mode="before")
176192
@classmethod
177193
def _validate_sql(cls, v: t.Union[str, SqlStr]) -> SqlStr:

tests/dbt/test_transformation.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,23 @@ def test_model_kind():
652652
== ManagedKind()
653653
)
654654

655+
assert ModelConfig(
656+
materialized=Materialization.SNAPSHOT,
657+
unique_key=["id"],
658+
updated_at="updated_at::timestamp",
659+
strategy="timestamp",
660+
dialect="redshift",
661+
).model_kind(context) == SCDType2ByTimeKind(
662+
unique_key=["id"],
663+
valid_from_name="dbt_valid_from",
664+
valid_to_name="dbt_valid_to",
665+
updated_at_as_valid_from=True,
666+
updated_at_name="updated_at",
667+
dialect="redshift",
668+
on_destructive_change=OnDestructiveChange.IGNORE,
669+
on_additive_change=OnAdditiveChange.ALLOW,
670+
)
671+
655672

656673
def test_model_kind_snapshot_bigquery():
657674
context = DbtContext()

0 commit comments

Comments
 (0)