Skip to content
This repository was archived by the owner on Apr 27, 2021. It is now read-only.

Commit 7bd74e5

Browse files
committed
Add doc for DISTINCT
1 parent 9949f7c commit 7bd74e5

3 files changed

Lines changed: 31 additions & 6 deletions

File tree

cassandra/cqlengine/query.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def __init__(self, model):
266266
self._result_cache = None
267267
self._result_idx = None
268268

269-
self._distinct_fields = []
269+
self._distinct_fields = None
270270
self._batch = None
271271
self._ttl = getattr(model, '__default_ttl__', None)
272272
self._consistency = None
@@ -336,11 +336,11 @@ def _select_query(self):
336336
return SelectStatement(
337337
self.column_family_name,
338338
fields=self._select_fields(),
339-
distinct_fields=self._distinct_fields,
340339
where=self._where,
341340
order_by=self._order,
342341
limit=self._limit,
343-
allow_filtering=self._allow_filtering
342+
allow_filtering=self._allow_filtering,
343+
distinct_fields=self._distinct_fields
344344
)
345345

346346
# ----Reads------
@@ -664,7 +664,30 @@ def count(self):
664664

665665
def distinct(self, distinct_fields=None):
666666
"""
667-
Returns the DISTINCT rows by this query. Default to partition key if no distinct_fields specified.
667+
Returns the DISTINCT rows matched by this query.
668+
669+
distinct_fields default to the partition key fields if not specified.
670+
671+
*Note: distinct_fields must be a partition key or a static column*
672+
673+
.. code-block:: python
674+
675+
class Automobile(Model):
676+
manufacturer = columns.Text(partition_key=True)
677+
year = columns.Integer(primary_key=True)
678+
model = columns.Text(primary_key=True)
679+
price = columns.Decimal()
680+
681+
sync_table(Automobile)
682+
683+
# create rows
684+
685+
Automobile.objects.distinct()
686+
687+
# or
688+
689+
Automobile.objects.distinct(['manufacturer'])
690+
668691
"""
669692

670693
clone = copy.deepcopy(self)

cassandra/cqlengine/statements.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,13 +550,13 @@ class SelectStatement(BaseCQLStatement):
550550
def __init__(self,
551551
table,
552552
fields=None,
553-
distinct_fields=[],
554553
count=False,
555554
consistency=None,
556555
where=None,
557556
order_by=None,
558557
limit=None,
559-
allow_filtering=False):
558+
allow_filtering=False,
559+
distinct_fields=None):
560560

561561
"""
562562
:param where

docs/api/cassandra/cqlengine/query.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ The methods here are used to filter, order, and constrain results.
1818

1919
.. automethod:: count
2020

21+
.. automethod:: distinct
22+
2123
.. automethod:: filter
2224

2325
.. automethod:: get

0 commit comments

Comments
 (0)