diff --git a/sqlglot/generator.py b/sqlglot/generator.py index 6e18995818..e9ebf8fdd9 100644 --- a/sqlglot/generator.py +++ b/sqlglot/generator.py @@ -2314,8 +2314,10 @@ def withfill_sql(self, expression: exp.WithFill) -> str: 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 100fd1d0db..bb612912a7 100644 --- a/sqlglot/parser.py +++ b/sqlglot/parser.py @@ -4166,12 +4166,11 @@ def _parse_connect(self, skip_start_token: bool = False) -> t.Optional[exp.Conne 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"): diff --git a/tests/dialects/test_clickhouse.py b/tests/dialects/test_clickhouse.py index 1e84ade2d6..d99a4ef4a1 100644 --- a/tests/dialects/test_clickhouse.py +++ b/tests/dialects/test_clickhouse.py @@ -96,6 +96,9 @@ def test_clickhouse(self): 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" )