@@ -24,7 +24,7 @@ class SQLCompiler(compiler.SQLCompiler):
24
24
25
25
def __init__ (self , * args , ** kwargs ):
26
26
super ().__init__ (* args , ** kwargs )
27
- self ._group_pipeline = None
27
+ self .aggregation_pipeline = None
28
28
29
29
def _get_group_alias_column (self , expr , annotation_group_idx ):
30
30
"""
@@ -100,7 +100,7 @@ def _prepare_expressions_for_pipeline(self, expression, target, annotation_group
100
100
replacements [sub_expr ] = replacing_expr
101
101
return replacements , group
102
102
103
- def _prepare_annotations_for_group_pipeline (self ):
103
+ def _prepare_annotations_for_aggregation_pipeline (self ):
104
104
"""Prepare annotations for the MongoDB aggregation pipeline."""
105
105
replacements = {}
106
106
group = {}
@@ -157,7 +157,7 @@ def _get_group_id_expressions(self, order_by):
157
157
158
158
return ids , replacements
159
159
160
- def _build_group_pipeline (self , ids , group ):
160
+ def _build_aggregation_pipeline (self , ids , group ):
161
161
"""Build the aggregation pipeline for grouping."""
162
162
pipeline = []
163
163
if not ids :
@@ -196,15 +196,15 @@ def _build_group_pipeline(self, ids, group):
196
196
197
197
def pre_sql_setup (self , with_col_aliases = False ):
198
198
extra_select , order_by , group_by = super ().pre_sql_setup (with_col_aliases = with_col_aliases )
199
- group , all_replacements = self ._prepare_annotations_for_group_pipeline ()
199
+ group , all_replacements = self ._prepare_annotations_for_aggregation_pipeline ()
200
200
# query.group_by is either:
201
201
# - None: no GROUP BY
202
202
# - True: group by select fields
203
203
# - a list of expressions to group by.
204
204
if group or self .query .group_by :
205
205
ids , replacements = self ._get_group_id_expressions (order_by )
206
206
all_replacements .update (replacements )
207
- pipeline = self ._build_group_pipeline (ids , group )
207
+ pipeline = self ._build_aggregation_pipeline (ids , group )
208
208
if self .having :
209
209
pipeline .append (
210
210
{
@@ -215,7 +215,7 @@ def pre_sql_setup(self, with_col_aliases=False):
215
215
}
216
216
}
217
217
)
218
- self ._group_pipeline = pipeline
218
+ self .aggregation_pipeline = pipeline
219
219
220
220
self .annotations = {
221
221
target : expr .replace_expressions (all_replacements )
@@ -342,7 +342,7 @@ def build_query(self, columns=None):
342
342
"""Check if the query is supported and prepare a MongoQuery."""
343
343
self .check_query ()
344
344
query = self .query_class (self )
345
- query .aggregation_pipeline = self .get_aggregation_pipeline ()
345
+ query .aggregation_pipeline = self .aggregation_pipeline
346
346
query .lookup_pipeline = self .get_lookup_pipeline ()
347
347
query .order_by (self ._get_ordering ())
348
348
query .project_fields = self .get_project_fields (columns , ordering = query .ordering )
@@ -461,9 +461,6 @@ def _get_aggregate_expressions(self, expr):
461
461
elif hasattr (expr , "get_source_expressions" ):
462
462
stack .extend (expr .get_source_expressions ())
463
463
464
- def get_aggregation_pipeline (self ):
465
- return self ._group_pipeline
466
-
467
464
def get_project_fields (self , columns = None , ordering = None ):
468
465
fields = {}
469
466
for name , expr in columns or []:
@@ -613,22 +610,20 @@ class SQLAggregateCompiler(SQLCompiler):
613
610
def build_query (self , columns = None ):
614
611
query = self .query_class (self )
615
612
query .project_fields = self .get_project_fields (tuple (self .annotations .items ()))
616
- query .aggregation_pipeline = self .get_aggregation_pipeline ()
613
+ query .aggregation_pipeline = self .aggregation_pipeline
617
614
618
615
compiler = self .query .inner_query .get_compiler (
619
616
self .using ,
620
617
elide_empty = self .elide_empty ,
621
618
)
622
619
compiler .pre_sql_setup (with_col_aliases = False )
620
+ # Avoid $project (columns=None) if unneeded.
623
621
columns = (
624
622
compiler .get_columns ()
625
623
if compiler .query .annotations or not compiler .query .default_cols
626
624
else None
627
625
)
628
- subquery = compiler .build_query (
629
- # Avoid $project (columns=None) if unneeded.
630
- columns
631
- )
626
+ subquery = compiler .build_query (columns )
632
627
query .subquery = subquery
633
628
return query
634
629
0 commit comments