summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/compiler.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2022-08-05 17:25:05 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2022-08-05 17:26:49 -0400
commit7914b6491b31e07d2aa0313a97a0ded27627da07 (patch)
tree541d8247578af79dc69910cfa7df550914ea86f6 /lib/sqlalchemy/sql/compiler.py
parentbb954d40a12c81b1867474d7c6fd300a31382ec2 (diff)
downloadsqlalchemy-7914b6491b31e07d2aa0313a97a0ded27627da07.tar.gz
deep compare CTEs before considering them conflicting
Fixed issue where referencing a CTE multiple times in conjunction with a polymorphic SELECT could result in multiple "clones" of the same CTE being constructed, which would then trigger these two CTEs as duplicates. To resolve, the two CTEs are deep-compared when this occurs to ensure that they are equivalent, then are treated as equivalent. Fixes: #8357 Change-Id: I1f634a9cf7a6c4256912aac1a00506aecea3b0e2 (cherry picked from commit 85fa363c846f4ed287565c43c32e2cca29470e25)
Diffstat (limited to 'lib/sqlalchemy/sql/compiler.py')
-rw-r--r--lib/sqlalchemy/sql/compiler.py17
1 files changed, 13 insertions, 4 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
index 330f3c3bc..c9b6ba670 100644
--- a/lib/sqlalchemy/sql/compiler.py
+++ b/lib/sqlalchemy/sql/compiler.py
@@ -2708,10 +2708,19 @@ class SQLCompiler(Compiled):
del self.level_name_by_cte[existing_cte_reference_cte]
else:
- raise exc.CompileError(
- "Multiple, unrelated CTEs found with "
- "the same name: %r" % cte_name
- )
+ # if the two CTEs are deep-copy identical, consider them
+ # the same, **if** they are clones, that is, they came from
+ # the ORM or other visit method
+ if (
+ cte._is_clone_of is not None
+ or existing_cte._is_clone_of is not None
+ ) and cte.compare(existing_cte):
+ is_new_cte = False
+ else:
+ raise exc.CompileError(
+ "Multiple, unrelated CTEs found with "
+ "the same name: %r" % cte_name
+ )
if not asfrom and not is_new_cte:
return None