Skip to content

Commit 500be00

Browse files
authored
Fix: unescape sequences in CSV settings (#2690)
1 parent ec840bf commit 500be00

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

sqlmesh/core/model/seed.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import pandas as pd
99
from sqlglot import exp
10+
from sqlglot.dialects.dialect import UNESCAPED_SEQUENCES
1011
from sqlglot.optimizer.normalize_identifiers import normalize_identifiers
1112

1213
from sqlmesh.core.model.common import parse_bool
@@ -39,7 +40,11 @@ def _bool_validator(cls, v: t.Any) -> t.Optional[bool]:
3940
def _str_validator(cls, v: t.Any) -> t.Optional[str]:
4041
if v is None or not isinstance(v, exp.Expression):
4142
return v
42-
return v.this
43+
44+
# SQLGlot parses escape sequences like \t as \\t for dialects that don't treat \ as
45+
# an escape character, so we map them back to the corresponding escaped sequence
46+
v = v.this
47+
return UNESCAPED_SEQUENCES.get(v, v)
4348

4449

4550
class CsvSeedReader:

tests/core/test_model.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -895,14 +895,29 @@ def test_seed_with_special_characters_in_column(tmp_path, assert_exp_eq):
895895

896896
model_csv_path = (tmp_path / "model.csv").absolute()
897897
with open(model_csv_path, "w", encoding="utf-8") as fd:
898-
fd.write("col.\n123")
898+
fd.write("col.\tcol!@#$\n123\tfoo")
899899

900-
model = create_seed_model("memory.test_db.test_model", SeedKind(path=str(model_csv_path)))
901-
context.upsert_model(model)
900+
expressions = d.parse(
901+
f"""
902+
MODEL (
903+
name memory.test_db.test_model,
904+
kind SEED (
905+
path '{model_csv_path}',
906+
csv_settings (
907+
delimiter = '\\t'
908+
)
909+
),
910+
);
911+
"""
912+
)
902913

914+
context.upsert_model(load_sql_based_model(expressions))
903915
assert_exp_eq(
904916
context.render("memory.test_db.test_model").sql(),
905-
'SELECT CAST("col." AS BIGINT) AS "col." FROM (VALUES (123)) AS t("col.")',
917+
"SELECT "
918+
'CAST("col." AS BIGINT) AS "col.", '
919+
'CAST("col!@#$" AS TEXT) AS "col!@#$" '
920+
"""FROM (VALUES (123, 'foo')) AS t("col.", "col!@#$")""",
906921
)
907922

908923

0 commit comments

Comments
 (0)