mirror of
https://github.com/django/django.git
synced 2025-08-02 18:13:02 +00:00
Fixed #31507 -- Added QuerySet.exists() optimizations to compound queries.
This commit is contained in:
parent
7b42d34646
commit
ba42569d5c
3 changed files with 35 additions and 8 deletions
|
@ -3,6 +3,7 @@ import operator
|
|||
from django.db import DatabaseError, NotSupportedError, connection
|
||||
from django.db.models import Exists, F, IntegerField, OuterRef, Value
|
||||
from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
|
||||
from django.test.utils import CaptureQueriesContext
|
||||
|
||||
from .models import Number, ReservedName
|
||||
|
||||
|
@ -257,7 +258,19 @@ class QuerySetSetOperationTests(TestCase):
|
|||
def test_exists_union(self):
|
||||
qs1 = Number.objects.filter(num__gte=5)
|
||||
qs2 = Number.objects.filter(num__lte=5)
|
||||
self.assertIs(qs1.union(qs2).exists(), True)
|
||||
with CaptureQueriesContext(connection) as context:
|
||||
self.assertIs(qs1.union(qs2).exists(), True)
|
||||
captured_queries = context.captured_queries
|
||||
self.assertEqual(len(captured_queries), 1)
|
||||
captured_sql = captured_queries[0]['sql']
|
||||
self.assertNotIn(
|
||||
connection.ops.quote_name(Number._meta.pk.column),
|
||||
captured_sql,
|
||||
)
|
||||
self.assertEqual(
|
||||
captured_sql.count(connection.ops.limit_offset_sql(None, 1)),
|
||||
3 if connection.features.supports_slicing_ordering_in_compound else 1
|
||||
)
|
||||
|
||||
def test_exists_union_empty_result(self):
|
||||
qs = Number.objects.filter(pk__in=[])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue