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:00 -0400
commit85fa363c846f4ed287565c43c32e2cca29470e25 (patch)
tree21205f1e8e2064f4527b81adb402ddd9c0e20d38 /lib/sqlalchemy/sql/compiler.py
parentf08576274417bb1a836c072ff240bd5ad2ab40b7 (diff)
downloadsqlalchemy-85fa363c846f4ed287565c43c32e2cca29470e25.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
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 6d9d47388..8c2699879 100644
--- a/lib/sqlalchemy/sql/compiler.py
+++ b/lib/sqlalchemy/sql/compiler.py
@@ -3196,10 +3196,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