summaryrefslogtreecommitdiff
path: root/django/db/models/sql
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2024-01-13 14:33:20 -0500
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2024-01-15 05:56:38 +0100
commitf3d10546a850df4fe3796f972d5b7e16adf52f54 (patch)
tree91bf01575742ce83d2dd38f10fa0f03451e8ef49 /django/db/models/sql
parentd074c7530b812a7b78c9a201c8a70f281ed3a36e (diff)
Refs #35102 -- Optimized replace_expressions()/relabelling aliases by adding early return.
This avoids costly hashing. Thanks Anthony Shaw for the report. Co-Authored-By: Simon Charette <charette.s@gmail.com>
Diffstat (limited to 'django/db/models/sql')
-rw-r--r--django/db/models/sql/query.py2
-rw-r--r--django/db/models/sql/where.py4
2 files changed, 6 insertions, 0 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index a79d66eb21..ce4fafb1e2 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -972,6 +972,8 @@ class Query(BaseExpression):
relabelling any references to them in select columns and the where
clause.
"""
+ if not change_map:
+ return self
# If keys and values of change_map were to intersect, an alias might be
# updated twice (e.g. T4 -> T5, T5 -> T6, so also T4 -> T6) depending
# on their order in change_map.
diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py
index 2f23a2932c..8423fcb528 100644
--- a/django/db/models/sql/where.py
+++ b/django/db/models/sql/where.py
@@ -204,6 +204,8 @@ class WhereNode(tree.Node):
Relabel the alias values of any children. 'change_map' is a dictionary
mapping old (current) alias values to the new values.
"""
+ if not change_map:
+ return self
for pos, child in enumerate(self.children):
if hasattr(child, "relabel_aliases"):
# For example another WhereNode
@@ -225,6 +227,8 @@ class WhereNode(tree.Node):
return clone
def replace_expressions(self, replacements):
+ if not replacements:
+ return self
if replacement := replacements.get(self):
return replacement
clone = self.create(connector=self.connector, negated=self.negated)