nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: TobikoData/sqlmesh
Pull request #3463 (2024-12-04 11:13:05)

Fix: apply cast-to-dcolon rule for every model statement
Context: https://tobiko-data.slack.com/archives/C044BRE5W4S/p1733307762741739


Patch:
diff --git a/sqlmesh/core/dialect.py b/sqlmesh/core/dialect.py
index e97c5c95..1639fd88 100644
--- a/sqlmesh/core/dialect.py
+++ b/sqlmesh/core/dialect.py
@@ -674,8 +674,6 @@ def format_model_expressions(
     if len(expressions) == 1 and is_meta_expression(expressions[0]):
         return expressions[0].sql(pretty=True, dialect=dialect)
 
-    *statements, query = expressions
-
     if rewrite_casts:
 
         def cast_to_colon(node: exp.Expression) -> exp.Expression:
@@ -696,14 +694,16 @@ def format_model_expressions(
             exp.replace_children(node, cast_to_colon)
             return node
 
-        query = query.copy()
-        exp.replace_children(query, cast_to_colon)
+        new_expressions = []
+        for expression in expressions:
+            expression = expression.copy()
+            exp.replace_children(expression, cast_to_colon)
+            new_expressions.append(expression)
+
+        expressions = new_expressions
 
     return ";\n\n".join(
-        [
-            *(statement.sql(pretty=True, dialect=dialect, **kwargs) for statement in statements),
-            query.sql(pretty=True, dialect=dialect, **kwargs),
-        ]
+        expression.sql(pretty=True, dialect=dialect, **kwargs) for expression in expressions
     ).strip()
 
 


Test patch:
diff --git a/tests/core/test_dialect.py b/tests/core/test_dialect.py
index 195c5b6a..1d6ca0fe 100644
--- a/tests/core/test_dialect.py
+++ b/tests/core/test_dialect.py
@@ -76,7 +76,12 @@ def test_format_model_expressions():
     sum(n + 1)::int as n, -- n
     o,
     p + 1,
-    CAST(x as int)::int,
+    CAST(x as int)::int,;
+
+@IF(
+    @runtime_stage = 'creating',
+    GRANT SELECT ON foo.bar TO "bla"
+)
     """
         )
     )
@@ -151,7 +156,9 @@ SELECT
   SUM(n + 1)::INT AS n, /* n */
   o,
   p + 1,
-  x::INT::INT"""
+  x::INT::INT;
+
+@IF(@runtime_stage = 'creating', GRANT SELECT ON foo.bar TO "bla")"""
     )
 
     x = format_model_expressions(