summaryrefslogtreecommitdiff
path: root/django/contrib/postgres
diff options
context:
space:
mode:
authorChris Muthig <camuthig@gmail.com>2024-04-03 16:09:44 -0600
committernessita <124304+nessita@users.noreply.github.com>2024-04-25 17:40:03 -0300
commitc8df2f994130d74ec35d32a36e30aad7d6ea8e3a (patch)
tree98e2f35ffa12db04cd7869bddb0ef78bdb8195b3 /django/contrib/postgres
parent42b567ab4c5bfb1bbd3e629b1079271c5ae44ea0 (diff)
Fixed #35339 -- Fixed PostgreSQL aggregate's filter and order_by params order.
Updated OrderableAggMixin.as_sql() to separate the order_by parameters from the filter parameters. Previously, the parameters and SQL were calculated by the Aggregate parent class, resulting in a mixture of order_by and filter parameters. Thanks Simon Charette for the review.
Diffstat (limited to 'django/contrib/postgres')
-rw-r--r--django/contrib/postgres/aggregates/mixins.py27
1 files changed, 21 insertions, 6 deletions
diff --git a/django/contrib/postgres/aggregates/mixins.py b/django/contrib/postgres/aggregates/mixins.py
index 527626da4c..d6ff535158 100644
--- a/django/contrib/postgres/aggregates/mixins.py
+++ b/django/contrib/postgres/aggregates/mixins.py
@@ -1,3 +1,4 @@
+from django.core.exceptions import FullResultSet
from django.db.models.expressions import OrderByList
@@ -24,9 +25,23 @@ class OrderableAggMixin:
return super().set_source_expressions(exprs)
def as_sql(self, compiler, connection):
- if self.order_by is not None:
- order_by_sql, order_by_params = compiler.compile(self.order_by)
- else:
- order_by_sql, order_by_params = "", ()
- sql, sql_params = super().as_sql(compiler, connection, ordering=order_by_sql)
- return sql, (*sql_params, *order_by_params)
+ *source_exprs, filtering_expr, ordering_expr = self.get_source_expressions()
+
+ order_by_sql = ""
+ order_by_params = []
+ if ordering_expr is not None:
+ order_by_sql, order_by_params = compiler.compile(ordering_expr)
+
+ filter_params = []
+ if filtering_expr is not None:
+ try:
+ _, filter_params = compiler.compile(filtering_expr)
+ except FullResultSet:
+ pass
+
+ source_params = []
+ for source_expr in source_exprs:
+ source_params += compiler.compile(source_expr)[1]
+
+ sql, _ = super().as_sql(compiler, connection, ordering=order_by_sql)
+ return sql, (*source_params, *order_by_params, *filter_params)