Skip to content

Commit

Permalink
fix: iexact lookup with Transform expression crash issue when RHS is …
Browse files Browse the repository at this point in the history
…direct value and a transform function is involved (#628)

* fix: lint_setup_py was failing in Kokoro is not fixed

* fix: iexact lookup with Transform expression chash issue #612
  • Loading branch information
vi3k6i5 authored May 20, 2021
1 parent 3a8d922 commit 2772b57
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion django_spanner/lookups.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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",))

0 comments on commit 2772b57

Please sign in to comment.