Skip to content

Commit 4a70fd0

Browse files
committed
move stddev/variance logic to separate function
1 parent 0359a69 commit 4a70fd0

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

django_mongodb/aggregates.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@
77

88
from .query_utils import process_lhs
99

10-
MONGO_AGGREGATIONS = {
11-
Count: "sum",
12-
StdDev: "stdDev", # Samp or Pop suffix added in aggregate().
13-
Variance: "stdDev", # Likewise.
14-
}
10+
MONGO_AGGREGATIONS = {Count: "sum"}
1511

1612

17-
def aggregate(self, compiler, connection, **extra_context): # noqa: ARG001
13+
def aggregate(self, compiler, connection, operator=None, **extra_context): # noqa: ARG001
1814
if self.filter:
1915
node = self.copy()
2016
node.filter = None
@@ -24,12 +20,7 @@ def aggregate(self, compiler, connection, **extra_context): # noqa: ARG001
2420
else:
2521
node = self
2622
lhs_mql = process_lhs(node, compiler, connection)
27-
operator = MONGO_AGGREGATIONS.get(self.__class__, self.function.lower())
28-
# Add suffixes to StdDev/Variance.
29-
if self.function.endswith("_SAMP"):
30-
operator += "Samp"
31-
elif self.function.endswith("_POP"):
32-
operator += "Pop"
23+
operator = operator or MONGO_AGGREGATIONS.get(self.__class__, self.function.lower())
3324
return {f"${operator}": lhs_mql}
3425

3526

@@ -69,6 +60,16 @@ def count(self, compiler, connection, resolve_inner_expression=False, **extra_co
6960
return {"$add": [{"$size": lhs_mql}, exits_null]}
7061

7162

63+
def stddev_variance(self, compiler, connection, **extra_context):
64+
if self.function.endswith("_SAMP"):
65+
operator = "stdDevSamp"
66+
elif self.function.endswith("_POP"):
67+
operator = "stdDevPop"
68+
return aggregate(self, compiler, connection, operator=operator, **extra_context)
69+
70+
7271
def register_aggregates():
7372
Aggregate.as_mql = aggregate
7473
Count.as_mql = count
74+
StdDev.as_mql = stddev_variance
75+
Variance.as_mql = stddev_variance

0 commit comments

Comments
 (0)