Skip to content

Commit b4ea602

Browse files
authored
Fix(clickhouse): improve parsing of WITH FILL ... INTERPOLATE (#4311)
1 parent e92904e commit b4ea602

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

sqlglot/generator.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,8 +2314,10 @@ def withfill_sql(self, expression: exp.WithFill) -> str:
23142314
step_sql = self.sql(expression, "step")
23152315
step_sql = f" STEP {step_sql}" if step_sql else ""
23162316
interpolated_values = [
2317-
f"{self.sql(named_expression, 'alias')} AS {self.sql(named_expression, 'this')}"
2318-
for named_expression in expression.args.get("interpolate") or []
2317+
f"{self.sql(e, 'alias')} AS {self.sql(e, 'this')}"
2318+
if isinstance(e, exp.Alias)
2319+
else self.sql(e, "this")
2320+
for e in expression.args.get("interpolate") or []
23192321
]
23202322
interpolate = (
23212323
f" INTERPOLATE ({', '.join(interpolated_values)})" if interpolated_values else ""

sqlglot/parser.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4166,12 +4166,11 @@ def _parse_connect(self, skip_start_token: bool = False) -> t.Optional[exp.Conne
41664166

41674167
return self.expression(exp.Connect, start=start, connect=connect, nocycle=nocycle)
41684168

4169-
def _parse_name_as_expression(self) -> exp.Alias:
4170-
return self.expression(
4171-
exp.Alias,
4172-
alias=self._parse_id_var(any_token=True),
4173-
this=self._match(TokenType.ALIAS) and self._parse_assignment(),
4174-
)
4169+
def _parse_name_as_expression(self) -> t.Optional[exp.Expression]:
4170+
this = self._parse_id_var(any_token=True)
4171+
if self._match(TokenType.ALIAS):
4172+
this = self.expression(exp.Alias, alias=this, this=self._parse_assignment())
4173+
return this
41754174

41764175
def _parse_interpolate(self) -> t.Optional[t.List[exp.Expression]]:
41774176
if self._match_text_seq("INTERPOLATE"):

tests/dialects/test_clickhouse.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ def test_clickhouse(self):
9696
self.validate_identity("TRUNCATE TABLE t1 ON CLUSTER test_cluster")
9797
self.validate_identity("TRUNCATE DATABASE db")
9898
self.validate_identity("TRUNCATE DATABASE db ON CLUSTER test_cluster")
99+
self.validate_identity(
100+
"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)"
101+
)
99102
self.validate_identity(
100103
"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"
101104
)

0 commit comments

Comments
 (0)