﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33262	Aggregate filtered by an Exists subquery crashes	Hannes Ljungberg	Hannes Ljungberg	"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
}}}"	Bug	closed	Database layer (models, ORM)	dev	Normal	fixed			Ready for checkin	1	0	0	0	0	0
