FIX: escape '[' wildcard in F() expression pattern lookups - #575
FIX: escape '[' wildcard in F() expression pattern lookups#575KHAN (Khan3K) wants to merge 1 commit into
Conversation
Pattern lookups (__contains, __startswith, __endswith and their case-insensitive variants) whose search term comes from a column reference (F()) escape backslash, percent, and underscore on the database side via DatabaseWrapper.pattern_esc, but not the '[' character-class wildcard. Values containing square brackets therefore match incorrectly and silently (e.g. '[J]ohnny' acts as a character class matching 'J'). Escape '[' as '[[]' in pattern_esc, mirroring the existing literal-pattern escaping in DatabaseOperations.prep_for_like_query. A bare ']' outside a character class is already literal in SQL Server LIKE, so no additional escaping is required. Fixes microsoft#573
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
Pull request overview
This PR fixes incorrect SQL Server LIKE behavior for pattern lookups (__contains, __startswith, __endswith and case-insensitive variants) when the pattern comes from an expression (notably F() column references). It updates the backend’s expression-side escaping to treat [ literally (matching SQL Server’s own literal-pattern escaping behavior), and adds regression tests plus re-enables the upstream Django test that was previously excluded.
Changes:
- Escape
[inDatabaseWrapper.pattern_escto prevent SQL ServerLIKEcharacter-class wildcard mis-matches for expression-sourced patterns. - Add regression tests covering
__contains/__startswithwithF()patterns containing bracketed values. - Remove the now-unnecessary exclusion of
expressions.tests.ExpressionsTests.test_patterns_escape.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
mssql/base.py |
Escapes [ in expression-side LIKE pattern escaping (pattern_esc) to ensure literal bracket matching. |
testapp/tests/test_expressions.py |
Adds regression tests for bracket escaping in F()-driven pattern lookups. |
testapp/settings.py |
Removes the excluded Django test now that bracket escaping is implemented. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
@microsoft-github-policy-service agree |
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
📊 Code Coverage Report
Diff CoverageDiff: dev...HEAD, staged and unstaged changesNo lines with coverage information in this diff. 📋 Files Needing Attention📉 Files with overall lowest coverage (click to expand)- mssql/client.py: 19.5% (41 lines)
- mssql/__init__.py: 50.0% (2 lines)
- mssql/creation.py: 56.8% (74 lines)
- mssql/operations.py: 78.8% (396 lines)
- mssql/compiler.py: 82.3% (644 lines)
- mssql/functions.py: 84.1% (428 lines)
- mssql/base.py: 84.8% (514 lines)
- mssql/schema.py: 88.3% (719 lines)
- mssql/introspection.py: 90.2% (133 lines)
- mssql/features.py: 98.9% (88 lines)🔗 Quick Links
|
Adds bracket escaping to
DatabaseWrapper.pattern_esc, which is applied to__contains,__startswith,__endswithand their case-insensitive variants when the search term comes from anF()expression (column reference).[is the SQL Server LIKE character-class wildcard. Before this change, values containing[were matched as a character class, so a row likefirstname='[J]ohnny'matchedfirstname__contains=F("lastname")wherelastname='[J]ohnny'— silently wrong results.]outside a character class is already literal in SQL Server LIKE and needs no escaping.Mirrors the existing literal-term escaping in
DatabaseOperations.prep_for_like_query, which already escapes[as[[].Adds regression tests for
__containsand__startswithwith anF()expression, and removes theexpressions.tests.ExpressionsTests.test_patterns_escapeexclusion from the test settings (it now passes).Tested with:
Fixes #573