Merge pull request #424 from investigativedata/fix/distinct-slice

Respect _limit and _offset kwargs for select distinct
This commit is contained in:
Gregor Aisch 2025-02-05 12:22:42 +00:00 committed by GitHub
commit b2ab09e58c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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 the number of rows in the table."""
return self.count() return self.count()
def distinct(self, *args, **_filter): def distinct(self, *args, **kwargs):
"""Return all the unique (distinct) values for the given ``columns``. """Return all the unique (distinct) values for the given ``columns``.
:: ::
@ -699,7 +699,9 @@ class Table(object):
raise DatasetException("No such column: %s" % column) raise DatasetException("No such column: %s" % column)
columns.append(self.table.c[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): if not len(columns):
return iter([]) return iter([])
@ -707,6 +709,8 @@ class Table(object):
columns, columns,
distinct=True, distinct=True,
whereclause=clause, whereclause=clause,
limit=_limit,
offset=_offset,
order_by=[c.asc() for c in columns], order_by=[c.asc() for c in columns],
) )
return self.db.query(q) return self.db.query(q)

View File

@ -384,6 +384,10 @@ class TableTestCase(unittest.TestCase):
assert len(x) == 3, x assert len(x) == 3, x
x = list(self.tbl.distinct("temperature", place=["B€rkeley", "G€lway"])) x = list(self.tbl.distinct("temperature", place=["B€rkeley", "G€lway"]))
assert len(x) == 6, x 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): def test_insert_many(self):
data = TEST_DATA * 100 data = TEST_DATA * 100