Skip to content

Commit a9d2029

Browse files
committed
comment edits
1 parent 959b1d6 commit a9d2029

2 files changed

Lines changed: 9 additions & 8 deletions

File tree

django_mongodb_backend/compiler.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ def _compile_window_sort_by(self, order_by_list, sort_fields):
843843
Compile Window.order_by to a MongoDB sortBy document and any pre-sort
844844
$addFields.
845845
846-
sort_fields is a shared dict (expr_mql_keyfield_name) across all
846+
sort_fields is a shared dict {expr_mql_key: field_name} across all
847847
windows in the same query, so that identical expressions reuse the same
848848
field name and different expressions get distinct names.
849849
"""
@@ -877,7 +877,7 @@ def _compile_window_sort_by(self, order_by_list, sort_fields):
877877
# so {ind: 0, val: v} vs {ind: 1, val: null} is decided by
878878
# "ind" before "val", giving clean null positioning.
879879
# null_ind=1 puts null AFTER non-null in ASC / BEFORE in DESC.
880-
# nulls_last!=descending means we want null last in ASC or
880+
# nulls_last != descending means nulls will be last in ASC or
881881
# first in DESC — both require the higher ind value (1).
882882
null_ind = int(bool(item.nulls_last) != bool(item.descending))
883883
null_field = f"__wsort{len(sort_fields) + 1}"
@@ -961,7 +961,7 @@ def _prepare_window_annotations_for_pipeline(self):
961961
"""
962962
Build $setWindowFields pipeline stages for window annotations.
963963
964-
Returns (stages, replacements) where stages is a list of pipeline dicts
964+
Return (stages, replacements) where stages is a list of pipeline dicts
965965
and replacements maps Window sub-expressions and full over-clause
966966
annotations to Ref objects for use in $project.
967967
"""
@@ -997,7 +997,7 @@ def _collect_windows_from_expr(expr, target=None):
997997
# $setWindowFields.
998998
groups = {}
999999
pre_add_fields = {}
1000-
sort_fields = {} # shared across all windows: expr_mql_key field_name
1000+
sort_fields = {} # shared across all windows, expr_mql_key: field_name
10011001
for alias, window in window_list:
10021002
partition_mql = self._compile_window_partition_by(window.partition_by)
10031003
sort_doc, pre_add_fields_ = self._compile_window_sort_by(window.order_by, sort_fields)

django_mongodb_backend/functions/window.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ def ntile(self, compiler, connection, alias, idx, default_frame): # noqa: ARG00
114114
}
115115
# SQL NTILE: rows 1..extra*(per+1) go to the first `extra` buckets (each
116116
# gets per+1 rows); remaining rows are distributed evenly across the rest.
117-
# `$max [$$per, 1]` guards against division by zero in the else branch when
118-
# num_buckets > total (per==0), which is unreachable but may be evaluated.
119117
add_fields = {
120118
alias: {
121119
"$let": {
@@ -156,6 +154,9 @@ def ntile(self, compiler, connection, alias, idx, default_frame): # noqa: ARG00
156154
{"$add": ["$$thr", 1]},
157155
]
158156
},
157+
# Guard against division by
158+
# zero when num_buckets >
159+
# total (per==0).
159160
{"$max": ["$$per", 1]},
160161
]
161162
}
@@ -174,8 +175,8 @@ def ntile(self, compiler, connection, alias, idx, default_frame): # noqa: ARG00
174175

175176

176177
def percent_rank(self, compiler, connection, alias, idx, default_frame): # noqa: ARG001
177-
# Use document-position rank ($sum:1 docs unbounded-to-current)
178-
# instead of $rank, which MongoDB limits to a single sort field.
178+
# Use document-position rank ($sum:1 docs unbounded-to-current) instead of
179+
# $rank, which MongoDB limits to a single sort field.
179180
row_num_alias = f"__wtemp{next(idx)}"
180181
count_alias = f"__wtemp{next(idx)}"
181182
output = {

0 commit comments

Comments
 (0)