Repository: TobikoData/sqlmesh (python)

Issue:
Sometimes sqlmesh format and the ctrl + s button in the UI makes the code less readable (IMO) for mssql
The following rules from sqlmesh format reduces readability IMO:
* Adding "as column_name" after every column name
* No spaces between CTE's
* The adding of coalesce(varchar(max)) when casting: https://github.com/TobikoData/sqlmesh/issues/1815

I can see there's another "format/translation" layer, that encapsulates all schema, table and column names in "" (it probably does more), before the code ends up in the database.
Maybe some of the above rules can be added to that last layer to avoid messing with readability of the model files?

Here's an example showing some code becoming less readable:

```
MODEL (
  name new_schema.new_table_name,
  kind VIEW
);

WITH first_cte AS (
  SELECT
    foo,
    bar,
    baz,
    ham,
    a_very_long_column_name
  FROM source_schema.source_table1
),

WITH second_cte AS (
  SELECT
    foo_foo_foo,
    bar_bar_bar,
    an_even_longer_column_name
  FROM source_schema.source_table2
),

concat_me AS (
  SELECT
    second_cte.foo_foo_foo,
    second_cte.bar_bar_bar,
    concat(first_cte.a_very_long_column_name, second_cte.an_even_longer_column_name, 'post_fix') as something_concat
  FROM first_cte
  LEFT JOIN second_cte
    ON first_cte.foo = second_cte.foo_foo_foo
    AND first_cte.bar = second_cte.bar_bar_bar
),

something_to_union AS (
  SELECT
    foo_foo_foo,
    bar_bar_bar,
    something_concat
  FROM another_schema.another_table
),

unionized AS (
  SELECT * FROM concat_me
  UNION ALL
  SELECT * FROM something_to_union
)

SELECT
  *
FROM unionized
```


Becomes:
```
MODEL (
  name new_schema.new_table_name,
  kind VIEW
);

WITH first_cte AS (
  SELECT
    foo AS foo,
    bar AS bar,
    baz AS baz,
    ham AS ham,
    a_very_long_column_name AS a_very_long_column_name
  FROM source_schema.source_table1
), second_cte AS (
  SELECT
    foo_foo_foo AS foo_foo_foo,
    bar_bar_bar AS bar_bar_bar,
    an_even_longer_column_name AS an_even_longer_column_name
  FROM source_schema.source_table2
), concat_me AS (
  SELECT
    second_cte.foo_foo_foo AS foo_foo_foo,
    second_cte.bar_bar_bar AS bar_bar_bar,
    CONCAT(
      COALESCE(CAST(first_cte.a_very_long_column_name AS VARCHAR(MAX)), ''),
      COALESCE(CAST(second_cte.an_even_longer_column_name AS VARCHAR(MAX)), ''),
      COALESCE(CAST('post_fix' AS VARCHAR(MAX)), '')
    ) AS something_concat
  FROM first_cte
  LEFT JOIN second_cte
    ON first_cte.foo = second_cte.foo_foo_foo AND first_cte.bar = second_cte.bar_bar_bar
), something_to_union AS (
  SELECT
    foo_foo_foo AS foo_foo_foo,
    bar_bar_bar AS bar_bar_bar,
    something_concat AS something_concat
  FROM another_schema.another_table
), unionized AS (
  SELECT
    *
  FROM concat_me
  UNION ALL
  SELECT
    *
  FROM something_to_union
)
SELECT
  *
FROM unionized
```

Pull request:
feat: less aggressive formatting closes #1816

Patch:
diff --git a/sqlmesh/core/dialect.py b/sqlmesh/core/dialect.py
index 599a6f8d..931ba069 100644
--- a/sqlmesh/core/dialect.py
+++ b/sqlmesh/core/dialect.py
@@ -486,29 +486,21 @@ def format_model_expressions(
         return expressions[0].sql(pretty=True, dialect=dialect)
 
     *statements, query = expressions
-    query = query.copy()
-    selects = []
-
-    for expression in query.expressions:
-        column = None
-        comments = expression.comments
-        expression.comments = None
-
-        if not isinstance(expression, exp.Alias) and expression.output_name not in ("", "*"):
-            expression = expression.replace(exp.alias_(expression, expression.output_name))
 
-        column = column or expression
-        expression = expression.this
+    def cast_to_colon(node: exp.Expression) -> exp.Expression:
+        if isinstance(node, exp.Cast) and not node.args.get("format"):
+            this = node.this
 
-        if isinstance(expression, exp.Cast):
-            this = expression.this
             if not isinstance(this, (exp.Binary, exp.Unary)) or isinstance(this, exp.Paren):
-                expression.replace(DColonCast(this=this, to=expression.to))
+                cast = DColonCast(this=this, to=node.to)
+                cast.comments = node.comments
+                node = cast
 
-        column.comments = comments
-        selects.append(column)
+        exp.replace_children(node, cast_to_colon)
+        return node
 
-    query.set("expressions", selects)
+    query = query.copy()
+    exp.replace_children(query, cast_to_colon)
 
     return ";\n\n".join(
         [


Test patch:
diff --git a/tests/core/test_dialect.py b/tests/core/test_dialect.py
index d8a76a88..acbc79d7 100644
--- a/tests/core/test_dialect.py
+++ b/tests/core/test_dialect.py
@@ -46,6 +46,7 @@ def test_format_model_expressions():
     sum(n + 1)::int as n, -- n
     o,
     p + 1,
+    CAST(x as int)::int,
     """
         )
     )
@@ -61,26 +62,27 @@ def test_format_model_expressions():
 
 SELECT
   *,
-  a::INT AS a,
+  a::INT,
   b::INT AS b,
   CAST(c + 1 AS INT) AS c,
-  d::INT AS d,
+  d::INT,
   e::INT AS e,
   (
     f + 1
   )::INT AS f,
   SUM(g + 1)::INT AS g,
-  h::INT AS h, /* h */
+  h::INT, /* h */
   i::INT AS i, /* i */
   CAST(j + 1 AS INT) AS j, /* j */
-  k::INT AS k, /* k */
+  k::INT, /* k */
   l::INT AS l, /* l */
   (
     m + 1
   )::INT AS m, /* m */
   SUM(n + 1)::INT AS n, /* n */
-  o AS o,
-  p + 1"""
+  o,
+  p + 1,
+  x::INT::INT"""
     )
 
     x = format_model_expressions(
@@ -130,7 +132,7 @@ def test_format_body_macros():
     1
 )
 SELECT
-  col::INT AS col
+  col::INT
 FROM foo
 @ORDER_BY(@include_order_by)
   @EACH(@columns, item -> @'@iteaoeuatnoehutoenahuoanteuhonateuhaoenthuaoentuhaeotnhaoem'),