Skip to content

Commit

Permalink
Merge pull request #79 from sdrenn/final-clause
Browse files Browse the repository at this point in the history
final clause
  • Loading branch information
xzkostyan committed Feb 15, 2020
2 parents 73bca7f + 5534f3e commit 5a9f3bd
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions clickhouse_sqlalchemy/drivers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ def _compose_select_body(
if sample_clause is not None:
text += self.sample_clause(select, **kwargs)

final_clause = getattr(select, '_final_clause', None)

if final_clause is not None:
text += self.final_clause()

if select._whereclause is not None:
t = select._whereclause._compiler_dispatch(self, **kwargs)
if t:
Expand Down Expand Up @@ -285,6 +290,9 @@ def _compose_select_body(
def sample_clause(self, select, **kw):
return " \nSAMPLE " + self.process(select._sample_clause, **kw)

def final_clause(self):
return " \nFINAL"

def group_by_clause(self, select, **kw):
text = ""

Expand Down
7 changes: 7 additions & 0 deletions clickhouse_sqlalchemy/orm/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

class Query(BaseQuery):
_with_totals = False
_final = None
_sample = None
_array_join = None

Expand All @@ -21,6 +22,7 @@ def _compile_context(self, labels=True):
statement = context.statement

statement._with_totals = self._with_totals
statement._final_clause = self._final
statement._sample_clause = sample_clause(self._sample)
statement._array_join = self._array_join

Expand All @@ -41,6 +43,11 @@ def array_join(self, *columns):
self._array_join = ArrayJoin(*columns)
return self

def final(self):
self._final = True

return self

def sample(self, sample):
self._sample = sample

Expand Down
5 changes: 5 additions & 0 deletions clickhouse_sqlalchemy/sql/selectable.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@ def __init__(self, left, right,

class Select(StandardSelect):
_with_totals = False
_final_clause = None
_sample_clause = None
_array_join = None

def with_totals(self):
self._with_totals = True
return self

def final(self):
self._final_clause = True
return self

def sample(self, sample):
self._sample_clause = sample_clause(sample)
return self
Expand Down
9 changes: 9 additions & 0 deletions tests/orm/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ def test_sample(self):
'SELECT x AS t1_x FROM t1 SAMPLE 0.1 GROUP BY x'
)

def test_final(self):
table = self.create_table()

query = session.query(table.c.x).final().group_by(table.c.x)
self.assertEqual(
self.compile(query),
'SELECT x AS t1_x FROM t1 FINAL GROUP BY x'
)

def test_lambda_functions(self):
query = self.session.query(
func.arrayFilter(
Expand Down
9 changes: 9 additions & 0 deletions tests/sql/test_selectable.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ def test_sample(self):
'SELECT x FROM t1 SAMPLE 0.1 GROUP BY x'
)

def test_final(self):
table = self.create_table()

query = select([table.c.x]).final().group_by(table.c.x)
self.assertEqual(
self.compile(query),
'SELECT x FROM t1 FINAL GROUP BY x'
)

def test_nested_type(self):
table = self.create_table(
't1',
Expand Down

0 comments on commit 5a9f3bd

Please sign in to comment.