Skip to content

Commit c2a9b4c

Browse files
WaVEVtimgraham
authored andcommitted
Fix columns order in aggregation queries
1 parent 5d77cd3 commit c2a9b4c

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

django_mongodb_backend/compiler.py

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

413407
annotation_idx = 1
414408

@@ -427,11 +421,28 @@ def project_field(column):
427421
annotation_idx += 1
428422
return target, column
429423

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

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

django_mongodb_backend/expressions.py

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

155159

156160
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
@@ -87,20 +87,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
8787
# of $setIsSubset must be arrays. Second argument is of type: null"
8888
# https://jira.mongodb.org/browse/SERVER-99186
8989
"model_fields_.test_arrayfield.QueryingTests.test_contained_by_subquery",
90-
# Broken by https://github.com/django/django/commit/65ad4ade74dc9208b9d686a451cd6045df0c9c3a
91-
"aggregation.tests.AggregateTestCase.test_even_more_aggregate",
92-
"aggregation.tests.AggregateTestCase.test_grouped_annotation_in_group_by",
93-
"aggregation.tests.AggregateTestCase.test_non_grouped_annotation_not_in_group_by",
94-
"aggregation_regress.tests.AggregationTests.test_aggregate_fexpr",
95-
"aggregation_regress.tests.AggregationTests.test_values_list_annotation_args_ordering",
96-
"annotations.tests.NonAggregateAnnotationTestCase.test_annotation_subquery_and_aggregate_values_chaining",
97-
"annotations.tests.NonAggregateAnnotationTestCase.test_values_fields_annotations_order",
98-
"queries.test_qs_combinators.QuerySetSetOperationTests.test_union_multiple_models_with_values_and_datetime_annotations",
99-
"queries.test_qs_combinators.QuerySetSetOperationTests.test_union_multiple_models_with_values_list_and_datetime_annotations",
100-
"queries.test_qs_combinators.QuerySetSetOperationTests.test_union_multiple_models_with_values_list_and_annotations",
101-
"queries.test_qs_combinators.QuerySetSetOperationTests.test_union_with_field_and_annotation_values",
102-
"queries.test_qs_combinators.QuerySetSetOperationTests.test_union_with_two_annotated_values_list",
103-
"queries.tests.Queries1Tests.test_union_values_subquery",
10490
# JSONArray not implemented.
10591
"db_functions.json.test_json_array.JSONArrayTests",
10692
# Some usage of prefetch_related() raises "ColPairs is not supported."

tests/indexes_/test_condition.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def test_composite_index(self):
7878
{
7979
"$and": [
8080
{"number": {"$gte": 3}},
81-
{"$or": [{"body": {"$gt": "test1"}}, {"body": {"$in": ["A", "B"]}}]},
81+
{"$or": [{"body": {"$gt": "test1"}}, {"body": {"$in": ("A", "B")}}]},
8282
]
8383
},
8484
)

0 commit comments

Comments
 (0)