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 import NotSupportedError
2+ from django .db .models .aggregates import (
3+ Aggregate ,
4+ AggregateFilter ,
5+ Count ,
6+ StdDev ,
7+ StringAgg ,
8+ Variance ,
9+ )
10+ from django .db .models .expressions import Case , Col , Value , When
311from django .db .models .lookups import IsNull
412
513from .query_utils import process_lhs
@@ -16,7 +24,11 @@ def aggregate(
1624 resolve_inner_expression = False ,
1725 ** extra_context , # noqa: ARG001
1826):
19- if self .filter :
27+ # TODO: isinstance(self.filter, Col) works around failure of
28+ # aggregation.tests.AggregateTestCase.test_distinct_on_aggregate. Is this
29+ # correct?
30+ if self .filter is not None and not isinstance (self .filter , Col ):
31+ # Generate a CASE statement for this aggregate.
2032 node = self .copy ()
2133 node .filter = None
2234 source_expressions = node .get_source_expressions ()
@@ -31,6 +43,10 @@ def aggregate(
3143 return {f"${ operator } " : lhs_mql }
3244
3345
46+ def aggregate_filter (self , compiler , connection , ** extra_context ):
47+ return self .condition .as_mql (compiler , connection , ** extra_context )
48+
49+
3450def count (self , compiler , connection , resolve_inner_expression = False , ** extra_context ): # noqa: ARG001
3551 """
3652 When resolve_inner_expression=True, return the MQL that resolves as a
@@ -72,8 +88,14 @@ def stddev_variance(self, compiler, connection, **extra_context):
7288 return aggregate (self , compiler , connection , operator = operator , ** extra_context )
7389
7490
91+ def string_agg (self , compiler , connection , ** extra_context ): # noqa: ARG001
92+ raise NotSupportedError ("StringAgg is not supported." )
93+
94+
7595def register_aggregates ():
7696 Aggregate .as_mql = aggregate
97+ AggregateFilter .as_mql = aggregate_filter
7798 Count .as_mql = count
7899 StdDev .as_mql = stddev_variance
100+ StringAgg .as_mql = string_agg
79101 Variance .as_mql = stddev_variance
0 commit comments