Skip to content

Commit

Permalink
Fix: preserve column quoting in DISTINCT ON elimination
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas committed Nov 17, 2024
1 parent 38af9b2 commit 59b8b6d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 2 additions & 1 deletion sqlglot/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ def eliminate_distinct_on(expression: exp.Expression) -> exp.Expression:

if not isinstance(select, exp.Alias):
alias = find_new_name(taken_names, select.output_name or "_col")
select = select.replace(exp.alias_(select, alias))
quoted = select.this.args.get("quoted") if isinstance(select, exp.Column) else None
select = select.replace(exp.alias_(select, alias, quoted=quoted))

taken_names.add(select.output_name)
new_selects.append(select.args["alias"])
Expand Down
5 changes: 5 additions & 0 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ def test_eliminate_distinct_on(self):
'SELECT DISTINCT ON (a) a AS "A", b FROM x ORDER BY c DESC',
'SELECT "A", b FROM (SELECT a AS "A", b AS b, ROW_NUMBER() OVER (PARTITION BY a ORDER BY c DESC) AS _row_number FROM x) AS _t WHERE _row_number = 1',
)
self.validate(
eliminate_distinct_on,
'SELECT DISTINCT ON (a) "A", b FROM x ORDER BY c DESC',
'SELECT "A", b FROM (SELECT "A" AS "A", b AS b, ROW_NUMBER() OVER (PARTITION BY a ORDER BY c DESC) AS _row_number FROM x) AS _t WHERE _row_number = 1',
)

def test_eliminate_qualify(self):
self.validate(
Expand Down

0 comments on commit 59b8b6d

Please sign in to comment.