Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions sqlmesh/dbt/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,22 @@ def _validate_check_cols(cls, v: t.Union[str, t.List[str]]) -> t.Union[str, t.Li
return "*"
return ensure_list(v)

@field_validator("updated_at", mode="before")
@classmethod
def _validate_updated_at(cls, v: t.Optional[str]) -> t.Optional[str]:
"""
Extract column name if updated_at contains a cast.

SCDType2ByTimeKind and SCDType2ByColumnKind expect a column, and the casting is done later.
"""
if v is None:
return None
parsed = d.parse_one(v)
if isinstance(parsed, exp.Cast) and isinstance(parsed.this, exp.Column):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment explaining that both scd type 2 expects a column and the logic for it does a cast anyways so this shouldn't be needed? Could be an issue later so it would at least explaining the reasoning at this time.

Copy link
Contributor Author

@toriwei toriwei Oct 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated with:


        """
        Extract column name if updated_at contains a cast.

        SCDType2ByTimeKind and SCDType2ByColumnKind expect a column, and the casting is done later.
        """

return parsed.this.name

return v

@field_validator("sql", mode="before")
@classmethod
def _validate_sql(cls, v: t.Union[str, SqlStr]) -> SqlStr:
Expand Down
17 changes: 17 additions & 0 deletions tests/dbt/test_transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,23 @@ def test_model_kind():
== ManagedKind()
)

assert ModelConfig(
materialized=Materialization.SNAPSHOT,
unique_key=["id"],
updated_at="updated_at::timestamp",
strategy="timestamp",
dialect="redshift",
).model_kind(context) == SCDType2ByTimeKind(
unique_key=["id"],
valid_from_name="dbt_valid_from",
valid_to_name="dbt_valid_to",
updated_at_as_valid_from=True,
updated_at_name="updated_at",
dialect="redshift",
on_destructive_change=OnDestructiveChange.IGNORE,
on_additive_change=OnAdditiveChange.ALLOW,
)


def test_model_kind_snapshot_bigquery():
context = DbtContext()
Expand Down