Skip to content

Commit 02c0c69

Browse files
fix(model): keep quoting when normalizing column_descriptions keys
_column_descriptions_validator built each key with part.this, which is the bare identifier string, so the quoted flag was gone before normalize_identifiers ran. A quoted key was then normalized as if it were unquoted, and on dialects where quoting makes a column case-sensitive the resulting name matched no column, so the description was dropped along with the rest of the table's comments. Normalize each part while it is still an identifier. Unquoted keys normalize exactly as before. Fixes #5943 Signed-off-by: ReguiguiMohamed <mohamedreguigui2004@gmail.com>
1 parent 839c5b2 commit 02c0c69

2 files changed

Lines changed: 36 additions & 8 deletions

File tree

‎sqlmesh/core/model/meta.py‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -321,17 +321,19 @@ def _column_descriptions_validator(
321321
if isinstance(vs, (exp.Tuple, exp.Array)):
322322
vs = vs.expressions
323323

324-
raw_col_descriptions = (
325-
vs
324+
# Normalize each part while it is still an identifier, so that a quoted
325+
# column keeps its case on dialects where quoting makes it significant.
326+
col_descriptions = (
327+
{normalize_identifiers(k, dialect=dialect).name: v for k, v in vs.items()}
326328
if isinstance(vs, dict)
327-
else {".".join([part.this for part in v.this.parts]): v.expression.name for v in vs}
329+
else {
330+
".".join(
331+
normalize_identifiers(part, dialect=dialect).name for part in v.this.parts
332+
): v.expression.name
333+
for v in vs
334+
}
328335
)
329336

330-
col_descriptions = {
331-
normalize_identifiers(k, dialect=dialect).name: v
332-
for k, v in raw_col_descriptions.items()
333-
}
334-
335337
columns_to_types = data.get("columns_to_types_")
336338
if columns_to_types:
337339
from sqlmesh.core.console import get_console

‎tests/core/test_model.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,32 @@ def test_column_descriptions(sushi_context, assert_exp_eq):
10001000
assert model.column_descriptions == {"id": "primary key", "foo": "bar"}
10011001

10021002

1003+
def test_column_descriptions_quoted_identifier():
1004+
expressions = d.parse(
1005+
"""
1006+
MODEL (
1007+
name db.table,
1008+
kind FULL,
1009+
dialect snowflake,
1010+
column_descriptions (
1011+
"myColumn" = 'a case-sensitive column',
1012+
other_column = 'an unquoted column'
1013+
)
1014+
);
1015+
1016+
SELECT 1 AS "myColumn", 2 AS other_column
1017+
"""
1018+
)
1019+
model = load_sql_based_model(expressions, dialect="snowflake")
1020+
1021+
# A quoted key keeps its case, an unquoted one is still normalized.
1022+
assert model.column_descriptions == {
1023+
"myColumn": "a case-sensitive column",
1024+
"OTHER_COLUMN": "an unquoted column",
1025+
}
1026+
assert set(model.column_descriptions) <= set(model.columns_to_types)
1027+
1028+
10031029
def test_model_jinja_macro_reference_extraction():
10041030
@macro()
10051031
def test_macro(**kwargs) -> None:

0 commit comments

Comments
 (0)