-
Notifications
You must be signed in to change notification settings - Fork 31
INTPYTHON-833 Remove $facet from aggregation pipelines #449
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,10 @@ def __init__(self, *args, **kwargs): | |
| self.subqueries = [] | ||
| # Atlas search stage. | ||
| self.search_pipeline = [] | ||
| # Does the aggregation have no GROUP BY fields and needs wrapping? | ||
| self.wrap_for_global_aggregation = False | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need a comment to explain what "global aggregation" is.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm still not sure what "global" is meant to signify. Would it have a different meaning to say "wrap_for_aggregation". The comment should explain what it means to wrap a query.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 yes, Well maybe I am too SQL in my mind. The idea of global is when a query has the following shape: SELECT avg(age)
FROM people;So it is a "global" aggregation, because there isn't any group by expression. how can we call those kind of queries? |
||
| # The MQL equivalent to a SQL HAVING clause. | ||
| self.having_match_mql = None | ||
|
|
||
| def _get_group_alias_column(self, expr, annotation_group_idx): | ||
| """Generate a dummy field for use in the ids fields in $group.""" | ||
|
|
@@ -234,21 +238,10 @@ def _build_aggregation_pipeline(self, ids, group): | |
| """Build the aggregation pipeline for grouping.""" | ||
| pipeline = [] | ||
| if not ids: | ||
| group["_id"] = None | ||
| pipeline.append({"$facet": {"group": [{"$group": group}]}}) | ||
| pipeline.append( | ||
| { | ||
| "$addFields": { | ||
| key: { | ||
| "$getField": { | ||
| "input": {"$arrayElemAt": ["$group", 0]}, | ||
| "field": key, | ||
| } | ||
| } | ||
| for key in group | ||
| } | ||
| } | ||
| ) | ||
| pipeline.append({"$group": {"_id": None, **group}}) | ||
| # Apply a global aggregation if there are no ids and no having | ||
| # clause. | ||
| self.wrap_for_global_aggregation = not bool(self.having) | ||
| else: | ||
| group["_id"] = ids | ||
| pipeline.append({"$group": group}) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"introduced upstream" could be clarified. When I think "upstream" I think of Django itself. I guess you mean "upstream" in the query generation process. Perhaps it's something like "introduced by aggregation wrapping or a subquery wrapping pipeline".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, in the query generation. Ok, will change.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like:
The extra placeholder is the document inserted with unionWith when the discussed flag is on.