Skip to content

Commit fe171e8

Browse files
authored
Revert "Fix(optimizer)!: parse unqualified names in DISTINCT ON (...) as iden…" (#4804)
This reverts commit 14c3de9.
1 parent 856faa9 commit fe171e8

File tree

2 files changed

+4
-23
lines changed

2 files changed

+4
-23
lines changed

sqlglot/parser.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3089,21 +3089,10 @@ def _parse_select(
30893089
)
30903090

30913091
if distinct:
3092-
on = self._parse_value() if self._match(TokenType.ON) else None
3093-
3094-
# In `SELECT DISTINCT ON (p1, t.c2)`, p1 is an identifier, while c2 is a column reference.
3095-
# The reason is that `p1` can be a projection alias or a column, but engines resolve it as
3096-
# the projection alias (e.g. postgres, duckdb, clickhouse).
3097-
if isinstance(on, exp.Tuple):
3098-
on.set(
3099-
"expressions",
3100-
[
3101-
c.this if isinstance(c, exp.Column) and not c.table else c
3102-
for c in on.expressions
3103-
],
3104-
)
3105-
3106-
distinct = self.expression(exp.Distinct, on=on)
3092+
distinct = self.expression(
3093+
exp.Distinct,
3094+
on=self._parse_value() if self._match(TokenType.ON) else None,
3095+
)
31073096

31083097
if all_ and distinct:
31093098
self.raise_error("Cannot specify both ALL and DISTINCT after SELECT")

tests/fixtures/optimizer/qualify_columns.sql

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,6 @@ SELECT x.a AS a, x.b AS b FROM x AS x ORDER BY a, b;
9090
SELECT DISTINCT a AS c, b AS d FROM x ORDER BY 1;
9191
SELECT DISTINCT x.a AS c, x.b AS d FROM x AS x ORDER BY c;
9292

93-
# execute: false
94-
SELECT DISTINCT ON (x, t.y) * FROM t;
95-
SELECT DISTINCT ON (x, t.y) * FROM t AS t;
96-
97-
# execute: false
98-
SELECT DISTINCT ON (new_col) t1.col1 AS new_col FROM table1 AS t1;
99-
SELECT DISTINCT ON (new_col) t1.col1 AS new_col FROM table1 AS t1;
100-
10193
SELECT 2 FROM x GROUP BY 1;
10294
SELECT 2 AS "2" FROM x AS x GROUP BY 1;
10395

0 commit comments

Comments
 (0)