10000 Fixed #36148 -- Enabled native tuple comparison lookups on Oracle >= 23.4 and SQLite >= 3.37. by charettes · Pull Request #19107 · django/django · GitHub
[go: up one dir, main page]

Skip to content

Fixed #36148 -- Enabled native tuple comparison lookups on Oracle >= 23.4 and SQLite >= 3.37. #19107

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

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 3 additions & 0 deletions django/db/backends/base/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ class BaseDatabaseFeatures:
# Does the backend support unlimited character columns?
supports_unlimited_charfield = False

# Does the backend support native tuple lookups (=, >, <, IN)?
supports_tuple_lookups = True

# Collation names for use by the Django test suite.
test_collations = {
"ci": None, # Case-insensitive.
Expand Down
5 changes: 5 additions & 0 deletions django/db/backends/oracle/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,8 @@ def supports_aggregation_over_interval_types(self):
@cached_property
def bare_select_suffix(self):
return "" if self.connection.oracle_version >= (23,) else " FROM DUAL"

@cached_property
def supports_tuple_lookups(self):
# Support is known to be missing on 23.2 but available on 23.4.
return self.connection.oracle_version >= (23, 4)
64 changes: 42 additions & 22 deletions django/db/models/fields/tuple_lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ def __len__(self):
def __iter__(self):
return iter(self.source_expressions)

def as_sqlite(self, compiler, connection):
if connection.get_database_version() < (3, 37) and isinstance(
first_expr := self.source_expressions[0], Tuple
):
first_expr = first_expr.copy()
first_expr.function = "VALUES"
return Tuple(first_expr, *self.source_expressions[1:]).as_sql(
compiler, connection
)
return self.as_sql(compiler, connection)


class TupleLookupMixin:
allows_composite_expressions = True
Expand Down Expand Up @@ -87,7 +98,7 @@ def process_rhs(self, compiler, connection):
Value(val, output_field=col.output_field)
for col, val in zip(self.lhs, self.rhs)
]
return Tuple(*args).as_sql(compiler, connection)
return compiler.compile(Tuple(*args))
else:
sql, params = compiler.compile(self.rhs)
if not isinstance(self.rhs, ColPairs):
Expand All @@ -96,9 +107,20 @@ def process_rhs(self, compiler, connection):
)
return "(%s)" % sql, params

def get_fallback_sql(self, compiler, connection):
raise NotImplementedError(
f"{self.__class__.__name__}.get_fallback_sql() must be implemented "
f"for backends that don't have the supports_tuple_lookups feature enabled."
)

def as_sql(self, compiler, connection):
if not connection.features.supports_tuple_lookups:
return self.get_fallback_sql(compiler, connection)
return super().as_sql(compiler, connection)


class TupleExact(TupleLookupMixin, Exact):
def as_oracle(self, compiler, connection):
def get_fallback_sql(self, compiler, connection):
# Process right-hand-side to trigger sanitization.
self.process_rhs(compiler, connection)
# e.g.: (a, b, c) == (x, y, z) as SQL:
Expand Down Expand Up @@ -132,7 +154,7 @@ def as_sql(self, compiler, connection):


class TupleGreaterThan(TupleLookupMixin, GreaterThan):
def as_oracle(self, compiler, connection):
def get_fallback_sql(self, compiler, connection):
# Process right-hand-side to trigger sanitization.
self.process_rhs(compiler, connection)
# e.g.: (a, b, c) > (x, y, z) as SQL:
Expand Down Expand Up @@ -160,7 +182,7 @@ def as_oracle(self, compiler, connection):


class TupleGreaterThanOrEqual(TupleLookupMixin, GreaterThanOrEqual):
def as_oracle(self, compiler, connection):
def get_fallback_sql(self, compiler, connection):
# Process right-hand-side to trigger sanitization.
self.process_rhs(compiler, connection)
# e.g.: (a, b, c) >= (x, y, z) as SQL:
Expand Down Expand Up @@ -188,7 +210,7 @@ def as_oracle(self, compiler, connection):


class TupleLessThan(TupleLookupMixin, LessThan):
def as_oracle(self, compiler, connection):
def get_fallback_sql(self, compiler, connection):
# Process right-hand-side to trigger sanitization.
self.process_rhs(compiler, connection)
# e.g.: (a, b, c) < (x, y, z) as SQL:
Expand Down Expand Up @@ -216,7 +238,7 @@ def as_oracle(self, compiler, connection):


class TupleLessThanOrEqual(TupleLookupMixin, LessThanOrEqual):
def as_oracle(self, compiler, connection):
def get_fallback_sql(self, compiler, connection):
# Process right-hand-side to trigger sanitization.
self.process_rhs(compiler, connection)
# e.g.: (a, b, c) <= (x, y, z) as SQL:
Expand Down Expand Up @@ -313,19 +335,21 @@ def process_rhs(self, compiler, connection):
)
)

return Tuple(*result).as_sql(compiler, connection)
return compiler.compile(Tuple(*result))

def as_sql(self, compiler, connection):
if not self.rhs_is_direct_value():
return self.as_subquery(compiler, connection)
return super().as_sql(compiler, connection)
def as_subquery_sql(self, compiler, connection):
lhs = self.lhs
rhs = self.rhs
if isinstance(lhs, ColPairs):
rhs = rhs.clone()
rhs.set_values([source.name for source in lhs.sources])
lhs = Tuple(lhs)
return compiler.compile(In(lhs, rhs))

def as_sqlite(self, compiler, connection):
def get_fallback_sql(self, compiler, connection):
rhs = self.rhs
if not rhs:
raise EmptyResultSet
if not self.rhs_is_direct_value():
return self.as_subquery(compiler, connection)

# e.g.: (a, b, c) in [(x1, y1, z1), (x2, y2, z2)] as SQL:
# WHERE (a = x1 AND b = y1 AND c = z1) OR (a = x2 AND b = y2 AND c = z2)
Expand All @@ -338,14 +362,10 @@ def as_sqlite(self, compiler, connection):

return root.as_sql(compiler, connection)

def as_subquery(self, compiler, connection):
lhs = self.lhs
rhs = self.rhs
if isinstance(lhs, ColPairs):
rhs = rhs.clone()
rhs.set_values([source.name for source in lhs.sources])
lhs = Tuple(lhs)
return compiler.compile(In(lhs, rhs))
def as_sql(self, compiler, connection):
if not self.rhs_is_direct_value():
return self.as_subquery_sql(compiler, connection)
return super().as_sql(compiler, connection)


tuple_lookups = {
Expand Down
0