-
Notifications
You must be signed in to change notification settings - Fork 30
INTPYTHON-736 Convert simple $expr queries to $match queries #373
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
172 changes: 172 additions & 0 deletions
172
django_mongodb_backend/query_conversion/expression_converters.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| class BaseConverter: | ||
| """Base class for $expr to $match converters.""" | ||
|
|
||
| @classmethod | ||
| def convert(cls, expr): | ||
| raise NotImplementedError("Subclasses must implement this method.") | ||
|
|
||
| @classmethod | ||
| def is_simple_value(cls, value): | ||
| """Is the value is a simple type (not a dict)?""" | ||
| if value is None: | ||
| return True | ||
| if isinstance(value, str) and value.startswith("$"): | ||
| return False | ||
| if isinstance(value, (list, tuple, set)): | ||
| return all(cls.is_simple_value(v) for v in value) | ||
| # TODO: Support `$getField` conversion. | ||
| return not isinstance(value, dict) | ||
|
|
||
|
|
||
| class BinaryConverter(BaseConverter): | ||
| """ | ||
| Base class for converting binary operations. | ||
|
|
||
| For example: | ||
| "$expr": { | ||
| {"$gt": ["$price", 100]} | ||
| } | ||
| is converted to: | ||
| {"$gt": ["price", 100]} | ||
| """ | ||
|
|
||
| operator: str | ||
|
|
||
| @classmethod | ||
| def convert(cls, args): | ||
| if isinstance(args, list) and len(args) == 2: | ||
| field_expr, value = args | ||
| # Check if first argument is a simple field reference. | ||
| if ( | ||
| isinstance(field_expr, str) | ||
| and field_expr.startswith("$") | ||
| and cls.is_simple_value(value) | ||
| ): | ||
| field_name = field_expr[1:] # Remove the $ prefix. | ||
| if cls.operator == "$eq": | ||
| return {field_name: value} | ||
| return {field_name: {cls.operator: value}} | ||
| return None | ||
|
|
||
|
|
||
| class EqConverter(BinaryConverter): | ||
| """ | ||
| Convert $eq operation to a $match query. | ||
|
|
||
| For example: | ||
| "$expr": { | ||
| {"$eq": ["$status", "active"]} | ||
| } | ||
| is converted to: | ||
| {"status": "active"} | ||
| """ | ||
|
|
||
| operator = "$eq" | ||
|
|
||
|
|
||
| class GtConverter(BinaryConverter): | ||
| operator = "$gt" | ||
|
|
||
|
|
||
| class GteConverter(BinaryConverter): | ||
| operator = "$gte" | ||
|
|
||
|
|
||
| class LtConverter(BinaryConverter): | ||
| operator = "$lt" | ||
|
|
||
|
|
||
| class LteConverter(BinaryConverter): | ||
| operator = "$lte" | ||
|
|
||
|
|
||
| class InConverter(BaseConverter): | ||
| """ | ||
| Convert $in operation to a $match query. | ||
|
|
||
| For example: | ||
| "$expr": { | ||
| {"$in": ["$category", ["electronics", "books"]]} | ||
| } | ||
| is converted to: | ||
| {"category": {"$in": ["electronics", "books"]}} | ||
| """ | ||
|
|
||
| @classmethod | ||
| def convert(cls, in_args): | ||
| if isinstance(in_args, list) and len(in_args) == 2: | ||
| field_expr, values = in_args | ||
| # Check if first argument is a simple field reference. | ||
| if isinstance(field_expr, str) and field_expr.startswith("$"): | ||
| field_name = field_expr[1:] # Remove the $ prefix. | ||
| if isinstance(values, (list, tuple, set)) and all( | ||
| cls.is_simple_value(v) for v in values | ||
| ): | ||
| return {field_name: {"$in": values}} | ||
| return None | ||
|
|
||
|
|
||
| class LogicalConverter(BaseConverter): | ||
| """ | ||
| Base class for converting logical operations to a $match query. | ||
|
|
||
| For example: | ||
| "$expr": { | ||
| "$or": [ | ||
| {"$eq": ["$status", "active"]}, | ||
| {"$in": ["$category", ["electronics", "books"]]}, | ||
| ] | ||
| } | ||
| is converted to: | ||
| "$or": [ | ||
| {"status": "active"}, | ||
| {"category": {"$in": ["electronics", "books"]}}, | ||
| ] | ||
| """ | ||
|
|
||
| @classmethod | ||
| def convert(cls, combined_conditions): | ||
| if isinstance(combined_conditions, list): | ||
| optimized_conditions = [] | ||
| for condition in combined_conditions: | ||
| if isinstance(condition, dict) and len(condition) == 1: | ||
| if optimized_condition := convert_expression(condition): | ||
| optimized_conditions.append(optimized_condition) | ||
| else: | ||
| # Any failure should stop optimization. | ||
| return None | ||
| if optimized_conditions: | ||
| return {cls._logical_op: optimized_conditions} | ||
| return None | ||
|
|
||
|
|
||
| class OrConverter(LogicalConverter): | ||
| _logical_op = "$or" | ||
|
|
||
|
|
||
| class AndConverter(LogicalConverter): | ||
| _logical_op = "$and" | ||
|
|
||
|
|
||
| OPTIMIZABLE_OPS = { | ||
| "$eq": EqConverter, | ||
| "$in": InConverter, | ||
| "$and": AndConverter, | ||
| "$or": OrConverter, | ||
| "$gt": GtConverter, | ||
| "$gte": GteConverter, | ||
| "$lt": LtConverter, | ||
| "$lte": LteConverter, | ||
| } | ||
|
|
||
|
|
||
| def convert_expression(expr): | ||
| """ | ||
| Optimize MQL by converting an $expr condition to $match. Return the $match | ||
| MQL, or None if not optimizable. | ||
| """ | ||
| if isinstance(expr, dict) and len(expr) == 1: | ||
| op = next(iter(expr.keys())) | ||
| if op in OPTIMIZABLE_OPS: | ||
| return OPTIMIZABLE_OPS[op].convert(expr[op]) | ||
| return None | ||
73 changes: 73 additions & 0 deletions
73
django_mongodb_backend/query_conversion/query_optimizer.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| from .expression_converters import convert_expression | ||
|
|
||
|
|
||
| def convert_expr_to_match(query): | ||
| """ | ||
| Optimize an MQL query by converting conditions into a list of $match | ||
| stages. | ||
| """ | ||
| if "$expr" not in query: | ||
| return [query] | ||
| if query["$expr"] == {}: | ||
| return [{"$match": {}}] | ||
| return _process_expression(query["$expr"]) | ||
|
|
||
|
|
||
| def _process_expression(expr): | ||
| """Process an expression and extract optimizable conditions.""" | ||
| match_conditions = [] | ||
| remaining_conditions = [] | ||
| if isinstance(expr, dict): | ||
| has_and = "$and" in expr | ||
| has_or = "$or" in expr | ||
| # Do a top-level check for $and or $or because these should inform. | ||
| # If they fail, they should failover to a remaining conditions list. | ||
| # There's probably a better way to do this. | ||
| if has_and: | ||
| and_match_conditions = _process_logical_conditions("$and", expr["$and"]) | ||
| match_conditions.extend(and_match_conditions) | ||
| if has_or: | ||
| or_match_conditions = _process_logical_conditions("$or", expr["$or"]) | ||
| match_conditions.extend(or_match_conditions) | ||
| if not has_and and not has_or: | ||
| # Process single condition. | ||
| if optimized := convert_expression(expr): | ||
| match_conditions.append({"$match": optimized}) | ||
| else: | ||
| remaining_conditions.append({"$match": {"$expr": expr}}) | ||
| else: | ||
| # Can't optimize. | ||
| remaining_conditions.append({"$expr": expr}) | ||
| return match_conditions + remaining_conditions | ||
|
|
||
|
|
||
| def _process_logical_conditions(logical_op, logical_conditions): | ||
| """Process conditions within a logical array.""" | ||
| optimized_conditions = [] | ||
| match_conditions = [] | ||
| remaining_conditions = [] | ||
| for condition in logical_conditions: | ||
| _remaining_conditions = [] | ||
| if isinstance(condition, dict): | ||
| if optimized := convert_expression(condition): | ||
| optimized_conditions.append(optimized) | ||
| else: | ||
| _remaining_conditions.append(condition) | ||
| else: | ||
| _remaining_conditions.append(condition) | ||
| if _remaining_conditions: | ||
| # Any expressions that can't be optimized must remain in a $expr | ||
| # that preserves the logical operator. | ||
| if len(_remaining_conditions) > 1: | ||
| remaining_conditions.append({"$expr": {logical_op: _remaining_conditions}}) | ||
| else: | ||
| remaining_conditions.append({"$expr": _remaining_conditions[0]}) | ||
| if optimized_conditions: | ||
| optimized_conditions.extend(remaining_conditions) | ||
| if len(optimized_conditions) > 1: | ||
| match_conditions.append({"$match": {logical_op: optimized_conditions}}) | ||
| else: | ||
| match_conditions.append({"$match": optimized_conditions[0]}) | ||
| else: | ||
| match_conditions.append({"$match": {logical_op: remaining_conditions}}) | ||
| return match_conditions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What if some conditions pass the
ifand others don'tThere 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.
I think isn't possible 🤔 . The way that the queries are made any literal like True or False are handled by a dict. Is there any condition/operator that needs more than one key?
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.
Emanuel, I'm not sure exactly what you meant here. Feel free to open a follow up PR.