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.
This commit is contained in:
Simon Charette 2021-05-21 22:32:16 -04:00 committed by Mariusz Felisiak
parent f3112fde98
commit 9f3cce172f
8 changed files with 101 additions and 34 deletions

View file

@ -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 <django.db.models.query.QuerySet.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