Fixed #30120 -- Fixed invalid SQL in distinct aggregate.

Regression in bc05547cd8 (refs #28658).
This commit is contained in:
Simon Charette 2019-01-21 08:24:32 -06:00 committed by Tim Graham
parent 838e432e3e
commit 65858119d2
2 changed files with 8 additions and 1 deletions

View File

@ -68,7 +68,7 @@ class Aggregate(Func):
return []
def as_sql(self, compiler, connection, **extra_context):
extra_context['distinct'] = 'DISTINCT' if self.distinct else ''
extra_context['distinct'] = 'DISTINCT ' if self.distinct else ''
if self.filter:
if connection.features.supports_aggregate_filter_clause:
filter_sql, filter_params = self.filter.as_sql(compiler, connection)

View File

@ -8,6 +8,7 @@ from django.db.models import (
Avg, Count, DecimalField, DurationField, F, FloatField, Func, IntegerField,
Max, Min, Sum, Value,
)
from django.db.models.expressions import Case, When
from django.test import TestCase
from django.test.utils import Approximate, CaptureQueriesContext
from django.utils import timezone
@ -395,6 +396,12 @@ class AggregateTestCase(TestCase):
sql = ctx.captured_queries[0]['sql']
self.assertIn('SELECT COUNT(*) ', sql)
def test_count_distinct_expression(self):
aggs = Book.objects.aggregate(
distinct_ratings=Count(Case(When(pages__gt=300, then='rating')), distinct=True),
)
self.assertEqual(aggs['distinct_ratings'], 4)
def test_non_grouped_annotation_not_in_group_by(self):
"""
An annotation not included in values() before an aggregate should be