Skip to content

Commit

Permalink
Fix: quote the table produced by _resolve_table
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas committed Dec 10, 2024
1 parent 3af1f17 commit d2f2560
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 6 additions & 1 deletion sqlmesh/core/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def _resolve_table(
table_mapping: t.Optional[t.Dict[str, str]] = None,
deployability_index: t.Optional[DeployabilityIndex] = None,
) -> exp.Table:
return exp.replace_tables(
table = exp.replace_tables(
exp.maybe_parse(table_name, into=exp.Table, dialect=self._dialect),
{
**self._to_table_mapping((snapshots or {}).values(), deployability_index),
Expand All @@ -249,6 +249,11 @@ def _resolve_table(
dialect=self._dialect,
copy=False,
)
# We quote the table here to mimic the behavior of _resolve_tables, otherwise we may end
# up normalizing twice, because _to_table_mapping returns the mapped names unquoted.
return (
d.quote_identifiers(table, dialect=self._dialect) if self._quote_identifiers else table
)

def _resolve_tables(
self,
Expand Down
11 changes: 10 additions & 1 deletion tests/core/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,16 +691,25 @@ def multiple_statements(evaluator, t1_value=exp.Literal.number(1)):
assert model.render_post_statements() == expected_post
assert "exp" in model.python_env

@macro()
def this_model_is_quoted_table(evaluator):
this_model = evaluator.locals.get("this_model")
return not this_model or (
isinstance(this_model, exp.Table)
and this_model.sql(dialect=evaluator.dialect, comments=False) == '"db"."table"'
)

expressions = d.parse(
"""
MODEL (name db.table);
SELECT 1 AS col;
SELECT 1 AS col, @this_model_is_quoted_table() AS this_model_is_quoted_table;
CREATE TABLE db.other AS SELECT * FROM @this_model;
"""
)
model = load_sql_based_model(expressions)
assert str(model.render_query()) == 'SELECT 1 AS "col", TRUE AS "this_model_is_quoted_table"'

expected_post = d.parse('CREATE TABLE "db"."other" AS SELECT * FROM "db"."table" AS "table";')
assert model.render_post_statements() == expected_post
Expand Down

0 comments on commit d2f2560

Please sign in to comment.