Skip to content

fix(django-google-spanner): use string functions for text lookups - #19250

Open
olavloite wants to merge 1 commit into
mainfrom
spanner-django-string-functions
Open

olavloite wants to merge 1 commit into
mainfrom
spanner-django-string-functions

Conversation

@olavloite

Copy link
Copy Markdown
Contributor

Previously, Django text lookups such as iexact, contains, icontains, startswith, istartswith, endswith, and iendswith were translated into Spanner regular expression checks (REGEXP_CONTAINS). When matching against expressions, column references, UUID fields, or transformed values, those values were not regex-escaped—causing characters like .* or [ to act as regular expression operators or fail with invalid regex errors. For iexact queries on expressions, it also mistakenly treated the left-hand column as the regex pattern.

Switch these lookups to native literal string operations in Spanner SQL:

  • Case-folded equality using UPPER(...) for iexact
  • STRPOS(...) > 0 for contains and icontains
  • STARTS_WITH(...) and ENDS_WITH(...) for startswith, istartswith, endswith, and iendswith

Because these functions compare text literally rather than as regular expressions, metacharacters, newlines, and non-string scalar values behave consistently across literal values, expressions, subqueries, transforms, and check constraints, while regex and iregex lookups continue to evaluate regular expressions.

Previously, Django text lookups such as iexact, contains, icontains,
startswith, istartswith, endswith, and iendswith were translated into
Spanner regular expression checks (REGEXP_CONTAINS). When matching against
expressions, column references, UUID fields, or transformed values, those
values were not regex-escaped—causing characters like `.*` or `[` to act as
regular expression operators or fail with invalid regex errors. For iexact
queries on expressions, it also mistakenly treated the left-hand column as
the regex pattern.

Switch these lookups to native literal string operations in Spanner SQL:
- Case-folded equality using UPPER(...) for iexact
- STRPOS(...) > 0 for contains and icontains
- STARTS_WITH(...) and ENDS_WITH(...) for startswith, istartswith,
  endswith, and iendswith

Because these functions compare text literally rather than as regular
expressions, metacharacters, newlines, and non-string scalar values behave
consistently across literal values, expressions, subqueries, transforms,
and check constraints, while regex and iregex lookups continue to evaluate
regular expressions.
@olavloite
olavloite requested a review from a team as a code owner November 27, 2026 17:17

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors Django Spanner's pattern and case-insensitive lookups (such as iexact, contains, startswith, and endswith) to use literal Spanner string functions like STRPOS, STARTS_WITH, ENDS_WITH, UPPER, and CAST instead of regular expressions (REGEXP_CONTAINS). This ensures that regex metacharacters are treated literally. Additionally, prep_for_like_query and prep_for_iexact_query have been updated to return values unmodified, and comprehensive tests have been added to verify the new behavior. No review comments were provided, and the implementation is clean and well-tested.

This branch has not been deployed

No deployments
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.

1 participant