@@ -44,6 +44,9 @@ class QueryExpression:
44
44
_heading = None
45
45
_support = None
46
46
47
+ # If the query will be using distinct
48
+ _distinct = False
49
+
47
50
@property
48
51
def connection (self ):
49
52
""" a dj.Connection object """
@@ -107,7 +110,7 @@ def make_sql(self, fields=None, distinct=True):
107
110
:param fields: used to explicitly set the select attributes
108
111
"""
109
112
return 'SELECT {distinct}{fields} FROM {from_}{where}' .format (
110
- distinct = "DISTINCT " if distinct else "" ,
113
+ distinct = "DISTINCT " if self . _distinct else "" ,
111
114
fields = self .heading .as_sql (fields or self .heading .names ),
112
115
from_ = self .from_clause (), where = self .where_clause ())
113
116
@@ -509,11 +512,9 @@ def cursor(self, offset=0, limit=None, order_by=None, as_dict=False):
509
512
"""
510
513
if offset and limit is None :
511
514
raise DataJointError ('limit is required when offset is set' )
515
+ sql = self .make_sql ()
512
516
if order_by is not None :
513
- sql = self .make_sql (distinct = False )
514
517
sql += ' ORDER BY ' + ', ' .join (order_by )
515
- else :
516
- sql = self .make_sql ()
517
518
if limit is not None :
518
519
sql += ' LIMIT %d' % limit + (' OFFSET %d' % offset if offset else "" )
519
520
logger .debug (sql )
@@ -719,6 +720,7 @@ def __and__(self, other):
719
720
if not isinstance (other , QueryExpression ):
720
721
raise DataJointError ('Set U can only be restricted with a QueryExpression.' )
721
722
result = copy .copy (other )
723
+ result ._distinct = True
722
724
result ._heading = result .heading .set_primary_key (self .primary_key )
723
725
result = result .proj ()
724
726
return result
0 commit comments