Skip to content

Commit 2130902

Browse files
committed
WIP.
1 parent a05cae7 commit 2130902

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

django_mongodb_backend/compiler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def pre_sql_setup(self, with_col_aliases=False):
327327
pipeline = self._build_aggregation_pipeline(ids, group)
328328
if self.having:
329329
having = self.having.replace_expressions(all_replacements).as_mql(
330-
self, self.connection
330+
self, self.connection, as_path=True
331331
)
332332
# Add HAVING subqueries.
333333
for query in self.subqueries or ():
@@ -643,7 +643,9 @@ def get_combinator_queries(self):
643643
for alias, expr in self.columns:
644644
# Unfold foreign fields.
645645
if isinstance(expr, Col) and expr.alias != self.collection_name:
646-
ids[expr.alias][expr.target.column] = expr.as_mql(self, self.connection)
646+
ids[expr.alias][expr.target.column] = expr.as_mql(
647+
self, self.connection, as_path=True
648+
)
647649
else:
648650
ids[alias] = f"${alias}"
649651
# Convert defaultdict to dict so it doesn't appear as

django_mongodb_backend/expressions/builtins.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
# EXTRA IS TOTALLY IGNORED
3232
# shall check if we could optimize match here
33-
def case(self, compiler, connection, **extra): # noqa: ARG001
33+
def case(self, compiler, connection, as_path=False):
3434
case_parts = []
3535
for case in self.cases:
3636
case_mql = {}
@@ -47,12 +47,16 @@ def case(self, compiler, connection, **extra): # noqa: ARG001
4747
default_mql = self.default.as_mql(compiler, connection)
4848
if not case_parts:
4949
return default_mql
50-
return {
50+
expr = {
5151
"$switch": {
5252
"branches": case_parts,
5353
"default": default_mql,
5454
}
5555
}
56+
if as_path:
57+
return {"$expr": expr}
58+
59+
return expr
5660

5761

5862
def col(self, compiler, connection, as_path=False): # noqa: ARG001
@@ -98,6 +102,7 @@ def expression_wrapper(self, compiler, connection, **extra):
98102

99103

100104
def negated_expression(self, compiler, connection, **extra):
105+
# review
101106
return {"$not": expression_wrapper(self, compiler, connection, **extra)}
102107

103108

@@ -175,10 +180,13 @@ def star(self, compiler, connection, **extra): # noqa: ARG001
175180
return {"$literal": True}
176181

177182

178-
def subquery(self, compiler, connection, get_wrapping_pipeline=None, **extra):
179-
return self.query.as_mql(
180-
compiler, connection, get_wrapping_pipeline=get_wrapping_pipeline, **extra
183+
def subquery(self, compiler, connection, get_wrapping_pipeline=None, as_path=False):
184+
expr = self.query.as_mql(
185+
compiler, connection, get_wrapping_pipeline=get_wrapping_pipeline, as_path=False
181186
)
187+
if as_path:
188+
return {"$expr": expr}
189+
return expr
182190

183191

184192
def exists(self, compiler, connection, get_wrapping_pipeline=None, as_path=False, **extra):
@@ -194,7 +202,7 @@ def exists(self, compiler, connection, get_wrapping_pipeline=None, as_path=False
194202
except EmptyResultSet:
195203
return Value(False).as_mql(compiler, connection)
196204
if as_path:
197-
return connection.mongo_operators_match["isnull"](lhs_mql, False)
205+
return {"$expr": connection.mongo_operators_match["isnull"](lhs_mql, False)}
198206
return connection.mongo_operators_expr["isnull"](lhs_mql, False)
199207

200208

django_mongodb_backend/query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def _get_reroot_replacements(expression):
194194
if extra:
195195
replacements = _get_reroot_replacements(extra)
196196
extra_conditions.append(
197-
extra.replace_expressions(replacements).as_mql(compiler, connection)
197+
extra.replace_expressions(replacements).as_mql(compiler, connection, as_path=True)
198198
)
199199
# pushed_filter_expression is a Where expression from the outer WHERE
200200
# clause that involves fields from the joined (right-hand) table and

0 commit comments

Comments
 (0)