Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 3 additions & 34 deletions packages/django-google-spanner/django_spanner/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,48 +62,17 @@ class DatabaseWrapper(BaseDatabaseWrapper):
}
operators = {
"exact": "= %s",
"iexact": "REGEXP_CONTAINS(%s, %%%%s)",
# contains uses REGEXP_CONTAINS instead of LIKE to allow
# DatabaseOperations.prep_for_like_query() to do regular expression
# escaping. prep_for_like_query() is called for all the lookups that
# use REGEXP_CONTAINS except regex/iregex (see
# django.db.models.lookups.PatternLookup).
"contains": "REGEXP_CONTAINS(%s, %%%%s)",
"icontains": "REGEXP_CONTAINS(%s, %%%%s)",
"iexact": "= UPPER(CAST(%s AS STRING))",
# Pattern lookups (contains, startswith, endswith, and case-insensitive
# variants) use function-based expressions in django_spanner.lookups.
"gt": "> %s",
"gte": ">= %s",
"lt": "< %s",
"lte": "<= %s",
# Using REGEXP_CONTAINS instead of STARTS_WITH and ENDS_WITH for the
# same reasoning as described above for 'contains'.
"startswith": "REGEXP_CONTAINS(%s, %%%%s)",
"endswith": "REGEXP_CONTAINS(%s, %%%%s)",
"istartswith": "REGEXP_CONTAINS(%s, %%%%s)",
"iendswith": "REGEXP_CONTAINS(%s, %%%%s)",
"regex": "REGEXP_CONTAINS(%s, %%%%s)",
"iregex": "REGEXP_CONTAINS(%s, %%%%s)",
}

# pattern_esc is used to generate SQL pattern lookup clauses when the
# right-hand side of the lookup isn't a raw string (it might be an
# expression or the result of a bilateral transformation). In those cases,
# special characters for REGEXP_CONTAINS operators (e.g. \, *, _) must be
# escaped on database side.
pattern_esc = (
r'REPLACE(REPLACE(REPLACE({}, "\\", "\\\\"), "%%", r"\%%"), "_", r"\_")'
)

# These are all no-ops in favor of using REGEXP_CONTAINS in the customized
# lookups.
pattern_ops = {
"contains": "",
"icontains": "",
"startswith": "",
"istartswith": "",
"endswith": "",
"iendswith": "",
}

data_type_check_constraints = {
"PositiveBigIntegerField": "%(qn_column)s >= 0",
"PositiveIntegerField": "%(qn_column)s >= 0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def introspected_field_types(self):
# TIMESTAMP.
can_introspect_time_field = False
closed_cursor_error_class = InterfaceError
# Spanner uses REGEXP_CONTAINS which is case-sensitive.
has_case_insensitive_like = False
# https://cloud.google.com/spanner/quotas#query_limits
max_query_params = 900
Expand Down
203 changes: 85 additions & 118 deletions packages/django-google-spanner/django_spanner/lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd

from typing import TYPE_CHECKING, Any, Tuple

from django.db.models.fields.json import (
HasKeyLookup,
KeyTextTransform,
KeyTransform,
KeyTransformIExact,
)
from django.db.models.lookups import (
Contains,
Expand All @@ -22,107 +25,104 @@
IStartsWith,
LessThan,
LessThanOrEqual,
Lookup,
Regex,
StartsWith,
)

if TYPE_CHECKING:
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.models.sql.compiler import SQLCompiler


def _pattern_lookup_sql(
lookup: "Lookup",
compiler: "SQLCompiler",
connection: "BaseDatabaseWrapper",
*,
strip_left: int,
strip_right: int,
) -> Tuple[str, str, Tuple[Any, ...]]:
"""Compile a string pattern lookup using literal Spanner string functions.

Django's PatternLookup.process_rhs wraps direct scalar values with leading
or trailing '%' wildcards. Strip those added wildcards when present and
cast the RHS expression to STRING (wrapped in UPPER for case-insensitive
lookups) so equality, STRPOS, STARTS_WITH, and ENDS_WITH treat all
characters literally.
"""
lhs_sql, lhs_params = lookup.process_lhs(compiler, connection)
# For plain and JSON iexact lookups, call Lookup.process_rhs directly:
# Spanner needs neither prep_for_iexact_query nor KeyTransformIExact's
# MySQL-only logic, and IExact.process_rhs crashes when a subquery returns
# tuple params and tries to assign params[0] in place.
if isinstance(lookup, IExact) and type(lookup).process_rhs in (
IExact.process_rhs,
KeyTransformIExact.process_rhs,
):
rhs_sql, rhs_params = Lookup.process_rhs(lookup, compiler, connection)
else:
rhs_sql, rhs_params = lookup.process_rhs(compiler, connection)
params = list(lhs_params)
params.extend(rhs_params)
# For direct Python values (e.g. float or Decimal), send str(val) so
# CAST(%s AS STRING) preserves formatting and works across parameter types,
# after stripping any '%' wildcards Django added. SQL expressions handle
# their own casts.
if lookup.rhs_is_direct_value() and rhs_params and not lookup.bilateral_transforms:
rhs_index = len(lhs_params)
value = str(params[rhs_index])
end = -strip_right if strip_right else None
params[rhs_index] = value[strip_left:end]
rhs_cast = f"CAST({rhs_sql} AS STRING)"
if lookup.lookup_name.startswith("i"):
rhs_cast = f"UPPER({rhs_cast})"
return lhs_sql, rhs_cast, tuple(params)

def contains(self, compiler, connection):
"""A method to extend Django Contains and IContains classes.

:type self: :class:`~django.db.models.lookups.Contains` or
:class:`~django.db.models.lookups.IContains`
def iexact(self, compiler, connection):
"""A method to extend Django IExact class.

:type self: :class:`~django.db.models.lookups.IExact`
:param self: the instance of the class that owns this method.

:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:type compiler: :class:`~django_spanner.compiler.SQLCompiler`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.

:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.

:rtype: tuple[str, str]
:rtype: tuple[str, tuple]
:returns: A tuple of the SQL request and parameters.
"""
lhs_sql, lhs_params = self.process_lhs(compiler, connection)
rhs_sql, rhs_params = self.process_rhs(compiler, connection)
params = list(lhs_params)
params.extend(rhs_params)
is_icontains = self.lookup_name.startswith("i")
if self.rhs_is_direct_value() and rhs_params and not self.bilateral_transforms:
rhs_sql = self.get_rhs_op(connection, rhs_sql)
rhs_idx = len(lhs_params)
# Chop the leading and trailing percent signs that Django adds to the
# param since this isn't a LIKE query as Django expects.
params[rhs_idx] = params[rhs_idx][1:-1]
# Add the case insensitive flag for icontains.
if is_icontains:
params[rhs_idx] = "(?i)" + params[rhs_idx]
# rhs_sql is REGEXP_CONTAINS(%s, %%s), and lhs_sql is the column name.
return rhs_sql % lhs_sql, tuple(params)
else:
# rhs_sql is the expression/column to use as the base of the regular
# expression.
if is_icontains:
rhs_sql = "CONCAT('(?i)', " + rhs_sql + ")"
return (
"REGEXP_CONTAINS(%s, %s)"
% (lhs_sql, connection.pattern_esc.format(rhs_sql)),
tuple(params),
)
lhs_sql, rhs_cast, params = _pattern_lookup_sql(
self, compiler, connection, strip_left=0, strip_right=0
)
return f"{lhs_sql} = {rhs_cast}", params


def iexact(self, compiler, connection):
"""A method to extend Django IExact class. Case-insensitive exact match.
If the value provided for comparison is None, it will be interpreted as
an SQL NULL.
def contains(self, compiler, connection):
"""A method to extend Django Contains and IContains classes.

:type self: :class:`~django.db.models.lookups.IExact`
:type self: :class:`~django.db.models.lookups.Contains` or
:class:`~django.db.models.lookups.IContains`
:param self: the instance of the class that owns this method.

:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:type compiler: :class:`~django_spanner.compiler.SQLCompiler`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.

:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.

:rtype: tuple[str, str]
:rtype: tuple[str, tuple]
:returns: A tuple of the SQL request and parameters.
"""
lhs_sql, lhs_params = self.process_lhs(compiler, connection)
rhs_sql, rhs_params = self.process_rhs(compiler, connection)
params = list(lhs_params)
params.extend(rhs_params)
rhs_sql = self.get_rhs_op(connection, rhs_sql)
# Wrap the parameter in ^ and $ to restrict the regex to an exact match.
if self.rhs_is_direct_value() and rhs_params and not self.bilateral_transforms:
rhs_idx = len(lhs_params)
params[rhs_idx] = "^(?i)%s$" % params[rhs_idx]
else:
# 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 + ", '$')"
if not self.rhs_is_direct_value():
# If rhs is not a direct value
if not params:
# if params is not present, then we have only 1 formatable
# argument in rhs_sql.
rhs_sql = rhs_sql.replace("%%s", "%s")
else:
# If params is present and rhs_sql is to be replaced as well.
# Example: model_fields.test_uuid.TestQuerying.test_iexact.
rhs_sql = rhs_sql.replace("%%s", "__PLACEHOLDER_FOR_LHS_SQL__")
rhs_sql = rhs_sql.replace("%s", "%%s")
rhs_sql = rhs_sql.replace("__PLACEHOLDER_FOR_LHS_SQL__", "%s")
# rhs_sql is REGEXP_CONTAINS(%s, %%s), and lhs_sql is the column name.
return rhs_sql % lhs_sql, tuple(params)
lhs_sql, rhs_cast, params = _pattern_lookup_sql(
self, compiler, connection, strip_left=1, strip_right=1
)
return f"STRPOS({lhs_sql}, {rhs_cast}) > 0", params


def regex(self, compiler, connection):
Expand All @@ -132,17 +132,14 @@ def regex(self, compiler, connection):
:class:`~django.db.models.lookups.IRegex`
:param self: the instance of the class that owns this method.

:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:type compiler: :class:`~django_spanner.compiler.SQLCompiler`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.

:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.

:rtype: tuple[str, str]
:rtype: tuple[str, tuple]
:returns: A tuple of the SQL request and parameters.
"""
lhs_sql, lhs_params = self.process_lhs(compiler, connection)
Expand Down Expand Up @@ -177,56 +174,26 @@ def startswith_endswith(self, compiler, connection):
:class:`~django.db.models.lookups.IEndsWith`
:param self: the instance of the class that owns this method.

:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:type compiler: :class:`~django_spanner.compiler.SQLCompiler`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.

:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.

:rtype: tuple[str, str]
:rtype: tuple[str, tuple]
:returns: A tuple of the SQL request and parameters.
"""
lhs_sql, lhs_params = self.process_lhs(compiler, connection)
rhs_sql, rhs_params = self.process_rhs(compiler, connection)
params = list(lhs_params)
params.extend(rhs_params)
is_startswith = "startswith" in self.lookup_name
is_endswith = "endswith" in self.lookup_name
is_insensitive = self.lookup_name.startswith("i")
# Chop the leading (endswith) or trailing (startswith) percent sign that
# Django adds to the param since this isn't a LIKE query as Django expects.
if self.rhs_is_direct_value() and rhs_params and not self.bilateral_transforms:
rhs_sql = self.get_rhs_op(connection, rhs_sql)
rhs_idx = len(lhs_params)
if is_endswith:
params[rhs_idx] = str(params[rhs_idx][1:]) + "$"
else:
params[rhs_idx] = "^" + str(params[rhs_idx][:-1])
# Add the case insensitive flag for istartswith or iendswith.
if is_insensitive:
params[rhs_idx] = "(?i)" + params[rhs_idx]
# rhs_sql is REGEXP_CONTAINS(%s, %%s), and lhs_sql is the column name.
return rhs_sql % lhs_sql, tuple(params)
else:
# rhs_sql is the expression/column to use as the base of the regular
# expression.
sql = "CONCAT('"
if is_startswith:
sql += "^"
if is_insensitive:
sql += "(?i)"
sql += "', " + rhs_sql
if is_endswith:
sql += ", '$'"
sql += ")"
return (
"REGEXP_CONTAINS(%s, %s)" % (lhs_sql, connection.pattern_esc.format(sql)),
tuple(params),
)
lhs_sql, rhs_cast, params = _pattern_lookup_sql(
self,
compiler,
connection,
strip_left=1 if is_endswith else 0,
strip_right=0 if is_endswith else 1,
)
function = "ENDS_WITH" if is_endswith else "STARTS_WITH"
return f"{function}({lhs_sql}, {rhs_cast})", params


def cast_param_to_float(self, compiler, connection):
Expand Down
Loading
Loading