Repository: TobikoData/sqlmesh
Pull request #3382 (2024-11-16 04:21:54)

Fix: normalize table identifier in star macro
The star macro mismatches the case of the table alias when used with snowflake dialect.
The table alias will be uppercase while the alias used with columns remains lowercase.
Since the identifiers are all quoted this causes a problem for snowflake.

Solution is to apply normalization function to the table_identifier.

Related to https://github.com/TobikoData/sqlmesh/issues/3381

mpcarter: Finally got the styles to run, and checks passed. 

Patch:
diff --git a/sqlmesh/core/macros.py b/sqlmesh/core/macros.py
index 3912d9f5..63f0316e 100644
--- a/sqlmesh/core/macros.py
+++ b/sqlmesh/core/macros.py
@@ -882,7 +882,9 @@ def star(
         for excluded in exclude.expressions or except_.expressions
     }
     quoted = quote_identifiers.this
-    table_identifier = alias.name or relation.name
+    table_identifier = normalize_identifiers(
+        alias if alias.name else relation, dialect=evaluator.dialect
+    ).name
 
     columns_to_types = {
         k: v for k, v in evaluator.columns_to_types(relation).items() if k not in excluded_names


Test patch:
diff --git a/tests/core/test_macros.py b/tests/core/test_macros.py
index fdd85121..e185e205 100644
--- a/tests/core/test_macros.py
+++ b/tests/core/test_macros.py
@@ -146,6 +146,58 @@ def test_star(assert_exp_eq) -> None:
     evaluator = MacroEvaluator(schema=schema, dialect="tsql")
     assert_exp_eq(evaluator.transform(parse_one(sql, read="tsql")), expected_sql, dialect="tsql")
 
+    sql = """SELECT @STAR(foo) FROM foo"""
+    expected_sql = (
+        """SELECT CAST("FOO"."A" AS DATE) AS "A", CAST("FOO"."B" AS INTEGER) AS "B" FROM foo"""
+    )
+    schema = MappingSchema(
+        {
+            "foo": {
+                "a": exp.DataType.build("date", dialect="snowflake"),
+                "b": "int",
+            },
+        },
+        dialect="snowflake",
+    )
+    evaluator = MacroEvaluator(schema=schema, dialect="snowflake")
+    assert_exp_eq(
+        evaluator.transform(parse_one(sql, read="snowflake")), expected_sql, dialect="snowflake"
+    )
+
+    sql = """SELECT @STAR("foo") FROM "foo" """
+    expected_sql = (
+        """SELECT CAST("foo"."A" AS DATE) AS "A", CAST("foo"."B" AS INTEGER) AS "B" FROM "foo" """
+    )
+    schema = MappingSchema(
+        {
+            '"foo"': {
+                "a": exp.DataType.build("date", dialect="snowflake"),
+                "b": "int",
+            },
+        },
+        dialect="snowflake",
+    )
+    evaluator = MacroEvaluator(schema=schema, dialect="snowflake")
+    assert_exp_eq(
+        evaluator.transform(parse_one(sql, read="snowflake")), expected_sql, dialect="snowflake"
+    )
+
+    sql = """SELECT @STAR(foo, alias := "bar") FROM foo "bar" """
+    expected_sql = """SELECT CAST("bar"."A" AS DATE) AS "A", CAST("bar"."B" AS INTEGER) AS "B" FROM foo "bar" """
+    schema = MappingSchema(
+        {
+            "foo": {
+                "a": exp.DataType.build("date", dialect="snowflake"),
+                "b": "int",
+            },
+        },
+        dialect="snowflake",
+    )
+    evaluator = MacroEvaluator(schema=schema, dialect="snowflake")
+    assert_exp_eq(
+        evaluator.transform(parse_one(sql, read="snowflake")), expected_sql, dialect="snowflake"
+    )
+
 
 def test_start_no_column_types(assert_exp_eq) -> None:
     sql = """SELECT @STAR(foo) FROM foo"""