Skip to content

Commit a932c44

Browse files
committed
Add docstring.
1 parent dc56fd9 commit a932c44

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

django_mongodb/compiler.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,39 @@ def __init__(self, *args, **kwargs):
2626
super().__init__(*args, **kwargs)
2727
self._group_pipeline = None
2828

29-
def _get_group_alias_column(self, col, annotation_group_idx):
30-
"""Generate alias and replacement for group columns."""
29+
def _get_group_alias_column(self, expr, annotation_group_idx):
30+
"""
31+
Generate a dummy field for use in ids fields in $group.
32+
33+
If the column is a composite expression (not a single column),
34+
it creates an auxiliary field and pass it through the flow.
35+
When a foreign field is needed in the ids, escape the dot (.) using GROUP_SEPARATOR.
36+
"""
3137
replacement = None
32-
if not isinstance(col, Col):
38+
if isinstance(expr, Col):
39+
col = expr
40+
else:
3341
alias = f"__annotation_group{next(annotation_group_idx)}"
34-
col_expr = self._get_column_from_expression(col, alias)
42+
col_expr = self._get_column_from_expression(expr, alias)
3543
replacement = col_expr
3644
col = col_expr
3745
if self.collection_name == col.alias:
3846
return col.target.column, replacement
3947
return f"{col.alias}{self.GROUP_SEPARATOR}{col.target.column}", replacement
4048

4149
def _get_column_from_expression(self, expr, alias):
42-
"""Get column target from expression."""
50+
"""
51+
Create a new column with the specified output type and alias to hold the aggregate value.
52+
53+
This function generates a column target from the given expression, setting the column's
54+
output type and assigning the provided alias to the column.
55+
"""
4356
column_target = expr.output_field.__class__()
4457
column_target.db_column = alias
4558
column_target.set_attributes_from_name(alias)
4659
return Col(self.collection_name, column_target)
4760

48-
def _prepare_expressions_for_pipeline(self, expression, target, count):
61+
def _prepare_expressions_for_pipeline(self, expression, target, annotation_group_idx):
4962
"""
5063
Prepare expressions for the aggregation pipeline.
5164
@@ -64,7 +77,9 @@ def _prepare_expressions_for_pipeline(self, expression, target, count):
6477
replacements = {}
6578
group = {}
6679
for sub_expr in self._get_aggregate_expressions(expression):
67-
alias = f"__aggregation{next(count)}" if sub_expr != expression else target
80+
alias = (
81+
f"__aggregation{next(annotation_group_idx)}" if sub_expr != expression else target
82+
)
6883
column_target = sub_expr.output_field.__class__()
6984
column_target.db_column = alias
7085
column_target.set_attributes_from_name(alias)

0 commit comments

Comments
 (0)