Skip to content

Commit

Permalink
support final keyword for ClickHouse dialect
Browse files Browse the repository at this point in the history
  • Loading branch information
YiuRULE committed Oct 18, 2023
1 parent 627b60a commit 27bcb5b
Show file tree
Hide file tree
Showing 2 changed files with 11 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 @@ -783,9 +783,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 @@ -803,6 +808,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
4 changes: 4 additions & 0 deletions pypika/tests/dialects/test_clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ 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 27bcb5b

Please sign in to comment.