Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SELECT ON #293

Merged
merged 1 commit into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
)
Loading