-
Notifications
You must be signed in to change notification settings - Fork 29
INTPYTHON-751 Make query generation omit $expr unless required #396
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
Open
WaVEV
wants to merge
14
commits into
mongodb:main
Choose a base branch
from
WaVEV:lookup-refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f4566b0
Functional approach solution
WaVEV b72c82a
Object-oriented approach solution
WaVEV 0a1bc45
Edits.
WaVEV 90bf7c1
Add example generated query test.
WaVEV a5af8e2
Refactor.
WaVEV f9efbdc
Update django_mongodb_backend/functions.py
WaVEV dc9536f
Edits.
WaVEV 8063b4b
Fix.
WaVEV 6c1c766
Remove query converter
WaVEV efaab2d
Refactor.
WaVEV d7fd359
Simplify if.
WaVEV 4b4cf53
Handle empty set or full set in range queries.
WaVEV d0c0d3f
Add mql check in EMF and EMFA unit test.
WaVEV 679f3d7
Rename method.
WaVEV 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
Some comments aren't visible on the classic Files Changed page.
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
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 |
---|---|---|
|
@@ -2,7 +2,8 @@ | |
import logging | ||
import os | ||
|
||
from django.core.exceptions import ImproperlyConfigured | ||
from bson import Decimal128 | ||
from django.core.exceptions import EmptyResultSet, FullResultSet, ImproperlyConfigured | ||
from django.db import DEFAULT_DB_ALIAS | ||
from django.db.backends.base.base import BaseDatabaseWrapper | ||
from django.db.backends.utils import debug_transaction | ||
|
@@ -20,7 +21,7 @@ | |
from .features import DatabaseFeatures | ||
from .introspection import DatabaseIntrospection | ||
from .operations import DatabaseOperations | ||
from .query_utils import regex_match | ||
from .query_utils import regex_expr, regex_match | ||
from .schema import DatabaseSchemaEditor | ||
from .utils import OperationDebugWrapper | ||
from .validation import DatabaseValidation | ||
|
@@ -108,7 +109,12 @@ def _isnull_operator(a, b): | |
} | ||
return is_null if b else {"$not": is_null} | ||
|
||
mongo_operators = { | ||
def _isnull_operator_match(a, b): | ||
if b: | ||
return {"$or": [{a: {"$exists": False}}, {a: None}]} | ||
return {"$and": [{a: {"$exists": True}}, {a: {"$ne": None}}]} | ||
|
||
mongo_expr_operators = { | ||
"exact": lambda a, b: {"$eq": [a, b]}, | ||
"gt": lambda a, b: {"$gt": [a, b]}, | ||
"gte": lambda a, b: {"$gte": [a, b]}, | ||
|
@@ -118,19 +124,64 @@ def _isnull_operator(a, b): | |
"lte": lambda a, b: { | ||
"$and": [{"$lte": [a, b]}, DatabaseWrapper._isnull_operator(a, False)] | ||
}, | ||
"in": lambda a, b: {"$in": [a, b]}, | ||
"in": lambda a, b: {"$in": (a, b)}, | ||
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. Is this to match the expected MQL in a test? 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 |
||
"isnull": _isnull_operator, | ||
"range": lambda a, b: { | ||
"$and": [ | ||
{"$or": [DatabaseWrapper._isnull_operator(b[0], True), {"$gte": [a, b[0]]}]}, | ||
{"$or": [DatabaseWrapper._isnull_operator(b[1], True), {"$lte": [a, b[1]]}]}, | ||
] | ||
}, | ||
"iexact": lambda a, b: regex_match(a, ("^", b, {"$literal": "$"}), insensitive=True), | ||
"startswith": lambda a, b: regex_match(a, ("^", b)), | ||
"istartswith": lambda a, b: regex_match(a, ("^", b), insensitive=True), | ||
"endswith": lambda a, b: regex_match(a, (b, {"$literal": "$"})), | ||
"iendswith": lambda a, b: regex_match(a, (b, {"$literal": "$"}), insensitive=True), | ||
"iexact": lambda a, b: regex_expr(a, ("^", b, {"$literal": "$"}), insensitive=True), | ||
"startswith": lambda a, b: regex_expr(a, ("^", b)), | ||
"istartswith": lambda a, b: regex_expr(a, ("^", b), insensitive=True), | ||
"endswith": lambda a, b: regex_expr(a, (b, {"$literal": "$"})), | ||
"iendswith": lambda a, b: regex_expr(a, (b, {"$literal": "$"}), insensitive=True), | ||
"contains": lambda a, b: regex_expr(a, b), | ||
"icontains": lambda a, b: regex_expr(a, b, insensitive=True), | ||
"regex": lambda a, b: regex_expr(a, b), | ||
"iregex": lambda a, b: regex_expr(a, b, insensitive=True), | ||
} | ||
|
||
def range_match(a, b): | ||
conditions = [] | ||
start, end = b | ||
if start is not None: | ||
conditions.append({a: {"$gte": b[0]}}) | ||
if end is not None: | ||
conditions.append({a: {"$lte": b[1]}}) | ||
if start is not None and end is not None: | ||
if isinstance(start, Decimal128): | ||
start = start.to_decimal() | ||
if isinstance(end, Decimal128): | ||
end = end.to_decimal() | ||
if start > end: | ||
raise EmptyResultSet | ||
if not conditions: | ||
raise FullResultSet | ||
return {"$and": conditions} | ||
|
||
# match, path, find? don't know which name use. | ||
mongo_match_operators = { | ||
"exact": lambda a, b: {a: b}, | ||
"gt": lambda a, b: {a: {"$gt": b}}, | ||
"gte": lambda a, b: {a: {"$gte": b}}, | ||
# MongoDB considers null less than zero. Exclude null values to match | ||
# SQL behavior. | ||
"lt": lambda a, b: { | ||
"$and": [{a: {"$lt": b}}, DatabaseWrapper._isnull_operator_match(a, False)] | ||
}, | ||
"lte": lambda a, b: { | ||
"$and": [{a: {"$lte": b}}, DatabaseWrapper._isnull_operator_match(a, False)] | ||
}, | ||
"in": lambda a, b: {a: {"$in": tuple(b)}}, | ||
"isnull": _isnull_operator_match, | ||
"range": range_match, | ||
"iexact": lambda a, b: regex_match(a, f"^{b}$", insensitive=True), | ||
"startswith": lambda a, b: regex_match(a, f"^{b}"), | ||
"istartswith": lambda a, b: regex_match(a, f"^{b}", insensitive=True), | ||
"endswith": lambda a, b: regex_match(a, f"{b}$"), | ||
"iendswith": lambda a, b: regex_match(a, f"{b}$", insensitive=True), | ||
"contains": lambda a, b: regex_match(a, b), | ||
"icontains": lambda a, b: regex_match(a, b, insensitive=True), | ||
"regex": lambda a, b: regex_match(a, b), | ||
|
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
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
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
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
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.
I see we have
as_mql_expr()
,as_mql_path()
, andas_mql(..., as_path=...)
. If this is the way we keep it, it would be good to explain in the design document which objects (aggregate, func, expression, etc.) get which.I wonder about renaming
as_mql_expr()
oras_mql_path()
toas_mql()
(i.e. treating one of paths as the default). Do you think it would be more or less confusing?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, that was the idea. I’ll explain it in the docs, and we might also consider renaming some methods. The core concept is:
as_mql
method.as_mql
directly, so those methods don’t follow the common expression flow.as_mql
is a composite function that delegates toas_path
oras_expr
when applied.base_expression.as_mql
method controls when these are called and performs boilerplate checks to prevent nesting anexpr
inside anotherexpr
(a MongoDB 6 restriction).In short: every object has
as_mql
. Some also defineas_path
andas_expr
. Thebase_expression
coordinates how these methods are used, except for cases whereas_mql
is defined directly.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.
Doc here: link