diff --git a/sqlmesh/core/macros.py b/sqlmesh/core/macros.py index 1f8908af4e..234290cdde 100644 --- a/sqlmesh/core/macros.py +++ b/sqlmesh/core/macros.py @@ -240,8 +240,7 @@ def evaluate_macros( text = self.template(node.this, {}) if node.this != text: changed = True - node.args["this"] = text - return node + return exp.to_identifier(text, quoted=node.quoted or None) if node.is_string: text = node.this if has_jinja(text): @@ -1389,6 +1388,10 @@ def _convert_sql(v: t.Any, dialect: DialectType) -> t.Any: pass if isinstance(v, exp.Expression): + if (isinstance(v, exp.Column) and not v.table) or ( + isinstance(v, exp.Identifier) or v.is_string + ): + return v.name v = v.sql(dialect=dialect) return v diff --git a/tests/core/test_macros.py b/tests/core/test_macros.py index e05b51e6e1..c295e4865b 100644 --- a/tests/core/test_macros.py +++ b/tests/core/test_macros.py @@ -1082,3 +1082,24 @@ def test_resolve_template_table(): evaluator.transform(parsed_sql).sql(identify=True) == 'SELECT * FROM "test_catalog"."sqlmesh__test"."test__test_model__2517971505$partitions"' ) + + +def test_macro_with_spaces(): + evaluator = MacroEvaluator() + evaluator.evaluate(d.parse_one(""" @DEF(x, "a b") """)) + evaluator.evaluate(d.parse_one(""" @DEF(y, 'a b') """)) + evaluator.evaluate(d.parse_one(""" @DEF(z, a."b c") """)) + + for sql, expected in ( + ("@x", '"a b"'), + ("@{x}", '"a b"'), + ("a_@x", '"a_a b"'), + ("a.@x", 'a."a b"'), + ("@y", "'a b'"), + ("@{y}", '"a b"'), # a little tricky here as it's not a string + ("a_@y", '"a_a b"'), + ("a.@{y}", 'a."a b"'), + ("@z", 'a."b c"'), + ("d.@z", 'd.a."b c"'), + ): + assert evaluator.transform(parse_one(sql)).sql() == expected