Skip to content

Commit 768aa12

Browse files
authored
Fix: normalize column names in column_descriptions dict (#2731)
1 parent 7c3c20b commit 768aa12

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

sqlmesh/core/model/meta.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ def _columns_validator(
219219
def _column_descriptions_validator(
220220
cls, vs: t.Any, values: t.Dict[str, t.Any]
221221
) -> t.Optional[t.Dict[str, str]]:
222+
dialect = values.get("dialect")
223+
222224
if vs is None:
223225
return None
224226

@@ -228,10 +230,15 @@ def _column_descriptions_validator(
228230
if isinstance(vs, (exp.Tuple, exp.Array)):
229231
vs = vs.expressions
230232

231-
col_descriptions = (
233+
raw_col_descriptions = (
232234
vs if isinstance(vs, dict) else {v.this.name: v.expression.name for v in vs}
233235
)
234236

237+
col_descriptions = {
238+
normalize_identifiers(k, dialect=dialect).name: v
239+
for k, v in raw_col_descriptions.items()
240+
}
241+
235242
columns_to_types = values.get("columns_to_types_")
236243
if columns_to_types:
237244
for column_name in col_descriptions:

tests/core/test_model.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,35 @@ def my_model(context):
17521752
assert not mock_logger.call_args
17531753

17541754

1755+
def test_python_model_decorator_col_descriptions() -> None:
1756+
# `columns` and `column_descriptions` column names are different cases, but name normalization makes both lower
1757+
@model("col_descriptions", columns={"col": "int"}, column_descriptions={"COL": "a column"})
1758+
def a_model(context):
1759+
pass
1760+
1761+
py_model = model.get_registry()["col_descriptions"].model(
1762+
module_path=Path("."),
1763+
path=Path("."),
1764+
)
1765+
1766+
assert py_model.columns_to_types.keys() == py_model.column_descriptions.keys()
1767+
1768+
# error: `columns` and `column_descriptions` column names are different cases, quoting preserves case
1769+
@model(
1770+
"col_descriptions_quoted",
1771+
columns={'"col"': "int"},
1772+
column_descriptions={'"COL"': "a column"},
1773+
)
1774+
def b_model(context):
1775+
pass
1776+
1777+
with pytest.raises(ConfigError, match="a description is provided for column 'COL'"):
1778+
py_model = model.get_registry()["col_descriptions_quoted"].model(
1779+
module_path=Path("."),
1780+
path=Path("."),
1781+
)
1782+
1783+
17551784
def test_star_expansion(assert_exp_eq) -> None:
17561785
context = Context(config=Config())
17571786

0 commit comments

Comments
 (0)