Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for subqueries (Subquery, Exists, and QuerySet as a lookup value) #149

Merged
merged 2 commits into from
Oct 17, 2024

Conversation

WaVEV
Copy link
Collaborator

@WaVEV WaVEV commented Oct 3, 2024

Add support for subqueries

@timgraham timgraham changed the title Add support to subqueries. add support for subqueries (Subquery, Exists, and QuerySet as a lookup value) Oct 3, 2024
@WaVEV WaVEV force-pushed the add-support-to-subquery-expressions branch from c848f5b to 101fe23 Compare October 10, 2024 06:39
@WaVEV WaVEV requested review from timgraham and Jibola and removed request for timgraham October 10, 2024 07:04
@WaVEV WaVEV marked this pull request as ready for review October 10, 2024 07:04
django_mongodb/features.py Outdated Show resolved Hide resolved
django_mongodb/features.py Outdated Show resolved Hide resolved
django_mongodb/features.py Outdated Show resolved Hide resolved
django_mongodb/features.py Outdated Show resolved Hide resolved
django_mongodb/features.py Outdated Show resolved Hide resolved
from_table = next(
e.table_name for alias, e in self.alias_map.items() if self.alias_refcount[alias]
)
subquery.lookup_data = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name lookup_data does not readily indicate its purpose and I don't see any explanatory comments about it.

django_mongodb/expressions.py Outdated Show resolved Hide resolved
@@ -581,7 +613,7 @@ def _get_ordering(self):
return tuple(fields), sort_ordering, tuple(extra_fields)

def get_where(self):
return self.where
return getattr(self, "where", self.query.where)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we are back to getattr() for this... maybe we should drop the get_where() hook we previously added to try to avoid it. Those could be done in a follow up though. For now, perhaps a comment is warranted.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okey.

@WaVEV WaVEV force-pushed the add-support-to-subquery-expressions branch from bf5230b to d97a3d4 Compare October 14, 2024 07:03
README.md Outdated Show resolved Hide resolved
django_mongodb/features.py Outdated Show resolved Hide resolved
django_mongodb/features.py Outdated Show resolved Hide resolved
django_mongodb/query_utils.py Outdated Show resolved Hide resolved
django_mongodb/query.py Outdated Show resolved Hide resolved
Comment on lines 190 to 192
for expr in having_group_by:
group_expressions.add(expr)
if isinstance(self.query.group_by, tuple | list):
group_expressions |= set(self.query.group_by)
elif self.query.group_by is None:
group_expressions = set()
expressions.add(expr)
return self.collapse_group_by(expressions, having_group_by)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of collapse_group_by if we already have having_group_by getting added to the expressions set?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic is taken from here and the idea is to avoid repeating expressions and merge both aggregators.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reading I figured out that we haven't defined allows_group_by_selected_pks so there is not need to call this function (actually this function does nothing)

@@ -269,7 +296,7 @@ def where_node(self, compiler, connection):
raise FullResultSet

if self.negated and mql:
mql = {"$eq": [mql, {"$literal": False}]}
mql = {"$not": mql}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be treated as a separate change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, maybe. But It was hard to debug when I got those {eq: [big_expression, False]}

@WaVEV WaVEV force-pushed the add-support-to-subquery-expressions branch from 96115d6 to 4654f21 Compare October 17, 2024 02:53
@timgraham timgraham force-pushed the add-support-to-subquery-expressions branch from 7e6f435 to e971120 Compare October 17, 2024 18:14
@timgraham timgraham merged commit e971120 into mongodb:main Oct 17, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants