summaryrefslogtreecommitdiff
path: root/django/db/models/sql/compiler.py
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2023-03-28 00:13:00 -0400
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-07-03 16:36:25 +0200
commit65ad4ade74dc9208b9d686a451cd6045df0c9c3a (patch)
tree641c0c1e6264a3f23212ee383b46c25fd4d0f0a1 /django/db/models/sql/compiler.py
parent2e47dde438d689199934bca0967152a3b0e8a95f (diff)
Refs #28900 -- Made SELECT respect the order specified by values(*selected).
Previously the order was always extra_fields + model_fields + annotations with respective local ordering inferred from the insertion order of *selected. This commits introduces a new `Query.selected` propery that keeps tracks of the global select order as specified by on values assignment. This is crucial feature to allow the combination of queries mixing annotations and table references. It also allows the removal of the re-ordering shenanigans perform by ValuesListIterable in order to re-map the tuples returned from the database backend to the order specified by values_list() as they'll be in the right order at query compilation time. Refs #28553 as the initially reported issue that was only partially fixed for annotations by d6b6e5d0fd4e6b6d0183b4cf6e4bd4f9afc7bf67. Thanks Mariusz Felisiak and Sarah Boyce for review.
Diffstat (limited to 'django/db/models/sql/compiler.py')
-rw-r--r--django/db/models/sql/compiler.py45
1 files changed, 30 insertions, 15 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index 7377e555c3..d606505cdf 100644
--- a/django/db/models/sql/compiler.py
+++ b/django/db/models/sql/compiler.py
@@ -247,11 +247,6 @@ class SQLCompiler:
select = []
klass_info = None
annotations = {}
- select_idx = 0
- for alias, (sql, params) in self.query.extra_select.items():
- annotations[alias] = select_idx
- select.append((RawSQL(sql, params), alias))
- select_idx += 1
assert not (self.query.select and self.query.default_cols)
select_mask = self.query.get_select_mask()
if self.query.default_cols:
@@ -261,19 +256,39 @@ class SQLCompiler:
# any model.
cols = self.query.select
if cols:
- select_list = []
- for col in cols:
- select_list.append(select_idx)
- select.append((col, None))
- select_idx += 1
klass_info = {
"model": self.query.model,
- "select_fields": select_list,
+ "select_fields": list(
+ range(
+ len(self.query.extra_select),
+ len(self.query.extra_select) + len(cols),
+ )
+ ),
}
- for alias, annotation in self.query.annotation_select.items():
- annotations[alias] = select_idx
- select.append((annotation, alias))
- select_idx += 1
+ selected = []
+ if self.query.selected is None:
+ selected = [
+ *(
+ (alias, RawSQL(*args))
+ for alias, args in self.query.extra_select.items()
+ ),
+ *((None, col) for col in cols),
+ *self.query.annotation_select.items(),
+ ]
+ else:
+ for alias, expression in self.query.selected.items():
+ # Reference to an annotation.
+ if isinstance(expression, str):
+ expression = self.query.annotations[expression]
+ # Reference to a column.
+ elif isinstance(expression, int):
+ expression = cols[expression]
+ selected.append((alias, expression))
+
+ for select_idx, (alias, expression) in enumerate(selected):
+ if alias:
+ annotations[alias] = select_idx
+ select.append((expression, alias))
if self.query.select_related:
related_klass_infos = self.get_related_selections(select, select_mask)