cubicweb/server/sources/rql2sql.py
branch3.26
changeset 12296 23bc9f385645
parent 12233 62918203b9fc
child 12567 26744ad37953
--- a/cubicweb/server/sources/rql2sql.py	Mon Apr 23 15:19:19 2018 +0200
+++ b/cubicweb/server/sources/rql2sql.py	Thu Oct 12 15:22:00 2017 +0200
@@ -279,6 +279,14 @@
                         groups.append(vref)
 
 
+def is_in_aggregat(node):
+    while node:
+        node = node.parent
+        if isinstance(node, Function) and node.descr().aggregat:
+            return True
+    return False
+
+
 def fix_selection_and_group(rqlst, needwrap, selectsortterms,
                             sorts, groups, having):
     if selectsortterms and sorts:
@@ -288,13 +296,11 @@
         # when a query is grouped, ensure sort terms are grouped as well
         for sortterm in sorts:
             term = sortterm.term
-            if not (isinstance(term, Constant) or
-                    (isinstance(term, Function) and
-                     get_func_descr(term.name).aggregat)):
-                for vref in term.iget_nodes(VariableRef):
-                    if not any(vref.is_equivalent(group) for group in groupvrefs):
-                        groups.append(vref)
-                        groupvrefs.append(vref)
+            for vref in term.iget_nodes(VariableRef):
+                if not (any(vref.is_equivalent(group) for group in groupvrefs)
+                        or is_in_aggregat(vref)):
+                    groups.append(vref)
+                    groupvrefs.append(vref)
     if needwrap and (groups or having):
         selectedidx = set(vref.name for term in rqlst.selection
                           for vref in term.get_nodes(VariableRef))