Skip to content

Commit 72c8fea

Browse files
Merge pull request #963 from jverswijver/fetch_order_by_bug
Fix fetch order by bug when using DISTINCT
2 parents b74c492 + 7fb8829 commit 72c8fea

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Bugfix - Replace use of numpy aliases of built-in types with built-in type. (#938) PR #939
77
* Bugfix - `ExternalTable.delete` should not remove row on error (#953) PR #956
88
* Bugfix - Fix error handling of remove_object function in `s3.py` (#952) PR #955
9+
* Bugfix - Fix regression issue with `DISTINCT` clause and `GROUP_BY` (#914) PR #963
910
* Bugfix - Fix sql code generation to comply with sql mode `ONLY_FULL_GROUP_BY` (#916) PR #965
1011
* Bugfix - Fix count for left-joined `QueryExpressions` (#951) PR #966
1112
* Bugfix - Fix assertion error when performing a union into a join (#930) PR #967

datajoint/expression.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class QueryExpression:
4444
_heading = None
4545
_support = None
4646

47+
# If the query will be using distinct
48+
_distinct = False
49+
4750
@property
4851
def connection(self):
4952
""" a dj.Connection object """
@@ -106,9 +109,8 @@ def make_sql(self, fields=None):
106109
Make the SQL SELECT statement.
107110
:param fields: used to explicitly set the select attributes
108111
"""
109-
distinct = self.heading.names == self.primary_key
110112
return 'SELECT {distinct}{fields} FROM {from_}{where}'.format(
111-
distinct="DISTINCT " if distinct else "",
113+
distinct="DISTINCT " if self._distinct else "",
112114
fields=self.heading.as_sql(fields or self.heading.names),
113115
from_=self.from_clause(), where=self.where_clause())
114116

@@ -722,6 +724,7 @@ def __and__(self, other):
722724
if not isinstance(other, QueryExpression):
723725
raise DataJointError('Set U can only be restricted with a QueryExpression.')
724726
result = copy.copy(other)
727+
result._distinct = True
725728
result._heading = result.heading.set_primary_key(self.primary_key)
726729
result = result.proj()
727730
return result

docs-parts/intro/Releases_lang1.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Bugfix - Fix sql code generation to comply with sql mode ``ONLY_FULL_GROUP_BY`` (#916) PR #965
99
* Bugfix - Fix count for left-joined ``QueryExpressions`` (#951) PR #966
1010
* Bugfix - Fix assertion error when performing a union into a join (#930) PR #967
11+
* Bugfix - Fix regression issue with `DISTINCT` clause and `GROUP_BY` (#914) PR #963
1112

1213
0.13.2 -- May 7, 2021
1314
----------------------

tests/schema.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,3 +439,13 @@ class SessionDateA(dj.Lookup):
439439
('mouse1', '2020-12-03'),
440440
('mouse1', '2020-12-04')
441441
]
442+
443+
444+
@schema
445+
class Stimulus(dj.Lookup):
446+
definition = """
447+
id: int
448+
---
449+
contrast: int
450+
brightness: int
451+
"""

tests/test_fetch.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pandas
77
import warnings
88
from . import schema
9+
from .schema import Parent, Stimulus
910
import datajoint as dj
1011
import os
1112

@@ -287,3 +288,34 @@ def test_query_caching(self):
287288

288289
# reset cache directory state (will fail if purge was unsuccessful)
289290
os.rmdir(os.path.expanduser('~/dj_query_cache'))
291+
292+
def test_fetch_group_by(self):
293+
# https://github.com/datajoint/datajoint-python/issues/914
294+
295+
assert Parent().fetch('KEY', order_by='name') == [{'parent_id': 1}]
296+
297+
def test_dj_u_distinct(self):
298+
# Test developed to see if removing DISTINCT from the select statement
299+
# generation breakes the dj.U universal set imlementation
300+
301+
# Contents to be inserted
302+
contents = [
303+
(1,2,3),
304+
(2,2,3),
305+
(3,3,2),
306+
(4,5,5)
307+
]
308+
Stimulus.insert(contents)
309+
310+
# Query the whole table
311+
test_query = Stimulus()
312+
313+
# Use dj.U to create a list of unique contrast and brightness combinations
314+
result = dj.U('contrast', 'brightness') & test_query
315+
expected_result = [{'contrast': 2, 'brightness': 3},
316+
{'contrast': 3, 'brightness': 2},
317+
{'contrast': 5, 'brightness': 5}]
318+
319+
fetched_result = result.fetch(as_dict=True, order_by=('contrast', 'brightness'))
320+
Stimulus.delete_quick()
321+
assert fetched_result == expected_result

0 commit comments

Comments
 (0)