Skip to content

fix pattern lookup match escaping on F expressions #61

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 1 commit into from
Jul 5, 2024

Conversation

timgraham
Copy link
Collaborator

Escape $regexMatch special characters instead of LIKE special chars.

This is complemented by a patch to the Django fork which fails unless each of these replacements are made:

diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py
index 36624649e5..60c88e42e2 100644
--- a/tests/expressions/tests.py
+++ b/tests/expressions/tests.py
@@ -1229,6 +1229,17 @@ class ExpressionsTests(TestCase):
                 Employee(firstname="Jean-Claude", lastname="Claude%"),
                 Employee(firstname="Johnny", lastname="Joh\\n"),
                 Employee(firstname="Johnny", lastname="_ohn"),
+                Employee(firstname="Johnny", lastname="^Joh"),
+                Employee(firstname="Johnny", lastname="Johnny$"),
+                Employee(firstname="Johnny", lastname="Joh."),
+                Employee(firstname="Johnny", lastname="[J]ohnny"),
+                Employee(firstname="Johnny", lastname="(J)ohnny"),
+                Employee(firstname="Johnny", lastname="J*ohnny"),
+                Employee(firstname="Johnny", lastname="J+ohnny"),
+                Employee(firstname="Johnny", lastname="J?ohnny"),
+                Employee(firstname="Johnny", lastname="J{1}ohnny"),
+                Employee(firstname="Johnny", lastname="J|ohnny"),
+                Employee(firstname="Johnny", lastname="J-ohnny"),
             ]
         )
         claude = Employee.objects.create(firstname="Jean-Claude", lastname="Claude")

@WaVEV
Copy link
Collaborator

WaVEV commented Jun 29, 2024

Is it ready to review?

Comment on lines +28 to +63
REGEX_MATCH_ESCAPE_CHARS = (
("\\", r"\\"), # general escape character
("^", r"\^"), # start of string
({"$literal": "$"}, r"\$"), # end of string
(".", r"\."), # match any character
("[", r"\["), # start character class definition
("|", r"\|"), # start of alternative branch
("(", r"\("), # start group or control verb
(")", r"\)"), # end group or control verb
("*", r"\*"), # 0 or more quantifier
("+", r"\+"), # 1 or more quantifier
("?", r"\?"), # 0 or 1 quantifier
("{", r"\}"), # start min/max quantifier
)
Copy link
Contributor

@Jibola Jibola Jul 1, 2024

Choose a reason for hiding this comment

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

So this will trigger 13 $replaceAll calls when doing lookups?

Copy link
Collaborator

@WaVEV WaVEV Jul 1, 2024

Choose a reason for hiding this comment

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

is there a way to do that replacements in python? and pass the final string. if it is, so we can use re.escape .

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

When the value to be matched is a string, re.escape() handles it. This code is only for the case when regexMatch's regex is a column.

Escape $regexMatch special characters instead of LIKE special chars.
@timgraham timgraham force-pushed the pattern-lookup-col-escaping branch from 2f108e0 to 72a20d4 Compare July 5, 2024 13:36
@timgraham timgraham merged commit 72a20d4 into mongodb:main Jul 5, 2024
3 checks passed
@timgraham timgraham deleted the pattern-lookup-col-escaping branch July 5, 2024 13:49
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