From 2772b57ff0a2b7676636e70a85b64c9d2d0efe16 Mon Sep 17 00:00:00 2001 From: Vikash Singh <3116482+vi3k6i5@users.noreply.github.com> Date: Thu, 20 May 2021 12:05:44 +0530 Subject: [PATCH] fix: iexact lookup with Transform expression crash issue when RHS is 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 --- django_spanner/lookups.py | 5 ++++- tests/unit/django_spanner/test_lookups.py | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/django_spanner/lookups.py b/django_spanner/lookups.py index c2e642d26a..d9a54982f2 100644 --- a/django_spanner/lookups.py +++ b/django_spanner/lookups.py @@ -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 diff --git a/tests/unit/django_spanner/test_lookups.py b/tests/unit/django_spanner/test_lookups.py index 1931d255aa..53604691cc 100644 --- a/tests/unit/django_spanner/test_lookups.py +++ b/tests/unit/django_spanner/test_lookups.py @@ -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",))