Respect _limit and _offset kwargs for select distinct

This commit is contained in:
Simon Wörpel 2023-08-08 15:42:38 +02:00
parent 5c2dc8d3af
commit d61d309637
No known key found for this signature in database
GPG Key ID: 34C0223BCE8C94E5
2 changed files with 10 additions and 2 deletions

View File

@ -675,7 +675,7 @@ class Table(object):
"""Return the number of rows in the table."""
return self.count()
def distinct(self, *args, **_filter):
def distinct(self, *args, **kwargs):
"""Return all the unique (distinct) values for the given ``columns``.
::
@ -699,7 +699,9 @@ class Table(object):
raise DatasetException("No such column: %s" % column)
columns.append(self.table.c[column])
clause = self._args_to_clause(_filter, clauses=clauses)
_limit = kwargs.pop("_limit", None)
_offset = kwargs.pop("_offset", 0)
clause = self._args_to_clause(kwargs, clauses=clauses)
if not len(columns):
return iter([])
@ -707,6 +709,8 @@ class Table(object):
columns,
distinct=True,
whereclause=clause,
limit=_limit,
offset=_offset,
order_by=[c.asc() for c in columns],
)
return self.db.query(q)

View File

@ -384,6 +384,10 @@ class TableTestCase(unittest.TestCase):
assert len(x) == 3, x
x = list(self.tbl.distinct("temperature", place=["B€rkeley", "G€lway"]))
assert len(x) == 6, x
x = list(self.tbl.distinct("temperature", _limit=3, place=["B€rkeley", "G€lway"]))
assert len(x) == 3, x
x = list(self.tbl.distinct("temperature", _limit=6, _offset=1, place=["B€rkeley", "G€lway"]))
assert len(x) == 5, x
def test_insert_many(self):
data = TEST_DATA * 100