Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect _limit and _offset kwargs for select distinct #424

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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