Skip to content
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

fix: iexact lookup with Transform expression crash issue when RHS is direct value and a transform function is involved #628

Merged
merged 12 commits into from May 20, 2021
Merged
5 changes: 4 additions & 1 deletion django_spanner/lookups.py
Expand Up @@ -100,7 +100,10 @@ def iexact(self, compiler, connection):
# lhs_sql is the expression/column to use as the regular expression.
# Use concat to make the value case-insensitive.
lhs_sql = "CONCAT('^(?i)', " + lhs_sql + ", '$')"
rhs_sql = rhs_sql.replace("%%s", "%s")
if not self.rhs_is_direct_value() and not params:
# If rhs is not a direct value and parameter is not present we want
# to have only 1 formatable argument in rhs_sql else we need 2.
rhs_sql = rhs_sql.replace("%%s", "%s")
# rhs_sql is REGEXP_CONTAINS(%s, %%s), and lhs_sql is the column name.
return rhs_sql % lhs_sql, params

Expand Down
14 changes: 14 additions & 0 deletions tests/unit/django_spanner/test_lookups.py
Expand Up @@ -283,3 +283,17 @@ def test_iexact_sql_query_case_insensitive_function_transform(self):
+ "CONCAT('^(?i)', CAST(UPPER(tests_author.name) AS STRING), '$'))",
)
self.assertEqual(params, ())

def test_iexact_sql_query_case_insensitive_value_match(self):

qs1 = Author.objects.filter(name__upper__iexact="abc").values("name")
compiler = SQLCompiler(qs1.query, self.connection, "default")
sql_compiled, params = compiler.as_sql()

self.assertEqual(
sql_compiled,
"SELECT tests_author.name FROM tests_author WHERE "
+ "REGEXP_CONTAINS((UPPER(CONCAT('^(?i)', "
+ "CAST(UPPER(tests_author.name) AS STRING), '$'))), %s)",
)
self.assertEqual(params, ("abc",))