Skip to content

Commit

Permalink
Respect _limit and _offset kwargs for select distinct
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwoerpel committed Aug 8, 2023
1 parent 5c2dc8d commit d61d309
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 deletions dataset/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ def __len__(self):
"""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``.
::
Expand All @@ -699,14 +699,18 @@ def distinct(self, *args, **_filter):
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([])

q = expression.select(
columns,
distinct=True,
whereclause=clause,
limit=_limit,
offset=_offset,
order_by=[c.asc() for c in columns],
)
return self.db.query(q)
Expand Down
4 changes: 4 additions & 0 deletions test/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ def test_distinct(self):
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
Expand Down

0 comments on commit d61d309

Please sign in to comment.