mirror of
https://github.com/Infinidat/infi.clickhouse_orm.git
synced 2024-11-22 00:56:34 +03:00
Support for using LIMIT N BY feature
See https://clickhouse.yandex/docs/en/query_language/select/#limit-by-clause
This commit is contained in:
parent
4848c7f813
commit
6d7b6250c5
|
@ -293,6 +293,8 @@ class QuerySet(object):
|
|||
self._grouping_with_totals = False
|
||||
self._fields = model_cls.fields().keys()
|
||||
self._limits = None
|
||||
self._limit_by = None
|
||||
self._limit_by_fields = None
|
||||
self._distinct = False
|
||||
self._final = False
|
||||
|
||||
|
@ -332,6 +334,24 @@ class QuerySet(object):
|
|||
qs._limits = (start, stop - start)
|
||||
return qs
|
||||
|
||||
def limit_by(self, offset_limit, *fields):
|
||||
if isinstance(offset_limit, six.integer_types):
|
||||
# Single limit
|
||||
assert offset_limit >= 0, 'negative limits are not supported'
|
||||
qs = copy(self)
|
||||
qs._limit_by = (0, offset_limit)
|
||||
qs._limit_by_fields = fields
|
||||
return qs
|
||||
else:
|
||||
# Offset, limit
|
||||
offset = offset_limit[0]
|
||||
limit = offset_limit[1]
|
||||
assert offset >= 0 and limit >= 0, 'negative limits are not supported'
|
||||
qs = copy(self)
|
||||
qs._limit_by = (offset, limit)
|
||||
qs._limit_by_fields = fields
|
||||
return qs
|
||||
|
||||
def select_fields_as_sql(self):
|
||||
"""
|
||||
Returns the selected fields or expressions as a SQL string.
|
||||
|
@ -369,6 +389,10 @@ class QuerySet(object):
|
|||
if self._limits:
|
||||
sql += '\nLIMIT %d, %d' % self._limits
|
||||
|
||||
if self._limit_by:
|
||||
sql += '\nLIMIT %d, %d' % self._limit_by
|
||||
sql += ' BY %s' % comma_join('`%s`' % field for field in self._limit_by_fields)
|
||||
|
||||
return sql
|
||||
|
||||
def order_by_as_sql(self):
|
||||
|
|
|
@ -432,6 +432,12 @@ class AggregateTestCase(TestCaseWithData):
|
|||
qs = Mdl.objects_in(self.database).filter(the__next__number__gt=1)
|
||||
self.assertEqual(qs.conditions_as_sql(), 'the__next__number > 1')
|
||||
|
||||
def test_limit_by(self):
|
||||
qs = Person.objects_in(self.database).aggregate('first_name', 'last_name', 'height', n='count()').\
|
||||
order_by('first_name', '-height').limit_by(1, 'first_name')
|
||||
self.assertEqual(qs.count(), 94)
|
||||
self.assertEqual(list(qs)[89].last_name, 'Bowen')
|
||||
|
||||
|
||||
Color = Enum('Color', u'red blue green yellow brown white black')
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user