Skip to content

Commit da69c83

Browse files
committed
refactor impl
1 parent d1840e7 commit da69c83

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

sqlmesh/dbt/model.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,14 @@ class ModelConfig(BaseModelConfig):
157157

158158
@field_validator(
159159
"unique_key",
160+
"cluster_by",
160161
"tags",
161162
mode="before",
162163
)
163164
@classmethod
164165
def _validate_list(cls, v: t.Union[str, t.List[str]]) -> t.List[str]:
165166
return ensure_list(v)
166167

167-
@field_validator("cluster_by", mode="before")
168-
@classmethod
169-
def _validate_cluster_by(cls, v: t.Union[str, t.List[str]]) -> t.Union[str, t.List[str]]:
170-
if isinstance(v, str):
171-
return [c.strip() for c in v.split(",")]
172-
return ensure_list(v)
173-
174168
@field_validator("check_cols", mode="before")
175169
@classmethod
176170
def _validate_check_cols(cls, v: t.Union[str, t.List[str]]) -> t.Union[str, t.List[str]]:
@@ -607,7 +601,13 @@ def to_sqlmesh(
607601
clustered_by = []
608602
for c in self.cluster_by:
609603
try:
610-
clustered_by.append(d.parse_one(c, dialect=model_dialect))
604+
cluster_expr = exp.maybe_parse(
605+
c, into=exp.Cluster, prefix="CLUSTER BY", dialect=model_dialect
606+
)
607+
for expr in cluster_expr.expressions:
608+
clustered_by.append(
609+
expr.this if isinstance(expr, exp.Ordered) else expr
610+
)
611611
except SqlglotError as e:
612612
raise ConfigError(
613613
f"Failed to parse model '{self.canonical_name(context)}' cluster_by field '{c}' in '{self.path}': {e}"

tests/dbt/test_transformation.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,6 +2320,46 @@ def test_model_cluster_by():
23202320
exp.to_column('"QUX"'),
23212321
]
23222322

2323+
model = ModelConfig(
2324+
name="model",
2325+
alias="model",
2326+
package_name="package",
2327+
target_schema="test",
2328+
cluster_by=['"Bar,qux"'],
2329+
sql="SELECT * FROM baz",
2330+
materialized=Materialization.TABLE.value,
2331+
)
2332+
assert model.to_sqlmesh(context).clustered_by == [
2333+
exp.to_column('"Bar,qux"'),
2334+
]
2335+
2336+
model = ModelConfig(
2337+
name="model",
2338+
alias="model",
2339+
package_name="package",
2340+
target_schema="test",
2341+
cluster_by='"Bar,qux"',
2342+
sql="SELECT * FROM baz",
2343+
materialized=Materialization.TABLE.value,
2344+
)
2345+
assert model.to_sqlmesh(context).clustered_by == [
2346+
exp.to_column('"Bar,qux"'),
2347+
]
2348+
2349+
model = ModelConfig(
2350+
name="model",
2351+
alias="model",
2352+
package_name="package",
2353+
target_schema="test",
2354+
cluster_by=["to_date(Bar),qux"],
2355+
sql="SELECT * FROM baz",
2356+
materialized=Materialization.TABLE.value,
2357+
)
2358+
assert model.to_sqlmesh(context).clustered_by == [
2359+
exp.TsOrDsToDate(this=exp.to_column('"BAR"')),
2360+
exp.to_column('"QUX"'),
2361+
]
2362+
23232363

23242364
def test_snowflake_dynamic_table():
23252365
context = DbtContext()

0 commit comments

Comments
 (0)