Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion sqlmesh/core/model/seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pandas as pd
from sqlglot import exp
from sqlglot.dialects.dialect import UNESCAPED_SEQUENCES
from sqlglot.optimizer.normalize_identifiers import normalize_identifiers

from sqlmesh.core.model.common import parse_bool
Expand Down Expand Up @@ -39,7 +40,11 @@ def _bool_validator(cls, v: t.Any) -> t.Optional[bool]:
def _str_validator(cls, v: t.Any) -> t.Optional[str]:
if v is None or not isinstance(v, exp.Expression):
return v
return v.this

# SQLGlot parses escape sequences like \t as \\t for dialects that don't treat \ as
# an escape character, so we map them back to the corresponding escaped sequence
v = v.this
return UNESCAPED_SEQUENCES.get(v, v)


class CsvSeedReader:
Expand Down
23 changes: 19 additions & 4 deletions tests/core/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,14 +895,29 @@ def test_seed_with_special_characters_in_column(tmp_path, assert_exp_eq):

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

model = create_seed_model("memory.test_db.test_model", SeedKind(path=str(model_csv_path)))
context.upsert_model(model)
expressions = d.parse(
f"""
MODEL (
name memory.test_db.test_model,
kind SEED (
path '{model_csv_path}',
csv_settings (
delimiter = '\\t'
)
),
);
"""
)

context.upsert_model(load_sql_based_model(expressions))
assert_exp_eq(
context.render("memory.test_db.test_model").sql(),
'SELECT CAST("col." AS BIGINT) AS "col." FROM (VALUES (123)) AS t("col.")',
"SELECT "
'CAST("col." AS BIGINT) AS "col.", '
'CAST("col!@#$" AS TEXT) AS "col!@#$" '
"""FROM (VALUES (123, 'foo')) AS t("col.", "col!@#$")""",
)


Expand Down