Skip to content

Commit

Permalink
support final keyword for ClickHouse dialect (#765)
Browse files Browse the repository at this point in the history
  • Loading branch information
YiuRULE authored Nov 19, 2024
1 parent 2fee81c commit 779fb5b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pypika/dialects.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,9 +796,14 @@ class ClickHouseQueryBuilder(QueryBuilder):

def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self._final = False
self._sample = None
self._sample_offset = None

@builder
def final(self) -> "ClickHouseQueryBuilder":
self._final = True

@builder
def sample(self, sample: int, offset: Optional[int] = None) -> "ClickHouseQueryBuilder":
self._sample = sample
Expand All @@ -816,6 +821,8 @@ def _from_sql(self, with_namespace: bool = False, **kwargs: Any) -> str:
if self._delete_from:
return " {selectable} DELETE".format(selectable=selectable)
clauses = [selectable]
if self._final is not False:
clauses.append("FINAL")
if self._sample is not None:
clauses.append(f"SAMPLE {self._sample}")
if self._sample_offset is not None:
Expand Down
5 changes: 5 additions & 0 deletions pypika/tests/dialects/test_clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def test_use_SAMPLE_with_offset_keyword(self):
query = ClickHouseQuery.from_(t).select(t.foo).sample(10, 5)
self.assertEqual(str(query), 'SELECT "foo" FROM "abc" SAMPLE 10 OFFSET 5')

def test_use_FINAL_keyword(self):
t = Table('abc')
query = ClickHouseQuery.from_(t).select(t.foo).final()
self.assertEqual(str(query), 'SELECT "foo" FROM "abc" FINAL')


class ClickHouseDeleteTests(TestCase):
table_abc = Table("abc")
Expand Down

0 comments on commit 779fb5b

Please sign in to comment.