Skip to content

Commit 604c7c6

Browse files
authored
Fix: allow parsing functions as property values in model meta (#1699)
* Fix: allow parsing functions as property values in model meta * Simplify
1 parent 923ef9a commit 604c7c6

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

docs/concepts/models/model_kinds.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ The `unique_key` values can either be column names or SQL expressions. For examp
164164
MODEL (
165165
name db.employees,
166166
kind INCREMENTAL_BY_UNIQUE_KEY (
167-
unique_key (COALESCE("ds", ''))
167+
unique_key COALESCE("ds", '')
168168
)
169169
);
170170
```

sqlmesh/core/dialect.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,6 @@ def _parse_order(
281281
return macro
282282

283283

284-
def _parse_prop_value(self: Parser) -> t.Optional[exp.Expression]:
285-
this = self._parse_string() or self._parse_id_var()
286-
if self._match(TokenType.EQ):
287-
this = exp.EQ(this=this, expression=self._parse_string() or self._parse_id_var())
288-
289-
return this
290-
291-
292284
def _parse_props(self: Parser) -> t.Optional[exp.Expression]:
293285
key = self._parse_id_var(any_token=True)
294286
if not key:
@@ -298,9 +290,7 @@ def _parse_props(self: Parser) -> t.Optional[exp.Expression]:
298290
if name == "when_matched":
299291
value: t.Optional[exp.Expression] = self._parse_when_matched()[0]
300292
elif self._match(TokenType.L_PAREN):
301-
value = self.expression(
302-
exp.Tuple, expressions=self._parse_csv(lambda: _parse_prop_value(self))
303-
)
293+
value = self.expression(exp.Tuple, expressions=self._parse_csv(self._parse_equality))
304294
self._match_r_paren()
305295
else:
306296
value = self._parse_bracket(self._parse_field(any_token=True))

tests/core/test_model.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2311,6 +2311,24 @@ def test_scd_type_2_defaults():
23112311
assert scd_type_2_model.kind.forward_only
23122312
assert scd_type_2_model.kind.disable_restatement
23132313

2314+
# Checks we can parse coalesced key with or without parentheses
2315+
for coalesced_id in ("""COALESCE("id", '')""", """(COALESCE("id", ''))"""):
2316+
model = load_sql_based_model(
2317+
d.parse(
2318+
f"""
2319+
MODEL (
2320+
name db.table,
2321+
kind SCD_TYPE_2 (
2322+
unique_key {coalesced_id},
2323+
),
2324+
);
2325+
2326+
SELECT 1 AS "id"
2327+
"""
2328+
)
2329+
)
2330+
assert model.unique_key == [exp.func("COALESCE", exp.column("id", quoted=True), "''")]
2331+
23142332

23152333
def test_scd_type_2_overrides():
23162334
view_model_expressions = d.parse(

0 commit comments

Comments
 (0)