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
3
10
from django .db .models .lookups import IsNull
4
11
5
12
from .query_utils import process_lhs
@@ -16,7 +23,11 @@ def aggregate(
16
23
resolve_inner_expression = False ,
17
24
** extra_context , # noqa: ARG001
18
25
):
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.
20
31
node = self .copy ()
21
32
node .filter = None
22
33
source_expressions = node .get_source_expressions ()
@@ -31,6 +42,10 @@ def aggregate(
31
42
return {f"${ operator } " : lhs_mql }
32
43
33
44
45
+ def aggregate_filter (self , compiler , connection , ** extra_context ):
46
+ return self .condition .as_mql (compiler , connection , ** extra_context )
47
+
48
+
34
49
def count (self , compiler , connection , resolve_inner_expression = False , ** extra_context ): # noqa: ARG001
35
50
"""
36
51
When resolve_inner_expression=True, return the MQL that resolves as a
@@ -72,8 +87,16 @@ def stddev_variance(self, compiler, connection, **extra_context):
72
87
return aggregate (self , compiler , connection , operator = operator , ** extra_context )
73
88
74
89
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
+
75
96
def register_aggregates ():
76
97
Aggregate .as_mql = aggregate
98
+ AggregateFilter .as_mql = aggregate_filter
77
99
Count .as_mql = count
78
100
StdDev .as_mql = stddev_variance
101
+ StringAgg .as_mql = string_agg
79
102
Variance .as_mql = stddev_variance
0 commit comments