|
@@ -313,6 +313,41 @@ class Query(object):
|
|
|
clone.change_aliases(change_map)
|
|
|
return clone
|
|
|
|
|
|
+ def rewrite_cols(self, annotation, col_cnt):
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ orig_exprs = annotation.get_source_expressions()
|
|
|
+ new_exprs = []
|
|
|
+ for expr in orig_exprs:
|
|
|
+ if isinstance(expr, Ref):
|
|
|
+
|
|
|
+
|
|
|
+ new_exprs.append(expr)
|
|
|
+ elif isinstance(expr, Col):
|
|
|
+
|
|
|
+
|
|
|
+ col_cnt += 1
|
|
|
+ col_alias = '__col%d' % col_cnt
|
|
|
+ self.annotation_select[col_alias] = expr
|
|
|
+ self.append_annotation_mask([col_alias])
|
|
|
+ new_exprs.append(Ref(col_alias, expr))
|
|
|
+ else:
|
|
|
+
|
|
|
+
|
|
|
+ new_expr, col_cnt = self.rewrite_cols(expr, col_cnt)
|
|
|
+ new_exprs.append(new_expr)
|
|
|
+ annotation.set_source_expressions(new_exprs)
|
|
|
+ return annotation, col_cnt
|
|
|
+
|
|
|
def get_aggregation(self, using, added_aggregate_names):
|
|
|
"""
|
|
|
Returns the dictionary with the values of the existing aggregations.
|
|
@@ -350,11 +385,11 @@ class Query(object):
|
|
|
relabels[None] = 'subquery'
|
|
|
|
|
|
|
|
|
- for alias, annotation in inner_query.annotation_select.items():
|
|
|
- if annotation.is_summary:
|
|
|
-
|
|
|
-
|
|
|
- outer_query.annotations[alias] = annotation.relabeled_clone(relabels)
|
|
|
+ col_cnt = 0
|
|
|
+ for alias, expression in inner_query.annotation_select.items():
|
|
|
+ if expression.is_summary:
|
|
|
+ expression, col_cnt = inner_query.rewrite_cols(expression, col_cnt)
|
|
|
+ outer_query.annotations[alias] = expression.relabeled_clone(relabels)
|
|
|
del inner_query.annotation_select[alias]
|
|
|
try:
|
|
|
outer_query.add_subquery(inner_query, using)
|
|
@@ -1495,6 +1530,10 @@ class Query(object):
|
|
|
raise FieldError("Joined field references are not permitted in this query")
|
|
|
if name in self.annotations:
|
|
|
if summarize:
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
return Ref(name, self.annotation_select[name])
|
|
|
else:
|
|
|
return self.annotation_select[name]
|