Skip to content

Commit f15c8a3

Browse files
WaVEVtimgraham
authored andcommitted
Fix columns order in aggregation queries
Adapt for a change in Django 5.2: django/django@65ad4ad
1 parent 0920486 commit f15c8a3

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

django_mongodb_backend/compiler.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,6 @@ def columns(self):
402402
columns = (
403403
self.get_default_columns(select_mask) if self.query.default_cols else self.query.select
404404
)
405-
# Populate QuerySet.select_related() data.
406-
related_columns = []
407-
if self.query.select_related:
408-
self.get_related_selections(related_columns, select_mask)
409-
if related_columns:
410-
related_columns, _ = zip(*related_columns, strict=True)
411405

412406
annotation_idx = 1
413407

@@ -426,11 +420,28 @@ def project_field(column):
426420
annotation_idx += 1
427421
return target, column
428422

429-
return (
430-
tuple(map(project_field, columns))
431-
+ tuple(self.annotations.items())
432-
+ tuple(map(project_field, related_columns))
433-
)
423+
selected = []
424+
if self.query.selected is None:
425+
selected = [
426+
*(project_field(col) for col in columns),
427+
*self.annotations.items(),
428+
]
429+
else:
430+
for expression in self.query.selected.values():
431+
# Reference to an annotation.
432+
if isinstance(expression, str):
433+
alias, expression = expression, self.annotations[expression]
434+
# Reference to a column.
435+
elif isinstance(expression, int):
436+
alias, expression = project_field(columns[expression])
437+
selected.append((alias, expression))
438+
# Populate QuerySet.select_related() data.
439+
related_columns = []
440+
if self.query.select_related:
441+
self.get_related_selections(related_columns, select_mask)
442+
if related_columns:
443+
related_columns, _ = zip(*related_columns, strict=True)
444+
return tuple(selected) + tuple(map(project_field, related_columns))
434445

435446
@cached_property
436447
def base_table(self):
@@ -477,7 +488,11 @@ def get_combinator_queries(self):
477488
# If the columns list is limited, then all combined queries
478489
# must have the same columns list. Set the selects defined on
479490
# the query on all combined queries, if not already set.
480-
if not compiler_.query.values_select and self.query.values_select:
491+
selected = self.query.selected
492+
if selected is not None and compiler_.query.selected is None:
493+
compiler_.query = compiler_.query.clone()
494+
compiler_.query.set_values(selected)
495+
elif not compiler_.query.values_select and self.query.values_select:
481496
compiler_.query = compiler_.query.clone()
482497
compiler_.query.set_values(
483498
(

django_mongodb_backend/expressions.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ def ref(self, compiler, connection): # noqa: ARG001
153153
if isinstance(self.source, Col) and self.source.alias != compiler.collection_name
154154
else ""
155155
)
156-
return f"${prefix}{self.refs}"
156+
if hasattr(self, "ordinal"):
157+
refs, _ = compiler.columns[self.ordinal - 1]
158+
else:
159+
refs = self.refs
160+
return f"${prefix}{refs}"
157161

158162

159163
def star(self, compiler, connection): # noqa: ARG001

django_mongodb_backend/features.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
8888
# of $setIsSubset must be arrays. Second argument is of type: null"
8989
# https://jira.mongodb.org/browse/SERVER-99186
9090
"model_fields_.test_arrayfield.QueryingTests.test_contained_by_subquery",
91-
# Broken by https://github.com/django/django/commit/65ad4ade74dc9208b9d686a451cd6045df0c9c3a
92-
"aggregation.tests.AggregateTestCase.test_even_more_aggregate",
93-
"aggregation.tests.AggregateTestCase.test_grouped_annotation_in_group_by",
94-
"aggregation.tests.AggregateTestCase.test_non_grouped_annotation_not_in_group_by",
95-
"aggregation_regress.tests.AggregationTests.test_aggregate_fexpr",
96-
"aggregation_regress.tests.AggregationTests.test_values_list_annotation_args_ordering",
97-
"annotations.tests.NonAggregateAnnotationTestCase.test_annotation_subquery_and_aggregate_values_chaining",
98-
"annotations.tests.NonAggregateAnnotationTestCase.test_values_fields_annotations_order",
99-
"queries.test_qs_combinators.QuerySetSetOperationTests.test_union_multiple_models_with_values_and_datetime_annotations",
100-
"queries.test_qs_combinators.QuerySetSetOperationTests.test_union_multiple_models_with_values_list_and_datetime_annotations",
101-
"queries.test_qs_combinators.QuerySetSetOperationTests.test_union_multiple_models_with_values_list_and_annotations",
102-
"queries.test_qs_combinators.QuerySetSetOperationTests.test_union_with_field_and_annotation_values",
103-
"queries.test_qs_combinators.QuerySetSetOperationTests.test_union_with_two_annotated_values_list",
104-
"queries.tests.Queries1Tests.test_union_values_subquery",
10591
# JSONArray not implemented.
10692
"db_functions.json.test_json_array.JSONArrayTests",
10793
# Some usage of prefetch_related() raises "ColPairs is not supported."

0 commit comments

Comments
 (0)