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>
This commit is contained in:
Mariusz Felisiak 2024-01-13 14:33:20 -05:00
parent d074c7530b
commit f3d10546a8
3 changed files with 10 additions and 1 deletions

View File

@ -402,10 +402,13 @@ class BaseExpression:
return clone
def replace_expressions(self, replacements):
if not replacements:
return self
if replacement := replacements.get(self):
return replacement
if not (source_expressions := self.get_source_expressions()):
return self
clone = self.copy()
source_expressions = clone.get_source_expressions()
clone.set_source_expressions(
[
expr.replace_expressions(replacements) if expr else None

View File

@ -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.

View File

@ -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)