1- from django .db .models .aggregates import Aggregate , Count , StdDev , Variance
2- from django .db .models .expressions import Case , Value , When
1+ from django .db .models .aggregates import (
2+ Aggregate ,
3+ AggregateFilter ,
4+ Count ,
5+ StdDev ,
6+ StringAgg ,
7+ Variance ,
8+ )
9+ from django .db .models .expressions import Case , Col , Value , When
310from django .db .models .lookups import IsNull
411
512from .query_utils import process_lhs
@@ -16,7 +23,11 @@ def aggregate(
1623 resolve_inner_expression = False ,
1724 ** extra_context , # noqa: ARG001
1825):
19- if self .filter :
26+ # TODO: isinstance(self.filter, Col) works around failure of
27+ # aggregation.tests.AggregateTestCase.test_distinct_on_aggregate. Is this
28+ # correct?
29+ if self .filter is not None and not isinstance (self .filter , Col ):
30+ # Generate a CASE statement for this aggregate.
2031 node = self .copy ()
2132 node .filter = None
2233 source_expressions = node .get_source_expressions ()
@@ -31,6 +42,10 @@ def aggregate(
3142 return {f"${ operator } " : lhs_mql }
3243
3344
45+ def aggregate_filter (self , compiler , connection , ** extra_context ):
46+ return self .condition .as_mql (compiler , connection , ** extra_context )
47+
48+
3449def count (self , compiler , connection , resolve_inner_expression = False , ** extra_context ): # noqa: ARG001
3550 """
3651 When resolve_inner_expression=True, return the MQL that resolves as a
@@ -72,8 +87,16 @@ def stddev_variance(self, compiler, connection, **extra_context):
7287 return aggregate (self , compiler , connection , operator = operator , ** extra_context )
7388
7489
90+ def string_agg (self , compiler , connection , ** extra_context ): # # noqa: ARG001
91+ from django .db import NotSupportedError
92+
93+ raise NotSupportedError ("StringAgg is not supported." )
94+
95+
7596def register_aggregates ():
7697 Aggregate .as_mql = aggregate
98+ AggregateFilter .as_mql = aggregate_filter
7799 Count .as_mql = count
78100 StdDev .as_mql = stddev_variance
101+ StringAgg .as_mql = string_agg
79102 Variance .as_mql = stddev_variance
0 commit comments