Skip to content

Commit 561c008

Browse files
committed
Support SAMPLE clause via new QuerySet.sample(sample_fraction, sample_offset)
1 parent aa41421 commit 561c008

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

clickhouse_backend/models/query.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ def prewhere(self, *args, **kwargs):
3131
clone._query.add_prewhere(Q(*args, **kwargs))
3232
return clone
3333

34+
def sample(self, sample_fraction, sample_offset=None):
35+
clone = self._chain()
36+
clone._query.add_sample(sample_fraction, sample_offset)
37+
return clone
38+
3439
def datetimes(self, field_name, kind, order="ASC", tzinfo=None):
3540
"""
3641
Return a list of datetime objects representing all available

clickhouse_backend/models/sql/compiler.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ def as_sql(self, with_limits=True, with_col_aliases=False):
122122
refcounts_before = self.query.alias_refcount.copy()
123123
try:
124124
combinator = self.query.combinator
125+
sample_fraction = self.query.sample_fraction
126+
sample_offset = self.query.sample_offset
125127
if compat.dj_ge42:
126128
extra_select, order_by, group_by = self.pre_sql_setup(
127129
with_col_aliases=with_col_aliases or bool(combinator),
@@ -203,6 +205,13 @@ def as_sql(self, with_limits=True, with_col_aliases=False):
203205
result += ["FROM", *from_]
204206
params.extend(f_params)
205207

208+
if sample_fraction:
209+
if sample_offset:
210+
sample_sql = "SAMPLE %s OFFSET %s" % (sample_fraction, sample_offset)
211+
else:
212+
sample_sql = "SAMPLE %s" % sample_fraction
213+
result.append(sample_sql)
214+
206215
if prewhere:
207216
result.append("PREWHERE %s" % prewhere)
208217
params.extend(p_params)

clickhouse_backend/models/sql/query.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def __init__(self, model, where=query.WhereNode, alias_cols=True):
1919
super().__init__(model, where, alias_cols)
2020
self.setting_info = {}
2121
self.prewhere = query.WhereNode()
22+
self.sample_fraction = None
23+
self.sample_offset = None
2224

2325
def sql_with_params(self):
2426
"""Choose the right db when database router is used."""
@@ -28,6 +30,8 @@ def clone(self):
2830
obj = super().clone()
2931
obj.setting_info = self.setting_info.copy()
3032
obj.prewhere = self.prewhere.clone()
33+
obj.sample_fraction = self.sample_fraction
34+
obj.sample_offset = self.sample_offset
3135
return obj
3236

3337
def explain(self, using, format=None, type=None, **settings):
@@ -36,6 +40,10 @@ def explain(self, using, format=None, type=None, **settings):
3640
compiler = q.get_compiler(using=using)
3741
return "\n".join(compiler.explain_query())
3842

43+
def add_sample(self, sample_fraction, sample_offset):
44+
self.sample_fraction = sample_fraction
45+
self.sample_offset = sample_offset
46+
3947
def add_prewhere(self, q_object):
4048
"""
4149
refer add_q

0 commit comments

Comments
 (0)