[Django] #33262: Aggregate filtered by an Exists subquery crashes

21 views
Skip to first unread message

Django

unread,
Nov 3, 2021, 10:27:13 AM11/3/21
to django-...@googlegroups.com
#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.

Django

unread,
Nov 3, 2021, 10:48:52 AM11/3/21
to django-...@googlegroups.com
#33262: Aggregate filtered by an Exists subquery crashes
-------------------------------------+-------------------------------------
Reporter: Hannes Ljungberg | Owner: Hannes

| Ljungberg
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hannes Ljungberg):

* has_patch: 0 => 1


Comment:

PR: https://github.com/django/django/pull/15052

--
Ticket URL: <https://code.djangoproject.com/ticket/33262#comment:1>

Django

unread,
Nov 3, 2021, 11:35:06 AM11/3/21
to django-...@googlegroups.com
#33262: Aggregate filtered by an Exists subquery crashes
-------------------------------------+-------------------------------------
Reporter: Hannes Ljungberg | Owner: Hannes

| Ljungberg
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/33262#comment:2>

Django

unread,
Nov 4, 2021, 1:34:46 AM11/4/21
to django-...@googlegroups.com
#33262: Aggregate filtered by an Exists subquery crashes
-------------------------------------+-------------------------------------
Reporter: Hannes Ljungberg | Owner: Hannes

| Ljungberg
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/33262#comment:3>

Django

unread,
Nov 4, 2021, 2:36:20 AM11/4/21
to django-...@googlegroups.com
#33262: Aggregate filtered by an Exists subquery crashes
-------------------------------------+-------------------------------------
Reporter: Hannes Ljungberg | Owner: Hannes
| Ljungberg
Type: Bug | Status: closed

Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"a934d377af2f90329485917fead8723f45d7a56e" a934d377]:
{{{
#!CommitTicketReference repository=""
revision="a934d377af2f90329485917fead8723f45d7a56e"
Fixed #33262 -- Fixed crash of conditional aggregation on Exists().
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33262#comment:4>

Reply all
Reply to author
Forward
0 new messages