Repository: tobymao/sqlglot

Issue:
Error when using WITH FILL + INTERPOLATE in Clickhouse
Hi all,

I am writing a query that uses some WITH FILL + INTERPOLATE in Clickhouse and I am seeing the following error:

```python
ParseError: Required keyword: 'this' missing for <class 'sqlglot.expressions.Alias'>. Line 10, Col: 20.
  HOUR
TO toDateTime64(1730185140, 3) - INTERVAL 7 HOUR
STEP toIntervalSecond(900)
INTERPOLATE (interp)
```

### To Reproduce

This is the offending Clickhouse query:

```sql
SELECT
    1730098800::DATETIME64 DATETIME,
    'test' interp
ORDER BY DATETIME
WITH FILL
FROM toDateTime64(1730098800, 3)
TO toDateTime64(1730185140, 3)
STEP toIntervalSecond(900)
INTERPOLATE (interp)
```

Steps to reproduce:

```python
import sqlglot
sql = '''
SELECT
    1730098800::DATETIME64 DATETIME,
    'test' interp
ORDER BY DATETIME
WITH FILL
FROM toDateTime64(1730098800, 3) - INTERVAL 7 HOUR
TO toDateTime64(1730185140, 3) - INTERVAL 7 HOUR
STEP toIntervalSecond(900)
INTERPOLATE (interp)
'''
sqlglot.parse_one(sql, read='clickhouse')
```

Discussion:

Patch:
diff --git a/sqlglot/generator.py b/sqlglot/generator.py
index 6e189958..e9ebf8fd 100644
--- a/sqlglot/generator.py
+++ b/sqlglot/generator.py
@@ -2314,8 +2314,10 @@ class Generator(metaclass=_Generator):
         step_sql = self.sql(expression, "step")
         step_sql = f" STEP {step_sql}" if step_sql else ""
         interpolated_values = [
-            f"{self.sql(named_expression, 'alias')} AS {self.sql(named_expression, 'this')}"
-            for named_expression in expression.args.get("interpolate") or []
+            f"{self.sql(e, 'alias')} AS {self.sql(e, 'this')}"
+            if isinstance(e, exp.Alias)
+            else self.sql(e, "this")
+            for e in expression.args.get("interpolate") or []
         ]
         interpolate = (
             f" INTERPOLATE ({', '.join(interpolated_values)})" if interpolated_values else ""
diff --git a/sqlglot/parser.py b/sqlglot/parser.py
index 100fd1d0..bb612912 100644
--- a/sqlglot/parser.py
+++ b/sqlglot/parser.py
@@ -4166,12 +4166,11 @@ class Parser(metaclass=_Parser):
 
         return self.expression(exp.Connect, start=start, connect=connect, nocycle=nocycle)
 
-    def _parse_name_as_expression(self) -> exp.Alias:
-        return self.expression(
-            exp.Alias,
-            alias=self._parse_id_var(any_token=True),
-            this=self._match(TokenType.ALIAS) and self._parse_assignment(),
-        )
+    def _parse_name_as_expression(self) -> t.Optional[exp.Expression]:
+        this = self._parse_id_var(any_token=True)
+        if self._match(TokenType.ALIAS):
+            this = self.expression(exp.Alias, alias=this, this=self._parse_assignment())
+        return this
 
     def _parse_interpolate(self) -> t.Optional[t.List[exp.Expression]]:
         if self._match_text_seq("INTERPOLATE"):


Test patch:
diff --git a/tests/dialects/test_clickhouse.py b/tests/dialects/test_clickhouse.py
index 1e84ade2..d99a4ef4 100644
--- a/tests/dialects/test_clickhouse.py
+++ b/tests/dialects/test_clickhouse.py
@@ -96,6 +96,9 @@ class TestClickhouse(Validator):
         self.validate_identity("TRUNCATE TABLE t1 ON CLUSTER test_cluster")
         self.validate_identity("TRUNCATE DATABASE db")
         self.validate_identity("TRUNCATE DATABASE db ON CLUSTER test_cluster")
+        self.validate_identity(
+            "SELECT CAST(1730098800 AS DateTime64) AS DATETIME, 'test' AS interp ORDER BY DATETIME WITH FILL FROM toDateTime64(1730098800, 3) - INTERVAL '7' HOUR TO toDateTime64(1730185140, 3) - INTERVAL '7' HOUR STEP toIntervalSecond(900) INTERPOLATE (interp)"
+        )
         self.validate_identity(
             "SELECT number, COUNT() OVER (PARTITION BY number % 3) AS partition_count FROM numbers(10) WINDOW window_name AS (PARTITION BY number) QUALIFY partition_count = 4 ORDER BY number"
         )