#33262: Aggregate filtered by an Exists subquery crashes
-------------------------------------+-------------------------------------
Reporter: Hannes | Owner: Hannes Ljungberg
Ljungberg |
Type: Bug | Status: assigned
Component: Database | Version: dev
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
For example:
{{{
Book.objects.values("publisher").aggregate(
max_rating=Max(
"rating",
filter=Exists(
Book.authors.through.objects.filter(book=OuterRef("pk")),
),
)
}}}
Will crash with the following traceback:
{{{
Traceback (most recent call last):
File "/tests/django/tests/aggregation/test_filter_argument.py", line
146, in test_filtered_aggregate_with_exists
aggregate = Book.objects.values('publisher').aggregate(
File "/tests/django/django/db/models/query.py", line 405, in aggregate
return query.get_aggregation(self.db, kwargs)
File "/tests/django/django/db/models/sql/query.py", line 501, in
get_aggregation
result = compiler.execute_sql(SINGLE)
File "/tests/django/django/db/models/sql/compiler.py", line 1189, in
execute_sql
sql, params = self.as_sql()
File "/tests/django/django/db/models/sql/compiler.py", line 531, in
as_sql
extra_select, order_by, group_by = self.pre_sql_setup()
File "/tests/django/django/db/models/sql/compiler.py", line 59, in
pre_sql_setup
self.setup_query()
File "/tests/django/django/db/models/sql/compiler.py", line 50, in
setup_query
self.select, self.klass_info, self.annotation_col_map =
self.get_select()
File "/tests/django/django/db/models/sql/compiler.py", line 267, in
get_select
sql, params = self.compile(col)
File "/tests/django/django/db/models/sql/compiler.py", line 463, in
compile
sql, params = node.as_sql(self, self.connection)
File "/tests/django/django/db/models/aggregates.py", line 90, in as_sql
return sql, params + filter_params
TypeError: can only concatenate list (not "tuple") to list
}}}
The following patch should fix the issue:
{{{
diff --git a/django/db/models/aggregates.py
b/django/db/models/aggregates.py
index 596a161669..8c4eae7906 100644
--- a/django/db/models/aggregates.py
+++ b/django/db/models/aggregates.py
@@ -87,7 +87,7 @@ class Aggregate(Func):
compiler, connection, template=template,
filter=filter_sql,
**extra_context
)
- return sql, params + filter_params
+ return sql, (*params, *filter_params)
else:
copy = self.copy()
copy.filter = None
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33262>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/052.2e72037eeaa3aa121a1ef704a5a868fe%40djangoproject.com.