Repository: tobymao/sqlglot

Issue:
sqlglot removes comments
```python
import sqlglot as sg

sql = """/* The result of  some calculations
 */
with
    base as (
        select 
            sum(sb.hep_amount) as hep_amount,
            -- I AM REMOVED            
            sum(sb.hep_budget) 
            /* Budget defined in sharepoint */
            as blub
            , 1 as bla
        from gold.data_budget sb
        group by all
    )
select
    *
from base
"""
result = sg.transpile(sql, read="databricks", write="databricks", pretty=True)[0]
print(result)

assert "I AM REMOVED" in result

```
The "I AM REMOVED" comment is removed if it's not on the same line as the column. The same is true for the other comments as well. That's a bit sad since I like the "pretty" feature a lot :)




Discussion:

Resolving patch:
diff --git a/sqlglot/parser.py b/sqlglot/parser.py
--- a/sqlglot/parser.py
+++ b/sqlglot/parser.py
@@ -3759,6 +3759,7 @@ def _parse_function_call(
         if not self._curr:
             return None
 
+        comments = self._curr.comments
         token_type = self._curr.token_type
         this = self._curr.text
         upper = this.upper()
@@ -3807,6 +3808,9 @@ def _parse_function_call(
             else:
                 this = self.expression(exp.Anonymous, this=this, expressions=args)
 
+        if isinstance(this, exp.Expression):
+            this.add_comments(comments)
+
         self._match_r_paren(this)
         return self._parse_window(this)
 
@@ -4823,6 +4827,7 @@ def _parse_alias(
         self, this: t.Optional[exp.Expression], explicit: bool = False
     ) -> t.Optional[exp.Expression]:
         any_token = self._match(TokenType.ALIAS)
+        comments = self._prev_comments
 
         if explicit and not any_token:
             return this
@@ -4830,6 +4835,7 @@ def _parse_alias(
         if self._match(TokenType.L_PAREN):
             aliases = self.expression(
                 exp.Aliases,
+                comments=comments,
                 this=this,
                 expressions=self._parse_csv(lambda: self._parse_id_var(any_token)),
             )
@@ -4839,7 +4845,7 @@ def _parse_alias(
         alias = self._parse_id_var(any_token)
 
         if alias:
-            return self.expression(exp.Alias, this=this, alias=alias)
+            return self.expression(exp.Alias, comments=comments, this=this, alias=alias)
 
         return this
 


Test patch:
diff --git a/tests/test_transpile.py b/tests/test_transpile.py
--- a/tests/test_transpile.py
+++ b/tests/test_transpile.py
@@ -91,6 +91,10 @@ def test_space(self):
         self.validate("SELECT 3>=3", "SELECT 3 >= 3")
 
     def test_comments(self):
+        self.validate(
+            "SELECT c AS /* foo */ (a, b, c) FROM t",
+            "SELECT c AS (a, b, c) /* foo */ FROM t",
+        )
         self.validate(
             "SELECT * FROM t1\n/*x*/\nUNION ALL SELECT * FROM t2",
             "SELECT * FROM t1 /* x */ UNION ALL SELECT * FROM t2",
@@ -435,6 +439,40 @@ def test_comments(self):
 FROM dw_1_dw_1_1.exactonline_2.transactionlines""",
             pretty=True,
         )
+        self.validate(
+            """/* The result of  some calculations
+ */
+with
+    base as (
+        select
+            sum(sb.hep_amount) as hep_amount,
+            -- I AM REMOVED
+            sum(sb.hep_budget)
+            /* Budget defined in sharepoint */
+            as blub
+            , 1 as bla
+        from gold.data_budget sb
+        group by all
+    )
+select
+    *
+from base
+""",
+            """/* The result of  some calculations
+ */
+WITH base AS (
+  SELECT
+    SUM(sb.hep_amount) AS hep_amount,
+    SUM(sb.hep_budget) /* I AM REMOVED */ AS blub, /* Budget defined in sharepoint */
+    1 AS bla
+  FROM gold.data_budget AS sb
+  GROUP BY ALL
+)
+SELECT
+  *
+FROM base""",
+            pretty=True,
+        )
 
     def test_types(self):
         self.validate("INT 1", "CAST(1 AS INT)")