|
| 1 | +from copy import deepcopy |
| 2 | + |
| 3 | +from django.db.models.aggregates import Aggregate, Count, StdDev, Variance |
| 4 | +from django.db.models.expressions import Case, Value, When |
| 5 | +from django.db.models.lookups import Exact |
| 6 | +from django.db.models.sql.where import WhereNode |
| 7 | + |
| 8 | +from .query_utils import process_lhs |
| 9 | + |
| 10 | +# Aggregates whose MongoDB aggregation name differ from Aggregate.function.lower(). |
| 11 | +MONGO_AGGREGATIONS = {Count: "sum"} |
| 12 | + |
| 13 | + |
| 14 | +def aggregate( |
| 15 | + self, |
| 16 | + compiler, |
| 17 | + connection, |
| 18 | + operator=None, |
| 19 | + resolve_inner_expression=False, |
| 20 | + **extra_context, # noqa: ARG001 |
| 21 | +): |
| 22 | + if self.filter: |
| 23 | + node = self.copy() |
| 24 | + node.filter = None |
| 25 | + source_expressions = node.get_source_expressions() |
| 26 | + condition = When(self.filter, then=source_expressions[0]) |
| 27 | + node.set_source_expressions([Case(condition)] + source_expressions[1:]) |
| 28 | + else: |
| 29 | + node = self |
| 30 | + lhs_mql = process_lhs(node, compiler, connection) |
| 31 | + if resolve_inner_expression: |
| 32 | + return lhs_mql |
| 33 | + operator = operator or MONGO_AGGREGATIONS.get(self.__class__, self.function.lower()) |
| 34 | + return {f"${operator}": lhs_mql} |
| 35 | + |
| 36 | + |
| 37 | +def count(self, compiler, connection, resolve_inner_expression=False, **extra_context): # noqa: ARG001 |
| 38 | + """ |
| 39 | + When resolve_inner_expression=True, return the MQL that resolves as a |
| 40 | + value. This is used to count different elements, so the inner values are |
| 41 | + returned to be pushed into a set. |
| 42 | + """ |
| 43 | + if not self.distinct or resolve_inner_expression: |
| 44 | + if self.filter: |
| 45 | + node = self.copy() |
| 46 | + node.filter = None |
| 47 | + source_expressions = node.get_source_expressions() |
| 48 | + filter_ = deepcopy(self.filter) |
| 49 | + filter_.add( |
| 50 | + WhereNode([Exact(source_expressions[0], Value(None))], negated=True), |
| 51 | + filter_.default, |
| 52 | + ) |
| 53 | + condition = When(filter_, then=Value(1)) |
| 54 | + node.set_source_expressions([Case(condition)] + source_expressions[1:]) |
| 55 | + inner_expression = process_lhs(node, compiler, connection) |
| 56 | + else: |
| 57 | + lhs_mql = process_lhs(self, compiler, connection) |
| 58 | + null_cond = {"$in": [{"$type": lhs_mql}, ["missing", "null"]]} |
| 59 | + inner_expression = { |
| 60 | + "$cond": {"if": null_cond, "then": None, "else": lhs_mql if self.distinct else 1} |
| 61 | + } |
| 62 | + if resolve_inner_expression: |
| 63 | + return inner_expression |
| 64 | + return {"$sum": inner_expression} |
| 65 | + # If distinct=True or resolve_inner_expression=False, sum the size of the |
| 66 | + # set. |
| 67 | + lhs_mql = process_lhs(self, compiler, connection) |
| 68 | + # None shouldn't be counted, so subtract 1 if it's present. |
| 69 | + exits_null = {"$cond": {"if": {"$in": [{"$literal": None}, lhs_mql]}, "then": -1, "else": 0}} |
| 70 | + return {"$add": [{"$size": lhs_mql}, exits_null]} |
| 71 | + |
| 72 | + |
| 73 | +def stddev_variance(self, compiler, connection, **extra_context): |
| 74 | + if self.function.endswith("_SAMP"): |
| 75 | + operator = "stdDevSamp" |
| 76 | + elif self.function.endswith("_POP"): |
| 77 | + operator = "stdDevPop" |
| 78 | + return aggregate(self, compiler, connection, operator=operator, **extra_context) |
| 79 | + |
| 80 | + |
| 81 | +def register_aggregates(): |
| 82 | + Aggregate.as_mql = aggregate |
| 83 | + Count.as_mql = count |
| 84 | + StdDev.as_mql = stddev_variance |
| 85 | + Variance.as_mql = stddev_variance |
0 commit comments