Skip to content

Commit

Permalink
Merge pull request #293 from aronbierbaum/add_distinct_on
Browse files Browse the repository at this point in the history
Add support for SELECT ON
  • Loading branch information
xzkostyan authored Mar 4, 2024
2 parents 46482bf + a712772 commit 2875475
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
20 changes: 20 additions & 0 deletions clickhouse_sqlalchemy/drivers/compilers/sqlcompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,23 @@ def visit_not_ilike_op_binary(self, binary, operator, **kw):
self.process(binary.left, **kw),
self.process(binary.right, **kw)
)

def get_select_precolumns(self, select, **kw):
# Do not call super().get_select_precolumns because
# it will warn/raise when distinct on is present
if select._distinct or select._distinct_on:
if select._distinct_on:
return (
"DISTINCT ON ("
+ ", ".join(
[
self.process(col, **kw)
for col in select._distinct_on
]
)
+ ") "
)
else:
return "DISTINCT "
else:
return ""
9 changes: 9 additions & 0 deletions tests/sql/test_selectable.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,3 +559,12 @@ def test_sql_expression_join(self):
self.compile(join),
'table_1 INNER JOIN table_2 ON table_1.x = table_2.x'
)

def test_distinct_on(self):
table = self._make_table()

query = select(table.c.x).distinct(table.c.x)
self.assertEqual(
self.compile(query),
'SELECT DISTINCT ON (t1.x) t1.x FROM t1'
)

0 comments on commit 2875475

Please sign in to comment.