From 9f3cce172f6913c5ac74272fa5fc07f847b4e112 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Fri, 21 May 2021 22:32:16 -0400 Subject: Refs #26430 -- Re-introduced empty aggregation optimization. The introduction of the Expression.empty_aggregate_value interface allows the compilation stage to enable the EmptyResultSet optimization if all the aggregates expressions implement it. This also removes unnecessary RegrCount/Count.convert_value() methods. Disabling the empty result set aggregation optimization when it wasn't appropriate prevented None returned for a Count aggregation value. Thanks Nick Pope for the review. --- docs/ref/models/expressions.txt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'docs/ref') diff --git a/docs/ref/models/expressions.txt b/docs/ref/models/expressions.txt index 9ab502d244..d45cfbe00a 100644 --- a/docs/ref/models/expressions.txt +++ b/docs/ref/models/expressions.txt @@ -410,6 +410,14 @@ The ``Aggregate`` API is as follows: allows passing a ``distinct`` keyword argument. If set to ``False`` (default), ``TypeError`` is raised if ``distinct=True`` is passed. + .. attribute:: empty_aggregate_value + + .. versionadded:: 4.0 + + Override :attr:`~django.db.models.Expression.empty_aggregate_value` to + ``None`` since most aggregate functions result in ``NULL`` when applied + to an empty result set. + The ``expressions`` positional arguments can include expressions, transforms of the model field, or the names of model fields. They will be converted to a string and used as the ``expressions`` placeholder within the ``template``. @@ -950,6 +958,15 @@ calling the appropriate methods on the wrapped expression. in :class:`~django.db.models.expressions.Window`. Defaults to ``False``. + .. attribute:: empty_aggregate_value + + .. versionadded:: 4.0 + + Tells Django which value should be returned when the expression is used + to :meth:`aggregate ` over + an empty result set. Defaults to :py:data:`NotImplemented` which forces + the expression to be computed on the database. + .. method:: resolve_expression(query=None, allow_joins=True, reuse=None, summarize=False, for_save=False) Provides the chance to do any pre-processing or validation of -- cgit v1.3